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.io.IOException;
31 import java.util.ArrayList;
32 import java.util.List;
34 import org.forester.archaeopteryx.Archaeopteryx;
35 import org.forester.io.parsers.nhx.NHXFormatException;
36 import org.forester.io.parsers.nhx.NHXParser;
37 import org.forester.phylogeny.data.BranchData;
38 import org.forester.phylogeny.data.NodeData;
39 import org.forester.phylogeny.factories.ParserBasedPhylogenyFactory;
40 import org.forester.phylogeny.factories.PhylogenyFactory;
41 import org.forester.phylogeny.iterators.ChildNodeIteratorForward;
42 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
43 import org.forester.phylogeny.iterators.PreorderTreeIterator;
44 import org.forester.util.ForesterUtil;
46 public class PhylogenyNode implements PhylogenyNodeI, Comparable<PhylogenyNode> {
48 /** Value of -99.0 is used as default value. */
49 public final static double DISTANCE_DEFAULT = -1024.0;
50 private static int _node_count = 0;
51 private byte _indicator;
53 private int _sum_ext_nodes;
56 private double _distance_parent;
57 private boolean _collapse;
58 private PhylogenyNode _parent;
59 private PhylogenyNode _link;
60 private ArrayList<PhylogenyNode> _descendants;
61 private NodeData _node_data;
62 private BranchData _branch_data;
63 private float _x_secondary;
64 private float _y_secondary;
67 * Default constructor for PhylogenyNode.
69 public PhylogenyNode() {
71 setId( PhylogenyNode.getNodeCount() );
72 PhylogenyNode.increaseNodeCount();
73 setSumExtNodes( 1 ); // For ext node, this number is 1 (not 0!!)
77 * Adds PhylogenyNode n to the list of child nodes and sets the _parent of n
81 * the PhylogenyNode to add
84 final public void addAsChild( final PhylogenyNodeI node ) {
85 final PhylogenyNode n = ( PhylogenyNode ) node;
91 * Adds PhylogenyNode n to the list of child nodes. But does NOT set the
92 * _parent of n to this.
94 * @see addAsChild( PhylogenyNode n )
96 * the PhylogenyNode to add
98 final private void addChildNode( final PhylogenyNode child ) {
99 getDescendants().add( child );
103 final public int compareTo( final PhylogenyNode o ) {
104 final PhylogenyNode n = o;
105 if ( ( getName() == null ) || ( n.getName() == null ) ) {
108 return getName().compareTo( n.getName() );
111 // ---------------------------------------------------------
112 // Copy and delete Nodes, copy subtress
113 // ---------------------------------------------------------
115 * Returns a new PhylogenyNode which has its data copied from this
116 * PhylogenyNode. Links to the other Nodes in the same Phylogeny are NOT
117 * copied (e.g. _link to _parent). Field "_link" IS copied.
121 final public PhylogenyNode copyNodeData() {
122 final PhylogenyNode node = new PhylogenyNode();
123 PhylogenyNode.decreaseNodeCount();
125 node._sum_ext_nodes = _sum_ext_nodes;
126 node._indicator = _indicator;
129 node._distance_parent = _distance_parent;
130 node._collapse = _collapse;
132 if ( _node_data != null ) {
133 node._node_data = ( NodeData ) _node_data.copy();
135 if ( _branch_data != null ) {
136 node._branch_data = ( BranchData ) _branch_data.copy();
142 * Returns a new PhylogenyNode which has the same data as this
143 * PhylogenyNode. Links to the other Nodes in the same Phylogeny are NOT
144 * copied (e.g. _link to _parent). Field "_link" IS copied.
148 final public PhylogenyNode copyNodeDataShallow() {
149 final PhylogenyNode node = new PhylogenyNode();
150 PhylogenyNode.decreaseNodeCount();
152 node._sum_ext_nodes = _sum_ext_nodes;
153 node._indicator = _indicator;
156 node._distance_parent = _distance_parent;
157 node._collapse = _collapse;
159 node._node_data = _node_data;
160 node._branch_data = _branch_data;
166 * Based on node name, sequence, and taxonomy.
170 final public boolean equals( final Object o ) {
174 else if ( o == null ) {
177 else if ( o.getClass() != this.getClass() ) {
178 throw new IllegalArgumentException( "attempt to check [" + this.getClass() + "] equality to " + o + " ["
179 + o.getClass() + "]" );
182 final PhylogenyNode other = ( PhylogenyNode ) o;
183 if ( !getName().equals( other.getName() ) ) {
186 final NodeData this_data = getNodeData();
187 final NodeData other_data = other.getNodeData();
188 if ( ( this_data.isHasSequence() && other_data.isHasSequence() )
189 && ( this_data.isHasTaxonomy() && other_data.isHasTaxonomy() ) ) {
190 return ( this_data.getTaxonomy().isEqual( other_data.getTaxonomy() ) && this_data.getSequence()
191 .isEqual( other_data.getSequence() ) );
193 else if ( this_data.isHasSequence() && other_data.isHasSequence() ) {
194 return ( this_data.getSequence().isEqual( other_data.getSequence() ) );
196 else if ( this_data.isHasTaxonomy() && other_data.isHasTaxonomy() ) {
197 return ( this_data.getTaxonomy().isEqual( other_data.getTaxonomy() ) );
199 else if ( getName().length() > 0 ) {
200 // Node name is not empty, and equal.
209 // ---------------------------------------------------------
210 // Obtaining of Nodes
211 // ---------------------------------------------------------
213 * Returns a List containing references to all external children of this
216 * @return List of references to external Nodes
218 final public List<PhylogenyNode> getAllExternalDescendants() {
219 final List<PhylogenyNode> nodes = new ArrayList<PhylogenyNode>();
220 if ( isExternal() ) {
224 PhylogenyNode node1 = this;
225 while ( !node1.isExternal() ) {
226 node1 = node1.getFirstChildNode();
228 PhylogenyNode node2 = this;
229 while ( !node2.isExternal() ) {
230 node2 = node2.getLastChildNode();
232 while ( node1 != node2 ) {
234 node1 = node1.getNextExternalNode();
241 * Returns a List containing references to all names of the external
242 * children of this PhylogenyNode.
244 * @return List of references to names of external Nodes
246 final public List<String> getAllExternalDescendantsNames() {
247 final List<PhylogenyNode> c = getAllExternalDescendants();
248 final List<String> n = new ArrayList<String>( c.size() );
249 for( final PhylogenyNode phylogenyNode : c ) {
250 n.add( phylogenyNode.getName() );
255 final public BranchData getBranchData() {
256 if ( _branch_data == null ) {
257 _branch_data = new BranchData();
262 final BranchData getBranchDataDirectly() {
267 * This return child node n of this node.
270 * the index of the child to get
271 * @return the child node with index n
272 * @throws IllegalArgumentException
273 * if n is out of bounds
276 final public PhylogenyNode getChildNode( final int i ) {
277 if ( isExternal() ) {
278 throw new UnsupportedOperationException( "attempt to get the child node of an external node." );
280 if ( ( i >= getNumberOfDescendants() ) || ( i < 0 ) ) {
281 throw new IllegalArgumentException( "attempt to get child node " + i + " of a node with "
282 + getNumberOfDescendants() + " child nodes" );
284 return getDescendants().get( i );
288 * Convenience method. Returns the first child PhylogenyNode of this
291 final public PhylogenyNode getChildNode1() {
292 return getChildNode( 0 );
296 * Convenience method. Returns the second child PhylogenyNode of this
299 * [last modified May 18, 2005 by CMZ]
301 final public PhylogenyNode getChildNode2() {
302 return getChildNode( 1 );
306 * This gets the child node index of this node.
309 * @return the child node index of this node
310 * @throws UnsupportedOperationException
311 * if this node is a root node
313 final public int getChildNodeIndex() {
314 return getChildNodeIndex( getParent() );
318 * This gets the child node index of this node, given that parent is its
321 * [last modified Aug 14, 2006 by CMZ]
323 * @return the child node index of this node
324 * @throws UnsupportedOperationException
325 * if this node is a root node
327 final public int getChildNodeIndex( final PhylogenyNode parent ) {
329 throw new UnsupportedOperationException( "Cannot get the child index for a root node." );
331 for( int i = 0; i < parent.getNumberOfDescendants(); ++i ) {
332 if ( parent.getChildNode( i ) == this ) {
336 throw new RuntimeException( "Unexpected exception: Could not determine the child index for node: " + this );
339 final public List<PhylogenyNode> getDescendants() {
344 * Returns the length of the branch leading to the _parent of this
345 * PhylogenyNode (double).
348 final public double getDistanceToParent() {
349 return _distance_parent;
353 * Convenience method. Returns the first child node of this node.
355 * [last modified May 18, 2005 by CMZ]
357 * @return the first child node of this node
359 public final PhylogenyNode getFirstChildNode() {
360 return getChildNode( 0 );
364 * Returns the _indicator value of this PhylogenyNode.
366 public final byte getIndicator() {
371 * Convenience method. Returns the last child node of this node.
373 * [last modified May 18, 2005 by CMZ]
375 * @return the last child node of this node
377 public final PhylogenyNode getLastChildNode() {
378 return getChildNode( getNumberOfDescendants() - 1 );
382 * Returns a refernce to the linked PhylogenyNode of this PhylogenyNode.
383 * Currently, this method is only used for the speciation-_duplication
384 * assignment algorithms.
386 public final PhylogenyNode getLink() {
391 * Returns a refernce to the next external PhylogenyNode of this
392 * PhylogenyNode. TODO should be in Phylogeny. Returns null if no next
393 * external node is available.
395 public final PhylogenyNode getNextExternalNode() {
396 if ( isInternal() ) {
397 throw new UnsupportedOperationException( "attempt to get next external node of an internal node" );
399 else if ( isLastExternalNode() ) {
402 int index = getChildNodeIndex();
403 PhylogenyNode previous_node = this;
404 PhylogenyNode current_node = getParent();
405 while ( !current_node.isRoot()
406 && ( ( current_node.getNumberOfDescendants() == 1 ) || previous_node.isLastChildNode() ) ) {
407 index = current_node.getChildNodeIndex();
408 previous_node = current_node;
409 current_node = current_node.getParent();
411 current_node = current_node.getChildNode( index + 1 );
412 while ( current_node.isInternal() ) {
413 current_node = current_node.getFirstChildNode();
418 public final PhylogenyNode getNextExternalNodeWhileTakingIntoAccountCollapsedNodes() {
420 if ( isInternal() && !isCollapse() ) {
421 throw new UnsupportedOperationException( "attempt to get next external node of an uncollapsed internal node" );
423 else if ( isLastExternalNode() ) {
426 int index = getChildNodeIndex();
427 PhylogenyNode previous_node = this;
428 PhylogenyNode current_node = getParent();
429 while ( !current_node.isRoot()
430 && ( current_node.isCollapse() || ( current_node.getNumberOfDescendants() == 1 ) || previous_node
431 .isLastChildNode() ) ) {
432 index = current_node.getChildNodeIndex();
433 previous_node = current_node;
434 System.out.println(" " + previous_node.getName());
435 current_node = current_node.getParent();
437 // if ( !current_node.isCollapse() ) {
438 current_node = current_node.getChildNode( index + 1 );
440 while ( current_node.isInternal() && !current_node.isCollapse() ) {
441 current_node = current_node.getFirstChildNode();
446 public static void main( final String[] args ) {
448 final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
450 List<PhylogenyNode> ext = new ArrayList<PhylogenyNode>();
451 // final StringBuffer sb0 = new StringBuffer(
452 // "((a,b)ab,(((c,d)cd,e)cde,(f,(g,h))fgh)cdefgh)abcdefgh" );
453 // final Phylogeny t0 = factory.create( sb0, new NHXParser() )[ 0 ];
455 // t0.getNode( "cd" ).setCollapse( true );
456 // t0.getNode( "cde" ).setCollapse( true );
457 // n = t0.getFirstExternalNode();
459 // while ( n != null ) {
460 // System.out.println( n.getName() );
462 // n = n.getNextExternalNodeWhileTakingIntoAccountCollapsedNodes();
465 // // Archaeopteryx.createApplication( t );
466 // if ( !ext.get( 0 ).getName().equals( "a" ) ) {
467 // System.out.println( "0 fail" );
469 // if ( !ext.get( 1 ).getName().equals( "b" ) ) {
470 // System.out.println( "1 fail" );
472 // if ( !ext.get( 2 ).getName().equals( "cde" ) ) {
473 // System.out.println( "2 fail" );
475 // if ( !ext.get( 3 ).getName().equals( "f" ) ) {
476 // System.out.println( "3 fail" );
478 // if ( !ext.get( 4 ).getName().equals( "g" ) ) {
479 // System.out.println( "4 fail" );
481 // if ( !ext.get( 5 ).getName().equals( "h" ) ) {
482 // System.out.println( "5 fail" );
484 // if ( !ext.get( 6 ).getName().equals( "a" ) ) {
485 // System.out.println( "6 fail" );
487 // if ( !ext.get( 7 ).getName().equals( "a" ) ) {
488 // System.out.println( "7 fail" );
492 final StringBuffer sb1 = new StringBuffer(
493 "((a,b)ab,(((c,d)cd,e)cde,(f,(g,h))fgh)cdefgh)abcdefgh" );
494 final Phylogeny t1 = factory.create( sb1, new NHXParser() )[ 0 ];
496 t1.getNode( "ab" ).setCollapse( true );
497 t1.getNode( "cd" ).setCollapse( true );
498 t1.getNode( "cde" ).setCollapse( true );
499 // n = t1.getFirstExternalNode();
500 n = t1.getNode( "ab" );
501 ext = new ArrayList<PhylogenyNode>();
502 while ( n != null ) {
503 System.out.println( n.getName() );
505 n = n.getNextExternalNodeWhileTakingIntoAccountCollapsedNodes();
508 // Archaeopteryx.createApplication( t1 );
509 if ( !ext.get( 0 ).getName().equals( "ab" ) ) {
510 System.out.println( "0 fail" );
513 if ( !ext.get( 1 ).getName().equals( "cde" ) ) {
514 System.out.println( "1 fail" );
516 if ( !ext.get( 2 ).getName().equals( "f" ) ) {
517 System.out.println( "2 fail" );
519 if ( !ext.get( 3 ).getName().equals( "g" ) ) {
520 System.out.println( "3 fail" );
522 if ( !ext.get( 4 ).getName().equals( "h" ) ) {
523 System.out.println( "4 fail" );
528 final StringBuffer sb2 = new StringBuffer(
529 "((a,b)ab,(((c,d)cd,e)cde,(f,(g,h)gh)fgh)cdefgh)abcdefgh" );
530 final Phylogeny t2 = factory.create( sb2, new NHXParser() )[ 0 ];
532 t2.getNode( "ab" ).setCollapse( true );
533 t2.getNode( "cd" ).setCollapse( true );
534 t2.getNode( "cde" ).setCollapse( true );
535 t2.getNode( "c" ).setCollapse( true );
536 t2.getNode( "d" ).setCollapse( true );
537 t2.getNode( "e" ).setCollapse( true );
538 t2.getNode( "gh" ).setCollapse( true );
539 // t2.getNode( "h" ).setCollapse( true );
540 // n = t1.getFirstExternalNode();
541 n = t2.getNode( "ab" );
542 ext = new ArrayList<PhylogenyNode>();
543 while ( n != null ) {
544 System.out.println( n.getName() );
546 n = n.getNextExternalNodeWhileTakingIntoAccountCollapsedNodes();
549 // Archaeopteryx.createApplication( t1 );
550 if ( !ext.get( 0 ).getName().equals( "ab" ) ) {
551 System.out.println( "0 fail" );
554 if ( !ext.get( 1 ).getName().equals( "cde" ) ) {
555 System.out.println( "1 fail" );
557 if ( !ext.get( 2 ).getName().equals( "f" ) ) {
558 System.out.println( "2 fail" );
560 if ( !ext.get( 3 ).getName().equals( "g" ) ) {
561 System.out.println( "3 fail" );
563 if ( !ext.get( 4 ).getName().equals( "h" ) ) {
564 System.out.println( "4 fail" );
569 catch ( IOException e ) {
570 // TODO Auto-generated catch block
576 public final NodeData getNodeData() {
577 if ( _node_data == null ) {
578 _node_data = new NodeData();
583 final NodeData getNodeDataDirectly() {
587 // ---------------------------------------------------------
588 // Set and get methods for Nodes
589 // ---------------------------------------------------------
591 * Returns the ID (int) of this PhylogenyNode.
594 final public int getId() {
599 * Returns the name of this node.
602 final public String getName() {
603 return getNodeData().getNodeName();
606 final public int getNumberOfDescendants() {
607 return _descendants.size();
611 * Returns the total number of external Nodes originating from this
612 * PhylogenyNode (int).
614 final public int getNumberOfExternalNodes() {
615 return _sum_ext_nodes;
618 final public int getNumberOfParents() {
623 * Returns a refernce to the parent PhylogenyNode of this PhylogenyNode.
625 final public PhylogenyNode getParent() {
630 * Returns a refernce to the next external PhylogenyNode of this
631 * PhylogenyNode. TODO should be in Phylogeny. Returns null if no next
632 * external node is available.
634 final public PhylogenyNode getPreviousExternalNode() {
635 if ( isInternal() ) {
636 throw new UnsupportedOperationException( "Cannot get the previous external node for an internal node." );
638 else if ( isRoot() /* TODO && tree is rooted */) {
639 throw new UnsupportedOperationException( "Cannot get the previous external node for a root node." );
641 else if ( isFirstExternalNode() ) {
642 throw new UnsupportedOperationException( "Attempt to get previous external node of the first external node." );
644 int index = getChildNodeIndex();
645 PhylogenyNode previous_node = this;
646 PhylogenyNode current_node = getParent();
647 while ( !current_node.isRoot()
648 && ( ( current_node.getNumberOfDescendants() == 1 ) || previous_node.isFirstChildNode() ) ) {
649 index = current_node.getChildNodeIndex();
650 previous_node = current_node;
651 current_node = current_node.getParent();
653 current_node = current_node.getChildNode( index - 1 );
654 while ( current_node.isInternal() ) {
655 current_node = current_node.getLastChildNode();
661 * Used for drawing of Trees.
663 final public float getXcoord() {
667 final public float getXSecondary() {
672 * Used for drawing of Trees.
674 final public float getYcoord() {
678 final public float getYSecondary() {
683 final public int hashCode() {
684 final NodeData data = getNodeData();
685 if ( ( getName().length() < 1 ) && !data.isHasSequence() && !data.isHasTaxonomy() ) {
686 return super.hashCode();
688 int result = getName().hashCode();
689 if ( data.isHasSequence() ) {
690 result ^= data.getSequence().hashCode();
692 if ( data.isHasTaxonomy() ) {
693 result ^= data.getTaxonomy().hashCode();
698 final private void init() {
699 _descendants = new ArrayList<PhylogenyNode>();
706 * Deletes data of this PhylogenyNode. Links to the other Nodes in the
707 * Phylogeny, the ID and the sum of external nodes are NOT deleted. Field
708 * "_link" (_link to Nodes in other Phylogeny) IS deleted.
710 * @see #getLink() (Last modified: 12/20/03)
712 final public void initializeData() {
717 _distance_parent = PhylogenyNode.DISTANCE_DEFAULT;
725 * Returns whether this PhylogenyNode should be drawn as collapsed.
727 final public boolean isCollapse() {
732 * Returns true if this PhylogenyNode represents a _duplication event, false
735 final public boolean isDuplication() {
736 return getNodeData().isHasEvent() && getNodeData().getEvent().isDuplication();
740 * Checks whether this PhylogenyNode is external (tip).
742 * @return true if this PhylogenyNode is external, false otherwise
744 final public boolean isExternal() {
745 return ( getNumberOfDescendants() < 1 );
751 * @return DOCUMENT ME!
753 final public boolean isFirstChildNode() {
754 if ( isRoot() /* and tree is rooted TODO */) {
755 throw new UnsupportedOperationException( "Cannot determine whether the root is the first child node of its _parent." );
757 return ( getChildNodeIndex() == 0 );
763 * @return DOCUMENT ME!
765 final public boolean isFirstExternalNode() {
766 if ( isInternal() ) {
769 PhylogenyNode node = this;
770 while ( !node.isRoot() ) {
771 if ( !node.isFirstChildNode() ) {
774 node = node.getParent();
780 * Returns whether a _duplication or speciation event has been assigned for
781 * this PhylogenyNode.
783 final public boolean isHasAssignedEvent() {
784 if ( !getNodeData().isHasEvent() ) {
787 if ( ( getNodeData().getEvent() ).isUnassigned() ) {
794 * Checks whether this PhylogenyNode is internal (tip).
796 * @return true if this PhylogenyNode is external, false otherwise
798 final public boolean isInternal() {
799 return ( !isExternal() );
803 * Returns true if this node is the last child node of its _parent.
805 * [last modified June 01, 2005 by CMZ]
807 * @return true if this node is the last child node of its _parent, false
810 final public boolean isLastChildNode() {
811 if ( isRoot() /* and tree is rooted TODO */) {
812 throw new UnsupportedOperationException( "Cannot determine whether the root is the last child node of its _parent." );
814 return ( getChildNodeIndex() == ( getParent().getNumberOfDescendants() - 1 ) );
820 * @return DOCUMENT ME!
822 final public boolean isLastExternalNode() {
823 if ( isInternal() ) {
826 PhylogenyNode node = this;
827 while ( !node.isRoot() ) {
828 if ( !node.isLastChildNode() ) {
831 node = node.getParent();
837 * Checks whether this PhylogenyNode is a root.
839 * @return true if this PhylogenyNode is the root, false otherwise
841 final public boolean isRoot() {
842 return _parent == null;
845 final public boolean isSpeciation() {
846 return getNodeData().isHasEvent() && getNodeData().getEvent().isSpeciation();
849 // ---------------------------------------------------------
851 // ---------------------------------------------------------
852 final public PhylogenyNodeIterator iterateChildNodesForward() {
853 return new ChildNodeIteratorForward( this );
856 // ---------------------------------------------------------
858 // ---------------------------------------------------------
860 * Prints to the console the subtree originating from this PhylogenyNode in
863 public void preorderPrint() {
864 System.out.println( this + "\n" );
865 if ( isInternal() ) {
866 for( int i = 0; i < getNumberOfDescendants(); ++i ) {
867 getChildNode( i ).preorderPrint();
872 final public void removeChildNode( final int i ) {
873 if ( isExternal() ) {
874 throw new UnsupportedOperationException( "cannot get the child node for a external node." );
876 if ( ( i >= getNumberOfDescendants() ) || ( i < 0 ) ) {
877 throw new IllegalArgumentException( "attempt to get child node " + i + " of a node with "
878 + getNumberOfDescendants() + " child nodes." );
880 getDescendants().remove( i );
883 final public void removeChildNode( final PhylogenyNode remove_me ) {
884 removeChildNode( remove_me.getChildNodeIndex() );
887 final public void setBranchData( final BranchData branch_data ) {
888 _branch_data = branch_data;
892 * Sets the first child PhylogenyNode of this PhylogenyNode to n.
894 final public void setChild1( final PhylogenyNode n ) {
895 setChildNode( 0, n );
899 * Sets the second child PhylogenyNode of this PhylogenyNode to n.
901 final public void setChild2( final PhylogenyNode n ) {
902 setChildNode( 1, n );
906 * Inserts PhylogenyNode n at the specified position i into the list of
907 * child nodes. This does not allow null slots in the list of child nodes:
908 * If i is larger than the number of child nodes, n is just added to the
909 * list, not place at index i.
912 * the index of position where to add the child
914 * the PhylogenyNode to add
916 final public void setChildNode( final int i, final PhylogenyNode node ) {
917 node.setParent( this );
918 if ( getNumberOfDescendants() <= i ) {
919 addChildNode( node );
922 getDescendants().set( i, node );
926 final void setChildNodeOnly( final int i, final PhylogenyNode node ) {
927 if ( getNumberOfDescendants() <= i ) {
928 addChildNode( node );
931 getDescendants().set( i, node );
936 * Sets whether this PhylogenyNode should be drawn as collapsed.
938 final public void setCollapse( final boolean b ) {
943 * Sets the length of the branch leading to the _parent of this
944 * PhylogenyNode to double d.
947 final public void setDistanceToParent( final double d ) {
948 _distance_parent = d;
952 * Sets the _indicator value of this PhylogenyNode to i.
954 final public void setIndicator( final byte i ) {
958 // --------------------------------------------------------------------
959 // Adjust methods (related to Phylogeny construction and
960 // Phylogeny modification)
961 // --------------------------------------------------------------------
963 * Sets the indicators of all the children of this PhylogenyNode to zero.
965 final void setIndicatorsToZero() {
966 for( final PreorderTreeIterator it = new PreorderTreeIterator( this ); it.hasNext(); ) {
967 it.next().setIndicator( ( byte ) 0 );
972 * Sets the linked PhylogenyNode of this PhylogenyNode to n. Currently, this
973 * method is only used for the speciation-_duplication assignment
976 final public void setLink( final PhylogenyNode n ) {
981 * Sets the name of this node.
984 final public void setName( final String node_name ) {
985 getNodeData().setNodeName( node_name );
989 * Sets the Id of this PhylogenyNode to i. In most cases, this number
990 * should not be set to values lower than getNodeCount() -- which this method
993 synchronized final protected void setId( final int i ) {
994 if ( i < getNodeCount() ) {
995 throw new IllegalArgumentException( "attempt to set node id to a value less than total node count (thus violating the uniqueness of node ids)" );
1001 * Sets the _parent PhylogenyNode of this PhylogenyNode to n.
1004 final public void setParent( final PhylogenyNode n ) {
1009 * Sets the total number of external Nodes originating from this
1010 * PhylogenyNode to i (int).
1012 final public void setSumExtNodes( final int i ) {
1014 throw new IllegalArgumentException( "attempt to set sum of external nodes to less than one" );
1020 * Used for drawing of Trees.
1022 final public void setXcoord( final float x ) {
1026 final public void setXSecondary( final float x_secondary ) {
1027 _x_secondary = x_secondary;
1032 * Used for drawing of Trees.
1034 final public void setYcoord( final float y ) {
1038 final public void setYSecondary( final float y_secondary ) {
1039 _y_secondary = y_secondary;
1042 // ---------------------------------------------------------
1043 // Writing of Nodes to Strings
1044 // ---------------------------------------------------------
1045 final public String toNewHampshire( final boolean simple_nh, final boolean write_distance_to_parent ) {
1046 final StringBuilder sb = new StringBuilder();
1048 if ( !ForesterUtil.isEmpty( getName() ) ) {
1051 else if ( getNodeData().isHasTaxonomy() ) {
1052 if ( !ForesterUtil.isEmpty( getNodeData().getTaxonomy().getTaxonomyCode() ) ) {
1053 data = getNodeData().getTaxonomy().getTaxonomyCode();
1055 else if ( !ForesterUtil.isEmpty( getNodeData().getTaxonomy().getScientificName() ) ) {
1056 data = getNodeData().getTaxonomy().getScientificName();
1058 else if ( !ForesterUtil.isEmpty( getNodeData().getTaxonomy().getCommonName() ) ) {
1059 data = getNodeData().getTaxonomy().getCommonName();
1061 else if ( getNodeData().getTaxonomy().getTaxonomyCode() != null ) {
1062 data = getNodeData().getTaxonomy().getTaxonomyCode();
1065 else if ( getNodeData().isHasSequence() ) {
1066 if ( !ForesterUtil.isEmpty( getNodeData().getSequence().getName() ) ) {
1067 data = getNodeData().getSequence().getName();
1070 if ( data.length() > 0 ) {
1071 data = ForesterUtil.replaceIllegalNhCharacters( data );
1072 if ( simple_nh && ( data.length() > 10 ) ) {
1073 data = data.substring( 0, 11 );
1075 if ( ForesterUtil.isContainsParanthesesableNhCharacter( data ) ) {
1084 if ( ( getDistanceToParent() != PhylogenyNode.DISTANCE_DEFAULT ) && write_distance_to_parent ) {
1086 sb.append( getDistanceToParent() );
1088 return sb.toString();
1092 * Converts this PhylogenyNode to a New Hampshire X (NHX) String
1095 final public String toNewHampshireX() {
1096 final StringBuffer sb = new StringBuffer();
1097 final StringBuffer s_nhx = new StringBuffer();
1098 if ( !ForesterUtil.isEmpty( getName() ) ) {
1099 final String name = ForesterUtil.replaceIllegalNhCharacters( getName() );
1100 if ( ForesterUtil.isContainsParanthesesableNhCharacter( name ) ) {
1109 if ( getDistanceToParent() != PhylogenyNode.DISTANCE_DEFAULT ) {
1111 sb.append( getDistanceToParent() );
1113 if ( getNodeDataDirectly() != null ) {
1114 s_nhx.append( getNodeDataDirectly().toNHX() );
1116 if ( getBranchDataDirectly() != null ) {
1117 s_nhx.append( getBranchDataDirectly().toNHX() );
1119 if ( s_nhx.length() > 0 ) {
1120 sb.append( "[&&NHX" );
1124 return sb.toString();
1128 final public String toString() {
1129 final StringBuilder sb = new StringBuilder();
1130 if ( !ForesterUtil.isEmpty( getName() ) ) {
1131 sb.append( getName() );
1135 sb.append( getId() );
1137 return sb.toString();
1141 * Decreases the total number of all Nodes created so far by one.
1143 final static synchronized void decreaseNodeCount() {
1144 --PhylogenyNode._node_count;
1148 * Returns the total number of all Nodes created so far.
1150 * @return total number of Nodes (int)
1152 synchronized final public static int getNodeCount() {
1153 return PhylogenyNode._node_count;
1157 * Increases the total number of all Nodes created so far by one.
1159 synchronized final private static void increaseNodeCount() {
1160 ++PhylogenyNode._node_count;
1164 * Sets the total number of all Nodes created so far to i (int).
1166 synchronized final static void setNodeCount( final int i ) {
1167 PhylogenyNode._node_count = i;
1170 public static PhylogenyNode createInstanceFromNhxString( final String nhx ) throws NHXFormatException {
1171 return new PhylogenyNode( nhx, ForesterUtil.TAXONOMY_EXTRACTION.NO, false );
1174 public static PhylogenyNode createInstanceFromNhxString( final String nhx,
1175 final ForesterUtil.TAXONOMY_EXTRACTION taxonomy_extraction )
1176 throws NHXFormatException {
1177 return new PhylogenyNode( nhx, taxonomy_extraction, false );
1180 public static PhylogenyNode createInstanceFromNhxString( final String nhx,
1181 final ForesterUtil.TAXONOMY_EXTRACTION taxonomy_extraction,
1182 final boolean replace_underscores )
1183 throws NHXFormatException {
1184 return new PhylogenyNode( nhx, taxonomy_extraction, replace_underscores );
1187 private PhylogenyNode( final String nhx,
1188 final ForesterUtil.TAXONOMY_EXTRACTION taxonomy_extraction,
1189 final boolean replace_underscores ) throws NHXFormatException {
1191 NHXParser.parseNHX( nhx, this, taxonomy_extraction, replace_underscores );
1192 setId( PhylogenyNode.getNodeCount() );
1193 PhylogenyNode.increaseNodeCount();
1194 setSumExtNodes( 1 ); // For ext node, this number is 1 (not 0!!)