Clover icon

Coverage Report

  1. Project Clover database Thu Dec 4 2025 14:43:25 GMT
  2. Package jalview.datamodel

File BinaryNode.java

 

Coverage histogram

../../img/srcFileCovDistChart7.png
30% of files have more coverage

Code metrics

26
72
34
1
461
222
53
0.74
2.12
34
1.56

Classes

Class Line # Actions
BinaryNode 31 72 53
0.696969769.7%
 

Contributing tests

This file is covered by 16 tests. .

Source view

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    // Stores additional details of annotation
40    // like pdb id, chain id
41    String annotationDetails = null;
42   
43    AlignmentAnnotation alignmentAnnotation = null;
44   
45    BinaryNode<T> left;
46   
47    BinaryNode<T> right;
48   
49    BinaryNode<T> parent;
50   
51    /** Bootstrap value */
52    public int bootstrap;
53   
54    /** DOCUMENT ME!! */
55    public double dist;
56   
57    /** DOCUMENT ME!! */
58    public int count;
59   
60    /** DOCUMENT ME!! */
61    public double height;
62   
63    /** DOCUMENT ME!! */
64    public float ycount;
65   
66    /** DOCUMENT ME!! */
67    public Color color = Color.black;
68   
69    /**
70    * if true, node is created to simulate polytomy between parent and its 3 or
71    * more children
72    */
73    public boolean dummy = false;
74   
75    /**
76    * Creates a new BinaryNode object.
77    */
 
78  666 toggle public BinaryNode()
79    {
80  666 left = right = parent = null;
81  666 bootstrap = 0;
82  666 dist = 0;
83    }
84   
85    /**
86    * Creates a new BinaryNode object.
87    *
88    * @param element
89    * DOCUMENT ME!
90    * @param parent
91    * DOCUMENT ME!
92    * @param name
93    * DOCUMENT ME!
94    */
 
95  463 toggle public BinaryNode(T element, BinaryNode<T> parent, String name,
96    double dist)
97    {
98  463 this();
99  463 this.element = element;
100  463 this.parent = parent;
101  463 setName(name);
102  463 this.dist = dist;
103    }
104   
 
105  463 toggle public BinaryNode(T element, BinaryNode<T> parent, String name,
106    double dist, int bootstrap)
107    {
108  463 this(element, parent, name, dist);
109  463 this.bootstrap = bootstrap;
110    }
111   
 
112  463 toggle public BinaryNode(T val, BinaryNode<T> parent, String name, double dist,
113    int bootstrap, boolean dummy)
114    {
115  463 this(val, parent, name, dist, bootstrap);
116  463 this.dummy = dummy;
117    }
118   
119    /**
120    * DOCUMENT ME!
121    *
122    * @return DOCUMENT ME!
123    */
 
124  2685 toggle public T element()
125    {
126  2685 return element;
127    }
128   
129    /**
130    * DOCUMENT ME!
131    *
132    * @param v
133    * DOCUMENT ME!
134    *
135    * @return DOCUMENT ME!
136    */
 
137  371 toggle public T setElement(T v)
138    {
139  371 return element = v;
140    }
141   
142    /**
143    * DOCUMENT ME!
144    *
145    * @return DOCUMENT ME!
146    */
 
147  23030 toggle public BinaryNode<T> left()
148    {
149  23030 return left;
150    }
151   
152    /**
153    * DOCUMENT ME!
154    *
155    * @param n
156    * DOCUMENT ME!
157    *
158    * @return DOCUMENT ME!
159    */
 
160  310 toggle public BinaryNode<T> setLeft(BinaryNode<T> n)
161    {
162  310 return left = n;
163    }
164   
165    /**
166    * DOCUMENT ME!
167    *
168    * @return DOCUMENT ME!
169    */
 
170  16617 toggle public BinaryNode<T> right()
171    {
172  16617 return right;
173    }
174   
175    /**
176    * DOCUMENT ME!
177    *
178    * @param n
179    * DOCUMENT ME!
180    *
181    * @return DOCUMENT ME!
182    */
 
183  331 toggle public BinaryNode<T> setRight(BinaryNode<T> n)
184    {
185  331 return right = n;
186    }
187   
188    /**
189    * DOCUMENT ME!
190    *
191    * @return DOCUMENT ME!
192    */
 
193  2654 toggle public BinaryNode<T> parent()
194    {
195  2654 return parent;
196    }
197   
198    /**
199    * DOCUMENT ME!
200    *
201    * @param n
202    * DOCUMENT ME!
203    *
204    * @return DOCUMENT ME!
205    */
 
206  178 toggle public BinaryNode<T> setParent(BinaryNode<T> n)
207    {
208  178 return parent = n;
209    }
210   
211    /**
212    * DOCUMENT ME!
213    *
214    * @return DOCUMENT ME!
215    */
 
216  300 toggle public boolean isLeaf()
217    {
218  300 return (left == null) && (right == null);
219    }
220   
221    /**
222    * attaches FIRST and SECOND node arguments as the LEFT and RIGHT children of
223    * this node (removing any old references) a null parameter DOES NOT mean that
224    * the pointer to the corresponding child node is set to NULL - you should use
225    * setChild(null), or detach() for this.
226    *
227    */
 
