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.PhyloXmlDataFormatException;
36 import org.forester.io.parsers.phyloxml.PhyloXmlUtil;
37 import org.forester.phylogeny.data.BranchData;
38 import org.forester.phylogeny.data.Confidence;
39 import org.forester.phylogeny.data.NodeData;
40 import org.forester.phylogeny.data.PhylogenyDataUtil;
41 import org.forester.phylogeny.iterators.ChildNodeIteratorForward;
42 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
43 import org.forester.phylogeny.iterators.PreorderTreeIterator;
44 import org.forester.util.ForesterUtil;
47 * Warning. Implementation of method 'compareTo' only looks at
48 * node name. Thus, use of this class in SortedSets might lead
49 * to unexpected behavior.
52 public final class PhylogenyNode implements PhylogenyNodeI, Comparable<PhylogenyNode> {
54 private static int _node_count = 0;
55 private byte _indicator;
57 private int _sum_ext_nodes;
60 private double _distance_parent = PhylogenyDataUtil.BRANCH_LENGTH_DEFAULT;
61 private boolean _collapse;
62 private PhylogenyNode _parent;
63 private PhylogenyNode _link;
64 private ArrayList<PhylogenyNode> _descendants;
65 private NodeData _node_data;
66 private BranchData _branch_data;
67 private float _x_secondary;
68 private float _y_secondary;
71 * Default constructor for PhylogenyNode.
73 public PhylogenyNode() {
75 setId( PhylogenyNode.getNodeCount() );
76 PhylogenyNode.increaseNodeCount();
77 setSumExtNodes( 1 ); // For ext node, this number is 1 (not 0!!)
80 public void removeConnections() {
87 * Adds PhylogenyNode n to the list of child nodes and sets the _parent of n
91 * the PhylogenyNode to add
94 final public void addAsChild( final PhylogenyNodeI node ) {
95 final PhylogenyNode n = ( PhylogenyNode ) node;
101 * Adds PhylogenyNode n to the list of child nodes. But does NOT set the
102 * _parent of n to this.
104 * @see addAsChild( PhylogenyNode n )
106 * the PhylogenyNode to add
108 final private void addChildNode( final PhylogenyNode child ) {
109 getDescendants().add( child );
113 // this is poor, as it only compares on names!
114 final public int compareTo( final PhylogenyNode o ) {
115 final PhylogenyNode n = o;
116 if ( ( getName() == null ) || ( n.getName() == null ) ) {
119 return getName().compareTo( n.getName() );
122 // ---------------------------------------------------------
123 // Copy and delete Nodes, copy subtress
124 // ---------------------------------------------------------
126 * Returns a new PhylogenyNode which has its data copied from this
127 * PhylogenyNode. Links to the other Nodes in the same Phylogeny are NOT
128 * copied (e.g. _link to _parent). Field "_link" IS copied.
132 final public PhylogenyNode copyNodeData() {
133 final PhylogenyNode node = new PhylogenyNode();
134 PhylogenyNode.decreaseNodeCount();
136 node._sum_ext_nodes = _sum_ext_nodes;
137 node._indicator = _indicator;
140 node._distance_parent = _distance_parent;
141 node._collapse = _collapse;
143 if ( _node_data != null ) {
144 node._node_data = ( NodeData ) _node_data.copy();
146 if ( _branch_data != null ) {
147 node._branch_data = ( BranchData ) _branch_data.copy();
153 * Returns a new PhylogenyNode which has the same data as this
154 * PhylogenyNode. Links to the other Nodes in the same Phylogeny are NOT
155 * copied (e.g. _link to _parent). Field "_link" IS copied.
159 final public PhylogenyNode copyNodeDataShallow() {
160 final PhylogenyNode node = new PhylogenyNode();
161 PhylogenyNode.decreaseNodeCount();
163 node._sum_ext_nodes = _sum_ext_nodes;
164 node._indicator = _indicator;
167 node._distance_parent = _distance_parent;
168 node._collapse = _collapse;
170 node._node_data = _node_data;
171 node._branch_data = _branch_data;
177 * Based on node name, sequence, and taxonomy.
181 final public boolean equals( final Object o ) {
185 else if ( o == null ) {
188 else if ( o.getClass() != this.getClass() ) {
189 throw new IllegalArgumentException( "attempt to check [" + this.getClass() + "] equality to " + o + " ["
190 + o.getClass() + "]" );
193 final PhylogenyNode other = ( PhylogenyNode ) o;
194 if ( !getName().equals( other.getName() ) ) {
197 final NodeData this_data = getNodeData();
198 final NodeData other_data = other.getNodeData();
199 if ( ( this_data.isHasSequence() && other_data.isHasSequence() )
200 && ( this_data.isHasTaxonomy() && other_data.isHasTaxonomy() ) ) {
201 return ( this_data.getTaxonomy().isEqual( other_data.getTaxonomy() ) && this_data.getSequence()
202 .isEqual( other_data.getSequence() ) );
204 else if ( this_data.isHasTaxonomy() && other_data.isHasTaxonomy() ) {
205 return ( this_data.getTaxonomy().isEqual( other_data.getTaxonomy() ) );
207 else if ( this_data.isHasSequence() && other_data.isHasSequence() ) {
208 return ( this_data.getSequence().isEqual( other_data.getSequence() ) );
210 else if ( getName().length() > 0 ) {
211 // Node name is not empty, and equal.
220 // ---------------------------------------------------------
221 // Obtaining of Nodes
222 // ---------------------------------------------------------
224 * Returns a List containing references to all external children of this
227 * @return List of references to external Nodes
229 final public List<PhylogenyNode> getAllExternalDescendants() {
230 final List<PhylogenyNode> nodes = new ArrayList<PhylogenyNode>();
231 if ( isExternal() ) {
235 PhylogenyNode node1 = this;
236 while ( !node1.isExternal() ) {
237 node1 = node1.getFirstChildNode();
239 PhylogenyNode node2 = this;
240 while ( !node2.isExternal() ) {
241 node2 = node2.getLastChildNode();
243 while ( node1 != node2 ) {
245 node1 = node1.getNextExternalNode();
252 * Returns a List containing references to all names of the external
253 * children of this PhylogenyNode.
255 * @return List of references to names of external Nodes
257 final public List<String> getAllExternalDescendantsNames() {
258 final List<PhylogenyNode> c = getAllExternalDescendants();
259 final List<String> n = new ArrayList<String>( c.size() );
260 for( final PhylogenyNode phylogenyNode : c ) {
261 n.add( phylogenyNode.getName() );
266 final public BranchData getBranchData() {
267 if ( _branch_data == null ) {
268 _branch_data = new BranchData();
273 final BranchData getBranchDataDirectly() {
278 * This return child node n of this node.
281 * the index of the child to get
282 * @return the child node with index n
283 * @throws IllegalArgumentException
284 * if n is out of bounds
287 final public PhylogenyNode getChildNode( final int i ) {
288 if ( isExternal() ) {
289 throw new UnsupportedOperationException( "attempt to get the child node of an external node." );
291 if ( ( i >= getNumberOfDescendants() ) || ( i < 0 ) ) {
292 throw new IllegalArgumentException( "attempt to get child node " + i + " of a node with "
293 + getNumberOfDescendants() + " child nodes" );
295 return getDescendants().get( i );
299 * Convenience method. Returns the first child PhylogenyNode of this
302 final public PhylogenyNode getChildNode1() {
303 return getChildNode( 0 );
307 * Convenience method. Returns the second child PhylogenyNode of this
310 * [last modified May 18, 2005 by CMZ]
312 final public PhylogenyNode getChildNode2() {
313 return getChildNode( 1 );
317 * This gets the child node index of this node.
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() {
325 return getChildNodeIndex( getParent() );
329 * This gets the child node index of this node, given that parent is its
332 * [last modified Aug 14, 2006 by CMZ]
334 * @return the child node index of this node
335 * @throws UnsupportedOperationException
336 * if this node is a root node
338 final public int getChildNodeIndex( final PhylogenyNode parent ) {
340 throw new UnsupportedOperationException( "Cannot get the child index for a root node." );
342 for( int i = 0; i < parent.getNumberOfDescendants(); ++i ) {
343 if ( parent.getChildNode( i ) == this ) {
347 throw new RuntimeException( "Unexpected exception: Could not determine the child index for node: " + this );
350 final public List<PhylogenyNode> getDescendants() {
351 if ( _descendants == null ) {
352 _descendants = new ArrayList<PhylogenyNode>();
358 * Returns the length of the branch leading to the _parent of this
359 * PhylogenyNode (double).
362 final public double getDistanceToParent() {
363 return _distance_parent;
367 * Convenience method. Returns the first child node of this node.
369 * [last modified May 18, 2005 by CMZ]
371 * @return the first child node of this node
373 public final PhylogenyNode getFirstChildNode() {
374 return getChildNode( 0 );
378 * Returns the _indicator value of this PhylogenyNode.
380 public final byte getIndicator() {
385 * Convenience method. Returns the last child node of this node.
387 * [last modified May 18, 2005 by CMZ]
389 * @return the last child node of this node
391 public final PhylogenyNode getLastChildNode() {
392 return getChildNode( getNumberOfDescendants() - 1 );
396 * Returns a refernce to the linked PhylogenyNode of this PhylogenyNode.
397 * Currently, this method is only used for the speciation-_duplication
398 * assignment algorithms.
400 public final PhylogenyNode getLink() {
405 * Returns a refernce to the next external PhylogenyNode of this
406 * PhylogenyNode. TODO should be in Phylogeny. Returns null if no next
407 * external node is available.
409 public final PhylogenyNode getNextExternalNode() {
410 if ( isInternal() ) {
411 throw new UnsupportedOperationException( "attempt to get next external node of an internal node" );
413 else if ( isLastExternalNode() ) {
416 int index = getChildNodeIndex();
417 PhylogenyNode previous_node = this;
418 PhylogenyNode current_node = getParent();
419 while ( !current_node.isRoot()
420 && ( ( current_node.getNumberOfDescendants() == 1 ) || previous_node.isLastChildNode() ) ) {
421 index = current_node.getChildNodeIndex();
422 previous_node = current_node;
423 current_node = current_node.getParent();
425 current_node = current_node.getChildNode( index + 1 );
426 while ( current_node.isInternal() ) {
427 current_node = current_node.getFirstChildNode();
432 public final PhylogenyNode getNextExternalNodeWhileTakingIntoAccountCollapsedNodes() {
434 if ( isInternal() && !isCollapse() ) {
435 throw new UnsupportedOperationException( "attempt to get next external node of an uncollapsed internal node" );
440 if ( getParent().isCollapse() ) {
441 throw new UnsupportedOperationException( "attempt to get next external node of node with a collapsed parent" );
443 // This checks if last node.
444 PhylogenyNode n = this;
446 while ( !n.isRoot() ) {
447 if ( !n.isLastChildNode() ) {
456 int index = getChildNodeIndex();
457 PhylogenyNode previous_node = this;
458 PhylogenyNode current_node = getParent();
459 while ( !current_node.isRoot()
460 && ( current_node.isCollapse() || ( current_node.getNumberOfDescendants() == 1 ) || previous_node
461 .isLastChildNode() ) ) {
462 index = current_node.getChildNodeIndex();
463 previous_node = current_node;
464 current_node = current_node.getParent();
466 if ( index < current_node.getNumberOfDescendants() - 1 ) {
467 current_node = current_node.getChildNode( index + 1 );
469 while ( current_node.isInternal() && !current_node.isCollapse() ) {
470 current_node = current_node.getFirstChildNode();
475 public final NodeData getNodeData() {
476 if ( _node_data == null ) {
477 _node_data = new NodeData();
482 final NodeData getNodeDataDirectly() {
486 // ---------------------------------------------------------
487 // Set and get methods for Nodes
488 // ---------------------------------------------------------
490 * Returns the ID (int) of this PhylogenyNode.
493 final public int getId() {
498 * Returns the name of this node.
501 final public String getName() {
502 return getNodeData().getNodeName();
505 final public int getNumberOfDescendants() {
506 if ( _descendants == null ) {
509 return _descendants.size();
513 * Returns the total number of external Nodes originating from this
514 * PhylogenyNode (int).
516 final public int getNumberOfExternalNodes() {
517 return _sum_ext_nodes;
520 final public int getNumberOfParents() {
525 * Returns a refernce to the parent PhylogenyNode of this PhylogenyNode.
527 final public PhylogenyNode getParent() {
532 * Returns a refernce to the next external PhylogenyNode of this
533 * PhylogenyNode. TODO should be in Phylogeny. Returns null if no next
534 * external node is available.
536 final public PhylogenyNode getPreviousExternalNode() {
537 if ( isInternal() ) {
538 throw new UnsupportedOperationException( "Cannot get the previous external node for an internal node." );
540 else if ( isRoot() /* TODO && tree is rooted */) {
541 throw new UnsupportedOperationException( "Cannot get the previous external node for a root node." );
543 else if ( isFirstExternalNode() ) {
544 throw new UnsupportedOperationException( "Attempt to get previous external node of the first external node." );
546 int index = getChildNodeIndex();
547 PhylogenyNode previous_node = this;
548 PhylogenyNode current_node = getParent();
549 while ( !current_node.isRoot()
550 && ( ( current_node.getNumberOfDescendants() == 1 ) || previous_node.isFirstChildNode() ) ) {
551 index = current_node.getChildNodeIndex();
552 previous_node = current_node;
553 current_node = current_node.getParent();
555 current_node = current_node.getChildNode( index - 1 );
556 while ( current_node.isInternal() ) {
557 current_node = current_node.getLastChildNode();
563 * Used for drawing of Trees.
565 final public float getXcoord() {
569 final public float getXSecondary() {
574 * Used for drawing of Trees.
576 final public float getYcoord() {
580 final public float getYSecondary() {
585 final public int hashCode() {
586 final NodeData data = getNodeData();
587 if ( ( getName().length() < 1 ) && !data.isHasSequence() && !data.isHasTaxonomy() ) {
588 return super.hashCode();
590 int result = getName().hashCode();
591 if ( data.isHasSequence() ) {
592 result ^= data.getSequence().hashCode();
594 if ( data.isHasTaxonomy() ) {
595 result ^= data.getTaxonomy().hashCode();
600 // final private void init() {
601 //_descendants = new ArrayList<PhylogenyNode>();
602 // _parent = null; //TODO not needed?
603 // _id = 0; //TODO not needed?
604 //initializeData(); //TODO not needed?
607 * Deletes data of this PhylogenyNode. Links to the other Nodes in the
608 * Phylogeny, the ID and the sum of external nodes are NOT deleted. Field
609 * "_link" (_link to Nodes in other Phylogeny) IS deleted.
611 * @see #getLink() (Last modified: 12/20/03)
613 // final private void initializeData() {
617 // //_node_name = "";
618 // _distance_parent = PhylogenyDataUtil.BRANCH_LENGTH_DEFAULT;
619 // _collapse = false;
621 // _branch_data = null;
622 // _node_data = null;
625 * Returns whether this PhylogenyNode should be drawn as collapsed.
627 final public boolean isCollapse() {
632 * Returns true if this PhylogenyNode represents a _duplication event, false
635 final public boolean isDuplication() {
636 return getNodeData().isHasEvent() && getNodeData().getEvent().isDuplication();
640 * Checks whether this PhylogenyNode is external (tip).
642 * @return true if this PhylogenyNode is external, false otherwise
644 final public boolean isExternal() {
645 if ( _descendants == null ) {
648 return ( getNumberOfDescendants() < 1 );
654 * @return DOCUMENT ME!
656 final public boolean isFirstChildNode() {
657 if ( isRoot() /* and tree is rooted TODO */) {
658 throw new UnsupportedOperationException( "Cannot determine whether the root is the first child node of its _parent." );
660 return ( getChildNodeIndex() == 0 );
666 * @return DOCUMENT ME!
668 final public boolean isFirstExternalNode() {
669 if ( isInternal() ) {
672 PhylogenyNode node = this;
673 while ( !node.isRoot() ) {
674 if ( !node.isFirstChildNode() ) {
677 node = node.getParent();
683 * Returns whether a _duplication or speciation event has been assigned for
684 * this PhylogenyNode.
686 final public boolean isHasAssignedEvent() {
687 if ( !getNodeData().isHasEvent() ) {
690 if ( ( getNodeData().getEvent() ).isUnassigned() ) {
697 * Checks whether this PhylogenyNode is internal (tip).
699 * @return true if this PhylogenyNode is external, false otherwise
701 final public boolean isInternal() {
702 return ( !isExternal() );
706 * Returns true if this node is the last child node of its _parent.
708 * [last modified June 01, 2005 by CMZ]
710 * @return true if this node is the last child node of its _parent, false
713 final public boolean isLastChildNode() {
714 if ( isRoot() /* and tree is rooted TODO */) {
715 throw new UnsupportedOperationException( "Cannot determine whether the root is the last child node of its _parent." );
717 return ( getChildNodeIndex() == ( getParent().getNumberOfDescendants() - 1 ) );
723 * @return DOCUMENT ME!
725 final public boolean isLastExternalNode() {
726 if ( isInternal() ) {
729 PhylogenyNode node = this;
730 while ( !node.isRoot() ) {
731 if ( !node.isLastChildNode() ) {
734 node = node.getParent();
740 * Checks whether this PhylogenyNode is a root.
742 * @return true if this PhylogenyNode is the root, false otherwise
744 final public boolean isRoot() {
745 return _parent == null;
748 final public boolean isSpeciation() {
749 return getNodeData().isHasEvent() && getNodeData().getEvent().isSpeciation();
752 // ---------------------------------------------------------
754 // ---------------------------------------------------------
755 final public PhylogenyNodeIterator iterateChildNodesForward() {
756 return new ChildNodeIteratorForward( this );
759 // ---------------------------------------------------------
761 // ---------------------------------------------------------
763 * Prints to the console the subtree originating from this PhylogenyNode in
766 public void preorderPrint() {
767 System.out.println( this + "\n" );
768 if ( isInternal() ) {
769 for( int i = 0; i < getNumberOfDescendants(); ++i ) {
770 getChildNode( i ).preorderPrint();
775 final public void removeChildNode( final int i ) {
776 if ( isExternal() ) {
777 throw new UnsupportedOperationException( "cannot get the child node for a external node." );
779 if ( ( i >= getNumberOfDescendants() ) || ( i < 0 ) ) {
780 throw new IllegalArgumentException( "attempt to get child node " + i + " of a node with "
781 + getNumberOfDescendants() + " child nodes." );
783 getDescendants().remove( i );
786 final public void removeChildNode( final PhylogenyNode remove_me ) {
787 removeChildNode( remove_me.getChildNodeIndex() );
790 final public void setBranchData( final BranchData branch_data ) {
791 _branch_data = branch_data;
795 * Sets the first child PhylogenyNode of this PhylogenyNode to n.
797 final public void setChild1( final PhylogenyNode n ) {
798 setChildNode( 0, n );
802 * Sets the second child PhylogenyNode of this PhylogenyNode to n.
804 final public void setChild2( final PhylogenyNode n ) {
805 setChildNode( 1, n );
809 * Inserts PhylogenyNode n at the specified position i into the list of
810 * child nodes. This does not allow null slots in the list of child nodes:
811 * If i is larger than the number of child nodes, n is just added to the
812 * list, not place at index i.
815 * the index of position where to add the child
817 * the PhylogenyNode to add
819 final public void setChildNode( final int i, final PhylogenyNode node ) {
820 node.setParent( this );
821 if ( getNumberOfDescendants() <= i ) {
822 addChildNode( node );
825 getDescendants().set( i, node );
829 final void setChildNodeOnly( final int i, final PhylogenyNode node ) {
830 if ( getNumberOfDescendants() <= i ) {
831 addChildNode( node );
834 getDescendants().set( i, node );
839 * Sets whether this PhylogenyNode should be drawn as collapsed.
841 final public void setCollapse( final boolean b ) {
846 * Sets the length of the branch leading to the _parent of this
847 * PhylogenyNode to double d.
850 final public void setDistanceToParent( final double d ) {
851 _distance_parent = d;
855 * Sets the _indicator value of this PhylogenyNode to i.
857 final public void setIndicator( final byte i ) {
861 // --------------------------------------------------------------------
862 // Adjust methods (related to Phylogeny construction and
863 // Phylogeny modification)
864 // --------------------------------------------------------------------
866 * Sets the indicators of all the children of this PhylogenyNode to zero.
868 final void setIndicatorsToZero() {
869 for( final PreorderTreeIterator it = new PreorderTreeIterator( this ); it.hasNext(); ) {
870 it.next().setIndicator( ( byte ) 0 );
875 * Sets the linked PhylogenyNode of this PhylogenyNode to n. Currently, this
876 * method is only used for the speciation-_duplication assignment
879 final public void setLink( final PhylogenyNode n ) {
884 * Sets the name of this node.
887 final public void setName( final String node_name ) {
888 getNodeData().setNodeName( node_name );
892 * Sets the Id of this PhylogenyNode to i. In most cases, this number
893 * should not be set to values lower than getNodeCount() -- which this method
896 synchronized final protected void setId( final int i ) {
897 if ( i < getNodeCount() ) {
898 throw new IllegalArgumentException( "attempt to set node id to a value less than total node count (thus violating the uniqueness of node ids)" );
904 * Sets the _parent PhylogenyNode of this PhylogenyNode to n.
907 final public void setParent( final PhylogenyNode n ) {
912 * Sets the total number of external Nodes originating from this
913 * PhylogenyNode to i (int).
915 final public void setSumExtNodes( final int i ) {
917 throw new IllegalArgumentException( "attempt to set sum of external nodes to less than one" );
923 * Used for drawing of Trees.
925 final public void setXcoord( final float x ) {
929 final public void setXSecondary( final float x_secondary ) {
930 _x_secondary = x_secondary;
935 * Used for drawing of Trees.
937 final public void setYcoord( final float y ) {
941 final public void setYSecondary( final float y_secondary ) {
942 _y_secondary = y_secondary;
945 // ---------------------------------------------------------
946 // Writing of Nodes to Strings
947 // ---------------------------------------------------------
948 final public String toNewHampshire( final boolean simple_nh,
949 final boolean write_distance_to_parent,
950 final NH_CONVERSION_SUPPORT_VALUE_STYLE svs ) {
951 final StringBuilder sb = new StringBuilder();
953 if ( ( svs == NH_CONVERSION_SUPPORT_VALUE_STYLE.AS_INTERNAL_NODE_NAMES ) && !isExternal() ) {
954 if ( getBranchData().isHasConfidences()
955 && ( getBranchData().getConfidence( 0 ).getValue() != Confidence.CONFIDENCE_DEFAULT_VALUE ) ) {
956 data = Confidence.FORMATTER.format( ForesterUtil
957 .round( getBranchData().getConfidence( 0 ).getValue(),
958 PhyloXmlUtil.ROUNDING_DIGITS_FOR_PHYLOXML_DOUBLE_OUTPUT ) );
961 else if ( !ForesterUtil.isEmpty( getName() ) ) {
964 else if ( getNodeData().isHasTaxonomy() ) {
965 if ( !ForesterUtil.isEmpty( getNodeData().getTaxonomy().getTaxonomyCode() ) ) {
966 data = getNodeData().getTaxonomy().getTaxonomyCode();
968 else if ( !ForesterUtil.isEmpty( getNodeData().getTaxonomy().getScientificName() ) ) {
969 data = getNodeData().getTaxonomy().getScientificName();
971 else if ( !ForesterUtil.isEmpty( getNodeData().getTaxonomy().getCommonName() ) ) {
972 data = getNodeData().getTaxonomy().getCommonName();
974 else if ( getNodeData().getTaxonomy().getTaxonomyCode() != null ) {
975 data = getNodeData().getTaxonomy().getTaxonomyCode();
978 else if ( getNodeData().isHasSequence() ) {
979 if ( !ForesterUtil.isEmpty( getNodeData().getSequence().getName() ) ) {
980 data = getNodeData().getSequence().getName();
983 if ( data.length() > 0 ) {
984 data = ForesterUtil.replaceIllegalNhCharacters( data );
985 if ( simple_nh && ( data.length() > 10 ) ) {
986 data = data.substring( 0, 11 );
988 if ( ForesterUtil.isContainsParanthesesableNhCharacter( data ) ) {
997 if ( write_distance_to_parent && ( getDistanceToParent() != PhylogenyDataUtil.BRANCH_LENGTH_DEFAULT ) ) {
999 sb.append( getDistanceToParent() );
1001 if ( ( svs == NH_CONVERSION_SUPPORT_VALUE_STYLE.IN_SQUARE_BRACKETS ) && !isExternal()
1002 && getBranchData().isHasConfidences()
1003 && ( getBranchData().getConfidence( 0 ).getValue() != Confidence.CONFIDENCE_DEFAULT_VALUE ) ) {
1005 sb.append( Confidence.FORMATTER.format( ForesterUtil
1006 .round( getBranchData().getConfidence( 0 ).getValue(),
1007 PhyloXmlUtil.ROUNDING_DIGITS_FOR_PHYLOXML_DOUBLE_OUTPUT ) ) );
1010 return sb.toString();
1014 * Swaps the the two childern of a PhylogenyNode node of this Phylogeny.
1016 public final void swapChildren() throws RuntimeException {
1017 if ( isExternal() ) {
1018 throw new RuntimeException( "attempt to swap descendants of external node" );
1020 if ( getNumberOfDescendants() != 2 ) {
1021 throw new RuntimeException( "attempt to swap descendants of node with " + getNumberOfDescendants()
1024 final PhylogenyNode a = getChildNode( 0 );
1025 final PhylogenyNode b = getChildNode( 1 );
1026 setChildNode( 0, b );
1027 setChildNode( 1, a );
1031 * Converts this PhylogenyNode to a New Hampshire X (NHX) String
1034 final public String toNewHampshireX() {
1035 final StringBuffer sb = new StringBuffer();
1036 final StringBuffer s_nhx = new StringBuffer();
1037 if ( !ForesterUtil.isEmpty( getName() ) ) {
1038 final String name = ForesterUtil.replaceIllegalNhCharacters( getName() );
1039 if ( ForesterUtil.isContainsParanthesesableNhCharacter( name ) ) {
1048 if ( getDistanceToParent() != PhylogenyDataUtil.BRANCH_LENGTH_DEFAULT ) {
1050 sb.append( getDistanceToParent() );
1052 if ( getNodeDataDirectly() != null ) {
1053 s_nhx.append( getNodeDataDirectly().toNHX() );
1055 if ( getBranchDataDirectly() != null ) {
1056 s_nhx.append( getBranchDataDirectly().toNHX() );
1058 if ( s_nhx.length() > 0 ) {
1059 sb.append( "[&&NHX" );
1063 return sb.toString();
1067 final public String toString() {
1068 final StringBuilder sb = new StringBuilder();
1069 if ( !ForesterUtil.isEmpty( getName() ) ) {
1070 sb.append( getName() );
1073 if ( getNodeData().isHasTaxonomy() ) {
1074 if ( !ForesterUtil.isEmpty( getNodeData().getTaxonomy().getScientificName() ) ) {
1075 sb.append( getNodeData().getTaxonomy().getScientificName() );
1078 else if ( !ForesterUtil.isEmpty( getNodeData().getTaxonomy().getTaxonomyCode() ) ) {
1079 sb.append( getNodeData().getTaxonomy().getTaxonomyCode() );
1082 else if ( getNodeData().getTaxonomy().getIdentifier() != null ) {
1083 sb.append( getNodeData().getTaxonomy().getIdentifier().toString() );
1087 if ( getNodeData().isHasSequence() ) {
1088 if ( !ForesterUtil.isEmpty( getNodeData().getSequence().getName() ) ) {
1089 sb.append( getNodeData().getSequence().getName() );
1092 else if ( !ForesterUtil.isEmpty( getNodeData().getSequence().getSymbol() ) ) {
1093 sb.append( getNodeData().getSequence().getSymbol() );
1096 else if ( getNodeData().getSequence().getAccession() != null ) {
1097 sb.append( getNodeData().getTaxonomy().getIdentifier().toString() );
1101 if ( sb.length() <= 1 ) {
1103 sb.append( getId() );
1106 return sb.toString().trim();
1110 * Decreases the total number of all Nodes created so far by one.
1112 final static synchronized void decreaseNodeCount() {
1113 --PhylogenyNode._node_count;
1117 * Returns the total number of all Nodes created so far.
1119 * @return total number of Nodes (int)
1121 synchronized final public static int getNodeCount() {
1122 return PhylogenyNode._node_count;
1126 * Increases the total number of all Nodes created so far by one.
1128 synchronized final private static void increaseNodeCount() {
1129 ++PhylogenyNode._node_count;
1133 * Sets the total number of all Nodes created so far to i (int).
1135 synchronized final static void setNodeCount( final int i ) {
1136 PhylogenyNode._node_count = i;
1139 public static PhylogenyNode createInstanceFromNhxString( final String nhx ) throws NHXFormatException,
1140 PhyloXmlDataFormatException {
1141 return new PhylogenyNode( nhx, PhylogenyMethods.TAXONOMY_EXTRACTION.NO, false );
1144 public static PhylogenyNode createInstanceFromNhxString( final String nhx,
1145 final PhylogenyMethods.TAXONOMY_EXTRACTION taxonomy_extraction )
1146 throws NHXFormatException, PhyloXmlDataFormatException {
1147 return new PhylogenyNode( nhx, taxonomy_extraction, false );
1150 public static PhylogenyNode createInstanceFromNhxString( final String nhx,
1151 final PhylogenyMethods.TAXONOMY_EXTRACTION taxonomy_extraction,
1152 final boolean replace_underscores )
1153 throws NHXFormatException, PhyloXmlDataFormatException {
1154 return new PhylogenyNode( nhx, taxonomy_extraction, replace_underscores );
1157 private PhylogenyNode( final String nhx,
1158 final PhylogenyMethods.TAXONOMY_EXTRACTION taxonomy_extraction,
1159 final boolean replace_underscores ) throws NHXFormatException, PhyloXmlDataFormatException {
1161 NHXParser.parseNHX( nhx, this, taxonomy_extraction, replace_underscores );
1162 setId( PhylogenyNode.getNodeCount() );
1163 PhylogenyNode.increaseNodeCount();
1164 setSumExtNodes( 1 ); // For ext node, this number is 1 (not 0!!)