Debian Codegraph @

src/dr/evolution/tree/NeighborJoiningTree.java

raw symbols SCIP
1 /*
2 * NeighborJoiningTree.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 * constructs a neighbor-joining tree from pairwise distances
32 *
33 * @version $Id: NeighborJoiningTree.java,v 1.15 2005/05/24 20:25:56 rambaut Exp $
34 *
35 * @author Andrew Rambaut
36 * @author Alexei Drummond
37 */
38 public class NeighborJoiningTree extends ClusteringTree {
39
40 /**
41 * construct NJ tree
42 *
43 * @param distanceMatrix distance matrix
44 */
45 public NeighborJoiningTree(DistanceMatrix distanceMatrix) {
46
47 super(distanceMatrix, 3);
48 }
49
50
51 //
52 // Private stuff
53 //
54
55 private double[] r;
56 private double scale, maxHeight;
57
58 protected void init(DistanceMatrix distanceMatrix) {
59
60 super.init(distanceMatrix);
61
62 r = new double[numClusters];
63 maxHeight = 0.0;
64 }
65
66 protected void findNextPair() {
67
68 for (int i = 0; i < numClusters; i++) {
69 r[i] = 0;
70 for (int j = 0; j < numClusters; j++) {
71 r[i] += getDist(i,j);
72 }
73 }
74
75 besti = 0;
76 bestj = 1;
77 double smax = -1.0;
78 scale = 1.0/(numClusters-2);
79 for (int i = 0; i < numClusters-1; i++) {
80 for (int j = i+1; j < numClusters; j++) {
81 double sij = (r[i] + r[j] ) * scale - getDist(i, j);
82
83 if (sij > smax) {
84 smax = sij;
85 besti = i;
86 bestj = j;
87 }
88 }
89 }
90 abi = alias[besti];
91 abj = alias[bestj];
92 }
93
94 protected void finish() {
95
96 // Connect up the final two clusters
97 abi = alias[0];
98 abj = alias[1];
99
100 newCluster = new SimpleNode();
101
102 double dij = getDist(0, 1);
103 double l = dij * 0.5;
104 if (l < 0.0)
105 l = 0.0;
106 appendHeight(clusters[abi], l);
107 appendHeight(clusters[abj], l);
108
109 newCluster.setHeight(0.0);
110
111 newCluster.addChild(clusters[abi]);
112 newCluster.addChild(clusters[abj]);
113
114 reverseHeights(newCluster, maxHeight);
115
116 super.finish();
117 }
118
119 private void reverseHeights(SimpleNode node, double totalHeight) {
120
121 double height = totalHeight - node.getHeight();
122 node.setHeight(height);
123
124 if (!node.isExternal()) {
125 int i, n = node.getChildCount();
126 for (i = 0; i < n; i++) {
127 reverseHeights(node.getChild(i), totalHeight);
128 }
129 }
130 }
131
132 protected double newNodeHeight() {
133
134 double dij = getDist(besti, bestj);
135 double li = (dij + (r[besti] - r[bestj]) * scale) * 0.5;
136 double lj = dij - li; // = (dij + (r[bestj]-r[besti])*scale)*0.5
137
138 if (li < 0.0)
139 li = 0.0;
140
141 if (lj < 0.0)
142 lj = 0.0;
143
144 appendHeight(clusters[abi], li);
145 appendHeight(clusters[abj], lj);
146
147 return 0.0;
148 }
149
150 private void appendHeight(SimpleNode node, double delta) {
151
152 double height = node.getHeight() + delta;
153 node.setHeight(height);
154
155 if (!node.isExternal()) {
156 int i, n = node.getChildCount();
157 for (i = 0; i < n; i++) {
158 appendHeight(node.getChild(i), delta);
159 }
160 } else {
161 if (height > maxHeight)
162 maxHeight = height;
163 }
164 }
165
166 /**
167 * compute updated distance between the new cluster (i,j)
168 * to any other cluster k
169 */
170 protected double updatedDistance(int i, int j, int k) {
171
172 return (getDist(k, i) + getDist(k, j) - getDist(i, j)) * 0.5;
173 }
174 }