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;
40 import java.util.SortedSet;
41 import java.util.TreeSet;
43 import org.forester.datastructures.IntMatrix;
44 import org.forester.io.parsers.IteratingPhylogenyParser;
45 import org.forester.io.parsers.PhylogenyParser;
46 import org.forester.io.parsers.nexus.NexusPhylogeniesParser;
47 import org.forester.io.parsers.nhx.NHXParser;
48 import org.forester.io.parsers.nhx.NHXParser.TAXONOMY_EXTRACTION;
49 import org.forester.io.parsers.util.ParserUtils;
50 import org.forester.phylogeny.Phylogeny;
51 import org.forester.phylogeny.PhylogenyMethods;
52 import org.forester.phylogeny.PhylogenyNode;
53 import org.forester.phylogeny.data.Taxonomy;
54 import org.forester.phylogeny.factories.ParserBasedPhylogenyFactory;
55 import org.forester.phylogeny.factories.PhylogenyFactory;
56 import org.forester.sdi.GSDI;
57 import org.forester.sdi.GSDIR;
58 import org.forester.sdi.SDIException;
59 import org.forester.sdi.SDIR;
60 import org.forester.sdi.SDIutil;
61 import org.forester.sdi.SDIutil.ALGORITHM;
62 import org.forester.sdi.SDIutil.TaxonomyComparisonBase;
63 import org.forester.util.BasicDescriptiveStatistics;
64 import org.forester.util.ForesterUtil;
66 public final class RIO {
68 public static final int DEFAULT_RANGE = -1;
69 private static final int END_OF_GT = Integer.MAX_VALUE;
70 private static IntMatrix _m;
71 private Phylogeny[] _analyzed_gene_trees;
72 private List<PhylogenyNode> _removed_gene_tree_nodes;
73 private int _ext_nodes;
74 private int _int_nodes;
75 private TaxonomyComparisonBase _gsdir_tax_comp_base;
76 private final StringBuilder _log;
77 private final BasicDescriptiveStatistics _duplications_stats;
78 private final boolean _produce_log;
79 private final boolean _verbose;
80 private final REROOTING _rerooting;
81 private final Phylogeny _species_tree;
82 private Phylogeny _min_dub_gene_tree;
83 private Map<Integer, Phylogeny> _dup_to_tree_map;
85 private RIO( final IteratingPhylogenyParser p,
86 final Phylogeny species_tree,
87 final ALGORITHM algorithm,
88 final REROOTING rerooting,
89 final String outgroup,
92 final boolean produce_log,
93 final boolean verbose,
94 final boolean transfer_taxonomy )
95 throws IOException, SDIException, RIOException {
96 if ( ( last == DEFAULT_RANGE ) && ( first >= 0 ) ) {
99 else if ( ( first == DEFAULT_RANGE ) && ( last >= 0 ) ) {
102 removeSingleDescendentsNodes( species_tree, verbose );
104 checkPreconditions( p, species_tree, rerooting, outgroup, first, last );
105 _produce_log = produce_log;
107 _rerooting = rerooting;
110 _log = new StringBuilder();
111 _gsdir_tax_comp_base = null;
112 _analyzed_gene_trees = null;
113 _removed_gene_tree_nodes = null;
114 _duplications_stats = new BasicDescriptiveStatistics();
116 inferOrthologs( p, species_tree, algorithm, outgroup, first, last, transfer_taxonomy );
117 _species_tree = species_tree;
120 private RIO( final Phylogeny[] gene_trees,
121 final Phylogeny species_tree,
122 final ALGORITHM algorithm,
123 final REROOTING rerooting,
124 final String outgroup,
127 final boolean produce_log,
128 final boolean verbose,
129 final boolean transfer_taxonomy )
130 throws IOException, SDIException, RIOException {
131 if ( ( last == DEFAULT_RANGE ) && ( first >= 0 ) ) {
132 last = gene_trees.length - 1;
134 else if ( ( first == DEFAULT_RANGE ) && ( last >= 0 ) ) {
137 removeSingleDescendentsNodes( species_tree, verbose );
138 checkPreconditions( gene_trees, species_tree, rerooting, outgroup, first, last );
139 _produce_log = produce_log;
141 _rerooting = rerooting;
144 _log = new StringBuilder();
145 _gsdir_tax_comp_base = null;
146 _analyzed_gene_trees = null;
147 _removed_gene_tree_nodes = null;
148 _duplications_stats = new BasicDescriptiveStatistics();
149 inferOrthologs( gene_trees, species_tree, algorithm, outgroup, first, last, transfer_taxonomy );
150 _species_tree = species_tree;
153 public final Phylogeny[] getAnalyzedGeneTrees() {
154 return _analyzed_gene_trees;
157 public final BasicDescriptiveStatistics getDuplicationsStatistics() {
158 return _duplications_stats;
162 * Returns the numbers of number of ext nodes in gene trees analyzed (after
165 * @return number of ext nodes in gene trees analyzed (after stripping)
167 public final int getExtNodesOfAnalyzedGeneTrees() {
171 public final TaxonomyComparisonBase getGSDIRtaxCompBase() {
172 return _gsdir_tax_comp_base;
176 * Returns the numbers of number of int nodes in gene trees analyzed (after
179 * @return number of int nodes in gene trees analyzed (after stripping)
181 public final int getIntNodesOfAnalyzedGeneTrees() {
185 public final StringBuilder getLog() {
189 final public Phylogeny getMinDuplicationsGeneTree() {
190 return _min_dub_gene_tree;
193 public final IntMatrix getOrthologTable() {
197 public final List<PhylogenyNode> getRemovedGeneTreeNodes() {
198 return _removed_gene_tree_nodes;
201 public final Phylogeny getSpeciesTree() {
202 return _species_tree;
205 private final void inferOrthologs( final IteratingPhylogenyParser parser,
206 final Phylogeny species_tree,
207 final ALGORITHM algorithm,
208 final String outgroup,
211 final boolean transfer_taxonomy )
212 throws SDIException, RIOException, FileNotFoundException, IOException {
213 if ( !parser.hasNext() ) {
214 throw new RIOException( "no gene trees to analyze" );
217 preLog( -1, species_tree, algorithm, outgroup );
220 System.out.println();
222 int gene_tree_ext_nodes = 0;
225 final boolean no_range = ( first < 0 ) || ( last < first );
226 while ( parser.hasNext() ) {
227 final Phylogeny gt = parser.next();
228 if ( no_range || ( ( i >= first ) && ( i <= last ) ) ) {
229 if ( gt.isEmpty() ) {
230 throw new RIOException( "gene tree #" + i + " is empty" );
232 if ( gt.getNumberOfExternalNodes() == 1 ) {
233 throw new RIOException( "gene tree #" + i + " has only one external node" );
236 System.out.print( "\r" + i );
238 if ( counter == 0 ) {
239 if ( algorithm == ALGORITHM.SDIR ) {
240 // Removes from species_tree all species not found in gene_tree.
241 PhylogenyMethods.taxonomyBasedDeletionOfExternalNodes( gt, species_tree );
242 if ( species_tree.isEmpty() ) {
243 throw new RIOException( "failed to establish species based mapping between gene and species trees" );
246 gene_tree_ext_nodes = gt.getNumberOfExternalNodes();
248 else if ( gene_tree_ext_nodes != gt.getNumberOfExternalNodes() ) {
249 throw new RIOException( "gene tree #" + i + " has a different number of external nodes ("
250 + gt.getNumberOfExternalNodes() + ") than the preceding gene tree(s) ("
251 + gene_tree_ext_nodes + ")" );
253 if ( algorithm == ALGORITHM.SDIR ) {
254 // Removes from gene_tree all species not found in species_tree.
255 PhylogenyMethods.taxonomyBasedDeletionOfExternalNodes( species_tree, gt );
256 if ( gt.isEmpty() ) {
257 throw new RIOException( "failed to establish species based mapping between gene and species trees" );
260 final Phylogeny analyzed_gt = performOrthologInference( gt,
266 RIO.calculateOrthologTable( analyzed_gt, true, counter );
272 System.out.print( "\rGene trees analyzed :\t" + counter );
274 if ( ( first >= 0 ) && ( counter == 0 ) && ( i > 0 ) ) {
275 throw new RIOException( "attempt to analyze first gene tree #" + first + " in a set of " + i );
281 postLog( species_tree, first, ( first + counter ) - 1 );
284 System.out.println();
285 System.out.println();
289 private final void inferOrthologs( final Phylogeny[] gene_trees,
290 final Phylogeny species_tree,
291 final ALGORITHM algorithm,
292 final String outgroup,
295 final boolean transfer_taxonomy )
296 throws SDIException, RIOException, FileNotFoundException, IOException {
297 if ( algorithm == ALGORITHM.SDIR ) {
298 // Removes from species_tree all species not found in gene_tree.
299 PhylogenyMethods.taxonomyBasedDeletionOfExternalNodes( gene_trees[ 0 ], species_tree );
300 if ( species_tree.isEmpty() ) {
301 throw new RIOException( "failed to establish species based mapping between gene and species trees" );
304 final Phylogeny[] my_gene_trees;
305 if ( ( first >= 0 ) && ( last >= first ) && ( last < gene_trees.length ) ) {
306 my_gene_trees = new Phylogeny[ ( 1 + last ) - first ];
308 for( int i = first; i <= last; ++i ) {
309 my_gene_trees[ c++ ] = gene_trees[ i ];
313 my_gene_trees = gene_trees;
316 preLog( gene_trees.length, species_tree, algorithm, outgroup );
318 if ( _verbose && ( my_gene_trees.length >= 4 ) ) {
319 System.out.println();
321 _analyzed_gene_trees = new Phylogeny[ my_gene_trees.length ];
322 int gene_tree_ext_nodes = 0;
323 for( int i = 0; i < my_gene_trees.length; ++i ) {
324 final Phylogeny gt = my_gene_trees[ i ];
325 if ( gt.isEmpty() ) {
326 throw new RIOException( "gene tree #" + i + " is empty" );
328 if ( gt.getNumberOfExternalNodes() == 1 ) {
329 throw new RIOException( "gene tree #" + i + " has only one external node" );
331 if ( _verbose && ( my_gene_trees.length > 4 ) ) {
332 ForesterUtil.updateProgress( ( ( double ) i ) / my_gene_trees.length );
335 gene_tree_ext_nodes = gt.getNumberOfExternalNodes();
337 else if ( gene_tree_ext_nodes != gt.getNumberOfExternalNodes() ) {
338 throw new RIOException( "gene tree #" + i + " has a different number of external nodes ("
339 + gt.getNumberOfExternalNodes() + ") than the preceding gene tree(s) (" + gene_tree_ext_nodes
342 if ( algorithm == ALGORITHM.SDIR ) {
343 // Removes from gene_tree all species not found in species_tree.
344 PhylogenyMethods.taxonomyBasedDeletionOfExternalNodes( species_tree, gt );
345 if ( gt.isEmpty() ) {
346 throw new RIOException( "failed to establish species based mapping between gene and species trees" );
349 _analyzed_gene_trees[ i ] = performOrthologInference( gt,
357 postLog( species_tree, first, last );
359 if ( _verbose && ( my_gene_trees.length > 4 ) ) {
360 System.out.println();
361 System.out.println();
365 private final boolean log() {
369 private final void log( final String s ) {
371 _log.append( ForesterUtil.LINE_SEPARATOR );
374 private final void logRemovedGeneTreeNodes() {
375 final SortedSet<String> rn = new TreeSet<String>();
376 for( final PhylogenyNode n : getRemovedGeneTreeNodes() ) {
377 final Taxonomy t = n.getNodeData().getTaxonomy();
378 switch ( getGSDIRtaxCompBase() ) {
380 rn.add( t.getTaxonomyCode() );
384 rn.add( t.getIdentifier().toString() );
387 case SCIENTIFIC_NAME: {
388 rn.add( t.getScientificName() );
393 final StringBuilder sb = new StringBuilder();
394 for( final String s : rn ) {
398 log( "Species stripped from gene trees :" + sb );
401 private final Phylogeny performOrthologInference( final Phylogeny gene_tree,
402 final Phylogeny species_tree,
403 final ALGORITHM algorithm,
404 final String outgroup,
406 final boolean transfer_taxonomy )
407 throws SDIException, RIOException {
408 final Phylogeny assigned_tree;
409 switch ( algorithm ) {
411 assigned_tree = performOrthologInferenceBySDI( gene_tree, species_tree );
415 assigned_tree = performOrthologInferenceByGSDI( gene_tree,
423 throw new IllegalArgumentException( "illegal algorithm: " + algorithm );
427 _ext_nodes = assigned_tree.getNumberOfExternalNodes();
428 _int_nodes = assigned_tree.getNumberOfInternalNodes();
430 else if ( _ext_nodes != assigned_tree.getNumberOfExternalNodes() ) {
431 throw new RIOException( "after stripping gene tree #" + i + " has a different number of external nodes ("
432 + assigned_tree.getNumberOfExternalNodes() + ") than the preceding gene tree(s) (" + _ext_nodes
435 return assigned_tree;
438 private final Phylogeny performOrthologInferenceByGSDI( final Phylogeny gene_tree,
439 final Phylogeny species_tree,
440 final String outgroup,
442 final boolean transfer_taxonomy )
443 throws SDIException, RIOException {
444 final Phylogeny assigned_tree;
446 if ( _rerooting == REROOTING.BY_ALGORITHM ) {
447 final GSDIR gsdir = new GSDIR( gene_tree, species_tree, true, i == 0, transfer_taxonomy );
448 assigned_tree = gsdir.getMinDuplicationsSumGeneTree();
450 _removed_gene_tree_nodes = gsdir.getStrippedExternalGeneTreeNodes();
451 for( final PhylogenyNode r : _removed_gene_tree_nodes ) {
452 if ( !r.getNodeData().isHasTaxonomy() ) {
453 throw new RIOException( "node with no (appropriate) taxonomic information found in gene tree #"
454 + i + ": " + r.toString() );
459 _gsdir_tax_comp_base = gsdir.getTaxCompBase();
461 dups = gsdir.getMinDuplicationsSum();
464 if ( _rerooting == REROOTING.MIDPOINT ) {
465 PhylogenyMethods.midpointRoot( gene_tree );
467 else if ( _rerooting == REROOTING.OUTGROUP ) {
468 final PhylogenyNode n = gene_tree.getNode( outgroup );
469 gene_tree.reRoot( n );
471 final GSDI gsdi = new GSDI( gene_tree, species_tree, true, true, true, transfer_taxonomy );
472 _removed_gene_tree_nodes = gsdi.getStrippedExternalGeneTreeNodes();
473 for( final PhylogenyNode r : _removed_gene_tree_nodes ) {
474 if ( !r.getNodeData().isHasTaxonomy() ) {
475 throw new RIOException( "node with no (appropriate) taxonomic information found in gene tree #" + i
476 + ": " + r.toString() );
479 assigned_tree = gene_tree;
481 _gsdir_tax_comp_base = gsdi.getTaxCompBase();
483 dups = gsdi.getDuplicationsSum();
485 assigned_tree.setRerootable( false );
486 double new_dist = -1;
487 if ( ( i == 0 ) || ( dups < _duplications_stats.getMin() ) ) {
488 _min_dub_gene_tree = assigned_tree;
490 else if ( dups == _duplications_stats.getMin() ) {
491 new_dist = PhylogenyMethods.calculateMaxDistanceToRoot( assigned_tree );
492 if ( new_dist < PhylogenyMethods.calculateMaxDistanceToRoot( _min_dub_gene_tree ) ) {
493 _min_dub_gene_tree = assigned_tree;
496 if ( _dup_to_tree_map == null ) {
497 _dup_to_tree_map = new HashMap<Integer, Phylogeny>();
499 if ( !_dup_to_tree_map.containsKey( dups ) ) {
500 _dup_to_tree_map.put( dups, assigned_tree );
503 if ( new_dist == -1 ) {
504 new_dist = PhylogenyMethods.calculateMaxDistanceToRoot( assigned_tree );
506 if ( new_dist < PhylogenyMethods.calculateMaxDistanceToRoot( _dup_to_tree_map.get( dups ) ) ) {
507 _dup_to_tree_map.put( dups, assigned_tree );
510 _duplications_stats.addValue( dups );
511 return assigned_tree;
514 final public Map<Integer, Phylogeny> getDuplicationsToTreeMap() {
515 return _dup_to_tree_map;
518 private final Phylogeny performOrthologInferenceBySDI( final Phylogeny gene_tree, final Phylogeny species_tree )
519 throws SDIException {
520 final SDIR sdir = new SDIR();
521 final Phylogeny r = sdir.infer( gene_tree, species_tree, false, true, true, true, 1 )[ 0 ];
522 r.setRerootable( false );
523 final int dups = sdir.getMinimalDuplications();
524 _duplications_stats.addValue( dups );
528 private final void postLog( final Phylogeny species_tree, final int first, final int last ) {
529 final java.text.DecimalFormat df = new java.text.DecimalFormat( "0.##" );
530 final int min = ( int ) getDuplicationsStatistics().getMin();
531 final int max = ( int ) getDuplicationsStatistics().getMax();
532 final int median = ( int ) getDuplicationsStatistics().median();
535 int median_count = 0;
536 for( double d : getDuplicationsStatistics().getData() ) {
537 if ( ( ( int ) d ) == min ) {
540 if ( ( ( int ) d ) == max ) {
543 if ( ( ( int ) d ) == median ) {
547 final double min_count_percentage = ( 100.0 * min_count ) / getDuplicationsStatistics().getN();
548 final double max_count_percentage = ( 100.0 * max_count ) / getDuplicationsStatistics().getN();
549 final double median_count_percentage = ( 100.0 * median_count ) / getDuplicationsStatistics().getN();
550 if ( ( getRemovedGeneTreeNodes() != null ) && ( getRemovedGeneTreeNodes().size() > 0 ) ) {
551 logRemovedGeneTreeNodes();
553 log( "Gene trees analyzed :\t" + getDuplicationsStatistics().getN() );
554 if ( ( first >= 0 ) && ( last >= 0 ) ) {
555 log( "Gene trees analyzed range :\t" + first + "-" + last );
557 log( "Gene tree internal nodes :\t" + getIntNodesOfAnalyzedGeneTrees() );
558 log( "Gene tree external nodes :\t" + getExtNodesOfAnalyzedGeneTrees() );
559 log( "Removed ext gene tree nodes :\t" + getRemovedGeneTreeNodes().size() );
560 log( "Spec tree ext nodes (after strip) :\t" + species_tree.getNumberOfExternalNodes() );
561 log( "Spec tree polytomies (after strip) :\t" + PhylogenyMethods.countNumberOfPolytomies( species_tree ) );
562 log( "Taxonomy linking based on :\t" + getGSDIRtaxCompBase() );
563 log( "Mean number of duplications :\t" + df.format( getDuplicationsStatistics().arithmeticMean() )
565 + df.format( ( 100.0 * getDuplicationsStatistics().arithmeticMean() )
566 / getIntNodesOfAnalyzedGeneTrees() )
567 + "%\t(sd: " + df.format( getDuplicationsStatistics().sampleStandardDeviation() ) + ")" );
568 if ( getDuplicationsStatistics().getN() > 3 ) {
569 log( "Median number of duplications :\t" + df.format( median ) + "\t"
570 + df.format( ( 100.0 * median ) / getIntNodesOfAnalyzedGeneTrees() ) + "%" );
572 log( "Minimum duplications :\t" + min + "\t"
573 + df.format( ( 100.0 * min ) / getIntNodesOfAnalyzedGeneTrees() ) + "%" );
574 log( "Maximum duplications :\t" + ( int ) max + "\t"
575 + df.format( ( 100.0 * max ) / getIntNodesOfAnalyzedGeneTrees() ) + "%" );
576 log( "Gene trees with median duplications :\t" + median_count + "\t" + df.format( median_count_percentage )
578 log( "Gene trees with minimum duplications:\t" + min_count + "\t" + df.format( min_count_percentage ) + "%" );
579 log( "Gene trees with maximum duplications:\t" + max_count + "\t" + df.format( max_count_percentage ) + "%" );
582 private final void preLog( final int gene_trees,
583 final Phylogeny species_tree,
584 final ALGORITHM algorithm,
585 final String outgroup ) {
586 if ( gene_trees > 0 ) {
587 log( "Number of gene trees (total) :\t" + gene_trees );
589 log( "Algorithm :\t" + algorithm );
590 log( "Spec tree ext nodes (prior strip) :\t" + species_tree.getNumberOfExternalNodes() );
591 log( "Spec tree polytomies (prior strip) :\t" + PhylogenyMethods.countNumberOfPolytomies( species_tree ) );
593 switch ( _rerooting ) {
595 rs = "minimizing duplications";
603 rs = "outgroup: " + outgroup;
611 log( "Re-rooting :\t" + rs );
614 public final static IntMatrix calculateOrthologTable( final Phylogeny[] analyzed_gene_trees, final boolean sort )
615 throws RIOException {
616 final List<String> labels = new ArrayList<String>();
617 final Set<String> labels_set = new HashSet<String>();
618 for( final PhylogenyNode n : analyzed_gene_trees[ 0 ].getExternalNodes() ) {
619 final String label = obtainLabel( labels_set, n );
620 labels_set.add( label );
624 Collections.sort( labels );
626 final IntMatrix m = new IntMatrix( labels );
628 for( final Phylogeny gt : analyzed_gene_trees ) {
630 updateCounts( m, counter, gt );
635 public final static RIO executeAnalysis( final File gene_trees_file,
636 final File species_tree_file,
637 final ALGORITHM algorithm,
638 final REROOTING rerooting,
639 final String outgroup,
642 final boolean produce_log,
643 final boolean verbose,
644 final boolean transfer_taxonomy )
645 throws IOException, SDIException, RIOException {
646 final Phylogeny[] gene_trees = parseGeneTrees( gene_trees_file );
647 if ( gene_trees.length < 1 ) {
648 throw new RIOException( "\"" + gene_trees_file + "\" is devoid of appropriate gene trees" );
650 final Phylogeny species_tree = SDIutil
651 .parseSpeciesTree( gene_trees[ 0 ], species_tree_file, false, true, TAXONOMY_EXTRACTION.NO );
652 return new RIO( gene_trees,
664 public final static RIO executeAnalysis( final File gene_trees_file,
665 final Phylogeny species_tree,
666 final ALGORITHM algorithm,
667 final REROOTING rerooting,
668 final String outgroup,
669 final boolean produce_log,
670 final boolean verbose,
671 final boolean transfer_taxonomy )
672 throws IOException, SDIException, RIOException {
673 return new RIO( parseGeneTrees( gene_trees_file ),
685 public final static RIO executeAnalysis( final File gene_trees_file,
686 final Phylogeny species_tree,
687 final ALGORITHM algorithm,
688 final REROOTING rerooting,
689 final String outgroup,
692 final boolean produce_log,
693 final boolean verbose,
694 final boolean transfer_taxonomy )
695 throws IOException, SDIException, RIOException {
696 return new RIO( parseGeneTrees( gene_trees_file ),
708 public final static RIO executeAnalysis( final IteratingPhylogenyParser p,
709 final File species_tree_file,
710 final ALGORITHM algorithm,
711 final REROOTING rerooting,
712 final String outgroup,
715 final boolean produce_log,
716 final boolean verbose,
717 final boolean transfer_taxonomy )
718 throws IOException, SDIException, RIOException {
719 final Phylogeny g0 = p.next();
720 if ( ( g0 == null ) || g0.isEmpty() || ( g0.getNumberOfExternalNodes() < 2 ) ) {
721 throw new RIOException( "input file does not seem to contain any gene trees" );
723 final Phylogeny species_tree = SDIutil
724 .parseSpeciesTree( g0, species_tree_file, false, true, TAXONOMY_EXTRACTION.NO );
738 public final static RIO executeAnalysis( final IteratingPhylogenyParser p,
739 final Phylogeny species_tree,
740 final ALGORITHM algorithm,
741 final REROOTING rerooting,
742 final String outgroup,
743 final boolean produce_log,
744 final boolean verbose,
745 final boolean transfer_taxonomy )
746 throws IOException, SDIException, RIOException {
759 public final static RIO executeAnalysis( final IteratingPhylogenyParser p,
760 final Phylogeny species_tree,
761 final ALGORITHM algorithm,
762 final REROOTING rerooting,
763 final String outgroup,
766 final boolean produce_log,
767 final boolean verbose,
768 final boolean transfer_taxonomy )
769 throws IOException, SDIException, RIOException {
782 public final static RIO executeAnalysis( final Phylogeny[] gene_trees, final Phylogeny species_tree )
783 throws IOException, SDIException, RIOException {
784 return new RIO( gene_trees,
787 REROOTING.BY_ALGORITHM,
796 public final static RIO executeAnalysis( final Phylogeny[] gene_trees,
797 final Phylogeny species_tree,
798 final ALGORITHM algorithm,
799 final REROOTING rerooting,
800 final String outgroup,
801 final boolean produce_log,
802 final boolean verbose,
803 final boolean transfer_taxonomy )
804 throws IOException, SDIException, RIOException {
805 return new RIO( gene_trees,
817 public final static RIO executeAnalysis( final Phylogeny[] gene_trees,
818 final Phylogeny species_tree,
819 final ALGORITHM algorithm,
820 final REROOTING rerooting,
821 final String outgroup,
824 final boolean produce_log,
825 final boolean verbose,
826 final boolean transfer_taxonomy )
827 throws IOException, SDIException, RIOException {
828 return new RIO( gene_trees,
840 private final static void calculateOrthologTable( final Phylogeny g, final boolean sort, final int counter )
841 throws RIOException {
842 if ( counter == 0 ) {
843 final List<String> labels = new ArrayList<String>();
844 final Set<String> labels_set = new HashSet<String>();
845 for( final PhylogenyNode n : g.getExternalNodes() ) {
846 final String label = obtainLabel( labels_set, n );
847 labels_set.add( label );
851 Collections.sort( labels );
853 _m = new IntMatrix( labels );
855 updateCounts( _m, counter, g );
858 private final static void checkPreconditions( final IteratingPhylogenyParser p,
859 final Phylogeny species_tree,
860 final REROOTING rerooting,
861 final String outgroup,
864 throws RIOException, IOException {
865 final Phylogeny g0 = p.next();
866 if ( ( g0 == null ) || g0.isEmpty() ) {
867 throw new RIOException( "input file does not seem to contain any gene trees" );
869 if ( g0.getNumberOfExternalNodes() < 2 ) {
870 throw new RIOException( "input file does not seem to contain any useable gene trees" );
872 if ( !species_tree.isRooted() ) {
873 throw new RIOException( "species tree is not rooted" );
875 if ( !( ( last == DEFAULT_RANGE ) && ( first == DEFAULT_RANGE ) )
876 && ( ( last < first ) || ( last < 0 ) || ( first < 0 ) ) ) {
877 throw new RIOException( "attempt to set range (0-based) of gene to analyze to: from " + first + " to "
880 if ( ( rerooting == REROOTING.OUTGROUP ) && ForesterUtil.isEmpty( outgroup ) ) {
881 throw new RIOException( "outgroup not set for midpoint rooting" );
883 if ( ( rerooting != REROOTING.OUTGROUP ) && !ForesterUtil.isEmpty( outgroup ) ) {
884 throw new RIOException( "outgroup only used for midpoint rooting" );
886 if ( ( rerooting == REROOTING.MIDPOINT ) && ( PhylogenyMethods.calculateMaxDistanceToRoot( g0 ) <= 0 ) ) {
887 throw new RIOException( "attempt to use midpoint rooting on gene trees which seem to have no (positive) branch lengths (cladograms)" );
889 if ( rerooting == REROOTING.OUTGROUP ) {
891 g0.getNode( outgroup );
893 catch ( final IllegalArgumentException e ) {
894 throw new RIOException( "cannot perform re-rooting by outgroup: " + e.getLocalizedMessage() );
899 private final static void checkPreconditions( final Phylogeny[] gene_trees,
900 final Phylogeny species_tree,
901 final REROOTING rerooting,
902 final String outgroup,
905 throws RIOException {
906 if ( !species_tree.isRooted() ) {
907 throw new RIOException( "species tree is not rooted" );
909 if ( !( ( last == DEFAULT_RANGE ) && ( first == DEFAULT_RANGE ) )
910 && ( ( last < first ) || ( last >= gene_trees.length ) || ( last < 0 ) || ( first < 0 ) ) ) {
911 throw new RIOException( "attempt to set range (0-based) of gene to analyze to: from " + first + " to "
912 + last + " (out of " + gene_trees.length + ")" );
914 if ( ( rerooting == REROOTING.OUTGROUP ) && ForesterUtil.isEmpty( outgroup ) ) {
915 throw new RIOException( "outgroup not set for midpoint rooting" );
917 if ( ( rerooting != REROOTING.OUTGROUP ) && !ForesterUtil.isEmpty( outgroup ) ) {
918 throw new RIOException( "outgroup only used for midpoint rooting" );
920 if ( ( rerooting == REROOTING.MIDPOINT )
921 && ( PhylogenyMethods.calculateMaxDistanceToRoot( gene_trees[ 0 ] ) <= 0 ) ) {
922 throw new RIOException( "attempt to use midpoint rooting on gene trees which seem to have no (positive) branch lengths (cladograms)" );
924 if ( rerooting == REROOTING.OUTGROUP ) {
926 gene_trees[ 0 ].getNode( outgroup );
928 catch ( final IllegalArgumentException e ) {
929 throw new RIOException( "cannot perform re-rooting by outgroup: " + e.getLocalizedMessage() );
934 private final static String obtainLabel( final Set<String> labels_set, final PhylogenyNode n ) throws RIOException {
936 if ( n.getNodeData().isHasSequence() && !ForesterUtil.isEmpty( n.getNodeData().getSequence().getName() ) ) {
937 label = n.getNodeData().getSequence().getName();
939 else if ( n.getNodeData().isHasSequence()
940 && !ForesterUtil.isEmpty( n.getNodeData().getSequence().getSymbol() ) ) {
941 label = n.getNodeData().getSequence().getSymbol();
943 else if ( n.getNodeData().isHasSequence()
944 && !ForesterUtil.isEmpty( n.getNodeData().getSequence().getGeneName() ) ) {
945 label = n.getNodeData().getSequence().getGeneName();
947 else if ( !ForesterUtil.isEmpty( n.getName() ) ) {
951 throw new RIOException( "node " + n + " has no appropriate label" );
953 if ( labels_set.contains( label ) ) {
954 throw new RIOException( "label " + label + " is not unique" );
959 private final static Phylogeny[] parseGeneTrees( final File gene_trees_file )
960 throws FileNotFoundException, IOException {
961 final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
962 final PhylogenyParser p = ParserUtils.createParserDependingOnFileType( gene_trees_file, true );
963 if ( p instanceof NHXParser ) {
964 final NHXParser nhx = ( NHXParser ) p;
965 nhx.setReplaceUnderscores( false );
966 nhx.setIgnoreQuotes( true );
967 nhx.setTaxonomyExtraction( TAXONOMY_EXTRACTION.AGGRESSIVE );
969 else if ( p instanceof NexusPhylogeniesParser ) {
970 final NexusPhylogeniesParser nex = ( NexusPhylogeniesParser ) p;
971 nex.setReplaceUnderscores( false );
972 nex.setIgnoreQuotes( true );
973 nex.setTaxonomyExtraction( TAXONOMY_EXTRACTION.AGGRESSIVE );
975 return factory.create( gene_trees_file, p );
978 private final static void removeSingleDescendentsNodes( final Phylogeny species_tree, final boolean verbose ) {
979 final int o = PhylogenyMethods.countNumberOfOneDescendantNodes( species_tree );
982 System.out.println( "warning: species tree has " + o
983 + " internal nodes with only one descendent which are therefore going to be removed" );
985 PhylogenyMethods.deleteInternalNodesWithOnlyOneDescendent( species_tree );
989 private final static void updateCounts( final IntMatrix m, final int counter, final Phylogeny g )
990 throws RIOException {
991 PhylogenyMethods.preOrderReId( g );
992 final HashMap<String, PhylogenyNode> map = PhylogenyMethods.createNameToExtNodeMap( g );
993 for( int x = 0; x < m.size(); ++x ) {
994 final String mx = m.getLabel( x );
995 final PhylogenyNode nx = map.get( mx );
997 throw new RIOException( "node \"" + mx + "\" not present in gene tree #" + counter );
1001 for( int y = 0; y < m.size(); ++y ) {
1002 my = m.getLabel( y );
1005 throw new RIOException( "node \"" + my + "\" not present in gene tree #" + counter );
1007 if ( !PhylogenyMethods.calculateLCAonTreeWithIdsInPreOrder( nx, ny ).isDuplication() ) {
1008 m.inreaseByOne( x, y );
1014 public enum REROOTING {