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 void extractFastaInformation( final Phylogeny phy ) {
79 for( final PhylogenyNodeIterator iter = phy.iteratorExternalForward(); iter.hasNext(); ) {
80 final PhylogenyNode node = iter.next();
81 if ( !ForesterUtil.isEmpty( node.getName() ) ) {
82 final Matcher name_m = FastaParser.FASTA_DESC_LINE.matcher( node.getName() );
83 if ( name_m.lookingAt() ) {
85 // System.out.println( name_m.group( 1 ) );
86 // System.out.println( name_m.group( 2 ) );
87 // System.out.println( name_m.group( 3 ) );
88 // System.out.println( name_m.group( 4 ) );
89 final String acc_source = name_m.group( 1 );
90 final String acc = name_m.group( 2 );
91 final String seq_name = name_m.group( 3 );
92 final String tax_sn = name_m.group( 4 );
93 if ( !ForesterUtil.isEmpty( acc_source ) && !ForesterUtil.isEmpty( acc ) ) {
94 ForesterUtil.ensurePresenceOfSequence( node );
95 node.getNodeData().getSequence( 0 ).setAccession( new Accession( acc, acc_source ) );
97 if ( !ForesterUtil.isEmpty( seq_name ) ) {
98 ForesterUtil.ensurePresenceOfSequence( node );
99 node.getNodeData().getSequence( 0 ).setName( seq_name );
101 if ( !ForesterUtil.isEmpty( tax_sn ) ) {
102 ForesterUtil.ensurePresenceOfTaxonomy( node );
103 node.getNodeData().getTaxonomy( 0 ).setScientificName( tax_sn );
110 public static DescriptiveStatistics calculatBranchLengthStatistics( final Phylogeny phy ) {
111 final DescriptiveStatistics stats = new BasicDescriptiveStatistics();
112 for( final PhylogenyNodeIterator iter = phy.iteratorPreorder(); iter.hasNext(); ) {
113 final PhylogenyNode n = iter.next();
114 if ( !n.isRoot() && ( n.getDistanceToParent() >= 0.0 ) ) {
115 stats.addValue( n.getDistanceToParent() );
121 public static List<DescriptiveStatistics> calculatConfidenceStatistics( final Phylogeny phy ) {
122 final List<DescriptiveStatistics> stats = new ArrayList<DescriptiveStatistics>();
123 for( final PhylogenyNodeIterator iter = phy.iteratorPreorder(); iter.hasNext(); ) {
124 final PhylogenyNode n = iter.next();
125 if ( !n.isExternal() && !n.isRoot() ) {
126 if ( n.getBranchData().isHasConfidences() ) {
127 for( int i = 0; i < n.getBranchData().getConfidences().size(); ++i ) {
128 final Confidence c = n.getBranchData().getConfidences().get( i );
129 if ( ( i > ( stats.size() - 1 ) ) || ( stats.get( i ) == null ) ) {
130 stats.add( i, new BasicDescriptiveStatistics() );
132 if ( !ForesterUtil.isEmpty( c.getType() ) ) {
133 if ( !ForesterUtil.isEmpty( stats.get( i ).getDescription() ) ) {
134 if ( !stats.get( i ).getDescription().equalsIgnoreCase( c.getType() ) ) {
135 throw new IllegalArgumentException( "support values in node [" + n.toString()
136 + "] appear inconsistently ordered" );
139 stats.get( i ).setDescription( c.getType() );
141 stats.get( i ).addValue( ( ( c != null ) && ( c.getValue() >= 0 ) ) ? c.getValue() : 0 );
150 * Calculates the distance between PhylogenyNodes node1 and node2.
155 * @return distance between node1 and node2
157 public static double calculateDistance( final PhylogenyNode node1, final PhylogenyNode node2 ) {
158 final PhylogenyNode lca = calculateLCA( node1, node2 );
159 final PhylogenyNode n1 = node1;
160 final PhylogenyNode n2 = node2;
161 return ( PhylogenyMethods.getDistance( n1, lca ) + PhylogenyMethods.getDistance( n2, lca ) );
165 * Returns the LCA of PhylogenyNodes node1 and node2.
170 * @return LCA of node1 and node2
172 public final static PhylogenyNode calculateLCA( PhylogenyNode node1, PhylogenyNode node2 ) {
173 if ( node1 == null ) {
174 throw new IllegalArgumentException( "first argument (node) is null" );
176 if ( node2 == null ) {
177 throw new IllegalArgumentException( "second argument (node) is null" );
179 if ( node1 == node2 ) {
182 if ( ( node1.getParent() == node2.getParent() ) ) {
183 return node1.getParent();
185 int depth1 = node1.calculateDepth();
186 int depth2 = node2.calculateDepth();
187 while ( ( depth1 > -1 ) && ( depth2 > -1 ) ) {
188 if ( depth1 > depth2 ) {
189 node1 = node1.getParent();
192 else if ( depth2 > depth1 ) {
193 node2 = node2.getParent();
197 if ( node1 == node2 ) {
200 node1 = node1.getParent();
201 node2 = node2.getParent();
206 throw new IllegalArgumentException( "illegal attempt to calculate LCA of two nodes which do not share a common root" );
210 * Returns the LCA of PhylogenyNodes node1 and node2.
211 * Precondition: ids are in pre-order (or level-order).
216 * @return LCA of node1 and node2
218 public final static PhylogenyNode calculateLCAonTreeWithIdsInPreOrder( PhylogenyNode node1, PhylogenyNode node2 ) {
219 if ( node1 == null ) {
220 throw new IllegalArgumentException( "first argument (node) is null" );
222 if ( node2 == null ) {
223 throw new IllegalArgumentException( "second argument (node) is null" );
225 while ( node1 != node2 ) {
226 if ( node1.getId() > node2.getId() ) {
227 node1 = node1.getParent();
230 node2 = node2.getParent();
236 public static short calculateMaxBranchesToLeaf( final PhylogenyNode node ) {
237 if ( node.isExternal() ) {
241 for( PhylogenyNode d : node.getAllExternalDescendants() ) {
243 while ( d != node ) {
244 if ( d.isCollapse() ) {
259 public static int calculateMaxDepth( final Phylogeny phy ) {
261 for( final PhylogenyNodeIterator iter = phy.iteratorExternalForward(); iter.hasNext(); ) {
262 final PhylogenyNode node = iter.next();
263 final int steps = node.calculateDepth();
271 public static double calculateMaxDistanceToRoot( final Phylogeny phy ) {
273 for( final PhylogenyNodeIterator iter = phy.iteratorExternalForward(); iter.hasNext(); ) {
274 final PhylogenyNode node = iter.next();
275 final double d = node.calculateDistanceToRoot();
283 public static int calculateNumberOfExternalNodesWithoutTaxonomy( final PhylogenyNode node ) {
284 final List<PhylogenyNode> descs = node.getAllExternalDescendants();
286 for( final PhylogenyNode n : descs ) {
287 if ( !n.getNodeData().isHasTaxonomy() || n.getNodeData().getTaxonomy().isEmpty() ) {
294 public static DescriptiveStatistics calculatNumberOfDescendantsPerNodeStatistics( final Phylogeny phy ) {
295 final DescriptiveStatistics stats = new BasicDescriptiveStatistics();
296 for( final PhylogenyNodeIterator iter = phy.iteratorPreorder(); iter.hasNext(); ) {
297 final PhylogenyNode n = iter.next();
298 if ( !n.isExternal() ) {
299 stats.addValue( n.getNumberOfDescendants() );
305 public final static void collapseSubtreeStructure( final PhylogenyNode n ) {
306 final List<PhylogenyNode> eds = n.getAllExternalDescendants();
307 final List<Double> d = new ArrayList<Double>();
308 for( final PhylogenyNode ed : eds ) {
309 d.add( calculateDistanceToAncestor( n, ed ) );
311 for( int i = 0; i < eds.size(); ++i ) {
312 n.setChildNode( i, eds.get( i ) );
313 eds.get( i ).setDistanceToParent( d.get( i ) );
317 public static int countNumberOfOneDescendantNodes( final Phylogeny phy ) {
319 for( final PhylogenyNodeIterator iter = phy.iteratorPreorder(); iter.hasNext(); ) {
320 final PhylogenyNode n = iter.next();
321 if ( !n.isExternal() && ( n.getNumberOfDescendants() == 1 ) ) {
328 public static int countNumberOfPolytomies( final Phylogeny phy ) {
330 for( final PhylogenyNodeIterator iter = phy.iteratorPreorder(); iter.hasNext(); ) {
331 final PhylogenyNode n = iter.next();
332 if ( !n.isExternal() && ( n.getNumberOfDescendants() > 2 ) ) {
339 public static final HashMap<String, PhylogenyNode> createNameToExtNodeMap( final Phylogeny phy ) {
340 final HashMap<String, PhylogenyNode> nodes = new HashMap<String, PhylogenyNode>();
341 final List<PhylogenyNode> ext = phy.getExternalNodes();
342 for( final PhylogenyNode n : ext ) {
343 nodes.put( n.getName(), n );
348 public static void deleteExternalNodesNegativeSelection( final Set<Long> to_delete, final Phylogeny phy ) {
349 for( final Long id : to_delete ) {
350 phy.deleteSubtree( phy.getNode( id ), true );
352 phy.clearHashIdToNodeMap();
353 phy.externalNodesHaveChanged();
356 public static void deleteExternalNodesNegativeSelection( final String[] node_names_to_delete, final Phylogeny p )
357 throws IllegalArgumentException {
358 for( final String element : node_names_to_delete ) {
359 if ( ForesterUtil.isEmpty( element ) ) {
362 List<PhylogenyNode> nodes = null;
363 nodes = p.getNodes( element );
364 final Iterator<PhylogenyNode> it = nodes.iterator();
365 while ( it.hasNext() ) {
366 final PhylogenyNode n = it.next();
367 if ( !n.isExternal() ) {
368 throw new IllegalArgumentException( "attempt to delete non-external node \"" + element + "\"" );
370 p.deleteSubtree( n, true );
373 p.clearHashIdToNodeMap();
374 p.externalNodesHaveChanged();
377 public static List<String> deleteExternalNodesPositiveSelection( final String[] node_names_to_keep,
378 final Phylogeny p ) {
379 final PhylogenyNodeIterator it = p.iteratorExternalForward();
380 final String[] to_delete = new String[ p.getNumberOfExternalNodes() ];
382 Arrays.sort( node_names_to_keep );
383 while ( it.hasNext() ) {
384 final String curent_name = it.next().getName();
385 if ( Arrays.binarySearch( node_names_to_keep, curent_name ) < 0 ) {
386 to_delete[ i++ ] = curent_name;
389 PhylogenyMethods.deleteExternalNodesNegativeSelection( to_delete, p );
390 final List<String> deleted = new ArrayList<String>();
391 for( final String n : to_delete ) {
392 if ( !ForesterUtil.isEmpty( n ) ) {
399 public static void deleteExternalNodesPositiveSelectionT( final List<Taxonomy> species_to_keep, final Phylogeny phy ) {
400 final Set<Long> to_delete = new HashSet<Long>();
401 for( final PhylogenyNodeIterator it = phy.iteratorExternalForward(); it.hasNext(); ) {
402 final PhylogenyNode n = it.next();
403 if ( n.getNodeData().isHasTaxonomy() ) {
404 if ( !species_to_keep.contains( n.getNodeData().getTaxonomy() ) ) {
405 to_delete.add( n.getId() );
409 throw new IllegalArgumentException( "node " + n.getId() + " has no taxonomic data" );
412 deleteExternalNodesNegativeSelection( to_delete, phy );
415 final public static void deleteInternalNodesWithOnlyOneDescendent( final Phylogeny phy ) {
416 final ArrayList<PhylogenyNode> to_delete = new ArrayList<PhylogenyNode>();
417 for( final PhylogenyNodeIterator iter = phy.iteratorPostorder(); iter.hasNext(); ) {
418 final PhylogenyNode n = iter.next();
419 if ( ( !n.isExternal() ) && ( n.getNumberOfDescendants() == 1 ) ) {
423 for( final PhylogenyNode d : to_delete ) {
424 PhylogenyMethods.removeNode( d, phy );
426 phy.clearHashIdToNodeMap();
427 phy.externalNodesHaveChanged();
430 final public static void deleteNonOrthologousExternalNodes( final Phylogeny phy, final PhylogenyNode n ) {
431 if ( n.isInternal() ) {
432 throw new IllegalArgumentException( "node is not external" );
434 final ArrayList<PhylogenyNode> to_delete = new ArrayList<PhylogenyNode>();
435 for( final PhylogenyNodeIterator it = phy.iteratorExternalForward(); it.hasNext(); ) {
436 final PhylogenyNode i = it.next();
437 if ( !PhylogenyMethods.getEventAtLCA( n, i ).isSpeciation() ) {
441 for( final PhylogenyNode d : to_delete ) {
442 phy.deleteSubtree( d, true );
444 phy.clearHashIdToNodeMap();
445 phy.externalNodesHaveChanged();
448 public final static List<List<PhylogenyNode>> divideIntoSubTrees( final Phylogeny phy,
449 final double min_distance_to_root ) {
450 if ( min_distance_to_root <= 0 ) {
451 throw new IllegalArgumentException( "attempt to use min distance to root of: " + min_distance_to_root );
453 final List<List<PhylogenyNode>> l = new ArrayList<List<PhylogenyNode>>();
454 setAllIndicatorsToZero( phy );
455 for( final PhylogenyNodeIterator it = phy.iteratorExternalForward(); it.hasNext(); ) {
456 final PhylogenyNode n = it.next();
457 if ( n.getIndicator() != 0 ) {
460 l.add( divideIntoSubTreesHelper( n, min_distance_to_root ) );
462 throw new RuntimeException( "this should not have happened" );
468 public static List<PhylogenyNode> getAllDescendants( final PhylogenyNode node ) {
469 final List<PhylogenyNode> descs = new ArrayList<PhylogenyNode>();
470 final Set<Long> encountered = new HashSet<Long>();
471 if ( !node.isExternal() ) {
472 final List<PhylogenyNode> exts = node.getAllExternalDescendants();
473 for( PhylogenyNode current : exts ) {
474 descs.add( current );
475 while ( current != node ) {
476 current = current.getParent();
477 if ( encountered.contains( current.getId() ) ) {
480 descs.add( current );
481 encountered.add( current.getId() );
495 public static Color getBranchColorValue( final PhylogenyNode node ) {
496 if ( node.getBranchData().getBranchColor() == null ) {
499 return node.getBranchData().getBranchColor().getValue();
505 public static double getBranchWidthValue( final PhylogenyNode node ) {
506 if ( !node.getBranchData().isHasBranchWidth() ) {
507 return BranchWidth.BRANCH_WIDTH_DEFAULT_VALUE;
509 return node.getBranchData().getBranchWidth().getValue();
515 public static double getConfidenceValue( final PhylogenyNode node ) {
516 if ( !node.getBranchData().isHasConfidences() ) {
517 return Confidence.CONFIDENCE_DEFAULT_VALUE;
519 return node.getBranchData().getConfidence( 0 ).getValue();
525 public static double[] getConfidenceValuesAsArray( final PhylogenyNode node ) {
526 if ( !node.getBranchData().isHasConfidences() ) {
527 return new double[ 0 ];
529 final double[] values = new double[ node.getBranchData().getConfidences().size() ];
531 for( final Confidence c : node.getBranchData().getConfidences() ) {
532 values[ i++ ] = c.getValue();
537 final public static Event getEventAtLCA( final PhylogenyNode n1, final PhylogenyNode n2 ) {
538 return calculateLCA( n1, n2 ).getNodeData().getEvent();
542 * Returns taxonomy t if all external descendants have
543 * the same taxonomy t, null otherwise.
546 public static Taxonomy getExternalDescendantsTaxonomy( final PhylogenyNode node ) {
547 final List<PhylogenyNode> descs = node.getAllExternalDescendants();
549 for( final PhylogenyNode n : descs ) {
550 if ( !n.getNodeData().isHasTaxonomy() || n.getNodeData().getTaxonomy().isEmpty() ) {
553 else if ( tax == null ) {
554 tax = n.getNodeData().getTaxonomy();
556 else if ( n.getNodeData().getTaxonomy().isEmpty() || !tax.isEqual( n.getNodeData().getTaxonomy() ) ) {
563 public static PhylogenyNode getFurthestDescendant( final PhylogenyNode node ) {
564 final List<PhylogenyNode> children = node.getAllExternalDescendants();
565 PhylogenyNode farthest = null;
566 double longest = -Double.MAX_VALUE;
567 for( final PhylogenyNode child : children ) {
568 if ( PhylogenyMethods.getDistance( child, node ) > longest ) {
570 longest = PhylogenyMethods.getDistance( child, node );
576 // public static PhylogenyMethods getInstance() {
577 // if ( PhylogenyMethods._instance == null ) {
578 // PhylogenyMethods._instance = new PhylogenyMethods();
580 // return PhylogenyMethods._instance;
583 * Returns the largest confidence value found on phy.
585 static public double getMaximumConfidenceValue( final Phylogeny phy ) {
586 double max = -Double.MAX_VALUE;
587 for( final PhylogenyNodeIterator iter = phy.iteratorPreorder(); iter.hasNext(); ) {
588 final double s = PhylogenyMethods.getConfidenceValue( iter.next() );
589 if ( ( s != Confidence.CONFIDENCE_DEFAULT_VALUE ) && ( s > max ) ) {
596 static public int getMinimumDescendentsPerInternalNodes( final Phylogeny phy ) {
597 int min = Integer.MAX_VALUE;
600 for( final PhylogenyNodeIterator it = phy.iteratorPreorder(); it.hasNext(); ) {
602 if ( n.isInternal() ) {
603 d = n.getNumberOfDescendants();
613 * Convenience method for display purposes.
614 * Not intended for algorithms.
616 public static String getSpecies( final PhylogenyNode node ) {
617 if ( !node.getNodeData().isHasTaxonomy() ) {
620 else if ( !ForesterUtil.isEmpty( node.getNodeData().getTaxonomy().getScientificName() ) ) {
621 return node.getNodeData().getTaxonomy().getScientificName();
623 if ( !ForesterUtil.isEmpty( node.getNodeData().getTaxonomy().getTaxonomyCode() ) ) {
624 return node.getNodeData().getTaxonomy().getTaxonomyCode();
627 return node.getNodeData().getTaxonomy().getCommonName();
632 * Convenience method for display purposes.
633 * Not intended for algorithms.
635 public static String getTaxonomyIdentifier( final PhylogenyNode node ) {
636 if ( !node.getNodeData().isHasTaxonomy() || ( node.getNodeData().getTaxonomy().getIdentifier() == null ) ) {
639 return node.getNodeData().getTaxonomy().getIdentifier().getValue();
642 public final static boolean isAllDecendentsAreDuplications( final PhylogenyNode n ) {
643 if ( n.isExternal() ) {
647 if ( n.isDuplication() ) {
648 for( final PhylogenyNode desc : n.getDescendants() ) {
649 if ( !isAllDecendentsAreDuplications( desc ) ) {
661 public static boolean isHasExternalDescendant( final PhylogenyNode node ) {
662 for( int i = 0; i < node.getNumberOfDescendants(); ++i ) {
663 if ( node.getChildNode( i ).isExternal() ) {
671 * This is case insensitive.
674 public synchronized static boolean isTaxonomyHasIdentifierOfGivenProvider( final Taxonomy tax,
675 final String[] providers ) {
676 if ( ( tax.getIdentifier() != null ) && !ForesterUtil.isEmpty( tax.getIdentifier().getProvider() ) ) {
677 final String my_tax_prov = tax.getIdentifier().getProvider();
678 for( final String provider : providers ) {
679 if ( provider.equalsIgnoreCase( my_tax_prov ) ) {
690 public static void midpointRoot( final Phylogeny phylogeny ) {
691 if ( ( phylogeny.getNumberOfExternalNodes() < 2 ) || ( calculateMaxDistanceToRoot( phylogeny ) <= 0 ) ) {
695 final int total_nodes = phylogeny.getNodeCount();
697 if ( ++counter > total_nodes ) {
698 throw new RuntimeException( "this should not have happened: midpoint rooting does not converge" );
700 PhylogenyNode a = null;
703 for( int i = 0; i < phylogeny.getRoot().getNumberOfDescendants(); ++i ) {
704 final PhylogenyNode f = getFurthestDescendant( phylogeny.getRoot().getChildNode( i ) );
705 final double df = getDistance( f, phylogeny.getRoot() );
712 else if ( df > db ) {
717 final double diff = da - db;
718 if ( diff < 0.000001 ) {
721 double x = da - ( diff / 2.0 );
722 while ( ( x > a.getDistanceToParent() ) && !a.isRoot() ) {
723 x -= ( a.getDistanceToParent() > 0 ? a.getDistanceToParent() : 0 );
726 phylogeny.reRoot( a, x );
728 phylogeny.recalculateNumberOfExternalDescendants( true );
731 public static void normalizeBootstrapValues( final Phylogeny phylogeny,
732 final double max_bootstrap_value,
733 final double max_normalized_value ) {
734 for( final PhylogenyNodeIterator iter = phylogeny.iteratorPreorder(); iter.hasNext(); ) {
735 final PhylogenyNode node = iter.next();
736 if ( node.isInternal() ) {
737 final double confidence = getConfidenceValue( node );
738 if ( confidence != Confidence.CONFIDENCE_DEFAULT_VALUE ) {
739 if ( confidence >= max_bootstrap_value ) {
740 setBootstrapConfidence( node, max_normalized_value );
743 setBootstrapConfidence( node, ( confidence * max_normalized_value ) / max_bootstrap_value );
750 public static List<PhylogenyNode> obtainAllNodesAsList( final Phylogeny phy ) {
751 final List<PhylogenyNode> nodes = new ArrayList<PhylogenyNode>();
752 if ( phy.isEmpty() ) {
755 for( final PhylogenyNodeIterator iter = phy.iteratorPreorder(); iter.hasNext(); ) {
756 nodes.add( iter.next() );
762 * Returns a map of distinct taxonomies of
763 * all external nodes of node.
764 * If at least one of the external nodes has no taxonomy,
768 public static Map<Taxonomy, Integer> obtainDistinctTaxonomyCounts( final PhylogenyNode node ) {
769 final List<PhylogenyNode> descs = node.getAllExternalDescendants();
770 final Map<Taxonomy, Integer> tax_map = new HashMap<Taxonomy, Integer>();
771 for( final PhylogenyNode n : descs ) {
772 if ( !n.getNodeData().isHasTaxonomy() || n.getNodeData().getTaxonomy().isEmpty() ) {
775 final Taxonomy t = n.getNodeData().getTaxonomy();
776 if ( tax_map.containsKey( t ) ) {
777 tax_map.put( t, tax_map.get( t ) + 1 );
787 * Arranges the order of childern for each node of this Phylogeny in such a
788 * way that either the branch with more children is on top (right) or on
789 * bottom (left), dependent on the value of boolean order.
792 * decides in which direction to order
795 public static void orderAppearance( final PhylogenyNode n,
797 final boolean order_ext_alphabetically,
798 final DESCENDANT_SORT_PRIORITY pri ) {
799 if ( n.isExternal() ) {
803 PhylogenyNode temp = null;
804 if ( ( n.getNumberOfDescendants() == 2 )
805 && ( n.getChildNode1().getNumberOfExternalNodes() != n.getChildNode2().getNumberOfExternalNodes() )
806 && ( ( n.getChildNode1().getNumberOfExternalNodes() < n.getChildNode2().getNumberOfExternalNodes() ) == order ) ) {
807 temp = n.getChildNode1();
808 n.setChild1( n.getChildNode2() );
811 else if ( order_ext_alphabetically ) {
812 boolean all_ext = true;
813 for( final PhylogenyNode i : n.getDescendants() ) {
814 if ( !i.isExternal() ) {
820 PhylogenyMethods.sortNodeDescendents( n, pri );
823 for( int i = 0; i < n.getNumberOfDescendants(); ++i ) {
824 orderAppearance( n.getChildNode( i ), order, order_ext_alphabetically, pri );
829 public static void postorderBranchColorAveragingExternalNodeBased( final Phylogeny p ) {
830 for( final PhylogenyNodeIterator iter = p.iteratorPostorder(); iter.hasNext(); ) {
831 final PhylogenyNode node = iter.next();
836 if ( node.isInternal() ) {
837 //for( final PhylogenyNodeIterator iterator = node.iterateChildNodesForward(); iterator.hasNext(); ) {
838 for( int i = 0; i < node.getNumberOfDescendants(); ++i ) {
839 final PhylogenyNode child_node = node.getChildNode( i );
840 final Color child_color = getBranchColorValue( child_node );
841 if ( child_color != null ) {
843 red += child_color.getRed();
844 green += child_color.getGreen();
845 blue += child_color.getBlue();
848 setBranchColorValue( node,
849 new Color( ForesterUtil.roundToInt( red / n ),
850 ForesterUtil.roundToInt( green / n ),
851 ForesterUtil.roundToInt( blue / n ) ) );
856 public static final void preOrderReId( final Phylogeny phy ) {
857 if ( phy.isEmpty() ) {
860 phy.setIdToNodeMap( null );
861 long i = PhylogenyNode.getNodeCount();
862 for( final PhylogenyNodeIterator it = phy.iteratorPreorder(); it.hasNext(); ) {
863 it.next().setId( i++ );
865 PhylogenyNode.setNodeCount( i );
868 public final static Phylogeny[] readPhylogenies( final PhylogenyParser parser, final File file ) throws IOException {
869 final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
870 final Phylogeny[] trees = factory.create( file, parser );
871 if ( ( trees == null ) || ( trees.length == 0 ) ) {
872 throw new PhylogenyParserException( "Unable to parse phylogeny from file: " + file );
877 public final static Phylogeny[] readPhylogenies( final PhylogenyParser parser, final List<File> files )
879 final List<Phylogeny> tree_list = new ArrayList<Phylogeny>();
880 for( final File file : files ) {
881 final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
882 final Phylogeny[] trees = factory.create( file, parser );
883 if ( ( trees == null ) || ( trees.length == 0 ) ) {
884 throw new PhylogenyParserException( "Unable to parse phylogeny from file: " + file );
886 tree_list.addAll( Arrays.asList( trees ) );
888 return tree_list.toArray( new Phylogeny[ tree_list.size() ] );
891 public static void removeNode( final PhylogenyNode remove_me, final Phylogeny phylogeny ) {
892 if ( remove_me.isRoot() ) {
893 if ( remove_me.getNumberOfDescendants() == 1 ) {
894 final PhylogenyNode desc = remove_me.getDescendants().get( 0 );
895 desc.setDistanceToParent( addPhylogenyDistances( remove_me.getDistanceToParent(),
896 desc.getDistanceToParent() ) );
897 desc.setParent( null );
898 phylogeny.setRoot( desc );
899 phylogeny.clearHashIdToNodeMap();
902 throw new IllegalArgumentException( "attempt to remove a root node with more than one descendants" );
905 else if ( remove_me.isExternal() ) {
906 phylogeny.deleteSubtree( remove_me, false );
907 phylogeny.clearHashIdToNodeMap();
908 phylogeny.externalNodesHaveChanged();
911 final PhylogenyNode parent = remove_me.getParent();
912 final List<PhylogenyNode> descs = remove_me.getDescendants();
913 parent.removeChildNode( remove_me );
914 for( final PhylogenyNode desc : descs ) {
915 parent.addAsChild( desc );
916 desc.setDistanceToParent( addPhylogenyDistances( remove_me.getDistanceToParent(),
917 desc.getDistanceToParent() ) );
919 remove_me.setParent( null );
920 phylogeny.clearHashIdToNodeMap();
921 phylogeny.externalNodesHaveChanged();
925 public static List<PhylogenyNode> searchData( final String query,
927 final boolean case_sensitive,
928 final boolean partial,
929 final boolean search_domains ) {
930 final List<PhylogenyNode> nodes = new ArrayList<PhylogenyNode>();
931 if ( phy.isEmpty() || ( query == null ) ) {
934 if ( ForesterUtil.isEmpty( query ) ) {
937 for( final PhylogenyNodeIterator iter = phy.iteratorPreorder(); iter.hasNext(); ) {
938 final PhylogenyNode node = iter.next();
939 boolean match = false;
940 if ( match( node.getName(), query, case_sensitive, partial ) ) {
943 else if ( node.getNodeData().isHasTaxonomy()
944 && match( node.getNodeData().getTaxonomy().getTaxonomyCode(), query, case_sensitive, partial ) ) {
947 else if ( node.getNodeData().isHasTaxonomy()
948 && match( node.getNodeData().getTaxonomy().getCommonName(), query, case_sensitive, partial ) ) {
951 else if ( node.getNodeData().isHasTaxonomy()
952 && match( node.getNodeData().getTaxonomy().getScientificName(), query, case_sensitive, partial ) ) {
955 else if ( node.getNodeData().isHasTaxonomy()
956 && ( node.getNodeData().getTaxonomy().getIdentifier() != null )
957 && match( node.getNodeData().getTaxonomy().getIdentifier().getValue(),
963 else if ( node.getNodeData().isHasTaxonomy() && !node.getNodeData().getTaxonomy().getSynonyms().isEmpty() ) {
964 final List<String> syns = node.getNodeData().getTaxonomy().getSynonyms();
965 I: for( final String syn : syns ) {
966 if ( match( syn, query, case_sensitive, partial ) ) {
972 if ( !match && node.getNodeData().isHasSequence()
973 && match( node.getNodeData().getSequence().getName(), query, case_sensitive, partial ) ) {
976 if ( !match && node.getNodeData().isHasSequence()
977 && match( node.getNodeData().getSequence().getGeneName(), query, case_sensitive, partial ) ) {
980 if ( !match && node.getNodeData().isHasSequence()
981 && match( node.getNodeData().getSequence().getSymbol(), query, case_sensitive, partial ) ) {
985 && node.getNodeData().isHasSequence()
986 && ( node.getNodeData().getSequence().getAccession() != null )
987 && match( node.getNodeData().getSequence().getAccession().getValue(),
993 if ( search_domains && !match && node.getNodeData().isHasSequence()
994 && ( node.getNodeData().getSequence().getDomainArchitecture() != null ) ) {
995 final DomainArchitecture da = node.getNodeData().getSequence().getDomainArchitecture();
996 I: for( int i = 0; i < da.getNumberOfDomains(); ++i ) {
997 if ( match( da.getDomain( i ).getName(), query, case_sensitive, partial ) ) {
1004 if ( !match && node.getNodeData().isHasSequence()
1005 && ( node.getNodeData().getSequence().getAnnotations() != null ) ) {
1006 for( final Annotation ann : node.getNodeData().getSequence().getAnnotations() ) {
1007 if ( match( ann.getDesc(), query, case_sensitive, partial ) ) {
1011 if ( match( ann.getRef(), query, case_sensitive, partial ) ) {
1017 if ( !match && node.getNodeData().isHasSequence()
1018 && ( node.getNodeData().getSequence().getCrossReferences() != null ) ) {
1019 for( final Accession x : node.getNodeData().getSequence().getCrossReferences() ) {
1020 if ( match( x.getComment(), query, case_sensitive, partial ) ) {
1024 if ( match( x.getSource(), query, case_sensitive, partial ) ) {
1028 if ( match( x.getValue(), query, case_sensitive, partial ) ) {
1035 if ( !match && ( node.getNodeData().getBinaryCharacters() != null ) ) {
1036 Iterator<String> it = node.getNodeData().getBinaryCharacters().getPresentCharacters().iterator();
1037 I: while ( it.hasNext() ) {
1038 if ( match( it.next(), query, case_sensitive, partial ) ) {
1043 it = node.getNodeData().getBinaryCharacters().getGainedCharacters().iterator();
1044 I: while ( it.hasNext() ) {
1045 if ( match( it.next(), query, case_sensitive, partial ) ) {
1058 public static List<PhylogenyNode> searchDataLogicalAnd( final String[] queries,
1059 final Phylogeny phy,
1060 final boolean case_sensitive,
1061 final boolean partial,
1062 final boolean search_domains ) {
1063 final List<PhylogenyNode> nodes = new ArrayList<PhylogenyNode>();
1064 if ( phy.isEmpty() || ( queries == null ) || ( queries.length < 1 ) ) {
1067 for( final PhylogenyNodeIterator iter = phy.iteratorPreorder(); iter.hasNext(); ) {
1068 final PhylogenyNode node = iter.next();
1069 boolean all_matched = true;
1070 for( final String query : queries ) {
1071 boolean match = false;
1072 if ( ForesterUtil.isEmpty( query ) ) {
1075 if ( match( node.getName(), query, case_sensitive, partial ) ) {
1078 else if ( node.getNodeData().isHasTaxonomy()
1079 && match( node.getNodeData().getTaxonomy().getTaxonomyCode(), query, case_sensitive, partial ) ) {
1082 else if ( node.getNodeData().isHasTaxonomy()
1083 && match( node.getNodeData().getTaxonomy().getCommonName(), query, case_sensitive, partial ) ) {
1086 else if ( node.getNodeData().isHasTaxonomy()
1087 && match( node.getNodeData().getTaxonomy().getScientificName(), query, case_sensitive, partial ) ) {
1090 else if ( node.getNodeData().isHasTaxonomy()
1091 && ( node.getNodeData().getTaxonomy().getIdentifier() != null )
1092 && match( node.getNodeData().getTaxonomy().getIdentifier().getValue(),
1098 else if ( node.getNodeData().isHasTaxonomy()
1099 && !node.getNodeData().getTaxonomy().getSynonyms().isEmpty() ) {
1100 final List<String> syns = node.getNodeData().getTaxonomy().getSynonyms();
1101 I: for( final String syn : syns ) {
1102 if ( match( syn, query, case_sensitive, partial ) ) {
1108 if ( !match && node.getNodeData().isHasSequence()
1109 && match( node.getNodeData().getSequence().getName(), query, case_sensitive, partial ) ) {
1112 if ( !match && node.getNodeData().isHasSequence()
1113 && match( node.getNodeData().getSequence().getGeneName(), query, case_sensitive, partial ) ) {
1116 if ( !match && node.getNodeData().isHasSequence()
1117 && match( node.getNodeData().getSequence().getSymbol(), query, case_sensitive, partial ) ) {
1121 && node.getNodeData().isHasSequence()
1122 && ( node.getNodeData().getSequence().getAccession() != null )
1123 && match( node.getNodeData().getSequence().getAccession().getValue(),
1129 if ( search_domains && !match && node.getNodeData().isHasSequence()
1130 && ( node.getNodeData().getSequence().getDomainArchitecture() != null ) ) {
1131 final DomainArchitecture da = node.getNodeData().getSequence().getDomainArchitecture();
1132 I: for( int i = 0; i < da.getNumberOfDomains(); ++i ) {
1133 if ( match( da.getDomain( i ).getName(), query, case_sensitive, partial ) ) {
1140 if ( !match && node.getNodeData().isHasSequence()
1141 && ( node.getNodeData().getSequence().getAnnotations() != null ) ) {
1142 for( final Annotation ann : node.getNodeData().getSequence().getAnnotations() ) {
1143 if ( match( ann.getDesc(), query, case_sensitive, partial ) ) {
1147 if ( match( ann.getRef(), query, case_sensitive, partial ) ) {
1153 if ( !match && node.getNodeData().isHasSequence()
1154 && ( node.getNodeData().getSequence().getCrossReferences() != null ) ) {
1155 for( final Accession x : node.getNodeData().getSequence().getCrossReferences() ) {
1156 if ( match( x.getComment(), query, case_sensitive, partial ) ) {
1160 if ( match( x.getSource(), query, case_sensitive, partial ) ) {
1164 if ( match( x.getValue(), query, case_sensitive, partial ) ) {
1171 if ( !match && ( node.getNodeData().getBinaryCharacters() != null ) ) {
1172 Iterator<String> it = node.getNodeData().getBinaryCharacters().getPresentCharacters().iterator();
1173 I: while ( it.hasNext() ) {
1174 if ( match( it.next(), query, case_sensitive, partial ) ) {
1179 it = node.getNodeData().getBinaryCharacters().getGainedCharacters().iterator();
1180 I: while ( it.hasNext() ) {
1181 if ( match( it.next(), query, case_sensitive, partial ) ) {
1188 all_matched = false;
1192 if ( all_matched ) {
1199 public static void setAllIndicatorsToZero( final Phylogeny phy ) {
1200 for( final PhylogenyNodeIterator it = phy.iteratorPostorder(); it.hasNext(); ) {
1201 it.next().setIndicator( ( byte ) 0 );
1206 * Convenience method.
1207 * Sets value for the first confidence value (created if not present, values overwritten otherwise).
1209 public static void setBootstrapConfidence( final PhylogenyNode node, final double bootstrap_confidence_value ) {
1210 setConfidence( node, bootstrap_confidence_value, "bootstrap" );
1213 public static void setBranchColorValue( final PhylogenyNode node, final Color color ) {
1214 if ( node.getBranchData().getBranchColor() == null ) {
1215 node.getBranchData().setBranchColor( new BranchColor() );
1217 node.getBranchData().getBranchColor().setValue( color );
1221 * Convenience method
1223 public static void setBranchWidthValue( final PhylogenyNode node, final double branch_width_value ) {
1224 node.getBranchData().setBranchWidth( new BranchWidth( branch_width_value ) );
1228 * Convenience method.
1229 * Sets value for the first confidence value (created if not present, values overwritten otherwise).
1231 public static void setConfidence( final PhylogenyNode node, final double confidence_value ) {
1232 setConfidence( node, confidence_value, "" );
1236 * Convenience method.
1237 * Sets value for the first confidence value (created if not present, values overwritten otherwise).
1239 public static void setConfidence( final PhylogenyNode node, final double confidence_value, final String type ) {
1240 Confidence c = null;
1241 if ( node.getBranchData().getNumberOfConfidences() > 0 ) {
1242 c = node.getBranchData().getConfidence( 0 );
1245 c = new Confidence();
1246 node.getBranchData().addConfidence( c );
1249 c.setValue( confidence_value );
1252 public static void setScientificName( final PhylogenyNode node, final String scientific_name ) {
1253 if ( !node.getNodeData().isHasTaxonomy() ) {
1254 node.getNodeData().setTaxonomy( new Taxonomy() );
1256 node.getNodeData().getTaxonomy().setScientificName( scientific_name );
1260 * Convenience method to set the taxonomy code of a phylogeny node.
1264 * @param taxonomy_code
1265 * @throws PhyloXmlDataFormatException
1267 public static void setTaxonomyCode( final PhylogenyNode node, final String taxonomy_code )
1268 throws PhyloXmlDataFormatException {
1269 if ( !node.getNodeData().isHasTaxonomy() ) {
1270 node.getNodeData().setTaxonomy( new Taxonomy() );
1272 node.getNodeData().getTaxonomy().setTaxonomyCode( taxonomy_code );
1275 final static public void sortNodeDescendents( final PhylogenyNode node, final DESCENDANT_SORT_PRIORITY pri ) {
1276 class PhylogenyNodeSortTaxonomyPriority implements Comparator<PhylogenyNode> {
1279 public int compare( final PhylogenyNode n1, final PhylogenyNode n2 ) {
1280 if ( n1.getNodeData().isHasTaxonomy() && n2.getNodeData().isHasTaxonomy() ) {
1281 if ( ( !ForesterUtil.isEmpty( n1.getNodeData().getTaxonomy().getScientificName() ) )
1282 && ( !ForesterUtil.isEmpty( n2.getNodeData().getTaxonomy().getScientificName() ) ) ) {
1283 return n1.getNodeData().getTaxonomy().getScientificName().toLowerCase()
1284 .compareTo( n2.getNodeData().getTaxonomy().getScientificName().toLowerCase() );
1286 if ( ( !ForesterUtil.isEmpty( n1.getNodeData().getTaxonomy().getTaxonomyCode() ) )
1287 && ( !ForesterUtil.isEmpty( n2.getNodeData().getTaxonomy().getTaxonomyCode() ) ) ) {
1288 return n1.getNodeData().getTaxonomy().getTaxonomyCode()
1289 .compareTo( n2.getNodeData().getTaxonomy().getTaxonomyCode() );
1291 if ( ( !ForesterUtil.isEmpty( n1.getNodeData().getTaxonomy().getCommonName() ) )
1292 && ( !ForesterUtil.isEmpty( n2.getNodeData().getTaxonomy().getCommonName() ) ) ) {
1293 return n1.getNodeData().getTaxonomy().getCommonName().toLowerCase()
1294 .compareTo( n2.getNodeData().getTaxonomy().getCommonName().toLowerCase() );
1297 if ( n1.getNodeData().isHasSequence() && n2.getNodeData().isHasSequence() ) {
1298 if ( ( !ForesterUtil.isEmpty( n1.getNodeData().getSequence().getName() ) )
1299 && ( !ForesterUtil.isEmpty( n2.getNodeData().getSequence().getName() ) ) ) {
1300 return n1.getNodeData().getSequence().getName().toLowerCase()
1301 .compareTo( n2.getNodeData().getSequence().getName().toLowerCase() );
1303 if ( ( !ForesterUtil.isEmpty( n1.getNodeData().getSequence().getSymbol() ) )
1304 && ( !ForesterUtil.isEmpty( n2.getNodeData().getSequence().getSymbol() ) ) ) {
1305 return n1.getNodeData().getSequence().getSymbol()
1306 .compareTo( n2.getNodeData().getSequence().getSymbol() );
1308 if ( ( !ForesterUtil.isEmpty( n1.getNodeData().getSequence().getGeneName() ) )
1309 && ( !ForesterUtil.isEmpty( n2.getNodeData().getSequence().getGeneName() ) ) ) {
1310 return n1.getNodeData().getSequence().getGeneName()
1311 .compareTo( n2.getNodeData().getSequence().getGeneName() );
1313 if ( ( n1.getNodeData().getSequence().getAccession() != null )
1314 && ( n2.getNodeData().getSequence().getAccession() != null )
1315 && !ForesterUtil.isEmpty( n1.getNodeData().getSequence().getAccession().getValue() )
1316 && !ForesterUtil.isEmpty( n2.getNodeData().getSequence().getAccession().getValue() ) ) {
1317 return n1.getNodeData().getSequence().getAccession().getValue()
1318 .compareTo( n2.getNodeData().getSequence().getAccession().getValue() );
1321 if ( ( !ForesterUtil.isEmpty( n1.getName() ) ) && ( !ForesterUtil.isEmpty( n2.getName() ) ) ) {
1322 return n1.getName().toLowerCase().compareTo( n2.getName().toLowerCase() );
1327 class PhylogenyNodeSortSequencePriority implements Comparator<PhylogenyNode> {
1330 public int compare( final PhylogenyNode n1, final PhylogenyNode n2 ) {
1331 if ( n1.getNodeData().isHasSequence() && n2.getNodeData().isHasSequence() ) {
1332 if ( ( !ForesterUtil.isEmpty( n1.getNodeData().getSequence().getName() ) )
1333 && ( !ForesterUtil.isEmpty( n2.getNodeData().getSequence().getName() ) ) ) {
1334 return n1.getNodeData().getSequence().getName().toLowerCase()
1335 .compareTo( n2.getNodeData().getSequence().getName().toLowerCase() );
1337 if ( ( !ForesterUtil.isEmpty( n1.getNodeData().getSequence().getSymbol() ) )
1338 && ( !ForesterUtil.isEmpty( n2.getNodeData().getSequence().getSymbol() ) ) ) {
1339 return n1.getNodeData().getSequence().getSymbol()
1340 .compareTo( n2.getNodeData().getSequence().getSymbol() );
1342 if ( ( !ForesterUtil.isEmpty( n1.getNodeData().getSequence().getGeneName() ) )
1343 && ( !ForesterUtil.isEmpty( n2.getNodeData().getSequence().getGeneName() ) ) ) {
1344 return n1.getNodeData().getSequence().getGeneName()
1345 .compareTo( n2.getNodeData().getSequence().getGeneName() );
1347 if ( ( n1.getNodeData().getSequence().getAccession() != null )
1348 && ( n2.getNodeData().getSequence().getAccession() != null )
1349 && !ForesterUtil.isEmpty( n1.getNodeData().getSequence().getAccession().getValue() )
1350 && !ForesterUtil.isEmpty( n2.getNodeData().getSequence().getAccession().getValue() ) ) {
1351 return n1.getNodeData().getSequence().getAccession().getValue()
1352 .compareTo( n2.getNodeData().getSequence().getAccession().getValue() );
1355 if ( n1.getNodeData().isHasTaxonomy() && n2.getNodeData().isHasTaxonomy() ) {
1356 if ( ( !ForesterUtil.isEmpty( n1.getNodeData().getTaxonomy().getScientificName() ) )
1357 && ( !ForesterUtil.isEmpty( n2.getNodeData().getTaxonomy().getScientificName() ) ) ) {
1358 return n1.getNodeData().getTaxonomy().getScientificName().toLowerCase()
1359 .compareTo( n2.getNodeData().getTaxonomy().getScientificName().toLowerCase() );
1361 if ( ( !ForesterUtil.isEmpty( n1.getNodeData().getTaxonomy().getTaxonomyCode() ) )
1362 && ( !ForesterUtil.isEmpty( n2.getNodeData().getTaxonomy().getTaxonomyCode() ) ) ) {
1363 return n1.getNodeData().getTaxonomy().getTaxonomyCode()
1364 .compareTo( n2.getNodeData().getTaxonomy().getTaxonomyCode() );
1366 if ( ( !ForesterUtil.isEmpty( n1.getNodeData().getTaxonomy().getCommonName() ) )
1367 && ( !ForesterUtil.isEmpty( n2.getNodeData().getTaxonomy().getCommonName() ) ) ) {
1368 return n1.getNodeData().getTaxonomy().getCommonName().toLowerCase()
1369 .compareTo( n2.getNodeData().getTaxonomy().getCommonName().toLowerCase() );
1372 if ( ( !ForesterUtil.isEmpty( n1.getName() ) ) && ( !ForesterUtil.isEmpty( n2.getName() ) ) ) {
1373 return n1.getName().toLowerCase().compareTo( n2.getName().toLowerCase() );
1378 class PhylogenyNodeSortNodeNamePriority implements Comparator<PhylogenyNode> {
1381 public int compare( final PhylogenyNode n1, final PhylogenyNode n2 ) {
1382 if ( ( !ForesterUtil.isEmpty( n1.getName() ) ) && ( !ForesterUtil.isEmpty( n2.getName() ) ) ) {
1383 return n1.getName().toLowerCase().compareTo( n2.getName().toLowerCase() );
1385 if ( n1.getNodeData().isHasTaxonomy() && n2.getNodeData().isHasTaxonomy() ) {
1386 if ( ( !ForesterUtil.isEmpty( n1.getNodeData().getTaxonomy().getScientificName() ) )
1387 && ( !ForesterUtil.isEmpty( n2.getNodeData().getTaxonomy().getScientificName() ) ) ) {
1388 return n1.getNodeData().getTaxonomy().getScientificName().toLowerCase()
1389 .compareTo( n2.getNodeData().getTaxonomy().getScientificName().toLowerCase() );
1391 if ( ( !ForesterUtil.isEmpty( n1.getNodeData().getTaxonomy().getTaxonomyCode() ) )
1392 && ( !ForesterUtil.isEmpty( n2.getNodeData().getTaxonomy().getTaxonomyCode() ) ) ) {
1393 return n1.getNodeData().getTaxonomy().getTaxonomyCode()
1394 .compareTo( n2.getNodeData().getTaxonomy().getTaxonomyCode() );
1396 if ( ( !ForesterUtil.isEmpty( n1.getNodeData().getTaxonomy().getCommonName() ) )
1397 && ( !ForesterUtil.isEmpty( n2.getNodeData().getTaxonomy().getCommonName() ) ) ) {
1398 return n1.getNodeData().getTaxonomy().getCommonName().toLowerCase()
1399 .compareTo( n2.getNodeData().getTaxonomy().getCommonName().toLowerCase() );
1402 if ( n1.getNodeData().isHasSequence() && n2.getNodeData().isHasSequence() ) {
1403 if ( ( !ForesterUtil.isEmpty( n1.getNodeData().getSequence().getName() ) )
1404 && ( !ForesterUtil.isEmpty( n2.getNodeData().getSequence().getName() ) ) ) {
1405 return n1.getNodeData().getSequence().getName().toLowerCase()
1406 .compareTo( n2.getNodeData().getSequence().getName().toLowerCase() );
1408 if ( ( !ForesterUtil.isEmpty( n1.getNodeData().getSequence().getSymbol() ) )
1409 && ( !ForesterUtil.isEmpty( n2.getNodeData().getSequence().getSymbol() ) ) ) {
1410 return n1.getNodeData().getSequence().getSymbol()
1411 .compareTo( n2.getNodeData().getSequence().getSymbol() );
1413 if ( ( !ForesterUtil.isEmpty( n1.getNodeData().getSequence().getGeneName() ) )
1414 && ( !ForesterUtil.isEmpty( n2.getNodeData().getSequence().getGeneName() ) ) ) {
1415 return n1.getNodeData().getSequence().getGeneName()
1416 .compareTo( n2.getNodeData().getSequence().getGeneName() );
1418 if ( ( n1.getNodeData().getSequence().getAccession() != null )
1419 && ( n2.getNodeData().getSequence().getAccession() != null )
1420 && !ForesterUtil.isEmpty( n1.getNodeData().getSequence().getAccession().getValue() )
1421 && !ForesterUtil.isEmpty( n2.getNodeData().getSequence().getAccession().getValue() ) ) {
1422 return n1.getNodeData().getSequence().getAccession().getValue()
1423 .compareTo( n2.getNodeData().getSequence().getAccession().getValue() );
1429 Comparator<PhylogenyNode> c;
1432 c = new PhylogenyNodeSortSequencePriority();
1435 c = new PhylogenyNodeSortNodeNamePriority();
1438 c = new PhylogenyNodeSortTaxonomyPriority();
1440 final List<PhylogenyNode> descs = node.getDescendants();
1441 Collections.sort( descs, c );
1443 for( final PhylogenyNode desc : descs ) {
1444 node.setChildNode( i++, desc );
1449 * Removes from Phylogeny to_be_stripped all external Nodes which are
1450 * associated with a species NOT found in Phylogeny reference.
1453 * a reference Phylogeny
1454 * @param to_be_stripped
1455 * Phylogeny to be stripped
1456 * @return nodes removed from to_be_stripped
1458 public static List<PhylogenyNode> taxonomyBasedDeletionOfExternalNodes( final Phylogeny reference,
1459 final Phylogeny to_be_stripped ) {
1460 final Set<String> ref_ext_taxo = new HashSet<String>();
1461 for( final PhylogenyNodeIterator it = reference.iteratorExternalForward(); it.hasNext(); ) {
1462 final PhylogenyNode n = it.next();
1463 if ( !n.getNodeData().isHasTaxonomy() ) {
1464 throw new IllegalArgumentException( "no taxonomic data in node: " + n );
1466 if ( !ForesterUtil.isEmpty( n.getNodeData().getTaxonomy().getScientificName() ) ) {
1467 ref_ext_taxo.add( n.getNodeData().getTaxonomy().getScientificName() );
1469 if ( !ForesterUtil.isEmpty( n.getNodeData().getTaxonomy().getTaxonomyCode() ) ) {
1470 ref_ext_taxo.add( n.getNodeData().getTaxonomy().getTaxonomyCode() );
1472 if ( ( n.getNodeData().getTaxonomy().getIdentifier() != null )
1473 && !ForesterUtil.isEmpty( n.getNodeData().getTaxonomy().getIdentifier().getValue() ) ) {
1474 ref_ext_taxo.add( n.getNodeData().getTaxonomy().getIdentifier().getValuePlusProvider() );
1477 final ArrayList<PhylogenyNode> nodes_to_delete = new ArrayList<PhylogenyNode>();
1478 for( final PhylogenyNodeIterator it = to_be_stripped.iteratorExternalForward(); it.hasNext(); ) {
1479 final PhylogenyNode n = it.next();
1480 if ( !n.getNodeData().isHasTaxonomy() ) {
1481 nodes_to_delete.add( n );
1483 else if ( !( ref_ext_taxo.contains( n.getNodeData().getTaxonomy().getScientificName() ) )
1484 && !( ref_ext_taxo.contains( n.getNodeData().getTaxonomy().getTaxonomyCode() ) )
1485 && !( ( n.getNodeData().getTaxonomy().getIdentifier() != null ) && ref_ext_taxo.contains( n
1486 .getNodeData().getTaxonomy().getIdentifier().getValuePlusProvider() ) ) ) {
1487 nodes_to_delete.add( n );
1490 for( final PhylogenyNode n : nodes_to_delete ) {
1491 to_be_stripped.deleteSubtree( n, true );
1493 to_be_stripped.clearHashIdToNodeMap();
1494 to_be_stripped.externalNodesHaveChanged();
1495 return nodes_to_delete;
1498 final static public void transferInternalNamesToBootstrapSupport( final Phylogeny phy ) {
1499 final PhylogenyNodeIterator it = phy.iteratorPostorder();
1500 while ( it.hasNext() ) {
1501 final PhylogenyNode n = it.next();
1502 if ( !n.isExternal() && !ForesterUtil.isEmpty( n.getName() ) ) {
1505 value = Double.parseDouble( n.getName() );
1507 catch ( final NumberFormatException e ) {
1508 throw new IllegalArgumentException( "failed to parse number from [" + n.getName() + "]: "
1509 + e.getLocalizedMessage() );
1511 if ( value >= 0.0 ) {
1512 n.getBranchData().addConfidence( new Confidence( value, "bootstrap" ) );
1519 final static public void transferInternalNodeNamesToConfidence( final Phylogeny phy ) {
1520 final PhylogenyNodeIterator it = phy.iteratorPostorder();
1521 while ( it.hasNext() ) {
1522 final PhylogenyNode n = it.next();
1523 if ( !n.isExternal() && !n.getBranchData().isHasConfidences() ) {
1524 if ( !ForesterUtil.isEmpty( n.getName() ) ) {
1527 d = Double.parseDouble( n.getName() );
1529 catch ( final Exception e ) {
1533 n.getBranchData().addConfidence( new Confidence( d, "" ) );
1541 final static public void transferNodeNameToField( final Phylogeny phy,
1542 final PhylogenyNodeField field,
1543 final boolean external_only ) throws PhyloXmlDataFormatException {
1544 final PhylogenyNodeIterator it = phy.iteratorPostorder();
1545 while ( it.hasNext() ) {
1546 final PhylogenyNode n = it.next();
1547 if ( external_only && n.isInternal() ) {
1550 final String name = n.getName().trim();
1551 if ( !ForesterUtil.isEmpty( name ) ) {
1555 setTaxonomyCode( n, name );
1557 case TAXONOMY_SCIENTIFIC_NAME:
1559 if ( !n.getNodeData().isHasTaxonomy() ) {
1560 n.getNodeData().setTaxonomy( new Taxonomy() );
1562 n.getNodeData().getTaxonomy().setScientificName( name );
1564 case TAXONOMY_COMMON_NAME:
1566 if ( !n.getNodeData().isHasTaxonomy() ) {
1567 n.getNodeData().setTaxonomy( new Taxonomy() );
1569 n.getNodeData().getTaxonomy().setCommonName( name );
1571 case SEQUENCE_SYMBOL:
1573 if ( !n.getNodeData().isHasSequence() ) {
1574 n.getNodeData().setSequence( new Sequence() );
1576 n.getNodeData().getSequence().setSymbol( name );
1580 if ( !n.getNodeData().isHasSequence() ) {
1581 n.getNodeData().setSequence( new Sequence() );
1583 n.getNodeData().getSequence().setName( name );
1585 case TAXONOMY_ID_UNIPROT_1: {
1586 if ( !n.getNodeData().isHasTaxonomy() ) {
1587 n.getNodeData().setTaxonomy( new Taxonomy() );
1590 final int i = name.indexOf( '_' );
1592 id = name.substring( 0, i );
1597 n.getNodeData().getTaxonomy()
1598 .setIdentifier( new Identifier( id, PhyloXmlUtil.UNIPROT_TAX_PROVIDER ) );
1601 case TAXONOMY_ID_UNIPROT_2: {
1602 if ( !n.getNodeData().isHasTaxonomy() ) {
1603 n.getNodeData().setTaxonomy( new Taxonomy() );
1606 final int i = name.indexOf( '_' );
1608 id = name.substring( i + 1, name.length() );
1613 n.getNodeData().getTaxonomy()
1614 .setIdentifier( new Identifier( id, PhyloXmlUtil.UNIPROT_TAX_PROVIDER ) );
1618 if ( !n.getNodeData().isHasTaxonomy() ) {
1619 n.getNodeData().setTaxonomy( new Taxonomy() );
1621 n.getNodeData().getTaxonomy().setIdentifier( new Identifier( name ) );
1629 static double addPhylogenyDistances( final double a, final double b ) {
1630 if ( ( a >= 0.0 ) && ( b >= 0.0 ) ) {
1633 else if ( a >= 0.0 ) {
1636 else if ( b >= 0.0 ) {
1639 return PhylogenyDataUtil.BRANCH_LENGTH_DEFAULT;
1642 static double calculateDistanceToAncestor( final PhylogenyNode anc, PhylogenyNode desc ) {
1644 boolean all_default = true;
1645 while ( anc != desc ) {
1646 if ( desc.getDistanceToParent() != PhylogenyDataUtil.BRANCH_LENGTH_DEFAULT ) {
1647 d += desc.getDistanceToParent();
1648 if ( all_default ) {
1649 all_default = false;
1652 desc = desc.getParent();
1654 if ( all_default ) {
1655 return PhylogenyDataUtil.BRANCH_LENGTH_DEFAULT;
1661 * Deep copies the phylogeny originating from this node.
1663 static PhylogenyNode copySubTree( final PhylogenyNode source ) {
1664 if ( source == null ) {
1668 final PhylogenyNode newnode = source.copyNodeData();
1669 if ( !source.isExternal() ) {
1670 for( int i = 0; i < source.getNumberOfDescendants(); ++i ) {
1671 newnode.setChildNode( i, PhylogenyMethods.copySubTree( source.getChildNode( i ) ) );
1679 * Shallow copies the phylogeny originating from this node.
1681 static PhylogenyNode copySubTreeShallow( final PhylogenyNode source ) {
1682 if ( source == null ) {
1686 final PhylogenyNode newnode = source.copyNodeDataShallow();
1687 if ( !source.isExternal() ) {
1688 for( int i = 0; i < source.getNumberOfDescendants(); ++i ) {
1689 newnode.setChildNode( i, PhylogenyMethods.copySubTreeShallow( source.getChildNode( i ) ) );
1696 private final static List<PhylogenyNode> divideIntoSubTreesHelper( final PhylogenyNode node,
1697 final double min_distance_to_root ) {
1698 final List<PhylogenyNode> l = new ArrayList<PhylogenyNode>();
1699 final PhylogenyNode r = moveTowardsRoot( node, min_distance_to_root );
1700 for( final PhylogenyNode ext : r.getAllExternalDescendants() ) {
1701 if ( ext.getIndicator() != 0 ) {
1702 throw new RuntimeException( "this should not have happened" );
1704 ext.setIndicator( ( byte ) 1 );
1711 * Calculates the distance between PhylogenyNodes n1 and n2.
1712 * PRECONDITION: n1 is a descendant of n2.
1715 * a descendant of n2
1717 * @return distance between n1 and n2
1719 private static double getDistance( PhylogenyNode n1, final PhylogenyNode n2 ) {
1721 while ( n1 != n2 ) {
1722 if ( n1.getDistanceToParent() > 0.0 ) {
1723 d += n1.getDistanceToParent();
1725 n1 = n1.getParent();
1730 private static boolean match( final String s,
1732 final boolean case_sensitive,
1733 final boolean partial ) {
1734 if ( ForesterUtil.isEmpty( s ) || ForesterUtil.isEmpty( query ) ) {
1737 String my_s = s.trim();
1738 String my_query = query.trim();
1739 if ( !case_sensitive ) {
1740 my_s = my_s.toLowerCase();
1741 my_query = my_query.toLowerCase();
1744 return my_s.indexOf( my_query ) >= 0;
1747 return Pattern.compile( "(\\b|_)" + Pattern.quote( my_query ) + "(\\b|_)" ).matcher( my_s ).find();
1751 private final static PhylogenyNode moveTowardsRoot( final PhylogenyNode node, final double min_distance_to_root ) {
1752 PhylogenyNode n = node;
1753 PhylogenyNode prev = node;
1754 while ( min_distance_to_root < n.calculateDistanceToRoot() ) {
1761 public static enum DESCENDANT_SORT_PRIORITY {
1762 NODE_NAME, SEQUENCE, TAXONOMY;
1765 public static enum PhylogenyNodeField {
1770 TAXONOMY_COMMON_NAME,
1772 TAXONOMY_ID_UNIPROT_1,
1773 TAXONOMY_ID_UNIPROT_2,
1774 TAXONOMY_SCIENTIFIC_NAME;