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.io.parsers.phyloxml.PhyloXmlUtil;
36 import org.forester.phylogeny.data.BranchData;
37 import org.forester.phylogeny.data.Confidence;
38 import org.forester.phylogeny.data.NodeData;
39 import org.forester.phylogeny.data.PhylogenyDataUtil;
40 import org.forester.phylogeny.iterators.ChildNodeIteratorForward;
41 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
42 import org.forester.phylogeny.iterators.PreorderTreeIterator;
43 import org.forester.util.ForesterUtil;
45 public class PhylogenyNode implements PhylogenyNodeI, Comparable<PhylogenyNode> {
47 private static int _node_count = 0;
48 private byte _indicator;
50 private int _sum_ext_nodes;
53 private double _distance_parent;
54 private boolean _collapse;
55 private PhylogenyNode _parent;
56 private PhylogenyNode _link;
57 private ArrayList<PhylogenyNode> _descendants;
58 private NodeData _node_data;
59 private BranchData _branch_data;
60 private float _x_secondary;
61 private float _y_secondary;
64 * Default constructor for PhylogenyNode.
66 public PhylogenyNode() {
68 setId( PhylogenyNode.getNodeCount() );
69 PhylogenyNode.increaseNodeCount();
70 setSumExtNodes( 1 ); // For ext node, this number is 1 (not 0!!)
74 * Adds PhylogenyNode n to the list of child nodes and sets the _parent of n
78 * the PhylogenyNode to add
81 final public void addAsChild( final PhylogenyNodeI node ) {
82 final PhylogenyNode n = ( PhylogenyNode ) node;
88 * Adds PhylogenyNode n to the list of child nodes. But does NOT set the
89 * _parent of n to this.
91 * @see addAsChild( PhylogenyNode n )
93 * the PhylogenyNode to add
95 final private void addChildNode( final PhylogenyNode child ) {
96 getDescendants().add( child );
100 final public int compareTo( final PhylogenyNode o ) {
101 final PhylogenyNode n = o;
102 if ( ( getName() == null ) || ( n.getName() == null ) ) {
105 return getName().compareTo( n.getName() );
108 // ---------------------------------------------------------
109 // Copy and delete Nodes, copy subtress
110 // ---------------------------------------------------------
112 * Returns a new PhylogenyNode which has its data copied from this
113 * PhylogenyNode. Links to the other Nodes in the same Phylogeny are NOT
114 * copied (e.g. _link to _parent). Field "_link" IS copied.
118 final public PhylogenyNode copyNodeData() {
119 final PhylogenyNode node = new PhylogenyNode();
120 PhylogenyNode.decreaseNodeCount();
122 node._sum_ext_nodes = _sum_ext_nodes;
123 node._indicator = _indicator;
126 node._distance_parent = _distance_parent;
127 node._collapse = _collapse;
129 if ( _node_data != null ) {
130 node._node_data = ( NodeData ) _node_data.copy();
132 if ( _branch_data != null ) {
133 node._branch_data = ( BranchData ) _branch_data.copy();
139 * Returns a new PhylogenyNode which has the same data as this
140 * PhylogenyNode. Links to the other Nodes in the same Phylogeny are NOT
141 * copied (e.g. _link to _parent). Field "_link" IS copied.
145 final public PhylogenyNode copyNodeDataShallow() {
146 final PhylogenyNode node = new PhylogenyNode();
147 PhylogenyNode.decreaseNodeCount();
149 node._sum_ext_nodes = _sum_ext_nodes;
150 node._indicator = _indicator;
153 node._distance_parent = _distance_parent;
154 node._collapse = _collapse;
156 node._node_data = _node_data;
157 node._branch_data = _branch_data;
163 * Based on node name, sequence, and taxonomy.
167 final public boolean equals( final Object o ) {
171 else if ( o == null ) {
174 else if ( o.getClass() != this.getClass() ) {
175 throw new IllegalArgumentException( "attempt to check [" + this.getClass() + "] equality to " + o + " ["
176 + o.getClass() + "]" );
179 final PhylogenyNode other = ( PhylogenyNode ) o;
180 if ( !getName().equals( other.getName() ) ) {
183 final NodeData this_data = getNodeData();
184 final NodeData other_data = other.getNodeData();
185 if ( ( this_data.isHasSequence() && other_data.isHasSequence() )
186 && ( this_data.isHasTaxonomy() && other_data.isHasTaxonomy() ) ) {
187 return ( this_data.getTaxonomy().isEqual( other_data.getTaxonomy() ) && this_data.getSequence()
188 .isEqual( other_data.getSequence() ) );
190 else if ( this_data.isHasSequence() && other_data.isHasSequence() ) {
191 return ( this_data.getSequence().isEqual( other_data.getSequence() ) );
193 else if ( this_data.isHasTaxonomy() && other_data.isHasTaxonomy() ) {
194 return ( this_data.getTaxonomy().isEqual( other_data.getTaxonomy() ) );
196 else if ( getName().length() > 0 ) {
197 // Node name is not empty, and equal.
206 // ---------------------------------------------------------
207 // Obtaining of Nodes
208 // ---------------------------------------------------------
210 * Returns a List containing references to all external children of this
213 * @return List of references to external Nodes
215 final public List<PhylogenyNode> getAllExternalDescendants() {
216 final List<PhylogenyNode> nodes = new ArrayList<PhylogenyNode>();
217 if ( isExternal() ) {
221 PhylogenyNode node1 = this;
222 while ( !node1.isExternal() ) {
223 node1 = node1.getFirstChildNode();
225 PhylogenyNode node2 = this;
226 while ( !node2.isExternal() ) {
227 node2 = node2.getLastChildNode();
229 while ( node1 != node2 ) {
231 node1 = node1.getNextExternalNode();
238 * Returns a List containing references to all names of the external
239 * children of this PhylogenyNode.
241 * @return List of references to names of external Nodes
243 final public List<String> getAllExternalDescendantsNames() {
244 final List<PhylogenyNode> c = getAllExternalDescendants();
245 final List<String> n = new ArrayList<String>( c.size() );
246 for( final PhylogenyNode phylogenyNode : c ) {
247 n.add( phylogenyNode.getName() );
252 final public BranchData getBranchData() {
253 if ( _branch_data == null ) {
254 _branch_data = new BranchData();
259 final BranchData getBranchDataDirectly() {
264 * This return child node n of this node.
267 * the index of the child to get
268 * @return the child node with index n
269 * @throws IllegalArgumentException
270 * if n is out of bounds
273 final public PhylogenyNode getChildNode( final int i ) {
274 if ( isExternal() ) {
275 throw new UnsupportedOperationException( "attempt to get the child node of an external node." );
277 if ( ( i >= getNumberOfDescendants() ) || ( i < 0 ) ) {
278 throw new IllegalArgumentException( "attempt to get child node " + i + " of a node with "
279 + getNumberOfDescendants() + " child nodes" );
281 return getDescendants().get( i );
285 * Convenience method. Returns the first child PhylogenyNode of this
288 final public PhylogenyNode getChildNode1() {
289 return getChildNode( 0 );
293 * Convenience method. Returns the second child PhylogenyNode of this
296 * [last modified May 18, 2005 by CMZ]
298 final public PhylogenyNode getChildNode2() {
299 return getChildNode( 1 );
303 * This gets the child node index of this node.
306 * @return the child node index of this node
307 * @throws UnsupportedOperationException
308 * if this node is a root node
310 final public int getChildNodeIndex() {
311 return getChildNodeIndex( getParent() );
315 * This gets the child node index of this node, given that parent is its
318 * [last modified Aug 14, 2006 by CMZ]
320 * @return the child node index of this node
321 * @throws UnsupportedOperationException
322 * if this node is a root node
324 final public int getChildNodeIndex( final PhylogenyNode parent ) {
326 throw new UnsupportedOperationException( "Cannot get the child index for a root node." );
328 for( int i = 0; i < parent.getNumberOfDescendants(); ++i ) {
329 if ( parent.getChildNode( i ) == this ) {
333 throw new RuntimeException( "Unexpected exception: Could not determine the child index for node: " + this );
336 final public List<PhylogenyNode> getDescendants() {
341 * Returns the length of the branch leading to the _parent of this
342 * PhylogenyNode (double).
345 final public double getDistanceToParent() {
346 return _distance_parent;
350 * Convenience method. Returns the first child node of this node.
352 * [last modified May 18, 2005 by CMZ]
354 * @return the first child node of this node
356 public final PhylogenyNode getFirstChildNode() {
357 return getChildNode( 0 );
361 * Returns the _indicator value of this PhylogenyNode.
363 public final byte getIndicator() {
368 * Convenience method. Returns the last child node of this node.
370 * [last modified May 18, 2005 by CMZ]
372 * @return the last child node of this node
374 public final PhylogenyNode getLastChildNode() {
375 return getChildNode( getNumberOfDescendants() - 1 );
379 * Returns a refernce to the linked PhylogenyNode of this PhylogenyNode.
380 * Currently, this method is only used for the speciation-_duplication
381 * assignment algorithms.
383 public final PhylogenyNode getLink() {
388 * Returns a refernce to the next external PhylogenyNode of this
389 * PhylogenyNode. TODO should be in Phylogeny. Returns null if no next
390 * external node is available.
392 public final PhylogenyNode getNextExternalNode() {
393 if ( isInternal() ) {
394 throw new UnsupportedOperationException( "attempt to get next external node of an internal node" );
396 else if ( isLastExternalNode() ) {
399 int index = getChildNodeIndex();
400 PhylogenyNode previous_node = this;
401 PhylogenyNode current_node = getParent();
402 while ( !current_node.isRoot()
403 && ( ( current_node.getNumberOfDescendants() == 1 ) || previous_node.isLastChildNode() ) ) {
404 index = current_node.getChildNodeIndex();
405 previous_node = current_node;
406 current_node = current_node.getParent();
408 current_node = current_node.getChildNode( index + 1 );
409 while ( current_node.isInternal() ) {
410 current_node = current_node.getFirstChildNode();
415 public final PhylogenyNode getNextExternalNodeWhileTakingIntoAccountCollapsedNodes() {
417 if ( isInternal() && !isCollapse() ) {
418 throw new UnsupportedOperationException( "attempt to get next external node of an uncollapsed internal node" );
423 if ( getParent().isCollapse() ) {
424 throw new UnsupportedOperationException( "attempt to get next external node of node with a collapsed parent" );
426 // This checks if last node.
427 PhylogenyNode n = this;
429 while ( !n.isRoot() ) {
430 if ( !n.isLastChildNode() ) {
439 int index = getChildNodeIndex();
440 PhylogenyNode previous_node = this;
441 PhylogenyNode current_node = getParent();
442 while ( !current_node.isRoot()
443 && ( current_node.isCollapse() || ( current_node.getNumberOfDescendants() == 1 ) || previous_node
444 .isLastChildNode() ) ) {
445 index = current_node.getChildNodeIndex();
446 previous_node = current_node;
447 current_node = current_node.getParent();
449 if ( index < current_node.getNumberOfDescendants() - 1 ) {
450 current_node = current_node.getChildNode( index + 1 );
452 while ( current_node.isInternal() && !current_node.isCollapse() ) {
453 current_node = current_node.getFirstChildNode();
458 public final NodeData getNodeData() {
459 if ( _node_data == null ) {
460 _node_data = new NodeData();
465 final NodeData getNodeDataDirectly() {
469 // ---------------------------------------------------------
470 // Set and get methods for Nodes
471 // ---------------------------------------------------------
473 * Returns the ID (int) of this PhylogenyNode.
476 final public int getId() {
481 * Returns the name of this node.
484 final public String getName() {
485 return getNodeData().getNodeName();
488 final public int getNumberOfDescendants() {
489 return _descendants.size();
493 * Returns the total number of external Nodes originating from this
494 * PhylogenyNode (int).
496 final public int getNumberOfExternalNodes() {
497 return _sum_ext_nodes;
500 final public int getNumberOfParents() {
505 * Returns a refernce to the parent PhylogenyNode of this PhylogenyNode.
507 final public PhylogenyNode getParent() {
512 * Returns a refernce to the next external PhylogenyNode of this
513 * PhylogenyNode. TODO should be in Phylogeny. Returns null if no next
514 * external node is available.
516 final public PhylogenyNode getPreviousExternalNode() {
517 if ( isInternal() ) {
518 throw new UnsupportedOperationException( "Cannot get the previous external node for an internal node." );
520 else if ( isRoot() /* TODO && tree is rooted */) {
521 throw new UnsupportedOperationException( "Cannot get the previous external node for a root node." );
523 else if ( isFirstExternalNode() ) {
524 throw new UnsupportedOperationException( "Attempt to get previous external node of the first external node." );
526 int index = getChildNodeIndex();
527 PhylogenyNode previous_node = this;
528 PhylogenyNode current_node = getParent();
529 while ( !current_node.isRoot()
530 && ( ( current_node.getNumberOfDescendants() == 1 ) || previous_node.isFirstChildNode() ) ) {
531 index = current_node.getChildNodeIndex();
532 previous_node = current_node;
533 current_node = current_node.getParent();
535 current_node = current_node.getChildNode( index - 1 );
536 while ( current_node.isInternal() ) {
537 current_node = current_node.getLastChildNode();
543 * Used for drawing of Trees.
545 final public float getXcoord() {
549 final public float getXSecondary() {
554 * Used for drawing of Trees.
556 final public float getYcoord() {
560 final public float getYSecondary() {
565 final public int hashCode() {
566 final NodeData data = getNodeData();
567 if ( ( getName().length() < 1 ) && !data.isHasSequence() && !data.isHasTaxonomy() ) {
568 return super.hashCode();
570 int result = getName().hashCode();
571 if ( data.isHasSequence() ) {
572 result ^= data.getSequence().hashCode();
574 if ( data.isHasTaxonomy() ) {
575 result ^= data.getTaxonomy().hashCode();
580 final private void init() {
581 _descendants = new ArrayList<PhylogenyNode>();
588 * Deletes data of this PhylogenyNode. Links to the other Nodes in the
589 * Phylogeny, the ID and the sum of external nodes are NOT deleted. Field
590 * "_link" (_link to Nodes in other Phylogeny) IS deleted.
592 * @see #getLink() (Last modified: 12/20/03)
594 final public void initializeData() {
599 _distance_parent = PhylogenyDataUtil.BRANCH_LENGTH_DEFAULT;
607 * Returns whether this PhylogenyNode should be drawn as collapsed.
609 final public boolean isCollapse() {
614 * Returns true if this PhylogenyNode represents a _duplication event, false
617 final public boolean isDuplication() {
618 return getNodeData().isHasEvent() && getNodeData().getEvent().isDuplication();
622 * Checks whether this PhylogenyNode is external (tip).
624 * @return true if this PhylogenyNode is external, false otherwise
626 final public boolean isExternal() {
627 return ( getNumberOfDescendants() < 1 );
633 * @return DOCUMENT ME!
635 final public boolean isFirstChildNode() {
636 if ( isRoot() /* and tree is rooted TODO */) {
637 throw new UnsupportedOperationException( "Cannot determine whether the root is the first child node of its _parent." );
639 return ( getChildNodeIndex() == 0 );
645 * @return DOCUMENT ME!
647 final public boolean isFirstExternalNode() {
648 if ( isInternal() ) {
651 PhylogenyNode node = this;
652 while ( !node.isRoot() ) {
653 if ( !node.isFirstChildNode() ) {
656 node = node.getParent();
662 * Returns whether a _duplication or speciation event has been assigned for
663 * this PhylogenyNode.
665 final public boolean isHasAssignedEvent() {
666 if ( !getNodeData().isHasEvent() ) {
669 if ( ( getNodeData().getEvent() ).isUnassigned() ) {
676 * Checks whether this PhylogenyNode is internal (tip).
678 * @return true if this PhylogenyNode is external, false otherwise
680 final public boolean isInternal() {
681 return ( !isExternal() );
685 * Returns true if this node is the last child node of its _parent.
687 * [last modified June 01, 2005 by CMZ]
689 * @return true if this node is the last child node of its _parent, false
692 final public boolean isLastChildNode() {
693 if ( isRoot() /* and tree is rooted TODO */) {
694 throw new UnsupportedOperationException( "Cannot determine whether the root is the last child node of its _parent." );
696 return ( getChildNodeIndex() == ( getParent().getNumberOfDescendants() - 1 ) );
702 * @return DOCUMENT ME!
704 final public boolean isLastExternalNode() {
705 if ( isInternal() ) {
708 PhylogenyNode node = this;
709 while ( !node.isRoot() ) {
710 if ( !node.isLastChildNode() ) {
713 node = node.getParent();
719 * Checks whether this PhylogenyNode is a root.
721 * @return true if this PhylogenyNode is the root, false otherwise
723 final public boolean isRoot() {
724 return _parent == null;
727 final public boolean isSpeciation() {
728 return getNodeData().isHasEvent() && getNodeData().getEvent().isSpeciation();
731 // ---------------------------------------------------------
733 // ---------------------------------------------------------
734 final public PhylogenyNodeIterator iterateChildNodesForward() {
735 return new ChildNodeIteratorForward( this );
738 // ---------------------------------------------------------
740 // ---------------------------------------------------------
742 * Prints to the console the subtree originating from this PhylogenyNode in
745 public void preorderPrint() {
746 System.out.println( this + "\n" );
747 if ( isInternal() ) {
748 for( int i = 0; i < getNumberOfDescendants(); ++i ) {
749 getChildNode( i ).preorderPrint();
754 final public void removeChildNode( final int i ) {
755 if ( isExternal() ) {
756 throw new UnsupportedOperationException( "cannot get the child node for a external node." );
758 if ( ( i >= getNumberOfDescendants() ) || ( i < 0 ) ) {
759 throw new IllegalArgumentException( "attempt to get child node " + i + " of a node with "
760 + getNumberOfDescendants() + " child nodes." );
762 getDescendants().remove( i );
765 final public void removeChildNode( final PhylogenyNode remove_me ) {
766 removeChildNode( remove_me.getChildNodeIndex() );
769 final public void setBranchData( final BranchData branch_data ) {
770 _branch_data = branch_data;
774 * Sets the first child PhylogenyNode of this PhylogenyNode to n.
776 final public void setChild1( final PhylogenyNode n ) {
777 setChildNode( 0, n );
781 * Sets the second child PhylogenyNode of this PhylogenyNode to n.
783 final public void setChild2( final PhylogenyNode n ) {
784 setChildNode( 1, n );
788 * Inserts PhylogenyNode n at the specified position i into the list of
789 * child nodes. This does not allow null slots in the list of child nodes:
790 * If i is larger than the number of child nodes, n is just added to the
791 * list, not place at index i.
794 * the index of position where to add the child
796 * the PhylogenyNode to add
798 final public void setChildNode( final int i, final PhylogenyNode node ) {
799 node.setParent( this );
800 if ( getNumberOfDescendants() <= i ) {
801 addChildNode( node );
804 getDescendants().set( i, node );
808 final void setChildNodeOnly( final int i, final PhylogenyNode node ) {
809 if ( getNumberOfDescendants() <= i ) {
810 addChildNode( node );
813 getDescendants().set( i, node );
818 * Sets whether this PhylogenyNode should be drawn as collapsed.
820 final public void setCollapse( final boolean b ) {
825 * Sets the length of the branch leading to the _parent of this
826 * PhylogenyNode to double d.
829 final public void setDistanceToParent( final double d ) {
830 _distance_parent = d;
834 * Sets the _indicator value of this PhylogenyNode to i.
836 final public void setIndicator( final byte i ) {
840 // --------------------------------------------------------------------
841 // Adjust methods (related to Phylogeny construction and
842 // Phylogeny modification)
843 // --------------------------------------------------------------------
845 * Sets the indicators of all the children of this PhylogenyNode to zero.
847 final void setIndicatorsToZero() {
848 for( final PreorderTreeIterator it = new PreorderTreeIterator( this ); it.hasNext(); ) {
849 it.next().setIndicator( ( byte ) 0 );
854 * Sets the linked PhylogenyNode of this PhylogenyNode to n. Currently, this
855 * method is only used for the speciation-_duplication assignment
858 final public void setLink( final PhylogenyNode n ) {
863 * Sets the name of this node.
866 final public void setName( final String node_name ) {
867 getNodeData().setNodeName( node_name );
871 * Sets the Id of this PhylogenyNode to i. In most cases, this number
872 * should not be set to values lower than getNodeCount() -- which this method
875 synchronized final protected void setId( final int i ) {
876 if ( i < getNodeCount() ) {
877 throw new IllegalArgumentException( "attempt to set node id to a value less than total node count (thus violating the uniqueness of node ids)" );
883 * Sets the _parent PhylogenyNode of this PhylogenyNode to n.
886 final public void setParent( final PhylogenyNode n ) {
891 * Sets the total number of external Nodes originating from this
892 * PhylogenyNode to i (int).
894 final public void setSumExtNodes( final int i ) {
896 throw new IllegalArgumentException( "attempt to set sum of external nodes to less than one" );
902 * Used for drawing of Trees.
904 final public void setXcoord( final float x ) {
908 final public void setXSecondary( final float x_secondary ) {
909 _x_secondary = x_secondary;
914 * Used for drawing of Trees.
916 final public void setYcoord( final float y ) {
920 final public void setYSecondary( final float y_secondary ) {
921 _y_secondary = y_secondary;
924 // ---------------------------------------------------------
925 // Writing of Nodes to Strings
926 // ---------------------------------------------------------
927 final public String toNewHampshire( final boolean simple_nh,
928 final boolean write_distance_to_parent,
929 final NH_CONVERSION_SUPPORT_VALUE_STYLE svs ) {
930 final StringBuilder sb = new StringBuilder();
932 if ( ( svs == NH_CONVERSION_SUPPORT_VALUE_STYLE.AS_INTERNAL_NODE_NAMES ) && !isExternal() ) {
933 if ( getBranchData().isHasConfidences()
934 && ( getBranchData().getConfidence( 0 ).getValue() != Confidence.CONFIDENCE_DEFAULT_VALUE ) ) {
935 data = Confidence.FORMATTER.format( ForesterUtil
936 .round( getBranchData().getConfidence( 0 ).getValue(),
937 PhyloXmlUtil.ROUNDING_DIGITS_FOR_PHYLOXML_DOUBLE_OUTPUT ) );
940 else if ( !ForesterUtil.isEmpty( getName() ) ) {
943 else if ( getNodeData().isHasTaxonomy() ) {
944 if ( !ForesterUtil.isEmpty( getNodeData().getTaxonomy().getTaxonomyCode() ) ) {
945 data = getNodeData().getTaxonomy().getTaxonomyCode();
947 else if ( !ForesterUtil.isEmpty( getNodeData().getTaxonomy().getScientificName() ) ) {
948 data = getNodeData().getTaxonomy().getScientificName();
950 else if ( !ForesterUtil.isEmpty( getNodeData().getTaxonomy().getCommonName() ) ) {
951 data = getNodeData().getTaxonomy().getCommonName();
953 else if ( getNodeData().getTaxonomy().getTaxonomyCode() != null ) {
954 data = getNodeData().getTaxonomy().getTaxonomyCode();
957 else if ( getNodeData().isHasSequence() ) {
958 if ( !ForesterUtil.isEmpty( getNodeData().getSequence().getName() ) ) {
959 data = getNodeData().getSequence().getName();
962 if ( data.length() > 0 ) {
963 data = ForesterUtil.replaceIllegalNhCharacters( data );
964 if ( simple_nh && ( data.length() > 10 ) ) {
965 data = data.substring( 0, 11 );
967 if ( ForesterUtil.isContainsParanthesesableNhCharacter( data ) ) {
976 if ( write_distance_to_parent && ( getDistanceToParent() != PhylogenyDataUtil.BRANCH_LENGTH_DEFAULT ) ) {
978 sb.append( getDistanceToParent() );
980 if ( ( svs == NH_CONVERSION_SUPPORT_VALUE_STYLE.IN_SQUARE_BRACKETS ) && !isExternal()
981 && getBranchData().isHasConfidences()
982 && ( getBranchData().getConfidence( 0 ).getValue() != Confidence.CONFIDENCE_DEFAULT_VALUE ) ) {
984 sb.append( Confidence.FORMATTER.format( ForesterUtil
985 .round( getBranchData().getConfidence( 0 ).getValue(),
986 PhyloXmlUtil.ROUNDING_DIGITS_FOR_PHYLOXML_DOUBLE_OUTPUT ) ) );
989 return sb.toString();
993 * Swaps the the two childern of a PhylogenyNode node of this Phylogeny.
995 public void swapChildren() throws RuntimeException {
996 if ( isExternal() ) {
997 throw new RuntimeException( "attempt to swap descendants of external node" );
999 if ( getNumberOfDescendants() != 2 ) {
1000 throw new RuntimeException( "attempt to swap descendants of node with " + getNumberOfDescendants()
1003 final PhylogenyNode a = getChildNode( 0 );
1004 final PhylogenyNode b = getChildNode( 1 );
1005 setChildNode( 0, b );
1006 setChildNode( 1, a );
1010 * Converts this PhylogenyNode to a New Hampshire X (NHX) String
1013 final public String toNewHampshireX() {
1014 final StringBuffer sb = new StringBuffer();
1015 final StringBuffer s_nhx = new StringBuffer();
1016 if ( !ForesterUtil.isEmpty( getName() ) ) {
1017 final String name = ForesterUtil.replaceIllegalNhCharacters( getName() );
1018 if ( ForesterUtil.isContainsParanthesesableNhCharacter( name ) ) {
1027 if ( getDistanceToParent() != PhylogenyDataUtil.BRANCH_LENGTH_DEFAULT ) {
1029 sb.append( getDistanceToParent() );
1031 if ( getNodeDataDirectly() != null ) {
1032 s_nhx.append( getNodeDataDirectly().toNHX() );
1034 if ( getBranchDataDirectly() != null ) {
1035 s_nhx.append( getBranchDataDirectly().toNHX() );
1037 if ( s_nhx.length() > 0 ) {
1038 sb.append( "[&&NHX" );
1042 return sb.toString();
1046 final public String toString() {
1047 final StringBuilder sb = new StringBuilder();
1048 if ( !ForesterUtil.isEmpty( getName() ) ) {
1049 sb.append( getName() );
1053 sb.append( getId() );
1055 return sb.toString();
1059 * Decreases the total number of all Nodes created so far by one.
1061 final static synchronized void decreaseNodeCount() {
1062 --PhylogenyNode._node_count;
1066 * Returns the total number of all Nodes created so far.
1068 * @return total number of Nodes (int)
1070 synchronized final public static int getNodeCount() {
1071 return PhylogenyNode._node_count;
1075 * Increases the total number of all Nodes created so far by one.
1077 synchronized final private static void increaseNodeCount() {
1078 ++PhylogenyNode._node_count;
1082 * Sets the total number of all Nodes created so far to i (int).
1084 synchronized final static void setNodeCount( final int i ) {
1085 PhylogenyNode._node_count = i;
1088 public static PhylogenyNode createInstanceFromNhxString( final String nhx ) throws NHXFormatException {
1089 return new PhylogenyNode( nhx, PhylogenyMethods.TAXONOMY_EXTRACTION.NO, false );
1092 public static PhylogenyNode createInstanceFromNhxString( final String nhx,
1093 final PhylogenyMethods.TAXONOMY_EXTRACTION taxonomy_extraction )
1094 throws NHXFormatException {
1095 return new PhylogenyNode( nhx, taxonomy_extraction, false );
1098 public static PhylogenyNode createInstanceFromNhxString( final String nhx,
1099 final PhylogenyMethods.TAXONOMY_EXTRACTION taxonomy_extraction,
1100 final boolean replace_underscores )
1101 throws NHXFormatException {
1102 return new PhylogenyNode( nhx, taxonomy_extraction, replace_underscores );
1105 private PhylogenyNode( final String nhx,
1106 final PhylogenyMethods.TAXONOMY_EXTRACTION taxonomy_extraction,
1107 final boolean replace_underscores ) throws NHXFormatException {
1109 NHXParser.parseNHX( nhx, this, taxonomy_extraction, replace_underscores );
1110 setId( PhylogenyNode.getNodeCount() );
1111 PhylogenyNode.increaseNodeCount();
1112 setSumExtNodes( 1 ); // For ext node, this number is 1 (not 0!!)