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: https://sites.google.com/site/cmzmasek/home/software/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.Collections;
34 import java.util.Comparator;
35 import java.util.HashMap;
36 import java.util.HashSet;
37 import java.util.Iterator;
38 import java.util.List;
41 import java.util.regex.Matcher;
42 import java.util.regex.Pattern;
44 import org.forester.io.parsers.FastaParser;
45 import org.forester.io.parsers.PhylogenyParser;
46 import org.forester.io.parsers.phyloxml.PhyloXmlDataFormatException;
47 import org.forester.io.parsers.phyloxml.PhyloXmlUtil;
48 import org.forester.io.parsers.util.PhylogenyParserException;
49 import org.forester.phylogeny.data.Accession;
50 import org.forester.phylogeny.data.Annotation;
51 import org.forester.phylogeny.data.BranchColor;
52 import org.forester.phylogeny.data.BranchWidth;
53 import org.forester.phylogeny.data.Confidence;
54 import org.forester.phylogeny.data.DomainArchitecture;
55 import org.forester.phylogeny.data.Event;
56 import org.forester.phylogeny.data.Identifier;
57 import org.forester.phylogeny.data.PhylogenyDataUtil;
58 import org.forester.phylogeny.data.Sequence;
59 import org.forester.phylogeny.data.Taxonomy;
60 import org.forester.phylogeny.factories.ParserBasedPhylogenyFactory;
61 import org.forester.phylogeny.factories.PhylogenyFactory;
62 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
63 import org.forester.util.BasicDescriptiveStatistics;
64 import org.forester.util.DescriptiveStatistics;
65 import org.forester.util.ForesterUtil;
67 public class PhylogenyMethods {
69 private PhylogenyMethods() {
70 // Hidden constructor.
74 public Object clone() throws CloneNotSupportedException {
75 throw new CloneNotSupportedException();
78 public static boolean extractFastaInformation( final Phylogeny phy ) {
79 boolean could_extract = false;
80 for( final PhylogenyNodeIterator iter = phy.iteratorExternalForward(); iter.hasNext(); ) {
81 final PhylogenyNode node = iter.next();
82 if ( !ForesterUtil.isEmpty( node.getName() ) ) {
83 final Matcher name_m = FastaParser.FASTA_DESC_LINE.matcher( node.getName() );
84 if ( name_m.lookingAt() ) {
86 final String acc_source = name_m.group( 1 );
87 final String acc = name_m.group( 2 );
88 final String seq_name = name_m.group( 3 );
89 final String tax_sn = name_m.group( 4 );
90 if ( !ForesterUtil.isEmpty( acc_source ) && !ForesterUtil.isEmpty( acc ) ) {
91 ForesterUtil.ensurePresenceOfSequence( node );
92 node.getNodeData().getSequence( 0 ).setAccession( new Accession( acc, acc_source ) );
94 if ( !ForesterUtil.isEmpty( seq_name ) ) {
95 ForesterUtil.ensurePresenceOfSequence( node );
96 node.getNodeData().getSequence( 0 ).setName( seq_name );
98 if ( !ForesterUtil.isEmpty( tax_sn ) ) {
99 ForesterUtil.ensurePresenceOfTaxonomy( node );
100 node.getNodeData().getTaxonomy( 0 ).setScientificName( tax_sn );
105 return could_extract;
108 public static DescriptiveStatistics calculatBranchLengthStatistics( final Phylogeny phy ) {
109 final DescriptiveStatistics stats = new BasicDescriptiveStatistics();
110 for( final PhylogenyNodeIterator iter = phy.iteratorPreorder(); iter.hasNext(); ) {
111 final PhylogenyNode n = iter.next();
112 if ( !n.isRoot() && ( n.getDistanceToParent() >= 0.0 ) ) {
113 stats.addValue( n.getDistanceToParent() );
119 public static List<DescriptiveStatistics> calculatConfidenceStatistics( final Phylogeny phy ) {
120 final List<DescriptiveStatistics> stats = new ArrayList<DescriptiveStatistics>();
121 for( final PhylogenyNodeIterator iter = phy.iteratorPreorder(); iter.hasNext(); ) {
122 final PhylogenyNode n = iter.next();
123 if ( !n.isExternal() && !n.isRoot() ) {
124 if ( n.getBranchData().isHasConfidences() ) {
125 for( int i = 0; i < n.getBranchData().getConfidences().size(); ++i ) {
126 final Confidence c = n.getBranchData().getConfidences().get( i );
127 if ( ( i > ( stats.size() - 1 ) ) || ( stats.get( i ) == null ) ) {
128 stats.add( i, new BasicDescriptiveStatistics() );
130 if ( !ForesterUtil.isEmpty( c.getType() ) ) {
131 if ( !ForesterUtil.isEmpty( stats.get( i ).getDescription() ) ) {
132 if ( !stats.get( i ).getDescription().equalsIgnoreCase( c.getType() ) ) {
133 throw new IllegalArgumentException( "support values in node [" + n.toString()
134 + "] appear inconsistently ordered" );
137 stats.get( i ).setDescription( c.getType() );
139 stats.get( i ).addValue( ( ( c != null ) && ( c.getValue() >= 0 ) ) ? c.getValue() : 0 );
148 * Calculates the distance between PhylogenyNodes node1 and node2.
153 * @return distance between node1 and node2
155 public static double calculateDistance( final PhylogenyNode node1, final PhylogenyNode node2 ) {
156 final PhylogenyNode lca = calculateLCA( node1, node2 );
157 final PhylogenyNode n1 = node1;
158 final PhylogenyNode n2 = node2;
159 return ( PhylogenyMethods.getDistance( n1, lca ) + PhylogenyMethods.getDistance( n2, lca ) );
163 * Returns the LCA of PhylogenyNodes node1 and node2.
168 * @return LCA of node1 and node2
170 public final static PhylogenyNode calculateLCA( PhylogenyNode node1, PhylogenyNode node2 ) {
171 if ( node1 == null ) {
172 throw new IllegalArgumentException( "first argument (node) is null" );
174 if ( node2 == null ) {
175 throw new IllegalArgumentException( "second argument (node) is null" );
177 if ( node1 == node2 ) {
180 if ( ( node1.getParent() == node2.getParent() ) ) {
181 return node1.getParent();
183 int depth1 = node1.calculateDepth();
184 int depth2 = node2.calculateDepth();
185 while ( ( depth1 > -1 ) && ( depth2 > -1 ) ) {
186 if ( depth1 > depth2 ) {
187 node1 = node1.getParent();
190 else if ( depth2 > depth1 ) {
191 node2 = node2.getParent();
195 if ( node1 == node2 ) {
198 node1 = node1.getParent();
199 node2 = node2.getParent();
204 throw new IllegalArgumentException( "illegal attempt to calculate LCA of two nodes which do not share a common root" );
208 * Returns the LCA of PhylogenyNodes node1 and node2.
209 * Precondition: ids are in pre-order (or level-order).
214 * @return LCA of node1 and node2
216 public final static PhylogenyNode calculateLCAonTreeWithIdsInPreOrder( PhylogenyNode node1, PhylogenyNode node2 ) {
217 if ( node1 == null ) {
218 throw new IllegalArgumentException( "first argument (node) is null" );
220 if ( node2 == null ) {
221 throw new IllegalArgumentException( "second argument (node) is null" );
223 while ( node1 != node2 ) {
224 if ( node1.getId() > node2.getId() ) {
225 node1 = node1.getParent();
228 node2 = node2.getParent();
234 public static short calculateMaxBranchesToLeaf( final PhylogenyNode node ) {
235 if ( node.isExternal() ) {
239 for( PhylogenyNode d : node.getAllExternalDescendants() ) {
241 while ( d != node ) {
242 if ( d.isCollapse() ) {
257 public static int calculateMaxDepth( final Phylogeny phy ) {
259 for( final PhylogenyNodeIterator iter = phy.iteratorExternalForward(); iter.hasNext(); ) {
260 final PhylogenyNode node = iter.next();
261 final int steps = node.calculateDepth();
269 public static double calculateMaxDistanceToRoot( final Phylogeny phy ) {
271 for( final PhylogenyNodeIterator iter = phy.iteratorExternalForward(); iter.hasNext(); ) {
272 final PhylogenyNode node = iter.next();
273 final double d = node.calculateDistanceToRoot();
281 public static int calculateNumberOfExternalNodesWithoutTaxonomy( final PhylogenyNode node ) {
282 final List<PhylogenyNode> descs = node.getAllExternalDescendants();
284 for( final PhylogenyNode n : descs ) {
285 if ( !n.getNodeData().isHasTaxonomy() || n.getNodeData().getTaxonomy().isEmpty() ) {
292 public static DescriptiveStatistics calculatNumberOfDescendantsPerNodeStatistics( final Phylogeny phy ) {
293 final DescriptiveStatistics stats = new BasicDescriptiveStatistics();
294 for( final PhylogenyNodeIterator iter = phy.iteratorPreorder(); iter.hasNext(); ) {
295 final PhylogenyNode n = iter.next();
296 if ( !n.isExternal() ) {
297 stats.addValue( n.getNumberOfDescendants() );
303 public final static void collapseSubtreeStructure( final PhylogenyNode n ) {
304 final List<PhylogenyNode> eds = n.getAllExternalDescendants();
305 final List<Double> d = new ArrayList<Double>();
306 for( final PhylogenyNode ed : eds ) {
307 d.add( calculateDistanceToAncestor( n, ed ) );
309 for( int i = 0; i < eds.size(); ++i ) {
310 n.setChildNode( i, eds.get( i ) );
311 eds.get( i ).setDistanceToParent( d.get( i ) );
315 public static int countNumberOfOneDescendantNodes( final Phylogeny phy ) {
317 for( final PhylogenyNodeIterator iter = phy.iteratorPreorder(); iter.hasNext(); ) {
318 final PhylogenyNode n = iter.next();
319 if ( !n.isExternal() && ( n.getNumberOfDescendants() == 1 ) ) {
326 public static int countNumberOfPolytomies( final Phylogeny phy ) {
328 for( final PhylogenyNodeIterator iter = phy.iteratorPreorder(); iter.hasNext(); ) {
329 final PhylogenyNode n = iter.next();
330 if ( !n.isExternal() && ( n.getNumberOfDescendants() > 2 ) ) {
337 public static final HashMap<String, PhylogenyNode> createNameToExtNodeMap( final Phylogeny phy ) {
338 final HashMap<String, PhylogenyNode> nodes = new HashMap<String, PhylogenyNode>();
339 final List<PhylogenyNode> ext = phy.getExternalNodes();
340 for( final PhylogenyNode n : ext ) {
341 nodes.put( n.getName(), n );
346 public static void deleteExternalNodesNegativeSelection( final Set<Long> to_delete, final Phylogeny phy ) {
347 for( final Long id : to_delete ) {
348 phy.deleteSubtree( phy.getNode( id ), true );
350 phy.clearHashIdToNodeMap();
351 phy.externalNodesHaveChanged();
354 public static void deleteExternalNodesNegativeSelection( final String[] node_names_to_delete, final Phylogeny p )
355 throws IllegalArgumentException {
356 for( final String element : node_names_to_delete ) {
357 if ( ForesterUtil.isEmpty( element ) ) {
360 List<PhylogenyNode> nodes = null;
361 nodes = p.getNodes( element );
362 final Iterator<PhylogenyNode> it = nodes.iterator();
363 while ( it.hasNext() ) {
364 final PhylogenyNode n = it.next();
365 if ( !n.isExternal() ) {
366 throw new IllegalArgumentException( "attempt to delete non-external node \"" + element + "\"" );
368 p.deleteSubtree( n, true );
371 p.clearHashIdToNodeMap();
372 p.externalNodesHaveChanged();
375 public static List<String> deleteExternalNodesPositiveSelection( final String[] node_names_to_keep,
376 final Phylogeny p ) {
377 final PhylogenyNodeIterator it = p.iteratorExternalForward();
378 final String[] to_delete = new String[ p.getNumberOfExternalNodes() ];
380 Arrays.sort( node_names_to_keep );
381 while ( it.hasNext() ) {
382 final String curent_name = it.next().getName();
383 if ( Arrays.binarySearch( node_names_to_keep, curent_name ) < 0 ) {
384 to_delete[ i++ ] = curent_name;
387 PhylogenyMethods.deleteExternalNodesNegativeSelection( to_delete, p );
388 final List<String> deleted = new ArrayList<String>();
389 for( final String n : to_delete ) {
390 if ( !ForesterUtil.isEmpty( n ) ) {
397 public static void deleteExternalNodesPositiveSelectionT( final List<Taxonomy> species_to_keep, final Phylogeny phy ) {
398 final Set<Long> to_delete = new HashSet<Long>();
399 for( final PhylogenyNodeIterator it = phy.iteratorExternalForward(); it.hasNext(); ) {
400 final PhylogenyNode n = it.next();
401 if ( n.getNodeData().isHasTaxonomy() ) {
402 if ( !species_to_keep.contains( n.getNodeData().getTaxonomy() ) ) {
403 to_delete.add( n.getId() );
407 throw new IllegalArgumentException( "node " + n.getId() + " has no taxonomic data" );
410 deleteExternalNodesNegativeSelection( to_delete, phy );
413 final public static void deleteInternalNodesWithOnlyOneDescendent( final Phylogeny phy ) {
414 final ArrayList<PhylogenyNode> to_delete = new ArrayList<PhylogenyNode>();
415 for( final PhylogenyNodeIterator iter = phy.iteratorPostorder(); iter.hasNext(); ) {
416 final PhylogenyNode n = iter.next();
417 if ( ( !n.isExternal() ) && ( n.getNumberOfDescendants() == 1 ) ) {
421 for( final PhylogenyNode d : to_delete ) {
422 PhylogenyMethods.removeNode( d, phy );
424 phy.clearHashIdToNodeMap();
425 phy.externalNodesHaveChanged();
428 final public static void deleteNonOrthologousExternalNodes( final Phylogeny phy, final PhylogenyNode n ) {
429 if ( n.isInternal() ) {
430 throw new IllegalArgumentException( "node is not external" );
432 final ArrayList<PhylogenyNode> to_delete = new ArrayList<PhylogenyNode>();
433 for( final PhylogenyNodeIterator it = phy.iteratorExternalForward(); it.hasNext(); ) {
434 final PhylogenyNode i = it.next();
435 if ( !PhylogenyMethods.getEventAtLCA( n, i ).isSpeciation() ) {
439 for( final PhylogenyNode d : to_delete ) {
440 phy.deleteSubtree( d, true );
442 phy.clearHashIdToNodeMap();
443 phy.externalNodesHaveChanged();
446 public final static List<List<PhylogenyNode>> divideIntoSubTrees( final Phylogeny phy,
447 final double min_distance_to_root ) {
448 if ( min_distance_to_root <= 0 ) {
449 throw new IllegalArgumentException( "attempt to use min distance to root of: " + min_distance_to_root );
451 final List<List<PhylogenyNode>> l = new ArrayList<List<PhylogenyNode>>();
452 setAllIndicatorsToZero( phy );
453 for( final PhylogenyNodeIterator it = phy.iteratorExternalForward(); it.hasNext(); ) {
454 final PhylogenyNode n = it.next();
455 if ( n.getIndicator() != 0 ) {
458 l.add( divideIntoSubTreesHelper( n, min_distance_to_root ) );
460 throw new RuntimeException( "this should not have happened" );
466 public static List<PhylogenyNode> getAllDescendants( final PhylogenyNode node ) {
467 final List<PhylogenyNode> descs = new ArrayList<PhylogenyNode>();
468 final Set<Long> encountered = new HashSet<Long>();
469 if ( !node.isExternal() ) {
470 final List<PhylogenyNode> exts = node.getAllExternalDescendants();
471 for( PhylogenyNode current : exts ) {
472 descs.add( current );
473 while ( current != node ) {
474 current = current.getParent();
475 if ( encountered.contains( current.getId() ) ) {
478 descs.add( current );
479 encountered.add( current.getId() );
493 public static Color getBranchColorValue( final PhylogenyNode node ) {
494 if ( node.getBranchData().getBranchColor() == null ) {
497 return node.getBranchData().getBranchColor().getValue();
503 public static double getBranchWidthValue( final PhylogenyNode node ) {
504 if ( !node.getBranchData().isHasBranchWidth() ) {
505 return BranchWidth.BRANCH_WIDTH_DEFAULT_VALUE;
507 return node.getBranchData().getBranchWidth().getValue();
513 public static double getConfidenceValue( final PhylogenyNode node ) {
514 if ( !node.getBranchData().isHasConfidences() ) {
515 return Confidence.CONFIDENCE_DEFAULT_VALUE;
517 return node.getBranchData().getConfidence( 0 ).getValue();
523 public static double[] getConfidenceValuesAsArray( final PhylogenyNode node ) {
524 if ( !node.getBranchData().isHasConfidences() ) {
525 return new double[ 0 ];
527 final double[] values = new double[ node.getBranchData().getConfidences().size() ];
529 for( final Confidence c : node.getBranchData().getConfidences() ) {
530 values[ i++ ] = c.getValue();
535 final public static Event getEventAtLCA( final PhylogenyNode n1, final PhylogenyNode n2 ) {
536 return calculateLCA( n1, n2 ).getNodeData().getEvent();
540 * Returns taxonomy t if all external descendants have
541 * the same taxonomy t, null otherwise.
544 public static Taxonomy getExternalDescendantsTaxonomy( final PhylogenyNode node ) {
545 final List<PhylogenyNode> descs = node.getAllExternalDescendants();
547 for( final PhylogenyNode n : descs ) {
548 if ( !n.getNodeData().isHasTaxonomy() || n.getNodeData().getTaxonomy().isEmpty() ) {
551 else if ( tax == null ) {
552 tax = n.getNodeData().getTaxonomy();
554 else if ( n.getNodeData().getTaxonomy().isEmpty() || !tax.isEqual( n.getNodeData().getTaxonomy() ) ) {
561 public static PhylogenyNode getFurthestDescendant( final PhylogenyNode node ) {
562 final List<PhylogenyNode> children = node.getAllExternalDescendants();
563 PhylogenyNode farthest = null;
564 double longest = -Double.MAX_VALUE;
565 for( final PhylogenyNode child : children ) {
566 if ( PhylogenyMethods.getDistance( child, node ) > longest ) {
568 longest = PhylogenyMethods.getDistance( child, node );
574 // public static PhylogenyMethods getInstance() {
575 // if ( PhylogenyMethods._instance == null ) {
576 // PhylogenyMethods._instance = new PhylogenyMethods();
578 // return PhylogenyMethods._instance;
581 * Returns the largest confidence value found on phy.
583 static public double getMaximumConfidenceValue( final Phylogeny phy ) {
584 double max = -Double.MAX_VALUE;
585 for( final PhylogenyNodeIterator iter = phy.iteratorPreorder(); iter.hasNext(); ) {
586 final double s = PhylogenyMethods.getConfidenceValue( iter.next() );
587 if ( ( s != Confidence.CONFIDENCE_DEFAULT_VALUE ) && ( s > max ) ) {
594 static public int getMinimumDescendentsPerInternalNodes( final Phylogeny phy ) {
595 int min = Integer.MAX_VALUE;
598 for( final PhylogenyNodeIterator it = phy.iteratorPreorder(); it.hasNext(); ) {
600 if ( n.isInternal() ) {
601 d = n.getNumberOfDescendants();
611 * Convenience method for display purposes.
612 * Not intended for algorithms.
614 public static String getSpecies( final PhylogenyNode node ) {
615 if ( !node.getNodeData().isHasTaxonomy() ) {
618 else if ( !ForesterUtil.isEmpty( node.getNodeData().getTaxonomy().getScientificName() ) ) {
619 return node.getNodeData().getTaxonomy().getScientificName();
621 if ( !ForesterUtil.isEmpty( node.getNodeData().getTaxonomy().getTaxonomyCode() ) ) {
622 return node.getNodeData().getTaxonomy().getTaxonomyCode();
625 return node.getNodeData().getTaxonomy().getCommonName();
630 * Convenience method for display purposes.
631 * Not intended for algorithms.
633 public static String getTaxonomyIdentifier( final PhylogenyNode node ) {
634 if ( !node.getNodeData().isHasTaxonomy() || ( node.getNodeData().getTaxonomy().getIdentifier() == null ) ) {
637 return node.getNodeData().getTaxonomy().getIdentifier().getValue();
640 public final static boolean isAllDecendentsAreDuplications( final PhylogenyNode n ) {
641 if ( n.isExternal() ) {
645 if ( n.isDuplication() ) {
646 for( final PhylogenyNode desc : n.getDescendants() ) {
647 if ( !isAllDecendentsAreDuplications( desc ) ) {
659 public static boolean isHasExternalDescendant( final PhylogenyNode node ) {
660 for( int i = 0; i < node.getNumberOfDescendants(); ++i ) {
661 if ( node.getChildNode( i ).isExternal() ) {
669 * This is case insensitive.
672 public synchronized static boolean isTaxonomyHasIdentifierOfGivenProvider( final Taxonomy tax,
673 final String[] providers ) {
674 if ( ( tax.getIdentifier() != null ) && !ForesterUtil.isEmpty( tax.getIdentifier().getProvider() ) ) {
675 final String my_tax_prov = tax.getIdentifier().getProvider();
676 for( final String provider : providers ) {
677 if ( provider.equalsIgnoreCase( my_tax_prov ) ) {
688 public static void midpointRoot( final Phylogeny phylogeny ) {
689 if ( ( phylogeny.getNumberOfExternalNodes() < 2 ) || ( calculateMaxDistanceToRoot( phylogeny ) <= 0 ) ) {
693 final int total_nodes = phylogeny.getNodeCount();
695 if ( ++counter > total_nodes ) {
696 throw new RuntimeException( "this should not have happened: midpoint rooting does not converge" );
698 PhylogenyNode a = null;
701 for( int i = 0; i < phylogeny.getRoot().getNumberOfDescendants(); ++i ) {
702 final PhylogenyNode f = getFurthestDescendant( phylogeny.getRoot().getChildNode( i ) );
703 final double df = getDistance( f, phylogeny.getRoot() );
710 else if ( df > db ) {
715 final double diff = da - db;
716 if ( diff < 0.000001 ) {
719 double x = da - ( diff / 2.0 );
720 while ( ( x > a.getDistanceToParent() ) && !a.isRoot() ) {
721 x -= ( a.getDistanceToParent() > 0 ? a.getDistanceToParent() : 0 );
724 phylogeny.reRoot( a, x );
726 phylogeny.recalculateNumberOfExternalDescendants( true );
729 public static void normalizeBootstrapValues( final Phylogeny phylogeny,
730 final double max_bootstrap_value,
731 final double max_normalized_value ) {
732 for( final PhylogenyNodeIterator iter = phylogeny.iteratorPreorder(); iter.hasNext(); ) {
733 final PhylogenyNode node = iter.next();
734 if ( node.isInternal() ) {
735 final double confidence = getConfidenceValue( node );
736 if ( confidence != Confidence.CONFIDENCE_DEFAULT_VALUE ) {
737 if ( confidence >= max_bootstrap_value ) {
738 setBootstrapConfidence( node, max_normalized_value );
741 setBootstrapConfidence( node, ( confidence * max_normalized_value ) / max_bootstrap_value );
748 public static List<PhylogenyNode> obtainAllNodesAsList( final Phylogeny phy ) {
749 final List<PhylogenyNode> nodes = new ArrayList<PhylogenyNode>();
750 if ( phy.isEmpty() ) {
753 for( final PhylogenyNodeIterator iter = phy.iteratorPreorder(); iter.hasNext(); ) {
754 nodes.add( iter.next() );
760 * Returns a map of distinct taxonomies of
761 * all external nodes of node.
762 * If at least one of the external nodes has no taxonomy,
766 public static Map<Taxonomy, Integer> obtainDistinctTaxonomyCounts( final PhylogenyNode node ) {
767 final List<PhylogenyNode> descs = node.getAllExternalDescendants();
768 final Map<Taxonomy, Integer> tax_map = new HashMap<Taxonomy, Integer>();
769 for( final PhylogenyNode n : descs ) {
770 if ( !n.getNodeData().isHasTaxonomy() || n.getNodeData().getTaxonomy().isEmpty() ) {
773 final Taxonomy t = n.getNodeData().getTaxonomy();
774 if ( tax_map.containsKey( t ) ) {
775 tax_map.put( t, tax_map.get( t ) + 1 );
785 * Arranges the order of childern for each node of this Phylogeny in such a
786 * way that either the branch with more children is on top (right) or on
787 * bottom (left), dependent on the value of boolean order.
790 * decides in which direction to order
793 public static void orderAppearance( final PhylogenyNode n,
795 final boolean order_ext_alphabetically,
796 final DESCENDANT_SORT_PRIORITY pri ) {
797 if ( n.isExternal() ) {
801 PhylogenyNode temp = null;
802 if ( ( n.getNumberOfDescendants() == 2 )
803 && ( n.getChildNode1().getNumberOfExternalNodes() != n.getChildNode2().getNumberOfExternalNodes() )
804 && ( ( n.getChildNode1().getNumberOfExternalNodes() < n.getChildNode2().getNumberOfExternalNodes() ) == order ) ) {
805 temp = n.getChildNode1();
806 n.setChild1( n.getChildNode2() );
809 else if ( order_ext_alphabetically ) {
810 boolean all_ext = true;
811 for( final PhylogenyNode i : n.getDescendants() ) {
812 if ( !i.isExternal() ) {
818 PhylogenyMethods.sortNodeDescendents( n, pri );
821 for( int i = 0; i < n.getNumberOfDescendants(); ++i ) {
822 orderAppearance( n.getChildNode( i ), order, order_ext_alphabetically, pri );
827 public static void postorderBranchColorAveragingExternalNodeBased( final Phylogeny p ) {
828 for( final PhylogenyNodeIterator iter = p.iteratorPostorder(); iter.hasNext(); ) {
829 final PhylogenyNode node = iter.next();
834 if ( node.isInternal() ) {
835 //for( final PhylogenyNodeIterator iterator = node.iterateChildNodesForward(); iterator.hasNext(); ) {
836 for( int i = 0; i < node.getNumberOfDescendants(); ++i ) {
837 final PhylogenyNode child_node = node.getChildNode( i );
838 final Color child_color = getBranchColorValue( child_node );
839 if ( child_color != null ) {
841 red += child_color.getRed();
842 green += child_color.getGreen();
843 blue += child_color.getBlue();
846 setBranchColorValue( node,
847 new Color( ForesterUtil.roundToInt( red / n ),
848 ForesterUtil.roundToInt( green / n ),
849 ForesterUtil.roundToInt( blue / n ) ) );
854 public static final void preOrderReId( final Phylogeny phy ) {
855 if ( phy.isEmpty() ) {
858 phy.setIdToNodeMap( null );
859 long i = PhylogenyNode.getNodeCount();
860 for( final PhylogenyNodeIterator it = phy.iteratorPreorder(); it.hasNext(); ) {
861 it.next().setId( i++ );
863 PhylogenyNode.setNodeCount( i );
866 public final static Phylogeny[] readPhylogenies( final PhylogenyParser parser, final File file ) throws IOException {
867 final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
868 final Phylogeny[] trees = factory.create( file, parser );
869 if ( ( trees == null ) || ( trees.length == 0 ) ) {
870 throw new PhylogenyParserException( "Unable to parse phylogeny from file: " + file );
875 public final static Phylogeny[] readPhylogenies( final PhylogenyParser parser, final List<File> files )
877 final List<Phylogeny> tree_list = new ArrayList<Phylogeny>();
878 for( final File file : files ) {
879 final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
880 final Phylogeny[] trees = factory.create( file, parser );
881 if ( ( trees == null ) || ( trees.length == 0 ) ) {
882 throw new PhylogenyParserException( "Unable to parse phylogeny from file: " + file );
884 tree_list.addAll( Arrays.asList( trees ) );
886 return tree_list.toArray( new Phylogeny[ tree_list.size() ] );
889 public static void removeNode( final PhylogenyNode remove_me, final Phylogeny phylogeny ) {
890 if ( remove_me.isRoot() ) {
891 if ( remove_me.getNumberOfDescendants() == 1 ) {
892 final PhylogenyNode desc = remove_me.getDescendants().get( 0 );
893 desc.setDistanceToParent( addPhylogenyDistances( remove_me.getDistanceToParent(),
894 desc.getDistanceToParent() ) );
895 desc.setParent( null );
896 phylogeny.setRoot( desc );
897 phylogeny.clearHashIdToNodeMap();
900 throw new IllegalArgumentException( "attempt to remove a root node with more than one descendants" );
903 else if ( remove_me.isExternal() ) {
904 phylogeny.deleteSubtree( remove_me, false );
905 phylogeny.clearHashIdToNodeMap();
906 phylogeny.externalNodesHaveChanged();
909 final PhylogenyNode parent = remove_me.getParent();
910 final List<PhylogenyNode> descs = remove_me.getDescendants();
911 parent.removeChildNode( remove_me );
912 for( final PhylogenyNode desc : descs ) {
913 parent.addAsChild( desc );
914 desc.setDistanceToParent( addPhylogenyDistances( remove_me.getDistanceToParent(),
915 desc.getDistanceToParent() ) );
917 remove_me.setParent( null );
918 phylogeny.clearHashIdToNodeMap();
919 phylogeny.externalNodesHaveChanged();
923 public static List<PhylogenyNode> searchData( final String query,
925 final boolean case_sensitive,
926 final boolean partial,
927 final boolean search_domains ) {
928 final List<PhylogenyNode> nodes = new ArrayList<PhylogenyNode>();
929 if ( phy.isEmpty() || ( query == null ) ) {
932 if ( ForesterUtil.isEmpty( query ) ) {
935 for( final PhylogenyNodeIterator iter = phy.iteratorPreorder(); iter.hasNext(); ) {
936 final PhylogenyNode node = iter.next();
937 boolean match = false;
938 if ( match( node.getName(), query, case_sensitive, partial ) ) {
941 else if ( node.getNodeData().isHasTaxonomy()
942 && match( node.getNodeData().getTaxonomy().getTaxonomyCode(), query, case_sensitive, partial ) ) {
945 else if ( node.getNodeData().isHasTaxonomy()
946 && match( node.getNodeData().getTaxonomy().getCommonName(), query, case_sensitive, partial ) ) {
949 else if ( node.getNodeData().isHasTaxonomy()
950 && match( node.getNodeData().getTaxonomy().getScientificName(), query, case_sensitive, partial ) ) {
953 else if ( node.getNodeData().isHasTaxonomy()
954 && ( node.getNodeData().getTaxonomy().getIdentifier() != null )
955 && match( node.getNodeData().getTaxonomy().getIdentifier().getValue(),
961 else if ( node.getNodeData().isHasTaxonomy() && !node.getNodeData().getTaxonomy().getSynonyms().isEmpty() ) {
962 final List<String> syns = node.getNodeData().getTaxonomy().getSynonyms();
963 I: for( final String syn : syns ) {
964 if ( match( syn, query, case_sensitive, partial ) ) {
970 if ( !match && node.getNodeData().isHasSequence()
971 && match( node.getNodeData().getSequence().getName(), query, case_sensitive, partial ) ) {
974 if ( !match && node.getNodeData().isHasSequence()
975 && match( node.getNodeData().getSequence().getGeneName(), query, case_sensitive, partial ) ) {
978 if ( !match && node.getNodeData().isHasSequence()
979 && match( node.getNodeData().getSequence().getSymbol(), query, case_sensitive, partial ) ) {
983 && node.getNodeData().isHasSequence()
984 && ( node.getNodeData().getSequence().getAccession() != null )
985 && match( node.getNodeData().getSequence().getAccession().getValue(),
991 if ( search_domains && !match && node.getNodeData().isHasSequence()
992 && ( node.getNodeData().getSequence().getDomainArchitecture() != null ) ) {
993 final DomainArchitecture da = node.getNodeData().getSequence().getDomainArchitecture();
994 I: for( int i = 0; i < da.getNumberOfDomains(); ++i ) {
995 if ( match( da.getDomain( i ).getName(), query, case_sensitive, partial ) ) {
1002 if ( !match && node.getNodeData().isHasSequence()
1003 && ( node.getNodeData().getSequence().getAnnotations() != null ) ) {
1004 for( final Annotation ann : node.getNodeData().getSequence().getAnnotations() ) {
1005 if ( match( ann.getDesc(), query, case_sensitive, partial ) ) {
1009 if ( match( ann.getRef(), query, case_sensitive, partial ) ) {
1015 if ( !match && node.getNodeData().isHasSequence()
1016 && ( node.getNodeData().getSequence().getCrossReferences() != null ) ) {
1017 for( final Accession x : node.getNodeData().getSequence().getCrossReferences() ) {
1018 if ( match( x.getComment(), query, case_sensitive, partial ) ) {
1022 if ( match( x.getSource(), query, case_sensitive, partial ) ) {
1026 if ( match( x.getValue(), query, case_sensitive, partial ) ) {
1033 if ( !match && ( node.getNodeData().getBinaryCharacters() != null ) ) {
1034 Iterator<String> it = node.getNodeData().getBinaryCharacters().getPresentCharacters().iterator();
1035 I: while ( it.hasNext() ) {
1036 if ( match( it.next(), query, case_sensitive, partial ) ) {
1041 it = node.getNodeData().getBinaryCharacters().getGainedCharacters().iterator();
1042 I: while ( it.hasNext() ) {
1043 if ( match( it.next(), query, case_sensitive, partial ) ) {
1056 public static List<PhylogenyNode> searchDataLogicalAnd( final String[] queries,
1057 final Phylogeny phy,
1058 final boolean case_sensitive,
1059 final boolean partial,
1060 final boolean search_domains ) {
1061 final List<PhylogenyNode> nodes = new ArrayList<PhylogenyNode>();
1062 if ( phy.isEmpty() || ( queries == null ) || ( queries.length < 1 ) ) {
1065 for( final PhylogenyNodeIterator iter = phy.iteratorPreorder(); iter.hasNext(); ) {
1066 final PhylogenyNode node = iter.next();
1067 boolean all_matched = true;
1068 for( final String query : queries ) {
1069 boolean match = false;
1070 if ( ForesterUtil.isEmpty( query ) ) {
1073 if ( match( node.getName(), query, case_sensitive, partial ) ) {
1076 else if ( node.getNodeData().isHasTaxonomy()
1077 && match( node.getNodeData().getTaxonomy().getTaxonomyCode(), query, case_sensitive, partial ) ) {
1080 else if ( node.getNodeData().isHasTaxonomy()
1081 && match( node.getNodeData().getTaxonomy().getCommonName(), query, case_sensitive, partial ) ) {
1084 else if ( node.getNodeData().isHasTaxonomy()
1085 && match( node.getNodeData().getTaxonomy().getScientificName(), query, case_sensitive, partial ) ) {
1088 else if ( node.getNodeData().isHasTaxonomy()
1089 && ( node.getNodeData().getTaxonomy().getIdentifier() != null )
1090 && match( node.getNodeData().getTaxonomy().getIdentifier().getValue(),
1096 else if ( node.getNodeData().isHasTaxonomy()
1097 && !node.getNodeData().getTaxonomy().getSynonyms().isEmpty() ) {
1098 final List<String> syns = node.getNodeData().getTaxonomy().getSynonyms();
1099 I: for( final String syn : syns ) {
1100 if ( match( syn, query, case_sensitive, partial ) ) {
1106 if ( !match && node.getNodeData().isHasSequence()
1107 && match( node.getNodeData().getSequence().getName(), query, case_sensitive, partial ) ) {
1110 if ( !match && node.getNodeData().isHasSequence()
1111 && match( node.getNodeData().getSequence().getGeneName(), query, case_sensitive, partial ) ) {
1114 if ( !match && node.getNodeData().isHasSequence()
1115 && match( node.getNodeData().getSequence().getSymbol(), query, case_sensitive, partial ) ) {
1119 && node.getNodeData().isHasSequence()
1120 && ( node.getNodeData().getSequence().getAccession() != null )
1121 && match( node.getNodeData().getSequence().getAccession().getValue(),
1127 if ( search_domains && !match && node.getNodeData().isHasSequence()
1128 && ( node.getNodeData().getSequence().getDomainArchitecture() != null ) ) {
1129 final DomainArchitecture da = node.getNodeData().getSequence().getDomainArchitecture();
1130 I: for( int i = 0; i < da.getNumberOfDomains(); ++i ) {
1131 if ( match( da.getDomain( i ).getName(), query, case_sensitive, partial ) ) {
1138 if ( !match && node.getNodeData().isHasSequence()
1139 && ( node.getNodeData().getSequence().getAnnotations() != null ) ) {
1140 for( final Annotation ann : node.getNodeData().getSequence().getAnnotations() ) {
1141 if ( match( ann.getDesc(), query, case_sensitive, partial ) ) {
1145 if ( match( ann.getRef(), query, case_sensitive, partial ) ) {
1151 if ( !match && node.getNodeData().isHasSequence()
1152 && ( node.getNodeData().getSequence().getCrossReferences() != null ) ) {
1153 for( final Accession x : node.getNodeData().getSequence().getCrossReferences() ) {
1154 if ( match( x.getComment(), query, case_sensitive, partial ) ) {
1158 if ( match( x.getSource(), query, case_sensitive, partial ) ) {
1162 if ( match( x.getValue(), query, case_sensitive, partial ) ) {
1169 if ( !match && ( node.getNodeData().getBinaryCharacters() != null ) ) {
1170 Iterator<String> it = node.getNodeData().getBinaryCharacters().getPresentCharacters().iterator();
1171 I: while ( it.hasNext() ) {
1172 if ( match( it.next(), query, case_sensitive, partial ) ) {
1177 it = node.getNodeData().getBinaryCharacters().getGainedCharacters().iterator();
1178 I: while ( it.hasNext() ) {
1179 if ( match( it.next(), query, case_sensitive, partial ) ) {
1186 all_matched = false;
1190 if ( all_matched ) {
1197 public static void setAllIndicatorsToZero( final Phylogeny phy ) {
1198 for( final PhylogenyNodeIterator it = phy.iteratorPostorder(); it.hasNext(); ) {
1199 it.next().setIndicator( ( byte ) 0 );
1204 * Convenience method.
1205 * Sets value for the first confidence value (created if not present, values overwritten otherwise).
1207 public static void setBootstrapConfidence( final PhylogenyNode node, final double bootstrap_confidence_value ) {
1208 setConfidence( node, bootstrap_confidence_value, "bootstrap" );
1211 public static void setBranchColorValue( final PhylogenyNode node, final Color color ) {
1212 if ( node.getBranchData().getBranchColor() == null ) {
1213 node.getBranchData().setBranchColor( new BranchColor() );
1215 node.getBranchData().getBranchColor().setValue( color );
1219 * Convenience method
1221 public static void setBranchWidthValue( final PhylogenyNode node, final double branch_width_value ) {
1222 node.getBranchData().setBranchWidth( new BranchWidth( branch_width_value ) );
1226 * Convenience method.
1227 * Sets value for the first confidence value (created if not present, values overwritten otherwise).
1229 public static void setConfidence( final PhylogenyNode node, final double confidence_value ) {
1230 setConfidence( node, confidence_value, "" );
1234 * Convenience method.
1235 * Sets value for the first confidence value (created if not present, values overwritten otherwise).
1237 public static void setConfidence( final PhylogenyNode node, final double confidence_value, final String type ) {
1238 Confidence c = null;
1239 if ( node.getBranchData().getNumberOfConfidences() > 0 ) {
1240 c = node.getBranchData().getConfidence( 0 );
1243 c = new Confidence();
1244 node.getBranchData().addConfidence( c );
1247 c.setValue( confidence_value );
1250 public static void setScientificName( final PhylogenyNode node, final String scientific_name ) {
1251 if ( !node.getNodeData().isHasTaxonomy() ) {
1252 node.getNodeData().setTaxonomy( new Taxonomy() );
1254 node.getNodeData().getTaxonomy().setScientificName( scientific_name );
1258 * Convenience method to set the taxonomy code of a phylogeny node.
1262 * @param taxonomy_code
1263 * @throws PhyloXmlDataFormatException
1265 public static void setTaxonomyCode( final PhylogenyNode node, final String taxonomy_code )
1266 throws PhyloXmlDataFormatException {
1267 if ( !node.getNodeData().isHasTaxonomy() ) {
1268 node.getNodeData().setTaxonomy( new Taxonomy() );
1270 node.getNodeData().getTaxonomy().setTaxonomyCode( taxonomy_code );
1273 final static public void sortNodeDescendents( final PhylogenyNode node, final DESCENDANT_SORT_PRIORITY pri ) {
1274 class PhylogenyNodeSortTaxonomyPriority implements Comparator<PhylogenyNode> {
1277 public int compare( final PhylogenyNode n1, final PhylogenyNode n2 ) {
1278 if ( n1.getNodeData().isHasTaxonomy() && n2.getNodeData().isHasTaxonomy() ) {
1279 if ( ( !ForesterUtil.isEmpty( n1.getNodeData().getTaxonomy().getScientificName() ) )
1280 && ( !ForesterUtil.isEmpty( n2.getNodeData().getTaxonomy().getScientificName() ) ) ) {
1281 return n1.getNodeData().getTaxonomy().getScientificName().toLowerCase()
1282 .compareTo( n2.getNodeData().getTaxonomy().getScientificName().toLowerCase() );
1284 if ( ( !ForesterUtil.isEmpty( n1.getNodeData().getTaxonomy().getTaxonomyCode() ) )
1285 && ( !ForesterUtil.isEmpty( n2.getNodeData().getTaxonomy().getTaxonomyCode() ) ) ) {
1286 return n1.getNodeData().getTaxonomy().getTaxonomyCode()
1287 .compareTo( n2.getNodeData().getTaxonomy().getTaxonomyCode() );
1289 if ( ( !ForesterUtil.isEmpty( n1.getNodeData().getTaxonomy().getCommonName() ) )
1290 && ( !ForesterUtil.isEmpty( n2.getNodeData().getTaxonomy().getCommonName() ) ) ) {
1291 return n1.getNodeData().getTaxonomy().getCommonName().toLowerCase()
1292 .compareTo( n2.getNodeData().getTaxonomy().getCommonName().toLowerCase() );
1295 if ( n1.getNodeData().isHasSequence() && n2.getNodeData().isHasSequence() ) {
1296 if ( ( !ForesterUtil.isEmpty( n1.getNodeData().getSequence().getName() ) )
1297 && ( !ForesterUtil.isEmpty( n2.getNodeData().getSequence().getName() ) ) ) {
1298 return n1.getNodeData().getSequence().getName().toLowerCase()
1299 .compareTo( n2.getNodeData().getSequence().getName().toLowerCase() );
1301 if ( ( !ForesterUtil.isEmpty( n1.getNodeData().getSequence().getSymbol() ) )
1302 && ( !ForesterUtil.isEmpty( n2.getNodeData().getSequence().getSymbol() ) ) ) {
1303 return n1.getNodeData().getSequence().getSymbol()
1304 .compareTo( n2.getNodeData().getSequence().getSymbol() );
1306 if ( ( !ForesterUtil.isEmpty( n1.getNodeData().getSequence().getGeneName() ) )
1307 && ( !ForesterUtil.isEmpty( n2.getNodeData().getSequence().getGeneName() ) ) ) {
1308 return n1.getNodeData().getSequence().getGeneName()
1309 .compareTo( n2.getNodeData().getSequence().getGeneName() );
1311 if ( ( n1.getNodeData().getSequence().getAccession() != null )
1312 && ( n2.getNodeData().getSequence().getAccession() != null )
1313 && !ForesterUtil.isEmpty( n1.getNodeData().getSequence().getAccession().getValue() )
1314 && !ForesterUtil.isEmpty( n2.getNodeData().getSequence().getAccession().getValue() ) ) {
1315 return n1.getNodeData().getSequence().getAccession().getValue()
1316 .compareTo( n2.getNodeData().getSequence().getAccession().getValue() );
1319 if ( ( !ForesterUtil.isEmpty( n1.getName() ) ) && ( !ForesterUtil.isEmpty( n2.getName() ) ) ) {
1320 return n1.getName().toLowerCase().compareTo( n2.getName().toLowerCase() );
1325 class PhylogenyNodeSortSequencePriority implements Comparator<PhylogenyNode> {
1328 public int compare( final PhylogenyNode n1, final PhylogenyNode n2 ) {
1329 if ( n1.getNodeData().isHasSequence() && n2.getNodeData().isHasSequence() ) {
1330 if ( ( !ForesterUtil.isEmpty( n1.getNodeData().getSequence().getName() ) )
1331 && ( !ForesterUtil.isEmpty( n2.getNodeData().getSequence().getName() ) ) ) {
1332 return n1.getNodeData().getSequence().getName().toLowerCase()
1333 .compareTo( n2.getNodeData().getSequence().getName().toLowerCase() );
1335 if ( ( !ForesterUtil.isEmpty( n1.getNodeData().getSequence().getSymbol() ) )
1336 && ( !ForesterUtil.isEmpty( n2.getNodeData().getSequence().getSymbol() ) ) ) {
1337 return n1.getNodeData().getSequence().getSymbol()
1338 .compareTo( n2.getNodeData().getSequence().getSymbol() );
1340 if ( ( !ForesterUtil.isEmpty( n1.getNodeData().getSequence().getGeneName() ) )
1341 && ( !ForesterUtil.isEmpty( n2.getNodeData().getSequence().getGeneName() ) ) ) {
1342 return n1.getNodeData().getSequence().getGeneName()
1343 .compareTo( n2.getNodeData().getSequence().getGeneName() );
1345 if ( ( n1.getNodeData().getSequence().getAccession() != null )
1346 && ( n2.getNodeData().getSequence().getAccession() != null )
1347 && !ForesterUtil.isEmpty( n1.getNodeData().getSequence().getAccession().getValue() )
1348 && !ForesterUtil.isEmpty( n2.getNodeData().getSequence().getAccession().getValue() ) ) {
1349 return n1.getNodeData().getSequence().getAccession().getValue()
1350 .compareTo( n2.getNodeData().getSequence().getAccession().getValue() );
1353 if ( n1.getNodeData().isHasTaxonomy() && n2.getNodeData().isHasTaxonomy() ) {
1354 if ( ( !ForesterUtil.isEmpty( n1.getNodeData().getTaxonomy().getScientificName() ) )
1355 && ( !ForesterUtil.isEmpty( n2.getNodeData().getTaxonomy().getScientificName() ) ) ) {
1356 return n1.getNodeData().getTaxonomy().getScientificName().toLowerCase()
1357 .compareTo( n2.getNodeData().getTaxonomy().getScientificName().toLowerCase() );
1359 if ( ( !ForesterUtil.isEmpty( n1.getNodeData().getTaxonomy().getTaxonomyCode() ) )
1360 && ( !ForesterUtil.isEmpty( n2.getNodeData().getTaxonomy().getTaxonomyCode() ) ) ) {
1361 return n1.getNodeData().getTaxonomy().getTaxonomyCode()
1362 .compareTo( n2.getNodeData().getTaxonomy().getTaxonomyCode() );
1364 if ( ( !ForesterUtil.isEmpty( n1.getNodeData().getTaxonomy().getCommonName() ) )
1365 && ( !ForesterUtil.isEmpty( n2.getNodeData().getTaxonomy().getCommonName() ) ) ) {
1366 return n1.getNodeData().getTaxonomy().getCommonName().toLowerCase()
1367 .compareTo( n2.getNodeData().getTaxonomy().getCommonName().toLowerCase() );
1370 if ( ( !ForesterUtil.isEmpty( n1.getName() ) ) && ( !ForesterUtil.isEmpty( n2.getName() ) ) ) {
1371 return n1.getName().toLowerCase().compareTo( n2.getName().toLowerCase() );
1376 class PhylogenyNodeSortNodeNamePriority implements Comparator<PhylogenyNode> {
1379 public int compare( final PhylogenyNode n1, final PhylogenyNode n2 ) {
1380 if ( ( !ForesterUtil.isEmpty( n1.getName() ) ) && ( !ForesterUtil.isEmpty( n2.getName() ) ) ) {
1381 return n1.getName().toLowerCase().compareTo( n2.getName().toLowerCase() );
1383 if ( n1.getNodeData().isHasTaxonomy() && n2.getNodeData().isHasTaxonomy() ) {
1384 if ( ( !ForesterUtil.isEmpty( n1.getNodeData().getTaxonomy().getScientificName() ) )
1385 && ( !ForesterUtil.isEmpty( n2.getNodeData().getTaxonomy().getScientificName() ) ) ) {
1386 return n1.getNodeData().getTaxonomy().getScientificName().toLowerCase()
1387 .compareTo( n2.getNodeData().getTaxonomy().getScientificName().toLowerCase() );
1389 if ( ( !ForesterUtil.isEmpty( n1.getNodeData().getTaxonomy().getTaxonomyCode() ) )
1390 && ( !ForesterUtil.isEmpty( n2.getNodeData().getTaxonomy().getTaxonomyCode() ) ) ) {
1391 return n1.getNodeData().getTaxonomy().getTaxonomyCode()
1392 .compareTo( n2.getNodeData().getTaxonomy().getTaxonomyCode() );
1394 if ( ( !ForesterUtil.isEmpty( n1.getNodeData().getTaxonomy().getCommonName() ) )
1395 && ( !ForesterUtil.isEmpty( n2.getNodeData().getTaxonomy().getCommonName() ) ) ) {
1396 return n1.getNodeData().getTaxonomy().getCommonName().toLowerCase()
1397 .compareTo( n2.getNodeData().getTaxonomy().getCommonName().toLowerCase() );
1400 if ( n1.getNodeData().isHasSequence() && n2.getNodeData().isHasSequence() ) {
1401 if ( ( !ForesterUtil.isEmpty( n1.getNodeData().getSequence().getName() ) )
1402 && ( !ForesterUtil.isEmpty( n2.getNodeData().getSequence().getName() ) ) ) {
1403 return n1.getNodeData().getSequence().getName().toLowerCase()
1404 .compareTo( n2.getNodeData().getSequence().getName().toLowerCase() );
1406 if ( ( !ForesterUtil.isEmpty( n1.getNodeData().getSequence().getSymbol() ) )
1407 && ( !ForesterUtil.isEmpty( n2.getNodeData().getSequence().getSymbol() ) ) ) {
1408 return n1.getNodeData().getSequence().getSymbol()
1409 .compareTo( n2.getNodeData().getSequence().getSymbol() );
1411 if ( ( !ForesterUtil.isEmpty( n1.getNodeData().getSequence().getGeneName() ) )
1412 && ( !ForesterUtil.isEmpty( n2.getNodeData().getSequence().getGeneName() ) ) ) {
1413 return n1.getNodeData().getSequence().getGeneName()
1414 .compareTo( n2.getNodeData().getSequence().getGeneName() );
1416 if ( ( n1.getNodeData().getSequence().getAccession() != null )
1417 && ( n2.getNodeData().getSequence().getAccession() != null )
1418 && !ForesterUtil.isEmpty( n1.getNodeData().getSequence().getAccession().getValue() )
1419 && !ForesterUtil.isEmpty( n2.getNodeData().getSequence().getAccession().getValue() ) ) {
1420 return n1.getNodeData().getSequence().getAccession().getValue()
1421 .compareTo( n2.getNodeData().getSequence().getAccession().getValue() );
1427 Comparator<PhylogenyNode> c;
1430 c = new PhylogenyNodeSortSequencePriority();
1433 c = new PhylogenyNodeSortNodeNamePriority();
1436 c = new PhylogenyNodeSortTaxonomyPriority();
1438 final List<PhylogenyNode> descs = node.getDescendants();
1439 Collections.sort( descs, c );
1441 for( final PhylogenyNode desc : descs ) {
1442 node.setChildNode( i++, desc );
1447 * Removes from Phylogeny to_be_stripped all external Nodes which are
1448 * associated with a species NOT found in Phylogeny reference.
1451 * a reference Phylogeny
1452 * @param to_be_stripped
1453 * Phylogeny to be stripped
1454 * @return nodes removed from to_be_stripped
1456 public static List<PhylogenyNode> taxonomyBasedDeletionOfExternalNodes( final Phylogeny reference,
1457 final Phylogeny to_be_stripped ) {
1458 final Set<String> ref_ext_taxo = new HashSet<String>();
1459 for( final PhylogenyNodeIterator it = reference.iteratorExternalForward(); it.hasNext(); ) {
1460 final PhylogenyNode n = it.next();
1461 if ( !n.getNodeData().isHasTaxonomy() ) {
1462 throw new IllegalArgumentException( "no taxonomic data in node: " + n );
1464 if ( !ForesterUtil.isEmpty( n.getNodeData().getTaxonomy().getScientificName() ) ) {
1465 ref_ext_taxo.add( n.getNodeData().getTaxonomy().getScientificName() );
1467 if ( !ForesterUtil.isEmpty( n.getNodeData().getTaxonomy().getTaxonomyCode() ) ) {
1468 ref_ext_taxo.add( n.getNodeData().getTaxonomy().getTaxonomyCode() );
1470 if ( ( n.getNodeData().getTaxonomy().getIdentifier() != null )
1471 && !ForesterUtil.isEmpty( n.getNodeData().getTaxonomy().getIdentifier().getValue() ) ) {
1472 ref_ext_taxo.add( n.getNodeData().getTaxonomy().getIdentifier().getValuePlusProvider() );
1475 final ArrayList<PhylogenyNode> nodes_to_delete = new ArrayList<PhylogenyNode>();
1476 for( final PhylogenyNodeIterator it = to_be_stripped.iteratorExternalForward(); it.hasNext(); ) {
1477 final PhylogenyNode n = it.next();
1478 if ( !n.getNodeData().isHasTaxonomy() ) {
1479 nodes_to_delete.add( n );
1481 else if ( !( ref_ext_taxo.contains( n.getNodeData().getTaxonomy().getScientificName() ) )
1482 && !( ref_ext_taxo.contains( n.getNodeData().getTaxonomy().getTaxonomyCode() ) )
1483 && !( ( n.getNodeData().getTaxonomy().getIdentifier() != null ) && ref_ext_taxo.contains( n
1484 .getNodeData().getTaxonomy().getIdentifier().getValuePlusProvider() ) ) ) {
1485 nodes_to_delete.add( n );
1488 for( final PhylogenyNode n : nodes_to_delete ) {
1489 to_be_stripped.deleteSubtree( n, true );
1491 to_be_stripped.clearHashIdToNodeMap();
1492 to_be_stripped.externalNodesHaveChanged();
1493 return nodes_to_delete;
1496 final static public void transferInternalNamesToBootstrapSupport( final Phylogeny phy ) {
1497 final PhylogenyNodeIterator it = phy.iteratorPostorder();
1498 while ( it.hasNext() ) {
1499 final PhylogenyNode n = it.next();
1500 if ( !n.isExternal() && !ForesterUtil.isEmpty( n.getName() ) ) {
1503 value = Double.parseDouble( n.getName() );
1505 catch ( final NumberFormatException e ) {
1506 throw new IllegalArgumentException( "failed to parse number from [" + n.getName() + "]: "
1507 + e.getLocalizedMessage() );
1509 if ( value >= 0.0 ) {
1510 n.getBranchData().addConfidence( new Confidence( value, "bootstrap" ) );
1517 final static public boolean isInternalNamesLookLikeConfidences( final Phylogeny phy ) {
1518 final PhylogenyNodeIterator it = phy.iteratorPostorder();
1519 while ( it.hasNext() ) {
1520 final PhylogenyNode n = it.next();
1521 if ( !n.isExternal() && !n.isRoot() ) {
1522 if ( !ForesterUtil.isEmpty( n.getName() ) ) {
1525 value = Double.parseDouble( n.getName() );
1527 catch ( final NumberFormatException e ) {
1530 if ( ( value < 0.0 ) || ( value > 100 ) ) {
1539 final static public void transferInternalNodeNamesToConfidence( final Phylogeny phy, final String confidence_type ) {
1540 final PhylogenyNodeIterator it = phy.iteratorPostorder();
1541 while ( it.hasNext() ) {
1542 transferInternalNodeNameToConfidence( confidence_type, it.next() );
1546 private static void transferInternalNodeNameToConfidence( final String confidence_type, final PhylogenyNode n ) {
1547 if ( !n.isExternal() && !n.getBranchData().isHasConfidences() ) {
1548 if ( !ForesterUtil.isEmpty( n.getName() ) ) {
1551 d = Double.parseDouble( n.getName() );
1553 catch ( final Exception e ) {
1557 n.getBranchData().addConfidence( new Confidence( d, confidence_type ) );
1564 final static public void transferNodeNameToField( final Phylogeny phy,
1565 final PhylogenyNodeField field,
1566 final boolean external_only ) throws PhyloXmlDataFormatException {
1567 final PhylogenyNodeIterator it = phy.iteratorPostorder();
1568 while ( it.hasNext() ) {
1569 final PhylogenyNode n = it.next();
1570 if ( external_only && n.isInternal() ) {
1573 final String name = n.getName().trim();
1574 if ( !ForesterUtil.isEmpty( name ) ) {
1578 setTaxonomyCode( n, name );
1580 case TAXONOMY_SCIENTIFIC_NAME:
1582 if ( !n.getNodeData().isHasTaxonomy() ) {
1583 n.getNodeData().setTaxonomy( new Taxonomy() );
1585 n.getNodeData().getTaxonomy().setScientificName( name );
1587 case TAXONOMY_COMMON_NAME:
1589 if ( !n.getNodeData().isHasTaxonomy() ) {
1590 n.getNodeData().setTaxonomy( new Taxonomy() );
1592 n.getNodeData().getTaxonomy().setCommonName( name );
1594 case SEQUENCE_SYMBOL:
1596 if ( !n.getNodeData().isHasSequence() ) {
1597 n.getNodeData().setSequence( new Sequence() );
1599 n.getNodeData().getSequence().setSymbol( name );
1603 if ( !n.getNodeData().isHasSequence() ) {
1604 n.getNodeData().setSequence( new Sequence() );
1606 n.getNodeData().getSequence().setName( name );
1608 case TAXONOMY_ID_UNIPROT_1: {
1609 if ( !n.getNodeData().isHasTaxonomy() ) {
1610 n.getNodeData().setTaxonomy( new Taxonomy() );
1613 final int i = name.indexOf( '_' );
1615 id = name.substring( 0, i );
1620 n.getNodeData().getTaxonomy()
1621 .setIdentifier( new Identifier( id, PhyloXmlUtil.UNIPROT_TAX_PROVIDER ) );
1624 case TAXONOMY_ID_UNIPROT_2: {
1625 if ( !n.getNodeData().isHasTaxonomy() ) {
1626 n.getNodeData().setTaxonomy( new Taxonomy() );
1629 final int i = name.indexOf( '_' );
1631 id = name.substring( i + 1, name.length() );
1636 n.getNodeData().getTaxonomy()
1637 .setIdentifier( new Identifier( id, PhyloXmlUtil.UNIPROT_TAX_PROVIDER ) );
1641 if ( !n.getNodeData().isHasTaxonomy() ) {
1642 n.getNodeData().setTaxonomy( new Taxonomy() );
1644 n.getNodeData().getTaxonomy().setIdentifier( new Identifier( name ) );
1652 static double addPhylogenyDistances( final double a, final double b ) {
1653 if ( ( a >= 0.0 ) && ( b >= 0.0 ) ) {
1656 else if ( a >= 0.0 ) {
1659 else if ( b >= 0.0 ) {
1662 return PhylogenyDataUtil.BRANCH_LENGTH_DEFAULT;
1665 static double calculateDistanceToAncestor( final PhylogenyNode anc, PhylogenyNode desc ) {
1667 boolean all_default = true;
1668 while ( anc != desc ) {
1669 if ( desc.getDistanceToParent() != PhylogenyDataUtil.BRANCH_LENGTH_DEFAULT ) {
1670 d += desc.getDistanceToParent();
1671 if ( all_default ) {
1672 all_default = false;
1675 desc = desc.getParent();
1677 if ( all_default ) {
1678 return PhylogenyDataUtil.BRANCH_LENGTH_DEFAULT;
1684 * Deep copies the phylogeny originating from this node.
1686 static PhylogenyNode copySubTree( final PhylogenyNode source ) {
1687 if ( source == null ) {
1691 final PhylogenyNode newnode = source.copyNodeData();
1692 if ( !source.isExternal() ) {
1693 for( int i = 0; i < source.getNumberOfDescendants(); ++i ) {
1694 newnode.setChildNode( i, PhylogenyMethods.copySubTree( source.getChildNode( i ) ) );
1702 * Shallow copies the phylogeny originating from this node.
1704 static PhylogenyNode copySubTreeShallow( final PhylogenyNode source ) {
1705 if ( source == null ) {
1709 final PhylogenyNode newnode = source.copyNodeDataShallow();
1710 if ( !source.isExternal() ) {
1711 for( int i = 0; i < source.getNumberOfDescendants(); ++i ) {
1712 newnode.setChildNode( i, PhylogenyMethods.copySubTreeShallow( source.getChildNode( i ) ) );
1719 private final static List<PhylogenyNode> divideIntoSubTreesHelper( final PhylogenyNode node,
1720 final double min_distance_to_root ) {
1721 final List<PhylogenyNode> l = new ArrayList<PhylogenyNode>();
1722 final PhylogenyNode r = moveTowardsRoot( node, min_distance_to_root );
1723 for( final PhylogenyNode ext : r.getAllExternalDescendants() ) {
1724 if ( ext.getIndicator() != 0 ) {
1725 throw new RuntimeException( "this should not have happened" );
1727 ext.setIndicator( ( byte ) 1 );
1734 * Calculates the distance between PhylogenyNodes n1 and n2.
1735 * PRECONDITION: n1 is a descendant of n2.
1738 * a descendant of n2
1740 * @return distance between n1 and n2
1742 private static double getDistance( PhylogenyNode n1, final PhylogenyNode n2 ) {
1744 while ( n1 != n2 ) {
1745 if ( n1.getDistanceToParent() > 0.0 ) {
1746 d += n1.getDistanceToParent();
1748 n1 = n1.getParent();
1753 private static boolean match( final String s,
1755 final boolean case_sensitive,
1756 final boolean partial ) {
1757 if ( ForesterUtil.isEmpty( s ) || ForesterUtil.isEmpty( query ) ) {
1760 String my_s = s.trim();
1761 String my_query = query.trim();
1762 if ( !case_sensitive ) {
1763 my_s = my_s.toLowerCase();
1764 my_query = my_query.toLowerCase();
1767 return my_s.indexOf( my_query ) >= 0;
1770 return Pattern.compile( "(\\b|_)" + Pattern.quote( my_query ) + "(\\b|_)" ).matcher( my_s ).find();
1774 private final static PhylogenyNode moveTowardsRoot( final PhylogenyNode node, final double min_distance_to_root ) {
1775 PhylogenyNode n = node;
1776 PhylogenyNode prev = node;
1777 while ( min_distance_to_root < n.calculateDistanceToRoot() ) {
1784 public static enum DESCENDANT_SORT_PRIORITY {
1785 NODE_NAME, SEQUENCE, TAXONOMY;
1788 public static enum PhylogenyNodeField {
1793 TAXONOMY_COMMON_NAME,
1795 TAXONOMY_ID_UNIPROT_1,
1796 TAXONOMY_ID_UNIPROT_2,
1797 TAXONOMY_SCIENTIFIC_NAME;