Class |
Line # |
Actions |
|||
---|---|---|---|---|---|
BinaryNode | 31 | 67 | 48 |
1 | /* | |
2 | * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$) | |
3 | * Copyright (C) $$Year-Rel$$ The Jalview Authors | |
4 | * | |
5 | * This file is part of Jalview. | |
6 | * | |
7 | * Jalview is free software: you can redistribute it and/or | |
8 | * modify it under the terms of the GNU General Public License | |
9 | * as published by the Free Software Foundation, either version 3 | |
10 | * of the License, or (at your option) any later version. | |
11 | * | |
12 | * Jalview is distributed in the hope that it will be useful, but | |
13 | * WITHOUT ANY WARRANTY; without even the implied warranty | |
14 | * of MERCHANTABILITY or FITNESS FOR A PARTICULAR | |
15 | * PURPOSE. See the GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with Jalview. If not, see <http://www.gnu.org/licenses/>. | |
19 | * The Jalview Authors are detailed in the 'AUTHORS' file. | |
20 | */ | |
21 | package jalview.datamodel; | |
22 | ||
23 | import java.awt.Color; | |
24 | ||
25 | /** | |
26 | * Represent a node in a binary tree | |
27 | * | |
28 | * @author $mclamp (probably!)$ | |
29 | * @version $Revision$ | |
30 | */ | |
31 | public class BinaryNode<T> | |
32 | { | |
33 | T element; | |
34 | ||
35 | String name; | |
36 | ||
37 | String label = null; | |
38 | ||
39 | AlignmentAnnotation alignmentAnnotation = null; | |
40 | ||
41 | BinaryNode<T> left; | |
42 | ||
43 | BinaryNode<T> right; | |
44 | ||
45 | BinaryNode<T> parent; | |
46 | ||
47 | /** Bootstrap value */ | |
48 | public int bootstrap; | |
49 | ||
50 | /** DOCUMENT ME!! */ | |
51 | public double dist; | |
52 | ||
53 | /** DOCUMENT ME!! */ | |
54 | public int count; | |
55 | ||
56 | /** DOCUMENT ME!! */ | |
57 | public double height; | |
58 | ||
59 | /** DOCUMENT ME!! */ | |
60 | public float ycount; | |
61 | ||
62 | /** DOCUMENT ME!! */ | |
63 | public Color color = Color.black; | |
64 | ||
65 | /** | |
66 | * if true, node is created to simulate polytomy between parent and its 3 or | |
67 | * more children | |
68 | */ | |
69 | public boolean dummy = false; | |
70 | ||
71 | /** | |
72 | * Creates a new BinaryNode object. | |
73 | */ | |
74 | 563 | public BinaryNode() |
75 | { | |
76 | 563 | left = right = parent = null; |
77 | 563 | bootstrap = 0; |
78 | 563 | dist = 0; |
79 | } | |
80 | ||
81 | /** | |
82 | * Creates a new BinaryNode object. | |
83 | * | |
84 | * @param element | |
85 | * DOCUMENT ME! | |
86 | * @param parent | |
87 | * DOCUMENT ME! | |
88 | * @param name | |
89 | * DOCUMENT ME! | |
90 | */ | |
91 | 396 | public BinaryNode(T element, BinaryNode<T> parent, String name, |
92 | double dist) | |
93 | { | |
94 | 396 | this(); |
95 | 396 | this.element = element; |
96 | 396 | this.parent = parent; |
97 | 396 | this.name = name; |
98 | 396 | this.dist = dist; |
99 | } | |
100 | ||
101 | 396 | public BinaryNode(T element, BinaryNode<T> parent, String name, |
102 | double dist, int bootstrap) | |
103 | { | |
104 | 396 | this(element, parent, name, dist); |
105 | 396 | this.bootstrap = bootstrap; |
106 | } | |
107 | ||
108 | 396 | public BinaryNode(T val, BinaryNode<T> parent, String name, double dist, |
109 | int bootstrap, boolean dummy) | |
110 | { | |
111 | 396 | this(val, parent, name, dist, bootstrap); |
112 | 396 | this.dummy = dummy; |
113 | } | |
114 | ||
115 | /** | |
116 | * DOCUMENT ME! | |
117 | * | |
118 | * @return DOCUMENT ME! | |
119 | */ | |
120 | 114 | public T element() |
121 | { | |
122 | 114 | return element; |
123 | } | |
124 | ||
125 | /** | |
126 | * DOCUMENT ME! | |
127 | * | |
128 | * @param v | |
129 | * DOCUMENT ME! | |
130 | * | |
131 | * @return DOCUMENT ME! | |
132 | */ | |
133 | 283 | public T setElement(T v) |
134 | { | |
135 | 283 | return element = v; |
136 | } | |
137 | ||
138 | /** | |
139 | * DOCUMENT ME! | |
140 | * | |
141 | * @return DOCUMENT ME! | |
142 | */ | |
143 | 5487 | public BinaryNode<T> left() |
144 | { | |
145 | 5487 | return left; |
146 | } | |
147 | ||
148 | /** | |
149 | * DOCUMENT ME! | |
150 | * | |
151 | * @param n | |
152 | * DOCUMENT ME! | |
153 | * | |
154 | * @return DOCUMENT ME! | |
155 | */ | |
156 | 260 | public BinaryNode<T> setLeft(BinaryNode<T> n) |
157 | { | |
158 | 260 | return left = n; |
159 | } | |
160 | ||
161 | /** | |
162 | * DOCUMENT ME! | |
163 | * | |
164 | * @return DOCUMENT ME! | |
165 | */ | |
166 | 3785 | public BinaryNode<T> right() |
167 | { | |
168 | 3785 | return right; |
169 | } | |
170 | ||
171 | /** | |
172 | * DOCUMENT ME! | |
173 | * | |
174 | * @param n | |
175 | * DOCUMENT ME! | |
176 | * | |
177 | * @return DOCUMENT ME! | |
178 | */ | |
179 | 280 | public BinaryNode<T> setRight(BinaryNode<T> n) |
180 | { | |
181 | 280 | return right = n; |
182 | } | |
183 | ||
184 | /** | |
185 | * DOCUMENT ME! | |
186 | * | |
187 | * @return DOCUMENT ME! | |
188 | */ | |
189 | 2120 | public BinaryNode<T> parent() |
190 | { | |
191 | 2120 | return parent; |
192 | } | |
193 | ||
194 | /** | |
195 | * DOCUMENT ME! | |
196 | * | |
197 | * @param n | |
198 | * DOCUMENT ME! | |
199 | * | |
200 | * @return DOCUMENT ME! | |
201 | */ | |
202 | 144 | public BinaryNode<T> setParent(BinaryNode<T> n) |
203 | { | |
204 | 144 | return parent = n; |
205 | } | |
206 | ||
207 | /** | |
208 | * DOCUMENT ME! | |
209 | * | |
210 | * @return DOCUMENT ME! | |
211 | */ | |
212 | 278 | public boolean isLeaf() |
213 | { | |
214 | 278 | return (left == null) && (right == null); |
215 | } | |
216 | ||
217 | /** | |
218 | * attaches FIRST and SECOND node arguments as the LEFT and RIGHT children of | |
219 | * this node (removing any old references) a null parameter DOES NOT mean that | |
220 | * the pointer to the corresponding child node is set to NULL - you should use | |
221 | * setChild(null), or detach() for this. | |
222 | * | |
223 | */ | |
224 | 0 | public void SetChildren(BinaryNode<T> leftchild, BinaryNode<T> rightchild) |
225 | { | |
226 | 0 | if (leftchild != null) |
227 | { | |
228 | 0 | this.setLeft(leftchild); |
229 | 0 | leftchild.detach(); |
230 | 0 | leftchild.setParent(this); |
231 | } | |
232 | ||
233 | 0 | if (rightchild != null) |
234 | { | |
235 | 0 | this.setRight(rightchild); |
236 | 0 | rightchild.detach(); |
237 | 0 | rightchild.setParent(this); |
238 | } | |
239 | } | |
240 | ||
241 | /** | |
242 | * Detaches the node from the binary tree, along with all its child nodes. | |
243 | * | |
244 | * @return BinaryNode The detached node. | |
245 | */ | |
246 | 20 | public BinaryNode<T> detach() |
247 | { | |
248 | 20 | if (this.parent != null) |
249 | { | |
250 | 20 | if (this.parent.left == this) |
251 | { | |
252 | 0 | this.parent.left = null; |
253 | } | |
254 | else | |
255 | { | |
256 | 20 | if (this.parent.right == this) |
257 | { | |
258 | 20 | this.parent.right = null; |
259 | } | |
260 | } | |
261 | } | |
262 | ||
263 | 20 | this.parent = null; |
264 | ||
265 | 20 | return this; |
266 | } | |
267 | ||
268 | /** | |
269 | * Traverses up through the tree until a node with a free leftchild is | |
270 | * discovered. | |
271 | * | |
272 | * @return BinaryNode | |
273 | */ | |
274 | 0 | public BinaryNode<T> ascendLeft() |
275 | { | |
276 | 0 | BinaryNode<T> c = this; |
277 | ||
278 | 0 | do |
279 | { | |
280 | 0 | c = c.parent(); |
281 | 0 | } while ((c != null) && (c.left() != null) && !c.left().isLeaf()); |
282 | ||
283 | 0 | return c; |
284 | } | |
285 | ||
286 | /** | |
287 | * Traverses up through the tree until a node with a free rightchild is | |
288 | * discovered. Jalview builds trees by descent on the left, so this may be | |
289 | * unused. | |
290 | * | |
291 | * @return BinaryNode | |
292 | */ | |
293 | 0 | public BinaryNode<T> ascendRight() |
294 | { | |
295 | 0 | BinaryNode<T> c = this; |
296 | ||
297 | 0 | do |
298 | { | |
299 | 0 | c = c.parent(); |
300 | 0 | } while ((c != null) && (c.right() != null) && !c.right().isLeaf()); |
301 | ||
302 | 0 | return c; |
303 | } | |
304 | ||
305 | /** | |
306 | * | |
307 | * set the display name | |
308 | * | |
309 | * @param new | |
310 | * name | |
311 | */ | |
312 | 263 | public void setName(String name) |
313 | { | |
314 | 263 | this.name = name; |
315 | } | |
316 | ||
317 | /** | |
318 | * | |
319 | * | |
320 | * @return the display name for this node | |
321 | */ | |
322 | 802 | public String getName() |
323 | { | |
324 | 802 | return this.name; |
325 | } | |
326 | ||
327 | /** | |
328 | * set integer bootstrap value | |
329 | * | |
330 | * @param boot | |
331 | */ | |
332 | 188 | public void setBootstrap(int boot) |
333 | { | |
334 | 188 | this.bootstrap = boot; |
335 | } | |
336 | ||
337 | /** | |
338 | * get bootstrap | |
339 | * | |
340 | * @return integer value | |
341 | */ | |
342 | 234 | public int getBootstrap() |
343 | { | |
344 | 234 | return bootstrap; |
345 | } | |
346 | ||
347 | /** | |
348 | * @param dummy | |
349 | * true if node is created for the representation of polytomous trees | |
350 | */ | |
351 | 134 | public boolean isDummy() |
352 | { | |
353 | 134 | return dummy; |
354 | } | |
355 | ||
356 | /** | |
357 | * DOCUMENT ME! | |
358 | * | |
359 | * @param newstate | |
360 | * DOCUMENT ME! | |
361 | * | |
362 | * @return DOCUMENT ME! | |
363 | */ | |
364 | 0 | public boolean setDummy(boolean newstate) |
365 | { | |
366 | 0 | boolean oldstate = dummy; |
367 | 0 | dummy = newstate; |
368 | ||
369 | 0 | return oldstate; |
370 | } | |
371 | ||
372 | /** | |
373 | * check if there's a label to show | |
374 | * | |
375 | * @return true if non-empty/null string | |
376 | */ | |
377 | 192 | public boolean hasLabel() |
378 | { | |
379 | 192 | return label != null && !label.isEmpty(); |
380 | } | |
381 | ||
382 | 0 | public String getLabel() |
383 | { | |
384 | 0 | return label; |
385 | } | |
386 | ||
387 | 0 | public void setLabel(String label) |
388 | { | |
389 | 0 | this.label = label; |
390 | } | |
391 | ||
392 | 0 | public boolean hasAlignmentAnnotation() |
393 | { | |
394 | 0 | return alignmentAnnotation != null; |
395 | } | |
396 | ||
397 | 0 | public AlignmentAnnotation getAlignmentAnnotation() |
398 | { | |
399 | 0 | return alignmentAnnotation; |
400 | } | |
401 | ||
402 | 0 | public void setAlignmentAnnotation(AlignmentAnnotation alignmentAnnotation) |
403 | { | |
404 | 0 | this.alignmentAnnotation = alignmentAnnotation; |
405 | } | |
406 | ||
407 | ||
408 | /** | |
409 | * ascends the tree but doesn't stop until a non-dummy node is discovered. | |
410 | * | |
411 | */ | |
412 | 188 | public BinaryNode<T> AscendTree() |
413 | { | |
414 | 188 | BinaryNode<T> c = this; |
415 | ||
416 | 188 | do |
417 | { | |
418 | 188 | c = c.parent(); |
419 | 188 | } while ((c != null) && c.dummy); |
420 | ||
421 | 188 | return c; |
422 | } | |
423 | ||
424 | 192 | public String getDisplayName() |
425 | { | |
426 | 192 | if (name != null && !name.isBlank()) |
427 | { | |
428 | ||
429 | 192 | if (hasLabel()) |
430 | { | |
431 | 0 | return getName() + "|" + label; |
432 | } | |
433 | 192 | return name; |
434 | } | |
435 | 0 | return hasLabel() ? label : ""; |
436 | } | |
437 | } |