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.util.ArrayList;
33 import java.util.List;
35 import org.forester.datastructures.IntMatrix;
36 import org.forester.io.parsers.nhx.NHXParser;
37 import org.forester.rio.RIO;
38 import org.forester.rio.RIO.REROOTING;
39 import org.forester.rio.RIOException;
40 import org.forester.sdi.SDIException;
41 import org.forester.sdi.SDIutil.ALGORITHM;
42 import org.forester.util.BasicDescriptiveStatistics;
43 import org.forester.util.CommandLineArguments;
44 import org.forester.util.EasyWriter;
45 import org.forester.util.ForesterUtil;
49 final static private String PRG_NAME = "rio";
50 final static private String PRG_VERSION = "4.000 beta 4";
51 final static private String PRG_DATE = "2012.12.25";
52 final static private String E_MAIL = "czmasek@burnham.org";
53 final static private String WWW = "www.phylosoft.org/forester/";
54 final static private String HELP_OPTION_1 = "help";
55 final static private String HELP_OPTION_2 = "h";
56 final static private String GT_FIRST = "f";
57 final static private String GT_LAST = "l";
58 final static private String REROOTING_OPT = "r";
59 final static private String OUTGROUP = "o";
60 final static private String USE_SDIR = "b";
61 private static final boolean ITERATING = true;
63 public static void main( final String[] args ) {
64 ForesterUtil.printProgramInformation( PRG_NAME,
65 "resampled inference of orthologs",
70 ForesterUtil.getForesterLibraryInformation() );
71 CommandLineArguments cla = null;
73 cla = new CommandLineArguments( args );
75 catch ( final Exception e ) {
76 ForesterUtil.fatalError( e.getMessage() );
78 if ( cla.isOptionSet( HELP_OPTION_1 ) || cla.isOptionSet( HELP_OPTION_2 ) || ( args.length == 0 ) ) {
81 if ( ( args.length < 3 ) || ( args.length > 9 ) ) {
83 System.out.println( "error: incorrect number of arguments" );
87 final List<String> allowed_options = new ArrayList<String>();
88 allowed_options.add( GT_FIRST );
89 allowed_options.add( GT_LAST );
90 allowed_options.add( REROOTING_OPT );
91 allowed_options.add( OUTGROUP );
92 allowed_options.add( USE_SDIR );
93 final String dissallowed_options = cla.validateAllowedOptionsAsString( allowed_options );
94 if ( dissallowed_options.length() > 0 ) {
95 ForesterUtil.fatalError( "unknown option(s): " + dissallowed_options );
97 final File gene_trees_file = cla.getFile( 0 );
98 final File species_tree_file = cla.getFile( 1 );
99 final File orthology_outtable = cla.getFile( 2 );
101 if ( cla.getNumberOfNames() > 3 ) {
102 logfile = cla.getFile( 3 );
103 if ( logfile.exists() ) {
104 ForesterUtil.fatalError( "\"" + logfile + "\" already exists" );
110 boolean sdir = false;
111 if ( cla.isOptionSet( USE_SDIR ) ) {
112 if ( cla.isOptionHasAValue( USE_SDIR ) ) {
113 ForesterUtil.fatalError( "no value allowed for -" + USE_SDIR );
116 if ( logfile != null ) {
117 ForesterUtil.fatalError( "no logfile output for SDIR algorithm" );
120 String outgroup = null;
121 if ( cla.isOptionSet( OUTGROUP ) ) {
122 if ( !cla.isOptionHasAValue( OUTGROUP ) ) {
123 ForesterUtil.fatalError( "no value for -" + OUTGROUP );
126 ForesterUtil.fatalError( "no outgroup option for SDIR algorithm" );
128 outgroup = cla.getOptionValueAsCleanString( OUTGROUP );
130 REROOTING rerooting = REROOTING.BY_ALGORITHM;
131 if ( cla.isOptionSet( REROOTING_OPT ) ) {
132 if ( !cla.isOptionHasAValue( REROOTING_OPT ) ) {
133 ForesterUtil.fatalError( "no value for -" + REROOTING_OPT );
136 ForesterUtil.fatalError( "no re-rooting option for SDIR algorithm" );
138 final String rerooting_str = cla.getOptionValueAsCleanString( REROOTING_OPT ).toLowerCase();
139 if ( rerooting_str.equals( "none" ) ) {
140 rerooting = REROOTING.NONE;
142 else if ( rerooting_str.equals( "midpoint" ) ) {
143 rerooting = REROOTING.MIDPOINT;
145 else if ( rerooting_str.equals( "outgroup" ) ) {
146 rerooting = REROOTING.OUTGROUP;
150 .fatalError( "values for re-rooting are: 'none', 'midpoint', or 'outgroup' (minizming duplications is default)" );
153 if ( ForesterUtil.isEmpty( outgroup ) && ( rerooting == REROOTING.OUTGROUP ) ) {
154 ForesterUtil.fatalError( "selected re-rooting by outgroup, but outgroup not set" );
156 if ( !ForesterUtil.isEmpty( outgroup ) && ( rerooting != REROOTING.OUTGROUP ) ) {
157 ForesterUtil.fatalError( "outgroup set, but selected re-rooting by other approach" );
159 int gt_first = RIO.DEFAULT_RANGE;
160 int gt_last = RIO.DEFAULT_RANGE;
161 if ( cla.isOptionSet( GT_FIRST ) ) {
162 if ( !cla.isOptionHasAValue( GT_FIRST ) ) {
163 ForesterUtil.fatalError( "no value for -" + GT_FIRST );
166 ForesterUtil.fatalError( "no gene tree range option for SDIR algorithm" );
169 gt_first = cla.getOptionValueAsInt( GT_FIRST );
171 catch ( final IOException e ) {
172 ForesterUtil.fatalError( "could not parse integer for -" + GT_FIRST + " option" );
174 if ( gt_first < 0 ) {
175 ForesterUtil.fatalError( "attempt to set index of first tree to analyze to: " + gt_first );
178 if ( cla.isOptionSet( GT_LAST ) ) {
179 if ( !cla.isOptionHasAValue( GT_LAST ) ) {
180 ForesterUtil.fatalError( "no value for -" + GT_LAST );
183 ForesterUtil.fatalError( "no gene tree range option for SDIR algorithm" );
186 gt_last = cla.getOptionValueAsInt( GT_LAST );
188 catch ( final IOException e ) {
189 ForesterUtil.fatalError( "could not parse integer for -" + GT_LAST + " option" );
192 ForesterUtil.fatalError( "attempt to set index of last tree to analyze to: " + gt_last );
195 if ( ( ( gt_last != RIO.DEFAULT_RANGE ) && ( gt_first != RIO.DEFAULT_RANGE ) ) && ( ( gt_last < gt_first ) ) ) {
196 ForesterUtil.fatalError( "attempt to set range (0-based) of gene to analyze to: from " + gt_first + " to "
199 ForesterUtil.fatalErrorIfFileNotReadable( gene_trees_file );
200 ForesterUtil.fatalErrorIfFileNotReadable( species_tree_file );
201 if ( orthology_outtable.exists() ) {
202 ForesterUtil.fatalError( "\"" + orthology_outtable + "\" already exists" );
205 System.out.println( "Gene trees : " + gene_trees_file );
206 System.out.println( "Species tree : " + species_tree_file );
207 System.out.println( "All vs all orthology table: " + orthology_outtable );
208 if ( logfile != null ) {
209 System.out.println( "Logfile : " + logfile );
211 if ( gt_first != RIO.DEFAULT_RANGE ) {
212 System.out.println( "First gene tree to analyze: " + gt_first );
214 if ( gt_last != RIO.DEFAULT_RANGE ) {
215 System.out.println( "Last gene tree to analyze : " + gt_last );
217 String rerooting_str = "";
218 switch ( rerooting ) {
220 rerooting_str = "by minimizing duplications";
224 rerooting_str = "by midpoint method";
228 rerooting_str = "by outgroup: " + outgroup;
232 rerooting_str = "none";
236 System.out.println( "Re-rooting : " + rerooting_str );
238 System.out.println( "Non binary species tree : allowed" );
241 System.out.println( "Non binary species tree : disallowed" );
243 time = System.currentTimeMillis();
244 // Phylogeny species_tree = null;
246 // final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
247 // species_tree = factory.create( species_tree_file, new PhyloXmlParser() )[ 0 ];
249 // catch ( final Exception e ) {
250 // e.printStackTrace();
251 // System.exit( -1 );
253 final ALGORITHM algorithm;
255 algorithm = ALGORITHM.SDIR;
258 algorithm = ALGORITHM.GSDIR;
263 final NHXParser p = new NHXParser();
264 p.setReplaceUnderscores( false );
265 p.setIgnoreQuotes( true );
266 p.setTaxonomyExtraction( NHXParser.TAXONOMY_EXTRACTION.YES );
267 p.setSource( gene_trees_file );
268 rio = RIO.executeAnalysis( p,
279 rio = RIO.executeAnalysis( gene_trees_file,
289 if ( algorithm == ALGORITHM.GSDIR ) {
290 System.out.println( "Taxonomy linking based on : " + rio.getGSDIRtaxCompBase() );
294 m = rio.getOrthologTable();
297 m = RIO.calculateOrthologTable( rio.getAnalyzedGeneTrees(), true );
299 final BasicDescriptiveStatistics stats = rio.getDuplicationsStatistics();
300 writeTable( orthology_outtable, stats.getN(), m );
301 if ( ( algorithm != ALGORITHM.SDIR ) && ( logfile != null ) ) {
302 writeLogFile( logfile,
310 ForesterUtil.getForesterLibraryInformation() );
313 final java.text.DecimalFormat df = new java.text.DecimalFormat( "0.#" );
314 System.out.println( "Mean number of duplications : " + df.format( stats.arithmeticMean() ) + " (sd: "
315 + df.format( stats.sampleStandardDeviation() ) + ") ("
316 + df.format( 100.0 * stats.arithmeticMean() / rio.getIntNodesOfAnalyzedGeneTrees() ) + "%)" );
317 if ( stats.getN() > 3 ) {
318 System.out.println( "Median number of duplications: " + df.format( stats.median() ) + " ("
319 + df.format( 100.0 * stats.median() / rio.getIntNodesOfAnalyzedGeneTrees() ) + "%)" );
321 System.out.println( "Minimum duplications : " + ( int ) stats.getMin() + " ("
322 + df.format( 100.0 * stats.getMin() / rio.getIntNodesOfAnalyzedGeneTrees() ) + "%)" );
323 System.out.println( "Maximum duplications : " + ( int ) stats.getMax() + " ("
324 + df.format( 100.0 * stats.getMax() / rio.getIntNodesOfAnalyzedGeneTrees() ) + "%)" );
325 System.out.println( "Gene tree internal nodes : " + rio.getIntNodesOfAnalyzedGeneTrees() );
326 System.out.println( "Gene tree external nodes : " + rio.getExtNodesOfAnalyzedGeneTrees() );
328 catch ( final RIOException e ) {
329 ForesterUtil.fatalError( e.getLocalizedMessage() );
331 catch ( final SDIException e ) {
332 ForesterUtil.fatalError( e.getLocalizedMessage() );
334 catch ( final IOException e ) {
335 ForesterUtil.fatalError( e.getLocalizedMessage() );
337 catch ( final OutOfMemoryError e ) {
338 ForesterUtil.outOfMemoryError( e );
340 catch ( final Exception e ) {
341 ForesterUtil.unexpectedFatalError( e );
343 catch ( final Error e ) {
344 ForesterUtil.unexpectedFatalError( e );
346 time = System.currentTimeMillis() - time;
347 System.out.println( "Time: " + time + "ms" );
348 System.out.println( "OK" );
352 private final static void printHelp() {
353 System.out.println( "Usage" );
354 System.out.println();
357 + " [options] <gene trees infile> <species tree infile> <all vs all orthology table outfile> [logfile]" );
358 System.out.println();
359 System.out.println( " Options" );
360 System.out.println( " -" + GT_FIRST + "=<first> : first gene tree to analyze (0-based index)" );
361 System.out.println( " -" + GT_LAST + "=<last> : last gene tree to analyze (0-based index)" );
362 System.out.println( " -" + REROOTING_OPT
363 + "=<re-rooting>: re-rooting method for gene trees, possible values or 'none', 'midpoint'," );
364 System.out.println( " or 'outgroup' (default: by minizming duplications)" );
365 System.out.println( " -" + OUTGROUP
366 + "=<outgroup> : for rooting by outgroup, name of outgroup (external gene tree node)" );
367 System.out.println( " -" + USE_SDIR
368 + " : to use SDIR instead of GSDIR (faster, but non-binary species trees are" );
369 System.out.println( " disallowed, as are most options)" );
370 System.out.println();
371 System.out.println( " Formats" );
373 .println( " The gene trees, as well as the species tree, ideally are in phyloXML (www.phyloxml.org) format," );
375 .println( " but can also be in New Hamphshire (Newick) or Nexus format as long as species information can be" );
377 .println( " extracted from the gene names (e.g. \"HUMAN\" from \"BCL2_HUMAN\") and matched to a single species" );
378 System.out.println( " in the species tree." );
379 System.out.println();
380 System.out.println( " Examples" );
381 System.out.println( " \"rio gene_trees.nh species.xml outtable.tsv log.txt\"" );
382 System.out.println();
383 System.out.println( " More information: http://code.google.com/p/forester/wiki/RIO" );
384 System.out.println();
388 private static void writeLogFile( final File logfile,
390 final File species_tree_file,
391 final File gene_trees_file,
393 final String prg_name,
395 final String prg_date,
396 final String f ) throws IOException {
397 final EasyWriter out = ForesterUtil.createEasyWriter( logfile );
398 out.println( prg_name );
399 out.println( "version : " + prg_v );
400 out.println( "date : " + prg_date );
401 out.println( "based on: " + f );
402 out.println( "----------------------------------" );
403 out.println( "Gene trees : " + gene_trees_file );
404 out.println( "Species tree : " + species_tree_file );
405 out.println( "All vs all orthology table : " + outtable );
407 out.println( rio.getLog().toString() );
409 System.out.println( "Wrote log to \"" + logfile + "\"" );
412 private static void writeTable( final File table_outfile, final int gene_trees_analyzed, final IntMatrix m )
414 final EasyWriter w = ForesterUtil.createEasyWriter( table_outfile );
415 final java.text.DecimalFormat df = new java.text.DecimalFormat( "0.###" );
416 df.setDecimalSeparatorAlwaysShown( false );
417 for( int i = 0; i < m.size(); ++i ) {
419 w.print( m.getLabel( i ) );
422 for( int x = 0; x < m.size(); ++x ) {
423 w.print( m.getLabel( x ) );
424 for( int y = 0; y < m.size(); ++y ) {
427 if ( m.get( x, y ) != gene_trees_analyzed ) {
428 ForesterUtil.unexpectedFatalError( "diagonal value is off" );
433 w.print( df.format( ( ( double ) m.get( x, y ) ) / gene_trees_analyzed ) );
439 System.out.println( "Wrote table to \"" + table_outfile + "\"" );