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.application;
31 import java.io.IOException;
32 import java.math.RoundingMode;
33 import java.util.ArrayList;
34 import java.util.List;
36 import org.forester.datastructures.IntMatrix;
37 import org.forester.io.parsers.IteratingPhylogenyParser;
38 import org.forester.io.parsers.PhylogenyParser;
39 import org.forester.io.parsers.nexus.NexusPhylogeniesParser;
40 import org.forester.io.parsers.nhx.NHXParser;
41 import org.forester.io.parsers.nhx.NHXParser.TAXONOMY_EXTRACTION;
42 import org.forester.io.parsers.phyloxml.PhyloXmlParser;
43 import org.forester.io.parsers.util.ParserUtils;
44 import org.forester.io.writers.PhylogenyWriter;
45 import org.forester.phylogeny.Phylogeny;
46 import org.forester.rio.RIO;
47 import org.forester.rio.RIO.REROOTING;
48 import org.forester.rio.RIOException;
49 import org.forester.sdi.SDIException;
50 import org.forester.sdi.SDIutil.ALGORITHM;
51 import org.forester.util.BasicDescriptiveStatistics;
52 import org.forester.util.CommandLineArguments;
53 import org.forester.util.EasyWriter;
54 import org.forester.util.ForesterUtil;
58 final static private String PRG_NAME = "rio";
59 final static private String PRG_VERSION = "4.000 beta 11";
60 final static private String PRG_DATE = "170417";
61 final static private String E_MAIL = "phyloxml@gmail.com";
62 final static private String WWW = "https://sites.google.com/site/cmzmasek/home/software/forester";
63 final static private String HELP_OPTION_1 = "help";
64 final static private String HELP_OPTION_2 = "h";
65 final static private String GT_FIRST = "f";
66 final static private String GT_LAST = "l";
67 final static private String REROOTING_OPT = "r";
68 final static private String OUTGROUP = "o";
69 final static private String RETURN_SPECIES_TREE = "s";
70 final static private String RETURN_BEST_GENE_TREE = "g";
71 final static private String USE_SDIR = "b";
72 final static private String TRANSFER_TAXONOMY_OPTION = "t";
74 public static void main( final String[] args ) {
75 ForesterUtil.printProgramInformation( PRG_NAME,
76 "resampled inference of orthologs",
81 ForesterUtil.getForesterLibraryInformation() );
82 CommandLineArguments cla = null;
84 cla = new CommandLineArguments( args );
86 catch ( final Exception e ) {
87 ForesterUtil.fatalError( e.getMessage() );
89 if ( cla.isOptionSet( HELP_OPTION_1 ) || cla.isOptionSet( HELP_OPTION_2 ) || ( args.length == 0 ) ) {
92 if ( ( args.length < 3 ) || ( args.length > 11 ) || ( cla.getNumberOfNames() < 3 ) ) {
94 System.out.println( "error: incorrect number of arguments" );
98 final List<String> allowed_options = new ArrayList<String>();
99 allowed_options.add( GT_FIRST );
100 allowed_options.add( GT_LAST );
101 allowed_options.add( REROOTING_OPT );
102 allowed_options.add( OUTGROUP );
103 allowed_options.add( USE_SDIR );
104 allowed_options.add( RETURN_SPECIES_TREE );
105 allowed_options.add( RETURN_BEST_GENE_TREE );
106 allowed_options.add( TRANSFER_TAXONOMY_OPTION );
107 final String dissallowed_options = cla.validateAllowedOptionsAsString( allowed_options );
108 if ( dissallowed_options.length() > 0 ) {
109 ForesterUtil.fatalError( "unknown option(s): " + dissallowed_options );
111 final File gene_trees_file = cla.getFile( 0 );
112 final File species_tree_file = cla.getFile( 1 );
113 final File orthology_outtable = cla.getFile( 2 );
115 if ( cla.getNumberOfNames() > 3 ) {
116 logfile = cla.getFile( 3 );
117 if ( logfile.exists() ) {
118 ForesterUtil.fatalError( "\"" + logfile + "\" already exists" );
124 boolean sdir = false;
125 if ( cla.isOptionSet( USE_SDIR ) ) {
126 if ( cla.isOptionHasAValue( USE_SDIR ) ) {
127 ForesterUtil.fatalError( "no value allowed for -" + USE_SDIR );
130 if ( logfile != null ) {
131 ForesterUtil.fatalError( "no logfile output for SDIR algorithm" );
134 String outgroup = null;
135 if ( cla.isOptionSet( OUTGROUP ) ) {
136 if ( !cla.isOptionHasAValue( OUTGROUP ) ) {
137 ForesterUtil.fatalError( "no value for -" + OUTGROUP );
140 ForesterUtil.fatalError( "no outgroup option for SDIR algorithm" );
142 outgroup = cla.getOptionValueAsCleanString( OUTGROUP );
144 REROOTING rerooting = REROOTING.BY_ALGORITHM;
145 if ( cla.isOptionSet( REROOTING_OPT ) ) {
146 if ( !cla.isOptionHasAValue( REROOTING_OPT ) ) {
147 ForesterUtil.fatalError( "no value for -" + REROOTING_OPT );
150 ForesterUtil.fatalError( "no re-rooting option for SDIR algorithm" );
152 final String rerooting_str = cla.getOptionValueAsCleanString( REROOTING_OPT ).toLowerCase();
153 if ( rerooting_str.equals( "none" ) ) {
154 rerooting = REROOTING.NONE;
156 else if ( rerooting_str.equals( "midpoint" ) ) {
157 rerooting = REROOTING.MIDPOINT;
159 else if ( rerooting_str.equals( "outgroup" ) ) {
160 rerooting = REROOTING.OUTGROUP;
164 .fatalError( "values for re-rooting are: 'none', 'midpoint', or 'outgroup' (minizming duplications is default)" );
167 if ( ForesterUtil.isEmpty( outgroup ) && ( rerooting == REROOTING.OUTGROUP ) ) {
168 ForesterUtil.fatalError( "selected re-rooting by outgroup, but outgroup not set" );
170 if ( !ForesterUtil.isEmpty( outgroup ) && ( rerooting != REROOTING.OUTGROUP ) ) {
171 ForesterUtil.fatalError( "outgroup set, but selected re-rooting by other approach" );
173 int gt_first = RIO.DEFAULT_RANGE;
174 int gt_last = RIO.DEFAULT_RANGE;
175 if ( cla.isOptionSet( GT_FIRST ) ) {
176 if ( !cla.isOptionHasAValue( GT_FIRST ) ) {
177 ForesterUtil.fatalError( "no value for -" + GT_FIRST );
180 ForesterUtil.fatalError( "no gene tree range option for SDIR algorithm" );
183 gt_first = cla.getOptionValueAsInt( GT_FIRST );
185 catch ( final IOException e ) {
186 ForesterUtil.fatalError( "could not parse integer for -" + GT_FIRST + " option" );
188 if ( gt_first < 0 ) {
189 ForesterUtil.fatalError( "attempt to set index of first tree to analyze to: " + gt_first );
192 if ( cla.isOptionSet( GT_LAST ) ) {
193 if ( !cla.isOptionHasAValue( GT_LAST ) ) {
194 ForesterUtil.fatalError( "no value for -" + GT_LAST );
197 ForesterUtil.fatalError( "no gene tree range option for SDIR algorithm" );
200 gt_last = cla.getOptionValueAsInt( GT_LAST );
202 catch ( final IOException e ) {
203 ForesterUtil.fatalError( "could not parse integer for -" + GT_LAST + " option" );
206 ForesterUtil.fatalError( "attempt to set index of last tree to analyze to: " + gt_last );
209 if ( ( ( gt_last != RIO.DEFAULT_RANGE ) && ( gt_first != RIO.DEFAULT_RANGE ) ) && ( ( gt_last < gt_first ) ) ) {
210 ForesterUtil.fatalError( "attempt to set range (0-based) of gene to analyze to: from " + gt_first + " to "
213 File return_species_tree = null;
214 if ( !sdir && cla.isOptionSet( RETURN_SPECIES_TREE ) ) {
215 if ( !cla.isOptionHasAValue( RETURN_SPECIES_TREE ) ) {
216 ForesterUtil.fatalError( "no value for -" + RETURN_SPECIES_TREE );
218 final String s = cla.getOptionValueAsCleanString( RETURN_SPECIES_TREE );
219 return_species_tree = new File( s );
220 if ( return_species_tree.exists() ) {
221 ForesterUtil.fatalError( "\"" + return_species_tree + "\" already exists" );
224 File return_gene_tree = null;
225 if ( !sdir && cla.isOptionSet( RETURN_BEST_GENE_TREE ) ) {
226 if ( !cla.isOptionHasAValue( RETURN_BEST_GENE_TREE ) ) {
227 ForesterUtil.fatalError( "no value for -" + RETURN_BEST_GENE_TREE );
229 final String s = cla.getOptionValueAsCleanString( RETURN_BEST_GENE_TREE );
230 return_gene_tree = new File( s );
231 if ( return_gene_tree.exists() ) {
232 ForesterUtil.fatalError( "\"" + return_gene_tree + "\" already exists" );
235 boolean transfer_taxonomy = false;
236 if ( !sdir && cla.isOptionSet( TRANSFER_TAXONOMY_OPTION ) ) {
237 if ( return_gene_tree == null ) {
238 ForesterUtil.fatalError( "no point in transferring taxonomy data without returning best gene tree" );
240 transfer_taxonomy = true;
242 ForesterUtil.fatalErrorIfFileNotReadable( gene_trees_file );
243 ForesterUtil.fatalErrorIfFileNotReadable( species_tree_file );
244 if ( orthology_outtable.exists() ) {
245 ForesterUtil.fatalError( "\"" + orthology_outtable + "\" already exists" );
249 System.out.println( "Gene trees :\t" + gene_trees_file.getCanonicalPath() );
250 System.out.println( "Species tree :\t" + species_tree_file.getCanonicalPath() );
252 catch ( final IOException e ) {
253 ForesterUtil.fatalError( e.getLocalizedMessage() );
255 System.out.println( "All vs all orthology results table :\t" + orthology_outtable );
256 if ( logfile != null ) {
257 System.out.println( "Logfile :\t" + logfile );
259 if ( gt_first != RIO.DEFAULT_RANGE ) {
260 System.out.println( "First gene tree to analyze :\t" + gt_first );
262 if ( gt_last != RIO.DEFAULT_RANGE ) {
263 System.out.println( "Last gene tree to analyze :\t" + gt_last );
265 String rerooting_str = "";
266 switch ( rerooting ) {
268 rerooting_str = "by minimizing duplications";
272 rerooting_str = "by midpoint method";
276 rerooting_str = "by outgroup: " + outgroup;
280 rerooting_str = "none";
284 System.out.println( "Re-rooting : \t" + rerooting_str );
286 System.out.println( "Non binary species tree :\tallowed" );
289 System.out.println( "Non binary species tree :\tdisallowed" );
291 if ( return_species_tree != null ) {
292 System.out.println( "Write used species tree to :\t" + return_species_tree );
294 if ( return_gene_tree != null ) {
295 System.out.println( "Write best gene tree to :\t" + return_gene_tree );
296 System.out.println( "Transfer taxonomic data :\t" + transfer_taxonomy );
298 time = System.currentTimeMillis();
299 final ALGORITHM algorithm;
301 algorithm = ALGORITHM.SDIR;
304 algorithm = ALGORITHM.GSDIR;
308 boolean iterating = false;
309 final PhylogenyParser p = ParserUtils.createParserDependingOnFileType( gene_trees_file, true );
310 if ( p instanceof PhyloXmlParser ) {
311 rio = RIO.executeAnalysis( gene_trees_file,
324 if ( p instanceof NHXParser ) {
325 final NHXParser nhx = ( NHXParser ) p;
326 nhx.setReplaceUnderscores( false );
327 nhx.setIgnoreQuotes( true );
328 nhx.setTaxonomyExtraction( TAXONOMY_EXTRACTION.AGGRESSIVE );
330 else if ( p instanceof NexusPhylogeniesParser ) {
331 final NexusPhylogeniesParser nex = ( NexusPhylogeniesParser ) p;
332 nex.setReplaceUnderscores( false );
333 nex.setIgnoreQuotes( true );
334 nex.setTaxonomyExtraction( TAXONOMY_EXTRACTION.AGGRESSIVE );
337 throw new RuntimeException( "unknown parser type: " + p );
339 final IteratingPhylogenyParser ip = ( IteratingPhylogenyParser ) p;
340 ip.setSource( gene_trees_file );
341 rio = RIO.executeAnalysis( ip,
352 if ( algorithm == ALGORITHM.GSDIR ) {
353 System.out.println( "Taxonomy linking based on :\t" + rio.getGSDIRtaxCompBase() );
357 m = rio.getOrthologTable();
360 m = RIO.calculateOrthologTable( rio.getAnalyzedGeneTrees(), true );
362 final BasicDescriptiveStatistics stats = rio.getDuplicationsStatistics();
363 writeTable( orthology_outtable, stats.getN(), m );
364 if ( ( algorithm != ALGORITHM.SDIR ) && ( logfile != null ) ) {
365 writeLogFile( logfile,
373 ForesterUtil.getForesterLibraryInformation() );
375 if ( return_species_tree != null ) {
376 writeTree( rio.getSpeciesTree(), return_species_tree, "Wrote (stripped) species tree to :\t" );
378 if ( return_gene_tree != null ) {
379 writeTree( rio.getMinDuplicationsGeneTree(),
381 "Wrote one min duplication gene tree :\t" );
383 final java.text.DecimalFormat df = new java.text.DecimalFormat( "0.#" );
384 final int min = ( int ) stats.getMin();
385 final int max = ( int ) stats.getMax();
386 final int median = ( int ) stats.median();
389 int median_count = 0;
390 for( double d : stats.getData() ) {
391 if ( ( ( int ) d ) == min ) {
394 if ( ( ( int ) d ) == max ) {
397 if ( ( ( int ) d ) == median ) {
401 final double min_count_percentage = ( 100.0 * min_count ) / stats.getN();
402 final double max_count_percentage = ( 100.0 * max_count ) / stats.getN();
403 final double median_count_percentage = ( 100.0 * median_count ) / stats.getN();
404 System.out.println( "Gene tree internal nodes :\t" + rio.getIntNodesOfAnalyzedGeneTrees() );
405 System.out.println( "Gene tree external nodes :\t" + rio.getExtNodesOfAnalyzedGeneTrees() );
406 System.out.println( "Mean number of duplications :\t" + df.format( stats.arithmeticMean() ) + "\t"
407 + df.format( ( 100.0 * stats.arithmeticMean() ) / rio.getIntNodesOfAnalyzedGeneTrees() )
408 + "%\t(sd: " + df.format( stats.sampleStandardDeviation() ) + ")" );
409 if ( stats.getN() > 3 ) {
410 System.out.println( "Median number of duplications :\t" + df.format( median ) + "\t"
411 + df.format( ( 100.0 * median ) / rio.getIntNodesOfAnalyzedGeneTrees() ) + "%" );
413 System.out.println( "Minimum duplications :\t" + min + "\t"
414 + df.format( ( 100.0 * min ) / rio.getIntNodesOfAnalyzedGeneTrees() ) + "%" );
415 System.out.println( "Maximum duplications :\t" + ( int ) max + "\t"
416 + df.format( ( 100.0 * max ) / rio.getIntNodesOfAnalyzedGeneTrees() ) + "%" );
417 System.out.println( "Gene trees with median duplications :\t" + median_count + "\t"
418 + df.format( median_count_percentage ) + "%" );
419 System.out.println( "Gene trees with minimum duplications:\t" + min_count + "\t"
420 + df.format( min_count_percentage ) + "%" );
421 System.out.println( "Gene trees with maximum duplications:\t" + max_count + "\t"
422 + df.format( max_count_percentage ) + "%" );
424 catch ( final RIOException e ) {
425 ForesterUtil.fatalError( e.getLocalizedMessage() );
427 catch ( final SDIException e ) {
428 ForesterUtil.fatalError( e.getLocalizedMessage() );
430 catch ( final IOException e ) {
431 ForesterUtil.fatalError( e.getLocalizedMessage() );
433 catch ( final OutOfMemoryError e ) {
434 ForesterUtil.outOfMemoryError( e );
436 catch ( final Exception e ) {
437 ForesterUtil.unexpectedFatalError( e );
439 catch ( final Error e ) {
440 ForesterUtil.unexpectedFatalError( e );
442 time = System.currentTimeMillis() - time;
443 System.out.println( "Time :\t" + time + "ms" );
447 private final static void printHelp() {
448 System.out.println( "Usage" );
449 System.out.println();
450 System.out.println( PRG_NAME
451 + " [options] <gene trees infile> <species tree infile> <all vs all orthology table outfile> [logfile]" );
452 System.out.println();
453 System.out.println( " Options" );
454 System.out.println( " -" + GT_FIRST + "=<first> : first gene tree to analyze (0-based index)" );
455 System.out.println( " -" + GT_LAST + "=<last> : last gene tree to analyze (0-based index)" );
456 System.out.println( " -" + REROOTING_OPT
457 + "=<re-rooting>: re-rooting method for gene trees, possible values or 'none', 'midpoint'," );
458 System.out.println( " or 'outgroup' (default: by minizming duplications)" );
459 System.out.println( " -" + OUTGROUP
460 + "=<outgroup> : for rooting by outgroup, name of outgroup (external gene tree node)" );
462 .println( " -" + RETURN_SPECIES_TREE + "=<outfile> : to write the (stripped) species tree to file" );
463 System.out.println( " -" + RETURN_BEST_GENE_TREE
464 + "=<outfile> : to write (one) minimal duplication gene tree to file" );
465 System.out.println( " -" + TRANSFER_TAXONOMY_OPTION
466 + " : to transfer taxonomic data from species tree to returned minimal duplication gene tree\n"
467 + " (if -" + RETURN_BEST_GENE_TREE + " option is used)" );
468 System.out.println( " -" + USE_SDIR
469 + " : to use SDIR instead of GSDIR (faster, but non-binary species trees are" );
470 System.out.println( " disallowed, as are most options)" );
471 System.out.println();
472 System.out.println( " Formats" );
474 .println( " The gene trees, as well as the species tree, ideally are in phyloXML (www.phyloxml.org) format," );
476 .println( " but can also be in New Hamphshire (Newick) or Nexus format as long as species information can be" );
478 .println( " extracted from the gene names (e.g. \"HUMAN\" from \"BCL2_HUMAN\") and matched to a single species" );
479 System.out.println( " in the species tree." );
480 System.out.println();
481 System.out.println( " Examples" );
482 System.out.println( " rio gene_trees.nh species.xml outtable.tsv log.txt" );
484 .println( " rio -t -f=10 -l=100 -r=none -g=out_gene_tree.xml -s=stripped_species.xml gene_trees.xml species.xml outtable.tsv log.txt" );
485 System.out.println();
489 private static void writeLogFile( final File logfile,
491 final File species_tree_file,
492 final File gene_trees_file,
494 final String prg_name,
496 final String prg_date,
499 final EasyWriter out = ForesterUtil.createEasyWriter( logfile );
500 out.println( prg_name );
501 out.println( "version : " + prg_v );
502 out.println( "date : " + prg_date );
503 out.println( "based on: " + f );
504 out.println( "----------------------------------" );
505 out.println( "Gene trees : " + gene_trees_file.getCanonicalPath() );
506 out.println( "Species tree : " + species_tree_file.getCanonicalPath() );
507 out.println( "All vs all orthology table : " + outtable.getCanonicalPath() );
509 out.println( rio.getLog().toString() );
511 System.out.println( "Wrote log to :\t" + logfile.getCanonicalPath() );
514 private static void writeTable( final File table_outfile, final int gene_trees_analyzed, final IntMatrix m )
516 final EasyWriter w = ForesterUtil.createEasyWriter( table_outfile );
517 final java.text.DecimalFormat df = new java.text.DecimalFormat( "0.####" );
518 df.setDecimalSeparatorAlwaysShown( false );
519 df.setRoundingMode( RoundingMode.HALF_UP );
520 for( int i = 0; i < m.size(); ++i ) {
522 w.print( m.getLabel( i ) );
525 for( int x = 0; x < m.size(); ++x ) {
526 w.print( m.getLabel( x ) );
527 for( int y = 0; y < m.size(); ++y ) {
530 if ( m.get( x, y ) != gene_trees_analyzed ) {
531 ForesterUtil.unexpectedFatalError( "diagonal value is off" );
536 w.print( df.format( ( ( double ) m.get( x, y ) ) / gene_trees_analyzed ) );
542 System.out.println( "Wrote table to :\t" + table_outfile.getCanonicalPath() );
545 private static void writeTree( final Phylogeny p, final File f, final String comment ) throws IOException {
546 final PhylogenyWriter writer = new PhylogenyWriter();
547 writer.toPhyloXML( f, p, 0 );
548 System.out.println( comment + f.getCanonicalPath() );