| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 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 |
|
| 39 |
|
| 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 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 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 |
|
| 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) { |
| 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 |
|