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.HashSet;
36 import java.util.Iterator;
37 import java.util.List;
39 import java.util.SortedMap;
40 import java.util.TreeMap;
42 import org.forester.io.parsers.PhylogenyParser;
43 import org.forester.io.parsers.phyloxml.PhyloXmlUtil;
44 import org.forester.io.parsers.util.PhylogenyParserException;
45 import org.forester.phylogeny.data.BranchColor;
46 import org.forester.phylogeny.data.BranchWidth;
47 import org.forester.phylogeny.data.Confidence;
48 import org.forester.phylogeny.data.DomainArchitecture;
49 import org.forester.phylogeny.data.Identifier;
50 import org.forester.phylogeny.data.PhylogenyDataUtil;
51 import org.forester.phylogeny.data.Sequence;
52 import org.forester.phylogeny.data.Taxonomy;
53 import org.forester.phylogeny.factories.ParserBasedPhylogenyFactory;
54 import org.forester.phylogeny.factories.PhylogenyFactory;
55 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
56 import org.forester.util.BasicDescriptiveStatistics;
57 import org.forester.util.DescriptiveStatistics;
58 import org.forester.util.FailedConditionCheckException;
59 import org.forester.util.ForesterUtil;
61 public class PhylogenyMethods {
63 private static PhylogenyMethods _instance = null;
64 private final Set<Integer> _temp_hash_set = new HashSet<Integer>();
65 private PhylogenyNode _farthest_1 = null;
66 private PhylogenyNode _farthest_2 = null;
68 private PhylogenyMethods() {
69 // Hidden constructor.
73 * Calculates the distance between PhylogenyNodes node1 and node2.
78 * @return distance between node1 and node2
80 public double calculateDistance( final PhylogenyNode node1, final PhylogenyNode node2 ) {
81 final PhylogenyNode lca = obtainLCA( node1, node2 );
82 final PhylogenyNode n1 = node1;
83 final PhylogenyNode n2 = node2;
84 return ( PhylogenyMethods.getDistance( n1, lca ) + PhylogenyMethods.getDistance( n2, lca ) );
87 public double calculateFurthestDistance( final Phylogeny phylogeny ) {
88 if ( phylogeny.getNumberOfExternalNodes() < 2 ) {
93 PhylogenyNode node_1 = null;
94 PhylogenyNode node_2 = null;
95 double farthest_d = -Double.MAX_VALUE;
96 final PhylogenyMethods methods = PhylogenyMethods.getInstance();
97 final List<PhylogenyNode> ext_nodes = phylogeny.getRoot().getAllExternalDescendants();
98 for( int i = 1; i < ext_nodes.size(); ++i ) {
99 for( int j = 0; j < i; ++j ) {
100 final double d = methods.calculateDistance( ext_nodes.get( i ), ext_nodes.get( j ) );
102 throw new RuntimeException( "distance cannot be negative" );
104 if ( d > farthest_d ) {
106 node_1 = ext_nodes.get( i );
107 node_2 = ext_nodes.get( j );
111 _farthest_1 = node_1;
112 _farthest_2 = node_2;
117 public Object clone() throws CloneNotSupportedException {
118 throw new CloneNotSupportedException();
121 public PhylogenyNode getFarthestNode1() {
125 public PhylogenyNode getFarthestNode2() {
130 * Returns the LCA of PhylogenyNodes node1 and node2.
135 * @return LCA of node1 and node2
137 public PhylogenyNode obtainLCA( final PhylogenyNode node1, final PhylogenyNode node2 ) {
138 _temp_hash_set.clear();
139 PhylogenyNode n1 = node1;
140 PhylogenyNode n2 = node2;
141 _temp_hash_set.add( n1.getId() );
142 while ( !n1.isRoot() ) {
144 _temp_hash_set.add( n1.getId() );
146 while ( !_temp_hash_set.contains( n2.getId() ) && !n2.isRoot() ) {
149 if ( !_temp_hash_set.contains( n2.getId() ) ) {
150 throw new IllegalArgumentException( "attempt to get LCA of two nodes which do not share a common root" );
156 * Returns all orthologs of the external PhylogenyNode n of this Phylogeny.
157 * Orthologs are returned as List of node references.
159 * PRECONDITION: This tree must be binary and rooted, and speciation -
160 * duplication need to be assigned for each of its internal Nodes.
162 * Returns null if this Phylogeny is empty or if n is internal.
164 * external PhylogenyNode whose orthologs are to be returned
165 * @return Vector of references to all orthologous Nodes of PhylogenyNode n
166 * of this Phylogeny, null if this Phylogeny is empty or if n is
169 public List<PhylogenyNode> getOrthologousNodes( final Phylogeny phy, final PhylogenyNode node ) {
170 final List<PhylogenyNode> nodes = new ArrayList<PhylogenyNode>();
171 final PhylogenyNodeIterator it = phy.iteratorExternalForward();
172 while ( it.hasNext() ) {
173 final PhylogenyNode temp_node = it.next();
174 if ( ( temp_node != node ) && isAreOrthologous( node, temp_node ) ) {
175 nodes.add( temp_node );
181 public boolean isAreOrthologous( final PhylogenyNode node1, final PhylogenyNode node2 ) {
182 return !obtainLCA( node1, node2 ).isDuplication();
185 public final static Phylogeny[] readPhylogenies( final PhylogenyParser parser, final File file ) throws IOException {
186 final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
187 final Phylogeny[] trees = factory.create( file, parser );
188 if ( ( trees == null ) || ( trees.length == 0 ) ) {
189 throw new PhylogenyParserException( "Unable to parse phylogeny from file: " + file );
194 final static public void transferInternalNodeNamesToConfidence( final Phylogeny phy ) {
195 final PhylogenyNodeIterator it = phy.iteratorPostorder();
196 while ( it.hasNext() ) {
197 final PhylogenyNode n = it.next();
198 if ( !n.isExternal() && !n.getBranchData().isHasConfidences() ) {
199 if ( !ForesterUtil.isEmpty( n.getName() ) ) {
202 d = Double.parseDouble( n.getName() );
204 catch ( final Exception e ) {
208 n.getBranchData().addConfidence( new Confidence( d, "" ) );
216 final static public void transferInternalNamesToBootstrapSupport( final Phylogeny phy ) {
217 final PhylogenyNodeIterator it = phy.iteratorPostorder();
218 while ( it.hasNext() ) {
219 final PhylogenyNode n = it.next();
220 if ( !n.isExternal() && !ForesterUtil.isEmpty( n.getName() ) ) {
223 value = Double.parseDouble( n.getName() );
225 catch ( final NumberFormatException e ) {
226 throw new IllegalArgumentException( "failed to parse number from [" + n.getName() + "]: "
227 + e.getLocalizedMessage() );
229 if ( value >= 0.0 ) {
230 n.getBranchData().addConfidence( new Confidence( value, "bootstrap" ) );
237 final static public void sortNodeDescendents( final PhylogenyNode node, final DESCENDANT_SORT_PRIORITY pri ) {
238 class PhylogenyNodeSortTaxonomyPriority implements Comparator<PhylogenyNode> {
241 public int compare( final PhylogenyNode n1, final PhylogenyNode n2 ) {
242 if ( n1.getNodeData().isHasTaxonomy() && n2.getNodeData().isHasTaxonomy() ) {
243 if ( ( !ForesterUtil.isEmpty( n1.getNodeData().getTaxonomy().getScientificName() ) )
244 && ( !ForesterUtil.isEmpty( n2.getNodeData().getTaxonomy().getScientificName() ) ) ) {
245 return n1.getNodeData().getTaxonomy().getScientificName().toLowerCase()
246 .compareTo( n2.getNodeData().getTaxonomy().getScientificName().toLowerCase() );
248 if ( ( !ForesterUtil.isEmpty( n1.getNodeData().getTaxonomy().getTaxonomyCode() ) )
249 && ( !ForesterUtil.isEmpty( n2.getNodeData().getTaxonomy().getTaxonomyCode() ) ) ) {
250 return n1.getNodeData().getTaxonomy().getTaxonomyCode()
251 .compareTo( n2.getNodeData().getTaxonomy().getTaxonomyCode() );
253 if ( ( !ForesterUtil.isEmpty( n1.getNodeData().getTaxonomy().getCommonName() ) )
254 && ( !ForesterUtil.isEmpty( n2.getNodeData().getTaxonomy().getCommonName() ) ) ) {
255 return n1.getNodeData().getTaxonomy().getCommonName().toLowerCase()
256 .compareTo( n2.getNodeData().getTaxonomy().getCommonName().toLowerCase() );
259 if ( n1.getNodeData().isHasSequence() && n2.getNodeData().isHasSequence() ) {
260 if ( ( !ForesterUtil.isEmpty( n1.getNodeData().getSequence().getName() ) )
261 && ( !ForesterUtil.isEmpty( n2.getNodeData().getSequence().getName() ) ) ) {
262 return n1.getNodeData().getSequence().getName().toLowerCase()
263 .compareTo( n2.getNodeData().getSequence().getName().toLowerCase() );
265 if ( ( !ForesterUtil.isEmpty( n1.getNodeData().getSequence().getSymbol() ) )
266 && ( !ForesterUtil.isEmpty( n2.getNodeData().getSequence().getSymbol() ) ) ) {
267 return n1.getNodeData().getSequence().getSymbol()
268 .compareTo( n2.getNodeData().getSequence().getSymbol() );
270 if ( ( n1.getNodeData().getSequence().getAccession() != null )
271 && ( n2.getNodeData().getSequence().getAccession() != null )
272 && !ForesterUtil.isEmpty( n1.getNodeData().getSequence().getAccession().getValue() )
273 && !ForesterUtil.isEmpty( n2.getNodeData().getSequence().getAccession().getValue() ) ) {
274 return n1.getNodeData().getSequence().getAccession().getValue()
275 .compareTo( n2.getNodeData().getSequence().getAccession().getValue() );
278 if ( ( !ForesterUtil.isEmpty( n1.getName() ) ) && ( !ForesterUtil.isEmpty( n2.getName() ) ) ) {
279 return n1.getName().toLowerCase().compareTo( n2.getName().toLowerCase() );
284 class PhylogenyNodeSortSequencePriority implements Comparator<PhylogenyNode> {
287 public int compare( final PhylogenyNode n1, final PhylogenyNode n2 ) {
288 if ( n1.getNodeData().isHasSequence() && n2.getNodeData().isHasSequence() ) {
289 if ( ( !ForesterUtil.isEmpty( n1.getNodeData().getSequence().getName() ) )
290 && ( !ForesterUtil.isEmpty( n2.getNodeData().getSequence().getName() ) ) ) {
291 return n1.getNodeData().getSequence().getName().toLowerCase()
292 .compareTo( n2.getNodeData().getSequence().getName().toLowerCase() );
294 if ( ( !ForesterUtil.isEmpty( n1.getNodeData().getSequence().getSymbol() ) )
295 && ( !ForesterUtil.isEmpty( n2.getNodeData().getSequence().getSymbol() ) ) ) {
296 return n1.getNodeData().getSequence().getSymbol()
297 .compareTo( n2.getNodeData().getSequence().getSymbol() );
299 if ( ( n1.getNodeData().getSequence().getAccession() != null )
300 && ( n2.getNodeData().getSequence().getAccession() != null )
301 && !ForesterUtil.isEmpty( n1.getNodeData().getSequence().getAccession().getValue() )
302 && !ForesterUtil.isEmpty( n2.getNodeData().getSequence().getAccession().getValue() ) ) {
303 return n1.getNodeData().getSequence().getAccession().getValue()
304 .compareTo( n2.getNodeData().getSequence().getAccession().getValue() );
307 if ( n1.getNodeData().isHasTaxonomy() && n2.getNodeData().isHasTaxonomy() ) {
308 if ( ( !ForesterUtil.isEmpty( n1.getNodeData().getTaxonomy().getScientificName() ) )
309 && ( !ForesterUtil.isEmpty( n2.getNodeData().getTaxonomy().getScientificName() ) ) ) {
310 return n1.getNodeData().getTaxonomy().getScientificName().toLowerCase()
311 .compareTo( n2.getNodeData().getTaxonomy().getScientificName().toLowerCase() );
313 if ( ( !ForesterUtil.isEmpty( n1.getNodeData().getTaxonomy().getTaxonomyCode() ) )
314 && ( !ForesterUtil.isEmpty( n2.getNodeData().getTaxonomy().getTaxonomyCode() ) ) ) {
315 return n1.getNodeData().getTaxonomy().getTaxonomyCode()
316 .compareTo( n2.getNodeData().getTaxonomy().getTaxonomyCode() );
318 if ( ( !ForesterUtil.isEmpty( n1.getNodeData().getTaxonomy().getCommonName() ) )
319 && ( !ForesterUtil.isEmpty( n2.getNodeData().getTaxonomy().getCommonName() ) ) ) {
320 return n1.getNodeData().getTaxonomy().getCommonName().toLowerCase()
321 .compareTo( n2.getNodeData().getTaxonomy().getCommonName().toLowerCase() );
324 if ( ( !ForesterUtil.isEmpty( n1.getName() ) ) && ( !ForesterUtil.isEmpty( n2.getName() ) ) ) {
325 return n1.getName().toLowerCase().compareTo( n2.getName().toLowerCase() );
330 class PhylogenyNodeSortNodeNamePriority implements Comparator<PhylogenyNode> {
333 public int compare( final PhylogenyNode n1, final PhylogenyNode n2 ) {
334 if ( ( !ForesterUtil.isEmpty( n1.getName() ) ) && ( !ForesterUtil.isEmpty( n2.getName() ) ) ) {
335 return n1.getName().toLowerCase().compareTo( n2.getName().toLowerCase() );
337 if ( n1.getNodeData().isHasTaxonomy() && n2.getNodeData().isHasTaxonomy() ) {
338 if ( ( !ForesterUtil.isEmpty( n1.getNodeData().getTaxonomy().getScientificName() ) )
339 && ( !ForesterUtil.isEmpty( n2.getNodeData().getTaxonomy().getScientificName() ) ) ) {
340 return n1.getNodeData().getTaxonomy().getScientificName().toLowerCase()
341 .compareTo( n2.getNodeData().getTaxonomy().getScientificName().toLowerCase() );
343 if ( ( !ForesterUtil.isEmpty( n1.getNodeData().getTaxonomy().getTaxonomyCode() ) )
344 && ( !ForesterUtil.isEmpty( n2.getNodeData().getTaxonomy().getTaxonomyCode() ) ) ) {
345 return n1.getNodeData().getTaxonomy().getTaxonomyCode()
346 .compareTo( n2.getNodeData().getTaxonomy().getTaxonomyCode() );
348 if ( ( !ForesterUtil.isEmpty( n1.getNodeData().getTaxonomy().getCommonName() ) )
349 && ( !ForesterUtil.isEmpty( n2.getNodeData().getTaxonomy().getCommonName() ) ) ) {
350 return n1.getNodeData().getTaxonomy().getCommonName().toLowerCase()
351 .compareTo( n2.getNodeData().getTaxonomy().getCommonName().toLowerCase() );
354 if ( n1.getNodeData().isHasSequence() && n2.getNodeData().isHasSequence() ) {
355 if ( ( !ForesterUtil.isEmpty( n1.getNodeData().getSequence().getName() ) )
356 && ( !ForesterUtil.isEmpty( n2.getNodeData().getSequence().getName() ) ) ) {
357 return n1.getNodeData().getSequence().getName().toLowerCase()
358 .compareTo( n2.getNodeData().getSequence().getName().toLowerCase() );
360 if ( ( !ForesterUtil.isEmpty( n1.getNodeData().getSequence().getSymbol() ) )
361 && ( !ForesterUtil.isEmpty( n2.getNodeData().getSequence().getSymbol() ) ) ) {
362 return n1.getNodeData().getSequence().getSymbol()
363 .compareTo( n2.getNodeData().getSequence().getSymbol() );
365 if ( ( n1.getNodeData().getSequence().getAccession() != null )
366 && ( n2.getNodeData().getSequence().getAccession() != null )
367 && !ForesterUtil.isEmpty( n1.getNodeData().getSequence().getAccession().getValue() )
368 && !ForesterUtil.isEmpty( n2.getNodeData().getSequence().getAccession().getValue() ) ) {
369 return n1.getNodeData().getSequence().getAccession().getValue()
370 .compareTo( n2.getNodeData().getSequence().getAccession().getValue() );
376 Comparator<PhylogenyNode> c;
379 c = new PhylogenyNodeSortSequencePriority();
382 c = new PhylogenyNodeSortNodeNamePriority();
385 c = new PhylogenyNodeSortTaxonomyPriority();
387 final List<PhylogenyNode> descs = node.getDescendants();
388 Collections.sort( descs, c );
390 for( final PhylogenyNode desc : descs ) {
391 node.setChildNode( i++, desc );
395 final static public void transferNodeNameToField( final Phylogeny phy,
396 final PhylogenyMethods.PhylogenyNodeField field ) {
397 final PhylogenyNodeIterator it = phy.iteratorPostorder();
398 while ( it.hasNext() ) {
399 final PhylogenyNode n = it.next();
400 final String name = n.getName().trim();
401 if ( !ForesterUtil.isEmpty( name ) ) {
405 // if ( name.length() > 5 ) {
407 // if ( !n.getNodeData().isHasTaxonomy() ) {
408 // n.getNodeData().setTaxonomy( new Taxonomy() );
410 // n.getNodeData().getTaxonomy().setScientificName( name );
415 setTaxonomyCode( n, name );
417 case TAXONOMY_SCIENTIFIC_NAME:
419 if ( !n.getNodeData().isHasTaxonomy() ) {
420 n.getNodeData().setTaxonomy( new Taxonomy() );
422 n.getNodeData().getTaxonomy().setScientificName( name );
424 case TAXONOMY_COMMON_NAME:
426 if ( !n.getNodeData().isHasTaxonomy() ) {
427 n.getNodeData().setTaxonomy( new Taxonomy() );
429 n.getNodeData().getTaxonomy().setCommonName( name );
431 case SEQUENCE_SYMBOL:
433 if ( !n.getNodeData().isHasSequence() ) {
434 n.getNodeData().setSequence( new Sequence() );
436 n.getNodeData().getSequence().setSymbol( name );
440 if ( !n.getNodeData().isHasSequence() ) {
441 n.getNodeData().setSequence( new Sequence() );
443 n.getNodeData().getSequence().setName( name );
445 case TAXONOMY_ID_UNIPROT_1: {
446 if ( !n.getNodeData().isHasTaxonomy() ) {
447 n.getNodeData().setTaxonomy( new Taxonomy() );
450 final int i = name.indexOf( '_' );
452 id = name.substring( 0, i );
457 n.getNodeData().getTaxonomy()
458 .setIdentifier( new Identifier( id, PhyloXmlUtil.UNIPROT_TAX_PROVIDER ) );
461 case TAXONOMY_ID_UNIPROT_2: {
462 if ( !n.getNodeData().isHasTaxonomy() ) {
463 n.getNodeData().setTaxonomy( new Taxonomy() );
466 final int i = name.indexOf( '_' );
468 id = name.substring( i + 1, name.length() );
473 n.getNodeData().getTaxonomy()
474 .setIdentifier( new Identifier( id, PhyloXmlUtil.UNIPROT_TAX_PROVIDER ) );
482 static double addPhylogenyDistances( final double a, final double b ) {
483 if ( ( a >= 0.0 ) && ( b >= 0.0 ) ) {
486 else if ( a >= 0.0 ) {
489 else if ( b >= 0.0 ) {
492 return PhylogenyDataUtil.BRANCH_LENGTH_DEFAULT;
495 // Helper for getUltraParalogousNodes( PhylogenyNode ).
496 public static boolean areAllChildrenDuplications( final PhylogenyNode n ) {
497 if ( n.isExternal() ) {
501 if ( n.isDuplication() ) {
503 for( final PhylogenyNode desc : n.getDescendants() ) {
504 if ( !areAllChildrenDuplications( desc ) ) {
516 public static int calculateDepth( final PhylogenyNode node ) {
517 PhylogenyNode n = node;
519 while ( !n.isRoot() ) {
526 public static double calculateDistanceToRoot( final PhylogenyNode node ) {
527 PhylogenyNode n = node;
529 while ( !n.isRoot() ) {
530 if ( n.getDistanceToParent() > 0.0 ) {
531 d += n.getDistanceToParent();
538 public static short calculateMaxBranchesToLeaf( final PhylogenyNode node ) {
539 if ( node.isExternal() ) {
543 for( PhylogenyNode d : node.getAllExternalDescendants() ) {
545 while ( d != node ) {
546 if ( d.isCollapse() ) {
561 public static int calculateMaxDepth( final Phylogeny phy ) {
563 for( final PhylogenyNodeIterator iter = phy.iteratorExternalForward(); iter.hasNext(); ) {
564 final PhylogenyNode node = iter.next();
565 final int steps = calculateDepth( node );
573 public static double calculateMaxDistanceToRoot( final Phylogeny phy ) {
575 for( final PhylogenyNodeIterator iter = phy.iteratorExternalForward(); iter.hasNext(); ) {
576 final PhylogenyNode node = iter.next();
577 final double d = calculateDistanceToRoot( node );
585 public static DescriptiveStatistics calculatNumberOfDescendantsPerNodeStatistics( final Phylogeny phy ) {
586 final DescriptiveStatistics stats = new BasicDescriptiveStatistics();
587 for( final PhylogenyNodeIterator iter = phy.iteratorPreorder(); iter.hasNext(); ) {
588 final PhylogenyNode n = iter.next();
589 if ( !n.isExternal() ) {
590 stats.addValue( n.getNumberOfDescendants() );
596 public static DescriptiveStatistics calculatConfidenceStatistics( final Phylogeny phy ) {
597 final DescriptiveStatistics stats = new BasicDescriptiveStatistics();
598 for( final PhylogenyNodeIterator iter = phy.iteratorPreorder(); iter.hasNext(); ) {
599 final PhylogenyNode n = iter.next();
600 if ( !n.isExternal() ) {
601 if ( n.getBranchData().isHasConfidences() ) {
602 stats.addValue( n.getBranchData().getConfidence( 0 ).getValue() );
610 * Returns the set of distinct taxonomies of
611 * all external nodes of node.
612 * If at least one the external nodes has no taxonomy,
616 public static Set<Taxonomy> obtainDistinctTaxonomies( final PhylogenyNode node ) {
617 final List<PhylogenyNode> descs = node.getAllExternalDescendants();
618 final Set<Taxonomy> tax_set = new HashSet<Taxonomy>();
619 for( final PhylogenyNode n : descs ) {
620 if ( !n.getNodeData().isHasTaxonomy() || n.getNodeData().getTaxonomy().isEmpty() ) {
623 tax_set.add( n.getNodeData().getTaxonomy() );
629 * Returns a map of distinct taxonomies of
630 * all external nodes of node.
631 * If at least one of the external nodes has no taxonomy,
635 public static SortedMap<Taxonomy, Integer> obtainDistinctTaxonomyCounts( final PhylogenyNode node ) {
636 final List<PhylogenyNode> descs = node.getAllExternalDescendants();
637 final SortedMap<Taxonomy, Integer> tax_map = new TreeMap<Taxonomy, Integer>();
638 for( final PhylogenyNode n : descs ) {
639 if ( !n.getNodeData().isHasTaxonomy() || n.getNodeData().getTaxonomy().isEmpty() ) {
642 final Taxonomy t = n.getNodeData().getTaxonomy();
643 if ( tax_map.containsKey( t ) ) {
644 tax_map.put( t, tax_map.get( t ) + 1 );
653 public static int calculateNumberOfExternalNodesWithoutTaxonomy( final PhylogenyNode node ) {
654 final List<PhylogenyNode> descs = node.getAllExternalDescendants();
656 for( final PhylogenyNode n : descs ) {
657 if ( !n.getNodeData().isHasTaxonomy() || n.getNodeData().getTaxonomy().isEmpty() ) {
665 * Deep copies the phylogeny originating from this node.
667 static PhylogenyNode copySubTree( final PhylogenyNode source ) {
668 if ( source == null ) {
672 final PhylogenyNode newnode = source.copyNodeData();
673 if ( !source.isExternal() ) {
674 for( int i = 0; i < source.getNumberOfDescendants(); ++i ) {
675 newnode.setChildNode( i, PhylogenyMethods.copySubTree( source.getChildNode( i ) ) );
683 * Shallow copies the phylogeny originating from this node.
685 static PhylogenyNode copySubTreeShallow( final PhylogenyNode source ) {
686 if ( source == null ) {
690 final PhylogenyNode newnode = source.copyNodeDataShallow();
691 if ( !source.isExternal() ) {
692 for( int i = 0; i < source.getNumberOfDescendants(); ++i ) {
693 newnode.setChildNode( i, PhylogenyMethods.copySubTreeShallow( source.getChildNode( i ) ) );
700 public static void deleteExternalNodesNegativeSelection( final Set<Integer> to_delete, final Phylogeny phy ) {
702 for( final Integer id : to_delete ) {
703 phy.deleteSubtree( phy.getNode( id ), true );
708 public static void deleteExternalNodesNegativeSelection( final String[] node_names_to_delete, final Phylogeny p )
709 throws IllegalArgumentException {
710 for( int i = 0; i < node_names_to_delete.length; ++i ) {
711 if ( ForesterUtil.isEmpty( node_names_to_delete[ i ] ) ) {
714 List<PhylogenyNode> nodes = null;
715 nodes = p.getNodes( node_names_to_delete[ i ] );
716 final Iterator<PhylogenyNode> it = nodes.iterator();
717 while ( it.hasNext() ) {
718 final PhylogenyNode n = it.next();
719 if ( !n.isExternal() ) {
720 throw new IllegalArgumentException( "attempt to delete non-external node \""
721 + node_names_to_delete[ i ] + "\"" );
723 p.deleteSubtree( n, true );
728 public static void deleteExternalNodesPositiveSelection( final Set<Taxonomy> species_to_keep, final Phylogeny phy ) {
729 // final Set<Integer> to_delete = new HashSet<Integer>();
730 for( final PhylogenyNodeIterator it = phy.iteratorExternalForward(); it.hasNext(); ) {
731 final PhylogenyNode n = it.next();
732 if ( n.getNodeData().isHasTaxonomy() ) {
733 if ( !species_to_keep.contains( n.getNodeData().getTaxonomy() ) ) {
734 //to_delete.add( n.getNodeId() );
735 phy.deleteSubtree( n, true );
739 throw new IllegalArgumentException( "node " + n.getId() + " has no taxonomic data" );
743 phy.externalNodesHaveChanged();
744 // deleteExternalNodesNegativeSelection( to_delete, phy );
747 public static List<String> deleteExternalNodesPositiveSelection( final String[] node_names_to_keep,
748 final Phylogeny p ) {
749 final PhylogenyNodeIterator it = p.iteratorExternalForward();
750 final String[] to_delete = new String[ p.getNumberOfExternalNodes() ];
752 Arrays.sort( node_names_to_keep );
753 while ( it.hasNext() ) {
754 final String curent_name = it.next().getName();
755 if ( Arrays.binarySearch( node_names_to_keep, curent_name ) < 0 ) {
756 to_delete[ i++ ] = curent_name;
759 PhylogenyMethods.deleteExternalNodesNegativeSelection( to_delete, p );
760 final List<String> deleted = new ArrayList<String>();
761 for( final String n : to_delete ) {
762 if ( !ForesterUtil.isEmpty( n ) ) {
769 public static List<PhylogenyNode> getAllDescendants( final PhylogenyNode node ) {
770 final List<PhylogenyNode> descs = new ArrayList<PhylogenyNode>();
771 final Set<Integer> encountered = new HashSet<Integer>();
772 if ( !node.isExternal() ) {
773 final List<PhylogenyNode> exts = node.getAllExternalDescendants();
774 for( PhylogenyNode current : exts ) {
775 descs.add( current );
776 while ( current != node ) {
777 current = current.getParent();
778 if ( encountered.contains( current.getId() ) ) {
781 descs.add( current );
782 encountered.add( current.getId() );
796 public static Color getBranchColorValue( final PhylogenyNode node ) {
797 if ( node.getBranchData().getBranchColor() == null ) {
800 return node.getBranchData().getBranchColor().getValue();
806 public static double getBranchWidthValue( final PhylogenyNode node ) {
807 if ( !node.getBranchData().isHasBranchWidth() ) {
808 return BranchWidth.BRANCH_WIDTH_DEFAULT_VALUE;
810 return node.getBranchData().getBranchWidth().getValue();
816 public static double getConfidenceValue( final PhylogenyNode node ) {
817 if ( !node.getBranchData().isHasConfidences() ) {
818 return Confidence.CONFIDENCE_DEFAULT_VALUE;
820 return node.getBranchData().getConfidence( 0 ).getValue();
826 public static double[] getConfidenceValuesAsArray( final PhylogenyNode node ) {
827 if ( !node.getBranchData().isHasConfidences() ) {
828 return new double[ 0 ];
830 final double[] values = new double[ node.getBranchData().getConfidences().size() ];
832 for( final Confidence c : node.getBranchData().getConfidences() ) {
833 values[ i++ ] = c.getValue();
839 * Calculates the distance between PhylogenyNodes n1 and n2.
840 * PRECONDITION: n1 is a descendant of n2.
845 * @return distance between n1 and n2
847 private static double getDistance( PhylogenyNode n1, final PhylogenyNode n2 ) {
850 if ( n1.getDistanceToParent() > 0.0 ) {
851 d += n1.getDistanceToParent();
859 * Returns taxonomy t if all external descendants have
860 * the same taxonomy t, null otherwise.
863 public static Taxonomy getExternalDescendantsTaxonomy( final PhylogenyNode node ) {
864 final List<PhylogenyNode> descs = node.getAllExternalDescendants();
866 for( final PhylogenyNode n : descs ) {
867 if ( !n.getNodeData().isHasTaxonomy() || n.getNodeData().getTaxonomy().isEmpty() ) {
870 else if ( tax == null ) {
871 tax = n.getNodeData().getTaxonomy();
873 else if ( n.getNodeData().getTaxonomy().isEmpty() || !tax.isEqual( n.getNodeData().getTaxonomy() ) ) {
880 public static PhylogenyNode getFurthestDescendant( final PhylogenyNode node ) {
881 final List<PhylogenyNode> children = node.getAllExternalDescendants();
882 PhylogenyNode farthest = null;
883 double longest = -Double.MAX_VALUE;
884 for( final PhylogenyNode child : children ) {
885 if ( PhylogenyMethods.getDistance( child, node ) > longest ) {
887 longest = PhylogenyMethods.getDistance( child, node );
893 public static PhylogenyMethods getInstance() {
894 if ( PhylogenyMethods._instance == null ) {
895 PhylogenyMethods._instance = new PhylogenyMethods();
897 return PhylogenyMethods._instance;
901 * Returns the largest confidence value found on phy.
903 static public double getMaximumConfidenceValue( final Phylogeny phy ) {
904 double max = -Double.MAX_VALUE;
905 for( final PhylogenyNodeIterator iter = phy.iteratorPreorder(); iter.hasNext(); ) {
906 final double s = PhylogenyMethods.getConfidenceValue( iter.next() );
907 if ( ( s != Confidence.CONFIDENCE_DEFAULT_VALUE ) && ( s > max ) ) {
914 static public int getMinimumDescendentsPerInternalNodes( final Phylogeny phy ) {
915 int min = Integer.MAX_VALUE;
918 for( final PhylogenyNodeIterator it = phy.iteratorPreorder(); it.hasNext(); ) {
920 if ( n.isInternal() ) {
921 d = n.getNumberOfDescendants();
931 * Convenience method for display purposes.
932 * Not intended for algorithms.
934 public static String getSpecies( final PhylogenyNode node ) {
935 if ( !node.getNodeData().isHasTaxonomy() ) {
938 if ( !ForesterUtil.isEmpty( node.getNodeData().getTaxonomy().getTaxonomyCode() ) ) {
939 return node.getNodeData().getTaxonomy().getTaxonomyCode();
941 else if ( !ForesterUtil.isEmpty( node.getNodeData().getTaxonomy().getScientificName() ) ) {
942 return node.getNodeData().getTaxonomy().getScientificName();
945 return node.getNodeData().getTaxonomy().getCommonName();
950 * Returns all Nodes which are connected to external PhylogenyNode n of this
951 * Phylogeny by a path containing only speciation events. We call these
952 * "super orthologs". Nodes are returned as Vector of references to Nodes.
954 * PRECONDITION: This tree must be binary and rooted, and speciation -
955 * duplication need to be assigned for each of its internal Nodes.
957 * Returns null if this Phylogeny is empty or if n is internal.
959 * external PhylogenyNode whose strictly speciation related Nodes
961 * @return Vector of references to all strictly speciation related Nodes of
962 * PhylogenyNode n of this Phylogeny, null if this Phylogeny is
963 * empty or if n is internal
965 public static List<PhylogenyNode> getSuperOrthologousNodes( final PhylogenyNode n ) {
967 PhylogenyNode node = n, deepest = null;
968 final List<PhylogenyNode> v = new ArrayList<PhylogenyNode>();
969 if ( !node.isExternal() ) {
972 while ( !node.isRoot() && !node.getParent().isDuplication() ) {
973 node = node.getParent();
976 deepest.setIndicatorsToZero();
978 if ( !node.isExternal() ) {
979 if ( node.getIndicator() == 0 ) {
980 node.setIndicator( ( byte ) 1 );
981 if ( !node.isDuplication() ) {
982 node = node.getChildNode1();
985 if ( node.getIndicator() == 1 ) {
986 node.setIndicator( ( byte ) 2 );
987 if ( !node.isDuplication() ) {
988 node = node.getChildNode2();
991 if ( ( node != deepest ) && ( node.getIndicator() == 2 ) ) {
992 node = node.getParent();
999 if ( node != deepest ) {
1000 node = node.getParent();
1003 node.setIndicator( ( byte ) 2 );
1006 } while ( ( node != deepest ) || ( deepest.getIndicator() != 2 ) );
1011 * Convenience method for display purposes.
1012 * Not intended for algorithms.
1014 public static String getTaxonomyIdentifier( final PhylogenyNode node ) {
1015 if ( !node.getNodeData().isHasTaxonomy() || ( node.getNodeData().getTaxonomy().getIdentifier() == null ) ) {
1018 return node.getNodeData().getTaxonomy().getIdentifier().getValue();
1022 * Returns all Nodes which are connected to external PhylogenyNode n of this
1023 * Phylogeny by a path containing, and leading to, only duplication events.
1024 * We call these "ultra paralogs". Nodes are returned as Vector of
1025 * references to Nodes.
1027 * PRECONDITION: This tree must be binary and rooted, and speciation -
1028 * duplication need to be assigned for each of its internal Nodes.
1030 * Returns null if this Phylogeny is empty or if n is internal.
1032 * (Last modified: 10/06/01)
1035 * external PhylogenyNode whose ultra paralogs are to be returned
1036 * @return Vector of references to all ultra paralogs of PhylogenyNode n of
1037 * this Phylogeny, null if this Phylogeny is empty or if n is
1040 public static List<PhylogenyNode> getUltraParalogousNodes( final PhylogenyNode n ) {
1042 PhylogenyNode node = n;
1043 if ( !node.isExternal() ) {
1046 while ( !node.isRoot() && node.getParent().isDuplication() && areAllChildrenDuplications( node.getParent() ) ) {
1047 node = node.getParent();
1049 final List<PhylogenyNode> nodes = node.getAllExternalDescendants();
1054 public static String inferCommonPartOfScientificNameOfDescendants( final PhylogenyNode node ) {
1055 final List<PhylogenyNode> descs = node.getDescendants();
1057 for( final PhylogenyNode n : descs ) {
1058 if ( !n.getNodeData().isHasTaxonomy()
1059 || ForesterUtil.isEmpty( n.getNodeData().getTaxonomy().getScientificName() ) ) {
1062 else if ( sn == null ) {
1063 sn = n.getNodeData().getTaxonomy().getScientificName().trim();
1066 String sn_current = n.getNodeData().getTaxonomy().getScientificName().trim();
1067 if ( !sn.equals( sn_current ) ) {
1068 boolean overlap = false;
1069 while ( ( sn.indexOf( ' ' ) >= 0 ) || ( sn_current.indexOf( ' ' ) >= 0 ) ) {
1070 if ( ForesterUtil.countChars( sn, ' ' ) > ForesterUtil.countChars( sn_current, ' ' ) ) {
1071 sn = sn.substring( 0, sn.lastIndexOf( ' ' ) ).trim();
1074 sn_current = sn_current.substring( 0, sn_current.lastIndexOf( ' ' ) ).trim();
1076 if ( sn.equals( sn_current ) ) {
1090 public static boolean isHasExternalDescendant( final PhylogenyNode node ) {
1091 for( int i = 0; i < node.getNumberOfDescendants(); ++i ) {
1092 if ( node.getChildNode( i ).isExternal() ) {
1100 * This is case insensitive.
1103 public synchronized static boolean isTaxonomyHasIdentifierOfGivenProvider( final Taxonomy tax,
1104 final String[] providers ) {
1105 if ( ( tax.getIdentifier() != null ) && !ForesterUtil.isEmpty( tax.getIdentifier().getProvider() ) ) {
1106 final String my_tax_prov = tax.getIdentifier().getProvider();
1107 for( final String provider : providers ) {
1108 if ( provider.equalsIgnoreCase( my_tax_prov ) ) {
1119 private static boolean match( final String s,
1121 final boolean case_sensitive,
1122 final boolean partial ) {
1123 if ( ForesterUtil.isEmpty( s ) || ForesterUtil.isEmpty( query ) ) {
1126 String my_s = s.trim();
1127 String my_query = query.trim();
1128 if ( !case_sensitive ) {
1129 my_s = my_s.toLowerCase();
1130 my_query = my_query.toLowerCase();
1133 return my_s.indexOf( my_query ) >= 0;
1136 return my_s.equals( my_query );
1140 public static void midpointRoot( final Phylogeny phylogeny ) {
1141 if ( phylogeny.getNumberOfExternalNodes() < 2 ) {
1144 final PhylogenyMethods methods = getInstance();
1145 final double farthest_d = methods.calculateFurthestDistance( phylogeny );
1146 final PhylogenyNode f1 = methods.getFarthestNode1();
1147 final PhylogenyNode f2 = methods.getFarthestNode2();
1148 if ( farthest_d <= 0.0 ) {
1151 double x = farthest_d / 2.0;
1152 PhylogenyNode n = f1;
1153 if ( PhylogenyMethods.getDistance( f1, phylogeny.getRoot() ) < PhylogenyMethods.getDistance( f2, phylogeny
1157 while ( ( x > n.getDistanceToParent() ) && !n.isRoot() ) {
1158 x -= ( n.getDistanceToParent() > 0 ? n.getDistanceToParent() : 0 );
1161 phylogeny.reRoot( n, x );
1162 phylogeny.recalculateNumberOfExternalDescendants( true );
1163 final PhylogenyNode a = getFurthestDescendant( phylogeny.getRoot().getChildNode1() );
1164 final PhylogenyNode b = getFurthestDescendant( phylogeny.getRoot().getChildNode2() );
1165 final double da = getDistance( a, phylogeny.getRoot() );
1166 final double db = getDistance( b, phylogeny.getRoot() );
1167 if ( Math.abs( da - db ) > 0.000001 ) {
1168 throw new FailedConditionCheckException( "this should not have happened: midpoint rooting failed: da="
1169 + da + ", db=" + db + ", diff=" + Math.abs( da - db ) );
1173 public static void normalizeBootstrapValues( final Phylogeny phylogeny,
1174 final double max_bootstrap_value,
1175 final double max_normalized_value ) {
1176 for( final PhylogenyNodeIterator iter = phylogeny.iteratorPreorder(); iter.hasNext(); ) {
1177 final PhylogenyNode node = iter.next();
1178 if ( node.isInternal() ) {
1179 final double confidence = getConfidenceValue( node );
1180 if ( confidence != Confidence.CONFIDENCE_DEFAULT_VALUE ) {
1181 if ( confidence >= max_bootstrap_value ) {
1182 setBootstrapConfidence( node, max_normalized_value );
1185 setBootstrapConfidence( node, ( confidence * max_normalized_value ) / max_bootstrap_value );
1192 public static List<PhylogenyNode> obtainAllNodesAsList( final Phylogeny phy ) {
1193 final List<PhylogenyNode> nodes = new ArrayList<PhylogenyNode>();
1194 if ( phy.isEmpty() ) {
1197 for( final PhylogenyNodeIterator iter = phy.iteratorPreorder(); iter.hasNext(); ) {
1198 nodes.add( iter.next() );
1203 public static void postorderBranchColorAveragingExternalNodeBased( final Phylogeny p ) {
1204 for( final PhylogenyNodeIterator iter = p.iteratorPostorder(); iter.hasNext(); ) {
1205 final PhylogenyNode node = iter.next();
1210 if ( node.isInternal() ) {
1211 for( final PhylogenyNodeIterator iterator = node.iterateChildNodesForward(); iterator.hasNext(); ) {
1212 final PhylogenyNode child_node = iterator.next();
1213 final Color child_color = getBranchColorValue( child_node );
1214 if ( child_color != null ) {
1216 red += child_color.getRed();
1217 green += child_color.getGreen();
1218 blue += child_color.getBlue();
1221 setBranchColorValue( node,
1222 new Color( ForesterUtil.roundToInt( red / n ),
1223 ForesterUtil.roundToInt( green / n ),
1224 ForesterUtil.roundToInt( blue / n ) ) );
1229 public static void removeNode( final PhylogenyNode remove_me, final Phylogeny phylogeny ) {
1230 if ( remove_me.isRoot() ) {
1231 throw new IllegalArgumentException( "ill advised attempt to remove root node" );
1233 if ( remove_me.isExternal() ) {
1234 phylogeny.deleteSubtree( remove_me, false );
1237 final PhylogenyNode parent = remove_me.getParent();
1238 final List<PhylogenyNode> descs = remove_me.getDescendants();
1239 parent.removeChildNode( remove_me );
1240 for( final PhylogenyNode desc : descs ) {
1241 parent.addAsChild( desc );
1242 desc.setDistanceToParent( addPhylogenyDistances( remove_me.getDistanceToParent(),
1243 desc.getDistanceToParent() ) );
1245 remove_me.setParent( null );
1246 phylogeny.setIdHash( null );
1247 phylogeny.externalNodesHaveChanged();
1251 public static List<PhylogenyNode> searchData( final String query,
1252 final Phylogeny phy,
1253 final boolean case_sensitive,
1254 final boolean partial ) {
1255 final List<PhylogenyNode> nodes = new ArrayList<PhylogenyNode>();
1256 if ( phy.isEmpty() || ( query == null ) ) {
1259 if ( ForesterUtil.isEmpty( query ) ) {
1262 for( final PhylogenyNodeIterator iter = phy.iteratorPreorder(); iter.hasNext(); ) {
1263 final PhylogenyNode node = iter.next();
1264 boolean match = false;
1265 if ( match( node.getName(), query, case_sensitive, partial ) ) {
1268 else if ( node.getNodeData().isHasTaxonomy()
1269 && match( node.getNodeData().getTaxonomy().getTaxonomyCode(), query, case_sensitive, partial ) ) {
1272 else if ( node.getNodeData().isHasTaxonomy()
1273 && match( node.getNodeData().getTaxonomy().getCommonName(), query, case_sensitive, partial ) ) {
1276 else if ( node.getNodeData().isHasTaxonomy()
1277 && match( node.getNodeData().getTaxonomy().getScientificName(), query, case_sensitive, partial ) ) {
1280 else if ( node.getNodeData().isHasTaxonomy()
1281 && ( node.getNodeData().getTaxonomy().getIdentifier() != null )
1282 && match( node.getNodeData().getTaxonomy().getIdentifier().getValue(),
1288 else if ( node.getNodeData().isHasTaxonomy() && !node.getNodeData().getTaxonomy().getSynonyms().isEmpty() ) {
1289 final List<String> syns = node.getNodeData().getTaxonomy().getSynonyms();
1290 I: for( final String syn : syns ) {
1291 if ( match( syn, query, case_sensitive, partial ) ) {
1297 if ( !match && node.getNodeData().isHasSequence()
1298 && match( node.getNodeData().getSequence().getName(), query, case_sensitive, partial ) ) {
1301 if ( !match && node.getNodeData().isHasSequence()
1302 && match( node.getNodeData().getSequence().getSymbol(), query, case_sensitive, partial ) ) {
1306 && node.getNodeData().isHasSequence()
1307 && ( node.getNodeData().getSequence().getAccession() != null )
1308 && match( node.getNodeData().getSequence().getAccession().getValue(),
1314 if ( !match && node.getNodeData().isHasSequence()
1315 && ( node.getNodeData().getSequence().getDomainArchitecture() != null ) ) {
1316 final DomainArchitecture da = node.getNodeData().getSequence().getDomainArchitecture();
1317 I: for( int i = 0; i < da.getNumberOfDomains(); ++i ) {
1318 if ( match( da.getDomain( i ).getName(), query, case_sensitive, partial ) ) {
1324 if ( !match && ( node.getNodeData().getBinaryCharacters() != null ) ) {
1325 Iterator<String> it = node.getNodeData().getBinaryCharacters().getPresentCharacters().iterator();
1326 I: while ( it.hasNext() ) {
1327 if ( match( it.next(), query, case_sensitive, partial ) ) {
1332 it = node.getNodeData().getBinaryCharacters().getGainedCharacters().iterator();
1333 I: while ( it.hasNext() ) {
1334 if ( match( it.next(), query, case_sensitive, partial ) ) {
1347 public static List<PhylogenyNode> searchDataLogicalAnd( final String[] queries,
1348 final Phylogeny phy,
1349 final boolean case_sensitive,
1350 final boolean partial ) {
1351 final List<PhylogenyNode> nodes = new ArrayList<PhylogenyNode>();
1352 if ( phy.isEmpty() || ( queries == null ) || ( queries.length < 1 ) ) {
1355 for( final PhylogenyNodeIterator iter = phy.iteratorPreorder(); iter.hasNext(); ) {
1356 final PhylogenyNode node = iter.next();
1357 boolean all_matched = true;
1358 for( final String query : queries ) {
1359 boolean match = false;
1360 if ( ForesterUtil.isEmpty( query ) ) {
1363 if ( match( node.getName(), query, case_sensitive, partial ) ) {
1366 else if ( node.getNodeData().isHasTaxonomy()
1367 && match( node.getNodeData().getTaxonomy().getTaxonomyCode(), query, case_sensitive, partial ) ) {
1370 else if ( node.getNodeData().isHasTaxonomy()
1371 && match( node.getNodeData().getTaxonomy().getCommonName(), query, case_sensitive, partial ) ) {
1374 else if ( node.getNodeData().isHasTaxonomy()
1375 && match( node.getNodeData().getTaxonomy().getScientificName(), query, case_sensitive, partial ) ) {
1378 else if ( node.getNodeData().isHasTaxonomy()
1379 && ( node.getNodeData().getTaxonomy().getIdentifier() != null )
1380 && match( node.getNodeData().getTaxonomy().getIdentifier().getValue(),
1386 else if ( node.getNodeData().isHasTaxonomy()
1387 && !node.getNodeData().getTaxonomy().getSynonyms().isEmpty() ) {
1388 final List<String> syns = node.getNodeData().getTaxonomy().getSynonyms();
1389 I: for( final String syn : syns ) {
1390 if ( match( syn, query, case_sensitive, partial ) ) {
1396 if ( !match && node.getNodeData().isHasSequence()
1397 && match( node.getNodeData().getSequence().getName(), query, case_sensitive, partial ) ) {
1400 if ( !match && node.getNodeData().isHasSequence()
1401 && match( node.getNodeData().getSequence().getSymbol(), query, case_sensitive, partial ) ) {
1405 && node.getNodeData().isHasSequence()
1406 && ( node.getNodeData().getSequence().getAccession() != null )
1407 && match( node.getNodeData().getSequence().getAccession().getValue(),
1413 if ( !match && node.getNodeData().isHasSequence()
1414 && ( node.getNodeData().getSequence().getDomainArchitecture() != null ) ) {
1415 final DomainArchitecture da = node.getNodeData().getSequence().getDomainArchitecture();
1416 I: for( int i = 0; i < da.getNumberOfDomains(); ++i ) {
1417 if ( match( da.getDomain( i ).getName(), query, case_sensitive, partial ) ) {
1423 if ( !match && ( node.getNodeData().getBinaryCharacters() != null ) ) {
1424 Iterator<String> it = node.getNodeData().getBinaryCharacters().getPresentCharacters().iterator();
1425 I: while ( it.hasNext() ) {
1426 if ( match( it.next(), query, case_sensitive, partial ) ) {
1431 it = node.getNodeData().getBinaryCharacters().getGainedCharacters().iterator();
1432 I: while ( it.hasNext() ) {
1433 if ( match( it.next(), query, case_sensitive, partial ) ) {
1438 // final String[] bcp_ary = node.getNodeData().getBinaryCharacters()
1439 // .getPresentCharactersAsStringArray();
1440 // I: for( final String bc : bcp_ary ) {
1441 // if ( match( bc, query, case_sensitive, partial ) ) {
1446 // final String[] bcg_ary = node.getNodeData().getBinaryCharacters()
1447 // .getGainedCharactersAsStringArray();
1448 // I: for( final String bc : bcg_ary ) {
1449 // if ( match( bc, query, case_sensitive, partial ) ) {
1456 all_matched = false;
1460 if ( all_matched ) {
1468 * Convenience method.
1469 * Sets value for the first confidence value (created if not present, values overwritten otherwise).
1471 public static void setBootstrapConfidence( final PhylogenyNode node, final double bootstrap_confidence_value ) {
1472 setConfidence( node, bootstrap_confidence_value, "bootstrap" );
1475 public static void setBranchColorValue( final PhylogenyNode node, final Color color ) {
1476 if ( node.getBranchData().getBranchColor() == null ) {
1477 node.getBranchData().setBranchColor( new BranchColor() );
1479 node.getBranchData().getBranchColor().setValue( color );
1483 * Convenience method
1485 public static void setBranchWidthValue( final PhylogenyNode node, final double branch_width_value ) {
1486 node.getBranchData().setBranchWidth( new BranchWidth( branch_width_value ) );
1490 * Convenience method.
1491 * Sets value for the first confidence value (created if not present, values overwritten otherwise).
1493 public static void setConfidence( final PhylogenyNode node, final double confidence_value ) {
1494 setConfidence( node, confidence_value, "" );
1498 * Convenience method.
1499 * Sets value for the first confidence value (created if not present, values overwritten otherwise).
1501 public static void setConfidence( final PhylogenyNode node, final double confidence_value, final String type ) {
1502 Confidence c = null;
1503 if ( node.getBranchData().getNumberOfConfidences() > 0 ) {
1504 c = node.getBranchData().getConfidence( 0 );
1507 c = new Confidence();
1508 node.getBranchData().addConfidence( c );
1511 c.setValue( confidence_value );
1514 public static void setScientificName( final PhylogenyNode node, final String scientific_name ) {
1515 if ( !node.getNodeData().isHasTaxonomy() ) {
1516 node.getNodeData().setTaxonomy( new Taxonomy() );
1518 node.getNodeData().getTaxonomy().setScientificName( scientific_name );
1522 * Convenience method to set the taxonomy code of a phylogeny node.
1526 * @param taxonomy_code
1528 public static void setTaxonomyCode( final PhylogenyNode node, final String taxonomy_code ) {
1529 if ( !node.getNodeData().isHasTaxonomy() ) {
1530 node.getNodeData().setTaxonomy( new Taxonomy() );
1532 node.getNodeData().getTaxonomy().setTaxonomyCode( taxonomy_code );
1536 * Removes from Phylogeny to_be_stripped all external Nodes which are
1537 * associated with a species NOT found in Phylogeny reference.
1540 * a reference Phylogeny
1541 * @param to_be_stripped
1542 * Phylogeny to be stripped
1543 * @return number of external nodes removed from to_be_stripped
1545 public static int taxonomyBasedDeletionOfExternalNodes( final Phylogeny reference, final Phylogeny to_be_stripped ) {
1546 final Set<String> ref_ext_taxo = new HashSet<String>();
1547 final ArrayList<PhylogenyNode> nodes_to_delete = new ArrayList<PhylogenyNode>();
1548 for( final PhylogenyNodeIterator it = reference.iteratorExternalForward(); it.hasNext(); ) {
1549 ref_ext_taxo.add( getSpecies( it.next() ) );
1551 for( final PhylogenyNodeIterator it = to_be_stripped.iteratorExternalForward(); it.hasNext(); ) {
1552 final PhylogenyNode n = it.next();
1553 if ( !ref_ext_taxo.contains( getSpecies( n ) ) ) {
1554 nodes_to_delete.add( n );
1557 for( final PhylogenyNode phylogenyNode : nodes_to_delete ) {
1558 to_be_stripped.deleteSubtree( phylogenyNode, true );
1560 return nodes_to_delete.size();
1564 * Arranges the order of childern for each node of this Phylogeny in such a
1565 * way that either the branch with more children is on top (right) or on
1566 * bottom (left), dependent on the value of boolean order.
1569 * decides in which direction to order
1572 public static void orderAppearance( final PhylogenyNode n,
1573 final boolean order,
1574 final boolean order_ext_alphabetically,
1575 final DESCENDANT_SORT_PRIORITY pri ) {
1576 if ( n.isExternal() ) {
1580 PhylogenyNode temp = null;
1581 if ( ( n.getNumberOfDescendants() == 2 )
1582 && ( n.getChildNode1().getNumberOfExternalNodes() != n.getChildNode2().getNumberOfExternalNodes() )
1583 && ( ( n.getChildNode1().getNumberOfExternalNodes() < n.getChildNode2().getNumberOfExternalNodes() ) == order ) ) {
1584 temp = n.getChildNode1();
1585 n.setChild1( n.getChildNode2() );
1586 n.setChild2( temp );
1588 else if ( order_ext_alphabetically ) {
1589 boolean all_ext = true;
1590 for( final PhylogenyNode i : n.getDescendants() ) {
1591 if ( !i.isExternal() ) {
1597 PhylogenyMethods.sortNodeDescendents( n, pri );
1600 for( int i = 0; i < n.getNumberOfDescendants(); ++i ) {
1601 orderAppearance( n.getChildNode( i ), order, order_ext_alphabetically, pri );
1606 public static enum PhylogenyNodeField {
1609 TAXONOMY_SCIENTIFIC_NAME,
1610 TAXONOMY_COMMON_NAME,
1613 TAXONOMY_ID_UNIPROT_1,
1614 TAXONOMY_ID_UNIPROT_2;
1617 public static enum TAXONOMY_EXTRACTION {
1618 NO, YES, PFAM_STYLE_ONLY;
1621 public static enum DESCENDANT_SORT_PRIORITY {
1622 TAXONOMY, SEQUENCE, NODE_NAME;