2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
5 // Copyright (C) 2017 Christian M. Zmasek
8 // This library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Lesser General Public
10 // License as published by the Free Software Foundation; either
11 // version 2.1 of the License, or (at your option) any later version.
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
18 // You should have received a copy of the GNU Lesser General Public
19 // License along with this library; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
24 package org.forester.application;
27 import java.io.FilenameFilter;
28 import java.io.IOException;
29 import java.util.ArrayList;
30 import java.util.Arrays;
31 import java.util.List;
33 import org.forester.rio.RIO;
34 import org.forester.rio.RIO.REROOTING;
35 import org.forester.rio.RIOUtil;
36 import org.forester.sdi.SDIutil.ALGORITHM;
37 import org.forester.util.CommandLineArguments;
38 import org.forester.util.EasyWriter;
39 import org.forester.util.ForesterUtil;
43 public final static String PRG_NAME = "rio";
44 public final static String PRG_VERSION = "5.000";
45 public final static String PRG_DATE = "170411";
46 final static private String E_MAIL = "phyloxml@gmail.com";
47 final static private String WWW = "https://sites.google.com/site/cmzmasek/home/software/forester";
48 final static private String HELP_OPTION_1 = "help";
49 final static private String LOGFILE_SUFFIX = "_RIO_log.tsv";
50 final static private String STRIPPED_SPECIES_TREE_SUFFIX = "_RIO_sst.xml";
51 final static private String ORTHO_OUTTABLE_SUFFIX = "_RIO_orthologies.tsv";
52 final static private String OUT_MIN_DUP_GENE_TREE_SUFFIX = "_RIO_gene_tree_min_dup_";
53 final static private String OUT_MED_DUP_GENE_TREE_SUFFIX = "_RIO_gene_tree_med_dup_";
54 final static private String ORTHOLOG_GROUPS_SUFFIX = "_RIO_ortholog_groups.tsv";
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 = "s";
61 final static private String GENE_TREES_SUFFIX_OPTION = "g";
62 final static private String ORTHOLOG_GROUPS_CUTOFF_OPTION = "c";
63 final static private double ORTHOLOG_GROUPS_CUTOFF_DEFAULT = 0.5;
65 public static void main( final String[] args ) {
66 ForesterUtil.printProgramInformation( PRG_NAME,
67 "resampled inference of orthologs",
72 ForesterUtil.getForesterLibraryInformation() );
73 CommandLineArguments cla = null;
75 cla = new CommandLineArguments( args );
77 catch ( final Exception e ) {
78 ForesterUtil.fatalError( e.getMessage() );
80 if ( cla.isOptionSet( HELP_OPTION_1 ) || cla.isOptionSet( HELP_OPTION_2 ) || ( args.length == 0 ) ) {
83 if ( ( args.length < 3 ) || ( args.length > 11 ) || ( cla.getNumberOfNames() < 3 ) ) {
85 System.out.println( "error: incorrect number of arguments" );
89 final List<String> allowed_options = new ArrayList<String>();
90 allowed_options.add( GT_FIRST );
91 allowed_options.add( GT_LAST );
92 allowed_options.add( REROOTING_OPT );
93 allowed_options.add( OUTGROUP );
94 allowed_options.add( USE_SDIR );
95 allowed_options.add( GENE_TREES_SUFFIX_OPTION );
96 allowed_options.add( ORTHOLOG_GROUPS_CUTOFF_OPTION );
97 final String dissallowed_options = cla.validateAllowedOptionsAsString( allowed_options );
98 if ( dissallowed_options.length() > 0 ) {
99 ForesterUtil.fatalError( "unknown option(s): " + dissallowed_options );
101 final File gene_trees_file = cla.getFile( 0 );
102 final boolean use_dir;
105 if ( gene_trees_file.isDirectory() ) {
106 if ( !gene_trees_file.exists() ) {
107 ForesterUtil.fatalError( "gene trees directory \"" + gene_trees_file + "\" does not exist" );
110 indir = gene_trees_file;
115 final File species_tree_file = cla.getFile( 1 );
116 File orthology_outtable = null;
118 outdir = cla.getFile( 2 );
121 orthology_outtable = cla.getFile( 2 );
125 if ( ( cla.getNumberOfNames() < 4 ) ) {
126 System.out.println();
127 System.out.println( "error: incorrect number of arguments" );
128 System.out.println();
131 logfile = cla.getFile( 3 );
132 if ( logfile.exists() ) {
133 ForesterUtil.fatalError( "\"" + logfile + "\" already exists" );
137 if ( cla.getNumberOfNames() > 3 ) {
138 logfile = cla.getFile( 3 );
139 if ( logfile.exists() ) {
140 ForesterUtil.fatalError( "\"" + logfile + "\" already exists" );
147 boolean sdir = false;
148 if ( cla.isOptionSet( USE_SDIR ) ) {
149 if ( cla.isOptionHasAValue( USE_SDIR ) ) {
150 ForesterUtil.fatalError( "no value allowed for -" + USE_SDIR );
153 if ( !use_dir && logfile != null ) {
154 ForesterUtil.fatalError( "no logfile output for SDIR algorithm" );
157 String outgroup = null;
158 if ( cla.isOptionSet( OUTGROUP ) ) {
160 ForesterUtil.fatalError( "no outgroup option for SDIR algorithm" );
163 ForesterUtil.fatalError( "no outgroup option for operating on gene trees directory" );
165 if ( !cla.isOptionHasAValue( OUTGROUP ) ) {
166 ForesterUtil.fatalError( "no value for -" + OUTGROUP );
168 outgroup = cla.getOptionValueAsCleanString( OUTGROUP );
170 REROOTING rerooting = REROOTING.BY_ALGORITHM;
171 if ( cla.isOptionSet( REROOTING_OPT ) ) {
172 if ( !cla.isOptionHasAValue( REROOTING_OPT ) ) {
173 ForesterUtil.fatalError( "no value for -" + REROOTING_OPT );
176 ForesterUtil.fatalError( "no re-rooting option for SDIR algorithm" );
178 final String rerooting_str = cla.getOptionValueAsCleanString( REROOTING_OPT ).toLowerCase();
179 if ( rerooting_str.equals( "none" ) ) {
180 rerooting = REROOTING.NONE;
182 else if ( rerooting_str.equals( "midpoint" ) ) {
183 rerooting = REROOTING.MIDPOINT;
185 else if ( rerooting_str.equals( "outgroup" ) ) {
187 ForesterUtil.fatalError( "no outgroup option for operating on gene trees directory" );
189 rerooting = REROOTING.OUTGROUP;
193 .fatalError( "values for re-rooting are: 'none', 'midpoint', or 'outgroup' (minizming duplications is default)" );
196 if ( ForesterUtil.isEmpty( outgroup ) && ( rerooting == REROOTING.OUTGROUP ) ) {
197 ForesterUtil.fatalError( "selected re-rooting by outgroup, but outgroup not set" );
199 if ( !ForesterUtil.isEmpty( outgroup ) && ( rerooting != REROOTING.OUTGROUP ) ) {
200 ForesterUtil.fatalError( "outgroup set, but selected re-rooting by other approach" );
202 int gt_first = RIO.DEFAULT_RANGE;
203 int gt_last = RIO.DEFAULT_RANGE;
204 if ( cla.isOptionSet( GT_FIRST ) ) {
206 ForesterUtil.fatalError( "no gene tree range option for SDIR algorithm" );
208 if ( !cla.isOptionHasAValue( GT_FIRST ) ) {
209 ForesterUtil.fatalError( "no value for -" + GT_FIRST );
212 gt_first = cla.getOptionValueAsInt( GT_FIRST );
214 catch ( final IOException e ) {
215 ForesterUtil.fatalError( "could not parse integer for -" + GT_FIRST + " option" );
217 if ( gt_first < 0 ) {
218 ForesterUtil.fatalError( "attempt to set index of first tree to analyze to: " + gt_first );
221 if ( cla.isOptionSet( GT_LAST ) ) {
223 ForesterUtil.fatalError( "no gene tree range option for SDIR algorithm" );
225 if ( !cla.isOptionHasAValue( GT_LAST ) ) {
226 ForesterUtil.fatalError( "no value for -" + GT_LAST );
229 gt_last = cla.getOptionValueAsInt( GT_LAST );
231 catch ( final IOException e ) {
232 ForesterUtil.fatalError( "could not parse integer for -" + GT_LAST + " option" );
235 ForesterUtil.fatalError( "attempt to set index of last tree to analyze to: " + gt_last );
238 if ( ( ( gt_last != RIO.DEFAULT_RANGE ) && ( gt_first != RIO.DEFAULT_RANGE ) ) && ( ( gt_last < gt_first ) ) ) {
239 ForesterUtil.fatalError( "attempt to set range (0-based) of gene to analyze to: from " + gt_first + " to "
242 double ortholog_group_cutoff = ORTHOLOG_GROUPS_CUTOFF_DEFAULT;
243 if ( cla.isOptionSet( ORTHOLOG_GROUPS_CUTOFF_OPTION ) ) {
245 ForesterUtil.fatalError( "ortholog groups cutoff for SDIR algorithm" );
247 if ( !cla.isOptionHasAValue( ORTHOLOG_GROUPS_CUTOFF_OPTION ) ) {
248 ForesterUtil.fatalError( "no value for -" + ORTHOLOG_GROUPS_CUTOFF_OPTION );
251 ortholog_group_cutoff = cla.getOptionValueAsDouble( ORTHOLOG_GROUPS_CUTOFF_OPTION );
253 catch ( final IOException e ) {
254 ForesterUtil.fatalError( "could not parse double for -" + ORTHOLOG_GROUPS_CUTOFF_OPTION + " option" );
256 if ( ortholog_group_cutoff < 0 ) {
257 ForesterUtil.fatalError( "attempt to set ortholog groups cutoff to: " + ortholog_group_cutoff );
259 if ( ortholog_group_cutoff > 1 ) {
260 ForesterUtil.fatalError( "attempt to set ortholog groups cutoff to: " + ortholog_group_cutoff );
264 ForesterUtil.fatalErrorIfFileNotReadable( gene_trees_file );
266 final String gene_trees_suffix;
267 if ( cla.isOptionSet( GENE_TREES_SUFFIX_OPTION ) ) {
269 ForesterUtil.fatalError( "no gene tree suffix option when operating on indivual gene trees" );
271 if ( !cla.isOptionHasAValue( GENE_TREES_SUFFIX_OPTION ) ) {
272 ForesterUtil.fatalError( "no value for -" + GENE_TREES_SUFFIX_OPTION );
274 gene_trees_suffix = cla.getOptionValueAsCleanString( GENE_TREES_SUFFIX_OPTION );
277 gene_trees_suffix = ".mlt";
279 ForesterUtil.fatalErrorIfFileNotReadable( species_tree_file );
280 if ( !use_dir && orthology_outtable.exists() ) {
281 ForesterUtil.fatalError( "\"" + orthology_outtable + "\" already exists" );
286 System.out.println( "Gene trees in-dir :\t" + indir.getCanonicalPath() );
287 System.out.println( "Gene trees suffix :\t" + gene_trees_suffix );
290 System.out.println( "Gene trees :\t" + gene_trees_file.getCanonicalPath() );
292 System.out.println( "Species tree :\t" + species_tree_file.getCanonicalPath() );
294 catch ( final IOException e ) {
295 ForesterUtil.fatalError( e.getLocalizedMessage() );
298 System.out.println( "Out-dir :\t" + outdir );
301 System.out.println( "All vs all orthology results table :\t" + orthology_outtable );
303 if ( logfile != null ) {
304 System.out.println( "Logfile :\t" + logfile );
306 System.out.println( "Ortholog groups cutoff :\t" + ortholog_group_cutoff );
307 if ( gt_first != RIO.DEFAULT_RANGE ) {
308 System.out.println( "First gene tree to analyze :\t" + gt_first );
310 if ( gt_last != RIO.DEFAULT_RANGE ) {
311 System.out.println( "Last gene tree to analyze :\t" + gt_last );
313 String rerooting_str = "";
314 switch ( rerooting ) {
316 rerooting_str = "by minimizing duplications";
320 rerooting_str = "by midpoint method";
324 rerooting_str = "by outgroup: " + outgroup;
328 rerooting_str = "none";
332 System.out.println( "Re-rooting : \t" + rerooting_str );
334 System.out.println( "Non binary species tree :\tallowed" );
337 System.out.println( "Non binary species tree :\tdisallowed" );
339 time = System.currentTimeMillis();
340 final ALGORITHM algorithm;
342 algorithm = ALGORITHM.SDIR;
345 algorithm = ALGORITHM.GSDIR;
347 EasyWriter log = null;
349 if ( outdir.exists() ) {
350 if ( !outdir.isDirectory() ) {
351 ForesterUtil.fatalError( PRG_NAME,
352 "out-directory [" + outdir + "] already exists but is not a directory" );
356 final boolean success = outdir.mkdirs();
358 ForesterUtil.fatalError( PRG_NAME, "could not create out-directory [" + outdir + "]" );
361 final String species_tree_file_name = species_tree_file.getName();
362 final File gene_trees_files[] = indir.listFiles( new FilenameFilter() {
365 public boolean accept( final File dir, final String name ) {
366 return ( ( name.endsWith( gene_trees_suffix ) ) && !( name.equals( species_tree_file_name ) ) );
369 if ( gene_trees_files.length < 1 ) {
370 ForesterUtil.fatalError( PRG_NAME,
371 "in-directory [" + indir
372 + "] does not contain any gene tree files with suffix "
373 + gene_trees_suffix );
376 log = ForesterUtil.createEasyWriter( logfile );
378 catch ( final IOException e ) {
379 ForesterUtil.fatalError( PRG_NAME, "could not create [" + logfile + "]" );
381 Arrays.sort( gene_trees_files );
383 log.print( "# program" );
385 log.print( PRG_NAME );
387 log.print( "# version" );
389 log.print( PRG_VERSION );
391 log.print( "# date" );
393 log.print( PRG_DATE );
395 log.print( "# Algorithm " );
397 log.print( algorithm.toString() );
399 log.print( "# Gene trees in-dir" );
401 log.print( indir.getCanonicalPath() );
403 log.print( "# Gene trees suffix" );
405 log.print( gene_trees_suffix );
407 log.print( "# Species tree" );
409 log.print( species_tree_file.getCanonicalPath() );
411 log.print( "# Out-dir" );
413 log.print( outdir.getCanonicalPath() );
415 log.print( "# Logfile" );
417 log.print( logfile.getCanonicalPath() );
419 log.print( "# Ortholog groups cutoff" );
421 log.print( Double.toString( ortholog_group_cutoff ) );
423 if ( gt_first != RIO.DEFAULT_RANGE ) {
424 log.print( "# First gene tree to analyze" );
426 log.print( Integer.toString( gt_first ) );
429 if ( gt_last != RIO.DEFAULT_RANGE ) {
430 log.print( "# Last gene tree to analyze" );
432 log.print( Integer.toString( gt_last ) );
435 log.print( "# Re-rooting" );
437 log.print( rerooting_str );
439 log.print( "# Non binary species tree" );
442 log.print( "allowed" );
445 log.print( "disallowed" );
451 log.print( "EXT NODES" );
453 log.print( ortholog_group_cutoff + " O GROUPS" );
455 log.print( "0.05 O GROUPS" );
457 log.print( "0.25 O GROUPS" );
459 log.print( "0.5 O GROUPS" );
461 log.print( "0.75 O GROUPS" );
463 log.print( "0.95 O GROUPS" );
465 log.print( "MEDIAN DUP" );
467 log.print( "MEAN DUP" );
469 log.print( "MEAN DUP SD" );
471 log.print( "MIN DUP" );
473 log.print( "MAX DUP" );
475 log.print( "REMOVED EXT NODES" );
480 catch ( IOException e ) {
481 ForesterUtil.fatalError( PRG_NAME, e.getLocalizedMessage() );
484 for( final File gf : gene_trees_files ) {
485 String outname = gf.getName();
488 System.out.print( "\r" + counter + "/" + gene_trees_files.length + ": " + outname );
490 if ( outname.indexOf( "." ) > 0 ) {
491 outname = outname.substring( 0, outname.lastIndexOf( "." ) );
494 RIOUtil.executeAnalysis( gf,
496 new File( outdir.getCanonicalFile() + "/" + outname + ORTHO_OUTTABLE_SUFFIX ),
497 new File( outdir.getCanonicalFile() + "/" + outname + ORTHOLOG_GROUPS_SUFFIX ),
498 new File( outdir.getCanonicalFile() + "/" + outname + LOGFILE_SUFFIX ),
503 new File( outdir.getCanonicalFile() + "/" + outname
504 + STRIPPED_SPECIES_TREE_SUFFIX ),
505 new File( outdir.getCanonicalFile() + "/" + outname
506 + OUT_MIN_DUP_GENE_TREE_SUFFIX ),
507 new File( outdir.getCanonicalFile() + "/" + outname
508 + OUT_MED_DUP_GENE_TREE_SUFFIX ),
513 ortholog_group_cutoff );
515 catch ( IOException e ) {
516 ForesterUtil.fatalError( PRG_NAME, e.getLocalizedMessage() );
521 System.out.println();
524 String outname = orthology_outtable.toString();
525 if ( outname.indexOf( "." ) > 0 ) {
526 outname = outname.substring( 0, outname.lastIndexOf( "." ) );
528 RIOUtil.executeAnalysis( gene_trees_file,
531 new File( outname + ORTHOLOG_GROUPS_SUFFIX ),
537 new File( outname + STRIPPED_SPECIES_TREE_SUFFIX ),
538 new File( outname + OUT_MIN_DUP_GENE_TREE_SUFFIX ),
539 new File( outname + OUT_MED_DUP_GENE_TREE_SUFFIX ),
540 algorithm == ALGORITHM.GSDIR,
544 ortholog_group_cutoff );
547 time = System.currentTimeMillis() - time;
548 System.out.println( "Time :\t" + time + "ms" );
554 catch ( IOException e ) {
555 ForesterUtil.fatalError( PRG_NAME, e.getLocalizedMessage() );
557 time = System.currentTimeMillis() - time;
558 System.out.println( "Time :\t" + time + "ms" );
563 private final static void printHelp() {
564 System.out.println( "Usage" );
565 System.out.println();
566 System.out.println( PRG_NAME
567 + " [options] <gene trees infile> <species tree infile> <all vs all orthology table outfile> [logfile]" );
568 System.out.println();
569 System.out.println( PRG_NAME + " [options] <gene trees indir> <species tree infile> <outdir> <logfile>" );
570 System.out.println();
571 System.out.println();
572 System.out.println( " Options" );
573 System.out.println( " -" + GT_FIRST + "=<first> : first gene tree to analyze (0-based index)" );
574 System.out.println( " -" + GT_LAST + "=<last> : last gene tree to analyze (0-based index)" );
575 System.out.println( " -" + ORTHOLOG_GROUPS_CUTOFF_OPTION
576 + "=<cutoff> : cutoff value for ortholog groups (default: " + ORTHOLOG_GROUPS_CUTOFF_DEFAULT + ")" );
577 System.out.println( " -" + REROOTING_OPT
578 + "=<re-rooting>: re-rooting method for gene trees, possible values or 'none', 'midpoint'," );
579 System.out.println( " or 'outgroup' (default: by minizming duplications)" );
580 System.out.println( " -" + OUTGROUP
581 + "=<outgroup> : for rooting by outgroup, name of outgroup (external gene tree node)" );
582 System.out.println( " -" + USE_SDIR
583 + " : to use SDIR instead of GSDIR (faster, but non-binary species trees are" );
584 System.out.println( " disallowed, as are most options)" );
585 System.out.println( " -" + GENE_TREES_SUFFIX_OPTION
586 + "=<suffix> : suffix for gene trees when operating on gene tree directories (default: .mlt)" );
587 System.out.println();
588 System.out.println( " Formats" );
590 .println( " The gene trees, as well as the species tree, ideally are in phyloXML (www.phyloxml.org) format," );
592 .println( " but can also be in New Hamphshire (Newick) or Nexus format as long as species information can be" );
594 .println( " extracted from the gene names (e.g. \"HUMAN\" from \"BCL2_HUMAN\") and matched to a single species" );
595 System.out.println( " in the species tree." );
596 System.out.println();
597 System.out.println( " Examples" );
598 System.out.println( " rio -s gene_trees.nh species.xml outtable.tsv" );
599 System.out.println( " rio gene_trees.nh species.xml outtable.tsv log.txt" );
600 System.out.println( " rio -c=0.9 -f=10 -l=100 -r=none gene_trees.xml species.xml outtable.tsv log.txt" );
601 System.out.println( " rio -g=.xml gene_trees_dir species.xml out_dir log.tsv" );
602 System.out.println();