1 | // SimpleNode.java |
---|
2 | // |
---|
3 | // (c) 1999-2001 PAL Development Core Team |
---|
4 | // |
---|
5 | // This package may be distributed under the |
---|
6 | // terms of the Lesser GNU General Public License (LGPL) |
---|
7 | |
---|
8 | |
---|
9 | package de.ugoe.cs.autoquest.tasktrees.alignment.pal.tree; |
---|
10 | |
---|
11 | |
---|
12 | import java.util.ArrayList; |
---|
13 | import de.ugoe.cs.autoquest.tasktrees.alignment.algorithms.NumberSequence; |
---|
14 | import de.ugoe.cs.autoquest.tasktrees.alignment.pal.misc.Identifier; |
---|
15 | |
---|
16 | |
---|
17 | |
---|
18 | /** |
---|
19 | * |
---|
20 | * @version $Id: SimpleNode.java,v 1.20 2002/01/14 04:16:53 matt Exp $ |
---|
21 | * |
---|
22 | * @author Korbinian Strimmer |
---|
23 | * @author Alexei Drummond |
---|
24 | */ |
---|
25 | public class FengDoolittleNode implements Node { |
---|
26 | |
---|
27 | /** parent node */ |
---|
28 | private Node parent; |
---|
29 | |
---|
30 | /** number of node as displayed */ |
---|
31 | private int number; |
---|
32 | |
---|
33 | /** sequences associated with node */ |
---|
34 | private ArrayList<NumberSequence> sequences; |
---|
35 | |
---|
36 | /** length of branch to parent node */ |
---|
37 | private double length; |
---|
38 | |
---|
39 | /** standard error of length of branch to parent node */ |
---|
40 | private double lengthSE; |
---|
41 | |
---|
42 | /** height of this node */ |
---|
43 | private double height; |
---|
44 | |
---|
45 | /** identifier of node/associated branch */ |
---|
46 | private Identifier identifier; |
---|
47 | |
---|
48 | private Node[] child; |
---|
49 | |
---|
50 | // the following constructors should eventually become |
---|
51 | // "friendly" to prevent anyone calling them directly. |
---|
52 | // Instead, the NodeFactory should be used! |
---|
53 | |
---|
54 | /** constructor default node */ |
---|
55 | public FengDoolittleNode() |
---|
56 | { |
---|
57 | parent = null; |
---|
58 | child = null; |
---|
59 | length = 0.0; |
---|
60 | lengthSE = 0.0; |
---|
61 | height = 0.0; |
---|
62 | identifier = Identifier.ANONYMOUS; |
---|
63 | |
---|
64 | number = 0; |
---|
65 | sequences = new ArrayList<NumberSequence>(); |
---|
66 | } |
---|
67 | |
---|
68 | public FengDoolittleNode(String name, double branchLength) { |
---|
69 | this(); |
---|
70 | identifier = new Identifier(name); |
---|
71 | length = branchLength; |
---|
72 | } |
---|
73 | |
---|
74 | |
---|
75 | public void reset() |
---|
76 | { |
---|
77 | parent = null; |
---|
78 | child = null; |
---|
79 | length = 0.0; |
---|
80 | lengthSE = 0.0; |
---|
81 | height = 0.0; |
---|
82 | identifier = Identifier.ANONYMOUS; |
---|
83 | |
---|
84 | number = 0; |
---|
85 | sequences = null; |
---|
86 | } |
---|
87 | |
---|
88 | |
---|
89 | /** |
---|
90 | * Returns the parent node of this node. |
---|
91 | */ |
---|
92 | public final Node getParent() { |
---|
93 | return parent; |
---|
94 | } |
---|
95 | |
---|
96 | /** Set the parent node of this node. */ |
---|
97 | public void setParent(Node node) |
---|
98 | { |
---|
99 | parent = node; |
---|
100 | } |
---|
101 | |
---|
102 | /** |
---|
103 | * removes parent. |
---|
104 | */ |
---|
105 | public final void removeParent() { |
---|
106 | parent = null; |
---|
107 | } |
---|
108 | |
---|
109 | public void addSequence(NumberSequence s) { |
---|
110 | sequences.add(s); |
---|
111 | } |
---|
112 | |
---|
113 | |
---|
114 | /** |
---|
115 | * Returns the sequence at this node, in the form of a String. |
---|
116 | */ |
---|
117 | public String getSequenceString(int index) { |
---|
118 | return sequences.get(index).getSequence().toString(); |
---|
119 | } |
---|
120 | |
---|
121 | /** |
---|
122 | * Returns the sequence at this node, in the form of an array of bytes. |
---|
123 | */ |
---|
124 | public NumberSequence getSequence(int index) { |
---|
125 | return sequences.get(index); |
---|
126 | } |
---|
127 | |
---|
128 | /** |
---|
129 | * Sets the sequence at this node, in the form of an array of bytes. |
---|
130 | */ |
---|
131 | public void setSequence(int index,NumberSequence s) { |
---|
132 | sequences.set(index, s); |
---|
133 | } |
---|
134 | |
---|
135 | /** |
---|
136 | * Get the length of the branch attaching this node to its parent. |
---|
137 | */ |
---|
138 | public final double getBranchLength() { |
---|
139 | return length; |
---|
140 | } |
---|
141 | |
---|
142 | /** |
---|
143 | * Set the length of the branch attaching this node to its parent. |
---|
144 | */ |
---|
145 | public final void setBranchLength(double value) { |
---|
146 | length = value; |
---|
147 | } |
---|
148 | |
---|
149 | /** |
---|
150 | * Get the length SE of the branch attaching this node to its parent. |
---|
151 | */ |
---|
152 | public final double getBranchLengthSE() { |
---|
153 | return lengthSE; |
---|
154 | } |
---|
155 | |
---|
156 | /** |
---|
157 | * Set the length SE of the branch attaching this node to its parent. |
---|
158 | */ |
---|
159 | public final void setBranchLengthSE(double value) { |
---|
160 | lengthSE = value; |
---|
161 | } |
---|
162 | |
---|
163 | |
---|
164 | /** |
---|
165 | * Get the height of this node relative to the most recent node. |
---|
166 | */ |
---|
167 | public final double getNodeHeight() { |
---|
168 | return height; |
---|
169 | } |
---|
170 | |
---|
171 | /** |
---|
172 | * Set the height of this node relative to the most recent node. |
---|
173 | */ |
---|
174 | public final void setNodeHeight(double value) { |
---|
175 | height = Math.abs(value); |
---|
176 | } |
---|
177 | |
---|
178 | /** |
---|
179 | * Returns the identifier for this node. |
---|
180 | */ |
---|
181 | public final Identifier getIdentifier() { |
---|
182 | return identifier; |
---|
183 | } |
---|
184 | |
---|
185 | /** |
---|
186 | * Set identifier for this node. |
---|
187 | */ |
---|
188 | public final Identifier setIdentifier(Identifier id) { |
---|
189 | identifier = id; |
---|
190 | return identifier; |
---|
191 | } |
---|
192 | |
---|
193 | public void setNumber(int n) { |
---|
194 | number = n; |
---|
195 | } |
---|
196 | |
---|
197 | public int getNumber() { |
---|
198 | return number; |
---|
199 | } |
---|
200 | |
---|
201 | /** |
---|
202 | * get child node |
---|
203 | * |
---|
204 | * @param n number of child |
---|
205 | * |
---|
206 | * @return child node |
---|
207 | */ |
---|
208 | public Node getChild(int n) |
---|
209 | { |
---|
210 | return child[n]; |
---|
211 | } |
---|
212 | |
---|
213 | /** |
---|
214 | * set child node |
---|
215 | * |
---|
216 | * @param n number |
---|
217 | * @node node new child node |
---|
218 | */ |
---|
219 | public void setChild(int n, Node node) |
---|
220 | { |
---|
221 | child[n] = node; |
---|
222 | child[n].setParent(this); |
---|
223 | } |
---|
224 | |
---|
225 | /** |
---|
226 | * check whether this node is an internal node |
---|
227 | * |
---|
228 | * @return result (true or false) |
---|
229 | */ |
---|
230 | public boolean hasChildren() |
---|
231 | { |
---|
232 | return !isLeaf(); |
---|
233 | } |
---|
234 | |
---|
235 | /** |
---|
236 | * check whether this node is an external node |
---|
237 | * |
---|
238 | * @return result (true or false) |
---|
239 | */ |
---|
240 | public boolean isLeaf() { |
---|
241 | return (getChildCount() == 0); |
---|
242 | } |
---|
243 | |
---|
244 | /** |
---|
245 | * check whether this node is a root node |
---|
246 | * |
---|
247 | * @return result (true or false) |
---|
248 | */ |
---|
249 | public boolean isRoot() |
---|
250 | { |
---|
251 | if (parent == null) |
---|
252 | { |
---|
253 | return true; |
---|
254 | } |
---|
255 | else |
---|
256 | { |
---|
257 | return false; |
---|
258 | } |
---|
259 | } |
---|
260 | |
---|
261 | |
---|
262 | /** |
---|
263 | * add new child node |
---|
264 | * |
---|
265 | * @param n new child node |
---|
266 | */ |
---|
267 | public void addChild(Node n) |
---|
268 | { |
---|
269 | insertChild(n, getChildCount()); |
---|
270 | } |
---|
271 | |
---|
272 | /** |
---|
273 | * add new child node (insertion at a specific position) |
---|
274 | * |
---|
275 | * @param n new child node |
---|
276 | + @param pos position |
---|
277 | */ |
---|
278 | public void insertChild(Node n, int pos) |
---|
279 | { |
---|
280 | int numChildren = getChildCount(); |
---|
281 | |
---|
282 | Node[] newChild = new Node[numChildren + 1]; |
---|
283 | |
---|
284 | for (int i = 0; i < pos; i++) |
---|
285 | { |
---|
286 | newChild[i] = child[i]; |
---|
287 | } |
---|
288 | newChild[pos] = n; |
---|
289 | for (int i = pos; i < numChildren; i++) |
---|
290 | { |
---|
291 | newChild[i+1] = child[i]; |
---|
292 | } |
---|
293 | |
---|
294 | child = newChild; |
---|
295 | |
---|
296 | n.setParent(this); |
---|
297 | } |
---|
298 | |
---|
299 | |
---|
300 | /** |
---|
301 | * remove child |
---|
302 | * |
---|
303 | * @param n number of child to be removed |
---|
304 | */ |
---|
305 | public Node removeChild(int n) |
---|
306 | { |
---|
307 | int numChildren = getChildCount(); |
---|
308 | |
---|
309 | if (n >= numChildren) |
---|
310 | { |
---|
311 | throw new IllegalArgumentException("Nonexistent child"); |
---|
312 | } |
---|
313 | Node[] newChild = new Node[numChildren-1]; |
---|
314 | |
---|
315 | for (int i = 0; i < n; i++) |
---|
316 | { |
---|
317 | newChild[i] = child[i]; |
---|
318 | } |
---|
319 | |
---|
320 | for (int i = n; i < numChildren-1; i++) |
---|
321 | { |
---|
322 | newChild[i] = child[i+1]; |
---|
323 | } |
---|
324 | |
---|
325 | Node removed = child[n]; |
---|
326 | |
---|
327 | //remove parent link from removed child! |
---|
328 | removed.setParent(null); |
---|
329 | |
---|
330 | child = newChild; |
---|
331 | |
---|
332 | return removed; |
---|
333 | } |
---|
334 | |
---|
335 | |
---|
336 | |
---|
337 | /** |
---|
338 | * join two children, introducing a new node/branch in the tree |
---|
339 | * that replaces the first child |
---|
340 | * |
---|
341 | * @param n1 number of first child |
---|
342 | * @param n2 number of second child |
---|
343 | */ |
---|
344 | public Node joinChildren( int n1, int n2) { |
---|
345 | |
---|
346 | if (n1 == n2) { |
---|
347 | throw new IllegalArgumentException("CHILDREN MUST BE DIFFERENT"); |
---|
348 | } |
---|
349 | |
---|
350 | int c1, c2; |
---|
351 | if (n2 < n1) |
---|
352 | { |
---|
353 | c1 = n2; |
---|
354 | c2 = n1; |
---|
355 | } |
---|
356 | else |
---|
357 | { |
---|
358 | c1 = n1; |
---|
359 | c2 = n2; |
---|
360 | } |
---|
361 | |
---|
362 | Node newNode = NodeFactory.createNode(); |
---|
363 | |
---|
364 | Node child1 = this.getChild(c1); |
---|
365 | Node child2 = this.getChild(c2); |
---|
366 | |
---|
367 | setChild(c1, newNode); |
---|
368 | newNode.setParent(this); |
---|
369 | this.removeChild(c2); // now parent of child2 = null |
---|
370 | |
---|
371 | newNode.addChild(child1); |
---|
372 | newNode.addChild(child2); |
---|
373 | newNode.setIdentifier(new Identifier(child1.getIdentifier().getName() + " " + child2.getIdentifier().getName())); |
---|
374 | |
---|
375 | return newNode; |
---|
376 | } |
---|
377 | |
---|
378 | |
---|
379 | /** |
---|
380 | * Returns the number of children this node has. |
---|
381 | */ |
---|
382 | public final int getChildCount() { |
---|
383 | if (child == null) return 0; |
---|
384 | return child.length; |
---|
385 | } |
---|
386 | |
---|
387 | |
---|
388 | public ArrayList<NumberSequence> getSequences() { |
---|
389 | return sequences; |
---|
390 | } |
---|
391 | |
---|
392 | |
---|
393 | public void setSequences(ArrayList<NumberSequence> alignSequences) { |
---|
394 | this.sequences = alignSequences; |
---|
395 | } |
---|
396 | |
---|
397 | |
---|
398 | |
---|
399 | } |
---|