2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
5 // Copyright (C) 2008-2009 Christian M. Zmasek
6 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 // Contact: phylosoft @ gmail . com
24 // WWW: www.phylosoft.org/forester
26 package org.forester.phylogeny;
28 import java.awt.Color;
30 import java.io.IOException;
31 import java.util.ArrayList;
32 import java.util.Arrays;
33 import java.util.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;
40 import java.util.SortedMap;
41 import java.util.TreeMap;
43 import org.forester.io.parsers.PhylogenyParser;
44 import org.forester.io.parsers.phyloxml.PhyloXmlDataFormatException;
45 import org.forester.io.parsers.phyloxml.PhyloXmlUtil;
46 import org.forester.io.parsers.util.PhylogenyParserException;
47 import org.forester.phylogeny.data.BranchColor;
48 import org.forester.phylogeny.data.BranchWidth;
49 import org.forester.phylogeny.data.Confidence;
50 import org.forester.phylogeny.data.DomainArchitecture;
51 import org.forester.phylogeny.data.Event;
52 import org.forester.phylogeny.data.Identifier;
53 import org.forester.phylogeny.data.PhylogenyDataUtil;
54 import org.forester.phylogeny.data.Sequence;
55 import org.forester.phylogeny.data.Taxonomy;
56 import org.forester.phylogeny.factories.ParserBasedPhylogenyFactory;
57 import org.forester.phylogeny.factories.PhylogenyFactory;
58 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
59 import org.forester.util.BasicDescriptiveStatistics;
60 import org.forester.util.DescriptiveStatistics;
61 import org.forester.util.FailedConditionCheckException;
62 import org.forester.util.ForesterUtil;
64 public class PhylogenyMethods {
66 private static PhylogenyMethods _instance = null;
67 private PhylogenyNode _farthest_1 = null;
68 private PhylogenyNode _farthest_2 = null;
70 private PhylogenyMethods() {
71 // Hidden constructor.
75 * Calculates the distance between PhylogenyNodes node1 and node2.
80 * @return distance between node1 and node2
82 public double calculateDistance( final PhylogenyNode node1, final PhylogenyNode node2 ) {
83 final PhylogenyNode lca = calculateLCA( node1, node2 );
84 final PhylogenyNode n1 = node1;
85 final PhylogenyNode n2 = node2;
86 return ( PhylogenyMethods.getDistance( n1, lca ) + PhylogenyMethods.getDistance( n2, lca ) );
89 public double calculateFurthestDistance( final Phylogeny phylogeny ) {
90 if ( phylogeny.getNumberOfExternalNodes() < 2 ) {
95 PhylogenyNode node_1 = null;
96 PhylogenyNode node_2 = null;
97 double farthest_d = -Double.MAX_VALUE;
98 final PhylogenyMethods methods = PhylogenyMethods.getInstance();
99 final List<PhylogenyNode> ext_nodes = phylogeny.getRoot().getAllExternalDescendants();
100 for( int i = 1; i < ext_nodes.size(); ++i ) {
101 for( int j = 0; j < i; ++j ) {
102 final double d = methods.calculateDistance( ext_nodes.get( i ), ext_nodes.get( j ) );
104 throw new RuntimeException( "distance cannot be negative" );
106 if ( d > farthest_d ) {
108 node_1 = ext_nodes.get( i );
109 node_2 = ext_nodes.get( j );
113 _farthest_1 = node_1;
114 _farthest_2 = node_2;
119 public Object clone() throws CloneNotSupportedException {
120 throw new CloneNotSupportedException();
123 public PhylogenyNode getFarthestNode1() {
127 public PhylogenyNode getFarthestNode2() {
131 public static DescriptiveStatistics calculatBranchLengthStatistics( final Phylogeny phy ) {
132 final DescriptiveStatistics stats = new BasicDescriptiveStatistics();
133 for( final PhylogenyNodeIterator iter = phy.iteratorPreorder(); iter.hasNext(); ) {
134 final PhylogenyNode n = iter.next();
135 if ( !n.isRoot() && ( n.getDistanceToParent() >= 0.0 ) ) {
136 stats.addValue( n.getDistanceToParent() );
142 public static List<DescriptiveStatistics> calculatConfidenceStatistics( final Phylogeny phy ) {
143 final List<DescriptiveStatistics> stats = new ArrayList<DescriptiveStatistics>();
144 for( final PhylogenyNodeIterator iter = phy.iteratorPreorder(); iter.hasNext(); ) {
145 final PhylogenyNode n = iter.next();
146 if ( !n.isExternal() && !n.isRoot() ) {
147 if ( n.getBranchData().isHasConfidences() ) {
148 for( int i = 0; i < n.getBranchData().getConfidences().size(); ++i ) {
149 final Confidence c = n.getBranchData().getConfidences().get( i );
150 if ( ( i > ( stats.size() - 1 ) ) || ( stats.get( i ) == null ) ) {
151 stats.add( i, new BasicDescriptiveStatistics() );
153 if ( !ForesterUtil.isEmpty( c.getType() ) ) {
154 if ( !ForesterUtil.isEmpty( stats.get( i ).getDescription() ) ) {
155 if ( !stats.get( i ).getDescription().equalsIgnoreCase( c.getType() ) ) {
156 throw new IllegalArgumentException( "support values in node [" + n.toString()
157 + "] appear inconsistently ordered" );
160 stats.get( i ).setDescription( c.getType() );
162 stats.get( i ).addValue( ( ( c != null ) && ( c.getValue() >= 0 ) ) ? c.getValue() : 0 );
171 * Returns the LCA of PhylogenyNodes node1 and node2.
176 * @return LCA of node1 and node2
178 public final static PhylogenyNode calculateLCA( PhylogenyNode node1, PhylogenyNode node2 ) {
179 if ( node1 == null ) {
180 throw new IllegalArgumentException( "first argument (node) is null" );
182 if ( node2 == null ) {
183 throw new IllegalArgumentException( "second argument (node) is null" );
185 if ( node1 == node2 ) {
188 if ( ( node1.getParent() == node2.getParent() ) ) {
189 return node1.getParent();
191 int depth1 = node1.calculateDepth();
192 int depth2 = node2.calculateDepth();
193 while ( ( depth1 > -1 ) && ( depth2 > -1 ) ) {
194 if ( depth1 > depth2 ) {
195 node1 = node1.getParent();
198 else if ( depth2 > depth1 ) {
199 node2 = node2.getParent();
203 if ( node1 == node2 ) {
206 node1 = node1.getParent();
207 node2 = node2.getParent();
212 throw new IllegalArgumentException( "illegal attempt to calculate LCA of two nodes which do not share a common root" );
216 * Returns the LCA of PhylogenyNodes node1 and node2.
217 * Precondition: ids are in pre-order (or level-order).
222 * @return LCA of node1 and node2
224 public final static PhylogenyNode calculateLCAonTreeWithIdsInPreOrder( PhylogenyNode node1, PhylogenyNode node2 ) {
225 if ( node1 == null ) {
226 throw new IllegalArgumentException( "first argument (node) is null" );
228 if ( node2 == null ) {
229 throw new IllegalArgumentException( "second argument (node) is null" );
231 while ( node1 != node2 ) {
232 if ( node1.getId() > node2.getId() ) {
233 node1 = node1.getParent();
236 node2 = node2.getParent();
242 public static short calculateMaxBranchesToLeaf( final PhylogenyNode node ) {
243 if ( node.isExternal() ) {
247 for( PhylogenyNode d : node.getAllExternalDescendants() ) {
249 while ( d != node ) {
250 if ( d.isCollapse() ) {
265 public static int calculateMaxDepth( final Phylogeny phy ) {
267 for( final PhylogenyNodeIterator iter = phy.iteratorExternalForward(); iter.hasNext(); ) {
268 final PhylogenyNode node = iter.next();
269 final int steps = node.calculateDepth();
277 public static double calculateMaxDistanceToRoot( final Phylogeny phy ) {
279 for( final PhylogenyNodeIterator iter = phy.iteratorExternalForward(); iter.hasNext(); ) {
280 final PhylogenyNode node = iter.next();
281 final double d = node.calculateDistanceToRoot();
289 public static int calculateNumberOfExternalNodesWithoutTaxonomy( final PhylogenyNode node ) {
290 final List<PhylogenyNode> descs = node.getAllExternalDescendants();
292 for( final PhylogenyNode n : descs ) {
293 if ( !n.getNodeData().isHasTaxonomy() || n.getNodeData().getTaxonomy().isEmpty() ) {
300 public static DescriptiveStatistics calculatNumberOfDescendantsPerNodeStatistics( final Phylogeny phy ) {
301 final DescriptiveStatistics stats = new BasicDescriptiveStatistics();
302 for( final PhylogenyNodeIterator iter = phy.iteratorPreorder(); iter.hasNext(); ) {
303 final PhylogenyNode n = iter.next();
304 if ( !n.isExternal() ) {
305 stats.addValue( n.getNumberOfDescendants() );
311 public static int countNumberOfOneDescendantNodes( final Phylogeny phy ) {
313 for( final PhylogenyNodeIterator iter = phy.iteratorPreorder(); iter.hasNext(); ) {
314 final PhylogenyNode n = iter.next();
315 if ( !n.isExternal() && ( n.getNumberOfDescendants() == 1 ) ) {
322 public static int countNumberOfPolytomies( final Phylogeny phy ) {
324 for( final PhylogenyNodeIterator iter = phy.iteratorPreorder(); iter.hasNext(); ) {
325 final PhylogenyNode n = iter.next();
326 if ( !n.isExternal() && ( n.getNumberOfDescendants() > 2 ) ) {
333 public static final HashMap<String, PhylogenyNode> createNameToExtNodeMap( final Phylogeny phy ) {
334 final HashMap<String, PhylogenyNode> nodes = new HashMap<String, PhylogenyNode>();
335 for( final PhylogenyNodeIterator iter = phy.iteratorExternalForward(); iter.hasNext(); ) {
336 final PhylogenyNode n = iter.next();
337 nodes.put( n.getName(), n );
342 public static void deleteExternalNodesNegativeSelection( final Set<Integer> to_delete, final Phylogeny phy ) {
343 phy.clearHashIdToNodeMap();
344 for( final Integer id : to_delete ) {
345 phy.deleteSubtree( phy.getNode( id ), true );
347 phy.clearHashIdToNodeMap();
348 phy.externalNodesHaveChanged();
351 public static void deleteExternalNodesNegativeSelection( final String[] node_names_to_delete, final Phylogeny p )
352 throws IllegalArgumentException {
353 for( final String element : node_names_to_delete ) {
354 if ( ForesterUtil.isEmpty( element ) ) {
357 List<PhylogenyNode> nodes = null;
358 nodes = p.getNodes( element );
359 final Iterator<PhylogenyNode> it = nodes.iterator();
360 while ( it.hasNext() ) {
361 final PhylogenyNode n = it.next();
362 if ( !n.isExternal() ) {
363 throw new IllegalArgumentException( "attempt to delete non-external node \"" + element + "\"" );
365 p.deleteSubtree( n, true );
368 p.clearHashIdToNodeMap();
369 p.externalNodesHaveChanged();
372 public static void deleteExternalNodesPositiveSelection( final Set<Taxonomy> species_to_keep, final Phylogeny phy ) {
373 // final Set<Integer> to_delete = new HashSet<Integer>();
374 for( final PhylogenyNodeIterator it = phy.iteratorExternalForward(); it.hasNext(); ) {
375 final PhylogenyNode n = it.next();
376 if ( n.getNodeData().isHasTaxonomy() ) {
377 if ( !species_to_keep.contains( n.getNodeData().getTaxonomy() ) ) {
378 //to_delete.add( n.getNodeId() );
379 phy.deleteSubtree( n, true );
383 throw new IllegalArgumentException( "node " + n.getId() + " has no taxonomic data" );
386 phy.clearHashIdToNodeMap();
387 phy.externalNodesHaveChanged();
390 public static List<String> deleteExternalNodesPositiveSelection( final String[] node_names_to_keep,
391 final Phylogeny p ) {
392 final PhylogenyNodeIterator it = p.iteratorExternalForward();
393 final String[] to_delete = new String[ p.getNumberOfExternalNodes() ];
395 Arrays.sort( node_names_to_keep );
396 while ( it.hasNext() ) {
397 final String curent_name = it.next().getName();
398 if ( Arrays.binarySearch( node_names_to_keep, curent_name ) < 0 ) {
399 to_delete[ i++ ] = curent_name;
402 PhylogenyMethods.deleteExternalNodesNegativeSelection( to_delete, p );
403 final List<String> deleted = new ArrayList<String>();
404 for( final String n : to_delete ) {
405 if ( !ForesterUtil.isEmpty( n ) ) {
412 final public static void deleteInternalNodesWithOnlyOneDescendent( final Phylogeny phy ) {
413 final ArrayList<PhylogenyNode> to_delete = new ArrayList<PhylogenyNode>();
414 for( final PhylogenyNodeIterator iter = phy.iteratorPostorder(); iter.hasNext(); ) {
415 final PhylogenyNode n = iter.next();
416 if ( !n.isExternal() && ( n.getNumberOfDescendants() == 1 ) ) {
420 for( final PhylogenyNode d : to_delete ) {
421 PhylogenyMethods.removeNode( d, phy );
423 phy.clearHashIdToNodeMap();
424 phy.externalNodesHaveChanged();
427 final public static void deleteNonOrthologousExternalNodes( final Phylogeny phy, final PhylogenyNode n ) {
428 if ( n.isInternal() ) {
429 throw new IllegalArgumentException( "node is not external" );
431 final ArrayList<PhylogenyNode> to_delete = new ArrayList<PhylogenyNode>();
432 for( final PhylogenyNodeIterator it = phy.iteratorExternalForward(); it.hasNext(); ) {
433 final PhylogenyNode i = it.next();
434 if ( !PhylogenyMethods.getEventAtLCA( n, i ).isSpeciation() ) {
438 for( final PhylogenyNode d : to_delete ) {
439 phy.deleteSubtree( d, true );
441 phy.clearHashIdToNodeMap();
442 phy.externalNodesHaveChanged();
445 public static List<PhylogenyNode> getAllDescendants( final PhylogenyNode node ) {
446 final List<PhylogenyNode> descs = new ArrayList<PhylogenyNode>();
447 final Set<Integer> encountered = new HashSet<Integer>();
448 if ( !node.isExternal() ) {
449 final List<PhylogenyNode> exts = node.getAllExternalDescendants();
450 for( PhylogenyNode current : exts ) {
451 descs.add( current );
452 while ( current != node ) {
453 current = current.getParent();
454 if ( encountered.contains( current.getId() ) ) {
457 descs.add( current );
458 encountered.add( current.getId() );
472 public static Color getBranchColorValue( final PhylogenyNode node ) {
473 if ( node.getBranchData().getBranchColor() == null ) {
476 return node.getBranchData().getBranchColor().getValue();
482 public static double getBranchWidthValue( final PhylogenyNode node ) {
483 if ( !node.getBranchData().isHasBranchWidth() ) {
484 return BranchWidth.BRANCH_WIDTH_DEFAULT_VALUE;
486 return node.getBranchData().getBranchWidth().getValue();
492 public static double getConfidenceValue( final PhylogenyNode node ) {
493 if ( !node.getBranchData().isHasConfidences() ) {
494 return Confidence.CONFIDENCE_DEFAULT_VALUE;
496 return node.getBranchData().getConfidence( 0 ).getValue();
502 public static double[] getConfidenceValuesAsArray( final PhylogenyNode node ) {
503 if ( !node.getBranchData().isHasConfidences() ) {
504 return new double[ 0 ];
506 final double[] values = new double[ node.getBranchData().getConfidences().size() ];
508 for( final Confidence c : node.getBranchData().getConfidences() ) {
509 values[ i++ ] = c.getValue();
514 final public static Event getEventAtLCA( final PhylogenyNode n1, final PhylogenyNode n2 ) {
515 return calculateLCA( n1, n2 ).getNodeData().getEvent();
519 * Returns taxonomy t if all external descendants have
520 * the same taxonomy t, null otherwise.
523 public static Taxonomy getExternalDescendantsTaxonomy( final PhylogenyNode node ) {
524 final List<PhylogenyNode> descs = node.getAllExternalDescendants();
526 for( final PhylogenyNode n : descs ) {
527 if ( !n.getNodeData().isHasTaxonomy() || n.getNodeData().getTaxonomy().isEmpty() ) {
530 else if ( tax == null ) {
531 tax = n.getNodeData().getTaxonomy();
533 else if ( n.getNodeData().getTaxonomy().isEmpty() || !tax.isEqual( n.getNodeData().getTaxonomy() ) ) {
540 public static PhylogenyNode getFurthestDescendant( final PhylogenyNode node ) {
541 final List<PhylogenyNode> children = node.getAllExternalDescendants();
542 PhylogenyNode farthest = null;
543 double longest = -Double.MAX_VALUE;
544 for( final PhylogenyNode child : children ) {
545 if ( PhylogenyMethods.getDistance( child, node ) > longest ) {
547 longest = PhylogenyMethods.getDistance( child, node );
553 public static PhylogenyMethods getInstance() {
554 if ( PhylogenyMethods._instance == null ) {
555 PhylogenyMethods._instance = new PhylogenyMethods();
557 return PhylogenyMethods._instance;
561 * Returns the largest confidence value found on phy.
563 static public double getMaximumConfidenceValue( final Phylogeny phy ) {
564 double max = -Double.MAX_VALUE;
565 for( final PhylogenyNodeIterator iter = phy.iteratorPreorder(); iter.hasNext(); ) {
566 final double s = PhylogenyMethods.getConfidenceValue( iter.next() );
567 if ( ( s != Confidence.CONFIDENCE_DEFAULT_VALUE ) && ( s > max ) ) {
574 static public int getMinimumDescendentsPerInternalNodes( final Phylogeny phy ) {
575 int min = Integer.MAX_VALUE;
578 for( final PhylogenyNodeIterator it = phy.iteratorPreorder(); it.hasNext(); ) {
580 if ( n.isInternal() ) {
581 d = n.getNumberOfDescendants();
591 * Convenience method for display purposes.
592 * Not intended for algorithms.
594 public static String getSpecies( final PhylogenyNode node ) {
595 if ( !node.getNodeData().isHasTaxonomy() ) {
598 else if ( !ForesterUtil.isEmpty( node.getNodeData().getTaxonomy().getScientificName() ) ) {
599 return node.getNodeData().getTaxonomy().getScientificName();
601 if ( !ForesterUtil.isEmpty( node.getNodeData().getTaxonomy().getTaxonomyCode() ) ) {
602 return node.getNodeData().getTaxonomy().getTaxonomyCode();
605 return node.getNodeData().getTaxonomy().getCommonName();
610 * Convenience method for display purposes.
611 * Not intended for algorithms.
613 public static String getTaxonomyIdentifier( final PhylogenyNode node ) {
614 if ( !node.getNodeData().isHasTaxonomy() || ( node.getNodeData().getTaxonomy().getIdentifier() == null ) ) {
617 return node.getNodeData().getTaxonomy().getIdentifier().getValue();
620 public final static boolean isAllDecendentsAreDuplications( final PhylogenyNode n ) {
621 if ( n.isExternal() ) {
625 if ( n.isDuplication() ) {
626 for( final PhylogenyNode desc : n.getDescendants() ) {
627 if ( !isAllDecendentsAreDuplications( desc ) ) {
639 public static boolean isHasExternalDescendant( final PhylogenyNode node ) {
640 for( int i = 0; i < node.getNumberOfDescendants(); ++i ) {
641 if ( node.getChildNode( i ).isExternal() ) {
649 * This is case insensitive.
652 public synchronized static boolean isTaxonomyHasIdentifierOfGivenProvider( final Taxonomy tax,
653 final String[] providers ) {
654 if ( ( tax.getIdentifier() != null ) && !ForesterUtil.isEmpty( tax.getIdentifier().getProvider() ) ) {
655 final String my_tax_prov = tax.getIdentifier().getProvider();
656 for( final String provider : providers ) {
657 if ( provider.equalsIgnoreCase( my_tax_prov ) ) {
668 public static void midpointRoot( final Phylogeny phylogeny ) {
669 if ( phylogeny.getNumberOfExternalNodes() < 2 ) {
672 final PhylogenyMethods methods = getInstance();
673 final double farthest_d = methods.calculateFurthestDistance( phylogeny );
674 final PhylogenyNode f1 = methods.getFarthestNode1();
675 final PhylogenyNode f2 = methods.getFarthestNode2();
676 if ( farthest_d <= 0.0 ) {
679 double x = farthest_d / 2.0;
680 PhylogenyNode n = f1;
681 if ( PhylogenyMethods.getDistance( f1, phylogeny.getRoot() ) < PhylogenyMethods.getDistance( f2, phylogeny
685 while ( ( x > n.getDistanceToParent() ) && !n.isRoot() ) {
686 x -= ( n.getDistanceToParent() > 0 ? n.getDistanceToParent() : 0 );
689 phylogeny.reRoot( n, x );
690 phylogeny.recalculateNumberOfExternalDescendants( true );
691 final PhylogenyNode a = getFurthestDescendant( phylogeny.getRoot().getChildNode1() );
692 final PhylogenyNode b = getFurthestDescendant( phylogeny.getRoot().getChildNode2() );
693 final double da = getDistance( a, phylogeny.getRoot() );
694 final double db = getDistance( b, phylogeny.getRoot() );
695 if ( Math.abs( da - db ) > 0.000001 ) {
696 throw new FailedConditionCheckException( "this should not have happened: midpoint rooting failed: da="
697 + da + ", db=" + db + ", diff=" + Math.abs( da - db ) );
701 public static void normalizeBootstrapValues( final Phylogeny phylogeny,
702 final double max_bootstrap_value,
703 final double max_normalized_value ) {
704 for( final PhylogenyNodeIterator iter = phylogeny.iteratorPreorder(); iter.hasNext(); ) {
705 final PhylogenyNode node = iter.next();
706 if ( node.isInternal() ) {
707 final double confidence = getConfidenceValue( node );
708 if ( confidence != Confidence.CONFIDENCE_DEFAULT_VALUE ) {
709 if ( confidence >= max_bootstrap_value ) {
710 setBootstrapConfidence( node, max_normalized_value );
713 setBootstrapConfidence( node, ( confidence * max_normalized_value ) / max_bootstrap_value );
720 public static List<PhylogenyNode> obtainAllNodesAsList( final Phylogeny phy ) {
721 final List<PhylogenyNode> nodes = new ArrayList<PhylogenyNode>();
722 if ( phy.isEmpty() ) {
725 for( final PhylogenyNodeIterator iter = phy.iteratorPreorder(); iter.hasNext(); ) {
726 nodes.add( iter.next() );
732 * Returns the set of distinct taxonomies of
733 * all external nodes of node.
734 * If at least one the external nodes has no taxonomy,
738 public static Set<Taxonomy> obtainDistinctTaxonomies( final PhylogenyNode node ) {
739 final List<PhylogenyNode> descs = node.getAllExternalDescendants();
740 final Set<Taxonomy> tax_set = new HashSet<Taxonomy>();
741 for( final PhylogenyNode n : descs ) {
742 if ( !n.getNodeData().isHasTaxonomy() || n.getNodeData().getTaxonomy().isEmpty() ) {
745 tax_set.add( n.getNodeData().getTaxonomy() );
751 * Returns a map of distinct taxonomies of
752 * all external nodes of node.
753 * If at least one of the external nodes has no taxonomy,
757 public static SortedMap<Taxonomy, Integer> obtainDistinctTaxonomyCounts( final PhylogenyNode node ) {
758 final List<PhylogenyNode> descs = node.getAllExternalDescendants();
759 final SortedMap<Taxonomy, Integer> tax_map = new TreeMap<Taxonomy, Integer>();
760 for( final PhylogenyNode n : descs ) {
761 if ( !n.getNodeData().isHasTaxonomy() || n.getNodeData().getTaxonomy().isEmpty() ) {
764 final Taxonomy t = n.getNodeData().getTaxonomy();
765 if ( tax_map.containsKey( t ) ) {
766 tax_map.put( t, tax_map.get( t ) + 1 );
776 * Arranges the order of childern for each node of this Phylogeny in such a
777 * way that either the branch with more children is on top (right) or on
778 * bottom (left), dependent on the value of boolean order.
781 * decides in which direction to order
784 public static void orderAppearance( final PhylogenyNode n,
786 final boolean order_ext_alphabetically,
787 final DESCENDANT_SORT_PRIORITY pri ) {
788 if ( n.isExternal() ) {
792 PhylogenyNode temp = null;
793 if ( ( n.getNumberOfDescendants() == 2 )
794 && ( n.getChildNode1().getNumberOfExternalNodes() != n.getChildNode2().getNumberOfExternalNodes() )
795 && ( ( n.getChildNode1().getNumberOfExternalNodes() < n.getChildNode2().getNumberOfExternalNodes() ) == order ) ) {
796 temp = n.getChildNode1();
797 n.setChild1( n.getChildNode2() );
800 else if ( order_ext_alphabetically ) {
801 boolean all_ext = true;
802 for( final PhylogenyNode i : n.getDescendants() ) {
803 if ( !i.isExternal() ) {
809 PhylogenyMethods.sortNodeDescendents( n, pri );
812 for( int i = 0; i < n.getNumberOfDescendants(); ++i ) {
813 orderAppearance( n.getChildNode( i ), order, order_ext_alphabetically, pri );
818 public static void postorderBranchColorAveragingExternalNodeBased( final Phylogeny p ) {
819 for( final PhylogenyNodeIterator iter = p.iteratorPostorder(); iter.hasNext(); ) {
820 final PhylogenyNode node = iter.next();
825 if ( node.isInternal() ) {
826 //for( final PhylogenyNodeIterator iterator = node.iterateChildNodesForward(); iterator.hasNext(); ) {
827 for( int i = 0; i < node.getNumberOfDescendants(); ++i ) {
828 final PhylogenyNode child_node = node.getChildNode( i );
829 final Color child_color = getBranchColorValue( child_node );
830 if ( child_color != null ) {
832 red += child_color.getRed();
833 green += child_color.getGreen();
834 blue += child_color.getBlue();
837 setBranchColorValue( node,
838 new Color( ForesterUtil.roundToInt( red / n ),
839 ForesterUtil.roundToInt( green / n ),
840 ForesterUtil.roundToInt( blue / n ) ) );
845 public static final void preOrderReId( final Phylogeny phy ) {
846 if ( phy.isEmpty() ) {
849 phy.setIdToNodeMap( null );
850 int i = PhylogenyNode.getNodeCount();
851 for( final PhylogenyNodeIterator it = phy.iteratorPreorder(); it.hasNext(); ) {
852 it.next().setId( i++ );
854 PhylogenyNode.setNodeCount( i );
857 public final static Phylogeny[] readPhylogenies( final PhylogenyParser parser, final File file ) throws IOException {
858 final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
859 final Phylogeny[] trees = factory.create( file, parser );
860 if ( ( trees == null ) || ( trees.length == 0 ) ) {
861 throw new PhylogenyParserException( "Unable to parse phylogeny from file: " + file );
866 public final static Phylogeny[] readPhylogenies( final PhylogenyParser parser, final List<File> files )
868 final List<Phylogeny> tree_list = new ArrayList<Phylogeny>();
869 for( final File file : files ) {
870 final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
871 final Phylogeny[] trees = factory.create( file, parser );
872 if ( ( trees == null ) || ( trees.length == 0 ) ) {
873 throw new PhylogenyParserException( "Unable to parse phylogeny from file: " + file );
875 tree_list.addAll( Arrays.asList( trees ) );
877 return tree_list.toArray( new Phylogeny[ tree_list.size() ] );
880 public static void removeNode( final PhylogenyNode remove_me, final Phylogeny phylogeny ) {
881 if ( remove_me.isRoot() ) {
882 throw new IllegalArgumentException( "ill advised attempt to remove root node" );
884 if ( remove_me.isExternal() ) {
885 phylogeny.deleteSubtree( remove_me, false );
886 phylogeny.clearHashIdToNodeMap();
887 phylogeny.externalNodesHaveChanged();
890 final PhylogenyNode parent = remove_me.getParent();
891 final List<PhylogenyNode> descs = remove_me.getDescendants();
892 parent.removeChildNode( remove_me );
893 for( final PhylogenyNode desc : descs ) {
894 parent.addAsChild( desc );
895 desc.setDistanceToParent( addPhylogenyDistances( remove_me.getDistanceToParent(),
896 desc.getDistanceToParent() ) );
898 remove_me.setParent( null );
899 phylogeny.clearHashIdToNodeMap();
900 phylogeny.externalNodesHaveChanged();
904 public static List<PhylogenyNode> searchData( final String query,
906 final boolean case_sensitive,
907 final boolean partial,
908 final boolean search_domains ) {
909 final List<PhylogenyNode> nodes = new ArrayList<PhylogenyNode>();
910 if ( phy.isEmpty() || ( query == null ) ) {
913 if ( ForesterUtil.isEmpty( query ) ) {
916 for( final PhylogenyNodeIterator iter = phy.iteratorPreorder(); iter.hasNext(); ) {
917 final PhylogenyNode node = iter.next();
918 boolean match = false;
919 if ( match( node.getName(), query, case_sensitive, partial ) ) {
922 else if ( node.getNodeData().isHasTaxonomy()
923 && match( node.getNodeData().getTaxonomy().getTaxonomyCode(), query, case_sensitive, partial ) ) {
926 else if ( node.getNodeData().isHasTaxonomy()
927 && match( node.getNodeData().getTaxonomy().getCommonName(), query, case_sensitive, partial ) ) {
930 else if ( node.getNodeData().isHasTaxonomy()
931 && match( node.getNodeData().getTaxonomy().getScientificName(), query, case_sensitive, partial ) ) {
934 else if ( node.getNodeData().isHasTaxonomy()
935 && ( node.getNodeData().getTaxonomy().getIdentifier() != null )
936 && match( node.getNodeData().getTaxonomy().getIdentifier().getValue(),
942 else if ( node.getNodeData().isHasTaxonomy() && !node.getNodeData().getTaxonomy().getSynonyms().isEmpty() ) {
943 final List<String> syns = node.getNodeData().getTaxonomy().getSynonyms();
944 I: for( final String syn : syns ) {
945 if ( match( syn, query, case_sensitive, partial ) ) {
951 if ( !match && node.getNodeData().isHasSequence()
952 && match( node.getNodeData().getSequence().getName(), query, case_sensitive, partial ) ) {
955 if ( !match && node.getNodeData().isHasSequence()
956 && match( node.getNodeData().getSequence().getSymbol(), query, case_sensitive, partial ) ) {
960 && node.getNodeData().isHasSequence()
961 && ( node.getNodeData().getSequence().getAccession() != null )
962 && match( node.getNodeData().getSequence().getAccession().getValue(),
968 if ( search_domains && !match && node.getNodeData().isHasSequence()
969 && ( node.getNodeData().getSequence().getDomainArchitecture() != null ) ) {
970 final DomainArchitecture da = node.getNodeData().getSequence().getDomainArchitecture();
971 I: for( int i = 0; i < da.getNumberOfDomains(); ++i ) {
972 if ( match( da.getDomain( i ).getName(), query, case_sensitive, partial ) ) {
978 if ( !match && ( node.getNodeData().getBinaryCharacters() != null ) ) {
979 Iterator<String> it = node.getNodeData().getBinaryCharacters().getPresentCharacters().iterator();
980 I: while ( it.hasNext() ) {
981 if ( match( it.next(), query, case_sensitive, partial ) ) {
986 it = node.getNodeData().getBinaryCharacters().getGainedCharacters().iterator();
987 I: while ( it.hasNext() ) {
988 if ( match( it.next(), query, case_sensitive, partial ) ) {
1001 public static List<PhylogenyNode> searchDataLogicalAnd( final String[] queries,
1002 final Phylogeny phy,
1003 final boolean case_sensitive,
1004 final boolean partial,
1005 final boolean search_domains ) {
1006 final List<PhylogenyNode> nodes = new ArrayList<PhylogenyNode>();
1007 if ( phy.isEmpty() || ( queries == null ) || ( queries.length < 1 ) ) {
1010 for( final PhylogenyNodeIterator iter = phy.iteratorPreorder(); iter.hasNext(); ) {
1011 final PhylogenyNode node = iter.next();
1012 boolean all_matched = true;
1013 for( final String query : queries ) {
1014 boolean match = false;
1015 if ( ForesterUtil.isEmpty( query ) ) {
1018 if ( match( node.getName(), query, case_sensitive, partial ) ) {
1021 else if ( node.getNodeData().isHasTaxonomy()
1022 && match( node.getNodeData().getTaxonomy().getTaxonomyCode(), query, case_sensitive, partial ) ) {
1025 else if ( node.getNodeData().isHasTaxonomy()
1026 && match( node.getNodeData().getTaxonomy().getCommonName(), query, case_sensitive, partial ) ) {
1029 else if ( node.getNodeData().isHasTaxonomy()
1030 && match( node.getNodeData().getTaxonomy().getScientificName(), query, case_sensitive, partial ) ) {
1033 else if ( node.getNodeData().isHasTaxonomy()
1034 && ( node.getNodeData().getTaxonomy().getIdentifier() != null )
1035 && match( node.getNodeData().getTaxonomy().getIdentifier().getValue(),
1041 else if ( node.getNodeData().isHasTaxonomy()
1042 && !node.getNodeData().getTaxonomy().getSynonyms().isEmpty() ) {
1043 final List<String> syns = node.getNodeData().getTaxonomy().getSynonyms();
1044 I: for( final String syn : syns ) {
1045 if ( match( syn, query, case_sensitive, partial ) ) {
1051 if ( !match && node.getNodeData().isHasSequence()
1052 && match( node.getNodeData().getSequence().getName(), query, case_sensitive, partial ) ) {
1055 if ( !match && node.getNodeData().isHasSequence()
1056 && match( node.getNodeData().getSequence().getSymbol(), query, case_sensitive, partial ) ) {
1060 && node.getNodeData().isHasSequence()
1061 && ( node.getNodeData().getSequence().getAccession() != null )
1062 && match( node.getNodeData().getSequence().getAccession().getValue(),
1068 if ( search_domains && !match && node.getNodeData().isHasSequence()
1069 && ( node.getNodeData().getSequence().getDomainArchitecture() != null ) ) {
1070 final DomainArchitecture da = node.getNodeData().getSequence().getDomainArchitecture();
1071 I: for( int i = 0; i < da.getNumberOfDomains(); ++i ) {
1072 if ( match( da.getDomain( i ).getName(), query, case_sensitive, partial ) ) {
1078 if ( !match && ( node.getNodeData().getBinaryCharacters() != null ) ) {
1079 Iterator<String> it = node.getNodeData().getBinaryCharacters().getPresentCharacters().iterator();
1080 I: while ( it.hasNext() ) {
1081 if ( match( it.next(), query, case_sensitive, partial ) ) {
1086 it = node.getNodeData().getBinaryCharacters().getGainedCharacters().iterator();
1087 I: while ( it.hasNext() ) {
1088 if ( match( it.next(), query, case_sensitive, partial ) ) {
1095 all_matched = false;
1099 if ( all_matched ) {
1107 * Convenience method.
1108 * Sets value for the first confidence value (created if not present, values overwritten otherwise).
1110 public static void setBootstrapConfidence( final PhylogenyNode node, final double bootstrap_confidence_value ) {
1111 setConfidence( node, bootstrap_confidence_value, "bootstrap" );
1114 public static void setBranchColorValue( final PhylogenyNode node, final Color color ) {
1115 if ( node.getBranchData().getBranchColor() == null ) {
1116 node.getBranchData().setBranchColor( new BranchColor() );
1118 node.getBranchData().getBranchColor().setValue( color );
1122 * Convenience method
1124 public static void setBranchWidthValue( final PhylogenyNode node, final double branch_width_value ) {
1125 node.getBranchData().setBranchWidth( new BranchWidth( branch_width_value ) );
1129 * Convenience method.
1130 * Sets value for the first confidence value (created if not present, values overwritten otherwise).
1132 public static void setConfidence( final PhylogenyNode node, final double confidence_value ) {
1133 setConfidence( node, confidence_value, "" );
1137 * Convenience method.
1138 * Sets value for the first confidence value (created if not present, values overwritten otherwise).
1140 public static void setConfidence( final PhylogenyNode node, final double confidence_value, final String type ) {
1141 Confidence c = null;
1142 if ( node.getBranchData().getNumberOfConfidences() > 0 ) {
1143 c = node.getBranchData().getConfidence( 0 );
1146 c = new Confidence();
1147 node.getBranchData().addConfidence( c );
1150 c.setValue( confidence_value );
1153 public static void setScientificName( final PhylogenyNode node, final String scientific_name ) {
1154 if ( !node.getNodeData().isHasTaxonomy() ) {
1155 node.getNodeData().setTaxonomy( new Taxonomy() );
1157 node.getNodeData().getTaxonomy().setScientificName( scientific_name );
1161 * Convenience method to set the taxonomy code of a phylogeny node.
1165 * @param taxonomy_code
1166 * @throws PhyloXmlDataFormatException
1168 public static void setTaxonomyCode( final PhylogenyNode node, final String taxonomy_code )
1169 throws PhyloXmlDataFormatException {
1170 if ( !node.getNodeData().isHasTaxonomy() ) {
1171 node.getNodeData().setTaxonomy( new Taxonomy() );
1173 node.getNodeData().getTaxonomy().setTaxonomyCode( taxonomy_code );
1176 final static public void sortNodeDescendents( final PhylogenyNode node, final DESCENDANT_SORT_PRIORITY pri ) {
1177 class PhylogenyNodeSortTaxonomyPriority implements Comparator<PhylogenyNode> {
1180 public int compare( final PhylogenyNode n1, final PhylogenyNode n2 ) {
1181 if ( n1.getNodeData().isHasTaxonomy() && n2.getNodeData().isHasTaxonomy() ) {
1182 if ( ( !ForesterUtil.isEmpty( n1.getNodeData().getTaxonomy().getScientificName() ) )
1183 && ( !ForesterUtil.isEmpty( n2.getNodeData().getTaxonomy().getScientificName() ) ) ) {
1184 return n1.getNodeData().getTaxonomy().getScientificName().toLowerCase()
1185 .compareTo( n2.getNodeData().getTaxonomy().getScientificName().toLowerCase() );
1187 if ( ( !ForesterUtil.isEmpty( n1.getNodeData().getTaxonomy().getTaxonomyCode() ) )
1188 && ( !ForesterUtil.isEmpty( n2.getNodeData().getTaxonomy().getTaxonomyCode() ) ) ) {
1189 return n1.getNodeData().getTaxonomy().getTaxonomyCode()
1190 .compareTo( n2.getNodeData().getTaxonomy().getTaxonomyCode() );
1192 if ( ( !ForesterUtil.isEmpty( n1.getNodeData().getTaxonomy().getCommonName() ) )
1193 && ( !ForesterUtil.isEmpty( n2.getNodeData().getTaxonomy().getCommonName() ) ) ) {
1194 return n1.getNodeData().getTaxonomy().getCommonName().toLowerCase()
1195 .compareTo( n2.getNodeData().getTaxonomy().getCommonName().toLowerCase() );
1198 if ( n1.getNodeData().isHasSequence() && n2.getNodeData().isHasSequence() ) {
1199 if ( ( !ForesterUtil.isEmpty( n1.getNodeData().getSequence().getName() ) )
1200 && ( !ForesterUtil.isEmpty( n2.getNodeData().getSequence().getName() ) ) ) {
1201 return n1.getNodeData().getSequence().getName().toLowerCase()
1202 .compareTo( n2.getNodeData().getSequence().getName().toLowerCase() );
1204 if ( ( !ForesterUtil.isEmpty( n1.getNodeData().getSequence().getSymbol() ) )
1205 && ( !ForesterUtil.isEmpty( n2.getNodeData().getSequence().getSymbol() ) ) ) {
1206 return n1.getNodeData().getSequence().getSymbol()
1207 .compareTo( n2.getNodeData().getSequence().getSymbol() );
1209 if ( ( n1.getNodeData().getSequence().getAccession() != null )
1210 && ( n2.getNodeData().getSequence().getAccession() != null )
1211 && !ForesterUtil.isEmpty( n1.getNodeData().getSequence().getAccession().getValue() )
1212 && !ForesterUtil.isEmpty( n2.getNodeData().getSequence().getAccession().getValue() ) ) {
1213 return n1.getNodeData().getSequence().getAccession().getValue()
1214 .compareTo( n2.getNodeData().getSequence().getAccession().getValue() );
1217 if ( ( !ForesterUtil.isEmpty( n1.getName() ) ) && ( !ForesterUtil.isEmpty( n2.getName() ) ) ) {
1218 return n1.getName().toLowerCase().compareTo( n2.getName().toLowerCase() );
1223 class PhylogenyNodeSortSequencePriority implements Comparator<PhylogenyNode> {
1226 public int compare( final PhylogenyNode n1, final PhylogenyNode n2 ) {
1227 if ( n1.getNodeData().isHasSequence() && n2.getNodeData().isHasSequence() ) {
1228 if ( ( !ForesterUtil.isEmpty( n1.getNodeData().getSequence().getName() ) )
1229 && ( !ForesterUtil.isEmpty( n2.getNodeData().getSequence().getName() ) ) ) {
1230 return n1.getNodeData().getSequence().getName().toLowerCase()
1231 .compareTo( n2.getNodeData().getSequence().getName().toLowerCase() );
1233 if ( ( !ForesterUtil.isEmpty( n1.getNodeData().getSequence().getSymbol() ) )
1234 && ( !ForesterUtil.isEmpty( n2.getNodeData().getSequence().getSymbol() ) ) ) {
1235 return n1.getNodeData().getSequence().getSymbol()
1236 .compareTo( n2.getNodeData().getSequence().getSymbol() );
1238 if ( ( n1.getNodeData().getSequence().getAccession() != null )
1239 && ( n2.getNodeData().getSequence().getAccession() != null )
1240 && !ForesterUtil.isEmpty( n1.getNodeData().getSequence().getAccession().getValue() )
1241 && !ForesterUtil.isEmpty( n2.getNodeData().getSequence().getAccession().getValue() ) ) {
1242 return n1.getNodeData().getSequence().getAccession().getValue()
1243 .compareTo( n2.getNodeData().getSequence().getAccession().getValue() );
1246 if ( n1.getNodeData().isHasTaxonomy() && n2.getNodeData().isHasTaxonomy() ) {
1247 if ( ( !ForesterUtil.isEmpty( n1.getNodeData().getTaxonomy().getScientificName() ) )
1248 && ( !ForesterUtil.isEmpty( n2.getNodeData().getTaxonomy().getScientificName() ) ) ) {
1249 return n1.getNodeData().getTaxonomy().getScientificName().toLowerCase()
1250 .compareTo( n2.getNodeData().getTaxonomy().getScientificName().toLowerCase() );
1252 if ( ( !ForesterUtil.isEmpty( n1.getNodeData().getTaxonomy().getTaxonomyCode() ) )
1253 && ( !ForesterUtil.isEmpty( n2.getNodeData().getTaxonomy().getTaxonomyCode() ) ) ) {
1254 return n1.getNodeData().getTaxonomy().getTaxonomyCode()
1255 .compareTo( n2.getNodeData().getTaxonomy().getTaxonomyCode() );
1257 if ( ( !ForesterUtil.isEmpty( n1.getNodeData().getTaxonomy().getCommonName() ) )
1258 && ( !ForesterUtil.isEmpty( n2.getNodeData().getTaxonomy().getCommonName() ) ) ) {
1259 return n1.getNodeData().getTaxonomy().getCommonName().toLowerCase()
1260 .compareTo( n2.getNodeData().getTaxonomy().getCommonName().toLowerCase() );
1263 if ( ( !ForesterUtil.isEmpty( n1.getName() ) ) && ( !ForesterUtil.isEmpty( n2.getName() ) ) ) {
1264 return n1.getName().toLowerCase().compareTo( n2.getName().toLowerCase() );
1269 class PhylogenyNodeSortNodeNamePriority implements Comparator<PhylogenyNode> {
1272 public int compare( final PhylogenyNode n1, final PhylogenyNode n2 ) {
1273 if ( ( !ForesterUtil.isEmpty( n1.getName() ) ) && ( !ForesterUtil.isEmpty( n2.getName() ) ) ) {
1274 return n1.getName().toLowerCase().compareTo( n2.getName().toLowerCase() );
1276 if ( n1.getNodeData().isHasTaxonomy() && n2.getNodeData().isHasTaxonomy() ) {
1277 if ( ( !ForesterUtil.isEmpty( n1.getNodeData().getTaxonomy().getScientificName() ) )
1278 && ( !ForesterUtil.isEmpty( n2.getNodeData().getTaxonomy().getScientificName() ) ) ) {
1279 return n1.getNodeData().getTaxonomy().getScientificName().toLowerCase()
1280 .compareTo( n2.getNodeData().getTaxonomy().getScientificName().toLowerCase() );
1282 if ( ( !ForesterUtil.isEmpty( n1.getNodeData().getTaxonomy().getTaxonomyCode() ) )
1283 && ( !ForesterUtil.isEmpty( n2.getNodeData().getTaxonomy().getTaxonomyCode() ) ) ) {
1284 return n1.getNodeData().getTaxonomy().getTaxonomyCode()
1285 .compareTo( n2.getNodeData().getTaxonomy().getTaxonomyCode() );
1287 if ( ( !ForesterUtil.isEmpty( n1.getNodeData().getTaxonomy().getCommonName() ) )
1288 && ( !ForesterUtil.isEmpty( n2.getNodeData().getTaxonomy().getCommonName() ) ) ) {
1289 return n1.getNodeData().getTaxonomy().getCommonName().toLowerCase()
1290 .compareTo( n2.getNodeData().getTaxonomy().getCommonName().toLowerCase() );
1293 if ( n1.getNodeData().isHasSequence() && n2.getNodeData().isHasSequence() ) {
1294 if ( ( !ForesterUtil.isEmpty( n1.getNodeData().getSequence().getName() ) )
1295 && ( !ForesterUtil.isEmpty( n2.getNodeData().getSequence().getName() ) ) ) {
1296 return n1.getNodeData().getSequence().getName().toLowerCase()
1297 .compareTo( n2.getNodeData().getSequence().getName().toLowerCase() );
1299 if ( ( !ForesterUtil.isEmpty( n1.getNodeData().getSequence().getSymbol() ) )
1300 && ( !ForesterUtil.isEmpty( n2.getNodeData().getSequence().getSymbol() ) ) ) {
1301 return n1.getNodeData().getSequence().getSymbol()
1302 .compareTo( n2.getNodeData().getSequence().getSymbol() );
1304 if ( ( n1.getNodeData().getSequence().getAccession() != null )
1305 && ( n2.getNodeData().getSequence().getAccession() != null )
1306 && !ForesterUtil.isEmpty( n1.getNodeData().getSequence().getAccession().getValue() )
1307 && !ForesterUtil.isEmpty( n2.getNodeData().getSequence().getAccession().getValue() ) ) {
1308 return n1.getNodeData().getSequence().getAccession().getValue()
1309 .compareTo( n2.getNodeData().getSequence().getAccession().getValue() );
1315 Comparator<PhylogenyNode> c;
1318 c = new PhylogenyNodeSortSequencePriority();
1321 c = new PhylogenyNodeSortNodeNamePriority();
1324 c = new PhylogenyNodeSortTaxonomyPriority();
1326 final List<PhylogenyNode> descs = node.getDescendants();
1327 Collections.sort( descs, c );
1329 for( final PhylogenyNode desc : descs ) {
1330 node.setChildNode( i++, desc );
1335 * Removes from Phylogeny to_be_stripped all external Nodes which are
1336 * associated with a species NOT found in Phylogeny reference.
1339 * a reference Phylogeny
1340 * @param to_be_stripped
1341 * Phylogeny to be stripped
1342 * @return nodes removed from to_be_stripped
1344 public static List<PhylogenyNode> taxonomyBasedDeletionOfExternalNodes( final Phylogeny reference,
1345 final Phylogeny to_be_stripped ) {
1346 final Set<String> ref_ext_taxo = new HashSet<String>();
1347 for( final PhylogenyNodeIterator it = reference.iteratorExternalForward(); it.hasNext(); ) {
1348 final PhylogenyNode n = it.next();
1349 if ( !n.getNodeData().isHasTaxonomy() ) {
1350 throw new IllegalArgumentException( "no taxonomic data in node: " + n );
1352 // ref_ext_taxo.add( getSpecies( n ) );
1353 if ( !ForesterUtil.isEmpty( n.getNodeData().getTaxonomy().getScientificName() ) ) {
1354 ref_ext_taxo.add( n.getNodeData().getTaxonomy().getScientificName() );
1356 if ( !ForesterUtil.isEmpty( n.getNodeData().getTaxonomy().getTaxonomyCode() ) ) {
1357 ref_ext_taxo.add( n.getNodeData().getTaxonomy().getTaxonomyCode() );
1360 final ArrayList<PhylogenyNode> nodes_to_delete = new ArrayList<PhylogenyNode>();
1361 for( final PhylogenyNodeIterator it = to_be_stripped.iteratorExternalForward(); it.hasNext(); ) {
1362 final PhylogenyNode n = it.next();
1363 if ( !n.getNodeData().isHasTaxonomy() ) {
1364 nodes_to_delete.add( n );
1366 else if ( !( ref_ext_taxo.contains( n.getNodeData().getTaxonomy().getScientificName() ) )
1367 && !( ref_ext_taxo.contains( n.getNodeData().getTaxonomy().getTaxonomyCode() ) ) ) {
1368 nodes_to_delete.add( n );
1371 for( final PhylogenyNode n : nodes_to_delete ) {
1372 to_be_stripped.deleteSubtree( n, true );
1374 to_be_stripped.clearHashIdToNodeMap();
1375 to_be_stripped.externalNodesHaveChanged();
1376 return nodes_to_delete;
1379 final static public void transferInternalNamesToBootstrapSupport( final Phylogeny phy ) {
1380 final PhylogenyNodeIterator it = phy.iteratorPostorder();
1381 while ( it.hasNext() ) {
1382 final PhylogenyNode n = it.next();
1383 if ( !n.isExternal() && !ForesterUtil.isEmpty( n.getName() ) ) {
1386 value = Double.parseDouble( n.getName() );
1388 catch ( final NumberFormatException e ) {
1389 throw new IllegalArgumentException( "failed to parse number from [" + n.getName() + "]: "
1390 + e.getLocalizedMessage() );
1392 if ( value >= 0.0 ) {
1393 n.getBranchData().addConfidence( new Confidence( value, "bootstrap" ) );
1400 final static public void transferInternalNodeNamesToConfidence( final Phylogeny phy ) {
1401 final PhylogenyNodeIterator it = phy.iteratorPostorder();
1402 while ( it.hasNext() ) {
1403 final PhylogenyNode n = it.next();
1404 if ( !n.isExternal() && !n.getBranchData().isHasConfidences() ) {
1405 if ( !ForesterUtil.isEmpty( n.getName() ) ) {
1408 d = Double.parseDouble( n.getName() );
1410 catch ( final Exception e ) {
1414 n.getBranchData().addConfidence( new Confidence( d, "" ) );
1422 final static public void transferNodeNameToField( final Phylogeny phy,
1423 final PhylogenyNodeField field,
1424 final boolean external_only ) throws PhyloXmlDataFormatException {
1425 final PhylogenyNodeIterator it = phy.iteratorPostorder();
1426 while ( it.hasNext() ) {
1427 final PhylogenyNode n = it.next();
1428 if ( external_only && n.isInternal() ) {
1431 final String name = n.getName().trim();
1432 if ( !ForesterUtil.isEmpty( name ) ) {
1436 setTaxonomyCode( n, name );
1438 case TAXONOMY_SCIENTIFIC_NAME:
1440 if ( !n.getNodeData().isHasTaxonomy() ) {
1441 n.getNodeData().setTaxonomy( new Taxonomy() );
1443 n.getNodeData().getTaxonomy().setScientificName( name );
1445 case TAXONOMY_COMMON_NAME:
1447 if ( !n.getNodeData().isHasTaxonomy() ) {
1448 n.getNodeData().setTaxonomy( new Taxonomy() );
1450 n.getNodeData().getTaxonomy().setCommonName( name );
1452 case SEQUENCE_SYMBOL:
1454 if ( !n.getNodeData().isHasSequence() ) {
1455 n.getNodeData().setSequence( new Sequence() );
1457 n.getNodeData().getSequence().setSymbol( name );
1461 if ( !n.getNodeData().isHasSequence() ) {
1462 n.getNodeData().setSequence( new Sequence() );
1464 n.getNodeData().getSequence().setName( name );
1466 case TAXONOMY_ID_UNIPROT_1: {
1467 if ( !n.getNodeData().isHasTaxonomy() ) {
1468 n.getNodeData().setTaxonomy( new Taxonomy() );
1471 final int i = name.indexOf( '_' );
1473 id = name.substring( 0, i );
1478 n.getNodeData().getTaxonomy()
1479 .setIdentifier( new Identifier( id, PhyloXmlUtil.UNIPROT_TAX_PROVIDER ) );
1482 case TAXONOMY_ID_UNIPROT_2: {
1483 if ( !n.getNodeData().isHasTaxonomy() ) {
1484 n.getNodeData().setTaxonomy( new Taxonomy() );
1487 final int i = name.indexOf( '_' );
1489 id = name.substring( i + 1, name.length() );
1494 n.getNodeData().getTaxonomy()
1495 .setIdentifier( new Identifier( id, PhyloXmlUtil.UNIPROT_TAX_PROVIDER ) );
1499 if ( !n.getNodeData().isHasTaxonomy() ) {
1500 n.getNodeData().setTaxonomy( new Taxonomy() );
1502 n.getNodeData().getTaxonomy().setIdentifier( new Identifier( name ) );
1510 static double addPhylogenyDistances( final double a, final double b ) {
1511 if ( ( a >= 0.0 ) && ( b >= 0.0 ) ) {
1514 else if ( a >= 0.0 ) {
1517 else if ( b >= 0.0 ) {
1520 return PhylogenyDataUtil.BRANCH_LENGTH_DEFAULT;
1524 * Deep copies the phylogeny originating from this node.
1526 static PhylogenyNode copySubTree( final PhylogenyNode source ) {
1527 if ( source == null ) {
1531 final PhylogenyNode newnode = source.copyNodeData();
1532 if ( !source.isExternal() ) {
1533 for( int i = 0; i < source.getNumberOfDescendants(); ++i ) {
1534 newnode.setChildNode( i, PhylogenyMethods.copySubTree( source.getChildNode( i ) ) );
1542 * Shallow copies the phylogeny originating from this node.
1544 static PhylogenyNode copySubTreeShallow( final PhylogenyNode source ) {
1545 if ( source == null ) {
1549 final PhylogenyNode newnode = source.copyNodeDataShallow();
1550 if ( !source.isExternal() ) {
1551 for( int i = 0; i < source.getNumberOfDescendants(); ++i ) {
1552 newnode.setChildNode( i, PhylogenyMethods.copySubTreeShallow( source.getChildNode( i ) ) );
1560 * Calculates the distance between PhylogenyNodes n1 and n2.
1561 * PRECONDITION: n1 is a descendant of n2.
1564 * a descendant of n2
1566 * @return distance between n1 and n2
1568 private static double getDistance( PhylogenyNode n1, final PhylogenyNode n2 ) {
1570 while ( n1 != n2 ) {
1571 if ( n1.getDistanceToParent() > 0.0 ) {
1572 d += n1.getDistanceToParent();
1574 n1 = n1.getParent();
1579 private static boolean match( final String s,
1581 final boolean case_sensitive,
1582 final boolean partial ) {
1583 if ( ForesterUtil.isEmpty( s ) || ForesterUtil.isEmpty( query ) ) {
1586 String my_s = s.trim();
1587 String my_query = query.trim();
1588 if ( !case_sensitive ) {
1589 my_s = my_s.toLowerCase();
1590 my_query = my_query.toLowerCase();
1593 return my_s.indexOf( my_query ) >= 0;
1596 return my_s.equals( my_query );
1600 public static enum DESCENDANT_SORT_PRIORITY {
1601 TAXONOMY, SEQUENCE, NODE_NAME;
1604 public static enum PhylogenyNodeField {
1607 TAXONOMY_SCIENTIFIC_NAME,
1608 TAXONOMY_COMMON_NAME,
1611 TAXONOMY_ID_UNIPROT_1,
1612 TAXONOMY_ID_UNIPROT_2,