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.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.parsers.nhx.NHXParser;
42 import org.forester.io.writers.PhylogenyWriter;
43 import org.forester.phylogeny.PhylogenyNode.NH_CONVERSION_SUPPORT_VALUE_STYLE;
44 import org.forester.phylogeny.data.BranchData;
45 import org.forester.phylogeny.data.Confidence;
46 import org.forester.phylogeny.data.Identifier;
47 import org.forester.phylogeny.data.PhylogenyDataUtil;
48 import org.forester.phylogeny.data.Sequence;
49 import org.forester.phylogeny.data.SequenceRelation;
50 import org.forester.phylogeny.data.SequenceRelation.SEQUENCE_RELATION_TYPE;
51 import org.forester.phylogeny.factories.ParserBasedPhylogenyFactory;
52 import org.forester.phylogeny.factories.PhylogenyFactory;
53 import org.forester.phylogeny.iterators.ExternalForwardIterator;
54 import org.forester.phylogeny.iterators.LevelOrderTreeIterator;
55 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
56 import org.forester.phylogeny.iterators.PostorderTreeIterator;
57 import org.forester.phylogeny.iterators.PreorderTreeIterator;
58 import org.forester.util.FailedConditionCheckException;
59 import org.forester.util.ForesterUtil;
61 public class Phylogeny {
63 public final static boolean ALLOW_MULTIPLE_PARENTS_DEFAULT = false;
64 private PhylogenyNode _root;
65 private boolean _rooted;
66 private boolean _allow_multiple_parents;
69 private String _description;
70 private String _distance_unit;
71 private Confidence _confidence;
72 private Identifier _identifier;
73 private boolean _rerootable;
74 private HashMap<Long, PhylogenyNode> _id_to_node_map;
75 private List<PhylogenyNode> _external_nodes_set;
76 private Collection<Sequence> _sequenceRelationQueries;
77 private Collection<SequenceRelation.SEQUENCE_RELATION_TYPE> _relevant_sequence_relation_types;
80 * Default Phylogeny constructor. Constructs an empty Phylogeny.
87 * Adds this Phylogeny to the list of child nodes of PhylogenyNode parent
88 * and sets the parent of this to parent.
91 * the PhylogenyNode to add
93 public void addAsChild( final PhylogenyNode parent ) {
95 throw new IllegalArgumentException( "Attempt to add an empty tree." );
98 throw new IllegalArgumentException( "Attempt to add an unrooted tree." );
100 parent.addAsChild( getRoot() );
101 externalNodesHaveChanged();
104 public void addAsSibling( final PhylogenyNode sibling ) {
106 throw new IllegalArgumentException( "Attempt to add an empty tree." );
109 throw new IllegalArgumentException( "Attempt to add an unrooted tree." );
111 final int sibling_index = sibling.getChildNodeIndex();
112 final PhylogenyNode new_node = new PhylogenyNode();
113 final PhylogenyNode sibling_parent = sibling.getParent();
114 new_node.setChild1( sibling );
115 new_node.setChild2( getRoot() );
116 new_node.setParent( sibling_parent );
117 sibling.setParent( new_node );
118 sibling_parent.setChildNode( sibling_index, new_node );
119 final double new_dist = sibling.getDistanceToParent() == PhylogenyDataUtil.BRANCH_LENGTH_DEFAULT ? PhylogenyDataUtil.BRANCH_LENGTH_DEFAULT
120 : sibling.getDistanceToParent() / 2;
121 new_node.setDistanceToParent( new_dist );
122 sibling.setDistanceToParent( new_dist );
123 externalNodesHaveChanged();
127 * This calculates the height of the subtree emanating at n for rooted,
128 * tree-shaped phylogenies
131 * the root-node of a subtree
132 * @return the height of the subtree emanating at n
134 public double calculateSubtreeHeight( final PhylogenyNode n ) {
135 if ( n.isExternal() || n.isCollapse() ) {
136 return ForesterUtil.isLargerOrEqualToZero( n.getDistanceToParent() );
139 double max = -Double.MAX_VALUE;
140 for( int i = 0; i < n.getNumberOfDescendants(); ++i ) {
141 final double l = calculateSubtreeHeight( n.getChildNode( i ) );
146 return max + ForesterUtil.isLargerOrEqualToZero( n.getDistanceToParent() );
150 public void clearHashIdToNodeMap() {
151 setIdToNodeMap( null );
155 * Returns a deep copy of this Phylogeny.
157 * (The resulting Phylogeny has its references in the external nodes
158 * corrected, if they are lacking/obsolete in this.)
160 public Phylogeny copy() {
161 return copy( _root );
165 * Returns a deep copy of this Phylogeny.
167 * (The resulting Phylogeny has its references in the external nodes
168 * corrected, if they are lacking/obsolete in this.)
170 public Phylogeny copy( final PhylogenyNode source ) {
171 final Phylogeny tree = new Phylogeny();
176 tree._rooted = _rooted;
177 tree._name = new String( _name );
178 tree._description = new String( _description );
179 tree._type = new String( _type );
180 tree._rerootable = _rerootable;
181 tree._distance_unit = new String( _distance_unit );
182 if ( _confidence != null ) {
183 tree._confidence = ( Confidence ) _confidence.copy();
185 if ( _identifier != null ) {
186 tree._identifier = ( Identifier ) _identifier.copy();
188 tree.setAllowMultipleParents( isAllowMultipleParents() );
189 tree._root = PhylogenyMethods.copySubTree( source );
194 * Returns a shallow copy of this Phylogeny.
196 * (The resulting Phylogeny has its references in the external nodes
197 * corrected, if they are lacking/obsolete in this.)
199 public Phylogeny copyShallow() {
200 return copyShallow( _root );
203 public Phylogeny copyShallow( final PhylogenyNode source ) {
204 final Phylogeny tree = new Phylogeny();
209 tree._rooted = _rooted;
211 tree._description = _description;
213 tree._rerootable = _rerootable;
214 tree._distance_unit = _distance_unit;
215 tree._confidence = _confidence;
216 tree._identifier = _identifier;
217 tree.setAllowMultipleParents( isAllowMultipleParents() );
218 tree._root = PhylogenyMethods.copySubTreeShallow( source );
223 * Need to call clearHashIdToNodeMap() afterwards (not done automatically
224 * to allow client multiple deletions in linear time).
225 * Need to call 'recalculateNumberOfExternalDescendants(boolean)' after this
226 * if tree is to be displayed.
228 * @param remove_us the parent node of the subtree to be deleted
230 public void deleteSubtree( final PhylogenyNode remove_us, final boolean collapse_resulting_node_with_one_desc ) {
231 if ( isEmpty() || ( remove_us.isRoot() && ( getNumberOfExternalNodes() != 1 ) ) ) {
234 if ( remove_us.isRoot() && ( getNumberOfExternalNodes() == 1 ) ) {
237 else if ( !collapse_resulting_node_with_one_desc ) {
238 remove_us.getParent().removeChildNode( remove_us );
241 final PhylogenyNode removed_node = remove_us;
242 final PhylogenyNode p = remove_us.getParent();
244 if ( p.getNumberOfDescendants() == 2 ) {
245 if ( removed_node.isFirstChildNode() ) {
246 setRoot( getRoot().getChildNode( 1 ) );
247 getRoot().setParent( null );
250 setRoot( getRoot().getChildNode( 0 ) );
251 getRoot().setParent( null );
255 p.removeChildNode( removed_node.getChildNodeIndex() );
259 final PhylogenyNode pp = removed_node.getParent().getParent();
260 if ( p.getNumberOfDescendants() == 2 ) {
261 final int pi = p.getChildNodeIndex();
262 if ( removed_node.isFirstChildNode() ) {
263 p.getChildNode( 1 ).setDistanceToParent( PhylogenyMethods.addPhylogenyDistances( p
264 .getDistanceToParent(), p.getChildNode( 1 ).getDistanceToParent() ) );
265 pp.setChildNode( pi, p.getChildNode( 1 ) );
268 p.getChildNode( 0 ).setDistanceToParent( PhylogenyMethods.addPhylogenyDistances( p
269 .getDistanceToParent(), p.getChildNode( 0 ).getDistanceToParent() ) );
270 pp.setChildNode( pi, p.getChildNode( 0 ) );
274 p.removeChildNode( removed_node.getChildNodeIndex() );
278 remove_us.removeConnections();
279 externalNodesHaveChanged();
282 public void externalNodesHaveChanged() {
283 _external_nodes_set = null;
286 public String[] getAllExternalNodeNames() {
291 final String[] names = new String[ getNumberOfExternalNodes() ];
292 for( final PhylogenyNodeIterator iter = iteratorExternalForward(); iter.hasNext(); ) {
293 names[ i++ ] = new String( iter.next().getName() );
298 public Confidence getConfidence() {
302 public String getDescription() {
306 public String getDistanceUnit() {
307 return _distance_unit;
310 public final static Phylogeny createInstanceFromNhxString( final String nhx ) throws IOException {
311 final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
312 return factory.create( nhx, new NHXParser() )[ 0 ];
317 * Warning. The order of the returned nodes is random
318 * -- and hence cannot be relied on.
320 * @return Unordered set of PhylogenyNode
322 public List<PhylogenyNode> getExternalNodes() {
323 if ( _external_nodes_set == null ) {
324 _external_nodes_set = new ArrayList<PhylogenyNode>();
325 for( final PhylogenyNodeIterator it = iteratorPostorder(); it.hasNext(); ) {
326 final PhylogenyNode n = it.next();
327 if ( n.isExternal() ) {
328 _external_nodes_set.add( n );
332 return _external_nodes_set;
336 * Returns the number of duplications of this Phylogeny (int). A return
337 * value of -1 indicates that the number of duplications is unknown.
339 // public int getNumberOfDuplications() {
340 // return _number_of_duplications;
341 // } // getNumberOfDuplications()
343 * Sets the number of duplications of this Phylogeny (int). A value of -1
344 * indicates that the number of duplications is unknown.
347 * set to true for clean NH format
349 // public void setNumberOfDuplications( int i ) {
351 // _number_of_duplications = -1;
354 // _number_of_duplications = i;
356 // } // setNumberOfDuplications( int )
358 * Returns the first external PhylogenyNode.
360 public PhylogenyNode getFirstExternalNode() {
362 throw new FailedConditionCheckException( "attempt to obtain first external node of empty phylogeney" );
364 PhylogenyNode node = getRoot();
365 while ( node.isInternal() ) {
366 node = node.getFirstChildNode();
372 * This calculates the height for rooted, tree-shaped phylogenies. The
373 * height is the longest distance from the root to an external node. Please
374 * note. Child nodes of collapsed nodes are ignored -- which is useful for
375 * display purposes but might be misleading for other applications.
377 * @return the height for rooted, tree-shaped phylogenies
379 public double getHeight() {
383 return calculateSubtreeHeight( getRoot() );
386 public Identifier getIdentifier() {
391 * Returns the name of this Phylogeny.
393 public String getName() {
398 * Finds the PhylogenyNode of this Phylogeny which has a matching ID number.
399 * @return PhylogenyNode with matching ID, null if not found
401 public PhylogenyNode getNode( final long id ) throws NoSuchElementException {
403 throw new NoSuchElementException( "attempt to get node in an empty phylogeny" );
405 if ( ( getIdToNodeMap() == null ) || getIdToNodeMap().isEmpty() ) {
408 return getIdToNodeMap().get( id );
412 * Returns a PhylogenyNode of this Phylogeny which has a matching name.
413 * Throws an Exception if seqname is not present in this or not unique.
416 * name (String) of PhylogenyNode to find
417 * @return PhylogenyNode with matchin name
419 public PhylogenyNode getNode( final String name ) {
423 final List<PhylogenyNode> nodes = getNodes( name );
424 if ( ( nodes == null ) || ( nodes.size() < 1 ) ) {
425 throw new IllegalArgumentException( "node named \"" + name + "\" not found" );
427 if ( nodes.size() > 1 ) {
428 throw new IllegalArgumentException( "node named \"" + name + "\" not unique" );
430 return nodes.get( 0 );
434 * This is time-inefficient since it runs a iterator each time it is called.
437 public int getNodeCount() {
442 for( final PhylogenyNodeIterator it = iteratorPreorder(); it.hasNext(); it.next() ) {
449 * Returns a List with references to all Nodes of this Phylogeny which have
453 * name (String) of Nodes to find
454 * @return Vector of references to Nodes of this Phylogeny with matching
456 * @see #getNodesWithMatchingSpecies(String)
458 public List<PhylogenyNode> getNodes( final String name ) {
462 final List<PhylogenyNode> nodes = new ArrayList<PhylogenyNode>();
463 for( final PhylogenyNodeIterator iter = iteratorPreorder(); iter.hasNext(); ) {
464 final PhylogenyNode n = iter.next();
465 if ( n.getName().equals( name ) ) {
472 public List<PhylogenyNode> getNodesViaSequenceName( final String seq_name ) {
476 final List<PhylogenyNode> nodes = new ArrayList<PhylogenyNode>();
477 for( final PhylogenyNodeIterator iter = iteratorPreorder(); iter.hasNext(); ) {
478 final PhylogenyNode n = iter.next();
479 if ( n.getNodeData().isHasSequence() && n.getNodeData().getSequence().getName().equals( seq_name ) ) {
486 public List<PhylogenyNode> getNodesViaSequenceSymbol( final String seq_name ) {
490 final List<PhylogenyNode> nodes = new ArrayList<PhylogenyNode>();
491 for( final PhylogenyNodeIterator iter = iteratorPreorder(); iter.hasNext(); ) {
492 final PhylogenyNode n = iter.next();
493 if ( n.getNodeData().isHasSequence() && n.getNodeData().getSequence().getSymbol().equals( seq_name ) ) {
500 public List<PhylogenyNode> getNodesViaGeneName( final String seq_name ) {
504 final List<PhylogenyNode> nodes = new ArrayList<PhylogenyNode>();
505 for( final PhylogenyNodeIterator iter = iteratorPreorder(); iter.hasNext(); ) {
506 final PhylogenyNode n = iter.next();
507 if ( n.getNodeData().isHasSequence() && n.getNodeData().getSequence().getGeneName().equals( seq_name ) ) {
514 public List<PhylogenyNode> getNodesViaTaxonomyCode( final String taxonomy_code ) {
518 final List<PhylogenyNode> nodes = new ArrayList<PhylogenyNode>();
519 for( final PhylogenyNodeIterator iter = iteratorPreorder(); iter.hasNext(); ) {
520 final PhylogenyNode n = iter.next();
521 if ( n.getNodeData().isHasTaxonomy()
522 && n.getNodeData().getTaxonomy().getTaxonomyCode().equals( taxonomy_code ) ) {
530 * Returns a Vector with references to all Nodes of this Phylogeny which
531 * have a matching species name.
534 * species name (String) of Nodes to find
535 * @return Vector of references to Nodes of this Phylogeny with matching
537 * @see #getNodes(String)
539 public List<PhylogenyNode> getNodesWithMatchingSpecies( final String specname ) {
543 final List<PhylogenyNode> nodes = new ArrayList<PhylogenyNode>();
544 for( final PhylogenyNodeIterator iter = iteratorPreorder(); iter.hasNext(); ) {
545 final PhylogenyNode n = iter.next();
546 if ( PhylogenyMethods.getSpecies( n ).equals( specname ) ) {
553 public PhylogenyNode getNodeViaSequenceName( final String seq_name ) {
557 final List<PhylogenyNode> nodes = getNodesViaSequenceName( seq_name );
558 if ( ( nodes == null ) || ( nodes.size() < 1 ) ) {
559 throw new IllegalArgumentException( "node with sequence named [" + seq_name + "] not found" );
561 if ( nodes.size() > 1 ) {
562 throw new IllegalArgumentException( "node with sequence named [" + seq_name + "] not unique" );
564 return nodes.get( 0 );
567 public PhylogenyNode getNodeViaTaxonomyCode( final String taxonomy_code ) {
571 final List<PhylogenyNode> nodes = getNodesViaTaxonomyCode( taxonomy_code );
572 if ( ( nodes == null ) || ( nodes.size() < 1 ) ) {
573 throw new IllegalArgumentException( "node with taxonomy code \"" + taxonomy_code + "\" not found" );
575 if ( nodes.size() > 1 ) {
576 throw new IllegalArgumentException( "node with taxonomy code \"" + taxonomy_code + "\" not unique" );
578 return nodes.get( 0 );
581 public int getNumberOfBranches() {
586 for( final PhylogenyNodeIterator iter = iteratorPreorder(); iter.hasNext(); iter.next() ) {
595 public int getNumberOfInternalNodes() {
600 for( final PhylogenyNodeIterator iter = iteratorPreorder(); iter.hasNext(); ) {
601 if ( iter.next().isInternal() ) {
612 * Returns the sum of external Nodes of this Phylogeny (int).
614 public int getNumberOfExternalNodes() {
618 return getExternalNodes().size();
622 * Returns all paralogs of the external PhylogenyNode n of this Phylogeny.
623 * paralog are returned as List of node references.
625 * PRECONDITION: This tree must be binary and rooted, and speciation -
626 * duplication need to be assigned for each of its internal Nodes.
628 * Returns null if this Phylogeny is empty or if n is internal.
630 * (Last modified: 11/22/00) Olivier CHABROL :
631 * olivier.chabrol@univ-provence.fr
634 * external PhylogenyNode whose orthologs are to be returned
635 * @return Vector of references to all orthologous Nodes of PhylogenyNode n
636 * of this Phylogeny, null if this Phylogeny is empty or if n is
639 public List<PhylogenyNode> getParalogousNodes( final PhylogenyNode n, final String[] taxonomyCodeRange ) {
640 PhylogenyNode node = n;
641 PhylogenyNode prev = null;
642 final List<PhylogenyNode> v = new ArrayList<PhylogenyNode>();
643 final Map<PhylogenyNode, List<String>> map = new HashMap<PhylogenyNode, List<String>>();
644 getTaxonomyMap( getRoot(), map );
645 if ( !node.isExternal() || isEmpty() ) {
648 final String searchNodeSpeciesId = PhylogenyMethods.getTaxonomyIdentifier( n );
649 if ( !node.isExternal() || isEmpty() ) {
652 List<String> taxIdList = null;
653 final List<String> taxonomyCodeRangeList = Arrays.asList( taxonomyCodeRange );
654 while ( !node.isRoot() ) {
656 node = node.getParent();
657 taxIdList = map.get( node );
658 if ( node.isDuplication() && isContains( taxIdList, taxonomyCodeRangeList ) ) {
659 if ( node.getChildNode1() == prev ) {
660 v.addAll( getNodeByTaxonomyID( searchNodeSpeciesId, node.getChildNode2()
661 .getAllExternalDescendants() ) );
664 v.addAll( getNodeByTaxonomyID( searchNodeSpeciesId, node.getChildNode1()
665 .getAllExternalDescendants() ) );
672 public Collection<SequenceRelation.SEQUENCE_RELATION_TYPE> getRelevantSequenceRelationTypes() {
673 if ( _relevant_sequence_relation_types == null ) {
674 _relevant_sequence_relation_types = new Vector<SEQUENCE_RELATION_TYPE>();
676 return _relevant_sequence_relation_types;
680 * Returns the root PhylogenyNode of this Phylogeny.
682 public PhylogenyNode getRoot() {
686 public Collection<Sequence> getSequenceRelationQueries() {
687 return _sequenceRelationQueries;
690 public String getType() {
695 * Deletes this Phylogeny.
704 _id_to_node_map = null;
708 setAllowMultipleParents( Phylogeny.ALLOW_MULTIPLE_PARENTS_DEFAULT );
712 * Returns whether this is a completely binary tree (i.e. all internal nodes
716 public boolean isCompletelyBinary() {
720 for( final PhylogenyNodeIterator iter = iteratorPreorder(); iter.hasNext(); ) {
721 final PhylogenyNode node = iter.next();
722 if ( node.isInternal() && ( node.getNumberOfDescendants() != 2 ) ) {
730 * Checks whether a Phylogeny object is deleted (or empty).
732 * @return true if the tree is deleted (or empty), false otherwise
734 public boolean isEmpty() {
735 return ( getRoot() == null );
738 public boolean isRerootable() {
743 * Returns true is this Phylogeny is rooted.
745 public boolean isRooted() {
749 public boolean isTree() {
753 public PhylogenyNodeIterator iteratorExternalForward() {
754 return new ExternalForwardIterator( this );
757 public PhylogenyNodeIterator iteratorLevelOrder() {
758 return new LevelOrderTreeIterator( this );
761 public PhylogenyNodeIterator iteratorPostorder() {
762 return new PostorderTreeIterator( this );
765 public PhylogenyNodeIterator iteratorPreorder() {
766 return new PreorderTreeIterator( this );
770 * Resets the ID numbers of the nodes of this Phylogeny in level order,
771 * starting with start_label (for the root). <br>
772 * WARNING. After this method has been called, node IDs are no longer
775 public void levelOrderReID() {
779 _id_to_node_map = null;
781 for( final PhylogenyNodeIterator it = iteratorPreorder(); it.hasNext(); ) {
782 final PhylogenyNode node = it.next();
783 if ( node.isRoot() ) {
784 node.setId( PhylogenyNode.getNodeCount() );
787 node.setId( node.getParent().getId() + 1 );
788 if ( node.getId() > max ) {
793 PhylogenyNode.setNodeCount( max + 1 );
797 * Prints descriptions of all external Nodes of this Phylogeny to
800 public void printExtNodes() {
804 for( final PhylogenyNodeIterator iter = iteratorExternalForward(); iter.hasNext(); ) {
805 System.out.println( iter.next() + "\n" );
810 * (Re)counts the number of children for each PhylogenyNode of this
811 * Phylogeny. As an example, this method needs to be called after a
812 * Phylogeny has been reRooted and it is to be displayed.
814 * @param consider_collapsed_nodes
815 * set to true to take into account collapsed nodes (collapsed
816 * nodes have 1 child).
818 public void recalculateNumberOfExternalDescendants( final boolean consider_collapsed_nodes ) {
822 for( final PhylogenyNodeIterator iter = iteratorPostorder(); iter.hasNext(); ) {
823 final PhylogenyNode node = iter.next();
824 if ( node.isExternal() || ( consider_collapsed_nodes && node.isCollapse() ) ) {
825 node.setSumExtNodes( 1 );
829 for( int i = 0; i < node.getNumberOfDescendants(); ++i ) {
830 sum += node.getChildNode( i ).getNumberOfExternalNodes();
832 node.setSumExtNodes( sum );
838 * Places the root of this Phylogeny on the parent branch of the
839 * PhylogenyNode with a corresponding ID. The new root is always placed on
840 * the middle of the branch. If the resulting reRooted Phylogeny is to be
841 * used any further, in most cases the following methods have to be called
842 * on the resulting Phylogeny:
844 * <li>recalculateNumberOfExternalDescendants(boolean)
845 * <li>recalculateAndReset()
848 * ID (int) of PhylogenyNode of this Phylogeny
850 public void reRoot( final long id ) {
851 reRoot( getNode( id ) );
855 * Places the root of this Phylogeny on the parent branch PhylogenyNode n.
856 * The new root is always placed on the middle of the branch.
858 * If the resulting reRooted Phylogeny is to be used any further, in most
859 * cases the following three methods have to be called on the resulting
862 * <li>recalculateNumberOfExternalDescendants(boolean) <li>recalculateAndReset()
865 * (Last modified: 10/01/01)
868 * PhylogenyNode of this Phylogeny\
870 public void reRoot( final PhylogenyNode n ) {
874 public void reRoot( final PhylogenyNode n, final double distance_n_to_parent ) {
875 if ( isEmpty() || ( getNumberOfExternalNodes() < 2 ) ) {
882 else if ( n.getParent().isRoot() ) {
883 if ( ( n.getParent().getNumberOfDescendants() == 2 ) && ( distance_n_to_parent >= 0 ) ) {
884 final double d = n.getParent().getChildNode1().getDistanceToParent()
885 + n.getParent().getChildNode2().getDistanceToParent();
887 if ( n.getChildNodeIndex() == 0 ) {
888 other = n.getParent().getChildNode2();
891 other = n.getParent().getChildNode1();
893 n.setDistanceToParent( distance_n_to_parent );
894 final double dm = d - distance_n_to_parent;
896 other.setDistanceToParent( dm );
899 other.setDistanceToParent( 0 );
902 if ( n.getParent().getNumberOfDescendants() > 2 ) {
903 final int index = n.getChildNodeIndex();
904 final double dn = n.getDistanceToParent();
905 final PhylogenyNode prev_root = getRoot();
906 prev_root.getDescendants().remove( index );
907 final PhylogenyNode new_root = new PhylogenyNode();
908 new_root.setChildNode( 0, n );
909 new_root.setChildNode( 1, prev_root );
910 if ( n.getBranchDataDirectly() != null ) {
911 prev_root.setBranchData( ( BranchData ) n.getBranchDataDirectly().copy() );
914 if ( distance_n_to_parent >= 0 ) {
915 n.setDistanceToParent( distance_n_to_parent );
916 final double d = dn - distance_n_to_parent;
918 prev_root.setDistanceToParent( d );
921 prev_root.setDistanceToParent( 0 );
926 final double d = dn / 2.0;
927 n.setDistanceToParent( d );
928 prev_root.setDistanceToParent( d );
935 PhylogenyNode b = null;
936 PhylogenyNode c = null;
937 final PhylogenyNode new_root = new PhylogenyNode();
938 double distance1 = 0.0;
939 double distance2 = 0.0;
940 BranchData branch_data_1 = null;
941 BranchData branch_data_2 = null;
944 new_root.setChildNode( 0, a );
945 new_root.setChildNode( 1, b );
946 distance1 = c.getDistanceToParent();
947 if ( c.getBranchDataDirectly() != null ) {
948 branch_data_1 = ( BranchData ) c.getBranchDataDirectly().copy();
950 c.setDistanceToParent( b.getDistanceToParent() );
951 if ( b.getBranchDataDirectly() != null ) {
952 c.setBranchData( ( BranchData ) b.getBranchDataDirectly().copy() );
954 if ( a.getBranchDataDirectly() != null ) {
955 b.setBranchData( ( BranchData ) a.getBranchDataDirectly().copy() );
957 // New root is always placed in the middle of the branch:
958 if ( a.getDistanceToParent() == PhylogenyDataUtil.BRANCH_LENGTH_DEFAULT ) {
959 b.setDistanceToParent( PhylogenyDataUtil.BRANCH_LENGTH_DEFAULT );
962 if ( distance_n_to_parent >= 0.0 ) {
963 final double diff = a.getDistanceToParent() - distance_n_to_parent;
964 a.setDistanceToParent( distance_n_to_parent );
965 b.setDistanceToParent( diff >= 0.0 ? diff : 0.0 );
968 final double d = a.getDistanceToParent() / 2.0;
969 a.setDistanceToParent( d );
970 b.setDistanceToParent( d );
973 b.setChildNodeOnly( a.getChildNodeIndex( b ), c );
974 // moving to the old root, swapping references:
975 while ( !c.isRoot() ) {
979 b.setChildNodeOnly( a.getChildNodeIndex( b ), c );
981 distance2 = c.getDistanceToParent();
982 branch_data_2 = c.getBranchDataDirectly();
983 c.setDistanceToParent( distance1 );
984 c.setBranchData( branch_data_1 );
985 distance1 = distance2;
986 branch_data_1 = branch_data_2;
988 // removing the old root:
989 if ( c.getNumberOfDescendants() == 2 ) {
990 final PhylogenyNode node = c.getChildNode( 1 - b.getChildNodeIndex( c ) );
992 if ( ( c.getDistanceToParent() == PhylogenyDataUtil.BRANCH_LENGTH_DEFAULT )
993 && ( node.getDistanceToParent() == PhylogenyDataUtil.BRANCH_LENGTH_DEFAULT ) ) {
994 node.setDistanceToParent( PhylogenyDataUtil.BRANCH_LENGTH_DEFAULT );
997 node.setDistanceToParent( ( c.getDistanceToParent() >= 0.0 ? c.getDistanceToParent() : 0.0 )
998 + ( node.getDistanceToParent() >= 0.0 ? node.getDistanceToParent() : 0.0 ) );
1000 if ( c.getBranchDataDirectly() != null ) {
1001 node.setBranchData( ( BranchData ) c.getBranchDataDirectly().copy() );
1003 for( int i = 0; i < b.getNumberOfDescendants(); ++i ) {
1004 if ( b.getChildNode( i ) == c ) {
1005 b.setChildNodeOnly( i, node );
1012 c.removeChildNode( b.getChildNodeIndex( c ) );
1014 setRoot( new_root );
1019 * Sets all Nodes of this Phylogeny to not-collapsed.
1021 * In most cases methods adjustNodeCount(false) and recalculateAndReset()
1022 * need to be called after this method has been called.
1024 public void setAllNodesToNotCollapse() {
1028 for( final PhylogenyNodeIterator iter = iteratorPreorder(); iter.hasNext(); ) {
1029 final PhylogenyNode node = iter.next();
1030 node.setCollapse( false );
1034 public void setConfidence( final Confidence confidence ) {
1035 _confidence = confidence;
1038 public void setDescription( final String description ) {
1039 _description = description;
1042 public void setDistanceUnit( final String _distance_unit ) {
1043 this._distance_unit = _distance_unit;
1046 public void setIdentifier( final Identifier identifier ) {
1047 _identifier = identifier;
1050 public void setIdToNodeMap( final HashMap<Long, PhylogenyNode> idhash ) {
1051 _id_to_node_map = idhash;
1055 * Sets the indicators of all Nodes of this Phylogeny to 0.
1057 public void setIndicatorsToZero() {
1061 for( final PhylogenyNodeIterator iter = iteratorPreorder(); iter.hasNext(); ) {
1062 iter.next().setIndicator( ( byte ) 0 );
1064 } // setIndicatorsToZero()
1067 * Sets the name of this Phylogeny to s.
1069 public void setName( final String s ) {
1073 public void setRelevantSequenceRelationTypes( final Collection<SequenceRelation.SEQUENCE_RELATION_TYPE> types ) {
1074 _relevant_sequence_relation_types = types;
1077 public void setRerootable( final boolean rerootable ) {
1078 _rerootable = rerootable;
1081 public void setRoot( final PhylogenyNode n ) {
1086 * Sets whether this Phylogeny is rooted or not.
1088 public void setRooted( final boolean b ) {
1090 } // setRooted( boolean )
1092 public void setSequenceRelationQueries( final Collection<Sequence> sequencesByName ) {
1093 _sequenceRelationQueries = sequencesByName;
1096 public void setType( final String type ) {
1100 public String toNewHampshire() {
1101 return toNewHampshire( false, NH_CONVERSION_SUPPORT_VALUE_STYLE.NONE );
1104 public String toNewHampshire( final boolean simple_nh,
1105 final NH_CONVERSION_SUPPORT_VALUE_STYLE nh_conversion_support_style ) {
1107 return new PhylogenyWriter().toNewHampshire( this, simple_nh, true, nh_conversion_support_style )
1110 catch ( final IOException e ) {
1111 throw new Error( "this should not have happend: " + e.getMessage() );
1115 public String toNewHampshireX() {
1117 return new PhylogenyWriter().toNewHampshireX( this ).toString();
1119 catch ( final IOException e ) {
1120 throw new Error( "this should not have happend: " + e.getMessage() );
1124 public String toNexus() {
1125 return toNexus( NH_CONVERSION_SUPPORT_VALUE_STYLE.NONE );
1128 public String toNexus( final NH_CONVERSION_SUPPORT_VALUE_STYLE svs ) {
1130 return new PhylogenyWriter().toNexus( this, svs ).toString();
1132 catch ( final IOException e ) {
1133 throw new Error( "this should not have happend: " + e.getMessage() );
1137 public String toPhyloXML( final int phyloxml_level ) {
1139 return new PhylogenyWriter().toPhyloXML( this, phyloxml_level ).toString();
1141 catch ( final IOException e ) {
1142 throw new Error( "this should not have happend: " + e.getMessage() );
1146 // ---------------------------------------------------------
1147 // Writing of Phylogeny to Strings
1148 // ---------------------------------------------------------
1150 * Converts this Phylogeny to a New Hampshire X (String) representation.
1152 * @return New Hampshire X (String) representation of this
1153 * @see #toNewHampshireX()
1156 public String toString() {
1157 return toNewHampshireX();
1161 * Removes the root PhylogenyNode this Phylogeny.
1163 public void unRoot() throws RuntimeException {
1165 throw new FailedConditionCheckException( "Attempt to unroot a phylogeny which is not tree-like." );
1170 setIndicatorsToZero();
1171 if ( !isRooted() || ( getNumberOfExternalNodes() <= 1 ) ) {
1178 private HashMap<Long, PhylogenyNode> getIdToNodeMap() {
1179 return _id_to_node_map;
1183 * Return Node by TaxonomyId Olivier CHABROL :
1184 * olivier.chabrol@univ-provence.fr
1187 * search taxonomy identifier
1189 * sublist node to search
1190 * @return List node with the same taxonomy identifier
1192 private List<PhylogenyNode> getNodeByTaxonomyID( final String taxonomyID, final List<PhylogenyNode> nodes ) {
1193 final List<PhylogenyNode> retour = new ArrayList<PhylogenyNode>();
1194 for( final PhylogenyNode node : nodes ) {
1195 if ( taxonomyID.equals( PhylogenyMethods.getTaxonomyIdentifier( node ) ) ) {
1203 * List all species contains in all leaf under a node Olivier CHABROL :
1204 * olivier.chabrol@univ-provence.fr
1207 * PhylogenyNode whose sub node species are returned
1208 * @return species contains in all leaf under the param node
1210 private List<String> getSubNodeTaxonomy( final PhylogenyNode node ) {
1211 final List<String> taxonomyList = new ArrayList<String>();
1212 final List<PhylogenyNode> childs = node.getAllExternalDescendants();
1213 String speciesId = null;
1214 for( final PhylogenyNode phylogenyNode : childs ) {
1215 // taxId = new Long(phylogenyNode.getTaxonomyID());
1216 speciesId = PhylogenyMethods.getTaxonomyIdentifier( phylogenyNode );
1217 if ( !taxonomyList.contains( speciesId ) ) {
1218 taxonomyList.add( speciesId );
1221 return taxonomyList;
1225 * Create a map [<PhylogenyNode, List<String>], the list contains the
1226 * species contains in all leaf under phylogeny node Olivier CHABROL :
1227 * olivier.chabrol@univ-provence.fr
1230 * the tree root node
1234 private void getTaxonomyMap( final PhylogenyNode node, final Map<PhylogenyNode, List<String>> map ) {
1236 if ( node.isExternal() ) {
1239 map.put( node, getSubNodeTaxonomy( node ) );
1240 getTaxonomyMap( node.getChildNode1(), map );
1241 getTaxonomyMap( node.getChildNode2(), map );
1244 private boolean isAllowMultipleParents() {
1245 return _allow_multiple_parents;
1249 * Util method to check if all element of a list is contains in the
1250 * rangeList. Olivier CHABROL : olivier.chabrol@univ-provence.fr
1255 * the range list to compare
1256 * @return <code>true</code> if all param list element are contains in param
1257 * rangeList, <code>false</code> otherwise.
1259 private boolean isContains( final List<String> list, final List<String> rangeList ) {
1260 if ( list.size() > rangeList.size() ) {
1264 for( final Iterator<String> iterator = list.iterator(); iterator.hasNext(); ) {
1265 l = iterator.next();
1266 if ( !rangeList.contains( l ) ) {
1274 * Hashes the ID number of each PhylogenyNode of this Phylogeny to its
1275 * corresponding PhylogenyNode, in order to make method getNode( id ) run in
1276 * constant time. Important: The user is responsible for calling this method
1277 * (again) after this Phylogeny has been changed/created/renumbered.
1279 private void reHashIdToNodeMap() {
1283 setIdToNodeMap( new HashMap<Long, PhylogenyNode>() );
1284 for( final PhylogenyNodeIterator iter = iteratorPreorder(); iter.hasNext(); ) {
1285 final PhylogenyNode node = iter.next();
1286 getIdToNodeMap().put( node.getId(), node );
1290 private void setAllowMultipleParents( final boolean allow_multiple_parents ) {
1291 _allow_multiple_parents = allow_multiple_parents;