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: www.phylosoft.org/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.PhylogenyParser;
44 import org.forester.io.parsers.nexus.NexusPhylogeniesParser;
45 import org.forester.io.parsers.nhx.NHXParser;
46 import org.forester.io.parsers.nhx.NHXParser.TAXONOMY_EXTRACTION;
47 import org.forester.io.parsers.util.ParserUtils;
48 import org.forester.phylogeny.Phylogeny;
49 import org.forester.phylogeny.PhylogenyMethods;
50 import org.forester.phylogeny.PhylogenyNode;
51 import org.forester.phylogeny.data.Taxonomy;
52 import org.forester.phylogeny.factories.ParserBasedPhylogenyFactory;
53 import org.forester.phylogeny.factories.PhylogenyFactory;
54 import org.forester.sdi.GSDI;
55 import org.forester.sdi.GSDIR;
56 import org.forester.sdi.SDIException;
57 import org.forester.sdi.SDIR;
58 import org.forester.sdi.SDIutil;
59 import org.forester.sdi.SDIutil.ALGORITHM;
60 import org.forester.sdi.SDIutil.TaxonomyComparisonBase;
61 import org.forester.util.BasicDescriptiveStatistics;
62 import org.forester.util.ForesterUtil;
64 public final class RIO {
66 public static final int DEFAULT_RANGE = -1;
67 private Phylogeny[] _analyzed_gene_trees;
68 private List<PhylogenyNode> _removed_gene_tree_nodes;
69 private int _ext_nodes;
70 private TaxonomyComparisonBase _gsdir_tax_comp_base;
71 private final StringBuilder _log;
72 private final BasicDescriptiveStatistics _duplications_stats;
73 private final boolean _produce_log;
74 private final boolean _verbose;
75 private final REROOTING _rerooting;
77 private RIO( final Phylogeny[] gene_trees,
78 final Phylogeny species_tree,
79 final ALGORITHM algorithm,
80 final REROOTING rerooting,
81 final String outgroup,
84 final boolean produce_log,
85 final boolean verbose ) throws IOException, SDIException, RIOException {
86 if ( ( last == DEFAULT_RANGE ) && ( first >= 0 ) ) {
87 last = gene_trees.length - 1;
89 else if ( ( first == DEFAULT_RANGE ) && ( last >= 0 ) ) {
92 removeSingleDescendentsNodes( species_tree, verbose );
93 checkPreconditions( gene_trees, species_tree, rerooting, outgroup, first, last );
94 _produce_log = produce_log;
96 _rerooting = rerooting;
98 _log = new StringBuilder();
99 _gsdir_tax_comp_base = null;
100 _analyzed_gene_trees = null;
101 _removed_gene_tree_nodes = null;
102 _duplications_stats = new BasicDescriptiveStatistics();
103 inferOrthologs( gene_trees, species_tree, algorithm, outgroup, first, last );
106 public final Phylogeny[] getAnalyzedGeneTrees() {
107 return _analyzed_gene_trees;
110 public final BasicDescriptiveStatistics getDuplicationsStatistics() {
111 return _duplications_stats;
115 * Returns the numbers of number of ext nodes in gene trees analyzed (after
118 * @return number of ext nodes in gene trees analyzed (after stripping)
120 public final int getExtNodesOfAnalyzedGeneTrees() {
124 public final TaxonomyComparisonBase getGSDIRtaxCompBase() {
125 return _gsdir_tax_comp_base;
128 public final StringBuilder getLog() {
132 public final List<PhylogenyNode> getRemovedGeneTreeNodes() {
133 return _removed_gene_tree_nodes;
136 private final void inferOrthologs( final Phylogeny[] gene_trees,
137 final Phylogeny species_tree,
138 final ALGORITHM algorithm,
139 final String outgroup,
141 final int last ) throws SDIException, RIOException, FileNotFoundException,
143 if ( algorithm == ALGORITHM.SDIR ) {
144 // Removes from species_tree all species not found in gene_tree.
145 PhylogenyMethods.taxonomyBasedDeletionOfExternalNodes( gene_trees[ 0 ], species_tree );
146 if ( species_tree.isEmpty() ) {
147 throw new RIOException( "failed to establish species based mapping between gene and species trees" );
150 final Phylogeny[] my_gene_trees;
151 if ( ( first >= 0 ) && ( last >= first ) && ( last < gene_trees.length ) ) {
152 my_gene_trees = new Phylogeny[ 1 + last - first ];
154 for( int i = first; i <= last; ++i ) {
155 my_gene_trees[ c++ ] = gene_trees[ i ];
159 my_gene_trees = gene_trees;
162 preLog( gene_trees, species_tree, algorithm, outgroup, first, last );
164 if ( _verbose && ( my_gene_trees.length >= 4 ) ) {
165 System.out.println();
167 _analyzed_gene_trees = new Phylogeny[ my_gene_trees.length ];
168 int gene_tree_ext_nodes = 0;
169 for( int i = 0; i < my_gene_trees.length; ++i ) {
170 final Phylogeny gt = my_gene_trees[ i ];
171 if ( _verbose && ( my_gene_trees.length > 4 ) ) {
172 ForesterUtil.updateProgress( ( ( double ) i ) / my_gene_trees.length );
175 gene_tree_ext_nodes = gt.getNumberOfExternalNodes();
177 else if ( gene_tree_ext_nodes != gt.getNumberOfExternalNodes() ) {
178 throw new RIOException( "gene tree #" + ( i + 1 ) + " has a different number of external nodes ("
179 + gt.getNumberOfExternalNodes() + ") than the preceding gene trees (" + gene_tree_ext_nodes
182 if ( algorithm == ALGORITHM.SDIR ) {
183 // Removes from gene_tree all species not found in species_tree.
184 PhylogenyMethods.taxonomyBasedDeletionOfExternalNodes( species_tree, gt );
185 if ( gt.isEmpty() ) {
186 throw new RIOException( "failed to establish species based mapping between gene and species trees" );
189 _analyzed_gene_trees[ i ] = performOrthologInference( gt, species_tree, algorithm, outgroup, i );
192 postLog( species_tree );
194 if ( _verbose && ( my_gene_trees.length > 4 ) ) {
195 System.out.println();
196 System.out.println();
200 private final boolean log() {
204 private final void log( final String s ) {
206 _log.append( ForesterUtil.LINE_SEPARATOR );
209 private final void logRemovedGeneTreeNodes() {
210 log( "Species stripped from gene trees:" );
211 final SortedSet<String> rn = new TreeSet<String>();
212 for( final PhylogenyNode n : getRemovedGeneTreeNodes() ) {
213 final Taxonomy t = n.getNodeData().getTaxonomy();
214 switch ( getGSDIRtaxCompBase() ) {
216 rn.add( t.getTaxonomyCode() );
220 rn.add( t.getIdentifier().toString() );
223 case SCIENTIFIC_NAME: {
224 rn.add( t.getScientificName() );
229 for( final String s : rn ) {
235 private final Phylogeny performOrthologInference( final Phylogeny gene_tree,
236 final Phylogeny species_tree,
237 final ALGORITHM algorithm,
238 final String outgroup,
239 final int i ) throws SDIException, RIOException {
240 final Phylogeny assigned_tree;
241 switch ( algorithm ) {
243 assigned_tree = performOrthologInferenceBySDI( gene_tree, species_tree );
247 assigned_tree = performOrthologInferenceByGSDI( gene_tree, species_tree, outgroup, i );
251 throw new IllegalArgumentException( "illegal algorithm: " + algorithm );
255 _ext_nodes = assigned_tree.getNumberOfExternalNodes();
257 else if ( _ext_nodes != assigned_tree.getNumberOfExternalNodes() ) {
258 throw new RIOException( "after stripping gene tree #" + ( i + 1 )
259 + " has a different number of external nodes (" + assigned_tree.getNumberOfExternalNodes()
260 + ") than the preceding gene trees (" + _ext_nodes + ")" );
262 return assigned_tree;
265 private final Phylogeny performOrthologInferenceByGSDI( final Phylogeny gene_tree,
266 final Phylogeny species_tree,
267 final String outgroup,
268 final int i ) throws SDIException, RIOException {
269 final Phylogeny assigned_tree;
270 if ( _rerooting == REROOTING.BY_ALGORITHM ) {
271 final GSDIR gsdir = new GSDIR( gene_tree, species_tree, true, i == 0 );
272 final List<Phylogeny> assigned_trees = gsdir.getMinDuplicationsSumGeneTrees();
274 _removed_gene_tree_nodes = gsdir.getStrippedExternalGeneTreeNodes();
275 for( final PhylogenyNode r : _removed_gene_tree_nodes ) {
276 if ( !r.getNodeData().isHasTaxonomy() ) {
277 throw new RIOException( "node with no (appropriate) taxonomic information found in gene tree #1: "
282 final List<Integer> shortests = GSDIR.getIndexesOfShortestTree( assigned_trees );
283 assigned_tree = assigned_trees.get( shortests.get( 0 ) );
285 writeStatsToLog( i, gsdir, shortests );
288 _gsdir_tax_comp_base = gsdir.getTaxCompBase();
290 _duplications_stats.addValue( gsdir.getMinDuplicationsSum() );
293 if ( _rerooting == REROOTING.MIDPOINT ) {
294 PhylogenyMethods.midpointRoot( gene_tree );
296 else if ( _rerooting == REROOTING.OUTGROUP ) {
297 final PhylogenyNode n = gene_tree.getNode( outgroup );
298 gene_tree.reRoot( n );
300 final GSDI gsdi = new GSDI( gene_tree, species_tree, true, true, true );
301 _removed_gene_tree_nodes = gsdi.getStrippedExternalGeneTreeNodes();
302 for( final PhylogenyNode r : _removed_gene_tree_nodes ) {
303 if ( !r.getNodeData().isHasTaxonomy() ) {
304 throw new RIOException( "node with no (appropriate) taxonomic information found in gene tree #1: "
308 assigned_tree = gene_tree;
310 _gsdir_tax_comp_base = gsdi.getTaxCompBase();
312 _duplications_stats.addValue( gsdi.getDuplicationsSum() );
314 return assigned_tree;
317 private final Phylogeny performOrthologInferenceBySDI( final Phylogeny gene_tree, final Phylogeny species_tree )
318 throws SDIException {
319 final SDIR sdir = new SDIR();
320 return sdir.infer( gene_tree, species_tree, false, true, true, true, 1 )[ 0 ];
323 private final void postLog( final Phylogeny species_tree ) {
325 if ( getRemovedGeneTreeNodes().size() > 0 ) {
326 logRemovedGeneTreeNodes();
328 log( "Species tree external nodes (after stripping) : " + species_tree.getNumberOfExternalNodes() );
329 log( "Species tree polytomies (after stripping) : "
330 + PhylogenyMethods.countNumberOfPolytomies( species_tree ) );
331 log( "Taxonomy linking based on : " + getGSDIRtaxCompBase() );
332 final java.text.DecimalFormat df = new java.text.DecimalFormat( "0.#" );
333 log( "Gene trees analyzed : " + _duplications_stats.getN() );
334 log( "Mean number of duplications : " + df.format( _duplications_stats.arithmeticMean() )
335 + " (sd: " + df.format( _duplications_stats.sampleStandardDeviation() ) + ")" );
336 if ( _duplications_stats.getN() > 3 ) {
337 log( "Median number of duplications : " + df.format( _duplications_stats.median() ) );
339 log( "Minimum duplications : " + ( int ) _duplications_stats.getMin() );
340 log( "Maximum duplications : " + ( int ) _duplications_stats.getMax() );
343 private final void preLog( final Phylogeny[] gene_trees,
344 final Phylogeny species_tree,
345 final ALGORITHM algorithm,
346 final String outgroup,
349 log( "Number of gene tree (total) : " + gene_trees.length );
350 log( "Algorithm : " + algorithm );
351 log( "Species tree external nodes (prior to stripping): " + species_tree.getNumberOfExternalNodes() );
352 log( "Species tree polytomies (prior to stripping) : "
353 + PhylogenyMethods.countNumberOfPolytomies( species_tree ) );
355 switch ( _rerooting ) {
357 rs = "minimizing duplications";
365 rs = "outgroup: " + outgroup;
373 log( "Re-rooting : " + rs );
374 if ( ( first >= 0 ) || ( last >= 0 ) ) {
375 log( "Gene trees analyzed range : " + first + "-" + last );
377 if ( _rerooting == REROOTING.BY_ALGORITHM ) {
382 private final void writeLogSubHeader() {
383 _log.append( ForesterUtil.LINE_SEPARATOR );
384 _log.append( "Some information about duplication numbers in gene trees:" );
385 _log.append( ForesterUtil.LINE_SEPARATOR );
388 _log.append( "re-rootings with minimal number of duplications" );
390 _log.append( "total root placements" );
392 _log.append( "duplications range" );
394 _log.append( "mininal duplication re-rootings with shortest tree heigth" );
395 _log.append( ForesterUtil.LINE_SEPARATOR );
398 private final void writeStatsToLog( final int i, final GSDIR gsdir, final List<Integer> shortests ) {
399 final BasicDescriptiveStatistics stats = gsdir.getDuplicationsSumStats();
402 _log.append( gsdir.getMinDuplicationsSumGeneTrees().size() );
404 _log.append( stats.getN() );
406 _log.append( ( int ) stats.getMin() );
408 _log.append( ( int ) stats.getMax() );
410 _log.append( shortests.size() );
411 _log.append( ForesterUtil.LINE_SEPARATOR );
414 public final static IntMatrix calculateOrthologTable( final Phylogeny[] analyzed_gene_trees, final boolean sort )
415 throws RIOException {
416 final List<String> labels = new ArrayList<String>();
417 final Set<String> labels_set = new HashSet<String>();
419 for( final PhylogenyNode n : analyzed_gene_trees[ 0 ].getExternalNodes() ) {
420 if ( n.getNodeData().isHasSequence() && !ForesterUtil.isEmpty( n.getNodeData().getSequence().getName() ) ) {
421 label = n.getNodeData().getSequence().getName();
423 else if ( n.getNodeData().isHasSequence()
424 && !ForesterUtil.isEmpty( n.getNodeData().getSequence().getSymbol() ) ) {
425 label = n.getNodeData().getSequence().getSymbol();
427 else if ( !ForesterUtil.isEmpty( n.getName() ) ) {
431 throw new RIOException( "node " + n + " has no appropriate label" );
433 if ( labels_set.contains( label ) ) {
434 throw new RIOException( "label " + label + " is not unique" );
436 labels_set.add( label );
440 Collections.sort( labels );
442 final IntMatrix m = new IntMatrix( labels );
444 for( final Phylogeny gt : analyzed_gene_trees ) {
446 PhylogenyMethods.preOrderReId( gt );
447 final HashMap<String, PhylogenyNode> map = PhylogenyMethods.createNameToExtNodeMap( gt );
448 for( int x = 0; x < m.size(); ++x ) {
449 final String mx = m.getLabel( x );
450 final PhylogenyNode nx = map.get( mx );
452 throw new RIOException( "node \"" + mx + "\" not present in gene tree #" + counter );
456 for( int y = 0; y < m.size(); ++y ) {
457 my = m.getLabel( y );
460 throw new RIOException( "node \"" + my + "\" not present in gene tree #" + counter );
462 if ( !PhylogenyMethods.calculateLCAonTreeWithIdsInPreOrder( nx, ny ).isDuplication() ) {
463 m.inreaseByOne( x, y );
471 public final static RIO executeAnalysis( final File gene_trees_file,
472 final File species_tree_file,
473 final ALGORITHM algorithm,
474 final REROOTING rerooting,
475 final String outgroup,
478 final boolean produce_log,
479 final boolean verbose ) throws IOException, SDIException, RIOException {
480 final Phylogeny[] gene_trees = parseGeneTrees( gene_trees_file );
481 if ( gene_trees.length < 1 ) {
482 throw new RIOException( "\"" + gene_trees_file + "\" is devoid of appropriate gene trees" );
484 final Phylogeny species_tree = SDIutil.parseSpeciesTree( gene_trees[ 0 ],
488 TAXONOMY_EXTRACTION.NO );
489 return new RIO( gene_trees, species_tree, algorithm, rerooting, outgroup, first, last, produce_log, verbose );
492 public final static RIO executeAnalysis( final File gene_trees_file,
493 final Phylogeny species_tree,
494 final ALGORITHM algorithm,
495 final REROOTING rerooting,
496 final String outgroup,
497 final boolean produce_log,
498 final boolean verbose ) throws IOException, SDIException, RIOException {
499 return new RIO( parseGeneTrees( gene_trees_file ),
510 public final static RIO executeAnalysis( final File gene_trees_file,
511 final Phylogeny species_tree,
512 final ALGORITHM algorithm,
513 final REROOTING rerooting,
514 final String outgroup,
517 final boolean produce_log,
518 final boolean verbose ) throws IOException, SDIException, RIOException {
519 return new RIO( parseGeneTrees( gene_trees_file ),
530 public final static RIO executeAnalysis( final Phylogeny[] gene_trees, final Phylogeny species_tree )
531 throws IOException, SDIException, RIOException {
532 return new RIO( gene_trees,
535 REROOTING.BY_ALGORITHM,
543 public final static RIO executeAnalysis( final Phylogeny[] gene_trees,
544 final Phylogeny species_tree,
545 final ALGORITHM algorithm,
546 final REROOTING rerooting,
547 final String outgroup,
548 final boolean produce_log,
549 final boolean verbose ) throws IOException, SDIException, RIOException {
550 return new RIO( gene_trees,
561 public final static RIO executeAnalysis( final Phylogeny[] gene_trees,
562 final Phylogeny species_tree,
563 final ALGORITHM algorithm,
564 final REROOTING rerooting,
565 final String outgroup,
568 final boolean produce_log,
569 final boolean verbose ) throws IOException, SDIException, RIOException {
570 return new RIO( gene_trees, species_tree, algorithm, rerooting, outgroup, first, last, produce_log, verbose );
573 private final static void checkPreconditions( final Phylogeny[] gene_trees,
574 final Phylogeny species_tree,
575 final REROOTING rerooting,
576 final String outgroup,
578 final int last ) throws RIOException {
579 if ( !species_tree.isRooted() ) {
580 throw new RIOException( "species tree is not rooted" );
582 if ( !( ( last == DEFAULT_RANGE ) && ( first == DEFAULT_RANGE ) )
583 && ( ( last < first ) || ( last >= gene_trees.length ) || ( last < 0 ) || ( first < 0 ) ) ) {
584 throw new RIOException( "attempt to set range (0-based) of gene to analyze to: from " + first + " to "
585 + last + " (out of " + gene_trees.length + ")" );
587 if ( ( rerooting == REROOTING.OUTGROUP ) && ForesterUtil.isEmpty( outgroup ) ) {
588 throw new RIOException( "outgroup not set for midpoint rooting" );
590 if ( ( rerooting != REROOTING.OUTGROUP ) && !ForesterUtil.isEmpty( outgroup ) ) {
591 throw new RIOException( "outgroup only used for midpoint rooting" );
593 if ( ( rerooting == REROOTING.MIDPOINT )
594 && ( PhylogenyMethods.calculateMaxDistanceToRoot( gene_trees[ 0 ] ) <= 0 ) ) {
595 throw new RIOException( "attempt to use midpoint rooting on gene trees which seem to have no (positive) branch lengths (cladograms)" );
597 if ( rerooting == REROOTING.OUTGROUP ) {
599 gene_trees[ 0 ].getNode( outgroup );
601 catch ( final IllegalArgumentException e ) {
602 throw new RIOException( "cannot perform re-rooting by outgroup: " + e.getLocalizedMessage() );
607 private final static Phylogeny[] parseGeneTrees( final File gene_trees_file ) throws FileNotFoundException,
609 final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
610 final PhylogenyParser p = ParserUtils.createParserDependingOnFileType( gene_trees_file, true );
611 if ( p instanceof NHXParser ) {
612 final NHXParser nhx = ( NHXParser ) p;
613 nhx.setReplaceUnderscores( false );
614 nhx.setIgnoreQuotes( true );
615 nhx.setTaxonomyExtraction( NHXParser.TAXONOMY_EXTRACTION.YES );
617 else if ( p instanceof NexusPhylogeniesParser ) {
618 final NexusPhylogeniesParser nex = ( NexusPhylogeniesParser ) p;
619 nex.setReplaceUnderscores( false );
620 nex.setIgnoreQuotes( true );
621 nex.setTaxonomyExtraction( TAXONOMY_EXTRACTION.YES );
623 return factory.create( gene_trees_file, p );
626 private final static void removeSingleDescendentsNodes( final Phylogeny species_tree, final boolean verbose ) {
627 final int o = PhylogenyMethods.countNumberOfOneDescendantNodes( species_tree );
630 System.out.println( "warning: species tree has " + o
631 + " internal nodes with only one descendent which are therefore going to be removed" );
633 PhylogenyMethods.deleteInternalNodesWithOnlyOneDescendent( species_tree );
637 public enum REROOTING {
638 NONE, BY_ALGORITHM, MIDPOINT, OUTGROUP;