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!!)
82 * Adds PhylogenyNode n to the list of child nodes and sets the _parent of n
86 * the PhylogenyNode to add
89 final public void addAsChild( final PhylogenyNodeI node ) {
90 final PhylogenyNode n = ( PhylogenyNode ) node;
96 * Adds PhylogenyNode n to the list of child nodes. But does NOT set the
97 * _parent of n to this.
99 * @see addAsChild( PhylogenyNode n )
101 * the PhylogenyNode to add
103 final private void addChildNode( final PhylogenyNode child ) {
104 getDescendants().add( child );
108 final public int compareTo( final PhylogenyNode o ) {
109 final PhylogenyNode n = o;
110 if ( ( getName() == null ) || ( n.getName() == null ) ) {
113 return getName().compareTo( n.getName() );
116 // ---------------------------------------------------------
117 // Copy and delete Nodes, copy subtress
118 // ---------------------------------------------------------
120 * Returns a new PhylogenyNode which has its data copied from this
121 * PhylogenyNode. Links to the other Nodes in the same Phylogeny are NOT
122 * copied (e.g. _link to _parent). Field "_link" IS copied.
126 final public PhylogenyNode copyNodeData() {
127 final PhylogenyNode node = new PhylogenyNode();
128 PhylogenyNode.decreaseNodeCount();
130 node._sum_ext_nodes = _sum_ext_nodes;
131 node._indicator = _indicator;
134 node._distance_parent = _distance_parent;
135 node._collapse = _collapse;
137 if ( _node_data != null ) {
138 node._node_data = ( NodeData ) _node_data.copy();
140 if ( _branch_data != null ) {
141 node._branch_data = ( BranchData ) _branch_data.copy();
147 * Returns a new PhylogenyNode which has the same data as this
148 * PhylogenyNode. Links to the other Nodes in the same Phylogeny are NOT
149 * copied (e.g. _link to _parent). Field "_link" IS copied.
153 final public PhylogenyNode copyNodeDataShallow() {
154 final PhylogenyNode node = new PhylogenyNode();
155 PhylogenyNode.decreaseNodeCount();
157 node._sum_ext_nodes = _sum_ext_nodes;
158 node._indicator = _indicator;
161 node._distance_parent = _distance_parent;
162 node._collapse = _collapse;
164 node._node_data = _node_data;
165 node._branch_data = _branch_data;
171 * Based on node name, sequence, and taxonomy.
175 final public boolean equals( final Object o ) {
179 else if ( o == null ) {
182 else if ( o.getClass() != this.getClass() ) {
183 throw new IllegalArgumentException( "attempt to check [" + this.getClass() + "] equality to " + o + " ["
184 + o.getClass() + "]" );
187 final PhylogenyNode other = ( PhylogenyNode ) o;
188 if ( !getName().equals( other.getName() ) ) {
191 final NodeData this_data = getNodeData();
192 final NodeData other_data = other.getNodeData();
193 if ( ( this_data.isHasSequence() && other_data.isHasSequence() )
194 && ( this_data.isHasTaxonomy() && other_data.isHasTaxonomy() ) ) {
195 return ( this_data.getTaxonomy().isEqual( other_data.getTaxonomy() ) && this_data.getSequence()
196 .isEqual( other_data.getSequence() ) );
198 else if ( this_data.isHasSequence() && other_data.isHasSequence() ) {
199 return ( this_data.getSequence().isEqual( other_data.getSequence() ) );
201 else if ( this_data.isHasTaxonomy() && other_data.isHasTaxonomy() ) {
202 return ( this_data.getTaxonomy().isEqual( other_data.getTaxonomy() ) );
204 else if ( getName().length() > 0 ) {
205 // Node name is not empty, and equal.
214 // ---------------------------------------------------------
215 // Obtaining of Nodes
216 // ---------------------------------------------------------
218 * Returns a List containing references to all external children of this
221 * @return List of references to external Nodes
223 final public List<PhylogenyNode> getAllExternalDescendants() {
224 final List<PhylogenyNode> nodes = new ArrayList<PhylogenyNode>();
225 if ( isExternal() ) {
229 PhylogenyNode node1 = this;
230 while ( !node1.isExternal() ) {
231 node1 = node1.getFirstChildNode();
233 PhylogenyNode node2 = this;
234 while ( !node2.isExternal() ) {
235 node2 = node2.getLastChildNode();
237 while ( node1 != node2 ) {
239 node1 = node1.getNextExternalNode();
246 * Returns a List containing references to all names of the external
247 * children of this PhylogenyNode.
249 * @return List of references to names of external Nodes
251 final public List<String> getAllExternalDescendantsNames() {
252 final List<PhylogenyNode> c = getAllExternalDescendants();
253 final List<String> n = new ArrayList<String>( c.size() );
254 for( final PhylogenyNode phylogenyNode : c ) {
255 n.add( phylogenyNode.getName() );
260 final public BranchData getBranchData() {
261 if ( _branch_data == null ) {
262 _branch_data = new BranchData();
267 final BranchData getBranchDataDirectly() {
272 * This return child node n of this node.
275 * the index of the child to get
276 * @return the child node with index n
277 * @throws IllegalArgumentException
278 * if n is out of bounds
281 final public PhylogenyNode getChildNode( final int i ) {
282 if ( isExternal() ) {
283 throw new UnsupportedOperationException( "attempt to get the child node of an external node." );
285 if ( ( i >= getNumberOfDescendants() ) || ( i < 0 ) ) {
286 throw new IllegalArgumentException( "attempt to get child node " + i + " of a node with "
287 + getNumberOfDescendants() + " child nodes" );
289 return getDescendants().get( i );
293 * Convenience method. Returns the first child PhylogenyNode of this
296 final public PhylogenyNode getChildNode1() {
297 return getChildNode( 0 );
301 * Convenience method. Returns the second child PhylogenyNode of this
304 * [last modified May 18, 2005 by CMZ]
306 final public PhylogenyNode getChildNode2() {
307 return getChildNode( 1 );
311 * This gets the child node index of this node.
314 * @return the child node index of this node
315 * @throws UnsupportedOperationException
316 * if this node is a root node
318 final public int getChildNodeIndex() {
319 return getChildNodeIndex( getParent() );
323 * This gets the child node index of this node, given that parent is its
326 * [last modified Aug 14, 2006 by CMZ]
328 * @return the child node index of this node
329 * @throws UnsupportedOperationException
330 * if this node is a root node
332 final public int getChildNodeIndex( final PhylogenyNode parent ) {
334 throw new UnsupportedOperationException( "Cannot get the child index for a root node." );
336 for( int i = 0; i < parent.getNumberOfDescendants(); ++i ) {
337 if ( parent.getChildNode( i ) == this ) {
341 throw new RuntimeException( "Unexpected exception: Could not determine the child index for node: " + this );
344 final public List<PhylogenyNode> getDescendants() {
345 if ( _descendants == null ) {
346 _descendants = new ArrayList<PhylogenyNode>();
352 * Returns the length of the branch leading to the _parent of this
353 * PhylogenyNode (double).
356 final public double getDistanceToParent() {
357 return _distance_parent;
361 * Convenience method. Returns the first child node of this node.
363 * [last modified May 18, 2005 by CMZ]
365 * @return the first child node of this node
367 public final PhylogenyNode getFirstChildNode() {
368 return getChildNode( 0 );
372 * Returns the _indicator value of this PhylogenyNode.
374 public final byte getIndicator() {
379 * Convenience method. Returns the last child node of this node.
381 * [last modified May 18, 2005 by CMZ]
383 * @return the last child node of this node
385 public final PhylogenyNode getLastChildNode() {
386 return getChildNode( getNumberOfDescendants() - 1 );
390 * Returns a refernce to the linked PhylogenyNode of this PhylogenyNode.
391 * Currently, this method is only used for the speciation-_duplication
392 * assignment algorithms.
394 public final PhylogenyNode getLink() {
399 * Returns a refernce to the next external PhylogenyNode of this
400 * PhylogenyNode. TODO should be in Phylogeny. Returns null if no next
401 * external node is available.
403 public final PhylogenyNode getNextExternalNode() {
404 if ( isInternal() ) {
405 throw new UnsupportedOperationException( "attempt to get next external node of an internal node" );
407 else if ( isLastExternalNode() ) {
410 int index = getChildNodeIndex();
411 PhylogenyNode previous_node = this;
412 PhylogenyNode current_node = getParent();
413 while ( !current_node.isRoot()
414 && ( ( current_node.getNumberOfDescendants() == 1 ) || previous_node.isLastChildNode() ) ) {
415 index = current_node.getChildNodeIndex();
416 previous_node = current_node;
417 current_node = current_node.getParent();
419 current_node = current_node.getChildNode( index + 1 );
420 while ( current_node.isInternal() ) {
421 current_node = current_node.getFirstChildNode();
426 public final PhylogenyNode getNextExternalNodeWhileTakingIntoAccountCollapsedNodes() {
428 if ( isInternal() && !isCollapse() ) {
429 throw new UnsupportedOperationException( "attempt to get next external node of an uncollapsed internal node" );
434 if ( getParent().isCollapse() ) {
435 throw new UnsupportedOperationException( "attempt to get next external node of node with a collapsed parent" );
437 // This checks if last node.
438 PhylogenyNode n = this;
440 while ( !n.isRoot() ) {
441 if ( !n.isLastChildNode() ) {
450 int index = getChildNodeIndex();
451 PhylogenyNode previous_node = this;
452 PhylogenyNode current_node = getParent();
453 while ( !current_node.isRoot()
454 && ( current_node.isCollapse() || ( current_node.getNumberOfDescendants() == 1 ) || previous_node
455 .isLastChildNode() ) ) {
456 index = current_node.getChildNodeIndex();
457 previous_node = current_node;
458 current_node = current_node.getParent();
460 if ( index < current_node.getNumberOfDescendants() - 1 ) {
461 current_node = current_node.getChildNode( index + 1 );
463 while ( current_node.isInternal() && !current_node.isCollapse() ) {
464 current_node = current_node.getFirstChildNode();
469 public final NodeData getNodeData() {
470 if ( _node_data == null ) {
471 _node_data = new NodeData();
476 final NodeData getNodeDataDirectly() {
480 // ---------------------------------------------------------
481 // Set and get methods for Nodes
482 // ---------------------------------------------------------
484 * Returns the ID (int) of this PhylogenyNode.
487 final public int getId() {
492 * Returns the name of this node.
495 final public String getName() {
496 return getNodeData().getNodeName();
499 final public int getNumberOfDescendants() {
500 if ( _descendants == null ) {
503 return _descendants.size();
507 * Returns the total number of external Nodes originating from this
508 * PhylogenyNode (int).
510 final public int getNumberOfExternalNodes() {
511 return _sum_ext_nodes;
514 final public int getNumberOfParents() {
519 * Returns a refernce to the parent PhylogenyNode of this PhylogenyNode.
521 final public PhylogenyNode getParent() {
526 * Returns a refernce to the next external PhylogenyNode of this
527 * PhylogenyNode. TODO should be in Phylogeny. Returns null if no next
528 * external node is available.
530 final public PhylogenyNode getPreviousExternalNode() {
531 if ( isInternal() ) {
532 throw new UnsupportedOperationException( "Cannot get the previous external node for an internal node." );
534 else if ( isRoot() /* TODO && tree is rooted */) {
535 throw new UnsupportedOperationException( "Cannot get the previous external node for a root node." );
537 else if ( isFirstExternalNode() ) {
538 throw new UnsupportedOperationException( "Attempt to get previous external node of the first external node." );
540 int index = getChildNodeIndex();
541 PhylogenyNode previous_node = this;
542 PhylogenyNode current_node = getParent();
543 while ( !current_node.isRoot()
544 && ( ( current_node.getNumberOfDescendants() == 1 ) || previous_node.isFirstChildNode() ) ) {
545 index = current_node.getChildNodeIndex();
546 previous_node = current_node;
547 current_node = current_node.getParent();
549 current_node = current_node.getChildNode( index - 1 );
550 while ( current_node.isInternal() ) {
551 current_node = current_node.getLastChildNode();
557 * Used for drawing of Trees.
559 final public float getXcoord() {
563 final public float getXSecondary() {
568 * Used for drawing of Trees.
570 final public float getYcoord() {
574 final public float getYSecondary() {
579 final public int hashCode() {
580 final NodeData data = getNodeData();
581 if ( ( getName().length() < 1 ) && !data.isHasSequence() && !data.isHasTaxonomy() ) {
582 return super.hashCode();
584 int result = getName().hashCode();
585 if ( data.isHasSequence() ) {
586 result ^= data.getSequence().hashCode();
588 if ( data.isHasTaxonomy() ) {
589 result ^= data.getTaxonomy().hashCode();
594 // final private void init() {
595 //_descendants = new ArrayList<PhylogenyNode>();
596 // _parent = null; //TODO not needed?
597 // _id = 0; //TODO not needed?
598 //initializeData(); //TODO not needed?
601 * Deletes data of this PhylogenyNode. Links to the other Nodes in the
602 * Phylogeny, the ID and the sum of external nodes are NOT deleted. Field
603 * "_link" (_link to Nodes in other Phylogeny) IS deleted.
605 * @see #getLink() (Last modified: 12/20/03)
607 // final private void initializeData() {
611 // //_node_name = "";
612 // _distance_parent = PhylogenyDataUtil.BRANCH_LENGTH_DEFAULT;
613 // _collapse = false;
615 // _branch_data = null;
616 // _node_data = null;
619 * Returns whether this PhylogenyNode should be drawn as collapsed.
621 final public boolean isCollapse() {
626 * Returns true if this PhylogenyNode represents a _duplication event, false
629 final public boolean isDuplication() {
630 return getNodeData().isHasEvent() && getNodeData().getEvent().isDuplication();
634 * Checks whether this PhylogenyNode is external (tip).
636 * @return true if this PhylogenyNode is external, false otherwise
638 final public boolean isExternal() {
639 if ( _descendants == null ) {
642 return ( getNumberOfDescendants() < 1 );
648 * @return DOCUMENT ME!
650 final public boolean isFirstChildNode() {
651 if ( isRoot() /* and tree is rooted TODO */) {
652 throw new UnsupportedOperationException( "Cannot determine whether the root is the first child node of its _parent." );
654 return ( getChildNodeIndex() == 0 );
660 * @return DOCUMENT ME!
662 final public boolean isFirstExternalNode() {
663 if ( isInternal() ) {
666 PhylogenyNode node = this;
667 while ( !node.isRoot() ) {
668 if ( !node.isFirstChildNode() ) {
671 node = node.getParent();
677 * Returns whether a _duplication or speciation event has been assigned for
678 * this PhylogenyNode.
680 final public boolean isHasAssignedEvent() {
681 if ( !getNodeData().isHasEvent() ) {
684 if ( ( getNodeData().getEvent() ).isUnassigned() ) {
691 * Checks whether this PhylogenyNode is internal (tip).
693 * @return true if this PhylogenyNode is external, false otherwise
695 final public boolean isInternal() {
696 return ( !isExternal() );
700 * Returns true if this node is the last child node of its _parent.
702 * [last modified June 01, 2005 by CMZ]
704 * @return true if this node is the last child node of its _parent, false
707 final public boolean isLastChildNode() {
708 if ( isRoot() /* and tree is rooted TODO */) {
709 throw new UnsupportedOperationException( "Cannot determine whether the root is the last child node of its _parent." );
711 return ( getChildNodeIndex() == ( getParent().getNumberOfDescendants() - 1 ) );
717 * @return DOCUMENT ME!
719 final public boolean isLastExternalNode() {
720 if ( isInternal() ) {
723 PhylogenyNode node = this;
724 while ( !node.isRoot() ) {
725 if ( !node.isLastChildNode() ) {
728 node = node.getParent();
734 * Checks whether this PhylogenyNode is a root.
736 * @return true if this PhylogenyNode is the root, false otherwise
738 final public boolean isRoot() {
739 return _parent == null;
742 final public boolean isSpeciation() {
743 return getNodeData().isHasEvent() && getNodeData().getEvent().isSpeciation();
746 // ---------------------------------------------------------
748 // ---------------------------------------------------------
749 final public PhylogenyNodeIterator iterateChildNodesForward() {
750 return new ChildNodeIteratorForward( this );
753 // ---------------------------------------------------------
755 // ---------------------------------------------------------
757 * Prints to the console the subtree originating from this PhylogenyNode in
760 public void preorderPrint() {
761 System.out.println( this + "\n" );
762 if ( isInternal() ) {
763 for( int i = 0; i < getNumberOfDescendants(); ++i ) {
764 getChildNode( i ).preorderPrint();
769 final public void removeChildNode( final int i ) {
770 if ( isExternal() ) {
771 throw new UnsupportedOperationException( "cannot get the child node for a external node." );
773 if ( ( i >= getNumberOfDescendants() ) || ( i < 0 ) ) {
774 throw new IllegalArgumentException( "attempt to get child node " + i + " of a node with "
775 + getNumberOfDescendants() + " child nodes." );
777 getDescendants().remove( i );
780 final public void removeChildNode( final PhylogenyNode remove_me ) {
781 removeChildNode( remove_me.getChildNodeIndex() );
784 final public void setBranchData( final BranchData branch_data ) {
785 _branch_data = branch_data;
789 * Sets the first child PhylogenyNode of this PhylogenyNode to n.
791 final public void setChild1( final PhylogenyNode n ) {
792 setChildNode( 0, n );
796 * Sets the second child PhylogenyNode of this PhylogenyNode to n.
798 final public void setChild2( final PhylogenyNode n ) {
799 setChildNode( 1, n );
803 * Inserts PhylogenyNode n at the specified position i into the list of
804 * child nodes. This does not allow null slots in the list of child nodes:
805 * If i is larger than the number of child nodes, n is just added to the
806 * list, not place at index i.
809 * the index of position where to add the child
811 * the PhylogenyNode to add
813 final public void setChildNode( final int i, final PhylogenyNode node ) {
814 node.setParent( this );
815 if ( getNumberOfDescendants() <= i ) {
816 addChildNode( node );
819 getDescendants().set( i, node );
823 final void setChildNodeOnly( final int i, final PhylogenyNode node ) {
824 if ( getNumberOfDescendants() <= i ) {
825 addChildNode( node );
828 getDescendants().set( i, node );
833 * Sets whether this PhylogenyNode should be drawn as collapsed.
835 final public void setCollapse( final boolean b ) {
840 * Sets the length of the branch leading to the _parent of this
841 * PhylogenyNode to double d.
844 final public void setDistanceToParent( final double d ) {
845 _distance_parent = d;
849 * Sets the _indicator value of this PhylogenyNode to i.
851 final public void setIndicator( final byte i ) {
855 // --------------------------------------------------------------------
856 // Adjust methods (related to Phylogeny construction and
857 // Phylogeny modification)
858 // --------------------------------------------------------------------
860 * Sets the indicators of all the children of this PhylogenyNode to zero.
862 final void setIndicatorsToZero() {
863 for( final PreorderTreeIterator it = new PreorderTreeIterator( this ); it.hasNext(); ) {
864 it.next().setIndicator( ( byte ) 0 );
869 * Sets the linked PhylogenyNode of this PhylogenyNode to n. Currently, this
870 * method is only used for the speciation-_duplication assignment
873 final public void setLink( final PhylogenyNode n ) {
878 * Sets the name of this node.
881 final public void setName( final String node_name ) {
882 getNodeData().setNodeName( node_name );
886 * Sets the Id of this PhylogenyNode to i. In most cases, this number
887 * should not be set to values lower than getNodeCount() -- which this method
890 synchronized final protected void setId( final int i ) {
891 if ( i < getNodeCount() ) {
892 throw new IllegalArgumentException( "attempt to set node id to a value less than total node count (thus violating the uniqueness of node ids)" );
898 * Sets the _parent PhylogenyNode of this PhylogenyNode to n.
901 final public void setParent( final PhylogenyNode n ) {
906 * Sets the total number of external Nodes originating from this
907 * PhylogenyNode to i (int).
909 final public void setSumExtNodes( final int i ) {
911 throw new IllegalArgumentException( "attempt to set sum of external nodes to less than one" );
917 * Used for drawing of Trees.
919 final public void setXcoord( final float x ) {
923 final public void setXSecondary( final float x_secondary ) {
924 _x_secondary = x_secondary;
929 * Used for drawing of Trees.
931 final public void setYcoord( final float y ) {
935 final public void setYSecondary( final float y_secondary ) {
936 _y_secondary = y_secondary;
939 // ---------------------------------------------------------
940 // Writing of Nodes to Strings
941 // ---------------------------------------------------------
942 final public String toNewHampshire( final boolean simple_nh,
943 final boolean write_distance_to_parent,
944 final NH_CONVERSION_SUPPORT_VALUE_STYLE svs ) {
945 final StringBuilder sb = new StringBuilder();
947 if ( ( svs == NH_CONVERSION_SUPPORT_VALUE_STYLE.AS_INTERNAL_NODE_NAMES ) && !isExternal() ) {
948 if ( getBranchData().isHasConfidences()
949 && ( getBranchData().getConfidence( 0 ).getValue() != Confidence.CONFIDENCE_DEFAULT_VALUE ) ) {
950 data = Confidence.FORMATTER.format( ForesterUtil
951 .round( getBranchData().getConfidence( 0 ).getValue(),
952 PhyloXmlUtil.ROUNDING_DIGITS_FOR_PHYLOXML_DOUBLE_OUTPUT ) );
955 else if ( !ForesterUtil.isEmpty( getName() ) ) {
958 else if ( getNodeData().isHasTaxonomy() ) {
959 if ( !ForesterUtil.isEmpty( getNodeData().getTaxonomy().getTaxonomyCode() ) ) {
960 data = getNodeData().getTaxonomy().getTaxonomyCode();
962 else if ( !ForesterUtil.isEmpty( getNodeData().getTaxonomy().getScientificName() ) ) {
963 data = getNodeData().getTaxonomy().getScientificName();
965 else if ( !ForesterUtil.isEmpty( getNodeData().getTaxonomy().getCommonName() ) ) {
966 data = getNodeData().getTaxonomy().getCommonName();
968 else if ( getNodeData().getTaxonomy().getTaxonomyCode() != null ) {
969 data = getNodeData().getTaxonomy().getTaxonomyCode();
972 else if ( getNodeData().isHasSequence() ) {
973 if ( !ForesterUtil.isEmpty( getNodeData().getSequence().getName() ) ) {
974 data = getNodeData().getSequence().getName();
977 if ( data.length() > 0 ) {
978 data = ForesterUtil.replaceIllegalNhCharacters( data );
979 if ( simple_nh && ( data.length() > 10 ) ) {
980 data = data.substring( 0, 11 );
982 if ( ForesterUtil.isContainsParanthesesableNhCharacter( data ) ) {
991 if ( write_distance_to_parent && ( getDistanceToParent() != PhylogenyDataUtil.BRANCH_LENGTH_DEFAULT ) ) {
993 sb.append( getDistanceToParent() );
995 if ( ( svs == NH_CONVERSION_SUPPORT_VALUE_STYLE.IN_SQUARE_BRACKETS ) && !isExternal()
996 && getBranchData().isHasConfidences()
997 && ( getBranchData().getConfidence( 0 ).getValue() != Confidence.CONFIDENCE_DEFAULT_VALUE ) ) {
999 sb.append( Confidence.FORMATTER.format( ForesterUtil
1000 .round( getBranchData().getConfidence( 0 ).getValue(),
1001 PhyloXmlUtil.ROUNDING_DIGITS_FOR_PHYLOXML_DOUBLE_OUTPUT ) ) );
1004 return sb.toString();
1008 * Swaps the the two childern of a PhylogenyNode node of this Phylogeny.
1010 public final void swapChildren() throws RuntimeException {
1011 if ( isExternal() ) {
1012 throw new RuntimeException( "attempt to swap descendants of external node" );
1014 if ( getNumberOfDescendants() != 2 ) {
1015 throw new RuntimeException( "attempt to swap descendants of node with " + getNumberOfDescendants()
1018 final PhylogenyNode a = getChildNode( 0 );
1019 final PhylogenyNode b = getChildNode( 1 );
1020 setChildNode( 0, b );
1021 setChildNode( 1, a );
1025 * Converts this PhylogenyNode to a New Hampshire X (NHX) String
1028 final public String toNewHampshireX() {
1029 final StringBuffer sb = new StringBuffer();
1030 final StringBuffer s_nhx = new StringBuffer();
1031 if ( !ForesterUtil.isEmpty( getName() ) ) {
1032 final String name = ForesterUtil.replaceIllegalNhCharacters( getName() );
1033 if ( ForesterUtil.isContainsParanthesesableNhCharacter( name ) ) {
1042 if ( getDistanceToParent() != PhylogenyDataUtil.BRANCH_LENGTH_DEFAULT ) {
1044 sb.append( getDistanceToParent() );
1046 if ( getNodeDataDirectly() != null ) {
1047 s_nhx.append( getNodeDataDirectly().toNHX() );
1049 if ( getBranchDataDirectly() != null ) {
1050 s_nhx.append( getBranchDataDirectly().toNHX() );
1052 if ( s_nhx.length() > 0 ) {
1053 sb.append( "[&&NHX" );
1057 return sb.toString();
1061 final public String toString() {
1062 StringBuilder sb = new StringBuilder();
1063 if ( ForesterUtil.isEmpty( getName() ) ) {
1064 sb.append( getName() );
1067 if ( getNodeData().isHasTaxonomy() ) {
1068 if ( !ForesterUtil.isEmpty( getNodeData().getTaxonomy().getScientificName() ) ) {
1069 sb.append( getNodeData().getTaxonomy().getScientificName() );
1072 else if ( !ForesterUtil.isEmpty( getNodeData().getTaxonomy().getTaxonomyCode() ) ) {
1073 sb.append( getNodeData().getTaxonomy().getTaxonomyCode() );
1076 else if ( getNodeData().getTaxonomy().getIdentifier() != null ) {
1077 sb.append( getNodeData().getTaxonomy().getIdentifier().toString() );
1081 if ( getNodeData().isHasSequence() ) {
1082 if ( !ForesterUtil.isEmpty( getNodeData().getSequence().getName() ) ) {
1083 sb.append( getNodeData().getSequence().getName() );
1086 else if ( !ForesterUtil.isEmpty( getNodeData().getSequence().getSymbol() ) ) {
1087 sb.append( getNodeData().getSequence().getSymbol() );
1090 else if ( getNodeData().getSequence().getAccession() != null ) {
1091 sb.append( getNodeData().getTaxonomy().getIdentifier().toString() );
1095 if ( sb.length() <= 1 ) {
1097 sb.append( getId() );
1100 return sb.toString().trim();
1104 * Decreases the total number of all Nodes created so far by one.
1106 final static synchronized void decreaseNodeCount() {
1107 --PhylogenyNode._node_count;
1111 * Returns the total number of all Nodes created so far.
1113 * @return total number of Nodes (int)
1115 synchronized final public static int getNodeCount() {
1116 return PhylogenyNode._node_count;
1120 * Increases the total number of all Nodes created so far by one.
1122 synchronized final private static void increaseNodeCount() {
1123 ++PhylogenyNode._node_count;
1127 * Sets the total number of all Nodes created so far to i (int).
1129 synchronized final static void setNodeCount( final int i ) {
1130 PhylogenyNode._node_count = i;
1133 public static PhylogenyNode createInstanceFromNhxString( final String nhx ) throws NHXFormatException {
1134 return new PhylogenyNode( nhx, PhylogenyMethods.TAXONOMY_EXTRACTION.NO, false );
1137 public static PhylogenyNode createInstanceFromNhxString( final String nhx,
1138 final PhylogenyMethods.TAXONOMY_EXTRACTION taxonomy_extraction )
1139 throws NHXFormatException {
1140 return new PhylogenyNode( nhx, taxonomy_extraction, false );
1143 public static PhylogenyNode createInstanceFromNhxString( final String nhx,
1144 final PhylogenyMethods.TAXONOMY_EXTRACTION taxonomy_extraction,
1145 final boolean replace_underscores )
1146 throws NHXFormatException {
1147 return new PhylogenyNode( nhx, taxonomy_extraction, replace_underscores );
1150 private PhylogenyNode( final String nhx,
1151 final PhylogenyMethods.TAXONOMY_EXTRACTION taxonomy_extraction,
1152 final boolean replace_underscores ) throws NHXFormatException {
1154 NHXParser.parseNHX( nhx, this, taxonomy_extraction, replace_underscores );
1155 setId( PhylogenyNode.getNodeCount() );
1156 PhylogenyNode.increaseNodeCount();
1157 setSumExtNodes( 1 ); // For ext node, this number is 1 (not 0!!)