Debian Codegraph @

src/dr/evolution/tree/MutableTree.java

raw symbols SCIP
1 /*
2 * MutableTree.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.MutableTaxonList;
29 import dr.math.MathUtils;
30
31 /**
32 * Interface for a phylogenetic or genealogical tree.
33 *
34 * @version $Id: MutableTree.java,v 1.22 2006/07/28 11:27:32 rambaut Exp $
35 *
36 * @author Andrew Rambaut
37 * @author Alexei Drummond
38 */
39 public interface MutableTree extends Tree, MutableTaxonList {
40
41 public class InvalidTreeException extends Exception {
42 /**
43 *
44 */
45 private static final long serialVersionUID = 1955744780140327882L;
46
47 public InvalidTreeException(String message) { super(message); }
48 }
49
50 // return true if tree already in edit mode
51 boolean beginTreeEdit();
52
53 void endTreeEdit();
54
55 /**
56 * Add child to the children of parent.
57 * @throws IllegalArgumentException If child is already a child of parent
58 */
59 void addChild(NodeRef parent, NodeRef child);
60
61 /**
62 * Removes child from the children of parent.
63 * @throws IllegalArgumentException If child is not a child of parent
64 */
65 void removeChild(NodeRef parent, NodeRef child);
66
67 /**
68 * Replace child with another
69 * @param node
70 * @param child of node to replace
71 * @param newChild replacment child
72 */
73 void replaceChild(NodeRef node, NodeRef child, NodeRef newChild);
74
75 /**
76 * Will throw an exception if any nodes have this node as their children.
77 */
78 void setRoot(NodeRef root);
79
80
81 /**
82 * set the height of the ith node in the tree (where the first n are internal).
83 */
84 void setNodeHeight(NodeRef node, double height);
85
86 /**
87 * set the rate of the ith node in the tree (where the first n are internal).
88 */
89 void setNodeRate(NodeRef node, double height);
90
91 /**
92 * set the length of the branch above the ith node in the tree (where the first n are internal).
93 */
94 void setBranchLength(NodeRef node, double length);
95
96 /**
97 * Sets an named attribute for a given node.
98 * @param node the node whose attribute is being set.
99 * @param name the name of the attribute.
100 * @param value the new value of the attribute.
101 */
102 public void setNodeAttribute(NodeRef node, String name, Object value);
103
104 /**
105 * Adds a listener to this tree.
106 */
107 void addMutableTreeListener(MutableTreeListener listener);
108
109 public class Utils {
110
111 public static int order(MutableTree tree, NodeRef node) {
112
113 if (tree.isExternal(node)) {
114 return node.getNumber();
115 } else {
116 NodeRef child1 = tree.getChild(node, 0);
117 NodeRef child2 = tree.getChild(node, 1);
118
119 int num1 = order(tree, child1);
120 int num2 = order(tree, child2);
121
122 if (num1 > num2) {
123 // swap child order
124 tree.removeChild(node, child1);
125 tree.removeChild(node, child2);
126 tree.addChild(node, child2);
127 tree.addChild(node, child1);
128 }
129 return Math.min(num1, num2);
130 }
131 }
132
133 /**
134 * Multiples all node heights by the given scale.
135 */
136 public static void scaleNodeHeights(MutableTree tree, double scale) {
137 for (int i = 0; i < tree.getExternalNodeCount(); i++) {
138 NodeRef node = tree.getExternalNode(i);
139 tree.setNodeHeight(node, tree.getNodeHeight(node)*scale);
140 }
141
142 for (int i = 0; i < tree.getInternalNodeCount(); i++) {
143 NodeRef node = tree.getInternalNode(i);
144 tree.setNodeHeight(node, tree.getNodeHeight(node)*scale);
145 }
146 }
147
148 /**
149 * This method makes sure all internal nodes are at least as tall as their children.
150 * Following this method call there will be no negative branches, but some node heights may
151 * have changed
152 */
153 public static void correctHeightsForTips(MutableTree tree) {
154 correctHeightsForTips(tree, tree.getRoot());
155 }
156
157 /**
158 * This method makes sure all internal nodes from the given node down are at least as tall
159 * as their children.
160 * Following this method call there will be no negative branches, but some node heights may
161 * have changed
162 */
163 private static void correctHeightsForTips(MutableTree tree, NodeRef node) {
164
165 if( !tree.isExternal(node) ) {
166 // pre-order recursion
167 for(int i = 0; i < tree.getChildCount(node); i++) {
168 correctHeightsForTips(tree, tree.getChild(node, i));
169 }
170 }
171
172 if( !tree.isRoot(node) ) {
173 final double parentHeight = tree.getNodeHeight(tree.getParent(node));
174
175 if( parentHeight <= tree.getNodeHeight(node) ) {
176 // set the parent height to be slightly above this node's height
177 // picks
178 double height = tree.getNodeHeight(node);
179 height += tree.getNodeHeight(tree.getRoot()) * (MathUtils.nextDouble() * 0.001);
180 tree.setNodeHeight(tree.getParent(node), height);
181 }
182 }
183 }
184 }
185 }