Debian Codegraph @

src/dr/evolution/tree/SimpleTree.java

raw symbols SCIP
1 /*
2 * SimpleTree.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.util.MutableTaxonListListener;
29 import dr.evolution.util.Taxon;
30 import dr.util.Attributable;
31
32 import java.util.ArrayList;
33 import java.util.Iterator;
34 import java.util.List;
35
36 /**
37 * A data structure for binary rooted trees.
38 *
39 * @author Andrew Rambaut
40 * @author Alexei Drummond
41 *
42 * @version $Id: SimpleTree.java,v 1.65 2006/08/23 10:46:33 rambaut Exp $
43 */
44 public class SimpleTree implements MutableTree {
45
46 /** Constructor tree with no nodes. Use adoptNodes to add some nodes. */
47 public SimpleTree() {
48 root = null;
49 }
50
51 /** clone constructor */
52 public SimpleTree(Tree tree) {
53
54 setUnits(tree.getUnits());
55
56 root = new SimpleNode(tree, tree.getRoot());
57
58 nodeCount = tree.getNodeCount();
59 internalNodeCount = tree.getInternalNodeCount();
60 externalNodeCount = tree.getExternalNodeCount();
61
62 nodes = new SimpleNode[nodeCount];
63
64 SimpleNode node = root;
65 do {
66 node = (SimpleNode) TreeUtils.postorderSuccessor(this, node);
67 if ((node.getNumber() >= externalNodeCount && node.isExternal()) ||
68 (node.getNumber() < externalNodeCount && !node.isExternal())) {
69 throw new RuntimeException("Error cloning tree: node numbers are incompatible");
70 }
71 nodes[node.getNumber()] = node;
72 } while (node != root);
73 }
74
75 /** clone constructor */
76 public SimpleTree(SimpleNode root) {
77 adoptNodes(root);
78 }
79
80 /**
81 * @return a copy of this tree
82 */
83 public Tree getCopy() {
84 return new SimpleTree(this);
85 }
86
87 /**
88 * Adopt a node hierarchy as its own. Only called by the SimpleTree(SimpleNode, TaxonList).
89 * This creates the node list and stores the nodes in post-traversal order.
90 */
91 protected void adoptNodes(SimpleNode node) {
92
93 if (inEdit) throw new RuntimeException("Mustn't be in an edit transaction to call this method!");
94
95 internalNodeCount = 0;
96 externalNodeCount = 0;
97
98 root = node;
99
100 do {
101 node = (SimpleNode) TreeUtils.postorderSuccessor(this, node);
102 if (node.isExternal()) {
103 externalNodeCount++;
104 } else
105 internalNodeCount++;
106
107 } while(node != root);
108
109 nodeCount = internalNodeCount + externalNodeCount;
110
111 nodes = new SimpleNode[nodeCount];
112
113 node = root;
114 int i = 0;
115 int j = externalNodeCount;
116
117 do {
118 node = (SimpleNode) TreeUtils.postorderSuccessor(this, node);
119 if (node.isExternal()) {
120 node.setNumber(i);
121 nodes[i] = node;
122 i++;
123 } else {
124 node.setNumber(j);
125 nodes[j] = node;
126 j++;
127 }
128 } while(node != root);
129 }
130
131 /**
132 * Return the units that this tree is expressed in.
133 */
134 public final Type getUnits() {
135 return units;
136 }
137
138 /**
139 * Sets the units that this tree is expressed in.
140 */
141 public final void setUnits(Type units) {
142 this.units = units;
143 }
144
145 /**
146 * @return a count of the number of nodes (internal + external) in this
147 * tree.
148 */
149 public int getNodeCount() {
150 return nodeCount;
151 }
152
153 public boolean hasNodeHeights() { return true; }
154 public double getNodeHeight(NodeRef node) { return ((SimpleNode)node).getHeight(); }
155 public double getNodeRate(NodeRef node) { return ((SimpleNode)node).getRate(); }
156 public Taxon getNodeTaxon(NodeRef node) { return ((SimpleNode)node).getTaxon(); }
157 public int getChildCount(NodeRef node) { return ((SimpleNode)node).getChildCount(); }
158 public boolean isExternal(NodeRef node) { return ((SimpleNode)node).getChildCount() == 0; }
159 public boolean isRoot(NodeRef node) { return (node == root); }
160 public NodeRef getChild(NodeRef node, int i) { return ((SimpleNode)node).getChild(i); }
161 public NodeRef getParent(NodeRef node) { return ((SimpleNode)node).getParent(); }
162
163 public boolean hasBranchLengths() { return true; }
164 public double getBranchLength(NodeRef node) {
165 NodeRef parent = getParent(node);
166 if (parent == null) {
167 return 0.0;
168 }
169
170 return getNodeHeight(parent) - getNodeHeight(node);
171 }
172 public void setBranchLength(NodeRef node, double length) {
173 throw new UnsupportedOperationException("SimpleTree cannot have branch lengths set... use FlexibleTree");
174 }
175
176
177 public final SimpleNode getExternalNode(int i) {
178 return nodes[i];
179 }
180
181 public final SimpleNode getInternalNode(int i) {
182 return nodes[i+externalNodeCount];
183 }
184
185 public final NodeRef getNode(int i) {
186 return nodes[i];
187 }
188
189 /**
190 * Returns the number of external nodes.
191 */
192 public final int getExternalNodeCount() {
193 return externalNodeCount;
194 }
195
196 /**
197 * Returns the ith internal node.
198 */
199 public final int getInternalNodeCount() {
200 return internalNodeCount;
201 }
202
203 /**
204 * Returns the root node of this tree.
205 */
206 public final NodeRef getRoot() {
207 return root;
208 }
209
210 /**
211 * Set a new node as root node.
212 */
213 public final void setRoot(NodeRef r) {
214
215 if (!inEdit) throw new RuntimeException("Must be in edit transaction to call this method!");
216
217 if (!(r instanceof SimpleNode)) { throw new IllegalArgumentException(); }
218 root = (SimpleNode)r;
219 root.setParent(null);
220 }
221
222
223 /**
224 * @return the height of the root node.
225 */
226 public final double getRootHeight() {
227 return root.getHeight();
228 }
229
230 /**
231 * Set the height of the root node.
232 */
233 public final void setRootHeight(double height) {
234 root.setHeight(height);
235 fireTreeChanged();
236 }
237
238
239 public void addChild(NodeRef p, NodeRef c) {
240 if (!inEdit) throw new RuntimeException("Must be in edit transaction to call this method!");
241 SimpleNode parent = (SimpleNode)p;
242 SimpleNode child = (SimpleNode)c;
243 if (parent.hasChild(child)) throw new IllegalArgumentException("Child already existists in parent");
244
245 parent.addChild(child);
246 }
247
248 public void removeChild(NodeRef p, NodeRef c) {
249
250 if (!inEdit) throw new RuntimeException("Must be in edit transaction to call this method!");
251
252 SimpleNode parent = (SimpleNode)p;
253 SimpleNode child = (SimpleNode)c;
254
255 parent.removeChild(child);
256 }
257
258 public void replaceChild(NodeRef node, NodeRef child, NodeRef newChild) {
259 if (!inEdit) throw new RuntimeException("Must be in edit transaction to call this method!");
260 SimpleNode parent = (SimpleNode)node;
261 parent.replaceChild((SimpleNode)child, (SimpleNode)newChild);
262 }
263
264 public boolean beginTreeEdit() {
265 boolean r = inEdit;
266 inEdit = true;
267 return r;
268 }
269
270 public void endTreeEdit() {
271 inEdit = false;
272 fireTreeChanged();
273 }
274
275 public void setNodeHeight(NodeRef n, double height) {
276 SimpleNode node = (SimpleNode)n;
277 node.setHeight(height);
278 fireTreeChanged();
279 }
280
281 public void setNodeRate(NodeRef n, double rate) {
282 SimpleNode node = (SimpleNode)n;
283 node.setRate(rate);
284 fireTreeChanged();
285 }
286
287 /**
288 * Sets an named attribute for a given node.
289 * @param node the node whose attribute is being set.
290 * @param name the name of the attribute.
291 * @param value the new value of the attribute.
292 */
293 public void setNodeAttribute(NodeRef node, String name, Object value) {
294 ((SimpleNode)node).setAttribute(name, value);
295 fireTreeChanged();
296 }
297
298 /**
299 * @return an object representing the named attributed for the given node.
300 * @param node the node whose attribute is being fetched.
301 * @param name the name of the attribute of interest.
302 */
303 public Object getNodeAttribute(NodeRef node, String name) {
304 return ((SimpleNode)node).getAttribute(name);
305 }
306
307 /**
308 * @return an interator of attribute names available for this node.
309 * @return a key set of attribute names available for this node.
310 */
311 public Iterator getNodeAttributeNames(NodeRef node) {
312 return ((SimpleNode)node).getAttributeNames();
313 }
314
315 // **************************************************************
316 // TaxonList IMPLEMENTATION
317 // **************************************************************
318
319 /**
320 * @return a count of the number of taxa in the list.
321 */
322 public int getTaxonCount() {
323 return getExternalNodeCount();
324 }
325
326 /**
327 * @return the ith taxon in the list.
328 */
329 public Taxon getTaxon(int taxonIndex) {
330 return getExternalNode(taxonIndex).getTaxon();
331 }
332
333 /**
334 * @return the ID of the taxon of the ith external node. If it doesn't have
335 * a taxon, returns the ID of the node itself.
336 */
337 public String getTaxonId(int taxonIndex) {
338 Taxon taxon = getTaxon(taxonIndex);
339 if (taxon != null)
340 return taxon.getId();
341 else
342 return getExternalNode(taxonIndex).getId();
343 }
344
345 /**
346 * returns the index of the taxon with the given id.
347 */
348 public int getTaxonIndex(String id) {
349 for (int i = 0, n = getTaxonCount(); i < n; i++) {
350 if (getTaxonId(i).equals(id)) return i;
351 }
352 return -1;
353 }
354
355 /**
356 * returns the index of the given taxon.
357 */
358 public int getTaxonIndex(Taxon taxon) {
359 for (int i = 0, n = getTaxonCount(); i < n; i++) {
360 if (getTaxon(i) == taxon) return i;
361 }
362 return -1;
363 }
364
365 public List<Taxon> asList() {
366 List<Taxon> taxa = new ArrayList<Taxon>();
367 for (int i = 0, n = getTaxonCount(); i < n; i++) {
368 taxa.add(getTaxon(i));
369 }
370 return taxa;
371 }
372
373 public Iterator<Taxon> iterator() {
374 return new Iterator<Taxon>() {
375 private int index = -1;
376
377 public boolean hasNext() {
378 return index < getTaxonCount() - 1;
379 }
380
381 public Taxon next() {
382 index ++;
383 return getTaxon(index);
384 }
385
386 public void remove() { /* do nothing */ }
387 };
388 }
389
390 /**
391 * @return an object representing the named attributed for the taxon of the given
392 * external node. If the node doesn't have a taxon then the nodes own attribute
393 * is returned.
394 * @param taxonIndex the index of the taxon whose attribute is being fetched.
395 * @param name the name of the attribute of interest.
396 */
397 public Object getTaxonAttribute(int taxonIndex, String name) {
398 Taxon taxon = getTaxon(taxonIndex);
399 if (taxon != null)
400 return taxon.getAttribute(name);
401 else
402 return getExternalNode(taxonIndex).getAttribute(name);
403 }
404
405 // **************************************************************
406 // MutableTaxonList IMPLEMENTATION
407 // **************************************************************
408
409 public int addTaxon(Taxon taxon) { throw new IllegalArgumentException("Cannot add taxon to a MutableTree"); }
410 public boolean removeTaxon(Taxon taxon) { throw new IllegalArgumentException("Cannot add taxon to a MutableTree"); }
411
412 /**
413 * Sets the ID of the taxon of the ith external node. If it doesn't have
414 * a taxon, sets the ID of the node itself.
415 */
416 public void setTaxonId(int taxonIndex, String id) {
417 Taxon taxon = getTaxon(taxonIndex);
418 if (taxon != null)
419 taxon.setId(id);
420 else
421 getExternalNode(taxonIndex).setId(id);
422
423 fireTreeChanged();
424 fireTaxaChanged();
425 }
426
427 /**
428 * Sets an named attribute for the taxon of a given external node. If the node
429 * doesn't have a taxon then the attribute is added to the node itself.
430 * @param taxonIndex the index of the taxon whose attribute is being set.
431 * @param name the name of the attribute.
432 * @param value the new value of the attribute.
433 */
434 public void setTaxonAttribute(int taxonIndex, String name, Object value) {
435 Taxon taxon = getTaxon(taxonIndex);
436 if (taxon != null)
437 taxon.setAttribute(name, value);
438 else
439 getExternalNode(taxonIndex).setAttribute(name, value);
440
441 fireTreeChanged();
442 fireTaxaChanged();
443 }
444
445 // **************************************************************
446 // Identifiable IMPLEMENTATION
447 // **************************************************************
448
449 protected String id = null;
450
451 /**
452 * @return the id.
453 */
454 public String getId() {
455 return id;
456 }
457
458 /**
459 * Sets the id.
460 */
461 public void setId(String id) {
462 this.id = id;
463 }
464
465 // **************************************************************
466 // Attributable IMPLEMENTATION
467 // **************************************************************
468
469 private Attributable.AttributeHelper attributes = null;
470
471 /**
472 * Sets an named attribute for this object.
473 * @param name the name of the attribute.
474 * @param value the new value of the attribute.
475 */
476 public void setAttribute(String name, Object value) {
477 if (attributes == null)
478 attributes = new Attributable.AttributeHelper();
479 attributes.setAttribute(name, value);
480 }
481
482 /**
483 * @return an object representing the named attributed for this object.
484 * @param name the name of the attribute of interest.
485 */
486 public Object getAttribute(String name) {
487 if (attributes == null)
488 return null;
489 else
490 return attributes.getAttribute(name);
491 }
492
493 /**
494 * @return an iterator of the attributes that this object has.
495 */
496 public Iterator<String> getAttributeNames() {
497 if (attributes == null)
498 return null;
499 else
500 return attributes.getAttributeNames();
501 }
502
503 public void addMutableTreeListener(MutableTreeListener listener) {
504 mutableTreeListeners.add(listener);
505 }
506
507 private void fireTreeChanged() {
508 for (MutableTreeListener mutableTreeListener : mutableTreeListeners) {
509 mutableTreeListener.treeChanged(this);
510 }
511 }
512
513 private final ArrayList<MutableTreeListener> mutableTreeListeners = new ArrayList<MutableTreeListener>();
514
515 public void addMutableTaxonListListener(MutableTaxonListListener listener) {
516 mutableTaxonListListeners.add(listener);
517 }
518
519 private void fireTaxaChanged() {
520 for (MutableTaxonListListener mutableTaxonListListener : mutableTaxonListListeners) {
521 mutableTaxonListListener.taxaChanged(this);
522 }
523 }
524
525 private final ArrayList<MutableTaxonListListener> mutableTaxonListListeners = new ArrayList<MutableTaxonListListener>();
526
527 /**
528 * @return a string containing a newick representation of the tree
529 */
530 public String toString() {
531 return TreeUtils.newick(this);
532 }
533
534 /**
535 * @return whether two trees have the same topology
536 */
537 public boolean equals(Object obj) {
538 if (!(obj instanceof Tree)) {
539 throw new IllegalArgumentException("SimpleTree.equals can only compare instances of Tree");
540 }
541 return TreeUtils.equal(this, (Tree)obj);
542 }
543
544 // **************************************************************
545 // Private members
546 // **************************************************************
547
548 /** root node */
549 SimpleNode root;
550
551 /** list of internal nodes (including root) */
552 SimpleNode[] nodes = null;
553
554 /** number of nodes (including root and tips) */
555 int nodeCount;
556
557 /** number of external nodes */
558 int externalNodeCount;
559
560 /** number of internal nodes (including root) */
561 int internalNodeCount;
562
563 /** holds the units of the trees branches. */
564 private Type units = Type.SUBSTITUTIONS;
565
566 boolean inEdit = false;
567 }