| 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.distance.DistanceMatrix; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public abstract class ClusteringTree extends SimpleTree { |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public ClusteringTree(DistanceMatrix distanceMatrix, int minimumTaxa) { |
| 46 |
|
| 47 |
this.distanceMatrix = distanceMatrix; |
| 48 |
|
| 49 |
if (distanceMatrix.getTaxonCount() < minimumTaxa) { |
| 50 |
throw new IllegalArgumentException("less than " + minimumTaxa + " taxa in distance matrix"); |
| 51 |
} |
| 52 |
|
| 53 |
init(distanceMatrix); |
| 54 |
|
| 55 |
while (true) { |
| 56 |
findNextPair(); |
| 57 |
|
| 58 |
if (numClusters < minimumTaxa) |
| 59 |
break; |
| 60 |
|
| 61 |
newCluster(); |
| 62 |
} |
| 63 |
|
| 64 |
finish(); |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
protected double getDist(int a, int b) { |
| 73 |
|
| 74 |
return distance[alias[a]][alias[b]]; |
| 75 |
} |
| 76 |
|
| 77 |
protected void init(DistanceMatrix distanceMatrix) { |
| 78 |
|
| 79 |
numClusters = distanceMatrix.getTaxonCount(); |
| 80 |
clusters = new SimpleNode[numClusters]; |
| 81 |
|
| 82 |
distance = new double[numClusters][numClusters]; |
| 83 |
for (int i = 0; i < numClusters; i++) { |
| 84 |
for (int j = 0; j < numClusters; j++) { |
| 85 |
distance[i][j] = distanceMatrix.getElement(i, j); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
for (int i = 0; i < numClusters; i++) { |
| 90 |
clusters[i] = new SimpleNode(); |
| 91 |
clusters[i].setTaxon(distanceMatrix.getTaxon(i)); |
| 92 |
} |
| 93 |
|
| 94 |
alias = new int[numClusters]; |
| 95 |
tipCount = new int[numClusters]; |
| 96 |
|
| 97 |
for (int i = 0; i < numClusters; i++) { |
| 98 |
alias[i] = i; |
| 99 |
tipCount[i] = 1; |
| 100 |
} |
| 101 |
|
| 102 |
} |
| 103 |
|
| 104 |
protected void newCluster() { |
| 105 |
|
| 106 |
newCluster = new SimpleNode(); |
| 107 |
newCluster.setHeight(newNodeHeight()); |
| 108 |
|
| 109 |
newCluster.addChild(clusters[abi]); |
| 110 |
newCluster.addChild(clusters[abj]); |
| 111 |
|
| 112 |
clusters[abi] = newCluster; |
| 113 |
clusters[abj] = null; |
| 114 |
|
| 115 |
|
| 116 |
for (int k = 0; k < numClusters; k++) { |
| 117 |
|
| 118 |
if (k != besti && k != bestj) { |
| 119 |
|
| 120 |
int ak = alias[k]; |
| 121 |
distance[ak][abi] = distance[abi][ak] = updatedDistance(besti, bestj, k); |
| 122 |
distance[ak][abj] = distance[abj][ak] = -1.0; |
| 123 |
} |
| 124 |
} |
| 125 |
distance[abi][abi] = 0.0; |
| 126 |
distance[abj][abj] = -1.0; |
| 127 |
|
| 128 |
|
| 129 |
for (int i = bestj; i < numClusters-1; i++) { |
| 130 |
alias[i] = alias[i+1]; |
| 131 |
} |
| 132 |
|
| 133 |
tipCount[abi] += tipCount[abj]; |
| 134 |
tipCount[abj] = 0; |
| 135 |
|
| 136 |
numClusters--; |
| 137 |
} |
| 138 |
|
| 139 |
protected void finish() { |
| 140 |
|
| 141 |
adoptNodes(newCluster); |
| 142 |
distance = null; |
| 143 |
} |
| 144 |
|
| 145 |
protected abstract void findNextPair(); |
| 146 |
protected abstract double newNodeHeight(); |
| 147 |
protected abstract double updatedDistance(int i, int j, int k); |
| 148 |
|
| 149 |
protected DistanceMatrix distanceMatrix; |
| 150 |
|
| 151 |
protected int numClusters; |
| 152 |
protected SimpleNode[] clusters; |
| 153 |
protected SimpleNode newCluster; |
| 154 |
protected int besti, abi; |
| 155 |
protected int bestj, abj; |
| 156 |
protected int[] tipCount; |
| 157 |
protected int[] alias; |
| 158 |
protected double[][] distance; |
| 159 |
|
| 160 |
protected int minimumTaxa; |
| 161 |
} |