in progress
[jalview.git] / forester / java / src / org / forester / phylogeny / PhylogenyNode.java
1 // $Id:
2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
4 //
5 // Copyright (C) 2008-2009 Christian M. Zmasek
6 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
7 // Copyright (C) 2000-2001 Washington University School of Medicine
8 // and Howard Hughes Medical Institute
9 // All rights reserved
10 //
11 // This library is free software; you can redistribute it and/or
12 // modify it under the terms of the GNU Lesser General Public
13 // License as published by the Free Software Foundation; either
14 // version 2.1 of the License, or (at your option) any later version.
15 //
16 // This library is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 // Lesser General Public License for more details.
20 //
21 // You should have received a copy of the GNU Lesser General Public
22 // License along with this library; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 //
25 // Contact: phylosoft @ gmail . com
26 // WWW: www.phylosoft.org/forester
27
28 package org.forester.phylogeny;
29
30 import java.util.ArrayList;
31 import java.util.List;
32
33 import org.forester.io.parsers.nhx.NHXFormatException;
34 import org.forester.io.parsers.nhx.NHXParser;
35 import org.forester.phylogeny.data.BranchData;
36 import org.forester.phylogeny.data.NodeData;
37 import org.forester.phylogeny.iterators.ChildNodeIteratorForward;
38 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
39 import org.forester.phylogeny.iterators.PreorderTreeIterator;
40 import org.forester.util.ForesterUtil;
41
42 public class PhylogenyNode implements PhylogenyNodeI, Comparable<PhylogenyNode> {
43
44     /** Value of -99.0 is used as default value. */
45     public final static double       DISTANCE_DEFAULT = -1024.0;
46     private static int               _node_count      = 0;
47     private byte                     _indicator;
48     private int                      _id;
49     private int                      _sum_ext_nodes;
50     private float                    _x;
51     private float                    _y;
52     private double                   _distance_parent;
53     private boolean                  _collapse;
54     private PhylogenyNode            _parent;
55     private PhylogenyNode            _link;
56     private ArrayList<PhylogenyNode> _descendants;
57     private NodeData                 _node_data;
58     private BranchData               _branch_data;
59     private float                    _x_secondary;
60     private float                    _y_secondary;
61
62     /**
63      * Default constructor for PhylogenyNode.
64      */
65     public PhylogenyNode() {
66         init();
67         setId( PhylogenyNode.getNodeCount() );
68         PhylogenyNode.increaseNodeCount();
69         setSumExtNodes( 1 ); // For ext node, this number is 1 (not 0!!)
70     }
71
72     /**
73      * Adds PhylogenyNode n to the list of child nodes and sets the _parent of n
74      * to this.
75      * 
76      * @param n
77      *            the PhylogenyNode to add
78      */
79     @Override
80     final public void addAsChild( final PhylogenyNodeI node ) {
81         final PhylogenyNode n = ( PhylogenyNode ) node;
82         addChildNode( n );
83         n.setParent( this );
84     }
85
86     /**
87      * Adds PhylogenyNode n to the list of child nodes. But does NOT set the
88      * _parent of n to this.
89      * 
90      * @see addAsChild( PhylogenyNode n )
91      * @param n
92      *            the PhylogenyNode to add
93      */
94     final private void addChildNode( final PhylogenyNode child ) {
95         getDescendants().add( child );
96     }
97
98     @Override
99     final public int compareTo( final PhylogenyNode o ) {
100         final PhylogenyNode n = o;
101         if ( ( getName() == null ) || ( n.getName() == null ) ) {
102             return 0;
103         }
104         return getName().compareTo( n.getName() );
105     }
106
107     // ---------------------------------------------------------
108     // Copy and delete Nodes, copy subtress
109     // ---------------------------------------------------------
110     /**
111      * Returns a new PhylogenyNode which has its data copied from this
112      * PhylogenyNode. Links to the other Nodes in the same Phylogeny are NOT
113      * copied (e.g. _link to _parent). Field "_link" IS copied.
114      * 
115      * @see #getLink() 
116      */
117     final public PhylogenyNode copyNodeData() {
118         final PhylogenyNode node = new PhylogenyNode();
119         PhylogenyNode.decreaseNodeCount();
120         node._id = _id;
121         node._sum_ext_nodes = _sum_ext_nodes;
122         node._indicator = _indicator;
123         node._x = _x;
124         node._y = _y;
125         node._distance_parent = _distance_parent;
126         node._collapse = _collapse;
127         node._link = _link;
128         if ( _node_data != null ) {
129             node._node_data = ( NodeData ) _node_data.copy();
130         }
131         if ( _branch_data != null ) {
132             node._branch_data = ( BranchData ) _branch_data.copy();
133         }
134         return node;
135     }
136
137     /**
138      * Returns a new PhylogenyNode which has the same data as this
139      * PhylogenyNode. Links to the other Nodes in the same Phylogeny are NOT
140      * copied (e.g. _link to _parent). Field "_link" IS copied.
141      * 
142      * @see #getLink() 
143      */
144     final public PhylogenyNode copyNodeDataShallow() {
145         final PhylogenyNode node = new PhylogenyNode();
146         PhylogenyNode.decreaseNodeCount();
147         node._id = _id;
148         node._sum_ext_nodes = _sum_ext_nodes;
149         node._indicator = _indicator;
150         node._x = _x;
151         node._y = _y;
152         node._distance_parent = _distance_parent;
153         node._collapse = _collapse;
154         node._link = _link;
155         node._node_data = _node_data;
156         node._branch_data = _branch_data;
157         return node;
158     }
159
160     @Override
161     /**
162      * Based on node name, sequence, and taxonomy.
163      * 
164      * 
165      */
166     final public boolean equals( final Object o ) {
167         if ( this == o ) {
168             return true;
169         }
170         else if ( o == null ) {
171             return false;
172         }
173         else if ( o.getClass() != this.getClass() ) {
174             throw new IllegalArgumentException( "attempt to check [" + this.getClass() + "] equality to " + o + " ["
175                     + o.getClass() + "]" );
176         }
177         else {
178             final PhylogenyNode other = ( PhylogenyNode ) o;
179             if ( !getName().equals( other.getName() ) ) {
180                 return false;
181             }
182             final NodeData this_data = getNodeData();
183             final NodeData other_data = other.getNodeData();
184             if ( ( this_data.isHasSequence() && other_data.isHasSequence() )
185                     && ( this_data.isHasTaxonomy() && other_data.isHasTaxonomy() ) ) {
186                 return ( this_data.getTaxonomy().isEqual( other_data.getTaxonomy() ) && this_data.getSequence()
187                         .isEqual( other_data.getSequence() ) );
188             }
189             else if ( this_data.isHasSequence() && other_data.isHasSequence() ) {
190                 return ( this_data.getSequence().isEqual( other_data.getSequence() ) );
191             }
192             else if ( this_data.isHasTaxonomy() && other_data.isHasTaxonomy() ) {
193                 return ( this_data.getTaxonomy().isEqual( other_data.getTaxonomy() ) );
194             }
195             else if ( getName().length() > 0 ) {
196                 // Node name is not empty, and equal.
197                 return true;
198             }
199             else {
200                 return false;
201             }
202         }
203     }
204
205     // ---------------------------------------------------------
206     // Obtaining of Nodes
207     // ---------------------------------------------------------
208     /**
209      * Returns a List containing references to all external children of this
210      * PhylogenyNode.
211      * 
212      * @return List of references to external Nodes
213      */
214     final public List<PhylogenyNode> getAllExternalDescendants() {
215         final List<PhylogenyNode> nodes = new ArrayList<PhylogenyNode>();
216         if ( isExternal() ) {
217             nodes.add( this );
218             return nodes;
219         }
220         PhylogenyNode node1 = this;
221         while ( !node1.isExternal() ) {
222             node1 = node1.getFirstChildNode();
223         }
224         PhylogenyNode node2 = this;
225         while ( !node2.isExternal() ) {
226             node2 = node2.getLastChildNode();
227         }
228         while ( node1 != node2 ) {
229             nodes.add( node1 );
230             node1 = node1.getNextExternalNode();
231         }
232         nodes.add( node2 );
233         return nodes;
234     }
235
236     /**
237      * Returns a List containing references to all names of the external
238      * children of this PhylogenyNode.
239      * 
240      * @return List of references to names of external Nodes
241      */
242     final public List<String> getAllExternalDescendantsNames() {
243         final List<PhylogenyNode> c = getAllExternalDescendants();
244         final List<String> n = new ArrayList<String>( c.size() );
245         for( final PhylogenyNode phylogenyNode : c ) {
246             n.add( phylogenyNode.getName() );
247         }
248         return n;
249     }
250
251     final public BranchData getBranchData() {
252         if ( _branch_data == null ) {
253             _branch_data = new BranchData();
254         }
255         return _branch_data;
256     }
257
258     final BranchData getBranchDataDirectly() {
259         return _branch_data;
260     }
261
262     /**
263      * This return child node n of this node.
264      * 
265      * @param n
266      *            the index of the child to get
267      * @return the child node with index n
268      * @throws IllegalArgumentException
269      *             if n is out of bounds
270      */
271     @Override
272     final public PhylogenyNode getChildNode( final int i ) {
273         if ( isExternal() ) {
274             throw new UnsupportedOperationException( "attempt to get the child node of an external node." );
275         }
276         if ( ( i >= getNumberOfDescendants() ) || ( i < 0 ) ) {
277             throw new IllegalArgumentException( "attempt to get child node " + i + " of a node with "
278                     + getNumberOfDescendants() + " child nodes" );
279         }
280         return getDescendants().get( i );
281     }
282
283     /**
284      * Convenience method. Returns the first child PhylogenyNode of this
285      * PhylogenyNode.
286      */
287     final public PhylogenyNode getChildNode1() {
288         return getChildNode( 0 );
289     }
290
291     /**
292      * Convenience method. Returns the second child PhylogenyNode of this
293      * PhylogenyNode.
294      * <p>
295      * [last modified May 18, 2005 by CMZ]
296      */
297     final public PhylogenyNode getChildNode2() {
298         return getChildNode( 1 );
299     }
300
301     /**
302      * This gets the child node index of this node.
303      * <p>
304      * 
305      * @return the child node index of this node
306      * @throws UnsupportedOperationException
307      *             if this node is a root node
308      */
309     final public int getChildNodeIndex() {
310         return getChildNodeIndex( getParent() );
311     }
312
313     /**
314      * This gets the child node index of this node, given that parent is its
315      * parent
316      * <p>
317      * [last modified Aug 14, 2006 by CMZ]
318      * 
319      * @return the child node index of this node
320      * @throws UnsupportedOperationException
321      *             if this node is a root node
322      */
323     final public int getChildNodeIndex( final PhylogenyNode parent ) {
324         if ( isRoot() ) {
325             throw new UnsupportedOperationException( "Cannot get the child index for a root node." );
326         }
327         for( int i = 0; i < parent.getNumberOfDescendants(); ++i ) {
328             if ( parent.getChildNode( i ) == this ) {
329                 return i;
330             }
331         }
332         throw new RuntimeException( "Unexpected exception: Could not determine the child index for node: " + this );
333     }
334
335     final public List<PhylogenyNode> getDescendants() {
336         return _descendants;
337     }
338
339     /**
340      * Returns the length of the branch leading to the _parent of this
341      * PhylogenyNode (double).
342      */
343     @Override
344     final public double getDistanceToParent() {
345         return _distance_parent;
346     }
347
348     /**
349      * Convenience method. Returns the first child node of this node.
350      * <p>
351      * [last modified May 18, 2005 by CMZ]
352      * 
353      * @return the first child node of this node
354      */
355     public final PhylogenyNode getFirstChildNode() {
356         return getChildNode( 0 );
357     }
358
359     /**
360      * Returns the _indicator value of this PhylogenyNode.
361      */
362     public final byte getIndicator() {
363         return _indicator;
364     }
365
366     /**
367      * Convenience method. Returns the last child node of this node.
368      * <p>
369      * [last modified May 18, 2005 by CMZ]
370      * 
371      * @return the last child node of this node
372      */
373     public final PhylogenyNode getLastChildNode() {
374         return getChildNode( getNumberOfDescendants() - 1 );
375     }
376
377     /**
378      * Returns a refernce to the linked PhylogenyNode of this PhylogenyNode.
379      * Currently, this method is only used for the speciation-_duplication
380      * assignment algorithms.
381      */
382     public final PhylogenyNode getLink() {
383         return _link;
384     }
385
386     /**
387      * Returns a refernce to the next external PhylogenyNode of this
388      * PhylogenyNode. TODO should be in Phylogeny. Returns null if no next
389      * external node is available.
390      */
391     public final PhylogenyNode getNextExternalNode() {
392         if ( isInternal() ) {
393             throw new UnsupportedOperationException( "attempt to get next external node of an internal node" );
394         }
395         else if ( isLastExternalNode() ) {
396             return null;
397         }
398         int index = getChildNodeIndex();
399         PhylogenyNode previous_node = this;
400         PhylogenyNode current_node = getParent();
401         while ( !current_node.isRoot()
402                 && ( ( current_node.getNumberOfDescendants() == 1 ) || previous_node.isLastChildNode() ) ) {
403             index = current_node.getChildNodeIndex();
404             previous_node = current_node;
405             current_node = current_node.getParent();
406         }
407         current_node = current_node.getChildNode( index + 1 );
408         while ( current_node.isInternal() ) {
409             current_node = current_node.getFirstChildNode();
410         }
411         return current_node;
412     }
413
414     public final NodeData getNodeData() {
415         if ( _node_data == null ) {
416             _node_data = new NodeData();
417         }
418         return _node_data;
419     }
420
421     final NodeData getNodeDataDirectly() {
422         return _node_data;
423     }
424
425     // ---------------------------------------------------------
426     // Set and get methods for Nodes
427     // ---------------------------------------------------------
428     /**
429      * Returns the ID (int) of this PhylogenyNode.
430      */
431     @Override
432     final public int getId() {
433         return _id;
434     }
435
436     /**
437      * Returns the name of this node.
438      */
439     @Override
440     final public String getName() {
441         return getNodeData().getNodeName();
442     }
443
444     final public int getNumberOfDescendants() {
445         return _descendants.size();
446     }
447
448     /**
449      * Returns the total number of external Nodes originating from this
450      * PhylogenyNode (int).
451      */
452     final public int getNumberOfExternalNodes() {
453         return _sum_ext_nodes;
454     }
455
456     final public int getNumberOfParents() {
457         return 1;
458     }
459
460     /**
461      * Returns a refernce to the parent PhylogenyNode of this PhylogenyNode.
462      */
463     final public PhylogenyNode getParent() {
464         return _parent;
465     }
466
467     /**
468      * Returns a refernce to the next external PhylogenyNode of this
469      * PhylogenyNode. TODO should be in Phylogeny. Returns null if no next
470      * external node is available.
471      */
472     final public PhylogenyNode getPreviousExternalNode() {
473         if ( isInternal() ) {
474             throw new UnsupportedOperationException( "Cannot get the previous external node for an internal node." );
475         }
476         else if ( isRoot() /* TODO && tree is rooted */) {
477             throw new UnsupportedOperationException( "Cannot get the previous external node for a root node." );
478         }
479         else if ( isFirstExternalNode() ) {
480             throw new UnsupportedOperationException( "Attempt to get previous external node of the first external node." );
481         }
482         int index = getChildNodeIndex();
483         PhylogenyNode previous_node = this;
484         PhylogenyNode current_node = getParent();
485         while ( !current_node.isRoot()
486                 && ( ( current_node.getNumberOfDescendants() == 1 ) || previous_node.isFirstChildNode() ) ) {
487             index = current_node.getChildNodeIndex();
488             previous_node = current_node;
489             current_node = current_node.getParent();
490         }
491         current_node = current_node.getChildNode( index - 1 );
492         while ( current_node.isInternal() ) {
493             current_node = current_node.getLastChildNode();
494         }
495         return current_node;
496     }
497
498     /**
499      * Used for drawing of Trees.
500      */
501     final public float getXcoord() {
502         return _x;
503     }
504
505     final public float getXSecondary() {
506         return _x_secondary;
507     }
508
509     /**
510      * Used for drawing of Trees.
511      */
512     final public float getYcoord() {
513         return _y;
514     }
515
516     final public float getYSecondary() {
517         return _y_secondary;
518     }
519
520     @Override
521     final public int hashCode() {
522         final NodeData data = getNodeData();
523         if ( ( getName().length() < 1 ) && !data.isHasSequence() && !data.isHasTaxonomy() ) {
524             return super.hashCode();
525         }
526         int result = getName().hashCode();
527         if ( data.isHasSequence() ) {
528             result ^= data.getSequence().hashCode();
529         }
530         if ( data.isHasTaxonomy() ) {
531             result ^= data.getTaxonomy().hashCode();
532         }
533         return result;
534     }
535
536     final private void init() {
537         _descendants = new ArrayList<PhylogenyNode>();
538         _parent = null;
539         _id = 0;
540         initializeData();
541     }
542
543     /**
544      * Deletes data of this PhylogenyNode. Links to the other Nodes in the
545      * Phylogeny, the ID and the sum of external nodes are NOT deleted. Field
546      * "_link" (_link to Nodes in other Phylogeny) IS deleted.
547      * 
548      * @see #getLink() (Last modified: 12/20/03)
549      */
550     final public void initializeData() {
551         _indicator = 0;
552         _x = 0;
553         _y = 0;
554         //_node_name = "";
555         _distance_parent = PhylogenyNode.DISTANCE_DEFAULT;
556         _collapse = false;
557         _link = null;
558         _branch_data = null;
559         _node_data = null;
560     }
561
562     /**
563      * Returns whether this PhylogenyNode should be drawn as collapsed.
564      */
565     final public boolean isCollapse() {
566         return _collapse;
567     }
568
569     /**
570      * Returns true if this PhylogenyNode represents a _duplication event, false
571      * otherwise.
572      */
573     final public boolean isDuplication() {
574         return getNodeData().isHasEvent() && getNodeData().getEvent().isDuplication();
575     }
576
577     /**
578      * Checks whether this PhylogenyNode is external (tip).
579      * 
580      * @return true if this PhylogenyNode is external, false otherwise
581      */
582     final public boolean isExternal() {
583         return ( getNumberOfDescendants() < 1 );
584     }
585
586     /**
587      * DOCUMENT ME!
588      * 
589      * @return DOCUMENT ME!
590      */
591     final public boolean isFirstChildNode() {
592         if ( isRoot() /* and tree is rooted TODO */) {
593             throw new UnsupportedOperationException( "Cannot determine whether the root is the first child node of its _parent." );
594         }
595         return ( getChildNodeIndex() == 0 );
596     }
597
598     /**
599      * DOCUMENT ME!
600      * 
601      * @return DOCUMENT ME!
602      */
603     final public boolean isFirstExternalNode() {
604         if ( isInternal() ) {
605             return false;
606         }
607         PhylogenyNode node = this;
608         while ( !node.isRoot() ) {
609             if ( !node.isFirstChildNode() ) {
610                 return false;
611             }
612             node = node.getParent();
613         }
614         return true;
615     }
616
617     /**
618      * Returns whether a _duplication or speciation event has been assigned for
619      * this PhylogenyNode.
620      */
621     final public boolean isHasAssignedEvent() {
622         if ( !getNodeData().isHasEvent() ) {
623             return false;
624         }
625         if ( ( getNodeData().getEvent() ).isUnassigned() ) {
626             return false;
627         }
628         return true;
629     }
630
631     /**
632      * Checks whether this PhylogenyNode is internal (tip).
633      * 
634      * @return true if this PhylogenyNode is external, false otherwise
635      */
636     final public boolean isInternal() {
637         return ( !isExternal() );
638     }
639
640     /**
641      * Returns true if this node is the last child node of its _parent.
642      * <p>
643      * [last modified June 01, 2005 by CMZ]
644      * 
645      * @return true if this node is the last child node of its _parent, false
646      *         otherwise
647      */
648     final public boolean isLastChildNode() {
649         if ( isRoot() /* and tree is rooted TODO */) {
650             throw new UnsupportedOperationException( "Cannot determine whether the root is the last child node of its _parent." );
651         }
652         return ( getChildNodeIndex() == ( getParent().getNumberOfDescendants() - 1 ) );
653     }
654
655     /**
656      * DOCUMENT ME!
657      * 
658      * @return DOCUMENT ME!
659      */
660     final public boolean isLastExternalNode() {
661         if ( isInternal() ) {
662             return false;
663         }
664         PhylogenyNode node = this;
665         while ( !node.isRoot() ) {
666             if ( !node.isLastChildNode() ) {
667                 return false;
668             }
669             node = node.getParent();
670         }
671         return true;
672     }
673
674     /**
675      * Checks whether this PhylogenyNode is a root.
676      * 
677      * @return true if this PhylogenyNode is the root, false otherwise
678      */
679     final public boolean isRoot() {
680         return _parent == null;
681     }
682
683     final public boolean isSpeciation() {
684         return getNodeData().isHasEvent() && getNodeData().getEvent().isSpeciation();
685     }
686
687     // ---------------------------------------------------------
688     // Iterator
689     // ---------------------------------------------------------
690     final public PhylogenyNodeIterator iterateChildNodesForward() {
691         return new ChildNodeIteratorForward( this );
692     }
693
694     // ---------------------------------------------------------
695     // Basic printing
696     // ---------------------------------------------------------
697     /**
698      * Prints to the console the subtree originating from this PhylogenyNode in
699      * preorder.
700      */
701     public void preorderPrint() {
702         System.out.println( this + "\n" );
703         if ( isInternal() ) {
704             for( int i = 0; i < getNumberOfDescendants(); ++i ) {
705                 getChildNode( i ).preorderPrint();
706             }
707         }
708     }
709
710     final public void removeChildNode( final int i ) {
711         if ( isExternal() ) {
712             throw new UnsupportedOperationException( "cannot get the child node for a external node." );
713         }
714         if ( ( i >= getNumberOfDescendants() ) || ( i < 0 ) ) {
715             throw new IllegalArgumentException( "attempt to get child node " + i + " of a node with "
716                     + getNumberOfDescendants() + " child nodes." );
717         }
718         getDescendants().remove( i );
719     }
720
721     final public void removeChildNode( final PhylogenyNode remove_me ) {
722         removeChildNode( remove_me.getChildNodeIndex() );
723     }
724
725     final public void setBranchData( final BranchData branch_data ) {
726         _branch_data = branch_data;
727     }
728
729     /**
730      * Sets the first child PhylogenyNode of this PhylogenyNode to n.
731      */
732     final public void setChild1( final PhylogenyNode n ) {
733         setChildNode( 0, n );
734     }
735
736     /**
737      * Sets the second child PhylogenyNode of this PhylogenyNode to n.
738      */
739     final public void setChild2( final PhylogenyNode n ) {
740         setChildNode( 1, n );
741     }
742
743     /**
744      * Inserts PhylogenyNode n at the specified position i into the list of
745      * child nodes. This does not allow null slots in the list of child nodes:
746      * If i is larger than the number of child nodes, n is just added to the
747      * list, not place at index i.
748      * 
749      * @param i
750      *            the index of position where to add the child
751      * @param n
752      *            the PhylogenyNode to add
753      */
754     final public void setChildNode( final int i, final PhylogenyNode node ) {
755         node.setParent( this );
756         if ( getNumberOfDescendants() <= i ) {
757             addChildNode( node );
758         }
759         else {
760             getDescendants().set( i, node );
761         }
762     }
763
764     final void setChildNodeOnly( final int i, final PhylogenyNode node ) {
765         if ( getNumberOfDescendants() <= i ) {
766             addChildNode( node );
767         }
768         else {
769             getDescendants().set( i, node );
770         }
771     }
772
773     /**
774      * Sets whether this PhylogenyNode should be drawn as collapsed.
775      */
776     final public void setCollapse( final boolean b ) {
777         _collapse = b;
778     }
779
780     /**
781      * Sets the length of the branch leading to the _parent of this
782      * PhylogenyNode to double d.
783      */
784     @Override
785     final public void setDistanceToParent( final double d ) {
786         _distance_parent = d;
787     }
788
789     /**
790      * Sets the _indicator value of this PhylogenyNode to i.
791      */
792     final public void setIndicator( final byte i ) {
793         _indicator = i;
794     }
795
796     // --------------------------------------------------------------------
797     // Adjust methods (related to Phylogeny construction and
798     // Phylogeny modification)
799     // --------------------------------------------------------------------
800     /**
801      * Sets the indicators of all the children of this PhylogenyNode to zero.
802      */
803     final void setIndicatorsToZero() {
804         for( final PreorderTreeIterator it = new PreorderTreeIterator( this ); it.hasNext(); ) {
805             it.next().setIndicator( ( byte ) 0 );
806         }
807     }
808
809     /**
810      * Sets the linked PhylogenyNode of this PhylogenyNode to n. Currently, this
811      * method is only used for the speciation-_duplication assignment
812      * algorithms.
813      */
814     final public void setLink( final PhylogenyNode n ) {
815         _link = n;
816     }
817
818     /**
819      * Sets the name of this node.
820      */
821     @Override
822     final public void setName( final String node_name ) {
823         getNodeData().setNodeName( node_name );
824     }
825
826     /**
827      * Sets the Id of this PhylogenyNode to i. In most cases, this number
828      * should not be set to values lower than getNodeCount() -- which this method
829      * does not allow.
830      */
831     synchronized final protected void setId( final int i ) {
832         if ( i < getNodeCount() ) {
833             throw new IllegalArgumentException( "attempt to set node id to a value less than total node count (thus violating the uniqueness of node ids)" );
834         }
835         _id = i;
836     }
837
838     /**
839      * Sets the _parent PhylogenyNode of this PhylogenyNode to n.
840      */
841     @Override
842     final public void setParent( final PhylogenyNode n ) {
843         _parent = n;
844     }
845
846     /**
847      * Sets the total number of external Nodes originating from this
848      * PhylogenyNode to i (int).
849      */
850     final public void setSumExtNodes( final int i ) {
851         if ( i < 0 ) {
852             throw new IllegalArgumentException( "attempt to set sum of external nodes to less than one" );
853         }
854         _sum_ext_nodes = i;
855     }
856
857     /**
858      * Used for drawing of Trees.
859      */
860     final public void setXcoord( final float x ) {
861         _x = x;
862     }
863
864     final public void setXSecondary( final float x_secondary ) {
865         _x_secondary = x_secondary;
866     }
867
868     // -----------
869     /**
870      * Used for drawing of Trees.
871      */
872     final public void setYcoord( final float y ) {
873         _y = y;
874     }
875
876     final public void setYSecondary( final float y_secondary ) {
877         _y_secondary = y_secondary;
878     }
879
880     // ---------------------------------------------------------
881     // Writing of Nodes to Strings
882     // ---------------------------------------------------------
883     final public String toNewHampshire( final boolean simple_nh, final boolean write_distance_to_parent ) {
884         final StringBuilder sb = new StringBuilder();
885         String data = "";
886         if ( !ForesterUtil.isEmpty( getName() ) ) {
887             data = getName();
888         }
889         else if ( getNodeData().isHasTaxonomy() ) {
890             if ( !ForesterUtil.isEmpty( getNodeData().getTaxonomy().getTaxonomyCode() ) ) {
891                 data = getNodeData().getTaxonomy().getTaxonomyCode();
892             }
893             else if ( !ForesterUtil.isEmpty( getNodeData().getTaxonomy().getScientificName() ) ) {
894                 data = getNodeData().getTaxonomy().getScientificName();
895             }
896             else if ( !ForesterUtil.isEmpty( getNodeData().getTaxonomy().getCommonName() ) ) {
897                 data = getNodeData().getTaxonomy().getCommonName();
898             }
899             else if ( getNodeData().getTaxonomy().getTaxonomyCode() != null ) {
900                 data = getNodeData().getTaxonomy().getTaxonomyCode();
901             }
902         }
903         else if ( getNodeData().isHasSequence() ) {
904             if ( !ForesterUtil.isEmpty( getNodeData().getSequence().getName() ) ) {
905                 data = getNodeData().getSequence().getName();
906             }
907         }
908         if ( data.length() > 0 ) {
909             data = ForesterUtil.replaceIllegalNhCharacters( data );
910             if ( simple_nh && ( data.length() > 10 ) ) {
911                 data = data.substring( 0, 11 );
912             }
913             if ( ForesterUtil.isContainsParanthesesableNhCharacter( data ) ) {
914                 sb.append( '\'' );
915                 sb.append( data );
916                 sb.append( '\'' );
917             }
918             else {
919                 sb.append( data );
920             }
921         }
922         if ( ( getDistanceToParent() != PhylogenyNode.DISTANCE_DEFAULT ) && write_distance_to_parent ) {
923             sb.append( ":" );
924             sb.append( getDistanceToParent() );
925         }
926         return sb.toString();
927     }
928
929     /**
930      * Converts this PhylogenyNode to a New Hampshire X (NHX) String
931      * representation.
932      */
933     final public String toNewHampshireX() {
934         final StringBuffer sb = new StringBuffer();
935         final StringBuffer s_nhx = new StringBuffer();
936         if ( !ForesterUtil.isEmpty( getName() ) ) {
937             final String name = ForesterUtil.replaceIllegalNhCharacters( getName() );
938             if ( ForesterUtil.isContainsParanthesesableNhCharacter( name ) ) {
939                 sb.append( '\'' );
940                 sb.append( name );
941                 sb.append( '\'' );
942             }
943             else {
944                 sb.append( name );
945             }
946         }
947         if ( getDistanceToParent() != PhylogenyNode.DISTANCE_DEFAULT ) {
948             sb.append( ":" );
949             sb.append( getDistanceToParent() );
950         }
951         if ( getNodeDataDirectly() != null ) {
952             s_nhx.append( getNodeDataDirectly().toNHX() );
953         }
954         if ( getBranchDataDirectly() != null ) {
955             s_nhx.append( getBranchDataDirectly().toNHX() );
956         }
957         if ( s_nhx.length() > 0 ) {
958             sb.append( "[&&NHX" );
959             sb.append( s_nhx );
960             sb.append( "]" );
961         }
962         return sb.toString();
963     }
964
965     @Override
966     final public String toString() {
967         final StringBuilder sb = new StringBuilder();
968         if ( !ForesterUtil.isEmpty( getName() ) ) {
969             sb.append( getName() );
970             sb.append( " " );
971         }
972         sb.append( "[" );
973         sb.append( getId() );
974         sb.append( "]" );
975         return sb.toString();
976     }
977
978     /**
979      * Decreases the total number of all Nodes created so far by one.
980      */
981     final static synchronized void decreaseNodeCount() {
982         --PhylogenyNode._node_count;
983     }
984
985     /**
986      * Returns the total number of all Nodes created so far.
987      * 
988      * @return total number of Nodes (int)
989      */
990     synchronized final public static int getNodeCount() {
991         return PhylogenyNode._node_count;
992     }
993
994     /**
995      * Increases the total number of all Nodes created so far by one.
996      */
997     synchronized final private static void increaseNodeCount() {
998         ++PhylogenyNode._node_count;
999     }
1000
1001     /**
1002      * Sets the total number of all Nodes created so far to i (int).
1003      */
1004     synchronized final static void setNodeCount( final int i ) {
1005         PhylogenyNode._node_count = i;
1006     }
1007
1008     public static PhylogenyNode createInstanceFromNhxString( final String nhx ) throws NHXFormatException {
1009         return new PhylogenyNode( nhx, ForesterUtil.TAXONOMY_EXTRACTION.NO, false );
1010     }
1011
1012     public static PhylogenyNode createInstanceFromNhxString( final String nhx,
1013                                                              final ForesterUtil.TAXONOMY_EXTRACTION taxonomy_extraction )
1014             throws NHXFormatException {
1015         return new PhylogenyNode( nhx, taxonomy_extraction, false );
1016     }
1017
1018     public static PhylogenyNode createInstanceFromNhxString( final String nhx,
1019                                                              final ForesterUtil.TAXONOMY_EXTRACTION taxonomy_extraction,
1020                                                              final boolean replace_underscores )
1021             throws NHXFormatException {
1022         return new PhylogenyNode( nhx, taxonomy_extraction, replace_underscores );
1023     }
1024
1025     private PhylogenyNode( final String nhx,
1026                            final ForesterUtil.TAXONOMY_EXTRACTION taxonomy_extraction,
1027                            final boolean replace_underscores ) throws NHXFormatException {
1028         init();
1029         NHXParser.parseNHX( nhx, this, taxonomy_extraction, replace_underscores );
1030         setId( PhylogenyNode.getNodeCount() );
1031         PhylogenyNode.increaseNodeCount();
1032         setSumExtNodes( 1 ); // For ext node, this number is 1 (not 0!!)
1033     }
1034 }