| 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 |
public class FuLiD extends AbstractTreeSummaryStatistic { |
| 37 |
|
| 38 |
private FuLiD() { } |
| 39 |
|
| 40 |
public double[] getSummaryStatistic(Tree tree) { |
| 41 |
|
| 42 |
double externalLength = 0.0; |
| 43 |
double internalLength = 0.0; |
| 44 |
|
| 45 |
int externalNodeCount = tree.getExternalNodeCount(); |
| 46 |
for (int i = 0; i < externalNodeCount; i++) { |
| 47 |
NodeRef node = tree.getExternalNode(i); |
| 48 |
NodeRef parent = tree.getParent(node); |
| 49 |
externalLength += tree.getNodeHeight(parent) - tree.getNodeHeight(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 |
NodeRef parent = tree.getParent(node); |
| 57 |
internalLength += tree.getNodeHeight(parent) - tree.getNodeHeight(node); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
int n = externalNodeCount; |
| 62 |
|
| 63 |
double total = externalLength + internalLength; |
| 64 |
|
| 65 |
|
| 66 |
double D = total - a(n)*externalLength; |
| 67 |
|
| 68 |
|
| 69 |
D /= Math.sqrt(u(n)*total + (v(n)*(total*total))); |
| 70 |
|
| 71 |
return new double[] { D }; |
| 72 |
} |
| 73 |
|
| 74 |
private double a(int n) { |
| 75 |
double a = 0.0; |
| 76 |
for (int k = 1; k < n; k++) { |
| 77 |
a += 1/(double)k; |
| 78 |
} |
| 79 |
return a; |
| 80 |
} |
| 81 |
|
| 82 |
private double b(int n) { |
| 83 |
double b = 0.0; |
| 84 |
for (int k = 1; k < n; k++) { |
| 85 |
b += 1/(double)(k*k); |
| 86 |
} |
| 87 |
return b; |
| 88 |
} |
| 89 |
|
| 90 |
private double c(int n) { |
| 91 |
|
| 92 |
if (n==2) return 1.0; |
| 93 |
|
| 94 |
double an = a(n); |
| 95 |
|
| 96 |
double c = 2.0 * (n * an - 2.0 * (n - 1.0)); |
| 97 |
c /= (n-1)*(n-2); |
| 98 |
|
| 99 |
return c; |
| 100 |
} |
| 101 |
|
| 102 |
private double v(int n) { |
| 103 |
double an2 = a(n); |
| 104 |
an2 *= an2; |
| 105 |
|
| 106 |
double v = 1 + (an2/(b(n)+an2)) * (c(n)-((n+1)/(n-1))); |
| 107 |
|
| 108 |
return v; |
| 109 |
} |
| 110 |
|
| 111 |
private double u(int n) { |
| 112 |
return a(n) - 1 - v(n); |
| 113 |
} |
| 114 |
|
| 115 |
public String getSummaryStatisticName() { return FACTORY.getSummaryStatisticName(); } |
| 116 |
public String getSummaryStatisticDescription() { return FACTORY.getSummaryStatisticDescription(); } |
| 117 |
public String getSummaryStatisticReference() { return FACTORY.getSummaryStatisticReference(); } |
| 118 |
public boolean allowsPolytomies() { return FACTORY.allowsPolytomies(); } |
| 119 |
public boolean allowsNonultrametricTrees() { return FACTORY.allowsNonultrametricTrees(); } |
| 120 |
public boolean allowsUnrootedTrees() { return FACTORY.allowsUnrootedTrees(); } |
| 121 |
public SummaryStatisticDescription.Category getCategory() { return FACTORY.getCategory(); } |
| 122 |
|
| 123 |
public static final TreeSummaryStatistic.Factory FACTORY = new TreeSummaryStatistic.Factory() { |
| 124 |
|
| 125 |
public TreeSummaryStatistic createStatistic() { |
| 126 |
return new FuLiD(); |
| 127 |
} |
| 128 |
|
| 129 |
public String getSummaryStatisticName() { |
| 130 |
return "Fu & Li's D"; |
| 131 |
} |
| 132 |
|
| 133 |
public String getSummaryStatisticDescription() { |
| 134 |
|
| 135 |
return "The normalized difference between total tree length and external branch tree length. " + |
| 136 |
"Note that this implementation uses branch lengths rather than the counts of mutations (this " + |
| 137 |
"has a nice side-effect of taking into account complex mutation models.)"; |
| 138 |
} |
| 139 |
|
| 140 |
public String getSummaryStatisticReference() { |
| 141 |
|
| 142 |
return "Fu & Li (1993)"; |
| 143 |
} |
| 144 |
|
| 145 |
public boolean allowsPolytomies() { return false; } |
| 146 |
|
| 147 |
public boolean allowsNonultrametricTrees() { return false; } |
| 148 |
|
| 149 |
public boolean allowsUnrootedTrees() { return false; } |
| 150 |
|
| 151 |
public SummaryStatisticDescription.Category getCategory() { return SummaryStatisticDescription.Category.POPULATION_GENETIC; } |
| 152 |
}; |
| 153 |
} |