Debian Codegraph @

src/dr/evolution/tree/FlexibleTree.java

raw symbols SCIP
1 /*
2 * FlexibleTree.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 import java.util.Map;
36
37 /**
38 * data structure for binary rooted trees
39 *
40 * @author Andrew Rambaut
41 * @author Alexei Drummond
42 * @version $Id: FlexibleTree.java,v 1.34 2006/07/02 21:14:52 rambaut Exp $
43 */
44 public class FlexibleTree implements MutableTree {
45
46 /**
47 * Constructor tree with no nodes. Use adoptNodes to add some nodes.
48 */
49 public FlexibleTree() {
50 root = null;
51 }
52
53 /**
54 * clone constructor
55 */
56 public FlexibleTree(Tree tree) {
57 this(tree, false);
58 }
59
60 /**
61 * clone constructor
62 */
63 public FlexibleTree(Tree tree, boolean copyAttributes) {
64
65 setUnits(tree.getUnits());
66
67 root = new FlexibleNode(tree, tree.getRoot(), copyAttributes);
68
69 nodeCount = tree.getNodeCount();
70 internalNodeCount = tree.getInternalNodeCount();
71 externalNodeCount = tree.getExternalNodeCount();
72
73
74 nodes = new FlexibleNode[nodeCount];
75
76 FlexibleNode node = root;
77 do {
78 node = (FlexibleNode) TreeUtils.postorderSuccessor(this, node);
79 if ((node.getNumber() >= externalNodeCount && node.isExternal()) ||
80 (node.getNumber() < externalNodeCount && !node.isExternal())) {
81 throw new RuntimeException("Error cloning tree: node numbers are incompatible");
82 }
83 nodes[node.getNumber()] = node;
84 } while (node != root);
85
86 heightsKnown = tree.hasNodeHeights();
87 lengthsKnown = tree.hasBranchLengths();
88 }
89
90 /**
91 * clone constructor
92 */
93 public FlexibleTree(FlexibleNode root) {
94 this(root, true, true, null);
95 }
96
97 /**
98 * clone constructor
99 */
100 public FlexibleTree(FlexibleNode root, boolean heightsKnown, boolean lengthsKnown) {
101
102 this(root, heightsKnown, lengthsKnown, null);
103 }
104
105 /**
106 * clone constructor
107 */
108 public FlexibleTree(FlexibleNode root, Map<Taxon, Integer> taxonNumberMap) {
109 this(root, true, true, taxonNumberMap);
110 }
111
112 /**
113 * clone constructor
114 */
115 public FlexibleTree(FlexibleNode root, boolean heightsKnown, boolean lengthsKnown, Map<Taxon, Integer> taxonNumberMap) {
116
117 adoptNodes(root, taxonNumberMap);
118
119 this.heightsKnown = heightsKnown;
120 this.lengthsKnown = lengthsKnown;
121
122 }
123
124 /**
125 * @return a copy of this tree
126 */
127 public Tree getCopy() {
128 return new FlexibleTree(this);
129 }
130
131 public void adoptTreeModelOrdering() {
132 resolveTree();
133 int i = 0;
134 int j = externalNodeCount;
135
136 FlexibleNode node = (FlexibleNode) getRoot();
137
138 do {
139 node = (FlexibleNode) TreeUtils.postorderSuccessor(this, node);
140
141 if (node.isExternal()) {
142 node.setNumber(i);
143 nodes[i] = node;
144 i++;
145 } else {
146 node.setNumber(j);
147 nodes[j] = node;
148 j++;
149 }
150 } while (node != root);
151 }
152
153 /**
154 * Adopt a node hierarchy as its own. Only called by the FlexibleTree(FlexibleNode, TaxonList).
155 * This creates the node list and stores the nodes in post-traversal order.
156 */
157 protected void adoptNodes(FlexibleNode node, Map<Taxon, Integer> taxonNumberMap) {
158
159 if (inEdit) throw new RuntimeException("Mustn't be in an edit transaction to call this method!");
160
161 internalNodeCount = 0;
162 externalNodeCount = 0;
163
164 root = node;
165
166 do {
167 node = (FlexibleNode) TreeUtils.postorderSuccessor(this, node);
168 if (node.isExternal()) {
169 externalNodeCount++;
170 } else
171 internalNodeCount++;
172
173 } while (node != root);
174
175 nodeCount = internalNodeCount + externalNodeCount;
176 //System.out.println("internal count = " + internalNodeCount);
177 //System.out.println("external count = " + externalNodeCount);
178
179 nodes = new FlexibleNode[nodeCount];
180
181 node = root;
182 int i = 0;
183 int j = externalNodeCount;
184
185 do {
186 node = (FlexibleNode) TreeUtils.postorderSuccessor(this, node);
187 //System.out.print("node = " + node.getId() + " ");
188 if (node.isExternal()) {
189 if (taxonNumberMap != null && taxonNumberMap.size() > 0) {
190 i = taxonNumberMap.get(node.getTaxon());
191 }
192
193 node.setNumber(i);
194 //System.out.println(" leaf number " + i);
195 nodes[i] = node;
196
197 if (taxonNumberMap == null || taxonNumberMap.size() == 0) {
198 i++;
199 }
200 } else {
201 node.setNumber(j);
202 //System.out.println(" ancestor number " + j);
203 nodes[j] = node;
204 j++;
205 }
206 } while (node != root);
207 }
208
209 /**
210 * Return the units that this tree is expressed in.
211 */
212 public final Type getUnits() {
213 return units;
214 }
215
216 /**
217 * Sets the units that this tree is expressed in.
218 */
219 public final void setUnits(Type units) {
220 this.units = units;
221 }
222
223 /**
224 * @return a count of the number of nodes (internal + external) in this
225 * tree.
226 */
227 public int getNodeCount() {
228 return nodeCount;
229 }
230
231 public boolean hasNodeHeights() {
232 return heightsKnown;
233 }
234
235 public double getNodeHeight(NodeRef node) {
236 if (!heightsKnown) {
237 calculateNodeHeights();
238 }
239 return ((FlexibleNode) node).getHeight();
240 }
241
242 public boolean hasBranchLengths() {
243 return lengthsKnown;
244 }
245
246 public double getBranchLength(NodeRef node) {
247 if (!lengthsKnown) {
248 calculateBranchLengths();
249 }
250 return ((FlexibleNode) node).getLength();
251 }
252
253 public double getNodeRate(NodeRef node) {
254
255 Object rateAttr = getNodeAttribute(node, "rate");
256
257 if (rateAttr != null) {
258 if (rateAttr instanceof Number) return (Double) rateAttr;
259 if (rateAttr instanceof String) return Double.parseDouble((String) rateAttr);
260 }
261
262 return ((FlexibleNode) node).getRate();
263 }
264
265 public Taxon getNodeTaxon(NodeRef node) {
266 return ((FlexibleNode) node).getTaxon();
267 }
268
269 public void setNodeTaxon(NodeRef node, Taxon taxon) {
270 ((FlexibleNode) node).setTaxon(taxon);
271 }
272
273 public int getChildCount(NodeRef node) {
274 return ((FlexibleNode) node).getChildCount();
275 }
276
277 public boolean isExternal(NodeRef node) {
278 return ((FlexibleNode) node).getChildCount() == 0;
279 }
280
281 public boolean isRoot(NodeRef node) {
282 return (node == root);
283 }
284
285 public NodeRef getChild(NodeRef node, int i) {
286 return ((FlexibleNode) node).getChild(i);
287 }
288
289 public NodeRef getParent(NodeRef node) {
290 return ((FlexibleNode) node).getParent();
291 }
292
293
294 public final NodeRef getExternalNode(int i) {
295 return nodes[i];
296 }
297
298 public final NodeRef getInternalNode(int i) {
299 return nodes[i + externalNodeCount];
300 }
301
302 public final NodeRef getNode(int i) {
303 return nodes[i];
304 }
305
306 /**
307 * Returns the number of external nodes.
308 */
309 public final int getExternalNodeCount() {
310 return externalNodeCount;
311 }
312
313 /**
314 * Returns the ith internal node.
315 */
316 public final int getInternalNodeCount() {
317 return internalNodeCount;
318 }
319
320 /**
321 * Returns the root node of this tree.
322 */
323 public final NodeRef getRoot() {
324 return root;
325 }
326
327 /**
328 * Set a new node as root node.
329 */
330 public final void setRoot(NodeRef r) {
331
332 if (!inEdit) throw new RuntimeException("Must be in edit transaction to call this method!");
333
334 if (!(r instanceof FlexibleNode)) {
335 throw new IllegalArgumentException();
336 }
337 root = (FlexibleNode) r;
338 }
339
340
341 /**
342 * @return the height of the root node.
343 */
344 public final double getRootHeight() {
345 return getNodeHeight(root);
346 }
347
348 /**
349 * Set the height of the root node.
350 */
351 public final void setRootHeight(double height) {
352 setNodeHeight(root, height);
353
354 fireTreeChanged();
355 }
356
357
358 public void addChild(NodeRef p, NodeRef c) {
359 if (!inEdit) throw new RuntimeException("Must be in edit transaction to call this method!");
360 FlexibleNode parent = (FlexibleNode) p;
361 FlexibleNode child = (FlexibleNode) c;
362 if (parent.hasChild(child)) throw new IllegalArgumentException("Child already existists in parent");
363
364 parent.addChild(child);
365 }
366
367 public void removeChild(NodeRef p, NodeRef c) {
368
369 if (!inEdit) throw new RuntimeException("Must be in edit transaction to call this method!");
370
371 FlexibleNode parent = (FlexibleNode) p;
372 FlexibleNode child = (FlexibleNode) c;
373
374 for (int i = 0; i < parent.getChildCount(); i++) {
375 if (parent.getChild(i) == child) {
376 parent.removeChild(i);
377 return;
378 }
379 }
380 }
381
382 public void replaceChild(NodeRef node, NodeRef child, NodeRef newChild) {
383 throw new RuntimeException("Unimplemented");
384 }
385
386 public boolean beginTreeEdit() {
387 boolean r = inEdit;
388 inEdit = true;
389 return r;
390 }
391
392 public void endTreeEdit() {
393 inEdit = false;
394
395 fireTreeChanged();
396 }
397
398 public void setNodeHeight(NodeRef n, double height) {
399 if (!heightsKnown) {
400 calculateNodeHeights();
401 }
402 FlexibleNode node = (FlexibleNode) n;
403 node.setHeight(height);
404
405 lengthsKnown = false;
406
407 fireTreeChanged();
408 }
409
410 public void setBranchLength(NodeRef n, double length) {
411 if (!lengthsKnown) {
412 calculateBranchLengths();
413 }
414
415 FlexibleNode node = (FlexibleNode) n;
416 node.setLength(length);
417
418 heightsKnown = false;
419
420 fireTreeChanged();
421 }
422
423 public void setHeightsKnown(boolean heightsKnown) {
424 this.heightsKnown = heightsKnown;
425 }
426
427 public void setLengthsKnown(boolean lengthsKnown) {
428 this.lengthsKnown = lengthsKnown;
429 }
430
431 public void setNodeRate(NodeRef n, double rate) {
432 FlexibleNode node = (FlexibleNode) n;
433 node.setRate(rate);
434
435 fireTreeChanged();
436 }
437
438 /**
439 * Set the node heights from the current branch lengths.
440 */
441 protected void calculateNodeHeights() {
442
443 if (!lengthsKnown) {
444 throw new IllegalArgumentException("Branch lengths not known");
445 }
446
447 nodeLengthsToHeights((FlexibleNode) getRoot(), 0.0);
448
449 double maxHeight = 0.0;
450 FlexibleNode node;
451 for (int i = 0; i < getExternalNodeCount(); i++) {
452 node = (FlexibleNode) getExternalNode(i);
453 if (node.getHeight() > maxHeight) {
454 maxHeight = node.getHeight();
455 }
456 }
457
458 for (int i = 0; i < getNodeCount(); i++) {
459 node = (FlexibleNode) getNode(i);
460 node.setHeight(maxHeight - node.getHeight());
461 }
462
463 heightsKnown = true;
464 }
465
466 /**
467 * Set the node heights from the current node branch lengths. Actually
468 * sets distance from root so the heights then need to be reversed.
469 */
470 private void nodeLengthsToHeights(FlexibleNode node, double height) {
471
472 double newHeight = height;
473
474 if (node.getLength() > 0.0) {
475 newHeight += node.getLength();
476 }
477
478 node.setHeight(newHeight);
479
480 for (int i = 0; i < node.getChildCount(); i++) {
481 nodeLengthsToHeights(node.getChild(i), newHeight);
482 }
483 }
484
485 /**
486 * Calculate branch lengths from the current node heights.
487 */
488 protected void calculateBranchLengths() {
489
490 nodeHeightsToLengths((FlexibleNode) getRoot(), getRootHeight());
491
492 lengthsKnown = true;
493 }
494
495 /**
496 * Calculate branch lengths from the current node heights.
497 */
498 private void nodeHeightsToLengths(FlexibleNode node, double height) {
499
500 node.setLength(height - node.getHeight());
501
502 for (int i = 0; i < node.getChildCount(); i++) {
503 nodeHeightsToLengths(node.getChild(i), node.getHeight());
504 }
505
506 }
507
508 /**
509 * Re-root the tree on the branch above the given node at the given height.
510 */
511 public void changeRoot(NodeRef node, double height) {
512 FlexibleNode node1 = (FlexibleNode) node;
513 FlexibleNode parent = node1.getParent();
514
515 double l1 = height - getNodeHeight(node);
516 if (l1 < 0.0) {
517 throw new IllegalArgumentException("New root height less than the node's height");
518 }
519
520 double l2 = getNodeHeight(parent) - height;
521 if (l2 < 0.0) {
522 throw new IllegalArgumentException("New root height above the node's parent's height");
523 }
524
525 changeRoot(node, l1, l2);
526 }
527
528 /**
529 * Re-root the tree on the branch above the given node with the given branch lengths.
530 */
531 public void changeRoot(NodeRef node, double l1, double l2) {
532
533 FlexibleNode node1 = (FlexibleNode) node;
534 FlexibleNode parent = node1.getParent();
535 if (parent == null || parent == root) {
536 // the node is already the root so nothing to do...
537 return;
538 }
539
540 beginTreeEdit();
541
542 if (!lengthsKnown) {
543 calculateBranchLengths();
544 }
545
546 FlexibleNode parent2 = parent.getParent();
547
548 swapParentNode(parent, parent2, null);
549
550 // the root is now free so use it as the root again
551 parent.removeChild(node1);
552 root.addChild(node1);
553 root.addChild(parent);
554
555 node1.setLength(l1);
556 parent.setLength(l2);
557
558 heightsKnown = false;
559 String t = toString();
560
561 endTreeEdit();
562 }
563
564 /**
565 * Work up through the tree putting the parent into the child.
566 */
567 private void swapParentNode(FlexibleNode node, FlexibleNode parent, FlexibleNode child) {
568
569 if (parent != null) {
570 FlexibleNode parent2 = parent.getParent();
571
572 swapParentNode(parent, parent2, node);
573
574 if (child != null) {
575 node.removeChild(child);
576 child.addChild(node);
577 node.setLength(child.getLength());
578 }
579
580 } else {
581 // First remove child from the root
582 node.removeChild(child);
583 if (node.getChildCount() > 1) {
584 throw new IllegalArgumentException("Trees must be binary");
585 }
586
587 FlexibleNode tmp = node.getChild(0);
588 node.removeChild(tmp);
589 child.addChild(tmp);
590 tmp.setLength(tmp.getLength() + child.getLength());
591 }
592
593 }
594
595 /**
596 * Resolve tree so that it is fully bifurcating. Resolving nodes arbitrarily
597 */
598 public void resolveTree() {
599
600 for (int i = 0; i < getInternalNodeCount(); i++) {
601
602 FlexibleNode node = ((FlexibleNode) getInternalNode(i));
603
604 if (node.getChildCount() > 2) {
605 resolveNode(node);
606 } else if (node.getChildCount() == 1) {
607 FlexibleNode parent = node.getParent();
608 if (parent != null) {
609 // remove the degree 2 node and add its child to its parent
610 FlexibleNode child = node.getChild(0);
611 child.setParent(parent);
612 parent.removeChild(node);
613 parent.addChild(child);
614 } else {
615 // the root is a degree 1 node so make the root the node
616 root = node;
617 }
618 }
619
620 }
621
622 adoptNodes(root, null);
623
624 fireTreeChanged();
625 }
626
627 /**
628 * Resolve a node so that it is fully bifurcating.
629 */
630 private void resolveNode(FlexibleNode node) {
631
632 while (node.getChildCount() > 2) {
633 FlexibleNode node0 = node.getChild(0);
634 FlexibleNode node1 = node.getChild(1);
635
636 node.removeChild(node0);
637 node.removeChild(node1);
638
639 FlexibleNode node2 = node.getShallowCopy();
640 node2.addChild(node0);
641 node2.addChild(node1);
642 node2.setLength(0.0);
643
644 node.addChild(node2);
645 }
646 }
647
648 /**
649 * Sets an named attribute for a given node.
650 *
651 * @param node the node whose attribute is being set.
652 * @param name the name of the attribute.
653 * @param value the new value of the attribute.
654 */
655 public void setNodeAttribute(NodeRef node, String name, Object value) {
656 ((FlexibleNode) node).setAttribute(name, value);
657
658 fireTreeChanged();
659 }
660
661 /**
662 * @param node the node whose attribute is being fetched.
663 * @param name the name of the attribute of interest.
664 * @return an object representing the named attributed for the given node.
665 */
666 public Object getNodeAttribute(NodeRef node, String name) {
667 return ((FlexibleNode) node).getAttribute(name);
668 }
669
670 /**
671 * @return a key set of attribute names available for this node.
672 */
673 public Iterator getNodeAttributeNames(NodeRef node) {
674 return ((FlexibleNode) node).getAttributeNames();
675 }
676
677 // **************************************************************
678 // TaxonList IMPLEMENTATION
679 // **************************************************************
680
681 /**
682 * @return a count of the number of taxa in the list.
683 */
684 public int getTaxonCount() {
685 return getExternalNodeCount();
686 }
687
688 /**
689 * @return the ith taxon in the list.
690 */
691 public Taxon getTaxon(int taxonIndex) {
692 return ((FlexibleNode) getExternalNode(taxonIndex)).getTaxon();
693 }
694
695 /**
696 * @return the ID of the taxon of the ith external node. If it doesn't have
697 * a taxon, returns the ID of the node itself.
698 */
699 public String getTaxonId(int taxonIndex) {
700 Taxon taxon = getTaxon(taxonIndex);
701 if (taxon != null)
702 return taxon.getId();
703 else
704 return ((FlexibleNode) getExternalNode(taxonIndex)).getId();
705 }
706
707 /**
708 * returns the index of the taxon with the given id.
709 */
710 public int getTaxonIndex(String id) {
711 for (int i = 0, n = getTaxonCount(); i < n; i++) {
712 if (getTaxonId(i).equals(id)) return i;
713 }
714 return -1;
715 }
716
717 /**
718 * returns the index of the given taxon.
719 */
720 public int getTaxonIndex(Taxon taxon) {
721 for (int i = 0, n = getTaxonCount(); i < n; i++) {
722 if (getTaxon(i) == taxon) return i;
723 }
724 return -1;
725 }
726
727 public List<Taxon> asList() {
728 List<Taxon> taxa = new ArrayList<Taxon>();
729 for (int i = 0, n = getTaxonCount(); i < n; i++) {
730 taxa.add(getTaxon(i));
731 }
732 return taxa;
733 }
734
735 public Iterator<Taxon> iterator() {
736 return new Iterator<Taxon>() {
737 private int index = -1;
738
739 public boolean hasNext() {
740 return index < getTaxonCount() - 1;
741 }
742
743 public Taxon next() {
744 index++;
745 return getTaxon(index);
746 }
747
748 public void remove() { /* do nothing */ }
749 };
750 }
751
752 /**
753 * @param taxonIndex the index of the taxon whose attribute is being fetched.
754 * @param name the name of the attribute of interest.
755 * @return an object representing the named attributed for the taxon of the given
756 * external node. If the node doesn't have a taxon then the nodes own attribute
757 * is returned.
758 */
759 public Object getTaxonAttribute(int taxonIndex, String name) {
760 Taxon taxon = getTaxon(taxonIndex);
761 if (taxon != null)
762 return taxon.getAttribute(name);
763 else
764 return ((FlexibleNode) getExternalNode(taxonIndex)).getAttribute(name);
765 }
766
767 // **************************************************************
768 // MutableTaxonList IMPLEMENTATION
769 // **************************************************************
770
771 public int addTaxon(Taxon taxon) {
772 throw new IllegalArgumentException("Cannot add taxon to a MutableTree");
773 }
774
775 public boolean removeTaxon(Taxon taxon) {
776 throw new IllegalArgumentException("Cannot add taxon to a MutableTree");
777 }
778
779 /**
780 * Sets the ID of the taxon of the ith external node. If it doesn't have
781 * a taxon, sets the ID of the node itself.
782 */
783 public void setTaxonId(int taxonIndex, String id) {
784 Taxon taxon = getTaxon(taxonIndex);
785 if (taxon != null)
786 taxon.setId(id);
787 else
788 ((FlexibleNode) getExternalNode(taxonIndex)).setId(id);
789
790 fireTreeChanged();
791 fireTaxaChanged();
792 }
793
794 /**
795 * Sets an named attribute for the taxon of a given external node. If the node
796 * doesn't have a taxon then the attribute is added to the node itself.
797 *
798 * @param taxonIndex the index of the taxon whose attribute is being set.
799 * @param name the name of the attribute.
800 * @param value the new value of the attribute.
801 */
802 public void setTaxonAttribute(int taxonIndex, String name, Object value) {
803 Taxon taxon = getTaxon(taxonIndex);
804 if (taxon != null)
805 taxon.setAttribute(name, value);
806 else
807 ((FlexibleNode) getExternalNode(taxonIndex)).setAttribute(name, value);
808
809 fireTreeChanged();
810 fireTaxaChanged();
811 }
812
813 // **************************************************************
814 // Identifiable IMPLEMENTATION
815 // **************************************************************
816
817 protected String id = null;
818
819 /**
820 * @return the id.
821 */
822 public String getId() {
823 return id;
824 }
825
826 /**
827 * Sets the id.
828 */
829 public void setId(String id) {
830 this.id = id;
831
832 fireTreeChanged();
833 }
834
835 // **************************************************************
836 // Attributable IMPLEMENTATION
837 // **************************************************************
838
839 private Attributable.AttributeHelper attributes = null;
840
841 /**
842 * Sets an named attribute for this object.
843 *
844 * @param name the name of the attribute.
845 * @param value the new value of the attribute.
846 */
847 public void setAttribute(String name, Object value) {
848 if (attributes == null)
849 attributes = new Attributable.AttributeHelper();
850 attributes.setAttribute(name, value);
851
852 fireTreeChanged();
853 }
854
855 /**
856 * @param name the name of the attribute of interest.
857 * @return an object representing the named attributed for this object.
858 */
859 public Object getAttribute(String name) {
860 if (attributes == null)
861 return null;
862 else
863 return attributes.getAttribute(name);
864 }
865
866 /**
867 * @return an iterator of the attributes that this object has.
868 */
869 public Iterator<String> getAttributeNames() {
870 if (attributes == null)
871 return null;
872 else
873 return attributes.getAttributeNames();
874 }
875
876 public void addMutableTreeListener(MutableTreeListener listener) {
877 mutableTreeListeners.add(listener);
878 }
879
880 private void fireTreeChanged() {
881 for (MutableTreeListener mutableTreeListener : mutableTreeListeners) {
882 (mutableTreeListener).treeChanged(this);
883 }
884 }
885
886 private final ArrayList<MutableTreeListener> mutableTreeListeners = new ArrayList<MutableTreeListener>();
887
888 public void addMutableTaxonListListener(MutableTaxonListListener listener) {
889 mutableTaxonListListeners.add(listener);
890 }
891
892 private void fireTaxaChanged() {
893 for (MutableTaxonListListener mutableTaxonListListener : mutableTaxonListListeners) {
894 (mutableTaxonListListener).taxaChanged(this);
895 }
896 }
897
898 private final ArrayList<MutableTaxonListListener> mutableTaxonListListeners = new ArrayList<MutableTaxonListListener>();
899
900 /**
901 * @return a string containing a newick representation of the tree
902 */
903 public String toString() {
904 return TreeUtils.newick(this);
905 }
906
907 /**
908 * @return whether two trees have the same topology
909 */
910 public boolean equals(Object obj) {
911 if (obj == null) {
912 return false;
913 }
914
915 if (!(obj instanceof Tree)) {
916 throw new IllegalArgumentException("FlexibleTree.equals can only compare instances of Tree");
917 }
918 return TreeUtils.equal(this, (Tree) obj);
919 }
920
921 // **************************************************************
922 // Private Stuff
923 // **************************************************************
924
925 /**
926 * root node
927 */
928 FlexibleNode root;
929
930 /**
931 * list of internal nodes (including root)
932 */
933 FlexibleNode[] nodes = null;
934
935 /**
936 * number of nodes (including root and tips)
937 */
938 int nodeCount;
939
940 /**
941 * number of external nodes
942 */
943 int externalNodeCount;
944
945 /**
946 * number of internal nodes (including root)
947 */
948 int internalNodeCount;
949
950 /**
951 * holds the units of the trees branches.
952 */
953 private Type units = Type.SUBSTITUTIONS;
954
955 boolean inEdit = false;
956
957 boolean heightsKnown = false;
958 boolean lengthsKnown = false;
959 }