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