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() ) {
417 throw new UnsupportedOperationException( "attempt to get next external node of an internal node" );
419 else if ( isLastExternalNode() ) {
422 int index = getChildNodeIndex();
423 PhylogenyNode previous_node = this;
424 PhylogenyNode current_node = getParent();
425 while ( !current_node.isRoot()
426 && ( ( current_node.getNumberOfDescendants() == 1 ) || previous_node.isLastChildNode() ) ) {
427 index = current_node.getChildNodeIndex();
428 previous_node = current_node;
429 current_node = current_node.getParent();
431 current_node = current_node.getChildNode( index + 1 );
432 while ( current_node.isInternal() ) {
433 current_node = current_node.getFirstChildNode();
438 public final NodeData getNodeData() {
439 if ( _node_data == null ) {
440 _node_data = new NodeData();
445 final NodeData getNodeDataDirectly() {
449 // ---------------------------------------------------------
450 // Set and get methods for Nodes
451 // ---------------------------------------------------------
453 * Returns the ID (int) of this PhylogenyNode.
456 final public int getId() {
461 * Returns the name of this node.
464 final public String getName() {
465 return getNodeData().getNodeName();
468 final public int getNumberOfDescendants() {
469 return _descendants.size();
473 * Returns the total number of external Nodes originating from this
474 * PhylogenyNode (int).
476 final public int getNumberOfExternalNodes() {
477 return _sum_ext_nodes;
480 final public int getNumberOfParents() {
485 * Returns a refernce to the parent PhylogenyNode of this PhylogenyNode.
487 final public PhylogenyNode getParent() {
492 * Returns a refernce to the next external PhylogenyNode of this
493 * PhylogenyNode. TODO should be in Phylogeny. Returns null if no next
494 * external node is available.
496 final public PhylogenyNode getPreviousExternalNode() {
497 if ( isInternal() ) {
498 throw new UnsupportedOperationException( "Cannot get the previous external node for an internal node." );
500 else if ( isRoot() /* TODO && tree is rooted */) {
501 throw new UnsupportedOperationException( "Cannot get the previous external node for a root node." );
503 else if ( isFirstExternalNode() ) {
504 throw new UnsupportedOperationException( "Attempt to get previous external node of the first external node." );
506 int index = getChildNodeIndex();
507 PhylogenyNode previous_node = this;
508 PhylogenyNode current_node = getParent();
509 while ( !current_node.isRoot()
510 && ( ( current_node.getNumberOfDescendants() == 1 ) || previous_node.isFirstChildNode() ) ) {
511 index = current_node.getChildNodeIndex();
512 previous_node = current_node;
513 current_node = current_node.getParent();
515 current_node = current_node.getChildNode( index - 1 );
516 while ( current_node.isInternal() ) {
517 current_node = current_node.getLastChildNode();
523 * Used for drawing of Trees.
525 final public float getXcoord() {
529 final public float getXSecondary() {
534 * Used for drawing of Trees.
536 final public float getYcoord() {
540 final public float getYSecondary() {
545 final public int hashCode() {
546 final NodeData data = getNodeData();
547 if ( ( getName().length() < 1 ) && !data.isHasSequence() && !data.isHasTaxonomy() ) {
548 return super.hashCode();
550 int result = getName().hashCode();
551 if ( data.isHasSequence() ) {
552 result ^= data.getSequence().hashCode();
554 if ( data.isHasTaxonomy() ) {
555 result ^= data.getTaxonomy().hashCode();
560 final private void init() {
561 _descendants = new ArrayList<PhylogenyNode>();
568 * Deletes data of this PhylogenyNode. Links to the other Nodes in the
569 * Phylogeny, the ID and the sum of external nodes are NOT deleted. Field
570 * "_link" (_link to Nodes in other Phylogeny) IS deleted.
572 * @see #getLink() (Last modified: 12/20/03)
574 final public void initializeData() {
579 _distance_parent = PhylogenyNode.DISTANCE_DEFAULT;
587 * Returns whether this PhylogenyNode should be drawn as collapsed.
589 final public boolean isCollapse() {
594 * Returns true if this PhylogenyNode represents a _duplication event, false
597 final public boolean isDuplication() {
598 return getNodeData().isHasEvent() && getNodeData().getEvent().isDuplication();
602 * Checks whether this PhylogenyNode is external (tip).
604 * @return true if this PhylogenyNode is external, false otherwise
606 final public boolean isExternal() {
607 return ( getNumberOfDescendants() < 1 );
613 * @return DOCUMENT ME!
615 final public boolean isFirstChildNode() {
616 if ( isRoot() /* and tree is rooted TODO */) {
617 throw new UnsupportedOperationException( "Cannot determine whether the root is the first child node of its _parent." );
619 return ( getChildNodeIndex() == 0 );
625 * @return DOCUMENT ME!
627 final public boolean isFirstExternalNode() {
628 if ( isInternal() ) {
631 PhylogenyNode node = this;
632 while ( !node.isRoot() ) {
633 if ( !node.isFirstChildNode() ) {
636 node = node.getParent();
642 * Returns whether a _duplication or speciation event has been assigned for
643 * this PhylogenyNode.
645 final public boolean isHasAssignedEvent() {
646 if ( !getNodeData().isHasEvent() ) {
649 if ( ( getNodeData().getEvent() ).isUnassigned() ) {
656 * Checks whether this PhylogenyNode is internal (tip).
658 * @return true if this PhylogenyNode is external, false otherwise
660 final public boolean isInternal() {
661 return ( !isExternal() );
665 * Returns true if this node is the last child node of its _parent.
667 * [last modified June 01, 2005 by CMZ]
669 * @return true if this node is the last child node of its _parent, false
672 final public boolean isLastChildNode() {
673 if ( isRoot() /* and tree is rooted TODO */) {
674 throw new UnsupportedOperationException( "Cannot determine whether the root is the last child node of its _parent." );
676 return ( getChildNodeIndex() == ( getParent().getNumberOfDescendants() - 1 ) );
682 * @return DOCUMENT ME!
684 final public boolean isLastExternalNode() {
685 if ( isInternal() ) {
688 PhylogenyNode node = this;
689 while ( !node.isRoot() ) {
690 if ( !node.isLastChildNode() ) {
693 node = node.getParent();
699 * Checks whether this PhylogenyNode is a root.
701 * @return true if this PhylogenyNode is the root, false otherwise
703 final public boolean isRoot() {
704 return _parent == null;
707 final public boolean isSpeciation() {
708 return getNodeData().isHasEvent() && getNodeData().getEvent().isSpeciation();
711 // ---------------------------------------------------------
713 // ---------------------------------------------------------
714 final public PhylogenyNodeIterator iterateChildNodesForward() {
715 return new ChildNodeIteratorForward( this );
718 // ---------------------------------------------------------
720 // ---------------------------------------------------------
722 * Prints to the console the subtree originating from this PhylogenyNode in
725 public void preorderPrint() {
726 System.out.println( this + "\n" );
727 if ( isInternal() ) {
728 for( int i = 0; i < getNumberOfDescendants(); ++i ) {
729 getChildNode( i ).preorderPrint();
734 final public void removeChildNode( final int i ) {
735 if ( isExternal() ) {
736 throw new UnsupportedOperationException( "cannot get the child node for a external node." );
738 if ( ( i >= getNumberOfDescendants() ) || ( i < 0 ) ) {
739 throw new IllegalArgumentException( "attempt to get child node " + i + " of a node with "
740 + getNumberOfDescendants() + " child nodes." );
742 getDescendants().remove( i );
745 final public void removeChildNode( final PhylogenyNode remove_me ) {
746 removeChildNode( remove_me.getChildNodeIndex() );
749 final public void setBranchData( final BranchData branch_data ) {
750 _branch_data = branch_data;
754 * Sets the first child PhylogenyNode of this PhylogenyNode to n.
756 final public void setChild1( final PhylogenyNode n ) {
757 setChildNode( 0, n );
761 * Sets the second child PhylogenyNode of this PhylogenyNode to n.
763 final public void setChild2( final PhylogenyNode n ) {
764 setChildNode( 1, n );
768 * Inserts PhylogenyNode n at the specified position i into the list of
769 * child nodes. This does not allow null slots in the list of child nodes:
770 * If i is larger than the number of child nodes, n is just added to the
771 * list, not place at index i.
774 * the index of position where to add the child
776 * the PhylogenyNode to add
778 final public void setChildNode( final int i, final PhylogenyNode node ) {
779 node.setParent( this );
780 if ( getNumberOfDescendants() <= i ) {
781 addChildNode( node );
784 getDescendants().set( i, node );
788 final void setChildNodeOnly( final int i, final PhylogenyNode node ) {
789 if ( getNumberOfDescendants() <= i ) {
790 addChildNode( node );
793 getDescendants().set( i, node );
798 * Sets whether this PhylogenyNode should be drawn as collapsed.
800 final public void setCollapse( final boolean b ) {
805 * Sets the length of the branch leading to the _parent of this
806 * PhylogenyNode to double d.
809 final public void setDistanceToParent( final double d ) {
810 _distance_parent = d;
814 * Sets the _indicator value of this PhylogenyNode to i.
816 final public void setIndicator( final byte i ) {
820 // --------------------------------------------------------------------
821 // Adjust methods (related to Phylogeny construction and
822 // Phylogeny modification)
823 // --------------------------------------------------------------------
825 * Sets the indicators of all the children of this PhylogenyNode to zero.
827 final void setIndicatorsToZero() {
828 for( final PreorderTreeIterator it = new PreorderTreeIterator( this ); it.hasNext(); ) {
829 it.next().setIndicator( ( byte ) 0 );
834 * Sets the linked PhylogenyNode of this PhylogenyNode to n. Currently, this
835 * method is only used for the speciation-_duplication assignment
838 final public void setLink( final PhylogenyNode n ) {
843 * Sets the name of this node.
846 final public void setName( final String node_name ) {
847 getNodeData().setNodeName( node_name );
851 * Sets the Id of this PhylogenyNode to i. In most cases, this number
852 * should not be set to values lower than getNodeCount() -- which this method
855 synchronized final protected void setId( final int i ) {
856 if ( i < getNodeCount() ) {
857 throw new IllegalArgumentException( "attempt to set node id to a value less than total node count (thus violating the uniqueness of node ids)" );
863 * Sets the _parent PhylogenyNode of this PhylogenyNode to n.
866 final public void setParent( final PhylogenyNode n ) {
871 * Sets the total number of external Nodes originating from this
872 * PhylogenyNode to i (int).
874 final public void setSumExtNodes( final int i ) {
876 throw new IllegalArgumentException( "attempt to set sum of external nodes to less than one" );
882 * Used for drawing of Trees.
884 final public void setXcoord( final float x ) {
888 final public void setXSecondary( final float x_secondary ) {
889 _x_secondary = x_secondary;
894 * Used for drawing of Trees.
896 final public void setYcoord( final float y ) {
900 final public void setYSecondary( final float y_secondary ) {
901 _y_secondary = y_secondary;
904 // ---------------------------------------------------------
905 // Writing of Nodes to Strings
906 // ---------------------------------------------------------
907 final public String toNewHampshire( final boolean simple_nh, final boolean write_distance_to_parent ) {
908 final StringBuilder sb = new StringBuilder();
910 if ( !ForesterUtil.isEmpty( getName() ) ) {
913 else if ( getNodeData().isHasTaxonomy() ) {
914 if ( !ForesterUtil.isEmpty( getNodeData().getTaxonomy().getTaxonomyCode() ) ) {
915 data = getNodeData().getTaxonomy().getTaxonomyCode();
917 else if ( !ForesterUtil.isEmpty( getNodeData().getTaxonomy().getScientificName() ) ) {
918 data = getNodeData().getTaxonomy().getScientificName();
920 else if ( !ForesterUtil.isEmpty( getNodeData().getTaxonomy().getCommonName() ) ) {
921 data = getNodeData().getTaxonomy().getCommonName();
923 else if ( getNodeData().getTaxonomy().getTaxonomyCode() != null ) {
924 data = getNodeData().getTaxonomy().getTaxonomyCode();
927 else if ( getNodeData().isHasSequence() ) {
928 if ( !ForesterUtil.isEmpty( getNodeData().getSequence().getName() ) ) {
929 data = getNodeData().getSequence().getName();
932 if ( data.length() > 0 ) {
933 data = ForesterUtil.replaceIllegalNhCharacters( data );
934 if ( simple_nh && ( data.length() > 10 ) ) {
935 data = data.substring( 0, 11 );
937 if ( ForesterUtil.isContainsParanthesesableNhCharacter( data ) ) {
946 if ( ( getDistanceToParent() != PhylogenyNode.DISTANCE_DEFAULT ) && write_distance_to_parent ) {
948 sb.append( getDistanceToParent() );
950 return sb.toString();
954 * Converts this PhylogenyNode to a New Hampshire X (NHX) String
957 final public String toNewHampshireX() {
958 final StringBuffer sb = new StringBuffer();
959 final StringBuffer s_nhx = new StringBuffer();
960 if ( !ForesterUtil.isEmpty( getName() ) ) {
961 final String name = ForesterUtil.replaceIllegalNhCharacters( getName() );
962 if ( ForesterUtil.isContainsParanthesesableNhCharacter( name ) ) {
971 if ( getDistanceToParent() != PhylogenyNode.DISTANCE_DEFAULT ) {
973 sb.append( getDistanceToParent() );
975 if ( getNodeDataDirectly() != null ) {
976 s_nhx.append( getNodeDataDirectly().toNHX() );
978 if ( getBranchDataDirectly() != null ) {
979 s_nhx.append( getBranchDataDirectly().toNHX() );
981 if ( s_nhx.length() > 0 ) {
982 sb.append( "[&&NHX" );
986 return sb.toString();
990 final public String toString() {
991 final StringBuilder sb = new StringBuilder();
992 if ( !ForesterUtil.isEmpty( getName() ) ) {
993 sb.append( getName() );
997 sb.append( getId() );
999 return sb.toString();
1003 * Decreases the total number of all Nodes created so far by one.
1005 final static synchronized void decreaseNodeCount() {
1006 --PhylogenyNode._node_count;
1010 * Returns the total number of all Nodes created so far.
1012 * @return total number of Nodes (int)
1014 synchronized final public static int getNodeCount() {
1015 return PhylogenyNode._node_count;
1019 * Increases the total number of all Nodes created so far by one.
1021 synchronized final private static void increaseNodeCount() {
1022 ++PhylogenyNode._node_count;
1026 * Sets the total number of all Nodes created so far to i (int).
1028 synchronized final static void setNodeCount( final int i ) {
1029 PhylogenyNode._node_count = i;
1032 public static PhylogenyNode createInstanceFromNhxString( final String nhx ) throws NHXFormatException {
1033 return new PhylogenyNode( nhx, ForesterUtil.TAXONOMY_EXTRACTION.NO, false );
1036 public static PhylogenyNode createInstanceFromNhxString( final String nhx,
1037 final ForesterUtil.TAXONOMY_EXTRACTION taxonomy_extraction )
1038 throws NHXFormatException {
1039 return new PhylogenyNode( nhx, taxonomy_extraction, false );
1042 public static PhylogenyNode createInstanceFromNhxString( final String nhx,
1043 final ForesterUtil.TAXONOMY_EXTRACTION taxonomy_extraction,
1044 final boolean replace_underscores )
1045 throws NHXFormatException {
1046 return new PhylogenyNode( nhx, taxonomy_extraction, replace_underscores );
1049 private PhylogenyNode( final String nhx,
1050 final ForesterUtil.TAXONOMY_EXTRACTION taxonomy_extraction,
1051 final boolean replace_underscores ) throws NHXFormatException {
1053 NHXParser.parseNHX( nhx, this, taxonomy_extraction, replace_underscores );
1054 setId( PhylogenyNode.getNodeCount() );
1055 PhylogenyNode.increaseNodeCount();
1056 setSumExtNodes( 1 ); // For ext node, this number is 1 (not 0!!)