| 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.arg; |
| 27 |
|
| 28 |
import dr.evolution.io.Importer; |
| 29 |
import dr.evolution.tree.FlexibleTree; |
| 30 |
import dr.evolution.tree.NodeRef; |
| 31 |
import dr.evolution.tree.Tree; |
| 32 |
import dr.evolution.tree.TreeUtils; |
| 33 |
import dr.evoxml.util.GraphMLUtils; |
| 34 |
import dr.util.FrequencySet; |
| 35 |
import dr.util.NumberFormatter; |
| 36 |
|
| 37 |
import java.io.IOException; |
| 38 |
import java.io.Reader; |
| 39 |
import java.util.Set; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public class ARGTraceAnalysis { |
| 45 |
|
| 46 |
public ARGTraceAnalysis(ARGTrace[] trace, int burnIn, boolean verbose) { |
| 47 |
|
| 48 |
this.trace = trace; |
| 49 |
|
| 50 |
int minMaxState = Integer.MAX_VALUE; |
| 51 |
for (ARGTrace aTrace : trace) { |
| 52 |
if (aTrace.getMaximumState() < minMaxState) { |
| 53 |
minMaxState = aTrace.getMaximumState(); |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
if (burnIn < 0 || burnIn >= minMaxState) { |
| 58 |
this.burnin = minMaxState / (10 * trace[0].getStepSize()); |
| 59 |
if (verbose) |
| 60 |
System.out.println("WARNING: Burn-in larger than total number of states - using 10% of smallest trace"); |
| 61 |
} else { |
| 62 |
this.burnin = burnIn; |
| 63 |
} |
| 64 |
|
| 65 |
analyze(verbose); |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
public void analyze(boolean verbose) { |
| 73 |
|
| 74 |
if (verbose) { |
| 75 |
if (trace.length > 1) System.out.println("Combining " + trace.length + " traces."); |
| 76 |
} |
| 77 |
|
| 78 |
ARGModel arg = getARG(0); |
| 79 |
argSet = new FrequencySet<ARGModel>(); |
| 80 |
argSet.add(arg); |
| 81 |
|
| 82 |
|
| 83 |
for (ARGTrace aTrace : trace) { |
| 84 |
int treeCount = aTrace.getTreeCount(burnin * aTrace.getStepSize()); |
| 85 |
double stepSize = treeCount / 60.0; |
| 86 |
int counter = 1; |
| 87 |
|
| 88 |
if (verbose) { |
| 89 |
System.out.println("Analyzing " + treeCount + " ARGs..."); |
| 90 |
System.out.println("0 25 50 75 100"); |
| 91 |
System.out.println("|--------------|--------------|--------------|--------------|"); |
| 92 |
System.out.print("*"); |
| 93 |
} |
| 94 |
for (int i = 1; i < treeCount; i++) { |
| 95 |
arg = aTrace.getARG(i, burnin * aTrace.getStepSize()); |
| 96 |
|
| 97 |
argSet.add(arg); |
| 98 |
if (i >= (int) Math.round(counter * stepSize) && counter <= 60) { |
| 99 |
if (verbose) { |
| 100 |
System.out.print("*"); |
| 101 |
System.out.flush(); |
| 102 |
} |
| 103 |
counter += 1; |
| 104 |
} |
| 105 |
} |
| 106 |
if (verbose) { |
| 107 |
System.out.println("*"); |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
public final Tree analyzeARG(String target) { |
| 116 |
|
| 117 |
int n = getTreeCount(); |
| 118 |
|
| 119 |
FlexibleTree meanTree = null; |
| 120 |
|
| 121 |
for (int i = 0; i < n; i++) { |
| 122 |
Tree tree = getARG(i); |
| 123 |
|
| 124 |
if (TreeUtils.uniqueNewick(tree, tree.getRoot()).equals(target)) { |
| 125 |
meanTree = new FlexibleTree(tree); |
| 126 |
break; |
| 127 |
} |
| 128 |
} |
| 129 |
if (meanTree == null) throw new RuntimeException("No target tree in trace"); |
| 130 |
|
| 131 |
int m = meanTree.getInternalNodeCount(); |
| 132 |
for (int j = 0; j < m; j++) { |
| 133 |
double[] heights = new double[n]; |
| 134 |
NodeRef node1 = meanTree.getInternalNode(j); |
| 135 |
Set<String> leafSet = TreeUtils.getDescendantLeaves(meanTree, node1); |
| 136 |
|
| 137 |
for (int i = 0; i < n; i++) { |
| 138 |
Tree tree = getARG(i); |
| 139 |
|
| 140 |
NodeRef node2 = TreeUtils.getCommonAncestorNode(tree, leafSet); |
| 141 |
heights[i] = tree.getNodeHeight(node2); |
| 142 |
} |
| 143 |
meanTree.setNodeHeight(node1, dr.stats.DiscreteStatistics.mean(heights)); |
| 144 |
meanTree.setNodeAttribute(node1, "upper", new Double(dr.stats.DiscreteStatistics.quantile(0.975, heights))); |
| 145 |
meanTree.setNodeAttribute(node1, "lower", new Double(dr.stats.DiscreteStatistics.quantile(0.025, heights))); |
| 146 |
|
| 147 |
} |
| 148 |
|
| 149 |
return meanTree; |
| 150 |
} |
| 151 |
|
| 152 |
public final int getTreeCount() { |
| 153 |
|
| 154 |
int treeCount = 0; |
| 155 |
for (ARGTrace aTrace : trace) { |
| 156 |
treeCount += aTrace.getTreeCount(burnin * aTrace.getStepSize()); |
| 157 |
} |
| 158 |
return treeCount; |
| 159 |
} |
| 160 |
|
| 161 |
public final ARGModel getARG(int index) { |
| 162 |
|
| 163 |
int oldTreeCount = 0; |
| 164 |
int newTreeCount = 0; |
| 165 |
for (ARGTrace aTrace : trace) { |
| 166 |
newTreeCount += aTrace.getTreeCount(burnin * aTrace.getStepSize()); |
| 167 |
|
| 168 |
if (index < newTreeCount) { |
| 169 |
return aTrace.getARG(index - oldTreeCount, burnin * aTrace.getStepSize()); |
| 170 |
} |
| 171 |
oldTreeCount = newTreeCount; |
| 172 |
} |
| 173 |
throw new RuntimeException("Couldn't find ARG " + index); |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
public void report() throws IOException { |
| 178 |
|
| 179 |
int fieldWidth = 14; |
| 180 |
NumberFormatter formatter = new NumberFormatter(6); |
| 181 |
formatter.setPadding(true); |
| 182 |
formatter.setFieldWidth(fieldWidth); |
| 183 |
|
| 184 |
int n = argSet.size(); |
| 185 |
int totalTrees = argSet.getSumFrequency(); |
| 186 |
|
| 187 |
System.out.println(); |
| 188 |
System.out.println("burnIn=" + burnin); |
| 189 |
System.out.println("total ARGs used =" + totalTrees); |
| 190 |
System.out.println(); |
| 191 |
|
| 192 |
|
| 193 |
System.out.println("95% credible set (" + n + " unique ARGs, " + totalTrees + " total):"); |
| 194 |
System.out.println("Count\tPercent\tARG"); |
| 195 |
int credSet = (95 * totalTrees) / 100; |
| 196 |
int sumFreq = 0; |
| 197 |
|
| 198 |
NumberFormatter nf = new NumberFormatter(8); |
| 199 |
|
| 200 |
for (int i = 0; i < n; i++) { |
| 201 |
int freq = argSet.getFrequency(i); |
| 202 |
double prop = ((double) freq) / totalTrees; |
| 203 |
System.out.print(freq); |
| 204 |
System.out.print("\t" + nf.formatDecimal(prop * 100.0, 2) + "%"); |
| 205 |
|
| 206 |
sumFreq += freq; |
| 207 |
double sumProp = ((double) sumFreq) / totalTrees; |
| 208 |
System.out.print("\t" + nf.formatDecimal(sumProp * 100.0, 2) + "%"); |
| 209 |
|
| 210 |
String newickTree = |
| 211 |
argSet.get(i).toString(); |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
System.out.println("\t" + newickTree); |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
if (sumFreq >= credSet) { |
| 231 |
System.out.println(); |
| 232 |
System.out.println("95% credible set has " + (i + 1) + " ARGs."); |
| 233 |
break; |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
System.out.println(); |
| 238 |
|
| 239 |
System.out.println("DOT Format for most probable ARG:"); |
| 240 |
System.out.println(GraphMLUtils.dotFormat( |
| 241 |
argSet.get(0).toXML()) |
| 242 |
); |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
System.out.flush(); |
| 257 |
} |
| 258 |
|
| 259 |
public void shortReport(String name, Tree tree, boolean drawHeader) throws IOException { |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
} |
| 311 |
|
| 312 |
public int getBurnin() { |
| 313 |
return burnin; |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
public static ARGTraceAnalysis analyzeLogFile(Reader[] reader, int burnin, boolean verbose) throws IOException { |
| 327 |
|
| 328 |
ARGTrace[] trace = new ARGTrace[reader.length]; |
| 329 |
for (int i = 0; i < reader.length; i++) { |
| 330 |
try { |
| 331 |
trace[i] = ARGTrace.loadARGTrace(reader[i]); |
| 332 |
} catch (Importer.ImportException ie) { |
| 333 |
throw new RuntimeException(ie.toString()); |
| 334 |
} |
| 335 |
reader[i].close(); |
| 336 |
|
| 337 |
} |
| 338 |
|
| 339 |
return new ARGTraceAnalysis(trace, burnin, verbose); |
| 340 |
} |
| 341 |
|
| 342 |
private int burnin = -1; |
| 343 |
private ARGTrace[] trace; |
| 344 |
|
| 345 |
|
| 346 |
private FrequencySet<ARGModel> argSet; |
| 347 |
} |