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
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 // Contact: phylosoft @ gmail . com
24 // WWW: www.phylosoft.org/forester
26 package org.forester.phylogeny;
28 import java.awt.Color;
30 import java.io.IOException;
31 import java.util.ArrayList;
32 import java.util.Arrays;
33 import java.util.HashSet;
34 import java.util.Iterator;
35 import java.util.List;
37 import java.util.SortedMap;
38 import java.util.TreeMap;
40 import org.forester.io.parsers.PhylogenyParser;
41 import org.forester.io.parsers.phyloxml.PhyloXmlUtil;
42 import org.forester.io.parsers.util.PhylogenyParserException;
43 import org.forester.phylogeny.data.BranchColor;
44 import org.forester.phylogeny.data.BranchWidth;
45 import org.forester.phylogeny.data.Confidence;
46 import org.forester.phylogeny.data.DomainArchitecture;
47 import org.forester.phylogeny.data.Identifier;
48 import org.forester.phylogeny.data.Sequence;
49 import org.forester.phylogeny.data.Taxonomy;
50 import org.forester.phylogeny.factories.ParserBasedPhylogenyFactory;
51 import org.forester.phylogeny.factories.PhylogenyFactory;
52 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
53 import org.forester.util.BasicDescriptiveStatistics;
54 import org.forester.util.DescriptiveStatistics;
55 import org.forester.util.FailedConditionCheckException;
56 import org.forester.util.ForesterUtil;
58 public class PhylogenyMethods {
60 private static PhylogenyMethods _instance = null;
61 private final Set<Integer> _temp_hash_set = new HashSet<Integer>();
62 private PhylogenyNode _farthest_1 = null;
63 private PhylogenyNode _farthest_2 = null;
65 private PhylogenyMethods() {
66 // Hidden constructor.
70 * Calculates the distance between PhylogenyNodes node1 and node2.
75 * @return distance between node1 and node2
77 public double calculateDistance( final PhylogenyNode node1, final PhylogenyNode node2 ) {
78 final PhylogenyNode lca = obtainLCA( node1, node2 );
79 final PhylogenyNode n1 = node1;
80 final PhylogenyNode n2 = node2;
81 return ( PhylogenyMethods.getDistance( n1, lca ) + PhylogenyMethods.getDistance( n2, lca ) );
84 public double calculateFurthestDistance( final Phylogeny phylogeny ) {
85 if ( phylogeny.getNumberOfExternalNodes() < 2 ) {
90 PhylogenyNode node_1 = null;
91 PhylogenyNode node_2 = null;
92 double farthest_d = -Double.MAX_VALUE;
93 final PhylogenyMethods methods = PhylogenyMethods.getInstance();
94 final List<PhylogenyNode> ext_nodes = phylogeny.getRoot().getAllExternalDescendants();
95 for( int i = 1; i < ext_nodes.size(); ++i ) {
96 for( int j = 0; j < i; ++j ) {
97 final double d = methods.calculateDistance( ext_nodes.get( i ), ext_nodes.get( j ) );
99 throw new RuntimeException( "distance cannot be negative" );
101 if ( d > farthest_d ) {
103 node_1 = ext_nodes.get( i );
104 node_2 = ext_nodes.get( j );
108 _farthest_1 = node_1;
109 _farthest_2 = node_2;
114 public Object clone() throws CloneNotSupportedException {
115 throw new CloneNotSupportedException();
118 public PhylogenyNode getFarthestNode1() {
122 public PhylogenyNode getFarthestNode2() {
127 * Returns the LCA of PhylogenyNodes node1 and node2.
132 * @return LCA of node1 and node2
134 public PhylogenyNode obtainLCA( final PhylogenyNode node1, final PhylogenyNode node2 ) {
135 _temp_hash_set.clear();
136 PhylogenyNode n1 = node1;
137 PhylogenyNode n2 = node2;
138 _temp_hash_set.add( n1.getId() );
139 while ( !n1.isRoot() ) {
141 _temp_hash_set.add( n1.getId() );
143 while ( !_temp_hash_set.contains( n2.getId() ) && !n2.isRoot() ) {
146 if ( !_temp_hash_set.contains( n2.getId() ) ) {
147 throw new IllegalArgumentException( "attempt to get LCA of two nodes which do not share a common root" );
153 * Returns all orthologs of the external PhylogenyNode n of this Phylogeny.
154 * Orthologs are returned as List of node references.
156 * PRECONDITION: This tree must be binary and rooted, and speciation -
157 * duplication need to be assigned for each of its internal Nodes.
159 * Returns null if this Phylogeny is empty or if n is internal.
161 * external PhylogenyNode whose orthologs are to be returned
162 * @return Vector of references to all orthologous Nodes of PhylogenyNode n
163 * of this Phylogeny, null if this Phylogeny is empty or if n is
166 public List<PhylogenyNode> getOrthologousNodes( final Phylogeny phy, final PhylogenyNode node ) {
167 final List<PhylogenyNode> nodes = new ArrayList<PhylogenyNode>();
168 final PhylogenyNodeIterator it = phy.iteratorExternalForward();
169 while ( it.hasNext() ) {
170 final PhylogenyNode temp_node = it.next();
171 if ( ( temp_node != node ) && isAreOrthologous( node, temp_node ) ) {
172 nodes.add( temp_node );
178 public boolean isAreOrthologous( final PhylogenyNode node1, final PhylogenyNode node2 ) {
179 return !obtainLCA( node1, node2 ).isDuplication();
182 public final static Phylogeny[] readPhylogenies( final PhylogenyParser parser, final File file ) throws IOException {
183 final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
184 final Phylogeny[] trees = factory.create( file, parser );
185 if ( ( trees == null ) || ( trees.length == 0 ) ) {
186 throw new PhylogenyParserException( "Unable to parse phylogeny from file: " + file );
191 final static public void transferInternalNodeNamesToConfidence( final Phylogeny phy ) {
192 final PhylogenyNodeIterator it = phy.iteratorPostorder();
193 while ( it.hasNext() ) {
194 final PhylogenyNode n = it.next();
195 if ( !n.isRoot() && !n.isExternal() && !n.getBranchData().isHasConfidences() ) {
196 if ( !ForesterUtil.isEmpty( n.getName() ) ) {
199 d = Double.parseDouble( n.getName() );
201 catch ( final Exception e ) {
205 n.getBranchData().addConfidence( new Confidence( d, "" ) );
213 final static public void transferInternalNamesToBootstrapSupport( final Phylogeny phy ) {
214 final PhylogenyNodeIterator it = phy.iteratorPostorder();
215 while ( it.hasNext() ) {
216 final PhylogenyNode n = it.next();
217 if ( !n.isExternal() && !ForesterUtil.isEmpty( n.getName() ) ) {
220 value = Double.parseDouble( n.getName() );
222 catch ( final NumberFormatException e ) {
223 throw new IllegalArgumentException( "failed to parse number from [" + n.getName() + "]: "
224 + e.getLocalizedMessage() );
226 if ( value >= 0.0 ) {
227 n.getBranchData().addConfidence( new Confidence( value, "bootstrap" ) );
234 final static public void transferNodeNameToField( final Phylogeny phy,
235 final PhylogenyMethods.PhylogenyNodeField field ) {
236 final PhylogenyNodeIterator it = phy.iteratorPostorder();
237 while ( it.hasNext() ) {
238 final PhylogenyNode n = it.next();
239 final String name = n.getName().trim();
240 if ( !ForesterUtil.isEmpty( name ) ) {
244 // if ( name.length() > 5 ) {
246 // if ( !n.getNodeData().isHasTaxonomy() ) {
247 // n.getNodeData().setTaxonomy( new Taxonomy() );
249 // n.getNodeData().getTaxonomy().setScientificName( name );
254 setTaxonomyCode( n, name );
256 case TAXONOMY_SCIENTIFIC_NAME:
258 if ( !n.getNodeData().isHasTaxonomy() ) {
259 n.getNodeData().setTaxonomy( new Taxonomy() );
261 n.getNodeData().getTaxonomy().setScientificName( name );
263 case TAXONOMY_COMMON_NAME:
265 if ( !n.getNodeData().isHasTaxonomy() ) {
266 n.getNodeData().setTaxonomy( new Taxonomy() );
268 n.getNodeData().getTaxonomy().setCommonName( name );
270 case SEQUENCE_SYMBOL:
272 if ( !n.getNodeData().isHasSequence() ) {
273 n.getNodeData().setSequence( new Sequence() );
275 n.getNodeData().getSequence().setSymbol( name );
279 if ( !n.getNodeData().isHasSequence() ) {
280 n.getNodeData().setSequence( new Sequence() );
282 n.getNodeData().getSequence().setName( name );
284 case TAXONOMY_ID_UNIPROT_1: {
285 if ( !n.getNodeData().isHasTaxonomy() ) {
286 n.getNodeData().setTaxonomy( new Taxonomy() );
289 final int i = name.indexOf( '_' );
291 id = name.substring( 0, i );
296 n.getNodeData().getTaxonomy()
297 .setIdentifier( new Identifier( id, PhyloXmlUtil.UNIPROT_TAX_PROVIDER ) );
300 case TAXONOMY_ID_UNIPROT_2: {
301 if ( !n.getNodeData().isHasTaxonomy() ) {
302 n.getNodeData().setTaxonomy( new Taxonomy() );
305 final int i = name.indexOf( '_' );
307 id = name.substring( i + 1, name.length() );
312 n.getNodeData().getTaxonomy()
313 .setIdentifier( new Identifier( id, PhyloXmlUtil.UNIPROT_TAX_PROVIDER ) );
321 static double addPhylogenyDistances( final double a, final double b ) {
322 if ( ( a >= 0.0 ) && ( b >= 0.0 ) ) {
325 else if ( a >= 0.0 ) {
328 else if ( b >= 0.0 ) {
331 return PhylogenyNode.DISTANCE_DEFAULT;
334 // Helper for getUltraParalogousNodes( PhylogenyNode ).
335 public static boolean areAllChildrenDuplications( final PhylogenyNode n ) {
336 if ( n.isExternal() ) {
340 if ( n.isDuplication() ) {
342 for( final PhylogenyNode desc : n.getDescendants() ) {
343 if ( !areAllChildrenDuplications( desc ) ) {
355 public static int calculateDepth( final PhylogenyNode node ) {
356 PhylogenyNode n = node;
358 while ( !n.isRoot() ) {
365 public static double calculateDistanceToRoot( final PhylogenyNode node ) {
366 PhylogenyNode n = node;
368 while ( !n.isRoot() ) {
369 if ( n.getDistanceToParent() > 0.0 ) {
370 d += n.getDistanceToParent();
377 public static short calculateMaxBranchesToLeaf( final PhylogenyNode node ) {
378 if ( node.isExternal() ) {
382 for( PhylogenyNode d : node.getAllExternalDescendants() ) {
384 while ( d != node ) {
385 if ( d.isCollapse() ) {
400 public static int calculateMaxDepth( final Phylogeny phy ) {
402 for( final PhylogenyNodeIterator iter = phy.iteratorExternalForward(); iter.hasNext(); ) {
403 final PhylogenyNode node = iter.next();
404 final int steps = calculateDepth( node );
412 public static double calculateMaxDistanceToRoot( final Phylogeny phy ) {
414 for( final PhylogenyNodeIterator iter = phy.iteratorExternalForward(); iter.hasNext(); ) {
415 final PhylogenyNode node = iter.next();
416 final double d = calculateDistanceToRoot( node );
424 public static DescriptiveStatistics calculatNumberOfDescendantsPerNodeStatistics( final Phylogeny phy ) {
425 final DescriptiveStatistics stats = new BasicDescriptiveStatistics();
426 for( final PhylogenyNodeIterator iter = phy.iteratorPreorder(); iter.hasNext(); ) {
427 final PhylogenyNode n = iter.next();
428 if ( !n.isExternal() ) {
429 stats.addValue( n.getNumberOfDescendants() );
435 public static DescriptiveStatistics calculatConfidenceStatistics( final Phylogeny phy ) {
436 final DescriptiveStatistics stats = new BasicDescriptiveStatistics();
437 for( final PhylogenyNodeIterator iter = phy.iteratorPreorder(); iter.hasNext(); ) {
438 final PhylogenyNode n = iter.next();
439 if ( !n.isExternal() ) {
440 if ( n.getBranchData().isHasConfidences() ) {
441 stats.addValue( n.getBranchData().getConfidence( 0 ).getValue() );
449 * Returns the set of distinct taxonomies of
450 * all external nodes of node.
451 * If at least one the external nodes has no taxonomy,
455 public static Set<Taxonomy> obtainDistinctTaxonomies( final PhylogenyNode node ) {
456 final List<PhylogenyNode> descs = node.getAllExternalDescendants();
457 final Set<Taxonomy> tax_set = new HashSet<Taxonomy>();
458 for( final PhylogenyNode n : descs ) {
459 if ( !n.getNodeData().isHasTaxonomy() || n.getNodeData().getTaxonomy().isEmpty() ) {
462 tax_set.add( n.getNodeData().getTaxonomy() );
468 * Returns a map of distinct taxonomies of
469 * all external nodes of node.
470 * If at least one of the external nodes has no taxonomy,
474 public static SortedMap<Taxonomy, Integer> obtainDistinctTaxonomyCounts( final PhylogenyNode node ) {
475 final List<PhylogenyNode> descs = node.getAllExternalDescendants();
476 final SortedMap<Taxonomy, Integer> tax_map = new TreeMap<Taxonomy, Integer>();
477 for( final PhylogenyNode n : descs ) {
478 if ( !n.getNodeData().isHasTaxonomy() || n.getNodeData().getTaxonomy().isEmpty() ) {
481 final Taxonomy t = n.getNodeData().getTaxonomy();
482 if ( tax_map.containsKey( t ) ) {
483 tax_map.put( t, tax_map.get( t ) + 1 );
492 public static int calculateNumberOfExternalNodesWithoutTaxonomy( final PhylogenyNode node ) {
493 final List<PhylogenyNode> descs = node.getAllExternalDescendants();
495 for( final PhylogenyNode n : descs ) {
496 if ( !n.getNodeData().isHasTaxonomy() || n.getNodeData().getTaxonomy().isEmpty() ) {
504 * Deep copies the phylogeny originating from this node.
506 static PhylogenyNode copySubTree( final PhylogenyNode source ) {
507 if ( source == null ) {
511 final PhylogenyNode newnode = source.copyNodeData();
512 if ( !source.isExternal() ) {
513 for( int i = 0; i < source.getNumberOfDescendants(); ++i ) {
514 newnode.setChildNode( i, PhylogenyMethods.copySubTree( source.getChildNode( i ) ) );
522 * Shallow copies the phylogeny originating from this node.
524 static PhylogenyNode copySubTreeShallow( final PhylogenyNode source ) {
525 if ( source == null ) {
529 final PhylogenyNode newnode = source.copyNodeDataShallow();
530 if ( !source.isExternal() ) {
531 for( int i = 0; i < source.getNumberOfDescendants(); ++i ) {
532 newnode.setChildNode( i, PhylogenyMethods.copySubTreeShallow( source.getChildNode( i ) ) );
539 public static void deleteExternalNodesNegativeSelection( final Set<Integer> to_delete, final Phylogeny phy ) {
541 for( final Integer id : to_delete ) {
542 phy.deleteSubtree( phy.getNode( id ), true );
547 public static void deleteExternalNodesNegativeSelection( final String[] node_names_to_delete, final Phylogeny p )
548 throws IllegalArgumentException {
549 for( int i = 0; i < node_names_to_delete.length; ++i ) {
550 if ( ForesterUtil.isEmpty( node_names_to_delete[ i ] ) ) {
553 List<PhylogenyNode> nodes = null;
554 nodes = p.getNodes( node_names_to_delete[ i ] );
555 final Iterator<PhylogenyNode> it = nodes.iterator();
556 while ( it.hasNext() ) {
557 final PhylogenyNode n = it.next();
558 if ( !n.isExternal() ) {
559 throw new IllegalArgumentException( "attempt to delete non-external node \""
560 + node_names_to_delete[ i ] + "\"" );
562 p.deleteSubtree( n, true );
567 public static void deleteExternalNodesPositiveSelection( final Set<Taxonomy> species_to_keep, final Phylogeny phy ) {
568 // final Set<Integer> to_delete = new HashSet<Integer>();
569 for( final PhylogenyNodeIterator it = phy.iteratorExternalForward(); it.hasNext(); ) {
570 final PhylogenyNode n = it.next();
571 if ( n.getNodeData().isHasTaxonomy() ) {
572 if ( !species_to_keep.contains( n.getNodeData().getTaxonomy() ) ) {
573 //to_delete.add( n.getNodeId() );
574 phy.deleteSubtree( n, true );
578 throw new IllegalArgumentException( "node " + n.getId() + " has no taxonomic data" );
582 phy.externalNodesHaveChanged();
583 // deleteExternalNodesNegativeSelection( to_delete, phy );
586 public static List<String> deleteExternalNodesPositiveSelection( final String[] node_names_to_keep,
587 final Phylogeny p ) {
588 final PhylogenyNodeIterator it = p.iteratorExternalForward();
589 final String[] to_delete = new String[ p.getNumberOfExternalNodes() ];
591 Arrays.sort( node_names_to_keep );
592 while ( it.hasNext() ) {
593 final String curent_name = it.next().getName();
594 if ( Arrays.binarySearch( node_names_to_keep, curent_name ) < 0 ) {
595 to_delete[ i++ ] = curent_name;
598 PhylogenyMethods.deleteExternalNodesNegativeSelection( to_delete, p );
599 final List<String> deleted = new ArrayList<String>();
600 for( final String n : to_delete ) {
601 if ( !ForesterUtil.isEmpty( n ) ) {
608 public static List<PhylogenyNode> getAllDescendants( final PhylogenyNode node ) {
609 final List<PhylogenyNode> descs = new ArrayList<PhylogenyNode>();
610 final Set<Integer> encountered = new HashSet<Integer>();
611 if ( !node.isExternal() ) {
612 final List<PhylogenyNode> exts = node.getAllExternalDescendants();
613 for( PhylogenyNode current : exts ) {
614 descs.add( current );
615 while ( current != node ) {
616 current = current.getParent();
617 if ( encountered.contains( current.getId() ) ) {
620 descs.add( current );
621 encountered.add( current.getId() );
635 public static Color getBranchColorValue( final PhylogenyNode node ) {
636 if ( node.getBranchData().getBranchColor() == null ) {
639 return node.getBranchData().getBranchColor().getValue();
645 public static double getBranchWidthValue( final PhylogenyNode node ) {
646 if ( !node.getBranchData().isHasBranchWidth() ) {
647 return BranchWidth.BRANCH_WIDTH_DEFAULT_VALUE;
649 return node.getBranchData().getBranchWidth().getValue();
655 public static double getConfidenceValue( final PhylogenyNode node ) {
656 if ( !node.getBranchData().isHasConfidences() ) {
657 return Confidence.CONFIDENCE_DEFAULT_VALUE;
659 return node.getBranchData().getConfidence( 0 ).getValue();
665 public static double[] getConfidenceValuesAsArray( final PhylogenyNode node ) {
666 if ( !node.getBranchData().isHasConfidences() ) {
667 return new double[ 0 ];
669 final double[] values = new double[ node.getBranchData().getConfidences().size() ];
671 for( final Confidence c : node.getBranchData().getConfidences() ) {
672 values[ i++ ] = c.getValue();
678 * Calculates the distance between PhylogenyNodes n1 and n2.
679 * PRECONDITION: n1 is a descendant of n2.
684 * @return distance between n1 and n2
686 private static double getDistance( PhylogenyNode n1, final PhylogenyNode n2 ) {
689 if ( n1.getDistanceToParent() > 0.0 ) {
690 d += n1.getDistanceToParent();
698 * Returns taxonomy t if all external descendants have
699 * the same taxonomy t, null otherwise.
702 public static Taxonomy getExternalDescendantsTaxonomy( final PhylogenyNode node ) {
703 final List<PhylogenyNode> descs = node.getAllExternalDescendants();
705 for( final PhylogenyNode n : descs ) {
706 if ( !n.getNodeData().isHasTaxonomy() || n.getNodeData().getTaxonomy().isEmpty() ) {
709 else if ( tax == null ) {
710 tax = n.getNodeData().getTaxonomy();
712 else if ( n.getNodeData().getTaxonomy().isEmpty() || !tax.isEqual( n.getNodeData().getTaxonomy() ) ) {
719 public static PhylogenyNode getFurthestDescendant( final PhylogenyNode node ) {
720 final List<PhylogenyNode> children = node.getAllExternalDescendants();
721 PhylogenyNode farthest = null;
722 double longest = -Double.MAX_VALUE;
723 for( final PhylogenyNode child : children ) {
724 if ( PhylogenyMethods.getDistance( child, node ) > longest ) {
726 longest = PhylogenyMethods.getDistance( child, node );
732 public static PhylogenyMethods getInstance() {
733 if ( PhylogenyMethods._instance == null ) {
734 PhylogenyMethods._instance = new PhylogenyMethods();
736 return PhylogenyMethods._instance;
740 * Returns the largest confidence value found on phy.
742 static public double getMaximumConfidenceValue( final Phylogeny phy ) {
743 double max = -Double.MAX_VALUE;
744 for( final PhylogenyNodeIterator iter = phy.iteratorPreorder(); iter.hasNext(); ) {
745 final double s = PhylogenyMethods.getConfidenceValue( iter.next() );
746 if ( ( s != Confidence.CONFIDENCE_DEFAULT_VALUE ) && ( s > max ) ) {
753 static public int getMinimumDescendentsPerInternalNodes( final Phylogeny phy ) {
754 int min = Integer.MAX_VALUE;
757 for( final PhylogenyNodeIterator it = phy.iteratorPreorder(); it.hasNext(); ) {
759 if ( n.isInternal() ) {
760 d = n.getNumberOfDescendants();
770 * Convenience method for display purposes.
771 * Not intended for algorithms.
773 public static String getSpecies( final PhylogenyNode node ) {
774 if ( !node.getNodeData().isHasTaxonomy() ) {
777 if ( !ForesterUtil.isEmpty( node.getNodeData().getTaxonomy().getTaxonomyCode() ) ) {
778 return node.getNodeData().getTaxonomy().getTaxonomyCode();
780 else if ( !ForesterUtil.isEmpty( node.getNodeData().getTaxonomy().getScientificName() ) ) {
781 return node.getNodeData().getTaxonomy().getScientificName();
784 return node.getNodeData().getTaxonomy().getCommonName();
789 * Returns all Nodes which are connected to external PhylogenyNode n of this
790 * Phylogeny by a path containing only speciation events. We call these
791 * "super orthologs". Nodes are returned as Vector of references to Nodes.
793 * PRECONDITION: This tree must be binary and rooted, and speciation -
794 * duplication need to be assigned for each of its internal Nodes.
796 * Returns null if this Phylogeny is empty or if n is internal.
798 * external PhylogenyNode whose strictly speciation related Nodes
800 * @return Vector of references to all strictly speciation related Nodes of
801 * PhylogenyNode n of this Phylogeny, null if this Phylogeny is
802 * empty or if n is internal
804 public static List<PhylogenyNode> getSuperOrthologousNodes( final PhylogenyNode n ) {
806 PhylogenyNode node = n, deepest = null;
807 final List<PhylogenyNode> v = new ArrayList<PhylogenyNode>();
808 if ( !node.isExternal() ) {
811 while ( !node.isRoot() && !node.getParent().isDuplication() ) {
812 node = node.getParent();
815 deepest.setIndicatorsToZero();
817 if ( !node.isExternal() ) {
818 if ( node.getIndicator() == 0 ) {
819 node.setIndicator( ( byte ) 1 );
820 if ( !node.isDuplication() ) {
821 node = node.getChildNode1();
824 if ( node.getIndicator() == 1 ) {
825 node.setIndicator( ( byte ) 2 );
826 if ( !node.isDuplication() ) {
827 node = node.getChildNode2();
830 if ( ( node != deepest ) && ( node.getIndicator() == 2 ) ) {
831 node = node.getParent();
838 if ( node != deepest ) {
839 node = node.getParent();
842 node.setIndicator( ( byte ) 2 );
845 } while ( ( node != deepest ) || ( deepest.getIndicator() != 2 ) );
850 * Convenience method for display purposes.
851 * Not intended for algorithms.
853 public static String getTaxonomyIdentifier( final PhylogenyNode node ) {
854 if ( !node.getNodeData().isHasTaxonomy() || ( node.getNodeData().getTaxonomy().getIdentifier() == null ) ) {
857 return node.getNodeData().getTaxonomy().getIdentifier().getValue();
861 * Returns all Nodes which are connected to external PhylogenyNode n of this
862 * Phylogeny by a path containing, and leading to, only duplication events.
863 * We call these "ultra paralogs". Nodes are returned as Vector of
864 * references to Nodes.
866 * PRECONDITION: This tree must be binary and rooted, and speciation -
867 * duplication need to be assigned for each of its internal Nodes.
869 * Returns null if this Phylogeny is empty or if n is internal.
871 * (Last modified: 10/06/01)
874 * external PhylogenyNode whose ultra paralogs are to be returned
875 * @return Vector of references to all ultra paralogs of PhylogenyNode n of
876 * this Phylogeny, null if this Phylogeny is empty or if n is
879 public static List<PhylogenyNode> getUltraParalogousNodes( final PhylogenyNode n ) {
881 PhylogenyNode node = n;
882 if ( !node.isExternal() ) {
885 while ( !node.isRoot() && node.getParent().isDuplication() && areAllChildrenDuplications( node.getParent() ) ) {
886 node = node.getParent();
888 final List<PhylogenyNode> nodes = node.getAllExternalDescendants();
893 public static String inferCommonPartOfScientificNameOfDescendants( final PhylogenyNode node ) {
894 final List<PhylogenyNode> descs = node.getDescendants();
896 for( final PhylogenyNode n : descs ) {
897 if ( !n.getNodeData().isHasTaxonomy()
898 || ForesterUtil.isEmpty( n.getNodeData().getTaxonomy().getScientificName() ) ) {
901 else if ( sn == null ) {
902 sn = n.getNodeData().getTaxonomy().getScientificName().trim();
905 String sn_current = n.getNodeData().getTaxonomy().getScientificName().trim();
906 if ( !sn.equals( sn_current ) ) {
907 boolean overlap = false;
908 while ( ( sn.indexOf( ' ' ) >= 0 ) || ( sn_current.indexOf( ' ' ) >= 0 ) ) {
909 if ( ForesterUtil.countChars( sn, ' ' ) > ForesterUtil.countChars( sn_current, ' ' ) ) {
910 sn = sn.substring( 0, sn.lastIndexOf( ' ' ) ).trim();
913 sn_current = sn_current.substring( 0, sn_current.lastIndexOf( ' ' ) ).trim();
915 if ( sn.equals( sn_current ) ) {
929 public static boolean isHasExternalDescendant( final PhylogenyNode node ) {
930 for( int i = 0; i < node.getNumberOfDescendants(); ++i ) {
931 if ( node.getChildNode( i ).isExternal() ) {
939 * This is case insensitive.
942 public synchronized static boolean isTaxonomyHasIdentifierOfGivenProvider( final Taxonomy tax,
943 final String[] providers ) {
944 if ( ( tax.getIdentifier() != null ) && !ForesterUtil.isEmpty( tax.getIdentifier().getProvider() ) ) {
945 final String my_tax_prov = tax.getIdentifier().getProvider();
946 for( final String provider : providers ) {
947 if ( provider.equalsIgnoreCase( my_tax_prov ) ) {
958 private static boolean match( final String s,
960 final boolean case_sensitive,
961 final boolean partial ) {
962 if ( ForesterUtil.isEmpty( s ) || ForesterUtil.isEmpty( query ) ) {
965 String my_s = s.trim();
966 String my_query = query.trim();
967 if ( !case_sensitive ) {
968 my_s = my_s.toLowerCase();
969 my_query = my_query.toLowerCase();
972 return my_s.indexOf( my_query ) >= 0;
975 return my_s.equals( my_query );
979 public static void midpointRoot( final Phylogeny phylogeny ) {
980 if ( phylogeny.getNumberOfExternalNodes() < 2 ) {
983 final PhylogenyMethods methods = getInstance();
984 final double farthest_d = methods.calculateFurthestDistance( phylogeny );
985 final PhylogenyNode f1 = methods.getFarthestNode1();
986 final PhylogenyNode f2 = methods.getFarthestNode2();
987 if ( farthest_d <= 0.0 ) {
990 double x = farthest_d / 2.0;
991 PhylogenyNode n = f1;
992 if ( PhylogenyMethods.getDistance( f1, phylogeny.getRoot() ) < PhylogenyMethods.getDistance( f2, phylogeny
996 while ( ( x > n.getDistanceToParent() ) && !n.isRoot() ) {
997 x -= ( n.getDistanceToParent() > 0 ? n.getDistanceToParent() : 0 );
1000 phylogeny.reRoot( n, x );
1001 phylogeny.recalculateNumberOfExternalDescendants( true );
1002 final PhylogenyNode a = getFurthestDescendant( phylogeny.getRoot().getChildNode1() );
1003 final PhylogenyNode b = getFurthestDescendant( phylogeny.getRoot().getChildNode2() );
1004 final double da = getDistance( a, phylogeny.getRoot() );
1005 final double db = getDistance( b, phylogeny.getRoot() );
1006 if ( Math.abs( da - db ) > 0.000001 ) {
1007 throw new FailedConditionCheckException( "this should not have happened: midpoint rooting failed: da="
1008 + da + ", db=" + db + ", diff=" + Math.abs( da - db ) );
1012 public static void normalizeBootstrapValues( final Phylogeny phylogeny,
1013 final double max_bootstrap_value,
1014 final double max_normalized_value ) {
1015 for( final PhylogenyNodeIterator iter = phylogeny.iteratorPreorder(); iter.hasNext(); ) {
1016 final PhylogenyNode node = iter.next();
1017 if ( node.isInternal() ) {
1018 final double confidence = getConfidenceValue( node );
1019 if ( confidence != Confidence.CONFIDENCE_DEFAULT_VALUE ) {
1020 if ( confidence >= max_bootstrap_value ) {
1021 setBootstrapConfidence( node, max_normalized_value );
1024 setBootstrapConfidence( node, ( confidence * max_normalized_value ) / max_bootstrap_value );
1031 public static List<PhylogenyNode> obtainAllNodesAsList( final Phylogeny phy ) {
1032 final List<PhylogenyNode> nodes = new ArrayList<PhylogenyNode>();
1033 if ( phy.isEmpty() ) {
1036 for( final PhylogenyNodeIterator iter = phy.iteratorPreorder(); iter.hasNext(); ) {
1037 nodes.add( iter.next() );
1042 public static void postorderBranchColorAveragingExternalNodeBased( final Phylogeny p ) {
1043 for( final PhylogenyNodeIterator iter = p.iteratorPostorder(); iter.hasNext(); ) {
1044 final PhylogenyNode node = iter.next();
1049 if ( node.isInternal() ) {
1050 for( final PhylogenyNodeIterator iterator = node.iterateChildNodesForward(); iterator.hasNext(); ) {
1051 final PhylogenyNode child_node = iterator.next();
1052 final Color child_color = getBranchColorValue( child_node );
1053 if ( child_color != null ) {
1055 red += child_color.getRed();
1056 green += child_color.getGreen();
1057 blue += child_color.getBlue();
1060 setBranchColorValue( node,
1061 new Color( ForesterUtil.roundToInt( red / n ),
1062 ForesterUtil.roundToInt( green / n ),
1063 ForesterUtil.roundToInt( blue / n ) ) );
1068 public static void removeNode( final PhylogenyNode remove_me, final Phylogeny phylogeny ) {
1069 if ( remove_me.isRoot() ) {
1070 throw new IllegalArgumentException( "ill advised attempt to remove root node" );
1072 if ( remove_me.isExternal() ) {
1073 phylogeny.deleteSubtree( remove_me, false );
1076 final PhylogenyNode parent = remove_me.getParent();
1077 final List<PhylogenyNode> descs = remove_me.getDescendants();
1078 parent.removeChildNode( remove_me );
1079 for( final PhylogenyNode desc : descs ) {
1080 parent.addAsChild( desc );
1081 desc.setDistanceToParent( addPhylogenyDistances( remove_me.getDistanceToParent(),
1082 desc.getDistanceToParent() ) );
1084 remove_me.setParent( null );
1085 phylogeny.setIdHash( null );
1086 phylogeny.externalNodesHaveChanged();
1090 public static List<PhylogenyNode> searchData( final String query,
1091 final Phylogeny phy,
1092 final boolean case_sensitive,
1093 final boolean partial ) {
1094 final List<PhylogenyNode> nodes = new ArrayList<PhylogenyNode>();
1095 if ( phy.isEmpty() || ( query == null ) ) {
1098 if ( ForesterUtil.isEmpty( query ) ) {
1101 for( final PhylogenyNodeIterator iter = phy.iteratorPreorder(); iter.hasNext(); ) {
1102 final PhylogenyNode node = iter.next();
1103 boolean match = false;
1104 if ( match( node.getName(), query, case_sensitive, partial ) ) {
1107 else if ( node.getNodeData().isHasTaxonomy()
1108 && match( node.getNodeData().getTaxonomy().getTaxonomyCode(), query, case_sensitive, partial ) ) {
1111 else if ( node.getNodeData().isHasTaxonomy()
1112 && match( node.getNodeData().getTaxonomy().getCommonName(), query, case_sensitive, partial ) ) {
1115 else if ( node.getNodeData().isHasTaxonomy()
1116 && match( node.getNodeData().getTaxonomy().getScientificName(), query, case_sensitive, partial ) ) {
1119 else if ( node.getNodeData().isHasTaxonomy()
1120 && ( node.getNodeData().getTaxonomy().getIdentifier() != null )
1121 && match( node.getNodeData().getTaxonomy().getIdentifier().getValue(),
1127 else if ( node.getNodeData().isHasTaxonomy() && !node.getNodeData().getTaxonomy().getSynonyms().isEmpty() ) {
1128 final List<String> syns = node.getNodeData().getTaxonomy().getSynonyms();
1129 I: for( final String syn : syns ) {
1130 if ( match( syn, query, case_sensitive, partial ) ) {
1136 if ( !match && node.getNodeData().isHasSequence()
1137 && match( node.getNodeData().getSequence().getName(), query, case_sensitive, partial ) ) {
1140 if ( !match && node.getNodeData().isHasSequence()
1141 && match( node.getNodeData().getSequence().getSymbol(), query, case_sensitive, partial ) ) {
1145 && node.getNodeData().isHasSequence()
1146 && ( node.getNodeData().getSequence().getAccession() != null )
1147 && match( node.getNodeData().getSequence().getAccession().getValue(),
1153 if ( !match && node.getNodeData().isHasSequence()
1154 && ( node.getNodeData().getSequence().getDomainArchitecture() != null ) ) {
1155 final DomainArchitecture da = node.getNodeData().getSequence().getDomainArchitecture();
1156 I: for( int i = 0; i < da.getNumberOfDomains(); ++i ) {
1157 if ( match( da.getDomain( i ).getName(), query, case_sensitive, partial ) ) {
1163 if ( !match && ( node.getNodeData().getBinaryCharacters() != null ) ) {
1164 Iterator<String> it = node.getNodeData().getBinaryCharacters().getPresentCharacters().iterator();
1165 I: while ( it.hasNext() ) {
1166 if ( match( it.next(), query, case_sensitive, partial ) ) {
1171 it = node.getNodeData().getBinaryCharacters().getGainedCharacters().iterator();
1172 I: while ( it.hasNext() ) {
1173 if ( match( it.next(), query, case_sensitive, partial ) ) {
1186 public static List<PhylogenyNode> searchDataLogicalAnd( final String[] queries,
1187 final Phylogeny phy,
1188 final boolean case_sensitive,
1189 final boolean partial ) {
1190 final List<PhylogenyNode> nodes = new ArrayList<PhylogenyNode>();
1191 if ( phy.isEmpty() || ( queries == null ) || ( queries.length < 1 ) ) {
1194 for( final PhylogenyNodeIterator iter = phy.iteratorPreorder(); iter.hasNext(); ) {
1195 final PhylogenyNode node = iter.next();
1196 boolean all_matched = true;
1197 for( final String query : queries ) {
1198 boolean match = false;
1199 if ( ForesterUtil.isEmpty( query ) ) {
1202 if ( match( node.getName(), query, case_sensitive, partial ) ) {
1205 else if ( node.getNodeData().isHasTaxonomy()
1206 && match( node.getNodeData().getTaxonomy().getTaxonomyCode(), query, case_sensitive, partial ) ) {
1209 else if ( node.getNodeData().isHasTaxonomy()
1210 && match( node.getNodeData().getTaxonomy().getCommonName(), query, case_sensitive, partial ) ) {
1213 else if ( node.getNodeData().isHasTaxonomy()
1214 && match( node.getNodeData().getTaxonomy().getScientificName(), query, case_sensitive, partial ) ) {
1217 else if ( node.getNodeData().isHasTaxonomy()
1218 && ( node.getNodeData().getTaxonomy().getIdentifier() != null )
1219 && match( node.getNodeData().getTaxonomy().getIdentifier().getValue(),
1225 else if ( node.getNodeData().isHasTaxonomy()
1226 && !node.getNodeData().getTaxonomy().getSynonyms().isEmpty() ) {
1227 final List<String> syns = node.getNodeData().getTaxonomy().getSynonyms();
1228 I: for( final String syn : syns ) {
1229 if ( match( syn, query, case_sensitive, partial ) ) {
1235 if ( !match && node.getNodeData().isHasSequence()
1236 && match( node.getNodeData().getSequence().getName(), query, case_sensitive, partial ) ) {
1239 if ( !match && node.getNodeData().isHasSequence()
1240 && match( node.getNodeData().getSequence().getSymbol(), query, case_sensitive, partial ) ) {
1244 && node.getNodeData().isHasSequence()
1245 && ( node.getNodeData().getSequence().getAccession() != null )
1246 && match( node.getNodeData().getSequence().getAccession().getValue(),
1252 if ( !match && node.getNodeData().isHasSequence()
1253 && ( node.getNodeData().getSequence().getDomainArchitecture() != null ) ) {
1254 final DomainArchitecture da = node.getNodeData().getSequence().getDomainArchitecture();
1255 I: for( int i = 0; i < da.getNumberOfDomains(); ++i ) {
1256 if ( match( da.getDomain( i ).getName(), query, case_sensitive, partial ) ) {
1262 if ( !match && ( node.getNodeData().getBinaryCharacters() != null ) ) {
1263 Iterator<String> it = node.getNodeData().getBinaryCharacters().getPresentCharacters().iterator();
1264 I: while ( it.hasNext() ) {
1265 if ( match( it.next(), query, case_sensitive, partial ) ) {
1270 it = node.getNodeData().getBinaryCharacters().getGainedCharacters().iterator();
1271 I: while ( it.hasNext() ) {
1272 if ( match( it.next(), query, case_sensitive, partial ) ) {
1277 // final String[] bcp_ary = node.getNodeData().getBinaryCharacters()
1278 // .getPresentCharactersAsStringArray();
1279 // I: for( final String bc : bcp_ary ) {
1280 // if ( match( bc, query, case_sensitive, partial ) ) {
1285 // final String[] bcg_ary = node.getNodeData().getBinaryCharacters()
1286 // .getGainedCharactersAsStringArray();
1287 // I: for( final String bc : bcg_ary ) {
1288 // if ( match( bc, query, case_sensitive, partial ) ) {
1295 all_matched = false;
1299 if ( all_matched ) {
1307 * Convenience method.
1308 * Sets value for the first confidence value (created if not present, values overwritten otherwise).
1310 public static void setBootstrapConfidence( final PhylogenyNode node, final double bootstrap_confidence_value ) {
1311 setConfidence( node, bootstrap_confidence_value, "bootstrap" );
1314 public static void setBranchColorValue( final PhylogenyNode node, final Color color ) {
1315 if ( node.getBranchData().getBranchColor() == null ) {
1316 node.getBranchData().setBranchColor( new BranchColor() );
1318 node.getBranchData().getBranchColor().setValue( color );
1322 * Convenience method
1324 public static void setBranchWidthValue( final PhylogenyNode node, final double branch_width_value ) {
1325 node.getBranchData().setBranchWidth( new BranchWidth( branch_width_value ) );
1329 * Convenience method.
1330 * Sets value for the first confidence value (created if not present, values overwritten otherwise).
1332 public static void setConfidence( final PhylogenyNode node, final double confidence_value ) {
1333 setConfidence( node, confidence_value, "" );
1337 * Convenience method.
1338 * Sets value for the first confidence value (created if not present, values overwritten otherwise).
1340 public static void setConfidence( final PhylogenyNode node, final double confidence_value, final String type ) {
1341 Confidence c = null;
1342 if ( node.getBranchData().getNumberOfConfidences() > 0 ) {
1343 c = node.getBranchData().getConfidence( 0 );
1346 c = new Confidence();
1347 node.getBranchData().addConfidence( c );
1350 c.setValue( confidence_value );
1353 public static void setScientificName( final PhylogenyNode node, final String scientific_name ) {
1354 if ( !node.getNodeData().isHasTaxonomy() ) {
1355 node.getNodeData().setTaxonomy( new Taxonomy() );
1357 node.getNodeData().getTaxonomy().setScientificName( scientific_name );
1361 * Convenience method to set the taxonomy code of a phylogeny node.
1365 * @param taxonomy_code
1367 public static void setTaxonomyCode( final PhylogenyNode node, final String taxonomy_code ) {
1368 if ( !node.getNodeData().isHasTaxonomy() ) {
1369 node.getNodeData().setTaxonomy( new Taxonomy() );
1371 node.getNodeData().getTaxonomy().setTaxonomyCode( taxonomy_code );
1375 * Removes from Phylogeny to_be_stripped all external Nodes which are
1376 * associated with a species NOT found in Phylogeny reference.
1379 * a reference Phylogeny
1380 * @param to_be_stripped
1381 * Phylogeny to be stripped
1382 * @return number of external nodes removed from to_be_stripped
1384 public static int taxonomyBasedDeletionOfExternalNodes( final Phylogeny reference, final Phylogeny to_be_stripped ) {
1385 final Set<String> ref_ext_taxo = new HashSet<String>();
1386 final ArrayList<PhylogenyNode> nodes_to_delete = new ArrayList<PhylogenyNode>();
1387 for( final PhylogenyNodeIterator it = reference.iteratorExternalForward(); it.hasNext(); ) {
1388 ref_ext_taxo.add( getSpecies( it.next() ) );
1390 for( final PhylogenyNodeIterator it = to_be_stripped.iteratorExternalForward(); it.hasNext(); ) {
1391 final PhylogenyNode n = it.next();
1392 if ( !ref_ext_taxo.contains( getSpecies( n ) ) ) {
1393 nodes_to_delete.add( n );
1396 for( final PhylogenyNode phylogenyNode : nodes_to_delete ) {
1397 to_be_stripped.deleteSubtree( phylogenyNode, true );
1399 return nodes_to_delete.size();
1402 public static enum PhylogenyNodeField {
1405 TAXONOMY_SCIENTIFIC_NAME,
1406 TAXONOMY_COMMON_NAME,
1409 TAXONOMY_ID_UNIPROT_1,
1410 TAXONOMY_ID_UNIPROT_2;
1413 public static enum TAXONOMY_EXTRACTION {
1414 NO, YES, PFAM_STYLE_ONLY;