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 private static long NODE_COUNT = 0;
53 private BranchData _branch_data;
54 private boolean _collapse;
55 private ArrayList<PhylogenyNode> _descendants;
56 private double _distance_parent = PhylogenyDataUtil.BRANCH_LENGTH_DEFAULT;
58 private byte _indicator;
59 private PhylogenyNode _link;
60 private NodeData _node_data;
61 private PhylogenyNode _parent;
62 private int _sum_ext_nodes;
64 private float _x_secondary;
66 private float _y_secondary;
69 * Default constructor for PhylogenyNode.
71 public PhylogenyNode() {
72 setId( PhylogenyNode.getNodeCount() );
73 PhylogenyNode.increaseNodeCount();
74 setSumExtNodes( 1 ); // For ext node, this number is 1 (not 0!!)
77 public PhylogenyNode( final String node_name ) {
78 setId( PhylogenyNode.getNodeCount() );
79 PhylogenyNode.increaseNodeCount();
80 setSumExtNodes( 1 ); // For ext node, this number is 1 (not 0!!)
81 if ( node_name != null ) {
82 getNodeData().setNodeName( node_name );
86 private PhylogenyNode( final String nhx,
87 final NHXParser.TAXONOMY_EXTRACTION taxonomy_extraction,
88 final boolean replace_underscores ) throws NHXFormatException, PhyloXmlDataFormatException {
89 NHXParser.parseNHX( nhx, this, taxonomy_extraction, replace_underscores, false, false, false );
90 setId( PhylogenyNode.getNodeCount() );
91 PhylogenyNode.increaseNodeCount();
92 setSumExtNodes( 1 ); // For ext node, this number is 1 (not 0!!).
95 private PhylogenyNode( final String nhx,
96 final NHXParser.TAXONOMY_EXTRACTION taxonomy_extraction,
97 final boolean replace_underscores,
98 final boolean parse_extended_tags ) throws NHXFormatException, PhyloXmlDataFormatException {
99 NHXParser.parseNHX( nhx,
105 parse_extended_tags );
106 setId( PhylogenyNode.getNodeCount() );
107 PhylogenyNode.increaseNodeCount();
108 setSumExtNodes( 1 ); // For ext node, this number is 1 (not 0!!).
112 * Adds PhylogenyNode n to the list of child nodes and sets the _parent of n
116 * the PhylogenyNode to add
118 final public void addAsChild( final PhylogenyNode node ) {
119 final PhylogenyNode n = node;
124 public final int calculateDepth() {
125 PhylogenyNode n = this;
127 while ( n._parent != null ) {
134 public final double calculateDistanceToRoot() {
135 PhylogenyNode n = this;
137 while ( n._parent != null ) {
138 if ( n._distance_parent > 0.0 ) {
139 d += n._distance_parent;
147 // this is poor, as it only compares on names!
148 final public int compareTo( final PhylogenyNode o ) {
149 final PhylogenyNode n = o;
150 if ( ( getName() == null ) || ( n.getName() == null ) ) {
153 return getName().compareTo( n.getName() );
157 * Returns a new PhylogenyNode which has its data copied from this
158 * PhylogenyNode. Links to the other Nodes in the same Phylogeny are NOT
159 * copied (e.g. _link to _parent). Field "_link" IS copied.
163 final public PhylogenyNode copyNodeData() {
164 final PhylogenyNode node = new PhylogenyNode();
165 PhylogenyNode.decreaseNodeCount();
167 node._sum_ext_nodes = _sum_ext_nodes;
168 node._indicator = _indicator;
171 node._distance_parent = _distance_parent;
172 node._collapse = _collapse;
174 if ( _node_data != null ) {
175 node._node_data = ( NodeData ) _node_data.copy();
177 if ( _branch_data != null ) {
178 node._branch_data = ( BranchData ) _branch_data.copy();
184 * Returns a new PhylogenyNode which has the same data as this
185 * PhylogenyNode. Links to the other Nodes in the same Phylogeny are NOT
186 * copied (e.g. _link to _parent). Field "_link" IS copied.
190 final public PhylogenyNode copyNodeDataShallow() {
191 final PhylogenyNode node = new PhylogenyNode();
192 PhylogenyNode.decreaseNodeCount();
194 node._sum_ext_nodes = _sum_ext_nodes;
195 node._indicator = _indicator;
198 node._distance_parent = _distance_parent;
199 node._collapse = _collapse;
201 node._node_data = _node_data;
202 node._branch_data = _branch_data;
208 * Based on node name, sequence, and taxonomy.
212 final public boolean equals( final Object o ) {
216 else if ( o == null ) {
219 else if ( o.getClass() != this.getClass() ) {
220 throw new IllegalArgumentException( "attempt to check [" + this.getClass() + "] equality to " + o + " ["
221 + o.getClass() + "]" );
224 final PhylogenyNode other = ( PhylogenyNode ) o;
225 if ( !getName().equals( other.getName() ) ) {
228 final NodeData this_data = getNodeData();
229 final NodeData other_data = other.getNodeData();
230 if ( ( this_data.isHasSequence() && other_data.isHasSequence() )
231 && ( this_data.isHasTaxonomy() && other_data.isHasTaxonomy() ) ) {
232 return ( this_data.getTaxonomy().isEqual( other_data.getTaxonomy() ) && this_data.getSequence()
233 .isEqual( other_data.getSequence() ) );
235 else if ( this_data.isHasTaxonomy() && other_data.isHasTaxonomy() ) {
236 return ( this_data.getTaxonomy().isEqual( other_data.getTaxonomy() ) );
238 else if ( this_data.isHasSequence() && other_data.isHasSequence() ) {
239 return ( this_data.getSequence().isEqual( other_data.getSequence() ) );
241 else if ( getName().length() > 0 ) {
242 // Node name is not empty, and equal.
251 final public List<PhylogenyNode> getAllDescendants() {
256 * Returns a List containing references to all external children of this
259 * @return List of references to external Nodes
261 final public List<PhylogenyNode> getAllExternalDescendants() {
262 final List<PhylogenyNode> nodes = new ArrayList<PhylogenyNode>();
263 if ( isExternal() ) {
267 PhylogenyNode node1 = this;
268 while ( !node1.isExternal() ) {
269 node1 = node1.getFirstChildNode();
271 PhylogenyNode node2 = this;
272 while ( !node2.isExternal() ) {
273 node2 = node2.getLastChildNode();
275 while ( node1 != node2 ) {
277 node1 = node1.getNextExternalNode();
284 * Returns a List containing references to all names of the external
285 * children of this PhylogenyNode.
287 * @return List of references to names of external Nodes
289 final public List<String> getAllExternalDescendantsNames() {
290 final List<PhylogenyNode> c = getAllExternalDescendants();
291 final List<String> n = new ArrayList<String>( c.size() );
292 for( final PhylogenyNode phylogenyNode : c ) {
293 n.add( phylogenyNode.getName() );
298 final public BranchData getBranchData() {
299 if ( _branch_data == null ) {
300 _branch_data = new BranchData();
306 * This return child node n of this node.
309 * the index of the child to get
310 * @return the child node with index n
311 * @throws IllegalArgumentException
312 * if n is out of bounds
314 final public PhylogenyNode getChildNode( final int i ) {
315 if ( isExternal() ) {
316 throw new UnsupportedOperationException( "attempt to get the child node of an external node." );
318 if ( ( i >= getNumberOfDescendants() ) || ( i < 0 ) ) {
319 throw new IllegalArgumentException( "attempt to get child node " + i + " of a node with "
320 + getNumberOfDescendants() + " child nodes" );
322 return getDescendants().get( i );
326 * Convenience method. Returns the first child PhylogenyNode of this
329 final public PhylogenyNode getChildNode1() {
330 return getChildNode( 0 );
334 * Convenience method. Returns the second child PhylogenyNode of this
337 * [last modified May 18, 2005 by CMZ]
339 final public PhylogenyNode getChildNode2() {
340 return getChildNode( 1 );
344 * This gets the child node index of this node.
347 * @return the child node index of this node
348 * @throws UnsupportedOperationException
349 * if this node is a root node
351 final public int getChildNodeIndex() {
352 return getChildNodeIndex( getParent() );
356 * This gets the child node index of this node, given that parent is its
359 * [last modified Aug 14, 2006 by CMZ]
361 * @return the child node index of this node
362 * @throws UnsupportedOperationException
363 * if this node is a root node
365 final public int getChildNodeIndex( final PhylogenyNode parent ) {
367 throw new UnsupportedOperationException( "Cannot get the child index for a root node." );
369 for( int i = 0; i < parent.getNumberOfDescendants(); ++i ) {
370 if ( parent.getChildNode( i ) == this ) {
374 throw new RuntimeException( "Unexpected exception: Could not determine the child index for node: " + this );
377 final public List<PhylogenyNode> getDescendants() {
378 if ( _descendants == null ) {
379 _descendants = new ArrayList<PhylogenyNode>();
385 * Returns the length of the branch leading to the _parent of this
386 * PhylogenyNode (double).
388 final public double getDistanceToParent() {
389 return _distance_parent;
393 * Convenience method. Returns the first child node of this node.
395 * [last modified May 18, 2005 by CMZ]
397 * @return the first child node of this node
399 public final PhylogenyNode getFirstChildNode() {
400 return getChildNode( 0 );
404 * Returns the ID (int) of this PhylogenyNode.
406 final public long getId() {
411 * Returns the _indicator value of this PhylogenyNode.
413 public final byte getIndicator() {
418 * Convenience method. Returns the last child node of this node.
420 * [last modified May 18, 2005 by CMZ]
422 * @return the last child node of this node
424 public final PhylogenyNode getLastChildNode() {
425 return getChildNode( getNumberOfDescendants() - 1 );
429 * Returns a refernce to the linked PhylogenyNode of this PhylogenyNode.
430 * Currently, this method is only used for the speciation-_duplication
431 * assignment algorithms.
433 public final PhylogenyNode getLink() {
437 final public String getName() {
438 return getNodeData().getNodeName();
442 * Returns a refernce to the next external PhylogenyNode of this
443 * PhylogenyNode. TODO should be in Phylogeny. Returns null if no next
444 * external node is available.
446 public final PhylogenyNode getNextExternalNode() {
447 if ( isInternal() ) {
448 throw new UnsupportedOperationException( "attempt to get next external node of an internal node" );
450 else if ( isLastExternalNode() ) {
453 int index = getChildNodeIndex();
454 PhylogenyNode previous_node = this;
455 PhylogenyNode current_node = getParent();
456 while ( !current_node.isRoot()
457 && ( ( current_node.getNumberOfDescendants() == 1 ) || previous_node.isLastChildNode() ) ) {
458 index = current_node.getChildNodeIndex();
459 previous_node = current_node;
460 current_node = current_node.getParent();
462 current_node = current_node.getChildNode( index + 1 );
463 while ( current_node.isInternal() ) {
464 current_node = current_node.getFirstChildNode();
469 public final PhylogenyNode getNextExternalNodeWhileTakingIntoAccountCollapsedNodes() {
471 if ( isInternal() && !isCollapse() ) {
472 throw new UnsupportedOperationException( "attempt to get next external node of an uncollapsed internal node" );
477 if ( getParent().isCollapse() ) {
478 throw new UnsupportedOperationException( "attempt to get next external node of node with a collapsed parent" );
480 // This checks if last node.
481 PhylogenyNode n = this;
483 while ( !n.isRoot() ) {
484 if ( !n.isLastChildNode() ) {
493 int index = getChildNodeIndex();
494 PhylogenyNode previous_node = this;
495 PhylogenyNode current_node = getParent();
496 while ( !current_node.isRoot()
497 && ( current_node.isCollapse() || ( current_node.getNumberOfDescendants() == 1 ) || previous_node
498 .isLastChildNode() ) ) {
499 index = current_node.getChildNodeIndex();
500 previous_node = current_node;
501 current_node = current_node.getParent();
503 if ( index < ( current_node.getNumberOfDescendants() - 1 ) ) {
504 current_node = current_node.getChildNode( index + 1 );
506 while ( current_node.isInternal() && !current_node.isCollapse() ) {
507 current_node = current_node.getFirstChildNode();
512 public final NodeData getNodeData() {
513 if ( _node_data == null ) {
514 _node_data = new NodeData();
519 final public int getNumberOfDescendants() {
520 if ( _descendants == null ) {
523 return _descendants.size();
527 * Returns the total number of external Nodes originating from this
528 * PhylogenyNode (int).
530 final public int getNumberOfExternalNodes() {
531 return _sum_ext_nodes;
534 final public int getNumberOfParents() {
539 * Returns a refernce to the parent PhylogenyNode of this PhylogenyNode.
541 final public PhylogenyNode getParent() {
546 * Returns a refernce to the next external PhylogenyNode of this
547 * PhylogenyNode. TODO should be in Phylogeny. Returns null if no next
548 * external node is available.
550 final public PhylogenyNode getPreviousExternalNode() {
551 if ( isInternal() ) {
552 throw new UnsupportedOperationException( "Cannot get the previous external node for an internal node." );
554 else if ( isRoot() /* TODO && tree is rooted */) {
555 throw new UnsupportedOperationException( "Cannot get the previous external node for a root node." );
557 else if ( isFirstExternalNode() ) {
558 throw new UnsupportedOperationException( "Attempt to get previous external node of the first external node." );
560 int index = getChildNodeIndex();
561 PhylogenyNode previous_node = this;
562 PhylogenyNode current_node = getParent();
563 while ( !current_node.isRoot()
564 && ( ( current_node.getNumberOfDescendants() == 1 ) || previous_node.isFirstChildNode() ) ) {
565 index = current_node.getChildNodeIndex();
566 previous_node = current_node;
567 current_node = current_node.getParent();
569 current_node = current_node.getChildNode( index - 1 );
570 while ( current_node.isInternal() ) {
571 current_node = current_node.getLastChildNode();
577 * Used for drawing of Trees.
579 final public float getXcoord() {
583 final public float getXSecondary() {
588 * Used for drawing of Trees.
590 final public float getYcoord() {
594 final public float getYSecondary() {
599 final public int hashCode() {
600 final NodeData data = getNodeData();
601 if ( ( getName().length() < 1 ) && !data.isHasSequence() && !data.isHasTaxonomy() ) {
602 return super.hashCode();
604 int result = getName().hashCode();
605 if ( data.isHasSequence() ) {
606 result ^= data.getSequence().hashCode();
608 if ( data.isHasTaxonomy() ) {
609 result ^= data.getTaxonomy().hashCode();
615 * Returns whether this PhylogenyNode should be drawn as collapsed.
617 final public boolean isCollapse() {
622 * Returns true if this PhylogenyNode represents a _duplication event, false
625 final public boolean isDuplication() {
626 return getNodeData().isHasEvent() && getNodeData().getEvent().isDuplication();
629 public boolean isEmpty() {
630 return ( ( _node_data == null ) || _node_data.isEmpty() );
634 * Checks whether this PhylogenyNode is external (tip).
636 * @return true if this PhylogenyNode is external, false otherwise
638 final public boolean isExternal() {
639 if ( _descendants == null ) {
642 return ( getNumberOfDescendants() < 1 );
645 final public boolean isFirstChildNode() {
646 if ( isRoot() /* and tree is rooted TODO */) {
647 throw new UnsupportedOperationException( "Cannot determine whether the root is the first child node of its _parent." );
649 return ( getChildNodeIndex() == 0 );
652 final public boolean isFirstExternalNode() {
653 if ( isInternal() ) {
656 PhylogenyNode node = this;
657 while ( !node.isRoot() ) {
658 if ( !node.isFirstChildNode() ) {
661 node = node.getParent();
667 * Returns whether a _duplication or speciation event has been assigned for
668 * this PhylogenyNode.
670 final public boolean isHasAssignedEvent() {
671 if ( !getNodeData().isHasEvent() ) {
674 if ( ( getNodeData().getEvent() ).isUnassigned() ) {
681 * Checks whether this PhylogenyNode is internal (tip).
683 * @return true if this PhylogenyNode is external, false otherwise
685 final public boolean isInternal() {
686 return ( !isExternal() );
690 * Returns true if this node is the last child node of its _parent.
692 * [last modified June 01, 2005 by CMZ]
694 * @return true if this node is the last child node of its _parent, false
697 final public boolean isLastChildNode() {
698 if ( isRoot() /* and tree is rooted TODO */) {
699 throw new UnsupportedOperationException( "Cannot determine whether the root is the last child node of its _parent." );
701 return ( getChildNodeIndex() == ( getParent().getNumberOfDescendants() - 1 ) );
704 final public boolean isLastExternalNode() {
705 if ( isInternal() ) {
708 PhylogenyNode node = this;
709 while ( !node.isRoot() ) {
710 if ( !node.isLastChildNode() ) {
713 node = node.getParent();
719 * Checks whether this PhylogenyNode is a root.
721 * @return true if this PhylogenyNode is the root, false otherwise
723 final public boolean isRoot() {
724 return _parent == null;
727 final public boolean isSpeciation() {
728 return getNodeData().isHasEvent() && getNodeData().getEvent().isSpeciation();
731 // ---------------------------------------------------------
733 // ---------------------------------------------------------
735 * Prints to the console the subtree originating from this PhylogenyNode in
738 public void preorderPrint() {
739 System.out.println( this + "\n" );
740 if ( isInternal() ) {
741 for( int i = 0; i < getNumberOfDescendants(); ++i ) {
742 getChildNode( i ).preorderPrint();
747 final public void removeChildNode( final int i ) {
748 if ( isExternal() ) {
749 throw new UnsupportedOperationException( "cannot get the child node for a external node." );
751 if ( ( i >= getNumberOfDescendants() ) || ( i < 0 ) ) {
752 throw new IllegalArgumentException( "attempt to get child node " + i + " of a node with "
753 + getNumberOfDescendants() + " child nodes." );
755 getDescendants().remove( i );
758 final public void removeChildNode( final PhylogenyNode remove_me ) {
759 removeChildNode( remove_me.getChildNodeIndex() );
762 public void removeConnections() {
768 final public void setBranchData( final BranchData branch_data ) {
769 _branch_data = branch_data;
773 * Sets the first child PhylogenyNode of this PhylogenyNode to n.
775 final public void setChild1( final PhylogenyNode n ) {
776 setChildNode( 0, n );
780 * Sets the second child PhylogenyNode of this PhylogenyNode to n.
782 final public void setChild2( final PhylogenyNode n ) {
783 setChildNode( 1, n );
787 * Inserts PhylogenyNode n at the specified position i into the list of
788 * child nodes. This does not allow null slots in the list of child nodes:
789 * If i is larger than the number of child nodes, n is just added to the
790 * list, not place at index i.
793 * the index of position where to add the child
795 * the PhylogenyNode to add
797 final public void setChildNode( final int i, final PhylogenyNode node ) {
798 node.setParent( this );
799 if ( getNumberOfDescendants() <= i ) {
800 addChildNode( node );
803 getDescendants().set( i, node );
808 * Sets whether this PhylogenyNode should be drawn as collapsed.
810 final public void setCollapse( final boolean b ) {
815 * Sets the length of the branch leading to the _parent of this
816 * PhylogenyNode to double d.
818 final public void setDistanceToParent( final double d ) {
819 _distance_parent = d;
823 * Sets the _indicator value of this PhylogenyNode to i.
825 final public void setIndicator( final byte i ) {
830 * Sets the linked PhylogenyNode of this PhylogenyNode to n. Currently, this
831 * method is only used for the speciation-_duplication assignment
834 final public void setLink( final PhylogenyNode n ) {
839 * Sets the name of this node.
841 final public void setName( final String node_name ) {
842 getNodeData().setNodeName( node_name );
846 * Sets the _parent PhylogenyNode of this PhylogenyNode to n.
848 final public void setParent( final PhylogenyNode n ) {
853 * Sets the total number of external Nodes originating from this
854 * PhylogenyNode to i (int).
856 final public void setSumExtNodes( final int i ) {
858 throw new IllegalArgumentException( "attempt to set sum of external nodes to less than one" );
864 * Used for drawing of Trees.
866 final public void setXcoord( final float x ) {
870 final public void setXSecondary( final float x_secondary ) {
871 _x_secondary = x_secondary;
876 * Used for drawing of Trees.
878 final public void setYcoord( final float y ) {
882 final public void setYSecondary( final float y_secondary ) {
883 _y_secondary = y_secondary;
887 * Swaps the the two childern of a PhylogenyNode node of this Phylogeny.
889 public final void swapChildren() throws RuntimeException {
890 if ( isExternal() ) {
891 throw new RuntimeException( "attempt to swap descendants of external node" );
893 if ( getNumberOfDescendants() != 2 ) {
894 throw new RuntimeException( "attempt to swap descendants of node with " + getNumberOfDescendants()
897 final PhylogenyNode a = getChildNode( 0 );
898 final PhylogenyNode b = getChildNode( 1 );
899 setChildNode( 0, b );
900 setChildNode( 1, a );
903 // ---------------------------------------------------------
904 // Writing of Nodes to Strings
905 // ---------------------------------------------------------
906 final public String toNewHampshire( final boolean write_distance_to_parent,
907 final NH_CONVERSION_SUPPORT_VALUE_STYLE svs ) {
909 if ( ( svs == NH_CONVERSION_SUPPORT_VALUE_STYLE.AS_INTERNAL_NODE_NAMES ) && !isExternal() ) {
910 if ( getBranchData().isHasConfidences()
911 && ( getBranchData().getConfidence( 0 ).getValue() != Confidence.CONFIDENCE_DEFAULT_VALUE ) ) {
912 data = Confidence.FORMATTER.format( ForesterUtil
913 .round( getBranchData().getConfidence( 0 ).getValue(),
914 PhyloXmlUtil.ROUNDING_DIGITS_FOR_PHYLOXML_DOUBLE_OUTPUT ) );
917 else if ( !ForesterUtil.isEmpty( getName() ) ) {
920 else if ( getNodeData().isHasTaxonomy() ) {
921 if ( !ForesterUtil.isEmpty( getNodeData().getTaxonomy().getTaxonomyCode() ) ) {
922 data = getNodeData().getTaxonomy().getTaxonomyCode();
924 else if ( !ForesterUtil.isEmpty( getNodeData().getTaxonomy().getScientificName() ) ) {
925 data = getNodeData().getTaxonomy().getScientificName();
927 else if ( !ForesterUtil.isEmpty( getNodeData().getTaxonomy().getCommonName() ) ) {
928 data = getNodeData().getTaxonomy().getCommonName();
931 else if ( getNodeData().isHasSequence() ) {
932 if ( !ForesterUtil.isEmpty( getNodeData().getSequence().getName() ) ) {
933 data = getNodeData().getSequence().getName();
935 else if ( !ForesterUtil.isEmpty( getNodeData().getSequence().getSymbol() ) ) {
936 data = getNodeData().getSequence().getSymbol();
938 else if ( !ForesterUtil.isEmpty( getNodeData().getSequence().getGeneName() ) ) {
939 data = getNodeData().getSequence().getGeneName();
942 final StringBuilder sb = ForesterUtil.santitizeStringForNH( data );
943 if ( write_distance_to_parent && ( getDistanceToParent() != PhylogenyDataUtil.BRANCH_LENGTH_DEFAULT ) ) {
945 sb.append( getDistanceToParent() );
947 if ( ( svs == NH_CONVERSION_SUPPORT_VALUE_STYLE.IN_SQUARE_BRACKETS ) && !isExternal()
948 && getBranchData().isHasConfidences()
949 && ( getBranchData().getConfidence( 0 ).getValue() != Confidence.CONFIDENCE_DEFAULT_VALUE ) ) {
951 sb.append( Confidence.FORMATTER.format( ForesterUtil
952 .round( getBranchData().getConfidence( 0 ).getValue(),
953 PhyloXmlUtil.ROUNDING_DIGITS_FOR_PHYLOXML_DOUBLE_OUTPUT ) ) );
956 return sb.toString();
960 * Converts this PhylogenyNode to a New Hampshire X (NHX) String
963 final public String toNewHampshireX() {
964 final StringBuilder sb = new StringBuilder();
965 final StringBuffer s_nhx = new StringBuffer();
966 if ( !ForesterUtil.isEmpty( getName() ) ) {
967 sb.append( ForesterUtil.santitizeStringForNH( getName() ) );
969 if ( getDistanceToParent() != PhylogenyDataUtil.BRANCH_LENGTH_DEFAULT ) {
971 sb.append( getDistanceToParent() );
973 if ( getNodeDataDirectly() != null ) {
974 s_nhx.append( getNodeDataDirectly().toNHX() );
976 if ( getBranchDataDirectly() != null ) {
977 s_nhx.append( getBranchDataDirectly().toNHX() );
979 if ( s_nhx.length() > 0 ) {
980 sb.append( "[&&NHX" );
984 return sb.toString();
988 final public String toString() {
989 final StringBuilder sb = new StringBuilder();
990 if ( !ForesterUtil.isEmpty( getName() ) ) {
991 sb.append( getName() );
994 if ( getNodeData().isHasTaxonomy() ) {
995 if ( !ForesterUtil.isEmpty( getNodeData().getTaxonomy().getScientificName() ) ) {
996 sb.append( getNodeData().getTaxonomy().getScientificName() );
999 else if ( ( sb.length() <= 1 ) && !ForesterUtil.isEmpty( getNodeData().getTaxonomy().getTaxonomyCode() ) ) {
1000 sb.append( getNodeData().getTaxonomy().getTaxonomyCode() );
1003 else if ( getNodeData().getTaxonomy().getIdentifier() != null ) {
1004 sb.append( getNodeData().getTaxonomy().getIdentifier().toString() );
1008 if ( getNodeData().isHasSequence() ) {
1009 if ( !ForesterUtil.isEmpty( getNodeData().getSequence().getName() ) ) {
1010 sb.append( getNodeData().getSequence().getName() );
1013 if ( !ForesterUtil.isEmpty( getNodeData().getSequence().getSymbol() ) ) {
1014 sb.append( getNodeData().getSequence().getSymbol() );
1017 if ( !ForesterUtil.isEmpty( getNodeData().getSequence().getGeneName() ) ) {
1018 sb.append( getNodeData().getSequence().getGeneName() );
1021 if ( getNodeData().getSequence().getAccession() != null ) {
1022 sb.append( getNodeData().getSequence().getAccession().toString() );
1025 if ( !ForesterUtil.isEmpty( getNodeData().getSequence().getMolecularSequence() ) ) {
1026 sb.append( getNodeData().getSequence().getMolecularSequence() );
1030 if ( sb.length() <= 1 ) {
1032 sb.append( getId() );
1035 return sb.toString().trim();
1039 * Sets the Id of this PhylogenyNode to i. In most cases, this number
1040 * should not be set to values lower than getNodeCount() -- which this method
1043 synchronized final protected void setId( final long i ) {
1044 if ( i < getNodeCount() ) {
1045 throw new IllegalArgumentException( "attempt to set node id to a value less than total node count (thus violating the uniqueness of node ids)" );
1050 final BranchData getBranchDataDirectly() {
1051 return _branch_data;
1054 final NodeData getNodeDataDirectly() {
1058 final void setChildNodeOnly( final int i, final PhylogenyNode node ) {
1059 if ( getNumberOfDescendants() <= i ) {
1060 addChildNode( node );
1063 getDescendants().set( i, node );
1068 * Sets the indicators of all the children of this PhylogenyNode to zero.
1070 final void setIndicatorsToZero() {
1071 for( final PreorderTreeIterator it = new PreorderTreeIterator( this ); it.hasNext(); ) {
1072 it.next().setIndicator( ( byte ) 0 );
1077 * Adds PhylogenyNode n to the list of child nodes. But does NOT set the
1078 * _parent of n to this.
1080 * @see addAsChild( PhylogenyNode n )
1082 * the PhylogenyNode to add
1084 final private void addChildNode( final PhylogenyNode child ) {
1085 getDescendants().add( child );
1088 public static PhylogenyNode createInstanceFromNhxString( final String nhx ) throws NHXFormatException,
1089 PhyloXmlDataFormatException {
1090 return new PhylogenyNode( nhx, NHXParser.TAXONOMY_EXTRACTION.NO, false );
1093 public static PhylogenyNode createInstanceFromNhxString( final String nhx,
1094 final NHXParser.TAXONOMY_EXTRACTION taxonomy_extraction )
1095 throws NHXFormatException, PhyloXmlDataFormatException {
1096 return new PhylogenyNode( nhx, taxonomy_extraction, false );
1099 public static PhylogenyNode createInstanceFromNhxString( final String nhx,
1100 final NHXParser.TAXONOMY_EXTRACTION taxonomy_extraction,
1101 final boolean replace_underscores )
1102 throws NHXFormatException, PhyloXmlDataFormatException {
1103 return new PhylogenyNode( nhx, taxonomy_extraction, replace_underscores );
1106 public static PhylogenyNode createInstanceFromNhxString( final String nhx,
1107 final NHXParser.TAXONOMY_EXTRACTION taxonomy_extraction,
1108 final boolean replace_underscores,
1109 final boolean parse_extended_tags )
1110 throws NHXFormatException, PhyloXmlDataFormatException {
1111 return new PhylogenyNode( nhx, taxonomy_extraction, replace_underscores, parse_extended_tags );
1115 * Returns the total number of all Nodes created so far.
1117 * @return total number of Nodes (long)
1119 synchronized final public static long getNodeCount() {
1124 * Decreases the total number of all Nodes created so far by one.
1126 final static synchronized void decreaseNodeCount() {
1131 * Sets the total number of all Nodes created so far to i.
1133 synchronized final static void setNodeCount( final long i ) {
1134 PhylogenyNode.NODE_COUNT = i;
1138 * Increases the total number of all Nodes created so far by one.
1140 synchronized final private static void increaseNodeCount() {
1144 public enum NH_CONVERSION_SUPPORT_VALUE_STYLE {
1145 AS_INTERNAL_NODE_NAMES, IN_SQUARE_BRACKETS, NONE;