Clover icon

Coverage Report

  1. Project Clover database Mon Nov 18 2024 09:38:20 GMT
  2. Package jalview.datamodel

File BinaryNode.java

 

Coverage histogram

../../img/srcFileCovDistChart6.png
27% of files have more coverage

Code metrics

22
64
28
1
419
192
45
0.7
2.29
28
1.61

Classes

Class Line # Actions
BinaryNode 31 64 45
0.526315852.6%
 

Contributing tests

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