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.Arrays;
33 import java.util.Collection;
34 import java.util.HashMap;
35 import java.util.Iterator;
36 import java.util.List;
38 import java.util.NoSuchElementException;
39 import java.util.Vector;
41 import org.forester.io.writers.PhylogenyWriter;
42 import org.forester.phylogeny.data.BranchData;
43 import org.forester.phylogeny.data.Confidence;
44 import org.forester.phylogeny.data.Identifier;
45 import org.forester.phylogeny.data.Sequence;
46 import org.forester.phylogeny.data.SequenceRelation;
47 import org.forester.phylogeny.data.SequenceRelation.SEQUENCE_RELATION_TYPE;
48 import org.forester.phylogeny.iterators.ExternalForwardIterator;
49 import org.forester.phylogeny.iterators.LevelOrderTreeIterator;
50 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
51 import org.forester.phylogeny.iterators.PostorderTreeIterator;
52 import org.forester.phylogeny.iterators.PreorderTreeIterator;
53 import org.forester.util.FailedConditionCheckException;
54 import org.forester.util.ForesterUtil;
56 public class Phylogeny {
58 public final static boolean ALLOW_MULTIPLE_PARENTS_DEFAULT = false;
59 private PhylogenyNode _root;
60 private boolean _rooted;
61 private boolean _allow_multiple_parents;
64 private String _description;
65 private String _distance_unit;
66 private Confidence _confidence;
67 private Identifier _identifier;
68 private boolean _rerootable;
69 private HashMap<Integer, PhylogenyNode> _idhash;
70 private List<PhylogenyNode> _external_nodes_set;
71 private Collection<Sequence> _sequenceRelationQueries;
72 private Collection<SequenceRelation.SEQUENCE_RELATION_TYPE> _relevant_sequence_relation_types;
75 * Default Phylogeny constructor. Constructs an empty Phylogeny.
82 * Adds this Phylogeny to the list of child nodes of PhylogenyNode parent
83 * and sets the parent of this to parent.
86 * the PhylogenyNode to add
88 public void addAsChild( final PhylogenyNode parent ) {
90 throw new IllegalArgumentException( "Attempt to add an empty tree." );
93 throw new IllegalArgumentException( "Attempt to add an unrooted tree." );
95 parent.addAsChild( getRoot() );
96 externalNodesHaveChanged();
99 public void addAsSibling( final PhylogenyNode sibling ) {
101 throw new IllegalArgumentException( "Attempt to add an empty tree." );
104 throw new IllegalArgumentException( "Attempt to add an unrooted tree." );
106 final int sibling_index = sibling.getChildNodeIndex();
107 final PhylogenyNode new_node = new PhylogenyNode();
108 final PhylogenyNode sibling_parent = sibling.getParent();
109 new_node.setChild1( sibling );
110 new_node.setChild2( getRoot() );
111 new_node.setParent( sibling_parent );
112 sibling.setParent( new_node );
113 sibling_parent.setChildNode( sibling_index, new_node );
114 final double new_dist = sibling.getDistanceToParent() == PhylogenyNode.DISTANCE_DEFAULT ? PhylogenyNode.DISTANCE_DEFAULT
115 : sibling.getDistanceToParent() / 2;
116 new_node.setDistanceToParent( new_dist );
117 sibling.setDistanceToParent( new_dist );
118 externalNodesHaveChanged();
122 * This calculates the height of the subtree emanating at n for rooted,
123 * tree-shaped phylogenies
126 * the root-node of a subtree
127 * @return the height of the subtree emanating at n
129 public double calculateSubtreeHeight( final PhylogenyNode n ) {
130 if ( n.isExternal() || n.isCollapse() ) {
131 return ForesterUtil.isLargerOrEqualToZero( n.getDistanceToParent() );
134 double max = -Double.MAX_VALUE;
135 for( int i = 0; i < n.getNumberOfDescendants(); ++i ) {
136 final double l = calculateSubtreeHeight( n.getChildNode( i ) );
141 return max + ForesterUtil.isLargerOrEqualToZero( n.getDistanceToParent() );
146 * Returns a deep copy of this Phylogeny.
148 * (The resulting Phylogeny has its references in the external nodes
149 * corrected, if they are lacking/obsolete in this.)
151 public Phylogeny copy() {
152 return copy( _root );
156 * Returns a shallow copy of this Phylogeny.
158 * (The resulting Phylogeny has its references in the external nodes
159 * corrected, if they are lacking/obsolete in this.)
161 public Phylogeny copyShallow() {
162 return copyShallow( _root );
165 public Phylogeny copyShallow( final PhylogenyNode source ) {
166 final Phylogeny tree = new Phylogeny();
171 tree._rooted = _rooted;
173 tree._description = _description;
175 tree._rerootable = _rerootable;
176 tree._distance_unit = _distance_unit;
177 tree._confidence = _confidence;
178 tree._identifier = _identifier;
179 tree.setAllowMultipleParents( isAllowMultipleParents() );
180 tree._root = PhylogenyMethods.copySubTreeShallow( source );
185 * Returns a deep copy of this Phylogeny.
187 * (The resulting Phylogeny has its references in the external nodes
188 * corrected, if they are lacking/obsolete in this.)
190 public Phylogeny copy( final PhylogenyNode source ) {
191 final Phylogeny tree = new Phylogeny();
196 tree._rooted = _rooted;
197 tree._name = new String( _name );
198 tree._description = new String( _description );
199 tree._type = new String( _type );
200 tree._rerootable = _rerootable;
201 tree._distance_unit = new String( _distance_unit );
202 if ( _confidence != null ) {
203 tree._confidence = ( Confidence ) _confidence.copy();
205 if ( _identifier != null ) {
206 tree._identifier = ( Identifier ) _identifier.copy();
208 tree.setAllowMultipleParents( isAllowMultipleParents() );
209 tree._root = PhylogenyMethods.copySubTree( source );
214 * Need the delete and/or rehash _idhash (not done automatically
215 * to allow client multiple deletions in linear time).
216 * Need to call 'recalculateNumberOfExternalDescendants(boolean)' after this
217 * if tree is to be displayed.
219 * @param remove_us the parent node of the subtree to be deleted
221 public void deleteSubtree( final PhylogenyNode remove_us, final boolean collapse_resulting_node_with_one_desc ) {
225 if ( remove_us.isRoot() ) {
229 if ( !collapse_resulting_node_with_one_desc ) {
230 remove_us.getParent().removeChildNode( remove_us );
233 final PhylogenyNode removed_node = remove_us;
234 final PhylogenyNode p = remove_us.getParent();
236 if ( p.getNumberOfDescendants() == 2 ) {
237 if ( removed_node.isFirstChildNode() ) {
238 setRoot( getRoot().getChildNode( 1 ) );
239 getRoot().setParent( null );
242 setRoot( getRoot().getChildNode( 0 ) );
243 getRoot().setParent( null );
247 p.removeChildNode( removed_node.getChildNodeIndex() );
251 final PhylogenyNode pp = removed_node.getParent().getParent();
252 if ( p.getNumberOfDescendants() == 2 ) {
253 final int pi = p.getChildNodeIndex();
254 if ( removed_node.isFirstChildNode() ) {
255 p.getChildNode( 1 ).setDistanceToParent( PhylogenyMethods.addPhylogenyDistances( p
256 .getDistanceToParent(), p.getChildNode( 1 ).getDistanceToParent() ) );
257 pp.setChildNode( pi, p.getChildNode( 1 ) );
260 p.getChildNode( 0 ).setDistanceToParent( PhylogenyMethods.addPhylogenyDistances( p
261 .getDistanceToParent(), p.getChildNode( 0 ).getDistanceToParent() ) );
262 pp.setChildNode( pi, p.getChildNode( 0 ) );
266 p.removeChildNode( removed_node.getChildNodeIndex() );
270 remove_us.setParent( null );
272 externalNodesHaveChanged();
275 public void externalNodesHaveChanged() {
276 _external_nodes_set = null;
279 public String[] getAllExternalNodeNames() {
284 final String[] names = new String[ getNumberOfExternalNodes() ];
285 for( final PhylogenyNodeIterator iter = iteratorExternalForward(); iter.hasNext(); ) {
286 names[ i++ ] = new String( iter.next().getName() );
291 public Confidence getConfidence() {
295 public String getDescription() {
299 public String getDistanceUnit() {
300 return _distance_unit;
305 * Warning. The order of the returned nodes is random
306 * -- and hence cannot be relied on.
308 * @return Unordered set of PhylogenyNode
310 public List<PhylogenyNode> getExternalNodes() {
311 if ( _external_nodes_set == null ) {
312 _external_nodes_set = new ArrayList<PhylogenyNode>();
313 for( final PhylogenyNodeIterator it = iteratorPostorder(); it.hasNext(); ) {
314 final PhylogenyNode n = it.next();
315 if ( n.isExternal() ) {
316 _external_nodes_set.add( n );
320 return _external_nodes_set;
324 * Returns the number of duplications of this Phylogeny (int). A return
325 * value of -1 indicates that the number of duplications is unknown.
327 // public int getNumberOfDuplications() {
328 // return _number_of_duplications;
329 // } // getNumberOfDuplications()
331 * Sets the number of duplications of this Phylogeny (int). A value of -1
332 * indicates that the number of duplications is unknown.
335 * set to true for clean NH format
337 // public void setNumberOfDuplications( int i ) {
339 // _number_of_duplications = -1;
342 // _number_of_duplications = i;
344 // } // setNumberOfDuplications( int )
346 * Returns the first external PhylogenyNode.
348 public PhylogenyNode getFirstExternalNode() {
350 throw new FailedConditionCheckException( "attempt to obtain first external node of empty phylogeney" );
352 PhylogenyNode node = getRoot();
353 while ( node.isInternal() ) {
354 node = node.getFirstChildNode();
360 * This calculates the height for rooted, tree-shaped phylogenies. The
361 * height is the longest distance from the root to an external node. Please
362 * note. Child nodes of collapsed nodes are ignored -- which is useful for
363 * display purposes but might be misleading for other applications.
365 * @return the height for rooted, tree-shaped phylogenies
367 public double getHeight() {
371 return calculateSubtreeHeight( getRoot() );
374 public Identifier getIdentifier() {
378 // ---------------------------------------------------------
379 // Modification of Phylogeny topology and Phylogeny appearance
380 // ---------------------------------------------------------
381 private HashMap<Integer, PhylogenyNode> getIdHash() {
386 * Returns the name of this Phylogeny.
388 public String getName() {
393 * Finds the PhylogenyNode of this Phylogeny which has a matching ID number.
394 * Takes O(n) time. After method hashIDs() has been called it runs in
398 * ID number (int) of the PhylogenyNode to find
399 * @return PhylogenyNode with matching ID, null if not found
401 public PhylogenyNode getNode( final int id ) throws NoSuchElementException {
403 throw new NoSuchElementException( "attempt to get node in an empty phylogeny" );
405 if ( _idhash != null ) {
406 return _idhash.get( id );
409 for( final PhylogenyNodeIterator iter = iteratorPreorder(); iter.hasNext(); ) {
410 final PhylogenyNode node = iter.next();
411 if ( node.getId() == id ) {
420 * Returns a PhylogenyNode of this Phylogeny which has a matching name.
421 * Throws an Exception if seqname is not present in this or not unique.
424 * name (String) of PhylogenyNode to find
425 * @return PhylogenyNode with matchin name
427 public PhylogenyNode getNode( final String name ) {
431 final List<PhylogenyNode> nodes = getNodes( name );
432 if ( ( nodes == null ) || ( nodes.size() < 1 ) ) {
433 throw new IllegalArgumentException( "node named [" + name + "] not found" );
435 if ( nodes.size() > 1 ) {
436 throw new IllegalArgumentException( "node named [" + name + "] not unique" );
438 return nodes.get( 0 );
442 * Return Node by TaxonomyId Olivier CHABROL :
443 * olivier.chabrol@univ-provence.fr
446 * search taxonomy identifier
448 * sublist node to search
449 * @return List node with the same taxonomy identifier
451 private List<PhylogenyNode> getNodeByTaxonomyID( final String taxonomyID, final List<PhylogenyNode> nodes ) {
452 final List<PhylogenyNode> retour = new ArrayList<PhylogenyNode>();
453 for( final PhylogenyNode node : nodes ) {
454 if ( taxonomyID.equals( PhylogenyMethods.getTaxonomyIdentifier( node ) ) ) {
462 * Returns a List with references to all Nodes of this Phylogeny which have
466 * name (String) of Nodes to find
467 * @return Vector of references to Nodes of this Phylogeny with matching
469 * @see #getNodesWithMatchingSpecies(String)
471 public List<PhylogenyNode> getNodes( final String name ) {
475 final List<PhylogenyNode> nodes = new ArrayList<PhylogenyNode>();
476 for( final PhylogenyNodeIterator iter = iteratorPreorder(); iter.hasNext(); ) {
477 final PhylogenyNode n = iter.next();
478 if ( n.getName().equals( name ) ) {
485 public List<PhylogenyNode> getNodesViaSequenceName( final String seq_name ) {
489 final List<PhylogenyNode> nodes = new ArrayList<PhylogenyNode>();
490 for( final PhylogenyNodeIterator iter = iteratorPreorder(); iter.hasNext(); ) {
491 final PhylogenyNode n = iter.next();
492 if ( n.getNodeData().isHasSequence() && n.getNodeData().getSequence().getName().equals( seq_name ) ) {
499 public List<PhylogenyNode> getNodesViaTaxonomyCode( final String taxonomy_code ) {
503 final List<PhylogenyNode> nodes = new ArrayList<PhylogenyNode>();
504 for( final PhylogenyNodeIterator iter = iteratorPreorder(); iter.hasNext(); ) {
505 final PhylogenyNode n = iter.next();
506 if ( n.getNodeData().isHasTaxonomy()
507 && n.getNodeData().getTaxonomy().getTaxonomyCode().equals( taxonomy_code ) ) {
515 * Returns a Vector with references to all Nodes of this Phylogeny which
516 * have a matching species name.
519 * species name (String) of Nodes to find
520 * @return Vector of references to Nodes of this Phylogeny with matching
522 * @see #getNodes(String)
524 public List<PhylogenyNode> getNodesWithMatchingSpecies( final String specname ) {
528 final List<PhylogenyNode> nodes = new ArrayList<PhylogenyNode>();
529 for( final PhylogenyNodeIterator iter = iteratorPreorder(); iter.hasNext(); ) {
530 final PhylogenyNode n = iter.next();
531 if ( PhylogenyMethods.getSpecies( n ).equals( specname ) ) {
538 public PhylogenyNode getNodeViaSequenceName( final String seq_name ) {
542 final List<PhylogenyNode> nodes = getNodesViaSequenceName( seq_name );
543 if ( ( nodes == null ) || ( nodes.size() < 1 ) ) {
544 throw new IllegalArgumentException( "node with sequence named [" + seq_name + "] not found" );
546 if ( nodes.size() > 1 ) {
547 throw new IllegalArgumentException( "node with sequence named [" + seq_name + "] not unique" );
549 return nodes.get( 0 );
552 public PhylogenyNode getNodeViaTaxonomyCode( final String taxonomy_code ) {
556 final List<PhylogenyNode> nodes = getNodesViaTaxonomyCode( taxonomy_code );
557 if ( ( nodes == null ) || ( nodes.size() < 1 ) ) {
558 throw new IllegalArgumentException( "node with taxonomy code \"" + taxonomy_code + "\" not found" );
560 if ( nodes.size() > 1 ) {
561 throw new IllegalArgumentException( "node with taxonomy code \"" + taxonomy_code + "\" not unique" );
563 return nodes.get( 0 );
567 * This is time-inefficient since it runs a iterator each time it is called.
570 public int getNodeCount() {
575 for( final PhylogenyNodeIterator it = iteratorPreorder(); it.hasNext(); it.next() ) {
581 public int getNumberOfBranches() {
586 for( final PhylogenyNodeIterator iter = iteratorPreorder(); iter.hasNext(); iter.next() ) {
596 * Returns the sum of external Nodes of this Phylogeny (int).
598 public int getNumberOfExternalNodes() {
602 return getExternalNodes().size();
606 * Returns all paralogs of the external PhylogenyNode n of this Phylogeny.
607 * paralog are returned as List of node references.
609 * PRECONDITION: This tree must be binary and rooted, and speciation -
610 * duplication need to be assigned for each of its internal Nodes.
612 * Returns null if this Phylogeny is empty or if n is internal.
614 * (Last modified: 11/22/00) Olivier CHABROL :
615 * olivier.chabrol@univ-provence.fr
618 * external PhylogenyNode whose orthologs are to be returned
619 * @return Vector of references to all orthologous Nodes of PhylogenyNode n
620 * of this Phylogeny, null if this Phylogeny is empty or if n is
623 public List<PhylogenyNode> getParalogousNodes( final PhylogenyNode n, final String[] taxonomyCodeRange ) {
624 PhylogenyNode node = n;
625 PhylogenyNode prev = null;
626 final List<PhylogenyNode> v = new ArrayList<PhylogenyNode>();
627 final Map<PhylogenyNode, List<String>> map = new HashMap<PhylogenyNode, List<String>>();
628 getTaxonomyMap( getRoot(), map );
629 if ( !node.isExternal() || isEmpty() ) {
632 final String searchNodeSpeciesId = PhylogenyMethods.getTaxonomyIdentifier( n );
633 if ( !node.isExternal() || isEmpty() ) {
636 List<String> taxIdList = null;
637 final List<String> taxonomyCodeRangeList = Arrays.asList( taxonomyCodeRange );
638 while ( !node.isRoot() ) {
640 node = node.getParent();
641 taxIdList = map.get( node );
642 if ( node.isDuplication() && isContains( taxIdList, taxonomyCodeRangeList ) ) {
643 if ( node.getChildNode1() == prev ) {
644 v.addAll( getNodeByTaxonomyID( searchNodeSpeciesId, node.getChildNode2()
645 .getAllExternalDescendants() ) );
648 v.addAll( getNodeByTaxonomyID( searchNodeSpeciesId, node.getChildNode1()
649 .getAllExternalDescendants() ) );
656 public Collection<SequenceRelation.SEQUENCE_RELATION_TYPE> getRelevantSequenceRelationTypes() {
657 if ( _relevant_sequence_relation_types == null ) {
658 _relevant_sequence_relation_types = new Vector<SEQUENCE_RELATION_TYPE>();
660 return _relevant_sequence_relation_types;
664 * Returns the root PhylogenyNode of this Phylogeny.
666 public PhylogenyNode getRoot() {
670 public Collection<Sequence> getSequenceRelationQueries() {
671 return _sequenceRelationQueries;
675 * List all species contains in all leaf under a node Olivier CHABROL :
676 * olivier.chabrol@univ-provence.fr
679 * PhylogenyNode whose sub node species are returned
680 * @return species contains in all leaf under the param node
682 private List<String> getSubNodeTaxonomy( final PhylogenyNode node ) {
683 final List<String> taxonomyList = new ArrayList<String>();
684 final List<PhylogenyNode> childs = node.getAllExternalDescendants();
685 String speciesId = null;
686 for( final PhylogenyNode phylogenyNode : childs ) {
687 // taxId = new Long(phylogenyNode.getTaxonomyID());
688 speciesId = PhylogenyMethods.getTaxonomyIdentifier( phylogenyNode );
689 if ( !taxonomyList.contains( speciesId ) ) {
690 taxonomyList.add( speciesId );
697 * Create a map [<PhylogenyNode, List<String>], the list contains the
698 * species contains in all leaf under phylogeny node Olivier CHABROL :
699 * olivier.chabrol@univ-provence.fr
706 private void getTaxonomyMap( final PhylogenyNode node, final Map<PhylogenyNode, List<String>> map ) {
708 if ( node.isExternal() ) {
711 map.put( node, getSubNodeTaxonomy( node ) );
712 getTaxonomyMap( node.getChildNode1(), map );
713 getTaxonomyMap( node.getChildNode2(), map );
716 public String getType() {
721 * Hashes the ID number of each PhylogenyNode of this Phylogeny to its
722 * corresponding PhylogenyNode, in order to make method getNode( id ) run in
723 * constant time. Important: The user is responsible for calling this method
724 * (again) after this Phylogeny has been changed/created/renumbered.
726 public void hashIDs() {
730 setIdHash( new HashMap<Integer, PhylogenyNode>() );
731 for( final PhylogenyNodeIterator iter = iteratorPreorder(); iter.hasNext(); ) {
732 final PhylogenyNode node = iter.next();
733 getIdHash().put( node.getId(), node );
738 * Deletes this Phylogeny.
751 setAllowMultipleParents( Phylogeny.ALLOW_MULTIPLE_PARENTS_DEFAULT );
754 private boolean isAllowMultipleParents() {
755 return _allow_multiple_parents;
759 * Returns whether this is a completely binary tree (i.e. all internal nodes
763 public boolean isCompletelyBinary() {
767 for( final PhylogenyNodeIterator iter = iteratorPreorder(); iter.hasNext(); ) {
768 final PhylogenyNode node = iter.next();
769 if ( node.isInternal() && ( node.getNumberOfDescendants() != 2 ) ) {
777 * Util method to check if all element of a list is contains in the
778 * rangeList. Olivier CHABROL : olivier.chabrol@univ-provence.fr
783 * the range list to compare
784 * @return <code>true</code> if all param list element are contains in param
785 * rangeList, <code>false</code> otherwise.
787 private boolean isContains( final List<String> list, final List<String> rangeList ) {
788 if ( list.size() > rangeList.size() ) {
792 for( final Iterator<String> iterator = list.iterator(); iterator.hasNext(); ) {
794 if ( !rangeList.contains( l ) ) {
802 * Checks whether a Phylogeny object is deleted (or empty).
804 * @return true if the tree is deleted (or empty), false otherwise
806 public boolean isEmpty() {
807 return ( getRoot() == null );
810 public boolean isRerootable() {
815 * Returns true is this Phylogeny is rooted.
817 public boolean isRooted() {
821 public boolean isTree() {
825 public PhylogenyNodeIterator iteratorExternalForward() {
826 return new ExternalForwardIterator( this );
829 public PhylogenyNodeIterator iteratorLevelOrder() {
830 return new LevelOrderTreeIterator( this );
833 public PhylogenyNodeIterator iteratorPostorder() {
834 return new PostorderTreeIterator( this );
837 public PhylogenyNodeIterator iteratorPreorder() {
838 return new PreorderTreeIterator( this );
842 * Resets the ID numbers of the nodes of this Phylogeny in level order,
843 * starting with start_label (for the root). <br>
844 * WARNING. After this method has been called, node IDs are no longer
847 public void levelOrderReID() {
853 for( final PhylogenyNodeIterator it = iteratorPreorder(); it.hasNext(); ) {
854 final PhylogenyNode node = it.next();
855 if ( node.isRoot() ) {
856 node.setId( PhylogenyNode.getNodeCount() );
859 node.setId( node.getParent().getId() + 1 );
860 if ( node.getId() > max ) {
865 PhylogenyNode.setNodeCount( max + 1 );
869 * Arranges the order of childern for each node of this Phylogeny in such a
870 * way that either the branch with more children is on top (right) or on
871 * bottom (left), dependent on the value of boolean order.
874 * decides in which direction to order
876 public void orderAppearance( final boolean order ) throws RuntimeException {
878 throw new FailedConditionCheckException( "Attempt to order appearance on phylogeny which is not tree-like." );
883 orderAppearanceHelper( getRoot(), order );
886 // Helper method for "orderAppearance(boolean)".
887 // Traverses this Phylogeny recusively.
888 private void orderAppearanceHelper( final PhylogenyNode n, final boolean order ) {
889 if ( n.isExternal() ) {
893 PhylogenyNode temp = null;
895 if ( ( n.getNumberOfDescendants() == 2 )
896 && ( n.getChildNode1().getNumberOfExternalNodes() != n.getChildNode2().getNumberOfExternalNodes() )
897 && ( ( n.getChildNode1().getNumberOfExternalNodes() < n.getChildNode2().getNumberOfExternalNodes() ) == order ) ) {
898 temp = n.getChildNode1();
899 n.setChild1( n.getChildNode2() );
902 for( int i = 0; i < n.getNumberOfDescendants(); ++i ) {
903 orderAppearanceHelper( n.getChildNode( i ), order );
908 public void preOrderReId() {
913 int i = PhylogenyNode.getNodeCount();
914 for( final PhylogenyNodeIterator it = iteratorPreorder(); it.hasNext(); ) {
915 it.next().setId( i++ );
917 PhylogenyNode.setNodeCount( i );
921 * Prints descriptions of all external Nodes of this Phylogeny to
924 public void printExtNodes() {
928 for( final PhylogenyNodeIterator iter = iteratorExternalForward(); iter.hasNext(); ) {
929 System.out.println( iter.next() + "\n" );
934 * (Re)counts the number of children for each PhylogenyNode of this
935 * Phylogeny. As an example, this method needs to be called after a
936 * Phylogeny has been reRooted and it is to be displayed.
938 * @param consider_collapsed_nodes
939 * set to true to take into account collapsed nodes (collapsed
940 * nodes have 1 child).
942 public void recalculateNumberOfExternalDescendants( final boolean consider_collapsed_nodes ) {
946 for( final PhylogenyNodeIterator iter = iteratorPostorder(); iter.hasNext(); ) {
947 final PhylogenyNode node = iter.next();
948 if ( node.isExternal() || ( consider_collapsed_nodes && node.isCollapse() ) ) {
949 node.setSumExtNodes( 1 );
953 for( int i = 0; i < node.getNumberOfDescendants(); ++i ) {
954 sum += node.getChildNode( i ).getNumberOfExternalNodes();
956 node.setSumExtNodes( sum );
962 * Places the root of this Phylogeny on the parent branch of the
963 * PhylogenyNode with a corresponding ID. The new root is always placed on
964 * the middle of the branch. If the resulting reRooted Phylogeny is to be
965 * used any further, in most cases the following methods have to be called
966 * on the resulting Phylogeny:
968 * <li>recalculateNumberOfExternalDescendants(boolean)
969 * <li>recalculateAndReset()
972 * ID (int) of PhylogenyNode of this Phylogeny
974 public void reRoot( final int id ) {
975 reRoot( getNode( id ) );
979 * Places the root of this Phylogeny on Branch b. The new root is always
980 * placed on the middle of the branch b.
983 public void reRoot( final PhylogenyBranch b ) {
984 final PhylogenyNode n1 = b.getFirstNode();
985 final PhylogenyNode n2 = b.getSecondNode();
986 if ( n1.isExternal() ) {
989 else if ( n2.isExternal() ) {
992 else if ( ( n2 == n1.getChildNode1() ) || ( n2 == n1.getChildNode2() ) ) {
995 else if ( ( n1 == n2.getChildNode1() ) || ( n1 == n2.getChildNode2() ) ) {
998 else if ( ( n1.getParent() != null ) && n1.getParent().isRoot()
999 && ( ( n1.getParent().getChildNode1() == n2 ) || ( n1.getParent().getChildNode2() == n2 ) ) ) {
1003 throw new IllegalArgumentException( "reRoot( Branch b ): b is not a branch." );
1008 * Places the root of this Phylogeny on the parent branch PhylogenyNode n.
1009 * The new root is always placed on the middle of the branch.
1011 * If the resulting reRooted Phylogeny is to be used any further, in most
1012 * cases the following three methods have to be called on the resulting
1015 * <li>recalculateNumberOfExternalDescendants(boolean) <li>recalculateAndReset()
1018 * (Last modified: 10/01/01)
1021 * PhylogenyNode of this Phylogeny\
1023 public void reRoot( final PhylogenyNode n ) {
1027 public void reRoot( final PhylogenyNode n, final double distance_n_to_parent ) {
1028 if ( isEmpty() || ( getNumberOfExternalNodes() < 2 ) ) {
1035 else if ( n.getParent().isRoot() ) {
1036 if ( ( n.getParent().getNumberOfDescendants() == 2 ) && ( distance_n_to_parent >= 0 ) ) {
1037 final double d = n.getParent().getChildNode1().getDistanceToParent()
1038 + n.getParent().getChildNode2().getDistanceToParent();
1039 PhylogenyNode other;
1040 if ( n.getChildNodeIndex() == 0 ) {
1041 other = n.getParent().getChildNode2();
1044 other = n.getParent().getChildNode1();
1046 n.setDistanceToParent( distance_n_to_parent );
1047 final double dm = d - distance_n_to_parent;
1049 other.setDistanceToParent( dm );
1052 other.setDistanceToParent( 0 );
1055 if ( n.getParent().getNumberOfDescendants() > 2 ) {
1056 final int index = n.getChildNodeIndex();
1057 final double dn = n.getDistanceToParent();
1058 final PhylogenyNode prev_root = getRoot();
1059 prev_root.getDescendants().remove( index );
1060 final PhylogenyNode new_root = new PhylogenyNode();
1061 new_root.setChildNode( 0, n );
1062 new_root.setChildNode( 1, prev_root );
1063 if ( n.getBranchDataDirectly() != null ) {
1064 prev_root.setBranchData( ( BranchData ) n.getBranchDataDirectly().copy() );
1066 setRoot( new_root );
1067 if ( distance_n_to_parent >= 0 ) {
1068 n.setDistanceToParent( distance_n_to_parent );
1069 final double d = dn - distance_n_to_parent;
1071 prev_root.setDistanceToParent( d );
1074 prev_root.setDistanceToParent( 0 );
1079 final double d = dn / 2.0;
1080 n.setDistanceToParent( d );
1081 prev_root.setDistanceToParent( d );
1087 PhylogenyNode a = n;
1088 PhylogenyNode b = null;
1089 PhylogenyNode c = null;
1090 final PhylogenyNode new_root = new PhylogenyNode();
1091 double distance1 = 0.0;
1092 double distance2 = 0.0;
1093 BranchData branch_data_1 = null;
1094 BranchData branch_data_2 = null;
1097 new_root.setChildNode( 0, a );
1098 new_root.setChildNode( 1, b );
1099 distance1 = c.getDistanceToParent();
1100 if ( c.getBranchDataDirectly() != null ) {
1101 branch_data_1 = ( BranchData ) c.getBranchDataDirectly().copy();
1103 c.setDistanceToParent( b.getDistanceToParent() );
1104 if ( b.getBranchDataDirectly() != null ) {
1105 c.setBranchData( ( BranchData ) b.getBranchDataDirectly().copy() );
1107 if ( a.getBranchDataDirectly() != null ) {
1108 b.setBranchData( ( BranchData ) a.getBranchDataDirectly().copy() );
1110 // New root is always placed in the middle of the branch:
1111 if ( a.getDistanceToParent() == PhylogenyNode.DISTANCE_DEFAULT ) {
1112 b.setDistanceToParent( PhylogenyNode.DISTANCE_DEFAULT );
1115 if ( distance_n_to_parent >= 0.0 ) {
1116 final double diff = a.getDistanceToParent() - distance_n_to_parent;
1117 a.setDistanceToParent( distance_n_to_parent );
1118 b.setDistanceToParent( diff >= 0.0 ? diff : 0.0 );
1121 final double d = a.getDistanceToParent() / 2.0;
1122 a.setDistanceToParent( d );
1123 b.setDistanceToParent( d );
1126 b.setChildNodeOnly( a.getChildNodeIndex( b ), c );
1127 // moving to the old root, swapping references:
1128 while ( !c.isRoot() ) {
1132 b.setChildNodeOnly( a.getChildNodeIndex( b ), c );
1134 distance2 = c.getDistanceToParent();
1135 branch_data_2 = c.getBranchDataDirectly();
1136 c.setDistanceToParent( distance1 );
1137 c.setBranchData( branch_data_1 );
1138 distance1 = distance2;
1139 branch_data_1 = branch_data_2;
1141 // removing the old root:
1142 if ( c.getNumberOfDescendants() == 2 ) {
1143 final PhylogenyNode node = c.getChildNode( 1 - b.getChildNodeIndex( c ) );
1144 node.setParent( b );
1145 if ( ( c.getDistanceToParent() == PhylogenyNode.DISTANCE_DEFAULT )
1146 && ( node.getDistanceToParent() == PhylogenyNode.DISTANCE_DEFAULT ) ) {
1147 node.setDistanceToParent( PhylogenyNode.DISTANCE_DEFAULT );
1150 node.setDistanceToParent( ( c.getDistanceToParent() >= 0.0 ? c.getDistanceToParent() : 0.0 )
1151 + ( node.getDistanceToParent() >= 0.0 ? node.getDistanceToParent() : 0.0 ) );
1153 if ( c.getBranchDataDirectly() != null ) {
1154 node.setBranchData( ( BranchData ) c.getBranchDataDirectly().copy() );
1156 for( int i = 0; i < b.getNumberOfDescendants(); ++i ) {
1157 if ( b.getChildNode( i ) == c ) {
1158 b.setChildNodeOnly( i, node );
1165 c.removeChildNode( b.getChildNodeIndex( c ) );
1167 setRoot( new_root );
1172 * Sets all Nodes of this Phylogeny to not-collapsed.
1174 * In most cases methods adjustNodeCount(false) and recalculateAndReset()
1175 * need to be called after this method has been called.
1177 public void setAllNodesToNotCollapse() {
1181 for( final PhylogenyNodeIterator iter = iteratorPreorder(); iter.hasNext(); ) {
1182 final PhylogenyNode node = iter.next();
1183 node.setCollapse( false );
1187 private void setAllowMultipleParents( final boolean allow_multiple_parents ) {
1188 _allow_multiple_parents = allow_multiple_parents;
1191 public void setConfidence( final Confidence confidence ) {
1192 _confidence = confidence;
1195 public void setDescription( final String description ) {
1196 _description = description;
1199 public void setDistanceUnit( final String _distance_unit ) {
1200 this._distance_unit = _distance_unit;
1203 public void setIdentifier( final Identifier identifier ) {
1204 _identifier = identifier;
1207 void setIdHash( final HashMap<Integer, PhylogenyNode> idhash ) {
1212 * Sets the indicators of all Nodes of this Phylogeny to 0.
1214 public void setIndicatorsToZero() {
1218 for( final PhylogenyNodeIterator iter = iteratorPreorder(); iter.hasNext(); ) {
1219 iter.next().setIndicator( ( byte ) 0 );
1221 } // setIndicatorsToZero()
1224 * Sets the name of this Phylogeny to s.
1226 public void setName( final String s ) {
1230 public void setRelevantSequenceRelationTypes( final Collection<SequenceRelation.SEQUENCE_RELATION_TYPE> types ) {
1231 _relevant_sequence_relation_types = types;
1234 public void setRerootable( final boolean rerootable ) {
1235 _rerootable = rerootable;
1238 public void setRoot( final PhylogenyNode n ) {
1240 } // setRoot( PhylogenyNode )
1243 * Sets whether this Phylogeny is rooted or not.
1245 public void setRooted( final boolean b ) {
1247 } // setRooted( boolean )
1249 public void setSequenceRelationQueries( final Collection<Sequence> sequencesByName ) {
1250 _sequenceRelationQueries = sequencesByName;
1253 public void setType( final String type ) {
1258 * Swaps the the two childern of a PhylogenyNode node of this Phylogeny.
1260 * (Last modified: 06/13/01)
1263 * a PhylogenyNode of this Phylogeny
1265 public void swapChildren( final PhylogenyNode node ) throws RuntimeException {
1267 throw new FailedConditionCheckException( "Attempt to swap children on phylogeny which is not tree-like." );
1269 if ( isEmpty() || node.isExternal() || ( node.getNumberOfDescendants() < 2 ) ) {
1272 final PhylogenyNode first = node.getFirstChildNode();
1273 for( int i = 1; i < node.getNumberOfDescendants(); ++i ) {
1274 node.setChildNode( i - 1, node.getChildNode( i ) );
1276 node.setChildNode( node.getNumberOfDescendants() - 1, first );
1277 } // swapChildren( PhylogenyNode )
1279 public String toNewHampshire() {
1280 return toNewHampshire( false );
1283 public String toNewHampshire( final boolean simple_nh ) {
1285 return new PhylogenyWriter().toNewHampshire( this, simple_nh, true ).toString();
1287 catch ( final IOException e ) {
1288 throw new Error( "this should not have happend: " + e.getMessage() );
1292 public String toNewHampshireX() {
1294 return new PhylogenyWriter().toNewHampshireX( this ).toString();
1296 catch ( final IOException e ) {
1297 throw new Error( "this should not have happend: " + e.getMessage() );
1301 public String toNexus() {
1303 return new PhylogenyWriter().toNexus( this ).toString();
1305 catch ( final IOException e ) {
1306 throw new Error( "this should not have happend: " + e.getMessage() );
1310 public String toPhyloXML( final int phyloxml_level ) {
1312 return new PhylogenyWriter().toPhyloXML( this, phyloxml_level ).toString();
1314 catch ( final IOException e ) {
1315 throw new Error( "this should not have happend: " + e.getMessage() );
1319 // ---------------------------------------------------------
1320 // Writing of Phylogeny to Strings
1321 // ---------------------------------------------------------
1323 * Converts this Phylogeny to a New Hampshire X (String) representation.
1325 * @return New Hampshire X (String) representation of this
1326 * @see #toNewHampshireX()
1329 public String toString() {
1330 return toNewHampshireX();
1334 * Removes the root PhylogenyNode this Phylogeny.
1336 public void unRoot() throws RuntimeException {
1338 throw new FailedConditionCheckException( "Attempt to unroot a phylogeny which is not tree-like." );
1343 setIndicatorsToZero();
1344 if ( !isRooted() || ( getNumberOfExternalNodes() <= 1 ) ) {