Debian Codegraph @

src/dr/evolution/tree/ClusteringTree.java

raw symbols SCIP
1 /*
2 * ClusteringTree.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.evolution.tree;
27
28 import dr.evolution.distance.DistanceMatrix;
29
30 /**
31 * An abstract base class for clustering algorithms from pairwise distances
32 *
33 * @version $Id: ClusteringTree.java,v 1.8 2005/05/24 20:25:56 rambaut Exp $
34 *
35 * @author Andrew Rambaut
36 * @author Alexei Drummond
37 */
38 public abstract class ClusteringTree extends SimpleTree {
39
40 /**
41 * constructor ClusteringTree
42 *
43 * @param distanceMatrix distance matrix
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 // Protected and Private stuff
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 // Update distances
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 // Update alias
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 }