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
7 // Copyright (C) 2000-2001 Washington University School of Medicine
8 // and Howard Hughes Medical Institute
11 // This library is free software; you can redistribute it and/or
12 // modify it under the terms of the GNU Lesser General Public
13 // License as published by the Free Software Foundation; either
14 // version 2.1 of the License, or (at your option) any later version.
16 // This library is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 // Lesser General Public License for more details.
21 // You should have received a copy of the GNU Lesser General Public
22 // License along with this library; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25 // Contact: phylosoft @ gmail . com
26 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
28 package org.forester.rio;
31 import java.io.FileNotFoundException;
32 import java.io.IOException;
33 import java.util.ArrayList;
34 import java.util.Collections;
35 import java.util.HashMap;
36 import java.util.HashSet;
37 import java.util.List;
39 import java.util.SortedSet;
40 import java.util.TreeSet;
42 import org.forester.datastructures.IntMatrix;
43 import org.forester.io.parsers.IteratingPhylogenyParser;
44 import org.forester.io.parsers.PhylogenyParser;
45 import org.forester.io.parsers.nexus.NexusPhylogeniesParser;
46 import org.forester.io.parsers.nhx.NHXParser;
47 import org.forester.io.parsers.nhx.NHXParser.TAXONOMY_EXTRACTION;
48 import org.forester.io.parsers.util.ParserUtils;
49 import org.forester.phylogeny.Phylogeny;
50 import org.forester.phylogeny.PhylogenyMethods;
51 import org.forester.phylogeny.PhylogenyNode;
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.sdi.GSDI;
56 import org.forester.sdi.GSDIR;
57 import org.forester.sdi.SDIException;
58 import org.forester.sdi.SDIR;
59 import org.forester.sdi.SDIutil;
60 import org.forester.sdi.SDIutil.ALGORITHM;
61 import org.forester.sdi.SDIutil.TaxonomyComparisonBase;
62 import org.forester.util.BasicDescriptiveStatistics;
63 import org.forester.util.ForesterUtil;
65 public final class RIO {
67 public static final int DEFAULT_RANGE = -1;
68 private static final int END_OF_GT = Integer.MAX_VALUE;
69 private static IntMatrix _m;
70 private Phylogeny[] _analyzed_gene_trees;
71 private List<PhylogenyNode> _removed_gene_tree_nodes;
72 private int _ext_nodes;
73 private int _int_nodes;
74 private TaxonomyComparisonBase _gsdir_tax_comp_base;
75 private final StringBuilder _log;
76 private final BasicDescriptiveStatistics _duplications_stats;
77 private final boolean _produce_log;
78 private final boolean _verbose;
79 private final REROOTING _rerooting;
80 private final Phylogeny _species_tree;
81 private Phylogeny _min_dub_gene_tree;
83 private RIO( final IteratingPhylogenyParser p,
84 final Phylogeny species_tree,
85 final ALGORITHM algorithm,
86 final REROOTING rerooting,
87 final String outgroup,
90 final boolean produce_log,
91 final boolean verbose,
92 final boolean transfer_taxonomy ) throws IOException, SDIException, RIOException {
93 if ( ( last == DEFAULT_RANGE ) && ( first >= 0 ) ) {
96 else if ( ( first == DEFAULT_RANGE ) && ( last >= 0 ) ) {
99 removeSingleDescendentsNodes( species_tree, verbose );
101 checkPreconditions( p, species_tree, rerooting, outgroup, first, last );
102 _produce_log = produce_log;
104 _rerooting = rerooting;
107 _log = new StringBuilder();
108 _gsdir_tax_comp_base = null;
109 _analyzed_gene_trees = null;
110 _removed_gene_tree_nodes = null;
111 _duplications_stats = new BasicDescriptiveStatistics();
113 inferOrthologs( p, species_tree, algorithm, outgroup, first, last, transfer_taxonomy );
114 _species_tree = species_tree;
117 private RIO( final Phylogeny[] gene_trees,
118 final Phylogeny species_tree,
119 final ALGORITHM algorithm,
120 final REROOTING rerooting,
121 final String outgroup,
124 final boolean produce_log,
125 final boolean verbose,
126 final boolean transfer_taxonomy ) throws IOException, SDIException, RIOException {
127 if ( ( last == DEFAULT_RANGE ) && ( first >= 0 ) ) {
128 last = gene_trees.length - 1;
130 else if ( ( first == DEFAULT_RANGE ) && ( last >= 0 ) ) {
133 removeSingleDescendentsNodes( species_tree, verbose );
134 checkPreconditions( gene_trees, species_tree, rerooting, outgroup, first, last );
135 _produce_log = produce_log;
137 _rerooting = rerooting;
140 _log = new StringBuilder();
141 _gsdir_tax_comp_base = null;
142 _analyzed_gene_trees = null;
143 _removed_gene_tree_nodes = null;
144 _duplications_stats = new BasicDescriptiveStatistics();
145 inferOrthologs( gene_trees, species_tree, algorithm, outgroup, first, last, transfer_taxonomy );
146 _species_tree = species_tree;
149 public final Phylogeny[] getAnalyzedGeneTrees() {
150 return _analyzed_gene_trees;
153 public final BasicDescriptiveStatistics getDuplicationsStatistics() {
154 return _duplications_stats;
158 * Returns the numbers of number of ext nodes in gene trees analyzed (after
161 * @return number of ext nodes in gene trees analyzed (after stripping)
163 public final int getExtNodesOfAnalyzedGeneTrees() {
167 public final TaxonomyComparisonBase getGSDIRtaxCompBase() {
168 return _gsdir_tax_comp_base;
172 * Returns the numbers of number of int nodes in gene trees analyzed (after
175 * @return number of int nodes in gene trees analyzed (after stripping)
177 public final int getIntNodesOfAnalyzedGeneTrees() {
181 public final StringBuilder getLog() {
185 final public Phylogeny getMinDuplicationsGeneTree() {
186 return _min_dub_gene_tree;
189 public final IntMatrix getOrthologTable() {
193 public final List<PhylogenyNode> getRemovedGeneTreeNodes() {
194 return _removed_gene_tree_nodes;
197 public final Phylogeny getSpeciesTree() {
198 return _species_tree;
201 private final void inferOrthologs( final IteratingPhylogenyParser parser,
202 final Phylogeny species_tree,
203 final ALGORITHM algorithm,
204 final String outgroup,
207 final boolean transfer_taxonomy ) throws SDIException, RIOException,
208 FileNotFoundException, IOException {
209 if ( !parser.hasNext() ) {
210 throw new RIOException( "no gene trees to analyze" );
213 preLog( -1, species_tree, algorithm, outgroup );
216 System.out.println();
218 int gene_tree_ext_nodes = 0;
221 final boolean no_range = ( first < 0 ) || ( last < first );
222 while ( parser.hasNext() ) {
223 final Phylogeny gt = parser.next();
224 if ( no_range || ( ( i >= first ) && ( i <= last ) ) ) {
225 if ( gt.isEmpty() ) {
226 throw new RIOException( "gene tree #" + i + " is empty" );
228 if ( gt.getNumberOfExternalNodes() == 1 ) {
229 throw new RIOException( "gene tree #" + i + " has only one external node" );
232 System.out.print( "\r" + i );
234 if ( counter == 0 ) {
235 if ( algorithm == ALGORITHM.SDIR ) {
236 // Removes from species_tree all species not found in gene_tree.
237 PhylogenyMethods.taxonomyBasedDeletionOfExternalNodes( gt, species_tree );
238 if ( species_tree.isEmpty() ) {
239 throw new RIOException( "failed to establish species based mapping between gene and species trees" );
242 gene_tree_ext_nodes = gt.getNumberOfExternalNodes();
244 else if ( gene_tree_ext_nodes != gt.getNumberOfExternalNodes() ) {
245 throw new RIOException( "gene tree #" + i + " has a different number of external nodes ("
246 + gt.getNumberOfExternalNodes() + ") than the preceding gene tree(s) ("
247 + gene_tree_ext_nodes + ")" );
249 if ( algorithm == ALGORITHM.SDIR ) {
250 // Removes from gene_tree all species not found in species_tree.
251 PhylogenyMethods.taxonomyBasedDeletionOfExternalNodes( species_tree, gt );
252 if ( gt.isEmpty() ) {
253 throw new RIOException( "failed to establish species based mapping between gene and species trees" );
256 final Phylogeny analyzed_gt = performOrthologInference( gt,
262 RIO.calculateOrthologTable( analyzed_gt, true, counter );
268 System.out.print( "\rGene trees analyzed :\t" + counter );
270 if ( ( first >= 0 ) && ( counter == 0 ) && ( i > 0 ) ) {
271 throw new RIOException( "attempt to analyze first gene tree #" + first + " in a set of " + i );
277 postLog( species_tree, first, ( first + counter ) - 1 );
280 System.out.println();
281 System.out.println();
285 private final void inferOrthologs( final Phylogeny[] gene_trees,
286 final Phylogeny species_tree,
287 final ALGORITHM algorithm,
288 final String outgroup,
291 final boolean transfer_taxonomy ) throws SDIException, RIOException,
292 FileNotFoundException, IOException {
293 if ( algorithm == ALGORITHM.SDIR ) {
294 // Removes from species_tree all species not found in gene_tree.
295 PhylogenyMethods.taxonomyBasedDeletionOfExternalNodes( gene_trees[ 0 ], species_tree );
296 if ( species_tree.isEmpty() ) {
297 throw new RIOException( "failed to establish species based mapping between gene and species trees" );
300 final Phylogeny[] my_gene_trees;
301 if ( ( first >= 0 ) && ( last >= first ) && ( last < gene_trees.length ) ) {
302 my_gene_trees = new Phylogeny[ ( 1 + last ) - first ];
304 for( int i = first; i <= last; ++i ) {
305 my_gene_trees[ c++ ] = gene_trees[ i ];
309 my_gene_trees = gene_trees;
312 preLog( gene_trees.length, species_tree, algorithm, outgroup );
314 if ( _verbose && ( my_gene_trees.length >= 4 ) ) {
315 System.out.println();
317 _analyzed_gene_trees = new Phylogeny[ my_gene_trees.length ];
318 int gene_tree_ext_nodes = 0;
319 for( int i = 0; i < my_gene_trees.length; ++i ) {
320 final Phylogeny gt = my_gene_trees[ i ];
321 if ( gt.isEmpty() ) {
322 throw new RIOException( "gene tree #" + i + " is empty" );
324 if ( gt.getNumberOfExternalNodes() == 1 ) {
325 throw new RIOException( "gene tree #" + i + " has only one external node" );
327 if ( _verbose && ( my_gene_trees.length > 4 ) ) {
328 ForesterUtil.updateProgress( ( ( double ) i ) / my_gene_trees.length );
331 gene_tree_ext_nodes = gt.getNumberOfExternalNodes();
333 else if ( gene_tree_ext_nodes != gt.getNumberOfExternalNodes() ) {
334 throw new RIOException( "gene tree #" + i + " has a different number of external nodes ("
335 + gt.getNumberOfExternalNodes() + ") than the preceding gene tree(s) (" + gene_tree_ext_nodes
338 if ( algorithm == ALGORITHM.SDIR ) {
339 // Removes from gene_tree all species not found in species_tree.
340 PhylogenyMethods.taxonomyBasedDeletionOfExternalNodes( species_tree, gt );
341 if ( gt.isEmpty() ) {
342 throw new RIOException( "failed to establish species based mapping between gene and species trees" );
345 _analyzed_gene_trees[ i ] = performOrthologInference( gt,
353 postLog( species_tree, first, last );
355 if ( _verbose && ( my_gene_trees.length > 4 ) ) {
356 System.out.println();
357 System.out.println();
361 private final boolean log() {
365 private final void log( final String s ) {
367 _log.append( ForesterUtil.LINE_SEPARATOR );
370 private final void logRemovedGeneTreeNodes() {
371 final SortedSet<String> rn = new TreeSet<String>();
372 for( final PhylogenyNode n : getRemovedGeneTreeNodes() ) {
373 final Taxonomy t = n.getNodeData().getTaxonomy();
374 switch ( getGSDIRtaxCompBase() ) {
376 rn.add( t.getTaxonomyCode() );
380 rn.add( t.getIdentifier().toString() );
383 case SCIENTIFIC_NAME: {
384 rn.add( t.getScientificName() );
389 final StringBuilder sb = new StringBuilder();
390 for( final String s : rn ) {
394 log( "Species stripped from gene trees :" + sb);
397 private final Phylogeny performOrthologInference( final Phylogeny gene_tree,
398 final Phylogeny species_tree,
399 final ALGORITHM algorithm,
400 final String outgroup,
402 final boolean transfer_taxonomy ) throws SDIException,
404 final Phylogeny assigned_tree;
405 switch ( algorithm ) {
407 assigned_tree = performOrthologInferenceBySDI( gene_tree, species_tree );
411 assigned_tree = performOrthologInferenceByGSDI( gene_tree, species_tree, outgroup, i, transfer_taxonomy );
415 throw new IllegalArgumentException( "illegal algorithm: " + algorithm );
419 _ext_nodes = assigned_tree.getNumberOfExternalNodes();
420 _int_nodes = assigned_tree.getNumberOfInternalNodes();
422 else if ( _ext_nodes != assigned_tree.getNumberOfExternalNodes() ) {
423 throw new RIOException( "after stripping gene tree #" + i + " has a different number of external nodes ("
424 + assigned_tree.getNumberOfExternalNodes() + ") than the preceding gene tree(s) (" + _ext_nodes
427 return assigned_tree;
430 private final Phylogeny performOrthologInferenceByGSDI( final Phylogeny gene_tree,
431 final Phylogeny species_tree,
432 final String outgroup,
434 final boolean transfer_taxonomy ) throws SDIException,
436 final Phylogeny assigned_tree;
438 if ( _rerooting == REROOTING.BY_ALGORITHM ) {
439 final GSDIR gsdir = new GSDIR( gene_tree, species_tree, true, i == 0, transfer_taxonomy );
440 assigned_tree = gsdir.getMinDuplicationsSumGeneTree();
442 _removed_gene_tree_nodes = gsdir.getStrippedExternalGeneTreeNodes();
443 for( final PhylogenyNode r : _removed_gene_tree_nodes ) {
444 if ( !r.getNodeData().isHasTaxonomy() ) {
445 throw new RIOException( "node with no (appropriate) taxonomic information found in gene tree #"
446 + i + ": " + r.toString() );
451 _gsdir_tax_comp_base = gsdir.getTaxCompBase();
453 dups = gsdir.getMinDuplicationsSum();
456 if ( _rerooting == REROOTING.MIDPOINT ) {
457 PhylogenyMethods.midpointRoot( gene_tree );
459 else if ( _rerooting == REROOTING.OUTGROUP ) {
460 final PhylogenyNode n = gene_tree.getNode( outgroup );
461 gene_tree.reRoot( n );
463 final GSDI gsdi = new GSDI( gene_tree, species_tree, true, true, true, transfer_taxonomy );
464 _removed_gene_tree_nodes = gsdi.getStrippedExternalGeneTreeNodes();
465 for( final PhylogenyNode r : _removed_gene_tree_nodes ) {
466 if ( !r.getNodeData().isHasTaxonomy() ) {
467 throw new RIOException( "node with no (appropriate) taxonomic information found in gene tree #" + i
468 + ": " + r.toString() );
471 assigned_tree = gene_tree;
473 _gsdir_tax_comp_base = gsdi.getTaxCompBase();
475 dups = gsdi.getDuplicationsSum();
477 if ( ( i == 0 ) || ( dups < _duplications_stats.getMin() ) ) {
478 _min_dub_gene_tree = assigned_tree;
479 _min_dub_gene_tree.setRerootable( false );
481 _duplications_stats.addValue( dups );
482 return assigned_tree;
485 private final Phylogeny performOrthologInferenceBySDI( final Phylogeny gene_tree, final Phylogeny species_tree )
486 throws SDIException {
487 final SDIR sdir = new SDIR();
488 return sdir.infer( gene_tree, species_tree, false, true, true, true, 1 )[ 0 ];
491 private final void postLog( final Phylogeny species_tree, final int first, final int last ) {
492 final java.text.DecimalFormat df = new java.text.DecimalFormat( "0.##" );
493 final int min = ( int ) getDuplicationsStatistics().getMin();
494 final int max = ( int ) getDuplicationsStatistics().getMax();
495 final int median = ( int ) getDuplicationsStatistics().median();
498 int median_count = 0;
499 for( double d : getDuplicationsStatistics().getData() ) {
500 if ( ( ( int ) d ) == min ) {
503 if ( ( ( int ) d ) == max ) {
506 if ( ( ( int ) d ) == median ) {
510 final double min_count_percentage = ( 100.0 * min_count ) / getDuplicationsStatistics().getN();
511 final double max_count_percentage = ( 100.0 * max_count ) / getDuplicationsStatistics().getN();
512 final double median_count_percentage = ( 100.0 * median_count ) / getDuplicationsStatistics().getN();
515 if ( ( getRemovedGeneTreeNodes() != null ) && ( getRemovedGeneTreeNodes().size() > 0 ) ) {
516 logRemovedGeneTreeNodes();
519 log( "Gene trees analyzed :\t" + getDuplicationsStatistics().getN() );
520 if ( ( first >= 0 ) && ( last >= 0 ) ) {
521 log( "Gene trees analyzed range :\t" + first + "-" + last );
523 log( "Gene tree internal nodes :\t" + getIntNodesOfAnalyzedGeneTrees() );
524 log( "Gene tree external nodes :\t" + getExtNodesOfAnalyzedGeneTrees() );
525 log( "Removed ext gene tree nodes :\t" + getRemovedGeneTreeNodes().size() );
526 log( "Spec tree ext nodes (after strip) :\t" + species_tree.getNumberOfExternalNodes() );
527 log( "Spec tree polytomies (after strip) :\t"
528 + PhylogenyMethods.countNumberOfPolytomies( species_tree ) );
529 log( "Taxonomy linking based on :\t" + getGSDIRtaxCompBase() );
530 log( "Mean number of duplications :\t" + df.format( getDuplicationsStatistics().arithmeticMean() )
531 + "\t" + df.format( ( 100.0 * getDuplicationsStatistics().arithmeticMean() ) / getIntNodesOfAnalyzedGeneTrees() )
532 + "%\t(sd: " + df.format( getDuplicationsStatistics().sampleStandardDeviation() ) + ")" );
533 if ( getDuplicationsStatistics().getN() > 3 ) {
534 log( "Median number of duplications :\t" + df.format( median ) + "\t"
535 + df.format( ( 100.0 * median ) / getIntNodesOfAnalyzedGeneTrees() ) + "%" );
537 log( "Minimum duplications :\t" + min + "\t"
538 + df.format( ( 100.0 * min ) / getIntNodesOfAnalyzedGeneTrees() ) + "%" );
539 log( "Maximum duplications :\t" + ( int ) max + "\t"
540 + df.format( ( 100.0 * max ) / getIntNodesOfAnalyzedGeneTrees() ) + "%" );
541 log( "Gene trees with median duplications :\t" + median_count + "\t"
542 + df.format( median_count_percentage ) + "%" );
543 log( "Gene trees with minimum duplications:\t" + min_count + "\t"
544 + df.format( min_count_percentage ) + "%" );
545 log( "Gene trees with maximum duplications:\t" + max_count + "\t"
546 + df.format( max_count_percentage ) + "%" );
550 private final void preLog( final int gene_trees,
551 final Phylogeny species_tree,
552 final ALGORITHM algorithm,
553 final String outgroup ) {
554 if ( gene_trees > 0 ) {
555 log( "Number of gene trees (total) :\t" + gene_trees );
558 log( "Algorithm :\t" + algorithm );
559 log( "Spec tree ext nodes (prior strip) :\t" + species_tree.getNumberOfExternalNodes() );
560 log( "Spec tree polytomies (prior strip) :\t"
561 + PhylogenyMethods.countNumberOfPolytomies( species_tree ) );
563 switch ( _rerooting ) {
565 rs = "minimizing duplications";
573 rs = "outgroup: " + outgroup;
581 log( "Re-rooting :\t" + rs );
585 public final static IntMatrix calculateOrthologTable( final Phylogeny[] analyzed_gene_trees, final boolean sort )
586 throws RIOException {
587 final List<String> labels = new ArrayList<String>();
588 final Set<String> labels_set = new HashSet<String>();
589 for( final PhylogenyNode n : analyzed_gene_trees[ 0 ].getExternalNodes() ) {
590 final String label = obtainLabel( labels_set, n );
591 labels_set.add( label );
595 Collections.sort( labels );
597 final IntMatrix m = new IntMatrix( labels );
599 for( final Phylogeny gt : analyzed_gene_trees ) {
601 updateCounts( m, counter, gt );
606 public final static RIO executeAnalysis( final File gene_trees_file,
607 final File species_tree_file,
608 final ALGORITHM algorithm,
609 final REROOTING rerooting,
610 final String outgroup,
613 final boolean produce_log,
614 final boolean verbose,
615 final boolean transfer_taxonomy ) throws IOException, SDIException,
617 final Phylogeny[] gene_trees = parseGeneTrees( gene_trees_file );
618 if ( gene_trees.length < 1 ) {
619 throw new RIOException( "\"" + gene_trees_file + "\" is devoid of appropriate gene trees" );
621 final Phylogeny species_tree = SDIutil.parseSpeciesTree( gene_trees[ 0 ],
625 TAXONOMY_EXTRACTION.NO );
626 return new RIO( gene_trees,
638 public final static RIO executeAnalysis( final File gene_trees_file,
639 final Phylogeny species_tree,
640 final ALGORITHM algorithm,
641 final REROOTING rerooting,
642 final String outgroup,
643 final boolean produce_log,
644 final boolean verbose,
645 final boolean transfer_taxonomy ) throws IOException, SDIException,
647 return new RIO( parseGeneTrees( gene_trees_file ),
659 public final static RIO executeAnalysis( final File gene_trees_file,
660 final Phylogeny species_tree,
661 final ALGORITHM algorithm,
662 final REROOTING rerooting,
663 final String outgroup,
666 final boolean produce_log,
667 final boolean verbose,
668 final boolean transfer_taxonomy ) throws IOException, SDIException,
670 return new RIO( parseGeneTrees( gene_trees_file ),
682 public final static RIO executeAnalysis( final IteratingPhylogenyParser p,
683 final File species_tree_file,
684 final ALGORITHM algorithm,
685 final REROOTING rerooting,
686 final String outgroup,
689 final boolean produce_log,
690 final boolean verbose,
691 final boolean transfer_taxonomy ) throws IOException, SDIException,
693 final Phylogeny g0 = p.next();
694 if ( ( g0 == null ) || g0.isEmpty() || ( g0.getNumberOfExternalNodes() < 2 ) ) {
695 throw new RIOException( "input file does not seem to contain any gene trees" );
697 final Phylogeny species_tree = SDIutil.parseSpeciesTree( g0,
701 TAXONOMY_EXTRACTION.NO );
715 public final static RIO executeAnalysis( final IteratingPhylogenyParser p,
716 final Phylogeny species_tree,
717 final ALGORITHM algorithm,
718 final REROOTING rerooting,
719 final String outgroup,
720 final boolean produce_log,
721 final boolean verbose,
722 final boolean transfer_taxonomy ) throws IOException, SDIException,
736 public final static RIO executeAnalysis( final IteratingPhylogenyParser p,
737 final Phylogeny species_tree,
738 final ALGORITHM algorithm,
739 final REROOTING rerooting,
740 final String outgroup,
743 final boolean produce_log,
744 final boolean verbose,
745 final boolean transfer_taxonomy ) throws IOException, SDIException,
759 public final static RIO executeAnalysis( final Phylogeny[] gene_trees, final Phylogeny species_tree )
760 throws IOException, SDIException, RIOException {
761 return new RIO( gene_trees,
764 REROOTING.BY_ALGORITHM,
773 public final static RIO executeAnalysis( final Phylogeny[] gene_trees,
774 final Phylogeny species_tree,
775 final ALGORITHM algorithm,
776 final REROOTING rerooting,
777 final String outgroup,
778 final boolean produce_log,
779 final boolean verbose,
780 final boolean transfer_taxonomy ) throws IOException, SDIException,
782 return new RIO( gene_trees,
794 public final static RIO executeAnalysis( final Phylogeny[] gene_trees,
795 final Phylogeny species_tree,
796 final ALGORITHM algorithm,
797 final REROOTING rerooting,
798 final String outgroup,
801 final boolean produce_log,
802 final boolean verbose,
803 final boolean transfer_taxonomy ) throws IOException, SDIException,
805 return new RIO( gene_trees,
817 private final static void calculateOrthologTable( final Phylogeny g, final boolean sort, final int counter )
818 throws RIOException {
819 if ( counter == 0 ) {
820 final List<String> labels = new ArrayList<String>();
821 final Set<String> labels_set = new HashSet<String>();
822 for( final PhylogenyNode n : g.getExternalNodes() ) {
823 final String label = obtainLabel( labels_set, n );
824 labels_set.add( label );
828 Collections.sort( labels );
830 _m = new IntMatrix( labels );
832 updateCounts( _m, counter, g );
835 private final static void checkPreconditions( final IteratingPhylogenyParser p,
836 final Phylogeny species_tree,
837 final REROOTING rerooting,
838 final String outgroup,
840 final int last ) throws RIOException, IOException {
841 final Phylogeny g0 = p.next();
842 if ( ( g0 == null ) || g0.isEmpty() ) {
843 throw new RIOException( "input file does not seem to contain any gene trees" );
845 if ( g0.getNumberOfExternalNodes() < 2 ) {
846 throw new RIOException( "input file does not seem to contain any useable gene trees" );
848 if ( !species_tree.isRooted() ) {
849 throw new RIOException( "species tree is not rooted" );
851 if ( !( ( last == DEFAULT_RANGE ) && ( first == DEFAULT_RANGE ) )
852 && ( ( last < first ) || ( last < 0 ) || ( first < 0 ) ) ) {
853 throw new RIOException( "attempt to set range (0-based) of gene to analyze to: from " + first + " to "
856 if ( ( rerooting == REROOTING.OUTGROUP ) && ForesterUtil.isEmpty( outgroup ) ) {
857 throw new RIOException( "outgroup not set for midpoint rooting" );
859 if ( ( rerooting != REROOTING.OUTGROUP ) && !ForesterUtil.isEmpty( outgroup ) ) {
860 throw new RIOException( "outgroup only used for midpoint rooting" );
862 if ( ( rerooting == REROOTING.MIDPOINT ) && ( PhylogenyMethods.calculateMaxDistanceToRoot( g0 ) <= 0 ) ) {
863 throw new RIOException( "attempt to use midpoint rooting on gene trees which seem to have no (positive) branch lengths (cladograms)" );
865 if ( rerooting == REROOTING.OUTGROUP ) {
867 g0.getNode( outgroup );
869 catch ( final IllegalArgumentException e ) {
870 throw new RIOException( "cannot perform re-rooting by outgroup: " + e.getLocalizedMessage() );
875 private final static void checkPreconditions( final Phylogeny[] gene_trees,
876 final Phylogeny species_tree,
877 final REROOTING rerooting,
878 final String outgroup,
880 final int last ) throws RIOException {
881 if ( !species_tree.isRooted() ) {
882 throw new RIOException( "species tree is not rooted" );
884 if ( !( ( last == DEFAULT_RANGE ) && ( first == DEFAULT_RANGE ) )
885 && ( ( last < first ) || ( last >= gene_trees.length ) || ( last < 0 ) || ( first < 0 ) ) ) {
886 throw new RIOException( "attempt to set range (0-based) of gene to analyze to: from " + first + " to "
887 + last + " (out of " + gene_trees.length + ")" );
889 if ( ( rerooting == REROOTING.OUTGROUP ) && ForesterUtil.isEmpty( outgroup ) ) {
890 throw new RIOException( "outgroup not set for midpoint rooting" );
892 if ( ( rerooting != REROOTING.OUTGROUP ) && !ForesterUtil.isEmpty( outgroup ) ) {
893 throw new RIOException( "outgroup only used for midpoint rooting" );
895 if ( ( rerooting == REROOTING.MIDPOINT )
896 && ( PhylogenyMethods.calculateMaxDistanceToRoot( gene_trees[ 0 ] ) <= 0 ) ) {
897 throw new RIOException( "attempt to use midpoint rooting on gene trees which seem to have no (positive) branch lengths (cladograms)" );
899 if ( rerooting == REROOTING.OUTGROUP ) {
901 gene_trees[ 0 ].getNode( outgroup );
903 catch ( final IllegalArgumentException e ) {
904 throw new RIOException( "cannot perform re-rooting by outgroup: " + e.getLocalizedMessage() );
909 private final static String obtainLabel( final Set<String> labels_set, final PhylogenyNode n ) throws RIOException {
911 if ( n.getNodeData().isHasSequence() && !ForesterUtil.isEmpty( n.getNodeData().getSequence().getName() ) ) {
912 label = n.getNodeData().getSequence().getName();
914 else if ( n.getNodeData().isHasSequence() && !ForesterUtil.isEmpty( n.getNodeData().getSequence().getSymbol() ) ) {
915 label = n.getNodeData().getSequence().getSymbol();
917 else if ( n.getNodeData().isHasSequence()
918 && !ForesterUtil.isEmpty( n.getNodeData().getSequence().getGeneName() ) ) {
919 label = n.getNodeData().getSequence().getGeneName();
921 else if ( !ForesterUtil.isEmpty( n.getName() ) ) {
925 throw new RIOException( "node " + n + " has no appropriate label" );
927 if ( labels_set.contains( label ) ) {
928 throw new RIOException( "label " + label + " is not unique" );
933 private final static Phylogeny[] parseGeneTrees( final File gene_trees_file ) throws FileNotFoundException,
935 final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
936 final PhylogenyParser p = ParserUtils.createParserDependingOnFileType( gene_trees_file, true );
937 if ( p instanceof NHXParser ) {
938 final NHXParser nhx = ( NHXParser ) p;
939 nhx.setReplaceUnderscores( false );
940 nhx.setIgnoreQuotes( true );
941 nhx.setTaxonomyExtraction( TAXONOMY_EXTRACTION.AGGRESSIVE );
943 else if ( p instanceof NexusPhylogeniesParser ) {
944 final NexusPhylogeniesParser nex = ( NexusPhylogeniesParser ) p;
945 nex.setReplaceUnderscores( false );
946 nex.setIgnoreQuotes( true );
947 nex.setTaxonomyExtraction( TAXONOMY_EXTRACTION.AGGRESSIVE );
949 return factory.create( gene_trees_file, p );
952 private final static void removeSingleDescendentsNodes( final Phylogeny species_tree, final boolean verbose ) {
953 final int o = PhylogenyMethods.countNumberOfOneDescendantNodes( species_tree );
956 System.out.println( "warning: species tree has " + o
957 + " internal nodes with only one descendent which are therefore going to be removed" );
959 PhylogenyMethods.deleteInternalNodesWithOnlyOneDescendent( species_tree );
963 private final static void updateCounts( final IntMatrix m, final int counter, final Phylogeny g )
964 throws RIOException {
965 PhylogenyMethods.preOrderReId( g );
966 final HashMap<String, PhylogenyNode> map = PhylogenyMethods.createNameToExtNodeMap( g );
967 for( int x = 0; x < m.size(); ++x ) {
968 final String mx = m.getLabel( x );
969 final PhylogenyNode nx = map.get( mx );
971 throw new RIOException( "node \"" + mx + "\" not present in gene tree #" + counter );
975 for( int y = 0; y < m.size(); ++y ) {
976 my = m.getLabel( y );
979 throw new RIOException( "node \"" + my + "\" not present in gene tree #" + counter );
981 if ( !PhylogenyMethods.calculateLCAonTreeWithIdsInPreOrder( nx, ny ).isDuplication() ) {
982 m.inreaseByOne( x, y );
988 public enum REROOTING {
989 NONE, BY_ALGORITHM, MIDPOINT, OUTGROUP;