228  0 toggle public void SetChildren(BinaryNode<T> leftchild, BinaryNode<T> rightchild)
229    {
230  0 if (leftchild != null)
231    {
232  0 this.setLeft(leftchild);
233  0 leftchild.detach();
234  0 leftchild.setParent(this);
235    }
236   
237  0 if (rightchild != null)
238    {
239  0 this.setRight(rightchild);
240  0 rightchild.detach();
241  0 rightchild.setParent(this);
242    }
243    }
244   
245    /**
246    * Detaches the node from the binary tree, along with all its child nodes.
247    *
248    * @return BinaryNode The detached node.
249    */
 
250  21 toggle public BinaryNode<T> detach()
251    {
252  21 if (this.parent != null)
253    {
254  21 if (this.parent.left == this)
255    {
256  0 this.parent.left = null;
257    }
258    else
259    {
260  21 if (this.parent.right == this)
261    {
262  21 this.parent.right = null;
263    }
264    }
265    }
266   
267  21 this.parent = null;
268   
269  21 return this;
270    }
271   
272    /**
273    * Traverses up through the tree until a node with a free leftchild is
274    * discovered.
275    *
276    * @return BinaryNode
277    */
 
278  0 toggle public BinaryNode<T> ascendLeft()
279    {
280  0 BinaryNode<T> c = this;
281   
282  0 do
283    {
284  0 c = c.parent();
285  0 } while ((c != null) && (c.left() != null) && !c.left().isLeaf());
286   
287  0 return c;
288    }
289   
290    /**
291    * Traverses up through the tree until a node with a free rightchild is
292    * discovered. Jalview builds trees by descent on the left, so this may be
293    * unused.
294    *
295    * @return BinaryNode
296    */
 
297  0 toggle public BinaryNode<T> ascendRight()
298    {
299  0 BinaryNode<T> c = this;
300   
301  0 do
302    {
303  0 c = c.parent();
304  0 } while ((c != null) && (c.right() != null) && !c.right().isLeaf());
305   
306  0 return c;
307    }
308   
309    /**
310    *
311    * set the display name
312    *
313    * @param new
314    * name
315    */
 
316  813 toggle public void setName(String name)
317    {
318  813 this.name = name!=null ? name.trim() : null;
319    }
320   
321    /**
322    *
323    *
324    * @return the display name for this node
325    */
 
326  1696 toggle public String getName()
327    {
328  1696 return this.name;
329    }
330   
331    /**
332    * set integer bootstrap value
333    *
334    * @param boot
335    */
 
336  221 toggle public void setBootstrap(int boot)
337    {
338  221 this.bootstrap = boot;
339    }
340   
341    /**
342    * get bootstrap
343    *
344    * @return integer value
345    */
 
346  234 toggle public int getBootstrap()
347    {
348  234 return bootstrap;
349    }
350   
351    /**
352    * @param dummy
353    * true if node is created for the representation of polytomous trees
354    */
 
355  145 toggle public boolean isDummy()
356    {
357  145 return dummy;
358    }
359   
360    /**
361    * DOCUMENT ME!
362    *
363    * @param newstate
364    * DOCUMENT ME!
365    *
366    * @return DOCUMENT ME!
367    */
 
368  0 toggle public boolean setDummy(boolean newstate)
369    {
370  0 boolean oldstate = dummy;
371  0 dummy = newstate;
372   
373  0 return oldstate;
374    }
375   
376    /**
377    * check if there's a label to show
378    *
379    * @return true if non-empty/null string
380    */
 
381  12005 toggle public boolean hasLabel()
382    {
383  12005 return label != null && !label.isEmpty();
384    }
385   
 
386  3330 toggle public String getLabel()
387    {
388  3330 return label;
389    }
390   
 
391  1690 toggle public void setLabel(String label)
392    {
393  1690 this.label = label;
394    }
395   
 
396  801 toggle public boolean hasAnnotationDetails()
397    {
398  801 return annotationDetails != null && !annotationDetails.isEmpty();
399    }
400   
 
401  36 toggle public String getAnnotationDetails()
402    {
403  36 return annotationDetails;
404    }
405   
 
406  17 toggle public void setAnnotationDetails(String annotationDetails)
407    {
408  17 this.annotationDetails = annotationDetails;
409    }
410   
 
411  833 toggle public boolean hasAlignmentAnnotation()
412    {
413  833 return alignmentAnnotation != null;
414    }
415   
 
416  874 toggle public AlignmentAnnotation getAlignmentAnnotation()
417    {
418  874 return alignmentAnnotation;
419    }
420   
 
421  40 toggle public void setAlignmentAnnotation(AlignmentAnnotation alignmentAnnotation)
422    {
423  40 this.alignmentAnnotation = alignmentAnnotation;
424    }
425   
426   
427    /**
428    * ascends the tree but doesn't stop until a non-dummy node is discovered.
429    *
430    */
 
431  221 toggle public BinaryNode<T> AscendTree()
432    {
433  221 BinaryNode<T> c = this;
434   
435  221 do
436    {
437  221 c = c.parent();
438  221 } while ((c != null) && c.dummy);
439   
440  221 return c;
441    }
442   
 
443  1441 toggle public String getDisplayName()
444    {
445  1441 if (name != null && !name.isEmpty())
446    {
447   
448  1441 if (hasLabel())
449    {
450  801 if (hasAnnotationDetails())
451    {
452  174 return getName() + "|" + label + "|" + annotationDetails;
453    }
454   
455  627 return getName() + "|" + label;
456    }
457  640 return name;
458    }
459  0 return hasLabel() ? label : "";
460    }
461    }