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 PhylogenyNodeI, Comparable<PhylogenyNode> {
52 private static int _node_count = 0;
53 private byte _indicator;
55 private int _sum_ext_nodes;
58 private double _distance_parent = PhylogenyDataUtil.BRANCH_LENGTH_DEFAULT;
59 private boolean _collapse;
60 private PhylogenyNode _parent;
61 private PhylogenyNode _link;
62 private ArrayList<PhylogenyNode> _descendants;
63 private NodeData _node_data;
64 private BranchData _branch_data;
65 private float _x_secondary;
66 private float _y_secondary;
69 * Default constructor for PhylogenyNode.
71 public PhylogenyNode() {
73 setId( PhylogenyNode.getNodeCount() );
74 PhylogenyNode.increaseNodeCount();
75 setSumExtNodes( 1 ); // For ext node, this number is 1 (not 0!!)
78 public void removeConnections() {
85 * Adds PhylogenyNode n to the list of child nodes and sets the _parent of n
89 * the PhylogenyNode to add
92 final public void addAsChild( final PhylogenyNodeI node ) {
93 final PhylogenyNode n = ( PhylogenyNode ) node;
99 * Adds PhylogenyNode n to the list of child nodes. But does NOT set the
100 * _parent of n to this.
102 * @see addAsChild( PhylogenyNode n )
104 * the PhylogenyNode to add
106 final private void addChildNode( final PhylogenyNode child ) {
107 getDescendants().add( child );
111 // this is poor, as it only compares on names!
112 final public int compareTo( final PhylogenyNode o ) {
113 final PhylogenyNode n = o;
114 if ( ( getName() == null ) || ( n.getName() == null ) ) {
117 return getName().compareTo( n.getName() );
120 // ---------------------------------------------------------
121 // Copy and delete Nodes, copy subtress
122 // ---------------------------------------------------------
124 * Returns a new PhylogenyNode which has its data copied from this
125 * PhylogenyNode. Links to the other Nodes in the same Phylogeny are NOT
126 * copied (e.g. _link to _parent). Field "_link" IS copied.
130 final public PhylogenyNode copyNodeData() {
131 final PhylogenyNode node = new PhylogenyNode();
132 PhylogenyNode.decreaseNodeCount();
134 node._sum_ext_nodes = _sum_ext_nodes;
135 node._indicator = _indicator;
138 node._distance_parent = _distance_parent;
139 node._collapse = _collapse;
141 if ( _node_data != null ) {
142 node._node_data = ( NodeData ) _node_data.copy();
144 if ( _branch_data != null ) {
145 node._branch_data = ( BranchData ) _branch_data.copy();
151 * Returns a new PhylogenyNode which has the same data as this
152 * PhylogenyNode. Links to the other Nodes in the same Phylogeny are NOT
153 * copied (e.g. _link to _parent). Field "_link" IS copied.
157 final public PhylogenyNode copyNodeDataShallow() {
158 final PhylogenyNode node = new PhylogenyNode();
159 PhylogenyNode.decreaseNodeCount();
161 node._sum_ext_nodes = _sum_ext_nodes;
162 node._indicator = _indicator;
165 node._distance_parent = _distance_parent;
166 node._collapse = _collapse;
168 node._node_data = _node_data;
169 node._branch_data = _branch_data;
175 * Based on node name, sequence, and taxonomy.
179 final public boolean equals( final Object o ) {
183 else if ( o == null ) {
186 else if ( o.getClass() != this.getClass() ) {
187 throw new IllegalArgumentException( "attempt to check [" + this.getClass() + "] equality to " + o + " ["
188 + o.getClass() + "]" );
191 final PhylogenyNode other = ( PhylogenyNode ) o;
192 if ( !getName().equals( other.getName() ) ) {
195 final NodeData this_data = getNodeData();
196 final NodeData other_data = other.getNodeData();
197 if ( ( this_data.isHasSequence() && other_data.isHasSequence() )
198 && ( this_data.isHasTaxonomy() && other_data.isHasTaxonomy() ) ) {
199 return ( this_data.getTaxonomy().isEqual( other_data.getTaxonomy() ) && this_data.getSequence()
200 .isEqual( other_data.getSequence() ) );
202 else if ( this_data.isHasTaxonomy() && other_data.isHasTaxonomy() ) {
203 return ( this_data.getTaxonomy().isEqual( other_data.getTaxonomy() ) );
205 else if ( this_data.isHasSequence() && other_data.isHasSequence() ) {
206 return ( this_data.getSequence().isEqual( other_data.getSequence() ) );
208 else if ( getName().length() > 0 ) {
209 // Node name is not empty, and equal.
218 // ---------------------------------------------------------
219 // Obtaining of Nodes
220 // ---------------------------------------------------------
222 * Returns a List containing references to all external children of this
225 * @return List of references to external Nodes
227 final public List<PhylogenyNode> getAllExternalDescendants() {
228 final List<PhylogenyNode> nodes = new ArrayList<PhylogenyNode>();
229 if ( isExternal() ) {
233 PhylogenyNode node1 = this;
234 while ( !node1.isExternal() ) {
235 node1 = node1.getFirstChildNode();
237 PhylogenyNode node2 = this;
238 while ( !node2.isExternal() ) {
239 node2 = node2.getLastChildNode();
241 while ( node1 != node2 ) {
243 node1 = node1.getNextExternalNode();
250 * Returns a List containing references to all names of the external
251 * children of this PhylogenyNode.
253 * @return List of references to names of external Nodes
255 final public List<String> getAllExternalDescendantsNames() {
256 final List<PhylogenyNode> c = getAllExternalDescendants();
257 final List<String> n = new ArrayList<String>( c.size() );
258 for( final PhylogenyNode phylogenyNode : c ) {
259 n.add( phylogenyNode.getName() );
264 final public BranchData getBranchData() {
265 if ( _branch_data == null ) {
266 _branch_data = new BranchData();
271 final BranchData getBranchDataDirectly() {
276 * This return child node n of this node.
279 * the index of the child to get
280 * @return the child node with index n
281 * @throws IllegalArgumentException
282 * 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).
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.
491 final public int getId() {
496 final public String getName() {
497 return getNodeData().getNodeName();
500 final public List<PhylogenyNode> getAllDescendants() {
504 final public int getNumberOfDescendants() {
505 if ( _descendants == null ) {
508 return _descendants.size();
512 * Returns the total number of external Nodes originating from this
513 * PhylogenyNode (int).
515 final public int getNumberOfExternalNodes() {
516 return _sum_ext_nodes;
519 final public int getNumberOfParents() {
524 * Returns a refernce to the parent PhylogenyNode of this PhylogenyNode.
526 final public PhylogenyNode getParent() {
531 * Returns a refernce to the next external PhylogenyNode of this
532 * PhylogenyNode. TODO should be in Phylogeny. Returns null if no next
533 * external node is available.
535 final public PhylogenyNode getPreviousExternalNode() {
536 if ( isInternal() ) {
537 throw new UnsupportedOperationException( "Cannot get the previous external node for an internal node." );
539 else if ( isRoot() /* TODO && tree is rooted */) {
540 throw new UnsupportedOperationException( "Cannot get the previous external node for a root node." );
542 else if ( isFirstExternalNode() ) {
543 throw new UnsupportedOperationException( "Attempt to get previous external node of the first external node." );
545 int index = getChildNodeIndex();
546 PhylogenyNode previous_node = this;
547 PhylogenyNode current_node = getParent();
548 while ( !current_node.isRoot()
549 && ( ( current_node.getNumberOfDescendants() == 1 ) || previous_node.isFirstChildNode() ) ) {
550 index = current_node.getChildNodeIndex();
551 previous_node = current_node;
552 current_node = current_node.getParent();
554 current_node = current_node.getChildNode( index - 1 );
555 while ( current_node.isInternal() ) {
556 current_node = current_node.getLastChildNode();
562 * Used for drawing of Trees.
564 final public float getXcoord() {
568 final public float getXSecondary() {
573 * Used for drawing of Trees.
575 final public float getYcoord() {
579 final public float getYSecondary() {
584 final public int hashCode() {
585 final NodeData data = getNodeData();
586 if ( ( getName().length() < 1 ) && !data.isHasSequence() && !data.isHasTaxonomy() ) {
587 return super.hashCode();
589 int result = getName().hashCode();
590 if ( data.isHasSequence() ) {
591 result ^= data.getSequence().hashCode();
593 if ( data.isHasTaxonomy() ) {
594 result ^= data.getTaxonomy().hashCode();
599 // final private void init() {
600 //_descendants = new ArrayList<PhylogenyNode>();
601 // _parent = null; //TODO not needed?
602 // _id = 0; //TODO not needed?
603 //initializeData(); //TODO not needed?
606 * Deletes data of this PhylogenyNode. Links to the other Nodes in the
607 * Phylogeny, the ID and the sum of external nodes are NOT deleted. Field
608 * "_link" (_link to Nodes in other Phylogeny) IS deleted.
610 * @see #getLink() (Last modified: 12/20/03)
612 // final private void initializeData() {
616 // //_node_name = "";
617 // _distance_parent = PhylogenyDataUtil.BRANCH_LENGTH_DEFAULT;
618 // _collapse = false;
620 // _branch_data = null;
621 // _node_data = null;
624 * Returns whether this PhylogenyNode should be drawn as collapsed.
626 final public boolean isCollapse() {
631 * Returns true if this PhylogenyNode represents a _duplication event, false
634 final public boolean isDuplication() {
635 return getNodeData().isHasEvent() && getNodeData().getEvent().isDuplication();
639 * Checks whether this PhylogenyNode is external (tip).
641 * @return true if this PhylogenyNode is external, false otherwise
643 final public boolean isExternal() {
644 if ( _descendants == null ) {
647 return ( getNumberOfDescendants() < 1 );
653 * @return DOCUMENT ME!
655 final public boolean isFirstChildNode() {
656 if ( isRoot() /* and tree is rooted TODO */) {
657 throw new UnsupportedOperationException( "Cannot determine whether the root is the first child node of its _parent." );
659 return ( getChildNodeIndex() == 0 );
665 * @return DOCUMENT ME!
667 final public boolean isFirstExternalNode() {
668 if ( isInternal() ) {
671 PhylogenyNode node = this;
672 while ( !node.isRoot() ) {
673 if ( !node.isFirstChildNode() ) {
676 node = node.getParent();
682 * Returns whether a _duplication or speciation event has been assigned for
683 * this PhylogenyNode.
685 final public boolean isHasAssignedEvent() {
686 if ( !getNodeData().isHasEvent() ) {
689 if ( ( getNodeData().getEvent() ).isUnassigned() ) {
696 * Checks whether this PhylogenyNode is internal (tip).
698 * @return true if this PhylogenyNode is external, false otherwise
700 final public boolean isInternal() {
701 return ( !isExternal() );
705 * Returns true if this node is the last child node of its _parent.
707 * [last modified June 01, 2005 by CMZ]
709 * @return true if this node is the last child node of its _parent, false
712 final public boolean isLastChildNode() {
713 if ( isRoot() /* and tree is rooted TODO */) {
714 throw new UnsupportedOperationException( "Cannot determine whether the root is the last child node of its _parent." );
716 return ( getChildNodeIndex() == ( getParent().getNumberOfDescendants() - 1 ) );
722 * @return DOCUMENT ME!
724 final public boolean isLastExternalNode() {
725 if ( isInternal() ) {
728 PhylogenyNode node = this;
729 while ( !node.isRoot() ) {
730 if ( !node.isLastChildNode() ) {
733 node = node.getParent();
739 * Checks whether this PhylogenyNode is a root.
741 * @return true if this PhylogenyNode is the root, false otherwise
743 final public boolean isRoot() {
744 return _parent == null;
747 final public boolean isSpeciation() {
748 return getNodeData().isHasEvent() && getNodeData().getEvent().isSpeciation();
751 // ---------------------------------------------------------
753 // ---------------------------------------------------------
755 * Prints to the console the subtree originating from this PhylogenyNode in
758 public void preorderPrint() {
759 System.out.println( this + "\n" );
760 if ( isInternal() ) {
761 for( int i = 0; i < getNumberOfDescendants(); ++i ) {
762 getChildNode( i ).preorderPrint();
767 final public void removeChildNode( final int i ) {
768 if ( isExternal() ) {
769 throw new UnsupportedOperationException( "cannot get the child node for a external node." );
771 if ( ( i >= getNumberOfDescendants() ) || ( i < 0 ) ) {
772 throw new IllegalArgumentException( "attempt to get child node " + i + " of a node with "
773 + getNumberOfDescendants() + " child nodes." );
775 getDescendants().remove( i );
778 final public void removeChildNode( final PhylogenyNode remove_me ) {
779 removeChildNode( remove_me.getChildNodeIndex() );
782 final public void setBranchData( final BranchData branch_data ) {
783 _branch_data = branch_data;
787 * Sets the first child PhylogenyNode of this PhylogenyNode to n.
789 final public void setChild1( final PhylogenyNode n ) {
790 setChildNode( 0, n );
794 * Sets the second child PhylogenyNode of this PhylogenyNode to n.
796 final public void setChild2( final PhylogenyNode n ) {
797 setChildNode( 1, n );
801 * Inserts PhylogenyNode n at the specified position i into the list of
802 * child nodes. This does not allow null slots in the list of child nodes:
803 * If i is larger than the number of child nodes, n is just added to the
804 * list, not place at index i.
807 * the index of position where to add the child
809 * the PhylogenyNode to add
811 final public void setChildNode( final int i, final PhylogenyNode node ) {
812 node.setParent( this );
813 if ( getNumberOfDescendants() <= i ) {
814 addChildNode( node );
817 getDescendants().set( i, node );
821 final void setChildNodeOnly( final int i, final PhylogenyNode node ) {
822 if ( getNumberOfDescendants() <= i ) {
823 addChildNode( node );
826 getDescendants().set( i, node );
831 * Sets whether this PhylogenyNode should be drawn as collapsed.
833 final public void setCollapse( final boolean b ) {
838 * Sets the length of the branch leading to the _parent of this
839 * PhylogenyNode to double d.
842 final public void setDistanceToParent( final double d ) {
843 _distance_parent = d;
847 * Sets the _indicator value of this PhylogenyNode to i.
849 final public void setIndicator( final byte i ) {
853 // --------------------------------------------------------------------
854 // Adjust methods (related to Phylogeny construction and
855 // Phylogeny modification)
856 // --------------------------------------------------------------------
858 * Sets the indicators of all the children of this PhylogenyNode to zero.
860 final void setIndicatorsToZero() {
861 for( final PreorderTreeIterator it = new PreorderTreeIterator( this ); it.hasNext(); ) {
862 it.next().setIndicator( ( byte ) 0 );
867 * Sets the linked PhylogenyNode of this PhylogenyNode to n. Currently, this
868 * method is only used for the speciation-_duplication assignment
871 final public void setLink( final PhylogenyNode n ) {
876 * Sets the name of this node.
879 final public void setName( final String node_name ) {
880 getNodeData().setNodeName( node_name );
884 * Sets the Id of this PhylogenyNode to i. In most cases, this number
885 * should not be set to values lower than getNodeCount() -- which this method
888 synchronized final protected void setId( final int i ) {
889 if ( i < getNodeCount() ) {
890 throw new IllegalArgumentException( "attempt to set node id to a value less than total node count (thus violating the uniqueness of node ids)" );
896 * Sets the _parent PhylogenyNode of this PhylogenyNode to n.
899 final public void setParent( final PhylogenyNode n ) {
904 * Sets the total number of external Nodes originating from this
905 * PhylogenyNode to i (int).
907 final public void setSumExtNodes( final int i ) {
909 throw new IllegalArgumentException( "attempt to set sum of external nodes to less than one" );
915 * Used for drawing of Trees.
917 final public void setXcoord( final float x ) {
921 final public void setXSecondary( final float x_secondary ) {
922 _x_secondary = x_secondary;
927 * Used for drawing of Trees.
929 final public void setYcoord( final float y ) {
933 final public void setYSecondary( final float y_secondary ) {
934 _y_secondary = y_secondary;
937 // ---------------------------------------------------------
938 // Writing of Nodes to Strings
939 // ---------------------------------------------------------
940 final public String toNewHampshire( final boolean simple_nh,
941 final boolean write_distance_to_parent,
942 final NH_CONVERSION_SUPPORT_VALUE_STYLE svs ) {
943 final StringBuilder sb = new StringBuilder();
945 if ( ( svs == NH_CONVERSION_SUPPORT_VALUE_STYLE.AS_INTERNAL_NODE_NAMES ) && !isExternal() ) {
946 if ( getBranchData().isHasConfidences()
947 && ( getBranchData().getConfidence( 0 ).getValue() != Confidence.CONFIDENCE_DEFAULT_VALUE ) ) {
948 data = Confidence.FORMATTER.format( ForesterUtil
949 .round( getBranchData().getConfidence( 0 ).getValue(),
950 PhyloXmlUtil.ROUNDING_DIGITS_FOR_PHYLOXML_DOUBLE_OUTPUT ) );
953 else if ( !ForesterUtil.isEmpty( getName() ) ) {
956 else if ( getNodeData().isHasTaxonomy() ) {
957 if ( !ForesterUtil.isEmpty( getNodeData().getTaxonomy().getTaxonomyCode() ) ) {
958 data = getNodeData().getTaxonomy().getTaxonomyCode();
960 else if ( !ForesterUtil.isEmpty( getNodeData().getTaxonomy().getScientificName() ) ) {
961 data = getNodeData().getTaxonomy().getScientificName();
963 else if ( !ForesterUtil.isEmpty( getNodeData().getTaxonomy().getCommonName() ) ) {
964 data = getNodeData().getTaxonomy().getCommonName();
966 else if ( getNodeData().getTaxonomy().getTaxonomyCode() != null ) {
967 data = getNodeData().getTaxonomy().getTaxonomyCode();
970 else if ( getNodeData().isHasSequence() ) {
971 if ( !ForesterUtil.isEmpty( getNodeData().getSequence().getName() ) ) {
972 data = getNodeData().getSequence().getName();
975 if ( data.length() > 0 ) {
976 data = ForesterUtil.replaceIllegalNhCharacters( data );
977 if ( simple_nh && ( data.length() > 10 ) ) {
978 data = data.substring( 0, 11 );
980 if ( ForesterUtil.isContainsParanthesesableNhCharacter( data ) ) {
989 if ( write_distance_to_parent && ( getDistanceToParent() != PhylogenyDataUtil.BRANCH_LENGTH_DEFAULT ) ) {
991 sb.append( getDistanceToParent() );
993 if ( ( svs == NH_CONVERSION_SUPPORT_VALUE_STYLE.IN_SQUARE_BRACKETS ) && !isExternal()
994 && getBranchData().isHasConfidences()
995 && ( getBranchData().getConfidence( 0 ).getValue() != Confidence.CONFIDENCE_DEFAULT_VALUE ) ) {
997 sb.append( Confidence.FORMATTER.format( ForesterUtil
998 .round( getBranchData().getConfidence( 0 ).getValue(),
999 PhyloXmlUtil.ROUNDING_DIGITS_FOR_PHYLOXML_DOUBLE_OUTPUT ) ) );
1002 return sb.toString();
1006 * Swaps the the two childern of a PhylogenyNode node of this Phylogeny.
1008 public final void swapChildren() throws RuntimeException {
1009 if ( isExternal() ) {
1010 throw new RuntimeException( "attempt to swap descendants of external node" );
1012 if ( getNumberOfDescendants() != 2 ) {
1013 throw new RuntimeException( "attempt to swap descendants of node with " + getNumberOfDescendants()
1016 final PhylogenyNode a = getChildNode( 0 );
1017 final PhylogenyNode b = getChildNode( 1 );
1018 setChildNode( 0, b );
1019 setChildNode( 1, a );
1023 * Converts this PhylogenyNode to a New Hampshire X (NHX) String
1026 final public String toNewHampshireX() {
1027 final StringBuffer sb = new StringBuffer();
1028 final StringBuffer s_nhx = new StringBuffer();
1029 if ( !ForesterUtil.isEmpty( getName() ) ) {
1030 final String name = ForesterUtil.replaceIllegalNhCharacters( getName() );
1031 if ( ForesterUtil.isContainsParanthesesableNhCharacter( name ) ) {
1040 if ( getDistanceToParent() != PhylogenyDataUtil.BRANCH_LENGTH_DEFAULT ) {
1042 sb.append( getDistanceToParent() );
1044 if ( getNodeDataDirectly() != null ) {
1045 s_nhx.append( getNodeDataDirectly().toNHX() );
1047 if ( getBranchDataDirectly() != null ) {
1048 s_nhx.append( getBranchDataDirectly().toNHX() );
1050 if ( s_nhx.length() > 0 ) {
1051 sb.append( "[&&NHX" );
1055 return sb.toString();
1059 final public String toString() {
1060 final StringBuilder sb = new StringBuilder();
1061 if ( getNodeData().isHasTaxonomy() ) {
1062 if ( !ForesterUtil.isEmpty( getNodeData().getTaxonomy().getScientificName() ) ) {
1063 sb.append( getNodeData().getTaxonomy().getScientificName() );
1066 else if ( ( sb.length() <= 1 ) && !ForesterUtil.isEmpty( getNodeData().getTaxonomy().getTaxonomyCode() ) ) {
1067 sb.append( getNodeData().getTaxonomy().getTaxonomyCode() );
1070 else if ( getNodeData().getTaxonomy().getIdentifier() != null ) {
1071 sb.append( getNodeData().getTaxonomy().getIdentifier().toString() );
1075 if ( getNodeData().isHasSequence() ) {
1076 if ( !ForesterUtil.isEmpty( getNodeData().getSequence().getName() ) ) {
1077 sb.append( getNodeData().getSequence().getName() );
1080 if ( !ForesterUtil.isEmpty( getNodeData().getSequence().getSymbol() ) ) {
1081 sb.append( getNodeData().getSequence().getSymbol() );
1084 if ( getNodeData().getSequence().getAccession() != null ) {
1085 sb.append( getNodeData().getSequence().getAccession().toString() );
1089 if ( ( sb.length() <= 1 ) && !ForesterUtil.isEmpty( getName() ) ) {
1090 sb.append( getName() );
1093 if ( sb.length() <= 1 ) {
1095 sb.append( getId() );
1098 return sb.toString().trim();
1102 * Decreases the total number of all Nodes created so far by one.
1104 final static synchronized void decreaseNodeCount() {
1105 --PhylogenyNode._node_count;
1109 * Returns the total number of all Nodes created so far.
1111 * @return total number of Nodes (int)
1113 synchronized final public static int getNodeCount() {
1114 return PhylogenyNode._node_count;
1118 * Increases the total number of all Nodes created so far by one.
1120 synchronized final private static void increaseNodeCount() {
1121 ++PhylogenyNode._node_count;
1125 * Sets the total number of all Nodes created so far to i (int).
1127 synchronized final static void setNodeCount( final int i ) {
1128 PhylogenyNode._node_count = i;
1131 public static PhylogenyNode createInstanceFromNhxString( final String nhx ) throws NHXFormatException,
1132 PhyloXmlDataFormatException {
1133 return new PhylogenyNode( nhx, PhylogenyMethods.TAXONOMY_EXTRACTION.NO, false );
1136 public static PhylogenyNode createInstanceFromNhxString( final String nhx,
1137 final PhylogenyMethods.TAXONOMY_EXTRACTION taxonomy_extraction )
1138 throws NHXFormatException, PhyloXmlDataFormatException {
1139 return new PhylogenyNode( nhx, taxonomy_extraction, false );
1142 public static PhylogenyNode createInstanceFromNhxString( final String nhx,
1143 final PhylogenyMethods.TAXONOMY_EXTRACTION taxonomy_extraction,
1144 final boolean replace_underscores )
1145 throws NHXFormatException, PhyloXmlDataFormatException {
1146 return new PhylogenyNode( nhx, taxonomy_extraction, replace_underscores );
1149 private PhylogenyNode( final String nhx,
1150 final PhylogenyMethods.TAXONOMY_EXTRACTION taxonomy_extraction,
1151 final boolean replace_underscores ) throws NHXFormatException, PhyloXmlDataFormatException {
1153 NHXParser.parseNHX( nhx, this, taxonomy_extraction, replace_underscores );
1154 setId( PhylogenyNode.getNodeCount() );
1155 PhylogenyNode.increaseNodeCount();
1156 setSumExtNodes( 1 ); // For ext node, this number is 1 (not 0!!)