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.PreorderTreeIterator;
42 import org.forester.util.ForesterUtil;
45 * Warning. Implementation of method 'compareTo' only looks at
46 * node name. Thus, use of this class in SortedSets might lead
47 * to unexpected behavior.
50 public final class PhylogenyNode implements Comparable<PhylogenyNode> {
52 public enum NH_CONVERSION_SUPPORT_VALUE_STYLE {
53 NONE, IN_SQUARE_BRACKETS, AS_INTERNAL_NODE_NAMES;
55 private static int _node_count = 0;
56 private byte _indicator;
58 private int _sum_ext_nodes;
61 private double _distance_parent = PhylogenyDataUtil.BRANCH_LENGTH_DEFAULT;
62 private boolean _collapse;
63 private PhylogenyNode _parent;
64 private PhylogenyNode _link;
65 private ArrayList<PhylogenyNode> _descendants;
66 private NodeData _node_data;
67 private BranchData _branch_data;
68 private float _x_secondary;
69 private float _y_secondary;
72 * Default constructor for PhylogenyNode.
74 public PhylogenyNode() {
76 setId( PhylogenyNode.getNodeCount() );
77 PhylogenyNode.increaseNodeCount();
78 setSumExtNodes( 1 ); // For ext node, this number is 1 (not 0!!)
81 public void removeConnections() {
88 * Adds PhylogenyNode n to the list of child nodes and sets the _parent of n
92 * the PhylogenyNode to add
94 final public void addAsChild( final PhylogenyNode node ) {
95 final PhylogenyNode n = 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
286 final public PhylogenyNode getChildNode( final int i ) {
287 if ( isExternal() ) {
288 throw new UnsupportedOperationException( "attempt to get the child node of an external node." );
290 if ( ( i >= getNumberOfDescendants() ) || ( i < 0 ) ) {
291 throw new IllegalArgumentException( "attempt to get child node " + i + " of a node with "
292 + getNumberOfDescendants() + " child nodes" );
294 return getDescendants().get( i );
298 * Convenience method. Returns the first child PhylogenyNode of this
301 final public PhylogenyNode getChildNode1() {
302 return getChildNode( 0 );
306 * Convenience method. Returns the second child PhylogenyNode of this
309 * [last modified May 18, 2005 by CMZ]
311 final public PhylogenyNode getChildNode2() {
312 return getChildNode( 1 );
316 * This gets the child node index of this node.
319 * @return the child node index of this node
320 * @throws UnsupportedOperationException
321 * if this node is a root node
323 final public int getChildNodeIndex() {
324 return getChildNodeIndex( getParent() );
328 * This gets the child node index of this node, given that parent is its
331 * [last modified Aug 14, 2006 by CMZ]
333 * @return the child node index of this node
334 * @throws UnsupportedOperationException
335 * if this node is a root node
337 final public int getChildNodeIndex( final PhylogenyNode parent ) {
339 throw new UnsupportedOperationException( "Cannot get the child index for a root node." );
341 for( int i = 0; i < parent.getNumberOfDescendants(); ++i ) {
342 if ( parent.getChildNode( i ) == this ) {
346 throw new RuntimeException( "Unexpected exception: Could not determine the child index for node: " + this );
349 final public List<PhylogenyNode> getDescendants() {
350 if ( _descendants == null ) {
351 _descendants = new ArrayList<PhylogenyNode>();
357 * Returns the length of the branch leading to the _parent of this
358 * PhylogenyNode (double).
360 final public double getDistanceToParent() {
361 return _distance_parent;
365 * Convenience method. Returns the first child node of this node.
367 * [last modified May 18, 2005 by CMZ]
369 * @return the first child node of this node
371 public final PhylogenyNode getFirstChildNode() {
372 return getChildNode( 0 );
376 * Returns the _indicator value of this PhylogenyNode.
378 public final byte getIndicator() {
383 * Convenience method. Returns the last child node of this node.
385 * [last modified May 18, 2005 by CMZ]
387 * @return the last child node of this node
389 public final PhylogenyNode getLastChildNode() {
390 return getChildNode( getNumberOfDescendants() - 1 );
394 * Returns a refernce to the linked PhylogenyNode of this PhylogenyNode.
395 * Currently, this method is only used for the speciation-_duplication
396 * assignment algorithms.
398 public final PhylogenyNode getLink() {
403 * Returns a refernce to the next external PhylogenyNode of this
404 * PhylogenyNode. TODO should be in Phylogeny. Returns null if no next
405 * external node is available.
407 public final PhylogenyNode getNextExternalNode() {
408 if ( isInternal() ) {
409 throw new UnsupportedOperationException( "attempt to get next external node of an internal node" );
411 else if ( isLastExternalNode() ) {
414 int index = getChildNodeIndex();
415 PhylogenyNode previous_node = this;
416 PhylogenyNode current_node = getParent();
417 while ( !current_node.isRoot()
418 && ( ( current_node.getNumberOfDescendants() == 1 ) || previous_node.isLastChildNode() ) ) {
419 index = current_node.getChildNodeIndex();
420 previous_node = current_node;
421 current_node = current_node.getParent();
423 current_node = current_node.getChildNode( index + 1 );
424 while ( current_node.isInternal() ) {
425 current_node = current_node.getFirstChildNode();
430 public final PhylogenyNode getNextExternalNodeWhileTakingIntoAccountCollapsedNodes() {
432 if ( isInternal() && !isCollapse() ) {
433 throw new UnsupportedOperationException( "attempt to get next external node of an uncollapsed internal node" );
438 if ( getParent().isCollapse() ) {
439 throw new UnsupportedOperationException( "attempt to get next external node of node with a collapsed parent" );
441 // This checks if last node.
442 PhylogenyNode n = this;
444 while ( !n.isRoot() ) {
445 if ( !n.isLastChildNode() ) {
454 int index = getChildNodeIndex();
455 PhylogenyNode previous_node = this;
456 PhylogenyNode current_node = getParent();
457 while ( !current_node.isRoot()
458 && ( current_node.isCollapse() || ( current_node.getNumberOfDescendants() == 1 ) || previous_node
459 .isLastChildNode() ) ) {
460 index = current_node.getChildNodeIndex();
461 previous_node = current_node;
462 current_node = current_node.getParent();
464 if ( index < ( current_node.getNumberOfDescendants() - 1 ) ) {
465 current_node = current_node.getChildNode( index + 1 );
467 while ( current_node.isInternal() && !current_node.isCollapse() ) {
468 current_node = current_node.getFirstChildNode();
473 public final NodeData getNodeData() {
474 if ( _node_data == null ) {
475 _node_data = new NodeData();
480 final NodeData getNodeDataDirectly() {
484 // ---------------------------------------------------------
485 // Set and get methods for Nodes
486 // ---------------------------------------------------------
488 * Returns the ID (int) of this PhylogenyNode.
490 final public int getId() {
494 final public String getName() {
495 return getNodeData().getNodeName();
498 final public List<PhylogenyNode> getAllDescendants() {
502 final public int getNumberOfDescendants() {
503 if ( _descendants == null ) {
506 return _descendants.size();
510 * Returns the total number of external Nodes originating from this
511 * PhylogenyNode (int).
513 final public int getNumberOfExternalNodes() {
514 return _sum_ext_nodes;
517 final public int getNumberOfParents() {
522 * Returns a refernce to the parent PhylogenyNode of this PhylogenyNode.
524 final public PhylogenyNode getParent() {
529 * Returns a refernce to the next external PhylogenyNode of this
530 * PhylogenyNode. TODO should be in Phylogeny. Returns null if no next
531 * external node is available.
533 final public PhylogenyNode getPreviousExternalNode() {
534 if ( isInternal() ) {
535 throw new UnsupportedOperationException( "Cannot get the previous external node for an internal node." );
537 else if ( isRoot() /* TODO && tree is rooted */) {
538 throw new UnsupportedOperationException( "Cannot get the previous external node for a root node." );
540 else if ( isFirstExternalNode() ) {
541 throw new UnsupportedOperationException( "Attempt to get previous external node of the first external node." );
543 int index = getChildNodeIndex();
544 PhylogenyNode previous_node = this;
545 PhylogenyNode current_node = getParent();
546 while ( !current_node.isRoot()
547 && ( ( current_node.getNumberOfDescendants() == 1 ) || previous_node.isFirstChildNode() ) ) {
548 index = current_node.getChildNodeIndex();
549 previous_node = current_node;
550 current_node = current_node.getParent();
552 current_node = current_node.getChildNode( index - 1 );
553 while ( current_node.isInternal() ) {
554 current_node = current_node.getLastChildNode();
560 * Used for drawing of Trees.
562 final public float getXcoord() {
566 final public float getXSecondary() {
571 * Used for drawing of Trees.
573 final public float getYcoord() {
577 final public float getYSecondary() {
582 final public int hashCode() {
583 final NodeData data = getNodeData();
584 if ( ( getName().length() < 1 ) && !data.isHasSequence() && !data.isHasTaxonomy() ) {
585 return super.hashCode();
587 int result = getName().hashCode();
588 if ( data.isHasSequence() ) {
589 result ^= data.getSequence().hashCode();
591 if ( data.isHasTaxonomy() ) {
592 result ^= data.getTaxonomy().hashCode();
597 // final private void init() {
598 //_descendants = new ArrayList<PhylogenyNode>();
599 // _parent = null; //TODO not needed?
600 // _id = 0; //TODO not needed?
601 //initializeData(); //TODO not needed?
604 * Deletes data of this PhylogenyNode. Links to the other Nodes in the
605 * Phylogeny, the ID and the sum of external nodes are NOT deleted. Field
606 * "_link" (_link to Nodes in other Phylogeny) IS deleted.
608 * @see #getLink() (Last modified: 12/20/03)
610 // final private void initializeData() {
614 // //_node_name = "";
615 // _distance_parent = PhylogenyDataUtil.BRANCH_LENGTH_DEFAULT;
616 // _collapse = false;
618 // _branch_data = null;
619 // _node_data = null;
622 * Returns whether this PhylogenyNode should be drawn as collapsed.
624 final public boolean isCollapse() {
629 * Returns true if this PhylogenyNode represents a _duplication event, false
632 final public boolean isDuplication() {
633 return getNodeData().isHasEvent() && getNodeData().getEvent().isDuplication();
637 * Checks whether this PhylogenyNode is external (tip).
639 * @return true if this PhylogenyNode is external, false otherwise
641 final public boolean isExternal() {
642 if ( _descendants == null ) {
645 return ( getNumberOfDescendants() < 1 );
651 * @return DOCUMENT ME!
653 final public boolean isFirstChildNode() {
654 if ( isRoot() /* and tree is rooted TODO */) {
655 throw new UnsupportedOperationException( "Cannot determine whether the root is the first child node of its _parent." );
657 return ( getChildNodeIndex() == 0 );
663 * @return DOCUMENT ME!
665 final public boolean isFirstExternalNode() {
666 if ( isInternal() ) {
669 PhylogenyNode node = this;
670 while ( !node.isRoot() ) {
671 if ( !node.isFirstChildNode() ) {
674 node = node.getParent();
680 * Returns whether a _duplication or speciation event has been assigned for
681 * this PhylogenyNode.
683 final public boolean isHasAssignedEvent() {
684 if ( !getNodeData().isHasEvent() ) {
687 if ( ( getNodeData().getEvent() ).isUnassigned() ) {
694 * Checks whether this PhylogenyNode is internal (tip).
696 * @return true if this PhylogenyNode is external, false otherwise
698 final public boolean isInternal() {
699 return ( !isExternal() );
703 * Returns true if this node is the last child node of its _parent.
705 * [last modified June 01, 2005 by CMZ]
707 * @return true if this node is the last child node of its _parent, false
710 final public boolean isLastChildNode() {
711 if ( isRoot() /* and tree is rooted TODO */) {
712 throw new UnsupportedOperationException( "Cannot determine whether the root is the last child node of its _parent." );
714 return ( getChildNodeIndex() == ( getParent().getNumberOfDescendants() - 1 ) );
720 * @return DOCUMENT ME!
722 final public boolean isLastExternalNode() {
723 if ( isInternal() ) {
726 PhylogenyNode node = this;
727 while ( !node.isRoot() ) {
728 if ( !node.isLastChildNode() ) {
731 node = node.getParent();
736 public final int calculateDepth() {
737 PhylogenyNode n = this;
739 while ( n._parent != null ) {
746 public final double calculateDistanceToRoot() {
747 PhylogenyNode n = this;
749 while ( n._parent != null ) {
750 if ( n._distance_parent > 0.0 ) {
751 d += n._distance_parent;
759 * Checks whether this PhylogenyNode is a root.
761 * @return true if this PhylogenyNode is the root, false otherwise
763 final public boolean isRoot() {
764 return _parent == null;
767 final public boolean isSpeciation() {
768 return getNodeData().isHasEvent() && getNodeData().getEvent().isSpeciation();
771 // ---------------------------------------------------------
773 // ---------------------------------------------------------
775 * Prints to the console the subtree originating from this PhylogenyNode in
778 public void preorderPrint() {
779 System.out.println( this + "\n" );
780 if ( isInternal() ) {
781 for( int i = 0; i < getNumberOfDescendants(); ++i ) {
782 getChildNode( i ).preorderPrint();
787 final public void removeChildNode( final int i ) {
788 if ( isExternal() ) {
789 throw new UnsupportedOperationException( "cannot get the child node for a external node." );
791 if ( ( i >= getNumberOfDescendants() ) || ( i < 0 ) ) {
792 throw new IllegalArgumentException( "attempt to get child node " + i + " of a node with "
793 + getNumberOfDescendants() + " child nodes." );
795 getDescendants().remove( i );
798 final public void removeChildNode( final PhylogenyNode remove_me ) {
799 removeChildNode( remove_me.getChildNodeIndex() );
802 final public void setBranchData( final BranchData branch_data ) {
803 _branch_data = branch_data;
807 * Sets the first child PhylogenyNode of this PhylogenyNode to n.
809 final public void setChild1( final PhylogenyNode n ) {
810 setChildNode( 0, n );
814 * Sets the second child PhylogenyNode of this PhylogenyNode to n.
816 final public void setChild2( final PhylogenyNode n ) {
817 setChildNode( 1, n );
821 * Inserts PhylogenyNode n at the specified position i into the list of
822 * child nodes. This does not allow null slots in the list of child nodes:
823 * If i is larger than the number of child nodes, n is just added to the
824 * list, not place at index i.
827 * the index of position where to add the child
829 * the PhylogenyNode to add
831 final public void setChildNode( final int i, final PhylogenyNode node ) {
832 node.setParent( this );
833 if ( getNumberOfDescendants() <= i ) {
834 addChildNode( node );
837 getDescendants().set( i, node );
841 final void setChildNodeOnly( final int i, final PhylogenyNode node ) {
842 if ( getNumberOfDescendants() <= i ) {
843 addChildNode( node );
846 getDescendants().set( i, node );
851 * Sets whether this PhylogenyNode should be drawn as collapsed.
853 final public void setCollapse( final boolean b ) {
858 * Sets the length of the branch leading to the _parent of this
859 * PhylogenyNode to double d.
861 final public void setDistanceToParent( final double d ) {
862 _distance_parent = d;
866 * Sets the _indicator value of this PhylogenyNode to i.
868 final public void setIndicator( final byte i ) {
872 // --------------------------------------------------------------------
873 // Adjust methods (related to Phylogeny construction and
874 // Phylogeny modification)
875 // --------------------------------------------------------------------
877 * Sets the indicators of all the children of this PhylogenyNode to zero.
879 final void setIndicatorsToZero() {
880 for( final PreorderTreeIterator it = new PreorderTreeIterator( this ); it.hasNext(); ) {
881 it.next().setIndicator( ( byte ) 0 );
886 * Sets the linked PhylogenyNode of this PhylogenyNode to n. Currently, this
887 * method is only used for the speciation-_duplication assignment
890 final public void setLink( final PhylogenyNode n ) {
895 * Sets the name of this node.
897 final public void setName( final String node_name ) {
898 getNodeData().setNodeName( node_name );
902 * Sets the Id of this PhylogenyNode to i. In most cases, this number
903 * should not be set to values lower than getNodeCount() -- which this method
906 synchronized final protected void setId( final int i ) {
907 if ( i < getNodeCount() ) {
908 throw new IllegalArgumentException( "attempt to set node id to a value less than total node count (thus violating the uniqueness of node ids)" );
914 * Sets the _parent PhylogenyNode of this PhylogenyNode to n.
916 final public void setParent( final PhylogenyNode n ) {
921 * Sets the total number of external Nodes originating from this
922 * PhylogenyNode to i (int).
924 final public void setSumExtNodes( final int i ) {
926 throw new IllegalArgumentException( "attempt to set sum of external nodes to less than one" );
932 * Used for drawing of Trees.
934 final public void setXcoord( final float x ) {
938 final public void setXSecondary( final float x_secondary ) {
939 _x_secondary = x_secondary;
944 * Used for drawing of Trees.
946 final public void setYcoord( final float y ) {
950 final public void setYSecondary( final float y_secondary ) {
951 _y_secondary = y_secondary;
954 // ---------------------------------------------------------
955 // Writing of Nodes to Strings
956 // ---------------------------------------------------------
957 final public String toNewHampshire( final boolean simple_nh,
958 final boolean write_distance_to_parent,
959 final NH_CONVERSION_SUPPORT_VALUE_STYLE svs ) {
960 final StringBuilder sb = new StringBuilder();
962 if ( ( svs == NH_CONVERSION_SUPPORT_VALUE_STYLE.AS_INTERNAL_NODE_NAMES ) && !isExternal() ) {
963 if ( getBranchData().isHasConfidences()
964 && ( getBranchData().getConfidence( 0 ).getValue() != Confidence.CONFIDENCE_DEFAULT_VALUE ) ) {
965 data = Confidence.FORMATTER.format( ForesterUtil
966 .round( getBranchData().getConfidence( 0 ).getValue(),
967 PhyloXmlUtil.ROUNDING_DIGITS_FOR_PHYLOXML_DOUBLE_OUTPUT ) );
970 else if ( !ForesterUtil.isEmpty( getName() ) ) {
973 else if ( getNodeData().isHasTaxonomy() ) {
974 if ( !ForesterUtil.isEmpty( getNodeData().getTaxonomy().getTaxonomyCode() ) ) {
975 data = getNodeData().getTaxonomy().getTaxonomyCode();
977 else if ( !ForesterUtil.isEmpty( getNodeData().getTaxonomy().getScientificName() ) ) {
978 data = getNodeData().getTaxonomy().getScientificName();
980 else if ( !ForesterUtil.isEmpty( getNodeData().getTaxonomy().getCommonName() ) ) {
981 data = getNodeData().getTaxonomy().getCommonName();
983 else if ( getNodeData().getTaxonomy().getTaxonomyCode() != null ) {
984 data = getNodeData().getTaxonomy().getTaxonomyCode();
987 else if ( getNodeData().isHasSequence() ) {
988 if ( !ForesterUtil.isEmpty( getNodeData().getSequence().getName() ) ) {
989 data = getNodeData().getSequence().getName();
992 if ( data.length() > 0 ) {
993 data = ForesterUtil.replaceIllegalNhCharacters( data );
994 if ( simple_nh && ( data.length() > 10 ) ) {
995 data = data.substring( 0, 11 );
997 if ( ForesterUtil.isContainsParanthesesableNhCharacter( data ) ) {
1006 if ( write_distance_to_parent && ( getDistanceToParent() != PhylogenyDataUtil.BRANCH_LENGTH_DEFAULT ) ) {
1008 sb.append( getDistanceToParent() );
1010 if ( ( svs == NH_CONVERSION_SUPPORT_VALUE_STYLE.IN_SQUARE_BRACKETS ) && !isExternal()
1011 && getBranchData().isHasConfidences()
1012 && ( getBranchData().getConfidence( 0 ).getValue() != Confidence.CONFIDENCE_DEFAULT_VALUE ) ) {
1014 sb.append( Confidence.FORMATTER.format( ForesterUtil
1015 .round( getBranchData().getConfidence( 0 ).getValue(),
1016 PhyloXmlUtil.ROUNDING_DIGITS_FOR_PHYLOXML_DOUBLE_OUTPUT ) ) );
1019 return sb.toString();
1023 * Swaps the the two childern of a PhylogenyNode node of this Phylogeny.
1025 public final void swapChildren() throws RuntimeException {
1026 if ( isExternal() ) {
1027 throw new RuntimeException( "attempt to swap descendants of external node" );
1029 if ( getNumberOfDescendants() != 2 ) {
1030 throw new RuntimeException( "attempt to swap descendants of node with " + getNumberOfDescendants()
1033 final PhylogenyNode a = getChildNode( 0 );
1034 final PhylogenyNode b = getChildNode( 1 );
1035 setChildNode( 0, b );
1036 setChildNode( 1, a );
1040 * Converts this PhylogenyNode to a New Hampshire X (NHX) String
1043 final public String toNewHampshireX() {
1044 final StringBuffer sb = new StringBuffer();
1045 final StringBuffer s_nhx = new StringBuffer();
1046 if ( !ForesterUtil.isEmpty( getName() ) ) {
1047 final String name = ForesterUtil.replaceIllegalNhCharacters( getName() );
1048 if ( ForesterUtil.isContainsParanthesesableNhCharacter( name ) ) {
1057 if ( getDistanceToParent() != PhylogenyDataUtil.BRANCH_LENGTH_DEFAULT ) {
1059 sb.append( getDistanceToParent() );
1061 if ( getNodeDataDirectly() != null ) {
1062 s_nhx.append( getNodeDataDirectly().toNHX() );
1064 if ( getBranchDataDirectly() != null ) {
1065 s_nhx.append( getBranchDataDirectly().toNHX() );
1067 if ( s_nhx.length() > 0 ) {
1068 sb.append( "[&&NHX" );
1072 return sb.toString();
1076 final public String toString() {
1077 final StringBuilder sb = new StringBuilder();
1078 if ( getNodeData().isHasTaxonomy() ) {
1079 if ( !ForesterUtil.isEmpty( getNodeData().getTaxonomy().getScientificName() ) ) {
1080 sb.append( getNodeData().getTaxonomy().getScientificName() );
1083 else if ( ( sb.length() <= 1 ) && !ForesterUtil.isEmpty( getNodeData().getTaxonomy().getTaxonomyCode() ) ) {
1084 sb.append( getNodeData().getTaxonomy().getTaxonomyCode() );
1087 else if ( getNodeData().getTaxonomy().getIdentifier() != null ) {
1088 sb.append( getNodeData().getTaxonomy().getIdentifier().toString() );
1092 if ( getNodeData().isHasSequence() ) {
1093 if ( !ForesterUtil.isEmpty( getNodeData().getSequence().getName() ) ) {
1094 sb.append( getNodeData().getSequence().getName() );
1097 if ( !ForesterUtil.isEmpty( getNodeData().getSequence().getSymbol() ) ) {
1098 sb.append( getNodeData().getSequence().getSymbol() );
1101 if ( getNodeData().getSequence().getAccession() != null ) {
1102 sb.append( getNodeData().getSequence().getAccession().toString() );
1106 if ( ( sb.length() <= 1 ) && !ForesterUtil.isEmpty( getName() ) ) {
1107 sb.append( getName() );
1110 if ( sb.length() <= 1 ) {
1112 sb.append( getId() );
1115 return sb.toString().trim();
1119 * Decreases the total number of all Nodes created so far by one.
1121 final static synchronized void decreaseNodeCount() {
1122 --PhylogenyNode._node_count;
1126 * Returns the total number of all Nodes created so far.
1128 * @return total number of Nodes (int)
1130 synchronized final public static int getNodeCount() {
1131 return PhylogenyNode._node_count;
1135 * Increases the total number of all Nodes created so far by one.
1137 synchronized final private static void increaseNodeCount() {
1138 ++PhylogenyNode._node_count;
1142 * Sets the total number of all Nodes created so far to i (int).
1144 synchronized final static void setNodeCount( final int i ) {
1145 PhylogenyNode._node_count = i;
1148 public static PhylogenyNode createInstanceFromNhxString( final String nhx ) throws NHXFormatException,
1149 PhyloXmlDataFormatException {
1150 return new PhylogenyNode( nhx, NHXParser.TAXONOMY_EXTRACTION.NO, false );
1153 public static PhylogenyNode createInstanceFromNhxString( final String nhx,
1154 final NHXParser.TAXONOMY_EXTRACTION taxonomy_extraction )
1155 throws NHXFormatException, PhyloXmlDataFormatException {
1156 return new PhylogenyNode( nhx, taxonomy_extraction, false );
1159 public static PhylogenyNode createInstanceFromNhxString( final String nhx,
1160 final NHXParser.TAXONOMY_EXTRACTION taxonomy_extraction,
1161 final boolean replace_underscores )
1162 throws NHXFormatException, PhyloXmlDataFormatException {
1163 return new PhylogenyNode( nhx, taxonomy_extraction, replace_underscores );
1166 private PhylogenyNode( final String nhx,
1167 final NHXParser.TAXONOMY_EXTRACTION taxonomy_extraction,
1168 final boolean replace_underscores ) throws NHXFormatException, PhyloXmlDataFormatException {
1170 NHXParser.parseNHX( nhx, this, taxonomy_extraction, replace_underscores );
1171 setId( PhylogenyNode.getNodeCount() );
1172 PhylogenyNode.increaseNodeCount();
1173 setSumExtNodes( 1 ); // For ext node, this number is 1 (not 0!!)