| 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 |
import dr.evolution.util.TaxonList; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public class RankProportionStatistic extends AbstractTreeSummaryStatistic { |
| 39 |
|
| 40 |
private RankProportionStatistic() { |
| 41 |
this(true); |
| 42 |
} |
| 43 |
|
| 44 |
private RankProportionStatistic(boolean proportion) { |
| 45 |
this.rank = 1; |
| 46 |
this.proportion = proportion; |
| 47 |
} |
| 48 |
|
| 49 |
public void setInteger(int value) { |
| 50 |
this.rank = value; |
| 51 |
} |
| 52 |
|
| 53 |
public double[] getSummaryStatistic(Tree tree) { |
| 54 |
|
| 55 |
double externalLength = 0.0; |
| 56 |
double internalLength = 0.0; |
| 57 |
double rankLength = 0.0; |
| 58 |
|
| 59 |
int externalNodeCount = tree.getExternalNodeCount(); |
| 60 |
for (int i = 0; i < externalNodeCount; i++) { |
| 61 |
NodeRef node = tree.getExternalNode(i); |
| 62 |
NodeRef parent = tree.getParent(node); |
| 63 |
externalLength += tree.getNodeHeight(parent) - tree.getNodeHeight(node); |
| 64 |
} |
| 65 |
|
| 66 |
int internalNodeCount = tree.getInternalNodeCount(); |
| 67 |
for (int i = 0; i < internalNodeCount; i++) { |
| 68 |
NodeRef node = tree.getInternalNode(i); |
| 69 |
if (!tree.isRoot(node)) { |
| 70 |
NodeRef parent = tree.getParent(node); |
| 71 |
internalLength += tree.getNodeHeight(parent) - tree.getNodeHeight(node); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
if (rank == 1) { |
| 76 |
if (!proportion) { |
| 77 |
return new double[] { externalLength }; |
| 78 |
} |
| 79 |
return new double[] { externalLength/(internalLength+externalLength) }; |
| 80 |
} |
| 81 |
|
| 82 |
for (int i = 0; i < internalNodeCount; i++) { |
| 83 |
NodeRef node = tree.getInternalNode(i); |
| 84 |
if (!tree.isRoot(node)) { |
| 85 |
int r = getRank(tree, node); |
| 86 |
if (r == rank) { |
| 87 |
NodeRef parent = tree.getParent(node); |
| 88 |
rankLength += tree.getNodeHeight(parent) - tree.getNodeHeight(node); |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
if (!proportion) return new double[] { rankLength }; |
| 94 |
|
| 95 |
return new double[] { rankLength/(internalLength+externalLength) }; |
| 96 |
} |
| 97 |
|
| 98 |
private int getRank(Tree tree, NodeRef node) { |
| 99 |
int childCount = tree.getChildCount(node); |
| 100 |
if (childCount == 0) return 1; |
| 101 |
int size = 0; |
| 102 |
for (int i = 0; i < childCount; i++) { |
| 103 |
size += getRank(tree, tree.getChild(node, i)); |
| 104 |
} |
| 105 |
return size; |
| 106 |
} |
| 107 |
|
| 108 |
public String getSummaryStatisticName() { |
| 109 |
if (!proportion) return "Rank " + rank + " branch length"; |
| 110 |
return "Rank " + rank + " branch proportion"; |
| 111 |
} |
| 112 |
|
| 113 |
public String getSummaryStatisticDescription() { |
| 114 |
|
| 115 |
String message = ""; |
| 116 |
|
| 117 |
if (!proportion) { |
| 118 |
message += "The total length of branches of rank " + rank + ". "; |
| 119 |
} else { |
| 120 |
message += "The proportion of the total length of the tree that is made up of branches of rank " + rank + ". "; |
| 121 |
} |
| 122 |
|
| 123 |
message += "A branch is rank k if it has exactly k tips below it. For example, external tips are rank 1 and internal " + |
| 124 |
"branches directly above cherries are rank 2."; |
| 125 |
|
| 126 |
return message; |
| 127 |
} |
| 128 |
|
| 129 |
public String getSummaryStatisticReference() { return FACTORY.getSummaryStatisticReference(); } |
| 130 |
public boolean allowsPolytomies() { return FACTORY.allowsPolytomies(); } |
| 131 |
public boolean allowsNonultrametricTrees() { return FACTORY.allowsNonultrametricTrees(); } |
| 132 |
public boolean allowsUnrootedTrees() { return FACTORY.allowsUnrootedTrees(); } |
| 133 |
public SummaryStatisticDescription.Category getCategory() { return FACTORY.getCategory(); } |
| 134 |
|
| 135 |
public static final TreeSummaryStatistic.Factory FACTORY = new TreeSummaryStatistic.Factory() { |
| 136 |
|
| 137 |
public TreeSummaryStatistic createStatistic() { |
| 138 |
return new RankProportionStatistic(); |
| 139 |
} |
| 140 |
|
| 141 |
public String getSummaryStatisticName() { |
| 142 |
return "Rank branch proportion"; |
| 143 |
} |
| 144 |
|
| 145 |
public String getSummaryStatisticDescription() { |
| 146 |
|
| 147 |
return "The proportion of the total length of the tree that is made up of branches of a given rank"; |
| 148 |
} |
| 149 |
|
| 150 |
public String getSummaryStatisticReference() { |
| 151 |
|
| 152 |
return "-"; |
| 153 |
} |
| 154 |
|
| 155 |
public String getValueName() { return "The rank (k):"; } |
| 156 |
|
| 157 |
public boolean allowsPolytomies() { return true; } |
| 158 |
|
| 159 |
public boolean allowsNonultrametricTrees() { return true; } |
| 160 |
|
| 161 |
public boolean allowsUnrootedTrees() { return true; } |
| 162 |
|
| 163 |
public SummaryStatisticDescription.Category getCategory() { return SummaryStatisticDescription.Category.POPULATION_GENETIC; } |
| 164 |
|
| 165 |
public boolean allowsWholeTree() { return true; } |
| 166 |
|
| 167 |
public boolean allowsCharacter() { return false; } |
| 168 |
|
| 169 |
public boolean allowsCharacterState() { return false; } |
| 170 |
|
| 171 |
public boolean allowsTaxonList() { return false; } |
| 172 |
|
| 173 |
public boolean allowsInteger() { return true; } |
| 174 |
|
| 175 |
public boolean allowsDouble() { return false; } |
| 176 |
}; |
| 177 |
|
| 178 |
private int rank = 2; |
| 179 |
private boolean proportion = true; |
| 180 |
} |