| 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.evolution.tree; |
| 27 |
|
| 28 |
import dr.evolution.io.NewickImporter; |
| 29 |
import dr.evolution.util.TaxonList; |
| 30 |
|
| 31 |
import java.io.FileReader; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
@Deprecated |
| 42 |
public class TreeMetrics { |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public static double getRobinsonFouldsDistance(Tree t1, Tree t2) |
| 56 |
{ |
| 57 |
SplitSystem s1 = SplitUtils.getSplits(t1); |
| 58 |
|
| 59 |
return getRobinsonFouldsDistance(s1, t2); |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
public static double getRobinsonFouldsDistance(SplitSystem s1, Tree t2) |
| 70 |
{ |
| 71 |
TaxonList taxonList = s1.getTaxonList(); |
| 72 |
SplitSystem s2 = SplitUtils.getSplits(taxonList, t2); |
| 73 |
|
| 74 |
if (s1.getLabelCount() != s2.getLabelCount()) |
| 75 |
throw new IllegalArgumentException("Number of labels must be the same!"); |
| 76 |
|
| 77 |
int ns1 = s1.getSplitCount(); |
| 78 |
int ns2 = s2.getSplitCount(); |
| 79 |
|
| 80 |
|
| 81 |
int fn = 0; |
| 82 |
for (int i = 0; i < ns1; i++) |
| 83 |
{ |
| 84 |
if (!s2.hasSplit(s1.getSplit(i))) fn++; |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
int fp = 0; |
| 89 |
for (int i = 0; i < ns2; i++) |
| 90 |
{ |
| 91 |
if (!s1.hasSplit(s2.getSplit(i))) fp++; |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
return 0.5*((double) fp + (double) fn); |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
public static double getRobinsonFouldsRescaledDistance(Tree t1, Tree t2) |
| 106 |
{ |
| 107 |
SplitSystem s1 = SplitUtils.getSplits(t1); |
| 108 |
|
| 109 |
return getRobinsonFouldsRescaledDistance(s1, t2); |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
public static double getRobinsonFouldsRescaledDistance(SplitSystem s1, Tree t2) |
| 121 |
{ |
| 122 |
return getRobinsonFouldsDistance(s1, t2)/(double) s1.getSplitCount(); |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
private static void analyze(Tree[] trees, int update) { |
| 131 |
|
| 132 |
TaxonList masterList = trees[0]; |
| 133 |
|
| 134 |
System.out.println("Calculating splits..."); |
| 135 |
SplitSystem[] splits = new SplitSystem[trees.length]; |
| 136 |
for (int i =0; i < splits.length; i++) { |
| 137 |
splits[i] = SplitUtils.getSplits(masterList, trees[i]); |
| 138 |
} |
| 139 |
|
| 140 |
int maxOffset = MAX_OFFSET; |
| 141 |
int samples = trees.length; |
| 142 |
if ((samples/3) < maxOffset) { |
| 143 |
maxOffset = (samples/3); |
| 144 |
} |
| 145 |
|
| 146 |
double[] meanDistances = new double[maxOffset]; |
| 147 |
|
| 148 |
System.out.println("Calculating mean distance per lag..."); |
| 149 |
for (int i=0; i < maxOffset; i++) { |
| 150 |
meanDistances[i] = 0; |
| 151 |
for (int j = 0; j < samples-i; j++) { |
| 152 |
double distance = getRobinsonFouldsRescaledDistance(splits[i], trees[j]); |
| 153 |
meanDistances[i] += distance; |
| 154 |
} |
| 155 |
meanDistances[i] /= ((double) samples-i); |
| 156 |
System.out.println(meanDistances[i]); |
| 157 |
} |
| 158 |
|
| 159 |
} |
| 160 |
|
| 161 |
public static final void main(String[] args) throws Exception { |
| 162 |
FileReader reader = new FileReader(args[0]); |
| 163 |
NewickImporter importer = new NewickImporter(reader); |
| 164 |
|
| 165 |
Tree[] trees = importer.importTrees(null); |
| 166 |
System.out.println("Imported " + trees.length + " trees..."); |
| 167 |
analyze(trees, 1000); |
| 168 |
} |
| 169 |
|
| 170 |
private static int MAX_OFFSET = 1000; |
| 171 |
} |