Debian Codegraph @

src/dr/evolution/tree/UnrootedTree.java

raw symbols SCIP
1 /*
2 * UnrootedTree.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 public interface UnrootedTree {
29
30 public int getNodeCount();
31
32 public NodeRef getNode(int i);
33
34 public int getNeighbourCount(NodeRef focal);
35
36 public boolean isExternal(NodeRef focal);
37
38 /**
39 * Get the branch length with the ith neighbour
40 */
41 public double getBranchLength(NodeRef focal, int i);
42
43 public void setBranchLength(NodeRef focal, int i, double edgeLength);
44
45 /**
46 * Get the ith neighbouring node from the focal node
47 */
48 public NodeRef getNeighbour(NodeRef focal, int i);
49
50 /**
51 * Swap node A with node B across focal edge between node1 and node2
52 */
53 public void NNI(NodeRef node1, int neighbour, int A, int B);
54
55
56 public class Default implements UnrootedTree {
57
58
59 public Default(Tree tree) {
60
61 if (tree.getChildCount(tree.getRoot()) == 3) {
62
63 nodeCount = tree.getNodeCount();
64
65 nodeRef = new NodeRef[nodeCount];
66 for (int i = 0; i < nodeCount; i++) {
67 nodeRef[i] = new UnrootedNode(i);
68
69 NodeRef treeNode = tree.getNode(i);
70 if (!tree.isRoot(treeNode)) {
71 NodeRef treeParent = tree.getParent(treeNode);
72 addEdge(nodeRef[i], nodeRef[treeParent.getNumber()], tree.getNodeHeight(treeParent) - tree.getNodeHeight(treeNode));
73 }
74 }
75 }
76
77 if (tree.getChildCount(tree.getRoot()) == 2) {
78
79 nodeCount = tree.getNodeCount() - 1;
80
81 int count = 0;
82 int[] mapping = new int[tree.getNodeCount()];
83 for (int i = 0; i < tree.getNodeCount(); i++) {
84 mapping[i] = count;
85 if (!tree.isRoot(tree.getNode(i))) {
86 count += 1;
87 }
88 }
89
90 nodeRef = new NodeRef[nodeCount];
91 for (int i = 0; i < tree.getNodeCount(); i++) {
92
93 NodeRef treeNode = tree.getNode(i);
94 if (!tree.isRoot(treeNode)) {
95
96 nodeRef[mapping[i]] = new UnrootedNode(mapping[i]);
97
98 NodeRef treeParent = tree.getParent(treeNode);
99
100 if (!tree.isRoot(treeParent)) {
101 addEdge(nodeRef[mapping[i]], nodeRef[mapping[treeParent.getNumber()]], tree.getNodeHeight(treeParent) - tree.getNodeHeight(treeNode));
102 } else {
103
104 NodeRef child1 = tree.getChild(tree.getRoot(), 0);
105 NodeRef child2 = tree.getChild(tree.getRoot(), 1);
106
107 double edgeLength =
108 (tree.getNodeHeight(tree.getRoot()) - tree.getNodeHeight(child1)) +
109 (tree.getNodeHeight(tree.getRoot()) - tree.getNodeHeight(child2));
110
111 addEdge(nodeRef[mapping[child1.getNumber()]], nodeRef[mapping[child2.getNumber()]], edgeLength);
112 }
113 }
114 }
115 }
116 }
117
118 public Default(int tipCount) {
119
120 nodeCount = tipCount * 2 - 1;
121
122 edges = new double[nodeCount][nodeCount];
123
124 for (int i = 0; i < nodeCount; i++) {
125 for (int j = 0; j < nodeCount; j++) {
126 edges[i][j] = -1.0;
127 }
128 }
129
130 nodeRef = new NodeRef[nodeCount];
131 for (int i = 0; i < nodeCount; i++) {
132 nodeRef[i] = new UnrootedNode(i);
133 }
134 }
135
136 public int getNodeCount() { return nodeCount; }
137 public NodeRef getNode(int i) { return nodeRef[i];}
138
139 /**
140 * @return the number of edges attached to the given node
141 */
142 public int getNeighbourCount(NodeRef node) {
143 int index = node.getNumber();
144 int count = 0;
145 for (int i = 0; i < nodeCount; i++) {
146 if (edges[i][index] >= 0.0) {
147 count += 1;
148 }
149 }
150 return count;
151 }
152
153 /**
154 * @return true if this node has only one edge attached to it.
155 */
156 public boolean isExternal(NodeRef node) {
157 return getNeighbourCount(node) == 1;
158 }
159
160 public double getBranchLength(NodeRef node, int j) {
161 int index = node.getNumber();
162 int count = 0;
163 for (int i = 0; i < nodeCount; i++) {
164 if (edges[i][index] >= 0.0) {
165 if (count == j) return edges[i][index];
166 count += 1;
167 }
168 }
169 throw new IllegalArgumentException();
170 }
171
172 public void setBranchLength(NodeRef node, int j, double edgeLength) {
173 int index = node.getNumber();
174 int count = 0;
175 for (int i = 0; i < nodeCount; i++) {
176 if (edges[i][index] >= 0.0) {
177 if (count == j) {
178 edges[i][index] = edgeLength;
179 edges[index][i] = edgeLength;
180 }
181 count += 1;
182 }
183 }
184 }
185
186 /**
187 * @return the node at the end of the ith edge from the given node.
188 */
189 public NodeRef getNeighbour(NodeRef node, int j) {
190 int index = node.getNumber();
191 int count = 0;
192 for (int i = 0; i < nodeCount; i++) {
193 if (edges[i][index] >= 0.0) {
194 if (count == j) return nodeRef[i];
195 count += 1;
196 }
197 }
198 throw new IllegalArgumentException();
199 }
200
201
202
203 /**
204 * Swap node A with node B across focal edge between node1 and node2
205 */
206 public void NNI(NodeRef node1, int neighbour, int A, int B) {
207
208 if (neighbour == A) throw new IllegalArgumentException();
209
210 NodeRef node2 = getNeighbour(node1, neighbour);
211
212 NodeRef nA = getNeighbour(node1, A);
213 NodeRef nB = getNeighbour(node2, B);
214
215 addEdge(node1, nB, getBranchLength(node1, A));
216 addEdge(node2, nA, getBranchLength(node2, B));
217 removeEdge(node1, nA);
218 removeEdge(node2, nB);
219 }
220
221 /*
222 * Should be called from root node.
223 * /
224 private void traverse(NodeRef node, NodeRef from) {
225
226 int edgeCount = getNeighbourCount(node);
227
228 for (int i = 0; i < edgeCount; i++) {
229
230 NodeRef neighbour = getNeighbour(node, i);
231
232 if (from != neighbour) {
233 traverse(neighbour, node);
234 }
235 }
236 }*/
237
238 /**
239 * Add an edge between the two nodes of the given length
240 */
241 private void addEdge(NodeRef node, NodeRef node2, double edgeLength) {
242 edges[node.getNumber()][node2.getNumber()] = edgeLength;
243 edges[node2.getNumber()][node.getNumber()] = edgeLength;
244 }
245
246 /**
247 * Removes any edge between the two given nodes.
248 */
249 private void removeEdge(NodeRef node, NodeRef node2) {
250 edges[node.getNumber()][node2.getNumber()] = -1.0;
251 edges[node2.getNumber()][node.getNumber()] = -1.0;
252 }
253
254
255 class UnrootedNode implements NodeRef {
256
257 public UnrootedNode(int i) { number = i; }
258
259 public int getNumber() { return number;}
260
261 public void setNumber(int n) { number = n; }
262
263 int number;
264 }
265
266 private double[][] edges;
267 private NodeRef[] nodeRef;
268 private int nodeCount;
269 }
270 }