| 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.app.treestat.statistics; |
| 27 |
|
| 28 |
import dr.evolution.tree.NodeRef; |
| 29 |
import dr.evolution.tree.Tree; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public class TreeLength extends AbstractTreeSummaryStatistic { |
| 38 |
|
| 39 |
private TreeLength() { } |
| 40 |
|
| 41 |
public double[] getSummaryStatistic(Tree tree) { |
| 42 |
|
| 43 |
double externalLength = 0.0; |
| 44 |
double internalLength = 0.0; |
| 45 |
|
| 46 |
int externalNodeCount = tree.getExternalNodeCount(); |
| 47 |
for (int i = 0; i < externalNodeCount; i++) { |
| 48 |
NodeRef node = tree.getExternalNode(i); |
| 49 |
externalLength += tree.getBranchLength(node); |
| 50 |
} |
| 51 |
|
| 52 |
int internalNodeCount = tree.getInternalNodeCount(); |
| 53 |
for (int i = 0; i < internalNodeCount; i++) { |
| 54 |
NodeRef node = tree.getInternalNode(i); |
| 55 |
if (!tree.isRoot(node)) { |
| 56 |
internalLength += tree.getBranchLength(node); |
| 57 |
} |
| 58 |
} |
| 59 |
return new double[] { internalLength + externalLength }; |
| 60 |
} |
| 61 |
|
| 62 |
public String getSummaryStatisticName() { return FACTORY.getSummaryStatisticName(); } |
| 63 |
public String getSummaryStatisticDescription() { return FACTORY.getSummaryStatisticDescription(); } |
| 64 |
public String getSummaryStatisticReference() { return FACTORY.getSummaryStatisticReference(); } |
| 65 |
public boolean allowsPolytomies() { return FACTORY.allowsPolytomies(); } |
| 66 |
public boolean allowsNonultrametricTrees() { return FACTORY.allowsNonultrametricTrees(); } |
| 67 |
public boolean allowsUnrootedTrees() { return FACTORY.allowsUnrootedTrees(); } |
| 68 |
public SummaryStatisticDescription.Category getCategory() { return FACTORY.getCategory(); } |
| 69 |
|
| 70 |
public static final TreeSummaryStatistic.Factory FACTORY = new TreeSummaryStatistic.Factory() { |
| 71 |
|
| 72 |
public TreeSummaryStatistic createStatistic() { |
| 73 |
return new TreeLength(); |
| 74 |
} |
| 75 |
|
| 76 |
public String getSummaryStatisticName() { |
| 77 |
return "Tree Length"; |
| 78 |
} |
| 79 |
|
| 80 |
public String getSummaryStatisticDescription() { |
| 81 |
|
| 82 |
return "The sum of the branch lengths."; |
| 83 |
} |
| 84 |
|
| 85 |
public String getSummaryStatisticReference() { |
| 86 |
|
| 87 |
return "-"; |
| 88 |
} |
| 89 |
|
| 90 |
public boolean allowsPolytomies() { return true; } |
| 91 |
|
| 92 |
public boolean allowsNonultrametricTrees() { return true; } |
| 93 |
|
| 94 |
public boolean allowsUnrootedTrees() { return true; } |
| 95 |
|
| 96 |
public SummaryStatisticDescription.Category getCategory() { return SummaryStatisticDescription.Category.GENERAL; } |
| 97 |
}; |
| 98 |
} |