Debian Codegraph @

src/dr/evolution/tree/SimpleNode.java

raw symbols SCIP
1 /*
2 * SimpleNode.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
29 import dr.evolution.util.Taxon;
30 import dr.util.Attributable;
31
32 import java.util.Iterator;
33
34 /**
35 * A simple implementation of the Node interface.
36 *
37 * @version $Id: SimpleNode.java,v 1.40 2005/08/16 16:03:17 rambaut Exp $
38 *
39 * @author Andrew Rambaut
40 * @author Alexei Drummond
41 */
42 public class SimpleNode implements NodeRef, Attributable {
43
44 /** parent node */
45 private SimpleNode parent;
46
47 /** number of node */
48 private int nodeNumber;
49
50 /** height of this node */
51 private double height;
52
53 /** instantaneous rate at this node */
54 private double rate;
55
56 /** the taxon if node is external */
57 private Taxon taxon = null;
58
59 //
60 // Private stuff
61 //
62
63 private SimpleNode[] child;
64
65 /** constructor default node */
66 public SimpleNode()
67 {
68 parent = null;
69 child =null;
70 height = 0.0;
71 rate = 1.0;
72
73 nodeNumber = 0;
74 }
75
76 public SimpleNode(SimpleNode node) {
77 parent = null;
78
79 setHeight(node.getHeight());
80 setRate(node.getRate());
81 setId(node.getId());
82 setNumber(node.getNumber());
83 setTaxon(node.getTaxon());
84
85 child = null;
86
87 for (int i = 0; i < node.getChildCount(); i++) {
88 addChild(new SimpleNode(node.getChild(i)));
89 }
90 }
91
92 /** constructor used to clone a node and all children */
93 public SimpleNode(Tree tree, NodeRef node)
94 {
95 parent = null;
96 setHeight(tree.getNodeHeight(node));
97 setRate(tree.getNodeRate(node));
98 final int nodeNumber = node.getNumber();
99 setId(tree.getTaxonId(nodeNumber));
100 setNumber(nodeNumber);
101 setTaxon(tree.getNodeTaxon(node));
102
103 child = null;
104
105 for (int i = 0; i < tree.getChildCount(node); i++) {
106 addChild(new SimpleNode(tree, tree.getChild(node, i)));
107 }
108 }
109
110 public SimpleNode getDeepCopy() {
111 return new SimpleNode(this);
112 }
113
114 /**
115 * Returns the parent node of this node.
116 */
117 public final SimpleNode getParent() {
118 return parent;
119 }
120
121 /** Set the parent node of this node. */
122 public void setParent(SimpleNode node) { parent = node; }
123
124 /**
125 * Get the height of this node.
126 */
127 public final double getHeight() {
128 return height;
129 }
130
131 /**
132 * Set the height of this node.
133 */
134 public final void setHeight(double value) {
135 height = value;
136 }
137
138 /**
139 * Get the rate at this node.
140 */
141 public final double getRate() {
142 return rate;
143 }
144
145 /**
146 * Set the rate at this node.
147 */
148 public final void setRate(double value) {
149 rate = value;
150 }
151
152 public void setNumber(int n) {
153 nodeNumber = n;
154 }
155
156 public int getNumber() {
157 return nodeNumber;
158 }
159
160 public void setTaxon(Taxon taxon) {
161 this.taxon = taxon;
162 }
163
164 public Taxon getTaxon() {
165 return taxon;
166 }
167
168 /**
169 * get child node
170 *
171 * @param n number of child
172 *
173 * @return child node
174 */
175 public SimpleNode getChild(int n)
176 {
177 assert 0 <= n && n < child.length;
178
179 return child[n];
180 }
181
182 public boolean hasChild(SimpleNode node) {
183
184 for (int i = 0, n = getChildCount(); i < n; i++) {
185 if (node == child[i]) return true;
186 }
187 return false;
188 }
189
190 /**
191 * add new child node
192 *
193 * @param n new child node
194 */
195 public void addChild(SimpleNode n)
196 {
197 insertChild(n, getChildCount());
198 }
199
200 /**
201 * add new child node (insertion at a specific position)
202 *
203 * @param n new child node
204 + @param pos position
205 */
206 public void insertChild(SimpleNode n, int pos)
207 {
208 int numChildren = getChildCount();
209
210 SimpleNode[] newChild = new SimpleNode[numChildren + 1];
211
212 for (int i = 0; i < pos; i++)
213 {
214 newChild[i] = child[i];
215 }
216 newChild[pos] = n;
217 for (int i = pos; i < numChildren; i++)
218 {
219 newChild[i+1] = child[i];
220 }
221
222 child = newChild;
223
224 n.setParent(this);
225 }
226
227 /**
228 * remove child
229 *
230 * @param n child to be removed
231 */
232 public SimpleNode removeChild(SimpleNode n)
233 {
234 int numChildren = getChildCount();
235 SimpleNode[] newChild = new SimpleNode[numChildren-1];
236
237 int j = 0;
238 boolean found = false;
239 for (int i = 0; i < numChildren; i++) {
240 if (child[i] != n) {
241 newChild[j] = child[i];
242 j++;
243 } else
244 found = true;
245 }
246
247 if (!found)
248 throw new IllegalArgumentException("Nonexistent child");
249
250 //remove parent link from removed child!
251 n.setParent(null);
252
253 child = newChild;
254
255 return n;
256 }
257
258 /**
259 * remove child
260 *
261 * @param n number of child to be removed
262 */
263 public SimpleNode removeChild(int n)
264 {
265 int numChildren = getChildCount();
266
267 if (n >= numChildren)
268 {
269 throw new IllegalArgumentException("Nonexistent child");
270 }
271
272 return removeChild(child[n]);
273 }
274
275
276 public void replaceChild(SimpleNode childNode, SimpleNode replacment) {
277 for(int nc = 0; nc < child.length; ++nc) {
278 if( child[nc] == childNode ) {
279 replacment.setParent(this);
280 child[nc] = replacment;
281 break;
282 }
283 }
284 }
285
286 /**
287 * check whether this node has any children
288 *
289 * @return result (true or false)
290 */
291 public boolean hasChildren()
292 {
293 return (getChildCount() != 0);
294 }
295
296 /**
297 * check whether this node is an external node
298 *
299 * @return result (true or false)
300 */
301 public boolean isExternal() {
302 return !hasChildren();
303 }
304
305 /**
306 * check whether this node is a root node
307 *
308 * @return result (true or false)
309 */
310 public boolean isRoot()
311 {
312 return (getParent() == null);
313 }
314
315
316 /**
317 * Returns the number of children this node has.
318 */
319 public final int getChildCount() {
320 if (child == null) return 0;
321 return child.length;
322 }
323
324 // **************************************************************
325 // Identifiable IMPLEMENTATION
326 // **************************************************************
327
328 private String id = null;
329
330 /**
331 * @return the id as a string.
332 */
333 public String getId() {
334 return id;
335 }
336
337 /**
338 * set the id as a string.
339 */
340 public void setId(String id) {
341 this.id = id;
342 }
343
344 // **************************************************************
345 // Attributable IMPLEMENTATION
346 // **************************************************************
347
348 private Attributable.AttributeHelper attributes = null;
349
350 /**
351 * Sets an named attribute for this object.
352 * @param name the name of the attribute.
353 * @param value the new value of the attribute.
354 */
355 public void setAttribute(String name, Object value) {
356 if (attributes == null)
357 attributes = new Attributable.AttributeHelper();
358 attributes.setAttribute(name, value);
359 }
360
361 /**
362 * @return an object representing the named attributed for this object.
363 * @param name the name of the attribute of interest.
364 */
365 public Object getAttribute(String name) {
366 if (attributes == null)
367 return null;
368 else
369 return attributes.getAttribute(name);
370 }
371
372 /**
373 * @return an iterator of the attributes that this object has.
374 */
375 public Iterator<String> getAttributeNames() {
376 if (attributes == null)
377 return null;
378 else
379 return attributes.getAttributeNames();
380 }
381 }