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 final 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 = PhylogenyDataUtil.BRANCH_LENGTH_DEFAULT;
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() {
337 if ( _descendants == null ) {
338 _descendants = new ArrayList<PhylogenyNode>();
344 * Returns the length of the branch leading to the _parent of this
345 * PhylogenyNode (double).
348 final public double getDistanceToParent() {
349 return _distance_parent;
353 * Convenience method. Returns the first child node of this node.
355 * [last modified May 18, 2005 by CMZ]
357 * @return the first child node of this node
359 public final PhylogenyNode getFirstChildNode() {
360 return getChildNode( 0 );
364 * Returns the _indicator value of this PhylogenyNode.
366 public final byte getIndicator() {
371 * Convenience method. Returns the last child node of this node.
373 * [last modified May 18, 2005 by CMZ]
375 * @return the last child node of this node
377 public final PhylogenyNode getLastChildNode() {
378 return getChildNode( getNumberOfDescendants() - 1 );
382 * Returns a refernce to the linked PhylogenyNode of this PhylogenyNode.
383 * Currently, this method is only used for the speciation-_duplication
384 * assignment algorithms.
386 public final PhylogenyNode getLink() {
391 * Returns a refernce to the next external PhylogenyNode of this
392 * PhylogenyNode. TODO should be in Phylogeny. Returns null if no next
393 * external node is available.
395 public final PhylogenyNode getNextExternalNode() {
396 if ( isInternal() ) {
397 throw new UnsupportedOperationException( "attempt to get next external node of an internal node" );
399 else if ( isLastExternalNode() ) {
402 int index = getChildNodeIndex();
403 PhylogenyNode previous_node = this;
404 PhylogenyNode current_node = getParent();
405 while ( !current_node.isRoot()
406 && ( ( current_node.getNumberOfDescendants() == 1 ) || previous_node.isLastChildNode() ) ) {
407 index = current_node.getChildNodeIndex();
408 previous_node = current_node;
409 current_node = current_node.getParent();
411 current_node = current_node.getChildNode( index + 1 );
412 while ( current_node.isInternal() ) {
413 current_node = current_node.getFirstChildNode();
418 public final PhylogenyNode getNextExternalNodeWhileTakingIntoAccountCollapsedNodes() {
420 if ( isInternal() && !isCollapse() ) {
421 throw new UnsupportedOperationException( "attempt to get next external node of an uncollapsed internal node" );
426 if ( getParent().isCollapse() ) {
427 throw new UnsupportedOperationException( "attempt to get next external node of node with a collapsed parent" );
429 // This checks if last node.
430 PhylogenyNode n = this;
432 while ( !n.isRoot() ) {
433 if ( !n.isLastChildNode() ) {
442 int index = getChildNodeIndex();
443 PhylogenyNode previous_node = this;
444 PhylogenyNode current_node = getParent();
445 while ( !current_node.isRoot()
446 && ( current_node.isCollapse() || ( current_node.getNumberOfDescendants() == 1 ) || previous_node
447 .isLastChildNode() ) ) {
448 index = current_node.getChildNodeIndex();
449 previous_node = current_node;
450 current_node = current_node.getParent();
452 if ( index < current_node.getNumberOfDescendants() - 1 ) {
453 current_node = current_node.getChildNode( index + 1 );
455 while ( current_node.isInternal() && !current_node.isCollapse() ) {
456 current_node = current_node.getFirstChildNode();
461 public final NodeData getNodeData() {
462 if ( _node_data == null ) {
463 _node_data = new NodeData();
468 final NodeData getNodeDataDirectly() {
472 // ---------------------------------------------------------
473 // Set and get methods for Nodes
474 // ---------------------------------------------------------
476 * Returns the ID (int) of this PhylogenyNode.
479 final public int getId() {
484 * Returns the name of this node.
487 final public String getName() {
488 return getNodeData().getNodeName();
491 final public int getNumberOfDescendants() {
492 if ( _descendants == null ) {
495 return _descendants.size();
499 * Returns the total number of external Nodes originating from this
500 * PhylogenyNode (int).
502 final public int getNumberOfExternalNodes() {
503 return _sum_ext_nodes;
506 final public int getNumberOfParents() {
511 * Returns a refernce to the parent PhylogenyNode of this PhylogenyNode.
513 final public PhylogenyNode getParent() {
518 * Returns a refernce to the next external PhylogenyNode of this
519 * PhylogenyNode. TODO should be in Phylogeny. Returns null if no next
520 * external node is available.
522 final public PhylogenyNode getPreviousExternalNode() {
523 if ( isInternal() ) {
524 throw new UnsupportedOperationException( "Cannot get the previous external node for an internal node." );
526 else if ( isRoot() /* TODO && tree is rooted */) {
527 throw new UnsupportedOperationException( "Cannot get the previous external node for a root node." );
529 else if ( isFirstExternalNode() ) {
530 throw new UnsupportedOperationException( "Attempt to get previous external node of the first external node." );
532 int index = getChildNodeIndex();
533 PhylogenyNode previous_node = this;
534 PhylogenyNode current_node = getParent();
535 while ( !current_node.isRoot()
536 && ( ( current_node.getNumberOfDescendants() == 1 ) || previous_node.isFirstChildNode() ) ) {
537 index = current_node.getChildNodeIndex();
538 previous_node = current_node;
539 current_node = current_node.getParent();
541 current_node = current_node.getChildNode( index - 1 );
542 while ( current_node.isInternal() ) {
543 current_node = current_node.getLastChildNode();
549 * Used for drawing of Trees.
551 final public float getXcoord() {
555 final public float getXSecondary() {
560 * Used for drawing of Trees.
562 final public float getYcoord() {
566 final public float getYSecondary() {
571 final public int hashCode() {
572 final NodeData data = getNodeData();
573 if ( ( getName().length() < 1 ) && !data.isHasSequence() && !data.isHasTaxonomy() ) {
574 return super.hashCode();
576 int result = getName().hashCode();
577 if ( data.isHasSequence() ) {
578 result ^= data.getSequence().hashCode();
580 if ( data.isHasTaxonomy() ) {
581 result ^= data.getTaxonomy().hashCode();
586 // final private void init() {
587 //_descendants = new ArrayList<PhylogenyNode>();
588 // _parent = null; //TODO not needed?
589 // _id = 0; //TODO not needed?
590 //initializeData(); //TODO not needed?
593 * Deletes data of this PhylogenyNode. Links to the other Nodes in the
594 * Phylogeny, the ID and the sum of external nodes are NOT deleted. Field
595 * "_link" (_link to Nodes in other Phylogeny) IS deleted.
597 * @see #getLink() (Last modified: 12/20/03)
599 // final private void initializeData() {
603 // //_node_name = "";
604 // _distance_parent = PhylogenyDataUtil.BRANCH_LENGTH_DEFAULT;
605 // _collapse = false;
607 // _branch_data = null;
608 // _node_data = null;
611 * Returns whether this PhylogenyNode should be drawn as collapsed.
613 final public boolean isCollapse() {
618 * Returns true if this PhylogenyNode represents a _duplication event, false
621 final public boolean isDuplication() {
622 return getNodeData().isHasEvent() && getNodeData().getEvent().isDuplication();
626 * Checks whether this PhylogenyNode is external (tip).
628 * @return true if this PhylogenyNode is external, false otherwise
630 final public boolean isExternal() {
631 if ( _descendants == null ) {
634 return ( getNumberOfDescendants() < 1 );
640 * @return DOCUMENT ME!
642 final public boolean isFirstChildNode() {
643 if ( isRoot() /* and tree is rooted TODO */) {
644 throw new UnsupportedOperationException( "Cannot determine whether the root is the first child node of its _parent." );
646 return ( getChildNodeIndex() == 0 );
652 * @return DOCUMENT ME!
654 final public boolean isFirstExternalNode() {
655 if ( isInternal() ) {
658 PhylogenyNode node = this;
659 while ( !node.isRoot() ) {
660 if ( !node.isFirstChildNode() ) {
663 node = node.getParent();
669 * Returns whether a _duplication or speciation event has been assigned for
670 * this PhylogenyNode.
672 final public boolean isHasAssignedEvent() {
673 if ( !getNodeData().isHasEvent() ) {
676 if ( ( getNodeData().getEvent() ).isUnassigned() ) {
683 * Checks whether this PhylogenyNode is internal (tip).
685 * @return true if this PhylogenyNode is external, false otherwise
687 final public boolean isInternal() {
688 return ( !isExternal() );
692 * Returns true if this node is the last child node of its _parent.
694 * [last modified June 01, 2005 by CMZ]
696 * @return true if this node is the last child node of its _parent, false
699 final public boolean isLastChildNode() {
700 if ( isRoot() /* and tree is rooted TODO */) {
701 throw new UnsupportedOperationException( "Cannot determine whether the root is the last child node of its _parent." );
703 return ( getChildNodeIndex() == ( getParent().getNumberOfDescendants() - 1 ) );
709 * @return DOCUMENT ME!
711 final public boolean isLastExternalNode() {
712 if ( isInternal() ) {
715 PhylogenyNode node = this;
716 while ( !node.isRoot() ) {
717 if ( !node.isLastChildNode() ) {
720 node = node.getParent();
726 * Checks whether this PhylogenyNode is a root.
728 * @return true if this PhylogenyNode is the root, false otherwise
730 final public boolean isRoot() {
731 return _parent == null;
734 final public boolean isSpeciation() {
735 return getNodeData().isHasEvent() && getNodeData().getEvent().isSpeciation();
738 // ---------------------------------------------------------
740 // ---------------------------------------------------------
741 final public PhylogenyNodeIterator iterateChildNodesForward() {
742 return new ChildNodeIteratorForward( this );
745 // ---------------------------------------------------------
747 // ---------------------------------------------------------
749 * Prints to the console the subtree originating from this PhylogenyNode in
752 public void preorderPrint() {
753 System.out.println( this + "\n" );
754 if ( isInternal() ) {
755 for( int i = 0; i < getNumberOfDescendants(); ++i ) {
756 getChildNode( i ).preorderPrint();
761 final public void removeChildNode( final int i ) {
762 if ( isExternal() ) {
763 throw new UnsupportedOperationException( "cannot get the child node for a external node." );
765 if ( ( i >= getNumberOfDescendants() ) || ( i < 0 ) ) {
766 throw new IllegalArgumentException( "attempt to get child node " + i + " of a node with "
767 + getNumberOfDescendants() + " child nodes." );
769 getDescendants().remove( i );
772 final public void removeChildNode( final PhylogenyNode remove_me ) {
773 removeChildNode( remove_me.getChildNodeIndex() );
776 final public void setBranchData( final BranchData branch_data ) {
777 _branch_data = branch_data;
781 * Sets the first child PhylogenyNode of this PhylogenyNode to n.
783 final public void setChild1( final PhylogenyNode n ) {
784 setChildNode( 0, n );
788 * Sets the second child PhylogenyNode of this PhylogenyNode to n.
790 final public void setChild2( final PhylogenyNode n ) {
791 setChildNode( 1, n );
795 * Inserts PhylogenyNode n at the specified position i into the list of
796 * child nodes. This does not allow null slots in the list of child nodes:
797 * If i is larger than the number of child nodes, n is just added to the
798 * list, not place at index i.
801 * the index of position where to add the child
803 * the PhylogenyNode to add
805 final public void setChildNode( final int i, final PhylogenyNode node ) {
806 node.setParent( this );
807 if ( getNumberOfDescendants() <= i ) {
808 addChildNode( node );
811 getDescendants().set( i, node );
815 final void setChildNodeOnly( final int i, final PhylogenyNode node ) {
816 if ( getNumberOfDescendants() <= i ) {
817 addChildNode( node );
820 getDescendants().set( i, node );
825 * Sets whether this PhylogenyNode should be drawn as collapsed.
827 final public void setCollapse( final boolean b ) {
832 * Sets the length of the branch leading to the _parent of this
833 * PhylogenyNode to double d.
836 final public void setDistanceToParent( final double d ) {
837 _distance_parent = d;
841 * Sets the _indicator value of this PhylogenyNode to i.
843 final public void setIndicator( final byte i ) {
847 // --------------------------------------------------------------------
848 // Adjust methods (related to Phylogeny construction and
849 // Phylogeny modification)
850 // --------------------------------------------------------------------
852 * Sets the indicators of all the children of this PhylogenyNode to zero.
854 final void setIndicatorsToZero() {
855 for( final PreorderTreeIterator it = new PreorderTreeIterator( this ); it.hasNext(); ) {
856 it.next().setIndicator( ( byte ) 0 );
861 * Sets the linked PhylogenyNode of this PhylogenyNode to n. Currently, this
862 * method is only used for the speciation-_duplication assignment
865 final public void setLink( final PhylogenyNode n ) {
870 * Sets the name of this node.
873 final public void setName( final String node_name ) {
874 getNodeData().setNodeName( node_name );
878 * Sets the Id of this PhylogenyNode to i. In most cases, this number
879 * should not be set to values lower than getNodeCount() -- which this method
882 synchronized final protected void setId( final int i ) {
883 if ( i < getNodeCount() ) {
884 throw new IllegalArgumentException( "attempt to set node id to a value less than total node count (thus violating the uniqueness of node ids)" );
890 * Sets the _parent PhylogenyNode of this PhylogenyNode to n.
893 final public void setParent( final PhylogenyNode n ) {
898 * Sets the total number of external Nodes originating from this
899 * PhylogenyNode to i (int).
901 final public void setSumExtNodes( final int i ) {
903 throw new IllegalArgumentException( "attempt to set sum of external nodes to less than one" );
909 * Used for drawing of Trees.
911 final public void setXcoord( final float x ) {
915 final public void setXSecondary( final float x_secondary ) {
916 _x_secondary = x_secondary;
921 * Used for drawing of Trees.
923 final public void setYcoord( final float y ) {
927 final public void setYSecondary( final float y_secondary ) {
928 _y_secondary = y_secondary;
931 // ---------------------------------------------------------
932 // Writing of Nodes to Strings
933 // ---------------------------------------------------------
934 final public String toNewHampshire( final boolean simple_nh,
935 final boolean write_distance_to_parent,
936 final NH_CONVERSION_SUPPORT_VALUE_STYLE svs ) {
937 final StringBuilder sb = new StringBuilder();
939 if ( ( svs == NH_CONVERSION_SUPPORT_VALUE_STYLE.AS_INTERNAL_NODE_NAMES ) && !isExternal() ) {
940 if ( getBranchData().isHasConfidences()
941 && ( getBranchData().getConfidence( 0 ).getValue() != Confidence.CONFIDENCE_DEFAULT_VALUE ) ) {
942 data = Confidence.FORMATTER.format( ForesterUtil
943 .round( getBranchData().getConfidence( 0 ).getValue(),
944 PhyloXmlUtil.ROUNDING_DIGITS_FOR_PHYLOXML_DOUBLE_OUTPUT ) );
947 else if ( !ForesterUtil.isEmpty( getName() ) ) {
950 else if ( getNodeData().isHasTaxonomy() ) {
951 if ( !ForesterUtil.isEmpty( getNodeData().getTaxonomy().getTaxonomyCode() ) ) {
952 data = getNodeData().getTaxonomy().getTaxonomyCode();
954 else if ( !ForesterUtil.isEmpty( getNodeData().getTaxonomy().getScientificName() ) ) {
955 data = getNodeData().getTaxonomy().getScientificName();
957 else if ( !ForesterUtil.isEmpty( getNodeData().getTaxonomy().getCommonName() ) ) {
958 data = getNodeData().getTaxonomy().getCommonName();
960 else if ( getNodeData().getTaxonomy().getTaxonomyCode() != null ) {
961 data = getNodeData().getTaxonomy().getTaxonomyCode();
964 else if ( getNodeData().isHasSequence() ) {
965 if ( !ForesterUtil.isEmpty( getNodeData().getSequence().getName() ) ) {
966 data = getNodeData().getSequence().getName();
969 if ( data.length() > 0 ) {
970 data = ForesterUtil.replaceIllegalNhCharacters( data );
971 if ( simple_nh && ( data.length() > 10 ) ) {
972 data = data.substring( 0, 11 );
974 if ( ForesterUtil.isContainsParanthesesableNhCharacter( data ) ) {
983 if ( write_distance_to_parent && ( getDistanceToParent() != PhylogenyDataUtil.BRANCH_LENGTH_DEFAULT ) ) {
985 sb.append( getDistanceToParent() );
987 if ( ( svs == NH_CONVERSION_SUPPORT_VALUE_STYLE.IN_SQUARE_BRACKETS ) && !isExternal()
988 && getBranchData().isHasConfidences()
989 && ( getBranchData().getConfidence( 0 ).getValue() != Confidence.CONFIDENCE_DEFAULT_VALUE ) ) {
991 sb.append( Confidence.FORMATTER.format( ForesterUtil
992 .round( getBranchData().getConfidence( 0 ).getValue(),
993 PhyloXmlUtil.ROUNDING_DIGITS_FOR_PHYLOXML_DOUBLE_OUTPUT ) ) );
996 return sb.toString();
1000 * Swaps the the two childern of a PhylogenyNode node of this Phylogeny.
1002 public final void swapChildren() throws RuntimeException {
1003 if ( isExternal() ) {
1004 throw new RuntimeException( "attempt to swap descendants of external node" );
1006 if ( getNumberOfDescendants() != 2 ) {
1007 throw new RuntimeException( "attempt to swap descendants of node with " + getNumberOfDescendants()
1010 final PhylogenyNode a = getChildNode( 0 );
1011 final PhylogenyNode b = getChildNode( 1 );
1012 setChildNode( 0, b );
1013 setChildNode( 1, a );
1017 * Converts this PhylogenyNode to a New Hampshire X (NHX) String
1020 final public String toNewHampshireX() {
1021 final StringBuffer sb = new StringBuffer();
1022 final StringBuffer s_nhx = new StringBuffer();
1023 if ( !ForesterUtil.isEmpty( getName() ) ) {
1024 final String name = ForesterUtil.replaceIllegalNhCharacters( getName() );
1025 if ( ForesterUtil.isContainsParanthesesableNhCharacter( name ) ) {
1034 if ( getDistanceToParent() != PhylogenyDataUtil.BRANCH_LENGTH_DEFAULT ) {
1036 sb.append( getDistanceToParent() );
1038 if ( getNodeDataDirectly() != null ) {
1039 s_nhx.append( getNodeDataDirectly().toNHX() );
1041 if ( getBranchDataDirectly() != null ) {
1042 s_nhx.append( getBranchDataDirectly().toNHX() );
1044 if ( s_nhx.length() > 0 ) {
1045 sb.append( "[&&NHX" );
1049 return sb.toString();
1053 final public String toString() {
1054 final StringBuilder sb = new StringBuilder();
1055 if ( !ForesterUtil.isEmpty( getName() ) ) {
1056 sb.append( getName() );
1060 sb.append( getId() );
1062 return sb.toString();
1066 * Decreases the total number of all Nodes created so far by one.
1068 final static synchronized void decreaseNodeCount() {
1069 --PhylogenyNode._node_count;
1073 * Returns the total number of all Nodes created so far.
1075 * @return total number of Nodes (int)
1077 synchronized final public static int getNodeCount() {
1078 return PhylogenyNode._node_count;
1082 * Increases the total number of all Nodes created so far by one.
1084 synchronized final private static void increaseNodeCount() {
1085 ++PhylogenyNode._node_count;
1089 * Sets the total number of all Nodes created so far to i (int).
1091 synchronized final static void setNodeCount( final int i ) {
1092 PhylogenyNode._node_count = i;
1095 public static PhylogenyNode createInstanceFromNhxString( final String nhx ) throws NHXFormatException {
1096 return new PhylogenyNode( nhx, PhylogenyMethods.TAXONOMY_EXTRACTION.NO, false );
1099 public static PhylogenyNode createInstanceFromNhxString( final String nhx,
1100 final PhylogenyMethods.TAXONOMY_EXTRACTION taxonomy_extraction )
1101 throws NHXFormatException {
1102 return new PhylogenyNode( nhx, taxonomy_extraction, false );
1105 public static PhylogenyNode createInstanceFromNhxString( final String nhx,
1106 final PhylogenyMethods.TAXONOMY_EXTRACTION taxonomy_extraction,
1107 final boolean replace_underscores )
1108 throws NHXFormatException {
1109 return new PhylogenyNode( nhx, taxonomy_extraction, replace_underscores );
1112 private PhylogenyNode( final String nhx,
1113 final PhylogenyMethods.TAXONOMY_EXTRACTION taxonomy_extraction,
1114 final boolean replace_underscores ) throws NHXFormatException {
1116 NHXParser.parseNHX( nhx, this, taxonomy_extraction, replace_underscores );
1117 setId( PhylogenyNode.getNodeCount() );
1118 PhylogenyNode.increaseNodeCount();
1119 setSumExtNodes( 1 ); // For ext node, this number is 1 (not 0!!)