Debian Codegraph @

src/dr/evomodel/treelikelihood/utilities/DnDsLogger.java

raw symbols SCIP
1 /*
2 * DnDsLogger.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.evomodel.treelikelihood.utilities;
27
28 import dr.evomodel.substmodel.CodonLabeling;
29 import dr.evomodel.substmodel.CodonPartitionedRobustCounting;
30 import dr.evolution.tree.Tree;
31 import dr.evolution.tree.TreeTrait;
32 import dr.inference.loggers.LogColumn;
33 import dr.inference.loggers.Loggable;
34 import dr.inference.loggers.NumberColumn;
35 import dr.math.EmpiricalBayesPoissonSmoother;
36
37 /**
38 * @author Philippe Lemey
39 * @author Marc A. Suchard
40 */
41 public class DnDsLogger implements Loggable {
42
43 public DnDsLogger(String name, Tree tree, TreeTrait[] traits, boolean useSmoothing, boolean useDnMinusDs, boolean counts, boolean synonymous) {
44 this.tree = tree;
45 this.traits = traits;
46 numberSites = getNumberSites();
47 this.name = name;
48 this.useSmoothing = useSmoothing;
49 this.useDnMinusDs = useDnMinusDs;
50 this.counts = counts;
51 this.synonymous = synonymous;
52
53 for (int i = 0; i < NUM_TRAITS; i++) {
54 if (traits[i].getIntent() != TreeTrait.Intent.WHOLE_TREE) {
55 throw new IllegalArgumentException("Only whole tree traits are currently supported in DnDsLogger");
56 }
57 }
58 }
59
60 // public LogColumn[] getColumns() {
61 // LogColumn[] columns;
62 //// if (conditionalCounts) {
63 // columns = new LogColumn[numberSites * 3];
64 //// } else {
65 //// columns = new LogColumn[numberSites];
66 //// }
67 // int columnCount = 0;
68 //// if (!conditionalCounts){
69 // for (int i = 0; i < numberSites; i++) {
70 // columns[columnCount] = new SmoothedColumn(name, i);
71 // columnCount ++;
72 // }
73 //// } else {
74 // for (int j = 0; j < numberSites; j++) {
75 // //CN and CS are outputted per site
76 // columns[columnCount] = new ConditionalColumn(conditionalNon, j, true);
77 // columns[columnCount + 1] = new ConditionalColumn(conditionalSyn, j, false);
78 // columnCount = columnCount + 2;
79 // }
80 //// }
81 // return columns;
82 // }
83
84 public LogColumn[] getColumns() {
85 LogColumn[] columns;
86 if (!counts){
87 columns = new LogColumn[numberSites];
88 } else {
89 columns = new LogColumn[numberSites * 2];
90 }
91 int columnCount = 0;
92 if (!counts){
93 for (int i = 0; i < numberSites; i++) {
94 columns[columnCount] = new SmoothedColumn(name, i);
95 columnCount ++;
96 }
97 } else {
98 for (int j = 0; j < numberSites; j++) {
99 //CN and CS are outputted per site
100 if (synonymous){
101 columns[columnCount] = new ConditionalColumn(conditionalSyn, j, false, true);
102 } else {
103 columns[columnCount] = new ConditionalColumn(conditionalNon, j, true, true);
104 }
105 columnCount ++;
106 }
107 for (int k = 0; k < numberSites; k++) {
108 if (synonymous){
109 columns[columnCount] = new ConditionalColumn(unconditionalSyn, k, false, false);
110 } else {
111 columns[columnCount] = new ConditionalColumn(unconditionalNon, k, true, false);
112 }
113 columnCount ++;
114
115 }
116
117 }
118 return columns;
119 }
120
121 private class ConditionalColumn extends NumberColumn {
122 private final int index;
123 boolean nonsynonymous = false;
124 boolean conditional = true;
125
126 public ConditionalColumn(String label, int index, boolean N, boolean C) {
127 super(label + "[" + (index+1) + "]");
128 this.index = index;
129 if (N){
130 nonsynonymous = true;
131 }
132 if (!C){
133 conditional = false;
134 }
135 }
136
137 @Override
138 public double getDoubleValue() {
139 if (index == 0) {
140 doSmoothing();
141 }
142 if (conditional){
143 if (nonsynonymous){
144 return cachedValues[CN][index];
145 } else {
146 return cachedValues[CS][index];
147 }
148 } else {
149 if (nonsynonymous){
150 return cachedValues[UN][index];
151 } else {
152 return cachedValues[US][index];
153 }
154 }
155 }
156 }
157
158 private class SmoothedColumn extends NumberColumn {
159
160 private final int index;
161
162 public SmoothedColumn(String label, int index) {
163 super(label + "[" + (index+1) + "]");
164 this.index = index;
165 }
166
167 @Override
168 public double getDoubleValue() {
169 if (index == 0) { // Assumes that columns are accessed IN ORDER
170 doSmoothing();
171 }
172 return doCalculation(index);
173 }
174 }
175
176 private double doCalculation(int index) {
177 double returnValue;
178 if (!useDnMinusDs) {
179 returnValue = (cachedValues[CN][index] / cachedValues[UN][index]) /
180 (cachedValues[CS][index] / cachedValues[US][index]);
181
182 } else {
183 returnValue = (cachedValues[CN][index] / cachedValues[UN][index]) -
184 (cachedValues[CS][index] / cachedValues[US][index]);
185 }
186 return returnValue;
187 }
188
189 private int getNumberSites() {
190 double[] values = (double[]) traits[0].getTrait(tree, tree.getRoot());
191 return values.length;
192 }
193
194 private void doSmoothing() {
195
196 if (cachedValues == null) {
197 cachedValues = new double[NUM_TRAITS][];
198 }
199
200 for (int i = 0; i < NUM_TRAITS; i++) {
201 if (useSmoothing) {
202 cachedValues[i] = EmpiricalBayesPoissonSmoother.smooth((double[]) traits[i].getTrait(tree, tree.getRoot()));
203 } else {
204 cachedValues[i] = (double[]) traits[i].getTrait(tree, tree.getRoot());
205 }
206 }
207 }
208
209 private final TreeTrait[] traits;
210 private final Tree tree;
211 private final int numberSites;
212 private final String name;
213 private final String conditionalNon = "CN";
214 private final String conditionalSyn = "CS";
215 private final String unconditionalNon = "UN";
216 private final String unconditionalSyn = "US";
217 private final boolean useSmoothing;
218 private final boolean useDnMinusDs;
219 private final boolean counts;
220 private final boolean synonymous;
221
222 private final static int NUM_TRAITS = 4;
223 private final static int CS = 0;
224 private final static int US = 1;
225 private final static int CN = 2;
226 private final static int UN = 3;
227
228 private double[][] cachedValues;
229
230 public static String[] traitNames = new String[] {
231 CodonPartitionedRobustCounting.SITE_SPECIFIC_PREFIX + CodonLabeling.SYN.getText(),
232 CodonPartitionedRobustCounting.UNCONDITIONED_PREFIX + CodonLabeling.SYN.getText(),
233 CodonPartitionedRobustCounting.SITE_SPECIFIC_PREFIX + CodonLabeling.NON_SYN.getText(),
234 CodonPartitionedRobustCounting.UNCONDITIONED_PREFIX + CodonLabeling.NON_SYN.getText()
235 };
236 }
237
238