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: https://sites.google.com/site/cmzmasek/home/software/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 long 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() {
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
93 final public void addAsChild( final PhylogenyNode node ) {
94 final PhylogenyNode n = node;
100 * Adds PhylogenyNode n to the list of child nodes. But does NOT set the
101 * _parent of n to this.
103 * @see addAsChild( PhylogenyNode n )
105 * the PhylogenyNode to add
107 final private void addChildNode( final PhylogenyNode child ) {
108 getDescendants().add( child );
112 // this is poor, as it only compares on names!
113 final public int compareTo( final PhylogenyNode o ) {
114 final PhylogenyNode n = o;
115 if ( ( getName() == null ) || ( n.getName() == null ) ) {
118 return getName().compareTo( n.getName() );
121 // ---------------------------------------------------------
122 // Copy and delete Nodes, copy subtress
123 // ---------------------------------------------------------
125 * Returns a new PhylogenyNode which has its data copied from this
126 * PhylogenyNode. Links to the other Nodes in the same Phylogeny are NOT
127 * copied (e.g. _link to _parent). Field "_link" IS copied.
131 final public PhylogenyNode copyNodeData() {
132 final PhylogenyNode node = new PhylogenyNode();
133 PhylogenyNode.decreaseNodeCount();
135 node._sum_ext_nodes = _sum_ext_nodes;
136 node._indicator = _indicator;
139 node._distance_parent = _distance_parent;
140 node._collapse = _collapse;
142 if ( _node_data != null ) {
143 node._node_data = ( NodeData ) _node_data.copy();
145 if ( _branch_data != null ) {
146 node._branch_data = ( BranchData ) _branch_data.copy();
152 * Returns a new PhylogenyNode which has the same data as this
153 * PhylogenyNode. Links to the other Nodes in the same Phylogeny are NOT
154 * copied (e.g. _link to _parent). Field "_link" IS copied.
158 final public PhylogenyNode copyNodeDataShallow() {
159 final PhylogenyNode node = new PhylogenyNode();
160 PhylogenyNode.decreaseNodeCount();
162 node._sum_ext_nodes = _sum_ext_nodes;
163 node._indicator = _indicator;
166 node._distance_parent = _distance_parent;
167 node._collapse = _collapse;
169 node._node_data = _node_data;
170 node._branch_data = _branch_data;
176 * Based on node name, sequence, and taxonomy.
180 final public boolean equals( final Object o ) {
184 else if ( o == null ) {
187 else if ( o.getClass() != this.getClass() ) {
188 throw new IllegalArgumentException( "attempt to check [" + this.getClass() + "] equality to " + o + " ["
189 + o.getClass() + "]" );
192 final PhylogenyNode other = ( PhylogenyNode ) o;
193 if ( !getName().equals( other.getName() ) ) {
196 final NodeData this_data = getNodeData();
197 final NodeData other_data = other.getNodeData();
198 if ( ( this_data.isHasSequence() && other_data.isHasSequence() )
199 && ( this_data.isHasTaxonomy() && other_data.isHasTaxonomy() ) ) {
200 return ( this_data.getTaxonomy().isEqual( other_data.getTaxonomy() ) && this_data.getSequence()
201 .isEqual( other_data.getSequence() ) );
203 else if ( this_data.isHasTaxonomy() && other_data.isHasTaxonomy() ) {
204 return ( this_data.getTaxonomy().isEqual( other_data.getTaxonomy() ) );
206 else if ( this_data.isHasSequence() && other_data.isHasSequence() ) {
207 return ( this_data.getSequence().isEqual( other_data.getSequence() ) );
209 else if ( getName().length() > 0 ) {
210 // Node name is not empty, and equal.
219 // ---------------------------------------------------------
220 // Obtaining of Nodes
221 // ---------------------------------------------------------
223 * Returns a List containing references to all external children of this
226 * @return List of references to external Nodes
228 final public List<PhylogenyNode> getAllExternalDescendants() {
229 final List<PhylogenyNode> nodes = new ArrayList<PhylogenyNode>();
230 if ( isExternal() ) {
234 PhylogenyNode node1 = this;
235 while ( !node1.isExternal() ) {
236 node1 = node1.getFirstChildNode();
238 PhylogenyNode node2 = this;
239 while ( !node2.isExternal() ) {
240 node2 = node2.getLastChildNode();
242 while ( node1 != node2 ) {
244 node1 = node1.getNextExternalNode();
251 * Returns a List containing references to all names of the external
252 * children of this PhylogenyNode.
254 * @return List of references to names of external Nodes
256 final public List<String> getAllExternalDescendantsNames() {
257 final List<PhylogenyNode> c = getAllExternalDescendants();
258 final List<String> n = new ArrayList<String>( c.size() );
259 for( final PhylogenyNode phylogenyNode : c ) {
260 n.add( phylogenyNode.getName() );
265 final public BranchData getBranchData() {
266 if ( _branch_data == null ) {
267 _branch_data = new BranchData();
272 final BranchData getBranchDataDirectly() {
277 * This return child node n of this node.
280 * the index of the child to get
281 * @return the child node with index n
282 * @throws IllegalArgumentException
283 * if n is out of bounds
285 final public PhylogenyNode getChildNode( final int i ) {
286 if ( isExternal() ) {
287 throw new UnsupportedOperationException( "attempt to get the child node of an external node." );
289 if ( ( i >= getNumberOfDescendants() ) || ( i < 0 ) ) {
290 throw new IllegalArgumentException( "attempt to get child node " + i + " of a node with "
291 + getNumberOfDescendants() + " child nodes" );
293 return getDescendants().get( i );
297 * Convenience method. Returns the first child PhylogenyNode of this
300 final public PhylogenyNode getChildNode1() {
301 return getChildNode( 0 );
305 * Convenience method. Returns the second child PhylogenyNode of this
308 * [last modified May 18, 2005 by CMZ]
310 final public PhylogenyNode getChildNode2() {
311 return getChildNode( 1 );
315 * This gets the child node index of this node.
318 * @return the child node index of this node
319 * @throws UnsupportedOperationException
320 * if this node is a root node
322 final public int getChildNodeIndex() {
323 return getChildNodeIndex( getParent() );
327 * This gets the child node index of this node, given that parent is its
330 * [last modified Aug 14, 2006 by CMZ]
332 * @return the child node index of this node
333 * @throws UnsupportedOperationException
334 * if this node is a root node
336 final public int getChildNodeIndex( final PhylogenyNode parent ) {
338 throw new UnsupportedOperationException( "Cannot get the child index for a root node." );
340 for( int i = 0; i < parent.getNumberOfDescendants(); ++i ) {
341 if ( parent.getChildNode( i ) == this ) {
345 throw new RuntimeException( "Unexpected exception: Could not determine the child index for node: " + this );
348 final public List<PhylogenyNode> getDescendants() {
349 if ( _descendants == null ) {
350 _descendants = new ArrayList<PhylogenyNode>();
356 * Returns the length of the branch leading to the _parent of this
357 * PhylogenyNode (double).
359 final public double getDistanceToParent() {
360 return _distance_parent;
364 * Convenience method. Returns the first child node of this node.
366 * [last modified May 18, 2005 by CMZ]
368 * @return the first child node of this node
370 public final PhylogenyNode getFirstChildNode() {
371 return getChildNode( 0 );
375 * Returns the _indicator value of this PhylogenyNode.
377 public final byte getIndicator() {
382 * Convenience method. Returns the last child node of this node.
384 * [last modified May 18, 2005 by CMZ]
386 * @return the last child node of this node
388 public final PhylogenyNode getLastChildNode() {
389 return getChildNode( getNumberOfDescendants() - 1 );
393 * Returns a refernce to the linked PhylogenyNode of this PhylogenyNode.
394 * Currently, this method is only used for the speciation-_duplication
395 * assignment algorithms.
397 public final PhylogenyNode getLink() {
402 * Returns a refernce to the next external PhylogenyNode of this
403 * PhylogenyNode. TODO should be in Phylogeny. Returns null if no next
404 * external node is available.
406 public final PhylogenyNode getNextExternalNode() {
407 if ( isInternal() ) {
408 throw new UnsupportedOperationException( "attempt to get next external node of an internal node" );
410 else if ( isLastExternalNode() ) {
413 int index = getChildNodeIndex();
414 PhylogenyNode previous_node = this;
415 PhylogenyNode current_node = getParent();
416 while ( !current_node.isRoot()
417 && ( ( current_node.getNumberOfDescendants() == 1 ) || previous_node.isLastChildNode() ) ) {
418 index = current_node.getChildNodeIndex();
419 previous_node = current_node;
420 current_node = current_node.getParent();
422 current_node = current_node.getChildNode( index + 1 );
423 while ( current_node.isInternal() ) {
424 current_node = current_node.getFirstChildNode();
429 public final PhylogenyNode getNextExternalNodeWhileTakingIntoAccountCollapsedNodes() {
431 if ( isInternal() && !isCollapse() ) {
432 throw new UnsupportedOperationException( "attempt to get next external node of an uncollapsed internal node" );
437 if ( getParent().isCollapse() ) {
438 throw new UnsupportedOperationException( "attempt to get next external node of node with a collapsed parent" );
440 // This checks if last node.
441 PhylogenyNode n = this;
443 while ( !n.isRoot() ) {
444 if ( !n.isLastChildNode() ) {
453 int index = getChildNodeIndex();
454 PhylogenyNode previous_node = this;
455 PhylogenyNode current_node = getParent();
456 while ( !current_node.isRoot()
457 && ( current_node.isCollapse() || ( current_node.getNumberOfDescendants() == 1 ) || previous_node
458 .isLastChildNode() ) ) {
459 index = current_node.getChildNodeIndex();
460 previous_node = current_node;
461 current_node = current_node.getParent();
463 if ( index < ( current_node.getNumberOfDescendants() - 1 ) ) {
464 current_node = current_node.getChildNode( index + 1 );
466 while ( current_node.isInternal() && !current_node.isCollapse() ) {
467 current_node = current_node.getFirstChildNode();
472 public final NodeData getNodeData() {
473 if ( _node_data == null ) {
474 _node_data = new NodeData();
479 final NodeData getNodeDataDirectly() {
483 // ---------------------------------------------------------
484 // Set and get methods for Nodes
485 // ---------------------------------------------------------
487 * Returns the ID (int) of this PhylogenyNode.
489 final public long getId() {
493 final public String getName() {
494 return getNodeData().getNodeName();
497 final public List<PhylogenyNode> getAllDescendants() {
501 final public int getNumberOfDescendants() {
502 if ( _descendants == null ) {
505 return _descendants.size();
509 * Returns the total number of external Nodes originating from this
510 * PhylogenyNode (int).
512 final public int getNumberOfExternalNodes() {
513 return _sum_ext_nodes;
516 final public int getNumberOfParents() {
521 * Returns a refernce to the parent PhylogenyNode of this PhylogenyNode.
523 final public PhylogenyNode getParent() {
528 * Returns a refernce to the next external PhylogenyNode of this
529 * PhylogenyNode. TODO should be in Phylogeny. Returns null if no next
530 * external node is available.
532 final public PhylogenyNode getPreviousExternalNode() {
533 if ( isInternal() ) {
534 throw new UnsupportedOperationException( "Cannot get the previous external node for an internal node." );
536 else if ( isRoot() /* TODO && tree is rooted */) {
537 throw new UnsupportedOperationException( "Cannot get the previous external node for a root node." );
539 else if ( isFirstExternalNode() ) {
540 throw new UnsupportedOperationException( "Attempt to get previous external node of the first external node." );
542 int index = getChildNodeIndex();
543 PhylogenyNode previous_node = this;
544 PhylogenyNode current_node = getParent();
545 while ( !current_node.isRoot()
546 && ( ( current_node.getNumberOfDescendants() == 1 ) || previous_node.isFirstChildNode() ) ) {
547 index = current_node.getChildNodeIndex();
548 previous_node = current_node;
549 current_node = current_node.getParent();
551 current_node = current_node.getChildNode( index - 1 );
552 while ( current_node.isInternal() ) {
553 current_node = current_node.getLastChildNode();
559 * Used for drawing of Trees.
561 final public float getXcoord() {
565 final public float getXSecondary() {
570 * Used for drawing of Trees.
572 final public float getYcoord() {
576 final public float getYSecondary() {
581 final public int hashCode() {
582 final NodeData data = getNodeData();
583 if ( ( getName().length() < 1 ) && !data.isHasSequence() && !data.isHasTaxonomy() ) {
584 return super.hashCode();
586 int result = getName().hashCode();
587 if ( data.isHasSequence() ) {
588 result ^= data.getSequence().hashCode();
590 if ( data.isHasTaxonomy() ) {
591 result ^= data.getTaxonomy().hashCode();
596 // final private void init() {
597 //_descendants = new ArrayList<PhylogenyNode>();
598 // _parent = null; //TODO not needed?
599 // _id = 0; //TODO not needed?
600 //initializeData(); //TODO not needed?
603 * Deletes data of this PhylogenyNode. Links to the other Nodes in the
604 * Phylogeny, the ID and the sum of external nodes are NOT deleted. Field
605 * "_link" (_link to Nodes in other Phylogeny) IS deleted.
607 * @see #getLink() (Last modified: 12/20/03)
609 // final private void initializeData() {
613 // //_node_name = "";
614 // _distance_parent = PhylogenyDataUtil.BRANCH_LENGTH_DEFAULT;
615 // _collapse = false;
617 // _branch_data = null;
618 // _node_data = null;
621 * Returns whether this PhylogenyNode should be drawn as collapsed.
623 final public boolean isCollapse() {
628 * Returns true if this PhylogenyNode represents a _duplication event, false
631 final public boolean isDuplication() {
632 return getNodeData().isHasEvent() && getNodeData().getEvent().isDuplication();
636 * Checks whether this PhylogenyNode is external (tip).
638 * @return true if this PhylogenyNode is external, false otherwise
640 final public boolean isExternal() {
641 if ( _descendants == null ) {
644 return ( getNumberOfDescendants() < 1 );
650 * @return DOCUMENT ME!
652 final public boolean isFirstChildNode() {
653 if ( isRoot() /* and tree is rooted TODO */) {
654 throw new UnsupportedOperationException( "Cannot determine whether the root is the first child node of its _parent." );
656 return ( getChildNodeIndex() == 0 );
662 * @return DOCUMENT ME!
664 final public boolean isFirstExternalNode() {
665 if ( isInternal() ) {
668 PhylogenyNode node = this;
669 while ( !node.isRoot() ) {
670 if ( !node.isFirstChildNode() ) {
673 node = node.getParent();
679 * Returns whether a _duplication or speciation event has been assigned for
680 * this PhylogenyNode.
682 final public boolean isHasAssignedEvent() {
683 if ( !getNodeData().isHasEvent() ) {
686 if ( ( getNodeData().getEvent() ).isUnassigned() ) {
693 * Checks whether this PhylogenyNode is internal (tip).
695 * @return true if this PhylogenyNode is external, false otherwise
697 final public boolean isInternal() {
698 return ( !isExternal() );
702 * Returns true if this node is the last child node of its _parent.
704 * [last modified June 01, 2005 by CMZ]
706 * @return true if this node is the last child node of its _parent, false
709 final public boolean isLastChildNode() {
710 if ( isRoot() /* and tree is rooted TODO */) {
711 throw new UnsupportedOperationException( "Cannot determine whether the root is the last child node of its _parent." );
713 return ( getChildNodeIndex() == ( getParent().getNumberOfDescendants() - 1 ) );
719 * @return DOCUMENT ME!
721 final public boolean isLastExternalNode() {
722 if ( isInternal() ) {
725 PhylogenyNode node = this;
726 while ( !node.isRoot() ) {
727 if ( !node.isLastChildNode() ) {
730 node = node.getParent();
735 public final int calculateDepth() {
736 PhylogenyNode n = this;
738 while ( n._parent != null ) {
745 public final double calculateDistanceToRoot() {
746 PhylogenyNode n = this;
748 while ( n._parent != null ) {
749 if ( n._distance_parent > 0.0 ) {
750 d += n._distance_parent;
758 * Checks whether this PhylogenyNode is a root.
760 * @return true if this PhylogenyNode is the root, false otherwise
762 final public boolean isRoot() {
763 return _parent == null;
766 final public boolean isSpeciation() {
767 return getNodeData().isHasEvent() && getNodeData().getEvent().isSpeciation();
770 // ---------------------------------------------------------
772 // ---------------------------------------------------------
774 * Prints to the console the subtree originating from this PhylogenyNode in
777 public void preorderPrint() {
778 System.out.println( this + "\n" );
779 if ( isInternal() ) {
780 for( int i = 0; i < getNumberOfDescendants(); ++i ) {
781 getChildNode( i ).preorderPrint();
786 final public void removeChildNode( final int i ) {
787 if ( isExternal() ) {
788 throw new UnsupportedOperationException( "cannot get the child node for a external node." );
790 if ( ( i >= getNumberOfDescendants() ) || ( i < 0 ) ) {
791 throw new IllegalArgumentException( "attempt to get child node " + i + " of a node with "
792 + getNumberOfDescendants() + " child nodes." );
794 getDescendants().remove( i );
797 final public void removeChildNode( final PhylogenyNode remove_me ) {
798 removeChildNode( remove_me.getChildNodeIndex() );
801 final public void setBranchData( final BranchData branch_data ) {
802 _branch_data = branch_data;
806 * Sets the first child PhylogenyNode of this PhylogenyNode to n.
808 final public void setChild1( final PhylogenyNode n ) {
809 setChildNode( 0, n );
813 * Sets the second child PhylogenyNode of this PhylogenyNode to n.
815 final public void setChild2( final PhylogenyNode n ) {
816 setChildNode( 1, n );
820 * Inserts PhylogenyNode n at the specified position i into the list of
821 * child nodes. This does not allow null slots in the list of child nodes:
822 * If i is larger than the number of child nodes, n is just added to the
823 * list, not place at index i.
826 * the index of position where to add the child
828 * the PhylogenyNode to add
830 final public void setChildNode( final int i, final PhylogenyNode node ) {
831 node.setParent( this );
832 if ( getNumberOfDescendants() <= i ) {
833 addChildNode( node );
836 getDescendants().set( i, node );
840 final void setChildNodeOnly( final int i, final PhylogenyNode node ) {
841 if ( getNumberOfDescendants() <= i ) {
842 addChildNode( node );
845 getDescendants().set( i, node );
850 * Sets whether this PhylogenyNode should be drawn as collapsed.
852 final public void setCollapse( final boolean b ) {
857 * Sets the length of the branch leading to the _parent of this
858 * PhylogenyNode to double d.
860 final public void setDistanceToParent( final double d ) {
861 _distance_parent = d;
865 * Sets the _indicator value of this PhylogenyNode to i.
867 final public void setIndicator( final byte i ) {
871 // --------------------------------------------------------------------
872 // Adjust methods (related to Phylogeny construction and
873 // Phylogeny modification)
874 // --------------------------------------------------------------------
876 * Sets the indicators of all the children of this PhylogenyNode to zero.
878 final void setIndicatorsToZero() {
879 for( final PreorderTreeIterator it = new PreorderTreeIterator( this ); it.hasNext(); ) {
880 it.next().setIndicator( ( byte ) 0 );
885 * Sets the linked PhylogenyNode of this PhylogenyNode to n. Currently, this
886 * method is only used for the speciation-_duplication assignment
889 final public void setLink( final PhylogenyNode n ) {
894 * Sets the name of this node.
896 final public void setName( final String node_name ) {
897 getNodeData().setNodeName( node_name );
901 * Sets the Id of this PhylogenyNode to i. In most cases, this number
902 * should not be set to values lower than getNodeCount() -- which this method
905 synchronized final protected void setId( final long i ) {
906 if ( i < getNodeCount() ) {
907 throw new IllegalArgumentException( "attempt to set node id to a value less than total node count (thus violating the uniqueness of node ids)" );
913 * Sets the _parent PhylogenyNode of this PhylogenyNode to n.
915 final public void setParent( final PhylogenyNode n ) {
920 * Sets the total number of external Nodes originating from this
921 * PhylogenyNode to i (int).
923 final public void setSumExtNodes( final int i ) {
925 throw new IllegalArgumentException( "attempt to set sum of external nodes to less than one" );
931 * Used for drawing of Trees.
933 final public void setXcoord( final float x ) {
937 final public void setXSecondary( final float x_secondary ) {
938 _x_secondary = x_secondary;
943 * Used for drawing of Trees.
945 final public void setYcoord( final float y ) {
949 final public void setYSecondary( final float y_secondary ) {
950 _y_secondary = y_secondary;
953 // ---------------------------------------------------------
954 // Writing of Nodes to Strings
955 // ---------------------------------------------------------
956 final public String toNewHampshire( final boolean simple_nh,
957 final boolean write_distance_to_parent,
958 final NH_CONVERSION_SUPPORT_VALUE_STYLE svs ) {
959 final StringBuilder sb = new StringBuilder();
961 if ( ( svs == NH_CONVERSION_SUPPORT_VALUE_STYLE.AS_INTERNAL_NODE_NAMES ) && !isExternal() ) {
962 if ( getBranchData().isHasConfidences()
963 && ( getBranchData().getConfidence( 0 ).getValue() != Confidence.CONFIDENCE_DEFAULT_VALUE ) ) {
964 data = Confidence.FORMATTER.format( ForesterUtil
965 .round( getBranchData().getConfidence( 0 ).getValue(),
966 PhyloXmlUtil.ROUNDING_DIGITS_FOR_PHYLOXML_DOUBLE_OUTPUT ) );
969 else if ( !ForesterUtil.isEmpty( getName() ) ) {
972 else if ( getNodeData().isHasTaxonomy() ) {
973 if ( !ForesterUtil.isEmpty( getNodeData().getTaxonomy().getTaxonomyCode() ) ) {
974 data = getNodeData().getTaxonomy().getTaxonomyCode();
976 else if ( !ForesterUtil.isEmpty( getNodeData().getTaxonomy().getScientificName() ) ) {
977 data = getNodeData().getTaxonomy().getScientificName();
979 else if ( !ForesterUtil.isEmpty( getNodeData().getTaxonomy().getCommonName() ) ) {
980 data = getNodeData().getTaxonomy().getCommonName();
982 else if ( getNodeData().getTaxonomy().getTaxonomyCode() != null ) {
983 data = getNodeData().getTaxonomy().getTaxonomyCode();
986 else if ( getNodeData().isHasSequence() ) {
987 if ( !ForesterUtil.isEmpty( getNodeData().getSequence().getName() ) ) {
988 data = getNodeData().getSequence().getName();
991 if ( data.length() > 0 ) {
992 data = ForesterUtil.replaceIllegalNhCharacters( data );
993 if ( simple_nh && ( data.length() > 10 ) ) {
994 data = data.substring( 0, 11 );
996 if ( ForesterUtil.isContainsParanthesesableNhCharacter( data ) ) {
1005 if ( write_distance_to_parent && ( getDistanceToParent() != PhylogenyDataUtil.BRANCH_LENGTH_DEFAULT ) ) {
1007 sb.append( getDistanceToParent() );
1009 if ( ( svs == NH_CONVERSION_SUPPORT_VALUE_STYLE.IN_SQUARE_BRACKETS ) && !isExternal()
1010 && getBranchData().isHasConfidences()
1011 && ( getBranchData().getConfidence( 0 ).getValue() != Confidence.CONFIDENCE_DEFAULT_VALUE ) ) {
1013 sb.append( Confidence.FORMATTER.format( ForesterUtil
1014 .round( getBranchData().getConfidence( 0 ).getValue(),
1015 PhyloXmlUtil.ROUNDING_DIGITS_FOR_PHYLOXML_DOUBLE_OUTPUT ) ) );
1018 return sb.toString();
1022 * Swaps the the two childern of a PhylogenyNode node of this Phylogeny.
1024 public final void swapChildren() throws RuntimeException {
1025 if ( isExternal() ) {
1026 throw new RuntimeException( "attempt to swap descendants of external node" );
1028 if ( getNumberOfDescendants() != 2 ) {
1029 throw new RuntimeException( "attempt to swap descendants of node with " + getNumberOfDescendants()
1032 final PhylogenyNode a = getChildNode( 0 );
1033 final PhylogenyNode b = getChildNode( 1 );
1034 setChildNode( 0, b );
1035 setChildNode( 1, a );
1039 * Converts this PhylogenyNode to a New Hampshire X (NHX) String
1042 final public String toNewHampshireX() {
1043 final StringBuffer sb = new StringBuffer();
1044 final StringBuffer s_nhx = new StringBuffer();
1045 if ( !ForesterUtil.isEmpty( getName() ) ) {
1046 final String name = ForesterUtil.replaceIllegalNhCharacters( getName() );
1047 if ( ForesterUtil.isContainsParanthesesableNhCharacter( name ) ) {
1056 if ( getDistanceToParent() != PhylogenyDataUtil.BRANCH_LENGTH_DEFAULT ) {
1058 sb.append( getDistanceToParent() );
1060 if ( getNodeDataDirectly() != null ) {
1061 s_nhx.append( getNodeDataDirectly().toNHX() );
1063 if ( getBranchDataDirectly() != null ) {
1064 s_nhx.append( getBranchDataDirectly().toNHX() );
1066 if ( s_nhx.length() > 0 ) {
1067 sb.append( "[&&NHX" );
1071 return sb.toString();
1075 final public String toString() {
1076 final StringBuilder sb = new StringBuilder();
1077 if ( getNodeData().isHasTaxonomy() ) {
1078 if ( !ForesterUtil.isEmpty( getNodeData().getTaxonomy().getScientificName() ) ) {
1079 sb.append( getNodeData().getTaxonomy().getScientificName() );
1082 else if ( ( sb.length() <= 1 ) && !ForesterUtil.isEmpty( getNodeData().getTaxonomy().getTaxonomyCode() ) ) {
1083 sb.append( getNodeData().getTaxonomy().getTaxonomyCode() );
1086 else if ( getNodeData().getTaxonomy().getIdentifier() != null ) {
1087 sb.append( getNodeData().getTaxonomy().getIdentifier().toString() );
1091 if ( getNodeData().isHasSequence() ) {
1092 if ( !ForesterUtil.isEmpty( getNodeData().getSequence().getName() ) ) {
1093 sb.append( getNodeData().getSequence().getName() );
1096 if ( !ForesterUtil.isEmpty( getNodeData().getSequence().getSymbol() ) ) {
1097 sb.append( getNodeData().getSequence().getSymbol() );
1100 if ( getNodeData().getSequence().getAccession() != null ) {
1101 sb.append( getNodeData().getSequence().getAccession().toString() );
1105 if ( ( sb.length() <= 1 ) && !ForesterUtil.isEmpty( getName() ) ) {
1106 sb.append( getName() );
1109 if ( sb.length() <= 1 ) {
1111 sb.append( getId() );
1114 return sb.toString().trim();
1118 * Decreases the total number of all Nodes created so far by one.
1120 final static synchronized void decreaseNodeCount() {
1125 * Returns the total number of all Nodes created so far.
1127 * @return total number of Nodes (long)
1129 synchronized final public static long getNodeCount() {
1134 * Increases the total number of all Nodes created so far by one.
1136 synchronized final private static void increaseNodeCount() {
1141 * Sets the total number of all Nodes created so far to i.
1143 synchronized final static void setNodeCount( final long i ) {
1144 PhylogenyNode.NODE_COUNT = i;
1147 public static PhylogenyNode createInstanceFromNhxString( final String nhx ) throws NHXFormatException,
1148 PhyloXmlDataFormatException {
1149 return new PhylogenyNode( nhx, NHXParser.TAXONOMY_EXTRACTION.NO, false );
1152 public static PhylogenyNode createInstanceFromNhxString( final String nhx,
1153 final NHXParser.TAXONOMY_EXTRACTION taxonomy_extraction )
1154 throws NHXFormatException, PhyloXmlDataFormatException {
1155 return new PhylogenyNode( nhx, taxonomy_extraction, false );
1158 public static PhylogenyNode createInstanceFromNhxString( final String nhx,
1159 final NHXParser.TAXONOMY_EXTRACTION taxonomy_extraction,
1160 final boolean replace_underscores )
1161 throws NHXFormatException, PhyloXmlDataFormatException {
1162 return new PhylogenyNode( nhx, taxonomy_extraction, replace_underscores );
1165 private PhylogenyNode( final String nhx,
1166 final NHXParser.TAXONOMY_EXTRACTION taxonomy_extraction,
1167 final boolean replace_underscores ) throws NHXFormatException, PhyloXmlDataFormatException {
1168 NHXParser.parseNHX( nhx, this, taxonomy_extraction, replace_underscores );
1169 setId( PhylogenyNode.getNodeCount() );
1170 PhylogenyNode.increaseNodeCount();
1171 setSumExtNodes( 1 ); // For ext node, this number is 1 (not 0!!).