-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathSATreeTraceAnalysis.java
More file actions
589 lines (490 loc) · 19.6 KB
/
Copy pathSATreeTraceAnalysis.java
File metadata and controls
589 lines (490 loc) · 19.6 KB
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
package beast.app.tools;
import beast.evolution.tree.Node;
import beast.evolution.tree.Tree;
import beast.evolution.tree.TreeTraceAnalysis;
import beast.util.FrequencySet;
import beast.util.SANexusParser;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.PrintStream;
import java.nio.charset.StandardCharsets;
import java.util.*;
/**
* @author Alexandra Gavryushkina
* @author Walter Xie
*/
public class SATreeTraceAnalysis extends TreeTraceAnalysis {
FrequencySet<String> pairs = new FrequencySet<String>();
public SATreeTraceAnalysis(List<Tree> posteriorTreeList, double burninFraction) {
super(posteriorTreeList, burninFraction);
}
// This should calculate a credible set for the trees obtained after removing all fossils
// WARNING this procedure needs testing.
@Override
public void analyze(double credSetProbability) {
removeFossilsFromAllTrees();
super.analyze(credSetProbability);
}
@Override
public void analyze() {
analyze(FrequencySet.DEFAULT_CRED_SET);
}
@Override
public void report(PrintStream oStream) {
super.report(oStream);
}
/**
* Sorted Newick Trees
* @param noLength
* @return
*/
public List<String> getNewickTrees (boolean noLength) {
List<String> newickTrees = new ArrayList<>();
for (Tree tree : treeInTrace) {
String nTree = tree.getRoot().toSortedNewick(new int[] {1}, false);
if (noLength) {
nTree = Utils.convertToShortTree(nTree);
}
newickTrees.add(nTree);
}
return newickTrees;
}
/**
* Sorted Newick Trees with length
* @return
*/
public List<String> getNewickTrees () {
return getNewickTrees(false);
}
/**
*
* @param printCladeFrequencies
* @param printPairs
* @param printFrequencies
* @param printTopologyCredibleSet
* @param isHTML true if the report should be formatted as an HTML fragment
* @return a report string
* @throws Exception
*/
public String toReportString(boolean printCladeFrequencies, boolean printPairs, boolean printFrequencies, boolean printTopologyCredibleSet, Double credSetProbability, boolean isHTML) throws Exception {
FrequencySet<String> clades = new FrequencySet<String>();
ArrayList<String> tmp = new ArrayList<String>();
int treeWithSACount = 0;
for (int i=0; i < getTotalTreesBurninRemoved(); i++) {
Tree tree = treeInTrace.get(i);
ArrayList<String> saClades = extractAllSAClades(tree.getRoot());
tmp.addAll(saClades);
for (int j=0; j<tree.getNodeCount(); j++)
if (tree.getNode(j).isFake()) {
treeWithSACount++;
break;
}
}
for (int i=0; i < tmp.size(); i++) {
clades.add(tmp.get(i));
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(baos);
ResultsOutput output = null;
if (isHTML) {
output = new ResultsOutput.HTML(ps);
} else {
output = new ResultsOutput.TabDelimitedPlainText(ps);
}
int treeCount = getTotalTreesBurninRemoved();
if (printCladeFrequencies) {
output.beginTableOutput("Clade frequencies", new String[]{"Count", "Percent", "Clade"});
for (int i =0; i < clades.size(); i++) {
double percent = (double) (clades.getFrequency(i) * 100)/(treeCount);
output.outputRow(new String[] {
String.format("%-10d", clades.getFrequency(i)),
String.format("%-10.2f", percent),
clades.get(i)});
}
output.endTableOutput();
}
if (printPairs) {
output.beginTableOutput("Pair frequencies", new String[]{"Count", "Percent", "Pair"});
for (int i =0; i < pairs.size(); i++) {
int freq = pairs.getFrequency(i);
double percent = (double) (freq * 100)/(treeCount);
output.outputRow(new String[] {
String.format("%-10d", freq),
String.format("%-10.2f", percent),
pairs.get(i)});
}
double a = (double)treeWithSACount/ treeCount;
output.endTableOutput();
output.line(String.format(treeWithSACount + " trees (or %2.2f%%) have sampled internal nodes.", a * 100));
}
FrequencySet<String> sampledAncestors = new FrequencySet<String>();
tmp = new ArrayList<String>();
for (int i=0; i < getTotalTreesBurninRemoved(); i++) {
Tree tree = treeInTrace.get(i);
tmp.addAll(listSA(tree, false));
}
for (String ancestor : tmp) {
sampledAncestors.add(ancestor);
}
if (printFrequencies) {
output.beginTableOutput("SA frequencies", new String[]{"Count", "Percent", "SA"});
for (int i =0; i < sampledAncestors.size(); i++) {
double percent = (double) (sampledAncestors.getFrequency(i) * 100)/(getTotalTreesBurninRemoved());
output.outputRow(new String[] {
String.format("%-10d", sampledAncestors.getFrequency(i)),
String.format("%-10.2f", percent),
sampledAncestors.get(i)});
}
output.endTableOutput();
}
if (printTopologyCredibleSet) {
countTopologies(output, credSetProbability);
}
ps.flush();
String content = new String(baos.toByteArray(), StandardCharsets.UTF_8);
ps.close();
return content;
}
/**
* prints the number of trees with sampled ancestors
* to the standard output
*/
public void countTreesWithSA() throws Exception {
int treeWithSACount = 0;
Tree tree;
for (int i =0; i < getTotalTreesBurninRemoved(); i++) {
tree = treeInTrace.get(i);
int j;
for (j=0; j<tree.getNodeCount(); j++)
if (tree.getNode(j).isFake()) {
treeWithSACount++;
break;
}
}
double a = (double)treeWithSACount/ getTotalTreesBurninRemoved();
System.out.format(treeWithSACount + " trees (or %2.2f%%) have sampled internal nodes.%n", a * 100);
System.out.println();
}
/**
* print the average number of sampled ancestors in all trees
* to the standard output
*/
public void countAverageSampledAncestorNumber(){
Tree tree;
int totalSACount = 0;
for (int i =0; i < getTotalTreesBurninRemoved(); i++) {
tree = treeInTrace.get(i);
for (int j=0; j<tree.getNodeCount(); j++) {
if (tree.getNode(j).isFake()) totalSACount++;
}
}
System.out.format("The average number of sampled ancestors per tree is %2.2f", ((double) totalSACount / getTotalTreesBurninRemoved()));
System.out.println();
}
/**
* prints the frequency of each sampled ancestor clade to the standard output
*
* @param countPairs if true then the frequencies of sampled ancestors pairs are also counted
* @throws Exception
*/
public void countSAClades(boolean countPairs) throws Exception {
FrequencySet<String> clades = new FrequencySet<String>();
ArrayList<String> tmp = new ArrayList<String>();
int treesWithSACount = 0;
for (int i=0; i < getTotalTreesBurninRemoved(); i++) {
Tree tree = treeInTrace.get(i);
ArrayList<String> saClades = extractAllSAClades(tree.getRoot());
tmp.addAll(saClades);
for (int j=0; j<tree.getNodeCount(); j++)
if (tree.getNode(j).isFake()) {
treesWithSACount++;
break;
}
}
for (int i=0; i < tmp.size(); i++)
clades.add(tmp.get(i));
System.out.println("Clade frequencies");
System.out.println();
System.out.println("Count \t Percent \t Clade");
System.out.println();
int treeCount = getTotalTreesBurninRemoved();
for (int i =0; i < clades.size(); i++) {
double percent = (double) (clades.getFrequency(i) * 100)/(treeCount);
System.out.format("%-10d %-10.2f", clades.getFrequency(i), percent);
System.out.println(clades.get(i));
}
System.out.println();
if (countPairs) {
System.out.println("Pair frequencies");
System.out.println();
System.out.println("Count \t Percent \t Pair");
System.out.println();
for (int i =0; i < pairs.size(); i++) {
int freq = pairs.getFrequency(i);
double percent = (double) (freq * 100)/(treeCount);
System.out.format("%-10d %-10.2f", freq, percent);
System.out.println(pairs.get(i));
}
System.out.println();
double a = (double)treesWithSACount/ treeCount;
System.out.format(treesWithSACount + " trees (or %2.2f%%) have sampled internal nodes.%n", a*100);
System.out.println();
}
}
/**
* for each sampled node, it counts the percentage of trees in which this sampled node is a sampled ancestor
* @param print if is true then the percentages are printed
* @param useRanking if is true then sampled nodes are distinguished by the time order of sampling
* (that is, for each n in {1,..., #SampledNodes} it counts the number of
* trees in which nth sampled node is a sampled ancestor)
* if is false, labels are used
* @return a set of sampled nodes with assigned frequencies
*/
public FrequencySet<String> countSAFrequencies(boolean print, boolean useRanking) {
FrequencySet<String> sampledAncestors = new FrequencySet<String>();
ArrayList<String> tmp = new ArrayList<String>();
for (int i=0; i < getTotalTreesBurninRemoved(); i++) {
Tree tree = treeInTrace.get(i);
tmp.addAll(listSA(tree, useRanking));
}
for (int i=0; i < tmp.size(); i++) {
sampledAncestors.add(tmp.get(i));
}
if (print) {
System.out.println("There are " + getTotalTreesBurninRemoved() + " trees in the file. " +
"The first " + getBurnin() + " are removed as a burn-in. " +
"Then sampled ancestors are counted");
System.out.println();
System.out.println("Count \t Percent \t SA");
for (int i =0; i < sampledAncestors.size(); i++) {
double percent = (double) (sampledAncestors.getFrequency(i) * 100)/(getTotalTreesBurninRemoved());
System.out.format("%-10d %-10.2f", sampledAncestors.getFrequency(i), percent);
System.out.println(sampledAncestors.get(i));
}
}
return sampledAncestors;
}
/**
*
*/
public void countTopologies(ResultsOutput output, Double credSetProbability) {
FrequencySet<String> topologies = new FrequencySet<String>();
topologies.setCredSetProbability(credSetProbability);
List<String> trees;
trees = getNewickTrees(true);
for (int i=0; i < trees.size(); i++) {
topologies.add(trees.get(i));
}
output.line("The number of trees in the file = " + trees.size() + ".");
double sumPercentage = 0;
output.beginTableOutput(topologies.getCredSetProbability()*100 + "% credible set:", new String[] {"Count", "Percent", "Topology"});
int i;
for (i = 0; i < topologies.size(); i++)
if (sumPercentage < topologies.getCredSetProbability()*100) {
double percent = (double) (topologies.getFrequency(i) * 100)/(trees.size());
output.outputRow(new String[] {
String.format("%-10d", topologies.getFrequency(i)),
String.format("%-10.2f", percent),
topologies.get(i)});
sumPercentage += percent;
} else {
break;
}
output.endTableOutput();
output.line("Total \t" + Math.round(sumPercentage) + "%");
output.line(topologies.getCredSetProbability()*100 + "% credible set has " + i + " trees.");
}
/**
*
* @param node
* @return all sampled ancestor clades descendant from node
*/
public ArrayList<String> extractAllSAClades(Node node) {
ArrayList<String> tmp = new ArrayList<String>();
if (node.isLeaf())
tmp.add(extractSAClade(node));
if (node.getLeft() != null)
tmp.addAll(extractAllSAClades(node.getLeft()));
if (node.getRight() != null)
tmp.addAll(extractAllSAClades(node.getRight()));
return tmp;
}
/**
* retern the list of sampled ancestors
* @param tree
* @return
*/
public ArrayList<String> listSA(Tree tree, boolean useRanking){
ArrayList<String> sampledAncestors = new ArrayList<String>();
for (int i=0; i<tree.getLeafNodeCount(); i++){
if (tree.getNode(i).isDirectAncestor()) {
if (useRanking) {
sampledAncestors.add(Integer.toString(getRank(tree, tree.getNode(i))));
} else {
sampledAncestors.add(tree.getNode(i).getID());
}
}
}
return sampledAncestors;
}
public int getRank(Tree tree, Node node) {
ArrayList<Node> nodes = new ArrayList<Node>(tree.getExternalNodes());
Comparator<Node> comp = new NodeComparator();
Collections.sort(nodes, comp);
return nodes.size() - nodes.indexOf(node);
}
public void printTreeHeights(){
Tree tree;
System.out.print("heights <- c(");
for (int i =0; i < getTotalTreesBurninRemoved()-1; i++) {
tree = treeInTrace.get(i);
System.out.print(tree.getRoot().getHeight() + ",");
}
tree = treeInTrace.get(getTotalTreesBurninRemoved()-1);
System.out.println(tree.getRoot().getHeight() + ")");
System.out.print("lengths <- c(");
for (int i =0; i < getTotalTreesBurninRemoved()-1; i++) {
tree = treeInTrace.get(i);
double length = 0;
for (int j=0; j< tree.getNodeCount(); j++){
length += tree.getNode(j).getLength();
}
System.out.print(length + ",");
}
tree = treeInTrace.get(getTotalTreesBurninRemoved()-1);
System.out.println(tree.getRoot().getHeight() + ")");
}
public void removeFossilsFromAllTrees() {
Tree tree;
for (int i =0; i < getTotalTreesBurninRemoved(); i++) {
tree = treeInTrace.get(i);
removeFossils(tree.getRoot(), tree, new ArrayList<>());
System.out.println("tree STATE_"+ i*1000 + " = "+tree.getRoot().toSortedNewick(new int[]{0}, true) + ";");
}
}
public void removeFossils(Node node, Tree tree, ArrayList<Node> removedNodes) {
if (node.isLeaf()) {
if (node.getHeight() > 0.00005) {
node.getParent().removeChild(node);
node.setParent(null);
removedNodes.add(node);
}
} else {
Node left = node.getLeft();
Node right = node.getRight();
removeFossils(left, tree, removedNodes);
removeFossils(right, tree, removedNodes);
if (node.isLeaf()) {
node.getParent().removeChild(node);
node.setParent(null);
removedNodes.add(node);
}
if (node.getChildCount() == 1) {
Node child = node.getChild(0);
if (node.getParent() != null) {
Node grandParent = node.getParent();
grandParent.removeChild(node);
grandParent.addChild(child);
child.setParent(grandParent);
node.setParent(null);
}
else {
child.setParent(null);
tree.setRootOnly(child);
}
removedNodes.add(node);
}
}
}
//********* private ***********
/**
*
* @param node
* @return a sampled ancestor clade in the format:
* [SA < A_1, A_n], where SA is the MRCA of (SA, A_1,..., A_n)
*/
private String extractSAClade(Node node) {
String tmp = new String();
String ancestor = node.getID();
tmp += ancestor + '<';
if (node.isDirectAncestor()) {
ArrayList<String> descendants = listSampledNodeIDsUnder(node.getParent());
tmp+= descendants;
for (String des:descendants) {
if (!des.equals(ancestor)) {
pairs.add(ancestor + "<" + des);
}
}
}
return tmp;
}
private ArrayList<String> listSampledNodeIDsUnder(Node node) {
ArrayList<String> tmp = new ArrayList<String>();
if (!node.isLeaf()) {
for (Node child : node.getChildren()) {
tmp.addAll(listSampledNodeIDsUnder(child));
}
} else tmp.add(node.getID());
Collections.sort(tmp);
return tmp;
}
private ArrayList<Integer> listSampledNodeNumbersUnder(Node node) {
ArrayList<Integer> tmp = new ArrayList<Integer>();
if (node.isLeaf())
tmp.add(node.getNr()+1);
if (node.getLeft() != null)
tmp.addAll(listSampledNodeNumbersUnder(node.getLeft()));
if (node.getRight() != null)
tmp.addAll(listSampledNodeNumbersUnder(node.getRight()));
return tmp;
}
/**
* static Utils
*/
public static class Utils {
/**
* get list of SA trees from file
* @param treeFile
* @return
* @throws Exception
*/
public static List<Tree> getTrees (File treeFile) throws Exception {
SANexusParser parser = new SANexusParser();
parser.parseFile(treeFile);
return parser.trees;
}
public static String convertToShortTree(String strIn) {
StringBuilder buf = new StringBuilder();
boolean skipping = false;
boolean inComment = false;
for (int i=0; i < strIn.length(); i++) {
if (!skipping && !inComment) {
switch (strIn.charAt(i)) {
case ':':
if (strIn.charAt(i+1) != '(') {
skipping = true;
i += 1;
} else {
buf.append(strIn.charAt(i));
}
break;
case '[':
inComment = true;
break;
default:
buf.append(strIn.charAt(i));
}
} else {
if (!inComment && (strIn.charAt(i) == ',' || strIn.charAt(i) == ')')) {
buf.append(strIn.charAt(i));
skipping = false;
}
if (inComment && strIn.charAt(i) == ']') {
inComment = false;
}
}
}
return buf.toString();
}
}
}