2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
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
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.
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.
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
25 // Contact: phylosoft @ gmail . com
26 // WWW: www.phylosoft.org/forester
28 package org.forester.phylogeny;
30 import java.util.ArrayList;
31 import java.util.List;
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;
42 public class PhylogenyNode implements PhylogenyNodeI, Comparable<PhylogenyNode> {
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;
49 private int _sum_ext_nodes;
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;
63 * Default constructor for PhylogenyNode.
65 public PhylogenyNode() {
67 setId( PhylogenyNode.getNodeCount() );
68 PhylogenyNode.increaseNodeCount();
69 setSumExtNodes( 1 ); // For ext node, this number is 1 (not 0!!)
73 * Adds PhylogenyNode n to the list of child nodes and sets the _parent of n
77 * the PhylogenyNode to add
80 final public void addAsChild( final PhylogenyNodeI node ) {
81 final PhylogenyNode n = ( PhylogenyNode ) node;
87 * Adds PhylogenyNode n to the list of child nodes. But does NOT set the
88 * _parent of n to this.
90 * @see addAsChild( PhylogenyNode n )
92 * the PhylogenyNode to add
94 final private void addChildNode( final PhylogenyNode child ) {
95 getDescendants().add( child );
99 final public int compareTo( final PhylogenyNode o ) {
100 final PhylogenyNode n = o;
101 if ( ( getName() == null ) || ( n.getName() == null ) ) {
104 return getName().compareTo( n.getName() );
107 // ---------------------------------------------------------
108 // Copy and delete Nodes, copy subtress
109 // ---------------------------------------------------------
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.
117 final public PhylogenyNode copyNodeData() {
118 final PhylogenyNode node = new PhylogenyNode();
119 PhylogenyNode.decreaseNodeCount();
121 node._sum_ext_nodes = _sum_ext_nodes;
122 node._indicator = _indicator;
125 node._distance_parent = _distance_parent;
126 node._collapse = _collapse;
128 if ( _node_data != null ) {
129 node._node_data = ( NodeData ) _node_data.copy();
131 if ( _branch_data != null ) {
132 node._branch_data = ( BranchData ) _branch_data.copy();
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.
144 final public PhylogenyNode copyNodeDataShallow() {
145 final PhylogenyNode node = new PhylogenyNode();
146 PhylogenyNode.decreaseNodeCount();
148 node._sum_ext_nodes = _sum_ext_nodes;
149 node._indicator = _indicator;
152 node._distance_parent = _distance_parent;
153 node._collapse = _collapse;
155 node._node_data = _node_data;
156 node._branch_data = _branch_data;
162 * Based on node name, sequence, and taxonomy.
166 final public boolean equals( final Object o ) {
170 else if ( o == null ) {
173 else if ( o.getClass() != this.getClass() ) {
174 throw new IllegalArgumentException( "attempt to check [" + this.getClass() + "] equality to " + o + " ["
175 + o.getClass() + "]" );
178 final PhylogenyNode other = ( PhylogenyNode ) o;
179 if ( !getName().equals( other.getName() ) ) {
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() ) );
189 else if ( this_data.isHasSequence() && other_data.isHasSequence() ) {
190 return ( this_data.getSequence().isEqual( other_data.getSequence() ) );
192 else if ( this_data.isHasTaxonomy() && other_data.isHasTaxonomy() ) {
193 return ( this_data.getTaxonomy().isEqual( other_data.getTaxonomy() ) );
195 else if ( getName().length() > 0 ) {
196 // Node name is not empty, and equal.
205 // ---------------------------------------------------------
206 // Obtaining of Nodes
207 // ---------------------------------------------------------
209 * Returns a List containing references to all external children of this
212 * @return List of references to external Nodes
214 final public List<PhylogenyNode> getAllExternalDescendants() {
215 final List<PhylogenyNode> nodes = new ArrayList<PhylogenyNode>();
216 if ( isExternal() ) {
220 PhylogenyNode node1 = this;
221 while ( !node1.isExternal() ) {
222 node1 = node1.getFirstChildNode();
224 PhylogenyNode node2 = this;
225 while ( !node2.isExternal() ) {
226 node2 = node2.getLastChildNode();
228 while ( node1 != node2 ) {
230 node1 = node1.getNextExternalNode();
237 * Returns a List containing references to all names of the external
238 * children of this PhylogenyNode.
240 * @return List of references to names of external Nodes
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() );
251 final public BranchData getBranchData() {
252 if ( _branch_data == null ) {
253 _branch_data = new BranchData();
258 final BranchData getBranchDataDirectly() {
263 * This return child node n of this node.
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
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." );
276 if ( ( i >= getNumberOfDescendants() ) || ( i < 0 ) ) {
277 throw new IllegalArgumentException( "attempt to get child node " + i + " of a node with "
278 + getNumberOfDescendants() + " child nodes" );
280 return getDescendants().get( i );
284 * Convenience method. Returns the first child PhylogenyNode of this
287 final public PhylogenyNode getChildNode1() {
288 return getChildNode( 0 );
292 * Convenience method. Returns the second child PhylogenyNode of this
295 * [last modified May 18, 2005 by CMZ]
297 final public PhylogenyNode getChildNode2() {
298 return getChildNode( 1 );
302 * This gets the child node index of this node.
305 * @return the child node index of this node
306 * @throws UnsupportedOperationException
307 * if this node is a root node
309 final public int getChildNodeIndex() {
310 return getChildNodeIndex( getParent() );
314 * This gets the child node index of this node, given that parent is its
317 * [last modified Aug 14, 2006 by CMZ]
319 * @return the child node index of this node
320 * @throws UnsupportedOperationException
321 * if this node is a root node
323 final public int getChildNodeIndex( final PhylogenyNode parent ) {
325 throw new UnsupportedOperationException( "Cannot get the child index for a root node." );
327 for( int i = 0; i < parent.getNumberOfDescendants(); ++i ) {
328 if ( parent.getChildNode( i ) == this ) {
332 throw new RuntimeException( "Unexpected exception: Could not determine the child index for node: " + this );
335 final public List<PhylogenyNode> getDescendants() {
340 * Returns the length of the branch leading to the _parent of this
341 * PhylogenyNode (double).
344 final public double getDistanceToParent() {
345 return _distance_parent;
349 * Convenience method. Returns the first child node of this node.
351 * [last modified May 18, 2005 by CMZ]
353 * @return the first child node of this node
355 public final PhylogenyNode getFirstChildNode() {
356 return getChildNode( 0 );
360 * Returns the _indicator value of this PhylogenyNode.
362 public final byte getIndicator() {
367 * Convenience method. Returns the last child node of this node.
369 * [last modified May 18, 2005 by CMZ]
371 * @return the last child node of this node
373 public final PhylogenyNode getLastChildNode() {
374 return getChildNode( getNumberOfDescendants() - 1 );
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.
382 public final PhylogenyNode getLink() {
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.
391 public final PhylogenyNode getNextExternalNode() {
392 if ( isInternal() ) {
393 throw new UnsupportedOperationException( "attempt to get next external node of an internal node" );
395 else if ( isLastExternalNode() ) {
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();
407 current_node = current_node.getChildNode( index + 1 );
408 while ( current_node.isInternal() ) {
409 current_node = current_node.getFirstChildNode();
414 public final PhylogenyNode getNextExternalNodeWhileTakingIntoAccountCollapsedNodes() {
416 if ( isInternal() && !isCollapse() ) {
417 throw new UnsupportedOperationException( "attempt to get next external node of an uncollapsed internal node" );
422 if ( getParent().isCollapse() ) {
423 throw new UnsupportedOperationException( "attempt to get next external node of node with a collapsed parent" );
425 // This checks if last node.
426 PhylogenyNode n = this;
428 while ( !n.isRoot() ) {
429 if ( !n.isLastChildNode() ) {
438 int index = getChildNodeIndex();
439 PhylogenyNode previous_node = this;
440 PhylogenyNode current_node = getParent();
441 while ( !current_node.isRoot()
442 && ( current_node.isCollapse() || ( current_node.getNumberOfDescendants() == 1 ) || previous_node
443 .isLastChildNode() ) ) {
444 index = current_node.getChildNodeIndex();
445 previous_node = current_node;
446 current_node = current_node.getParent();
448 if ( index < current_node.getNumberOfDescendants() - 1 ) {
449 current_node = current_node.getChildNode( index + 1 );
451 while ( current_node.isInternal() && !current_node.isCollapse() ) {
452 current_node = current_node.getFirstChildNode();
457 public final NodeData getNodeData() {
458 if ( _node_data == null ) {
459 _node_data = new NodeData();
464 final NodeData getNodeDataDirectly() {
468 // ---------------------------------------------------------
469 // Set and get methods for Nodes
470 // ---------------------------------------------------------
472 * Returns the ID (int) of this PhylogenyNode.
475 final public int getId() {
480 * Returns the name of this node.
483 final public String getName() {
484 return getNodeData().getNodeName();
487 final public int getNumberOfDescendants() {
488 return _descendants.size();
492 * Returns the total number of external Nodes originating from this
493 * PhylogenyNode (int).
495 final public int getNumberOfExternalNodes() {
496 return _sum_ext_nodes;
499 final public int getNumberOfParents() {
504 * Returns a refernce to the parent PhylogenyNode of this PhylogenyNode.
506 final public PhylogenyNode getParent() {
511 * Returns a refernce to the next external PhylogenyNode of this
512 * PhylogenyNode. TODO should be in Phylogeny. Returns null if no next
513 * external node is available.
515 final public PhylogenyNode getPreviousExternalNode() {
516 if ( isInternal() ) {
517 throw new UnsupportedOperationException( "Cannot get the previous external node for an internal node." );
519 else if ( isRoot() /* TODO && tree is rooted */) {
520 throw new UnsupportedOperationException( "Cannot get the previous external node for a root node." );
522 else if ( isFirstExternalNode() ) {
523 throw new UnsupportedOperationException( "Attempt to get previous external node of the first external node." );
525 int index = getChildNodeIndex();
526 PhylogenyNode previous_node = this;
527 PhylogenyNode current_node = getParent();
528 while ( !current_node.isRoot()
529 && ( ( current_node.getNumberOfDescendants() == 1 ) || previous_node.isFirstChildNode() ) ) {
530 index = current_node.getChildNodeIndex();
531 previous_node = current_node;
532 current_node = current_node.getParent();
534 current_node = current_node.getChildNode( index - 1 );
535 while ( current_node.isInternal() ) {
536 current_node = current_node.getLastChildNode();
542 * Used for drawing of Trees.
544 final public float getXcoord() {
548 final public float getXSecondary() {
553 * Used for drawing of Trees.
555 final public float getYcoord() {
559 final public float getYSecondary() {
564 final public int hashCode() {
565 final NodeData data = getNodeData();
566 if ( ( getName().length() < 1 ) && !data.isHasSequence() && !data.isHasTaxonomy() ) {
567 return super.hashCode();
569 int result = getName().hashCode();
570 if ( data.isHasSequence() ) {
571 result ^= data.getSequence().hashCode();
573 if ( data.isHasTaxonomy() ) {
574 result ^= data.getTaxonomy().hashCode();
579 final private void init() {
580 _descendants = new ArrayList<PhylogenyNode>();
587 * Deletes data of this PhylogenyNode. Links to the other Nodes in the
588 * Phylogeny, the ID and the sum of external nodes are NOT deleted. Field
589 * "_link" (_link to Nodes in other Phylogeny) IS deleted.
591 * @see #getLink() (Last modified: 12/20/03)
593 final public void initializeData() {
598 _distance_parent = PhylogenyNode.DISTANCE_DEFAULT;
606 * Returns whether this PhylogenyNode should be drawn as collapsed.
608 final public boolean isCollapse() {
613 * Returns true if this PhylogenyNode represents a _duplication event, false
616 final public boolean isDuplication() {
617 return getNodeData().isHasEvent() && getNodeData().getEvent().isDuplication();
621 * Checks whether this PhylogenyNode is external (tip).
623 * @return true if this PhylogenyNode is external, false otherwise
625 final public boolean isExternal() {
626 return ( getNumberOfDescendants() < 1 );
632 * @return DOCUMENT ME!
634 final public boolean isFirstChildNode() {
635 if ( isRoot() /* and tree is rooted TODO */) {
636 throw new UnsupportedOperationException( "Cannot determine whether the root is the first child node of its _parent." );
638 return ( getChildNodeIndex() == 0 );
644 * @return DOCUMENT ME!
646 final public boolean isFirstExternalNode() {
647 if ( isInternal() ) {
650 PhylogenyNode node = this;
651 while ( !node.isRoot() ) {
652 if ( !node.isFirstChildNode() ) {
655 node = node.getParent();
661 * Returns whether a _duplication or speciation event has been assigned for
662 * this PhylogenyNode.
664 final public boolean isHasAssignedEvent() {
665 if ( !getNodeData().isHasEvent() ) {
668 if ( ( getNodeData().getEvent() ).isUnassigned() ) {
675 * Checks whether this PhylogenyNode is internal (tip).
677 * @return true if this PhylogenyNode is external, false otherwise
679 final public boolean isInternal() {
680 return ( !isExternal() );
684 * Returns true if this node is the last child node of its _parent.
686 * [last modified June 01, 2005 by CMZ]
688 * @return true if this node is the last child node of its _parent, false
691 final public boolean isLastChildNode() {
692 if ( isRoot() /* and tree is rooted TODO */) {
693 throw new UnsupportedOperationException( "Cannot determine whether the root is the last child node of its _parent." );
695 return ( getChildNodeIndex() == ( getParent().getNumberOfDescendants() - 1 ) );
701 * @return DOCUMENT ME!
703 final public boolean isLastExternalNode() {
704 if ( isInternal() ) {
707 PhylogenyNode node = this;
708 while ( !node.isRoot() ) {
709 if ( !node.isLastChildNode() ) {
712 node = node.getParent();
718 * Checks whether this PhylogenyNode is a root.
720 * @return true if this PhylogenyNode is the root, false otherwise
722 final public boolean isRoot() {
723 return _parent == null;
726 final public boolean isSpeciation() {
727 return getNodeData().isHasEvent() && getNodeData().getEvent().isSpeciation();
730 // ---------------------------------------------------------
732 // ---------------------------------------------------------
733 final public PhylogenyNodeIterator iterateChildNodesForward() {
734 return new ChildNodeIteratorForward( this );
737 // ---------------------------------------------------------
739 // ---------------------------------------------------------
741 * Prints to the console the subtree originating from this PhylogenyNode in
744 public void preorderPrint() {
745 System.out.println( this + "\n" );
746 if ( isInternal() ) {
747 for( int i = 0; i < getNumberOfDescendants(); ++i ) {
748 getChildNode( i ).preorderPrint();
753 final public void removeChildNode( final int i ) {
754 if ( isExternal() ) {
755 throw new UnsupportedOperationException( "cannot get the child node for a external node." );
757 if ( ( i >= getNumberOfDescendants() ) || ( i < 0 ) ) {
758 throw new IllegalArgumentException( "attempt to get child node " + i + " of a node with "
759 + getNumberOfDescendants() + " child nodes." );
761 getDescendants().remove( i );
764 final public void removeChildNode( final PhylogenyNode remove_me ) {
765 removeChildNode( remove_me.getChildNodeIndex() );
768 final public void setBranchData( final BranchData branch_data ) {
769 _branch_data = branch_data;
773 * Sets the first child PhylogenyNode of this PhylogenyNode to n.
775 final public void setChild1( final PhylogenyNode n ) {
776 setChildNode( 0, n );
780 * Sets the second child PhylogenyNode of this PhylogenyNode to n.
782 final public void setChild2( final PhylogenyNode n ) {
783 setChildNode( 1, n );
787 * Inserts PhylogenyNode n at the specified position i into the list of
788 * child nodes. This does not allow null slots in the list of child nodes:
789 * If i is larger than the number of child nodes, n is just added to the
790 * list, not place at index i.
793 * the index of position where to add the child
795 * the PhylogenyNode to add
797 final public void setChildNode( final int i, final PhylogenyNode node ) {
798 node.setParent( this );
799 if ( getNumberOfDescendants() <= i ) {
800 addChildNode( node );
803 getDescendants().set( i, node );
807 final void setChildNodeOnly( final int i, final PhylogenyNode node ) {
808 if ( getNumberOfDescendants() <= i ) {
809 addChildNode( node );
812 getDescendants().set( i, node );
817 * Sets whether this PhylogenyNode should be drawn as collapsed.
819 final public void setCollapse( final boolean b ) {
824 * Sets the length of the branch leading to the _parent of this
825 * PhylogenyNode to double d.
828 final public void setDistanceToParent( final double d ) {
829 _distance_parent = d;
833 * Sets the _indicator value of this PhylogenyNode to i.
835 final public void setIndicator( final byte i ) {
839 // --------------------------------------------------------------------
840 // Adjust methods (related to Phylogeny construction and
841 // Phylogeny modification)
842 // --------------------------------------------------------------------
844 * Sets the indicators of all the children of this PhylogenyNode to zero.
846 final void setIndicatorsToZero() {
847 for( final PreorderTreeIterator it = new PreorderTreeIterator( this ); it.hasNext(); ) {
848 it.next().setIndicator( ( byte ) 0 );
853 * Sets the linked PhylogenyNode of this PhylogenyNode to n. Currently, this
854 * method is only used for the speciation-_duplication assignment
857 final public void setLink( final PhylogenyNode n ) {
862 * Sets the name of this node.
865 final public void setName( final String node_name ) {
866 getNodeData().setNodeName( node_name );
870 * Sets the Id of this PhylogenyNode to i. In most cases, this number
871 * should not be set to values lower than getNodeCount() -- which this method
874 synchronized final protected void setId( final int i ) {
875 if ( i < getNodeCount() ) {
876 throw new IllegalArgumentException( "attempt to set node id to a value less than total node count (thus violating the uniqueness of node ids)" );
882 * Sets the _parent PhylogenyNode of this PhylogenyNode to n.
885 final public void setParent( final PhylogenyNode n ) {
890 * Sets the total number of external Nodes originating from this
891 * PhylogenyNode to i (int).
893 final public void setSumExtNodes( final int i ) {
895 throw new IllegalArgumentException( "attempt to set sum of external nodes to less than one" );
901 * Used for drawing of Trees.
903 final public void setXcoord( final float x ) {
907 final public void setXSecondary( final float x_secondary ) {
908 _x_secondary = x_secondary;
913 * Used for drawing of Trees.
915 final public void setYcoord( final float y ) {
919 final public void setYSecondary( final float y_secondary ) {
920 _y_secondary = y_secondary;
923 // ---------------------------------------------------------
924 // Writing of Nodes to Strings
925 // ---------------------------------------------------------
926 final public String toNewHampshire( final boolean simple_nh, final boolean write_distance_to_parent ) {
927 final StringBuilder sb = new StringBuilder();
929 if ( !ForesterUtil.isEmpty( getName() ) ) {
932 else if ( getNodeData().isHasTaxonomy() ) {
933 if ( !ForesterUtil.isEmpty( getNodeData().getTaxonomy().getTaxonomyCode() ) ) {
934 data = getNodeData().getTaxonomy().getTaxonomyCode();
936 else if ( !ForesterUtil.isEmpty( getNodeData().getTaxonomy().getScientificName() ) ) {
937 data = getNodeData().getTaxonomy().getScientificName();
939 else if ( !ForesterUtil.isEmpty( getNodeData().getTaxonomy().getCommonName() ) ) {
940 data = getNodeData().getTaxonomy().getCommonName();
942 else if ( getNodeData().getTaxonomy().getTaxonomyCode() != null ) {
943 data = getNodeData().getTaxonomy().getTaxonomyCode();
946 else if ( getNodeData().isHasSequence() ) {
947 if ( !ForesterUtil.isEmpty( getNodeData().getSequence().getName() ) ) {
948 data = getNodeData().getSequence().getName();
951 if ( data.length() > 0 ) {
952 data = ForesterUtil.replaceIllegalNhCharacters( data );
953 if ( simple_nh && ( data.length() > 10 ) ) {
954 data = data.substring( 0, 11 );
956 if ( ForesterUtil.isContainsParanthesesableNhCharacter( data ) ) {
965 if ( ( getDistanceToParent() != PhylogenyNode.DISTANCE_DEFAULT ) && write_distance_to_parent ) {
967 sb.append( getDistanceToParent() );
969 return sb.toString();
973 * Converts this PhylogenyNode to a New Hampshire X (NHX) String
976 final public String toNewHampshireX() {
977 final StringBuffer sb = new StringBuffer();
978 final StringBuffer s_nhx = new StringBuffer();
979 if ( !ForesterUtil.isEmpty( getName() ) ) {
980 final String name = ForesterUtil.replaceIllegalNhCharacters( getName() );
981 if ( ForesterUtil.isContainsParanthesesableNhCharacter( name ) ) {
990 if ( getDistanceToParent() != PhylogenyNode.DISTANCE_DEFAULT ) {
992 sb.append( getDistanceToParent() );
994 if ( getNodeDataDirectly() != null ) {
995 s_nhx.append( getNodeDataDirectly().toNHX() );
997 if ( getBranchDataDirectly() != null ) {
998 s_nhx.append( getBranchDataDirectly().toNHX() );
1000 if ( s_nhx.length() > 0 ) {
1001 sb.append( "[&&NHX" );
1005 return sb.toString();
1009 final public String toString() {
1010 final StringBuilder sb = new StringBuilder();
1011 if ( !ForesterUtil.isEmpty( getName() ) ) {
1012 sb.append( getName() );
1016 sb.append( getId() );
1018 return sb.toString();
1022 * Decreases the total number of all Nodes created so far by one.
1024 final static synchronized void decreaseNodeCount() {
1025 --PhylogenyNode._node_count;
1029 * Returns the total number of all Nodes created so far.
1031 * @return total number of Nodes (int)
1033 synchronized final public static int getNodeCount() {
1034 return PhylogenyNode._node_count;
1038 * Increases the total number of all Nodes created so far by one.
1040 synchronized final private static void increaseNodeCount() {
1041 ++PhylogenyNode._node_count;
1045 * Sets the total number of all Nodes created so far to i (int).
1047 synchronized final static void setNodeCount( final int i ) {
1048 PhylogenyNode._node_count = i;
1051 public static PhylogenyNode createInstanceFromNhxString( final String nhx ) throws NHXFormatException {
1052 return new PhylogenyNode( nhx, ForesterUtil.TAXONOMY_EXTRACTION.NO, false );
1055 public static PhylogenyNode createInstanceFromNhxString( final String nhx,
1056 final ForesterUtil.TAXONOMY_EXTRACTION taxonomy_extraction )
1057 throws NHXFormatException {
1058 return new PhylogenyNode( nhx, taxonomy_extraction, false );
1061 public static PhylogenyNode createInstanceFromNhxString( final String nhx,
1062 final ForesterUtil.TAXONOMY_EXTRACTION taxonomy_extraction,
1063 final boolean replace_underscores )
1064 throws NHXFormatException {
1065 return new PhylogenyNode( nhx, taxonomy_extraction, replace_underscores );
1068 private PhylogenyNode( final String nhx,
1069 final ForesterUtil.TAXONOMY_EXTRACTION taxonomy_extraction,
1070 final boolean replace_underscores ) throws NHXFormatException {
1072 NHXParser.parseNHX( nhx, this, taxonomy_extraction, replace_underscores );
1073 setId( PhylogenyNode.getNodeCount() );
1074 PhylogenyNode.increaseNodeCount();
1075 setSumExtNodes( 1 ); // For ext node, this number is 1 (not 0!!)