Debian Codegraph @

src/dr/evolution/tree/FlexibleNode.java

raw symbols SCIP
1 /*
2 * FlexibleNode.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.Taxon;
29 import dr.util.Attributable;
30
31 import java.util.Iterator;
32
33 /**
34 * A flexible implementation of the Node interface. Can have both node heights
35 * and node lengths. Note that the height and length are not garenteed to both
36 * be correct. Which is currently in use and the conversion between them is handled
37 * by FlexibleTree.
38 *
39 * @author Andrew Rambaut
40 * @author Alexei Drummond
41 * @version $Id: FlexibleNode.java,v 1.9 2005/06/20 16:06:02 rambaut Exp $
42 */
43 public class FlexibleNode implements NodeRef, Attributable {
44
45 /**
46 * parent node
47 */
48 private FlexibleNode parent;
49
50 /**
51 * number of node
52 */
53 private int nodeNumber;
54
55 /**
56 * height of this node
57 */
58 private double height;
59
60 /**
61 * length of the branch to the parent
62 */
63 private double length;
64
65 /**
66 * instantaneous rate at this node
67 */
68 private double rate;
69
70 /**
71 * the taxon if node is external
72 */
73 private Taxon taxon = null;
74
75 //
76 // Private stuff
77 //
78
79 private FlexibleNode[] child;
80
81 /**
82 * constructor default node
83 */
84 public FlexibleNode() {
85 parent = null;
86 child = null;
87 height = 0.0;
88 rate = 1.0;
89
90 nodeNumber = 0;
91 }
92
93 /**
94 * constructor default node
95 */
96 public FlexibleNode(Taxon taxon) {
97 parent = null;
98 child = null;
99 height = 0.0;
100 rate = 1.0;
101
102 nodeNumber = 0;
103 this.taxon = taxon;
104 }
105
106 public FlexibleNode(FlexibleNode node) {
107 parent = null;
108
109 setHeight(node.getHeight());
110 setLength(node.getLength());
111 setRate(node.getRate());
112 setId(node.getId());
113 setNumber(node.getNumber());
114 setTaxon(node.getTaxon());
115
116 child = null;
117
118 for (int i = 0; i < node.getChildCount(); i++) {
119 addChild(new FlexibleNode(node.getChild(i)));
120 }
121 }
122
123 /**
124 * constructor used to clone a node and all children
125 */
126 public FlexibleNode(Tree tree, NodeRef node) {
127 this(tree, node, false);
128 // Unbelievable code duplication
129
130 // parent = null;
131 // setHeight(tree.getNodeHeight(node));
132 // setLength(tree.getBranchLength(node));
133 // setRate(tree.getNodeRate(node));
134 // setId(tree.getTaxonId(node.getNumber()));
135 // setNumber(node.getNumber());
136 // setTaxon(tree.getNodeTaxon(node));
137 //
138 // child = null;
139 //
140 // for (int i = 0; i < tree.getChildCount(node); i++) {
141 // addChild(new FlexibleNode(tree, tree.getChild(node, i)));
142 // }
143 }
144
145 public FlexibleNode(Tree tree, NodeRef node, boolean copyAttributes) {
146 parent = null;
147 setHeight(tree.getNodeHeight(node));
148 setLength(tree.getBranchLength(node));
149 setRate(tree.getNodeRate(node));
150 setId(tree.getTaxonId(node.getNumber()));
151 setNumber(node.getNumber());
152 setTaxon(tree.getNodeTaxon(node));
153
154 child = null;
155
156 for (int i = 0; i < tree.getChildCount(node); i++) {
157 addChild(new FlexibleNode(tree, tree.getChild(node, i), copyAttributes));
158 }
159 if (copyAttributes) {
160 Iterator iter = tree.getNodeAttributeNames(node);
161 if (iter != null) {
162 while (iter.hasNext()) {
163 String name = (String) iter.next();
164 this.setAttribute(name, tree.getNodeAttribute(node, name));
165 }
166 }
167 }
168 }
169
170 public FlexibleNode getDeepCopy() {
171 return new FlexibleNode(this);
172 }
173
174 public FlexibleNode getShallowCopy() {
175 FlexibleNode copy = new FlexibleNode();
176
177 copy.setHeight(getHeight());
178 copy.setLength(getLength());
179 copy.setRate(getRate());
180 copy.setId(getId());
181 copy.setNumber(getNumber());
182 copy.setTaxon(getTaxon());
183
184 return copy;
185 }
186
187 /**
188 * Returns the parent node of this node.
189 */
190 public final FlexibleNode getParent() {
191 return parent;
192 }
193
194 /**
195 * Set the parent node of this node.
196 */
197 public void setParent(FlexibleNode node) {
198 parent = node;
199 }
200
201 /**
202 * Get the height of this node.
203 */
204 public final double getHeight() {
205 return height;
206 }
207
208 /**
209 * Set the height of this node.
210 */
211 public final void setHeight(double value) {
212 height = value;
213 }
214
215 /**
216 * Get the length of the branch to the node's parent.
217 */
218 public final double getLength() {
219 return length;
220 }
221
222 /**
223 * Set the length of the branch to the node's parent.
224 */
225 public final void setLength(double value) {
226 length = value;
227 }
228
229 /**
230 * Get the rate at this node.
231 */
232 public final double getRate() {
233 return rate;
234 }
235
236 /**
237 * Set the rate at this node.
238 */
239 public final void setRate(double value) {
240 rate = value;
241 }
242
243 public void setNumber(int n) {
244 nodeNumber = n;
245 }
246
247 public int getNumber() {
248 return nodeNumber;
249 }
250
251 public void setTaxon(Taxon taxon) {
252 this.taxon = taxon;
253 }
254
255 public Taxon getTaxon() {
256 return taxon;
257 }
258
259 /**
260 * get child node
261 *
262 * @param n number of child
263 * @return child node
264 */
265 public FlexibleNode getChild(int n) {
266 return child[n];
267 }
268
269 public boolean hasChild(FlexibleNode node) {
270
271 for (int i = 0, n = getChildCount(); i < n; i++) {
272 if (node == child[i]) return true;
273 }
274 return false;
275 }
276
277 /**
278 * add new child node
279 *
280 * @param n new child node
281 */
282 public void addChild(FlexibleNode n) {
283 insertChild(n, getChildCount());
284 }
285
286 /**
287 * add new child node (insertion at a specific position)
288 *
289 * @param n new child node
290 * + @param pos position
291 */
292 public void insertChild(FlexibleNode n, int pos) {
293 int numChildren = getChildCount();
294
295 FlexibleNode[] newChild = new FlexibleNode[numChildren + 1];
296
297 // AR 28/05/08
298 // This doesn't work because pos can be zero and (numChildren - pos) can be zero in which case an NullPointerException is thrown.
299 // We could check for these special cases but we are generally only moving one or two nodes so it is unlikely to be an efficiency
300 // gain for using arraycopy.
301 // System.arraycopy(child, 0, newChild, 0, pos);
302 // newChild[pos] = n;
303 // System.arraycopy(child, pos, newChild, pos + 1, numChildren - pos);
304
305 for (int i = 0; i < pos; i++) {
306 newChild[i] = child[i];
307 }
308 newChild[pos] = n;
309 for (int i = pos; i < numChildren; i++) {
310 newChild[i + 1] = child[i];
311 }
312
313 child = newChild;
314
315 n.setParent(this);
316 }
317
318 /**
319 * remove child
320 *
321 * @param n child to be removed
322 */
323 public FlexibleNode removeChild(FlexibleNode n) {
324 int numChildren = getChildCount();
325 FlexibleNode[] newChild = new FlexibleNode[numChildren - 1];
326
327 int j = 0;
328 boolean found = false;
329 for (int i = 0; i < numChildren; i++) {
330 if (child[i] != n) {
331 newChild[j] = child[i];
332 j++;
333 } else
334 found = true;
335 }
336
337 if (!found)
338 throw new IllegalArgumentException("Nonexistent child");
339
340 //remove parent link from removed child!
341 n.setParent(null);
342
343 child = newChild;
344
345 return n;
346 }
347
348 /**
349 * remove child
350 *
351 * @param n number of child to be removed
352 */
353 public FlexibleNode removeChild(int n) {
354 int numChildren = getChildCount();
355
356 if (n >= numChildren) {
357 throw new IllegalArgumentException("Nonexistent child");
358 }
359
360 return removeChild(child[n]);
361 }
362
363
364 /**
365 * check whether this node has any children
366 *
367 * @return result (true or false)
368 */
369 public boolean hasChildren() {
370 return (getChildCount() != 0);
371 }
372
373 /**
374 * check whether this node is an external node
375 *
376 * @return result (true or false)
377 */
378 public boolean isExternal() {
379 return !hasChildren();
380 }
381
382 /**
383 * check whether this node is a root node
384 *
385 * @return result (true or false)
386 */
387 public boolean isRoot() {
388 return (getParent() == null);
389 }
390
391
392 /**
393 * Returns the number of children this node has.
394 */
395 public final int getChildCount() {
396 if (child == null) return 0;
397 return child.length;
398 }
399
400 // **************************************************************
401 // Identifiable IMPLEMENTATION
402 // **************************************************************
403
404 private String id = null;
405
406 /**
407 * @return the id as a string.
408 */
409 public String getId() {
410 return id;
411 }
412
413 /**
414 * set the id as a string.
415 */
416 public void setId(String id) {
417 this.id = id;
418 }
419
420 // **************************************************************
421 // Attributable IMPLEMENTATION
422 // **************************************************************
423
424 private Attributable.AttributeHelper attributes = null;
425
426 /**
427 * Sets an named attribute for this object.
428 *
429 * @param name the name of the attribute.
430 * @param value the new value of the attribute.
431 */
432 public void setAttribute(String name, Object value) {
433 if (attributes == null)
434 attributes = new Attributable.AttributeHelper();
435 attributes.setAttribute(name, value);
436 }
437
438 /**
439 * @param name the name of the attribute of interest.
440 * @return an object representing the named attributed for this object.
441 */
442 public Object getAttribute(String name) {
443
444 if (attributes == null) {
445 return null;
446 } else {
447 return attributes.getAttribute(name);
448 }
449 }
450
451 /**
452 * @return an iterator of the attributes that this object has.
453 */
454 public Iterator<String> getAttributeNames() {
455 if (attributes == null)
456 return null;
457 else
458 return attributes.getAttributeNames();
459 }
460 }