aa5063925570eee0c3d3e3ac91a217cfb34e5dfd
[jalview.git] / forester / java / src / org / forester / application / rio.java
1 // $Id:
2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
4 //
5 // Copyright (C) 2017 Christian M. Zmasek
6 // All rights reserved
7 //
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.
12 //
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.
17 //
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
21 //
22 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
23
24 package org.forester.application;
25
26 import java.io.File;
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;
32
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;
40
41 public class rio {
42     //
43
44     public final static String  PRG_NAME                       = "rio";
45     public final static String  PRG_VERSION                    = "5.900";
46     public final static String  PRG_DATE                       = "170420";
47     final static private String E_MAIL                         = "phyloxml@gmail.com";
48     final static private String WWW                            = "https://sites.google.com/site/cmzmasek/home/software/forester";
49     final static private String HELP_OPTION_1                  = "help";
50     final static private String LOGFILE_SUFFIX                 = "_RIO_log.tsv";
51     final static private String STRIPPED_SPECIES_TREE_SUFFIX   = "_RIO_sst.xml";
52     final static private String ORTHO_OUTTABLE_SUFFIX          = "_RIO_orthologies.tsv";
53     final static private String ORTHO_OUTTABLE_WITH_MAP_SUFFIX = "_RIO_orthologies_ext_map.tsv";
54     final static private String OUT_MIN_DUP_GENE_TREE_SUFFIX   = "_RIO_gene_tree_min_dup_";
55     final static private String OUT_MED_DUP_GENE_TREE_SUFFIX   = "_RIO_gene_tree_med_dup_";
56     final static private String ORTHOLOG_GROUPS_SUFFIX         = "_RIO_ortholog_groups.tsv";
57     final static private String HELP_OPTION_2                  = "h";
58     final static private String GT_FIRST                       = "f";
59     final static private String GT_LAST                        = "l";
60     final static private String REROOTING_OPT                  = "r";
61     final static private String OUTGROUP                       = "o";
62     final static private String USE_SDIR                       = "s";
63     final static private String GENE_TREES_SUFFIX_OPTION       = "g";
64     final static private String MAPPINGS_DIR_OPTION            = "m";
65     final static private String MAPPINGS_SUFFIX_OPTION         = "ms";
66     final static private String MAPPINGS_SUFFIX_DEFAULT        = ".nim";
67     final static private String ORTHOLOG_GROUPS_CUTOFF_OPTION  = "c";
68     final static private String GENE_TREES_SUFFIX_DEFAULT      = ".mlt";
69     final static private double ORTHOLOG_GROUPS_CUTOFF_DEFAULT = 0.5;
70
71     public static void main( final String[] args ) {
72         ForesterUtil.printProgramInformation( PRG_NAME,
73                                               "resampled inference of orthologs",
74                                               PRG_VERSION,
75                                               PRG_DATE,
76                                               E_MAIL,
77                                               WWW,
78                                               ForesterUtil.getForesterLibraryInformation() );
79         CommandLineArguments cla = null;
80         try {
81             cla = new CommandLineArguments( args );
82         }
83         catch ( final Exception e ) {
84             ForesterUtil.fatalError( e.getMessage() );
85         }
86         if ( cla.isOptionSet( HELP_OPTION_1 ) || cla.isOptionSet( HELP_OPTION_2 ) || ( args.length == 0 ) ) {
87             printHelp();
88         }
89         if ( ( args.length < 3 ) || ( args.length > 11 ) || ( cla.getNumberOfNames() < 3 ) ) {
90             System.out.println();
91             System.out.println( "error: incorrect number of arguments" );
92             System.out.println();
93             printHelp();
94         }
95         final List<String> allowed_options = new ArrayList<String>();
96         allowed_options.add( GT_FIRST );
97         allowed_options.add( GT_LAST );
98         allowed_options.add( REROOTING_OPT );
99         allowed_options.add( OUTGROUP );
100         allowed_options.add( USE_SDIR );
101         allowed_options.add( GENE_TREES_SUFFIX_OPTION );
102         allowed_options.add( ORTHOLOG_GROUPS_CUTOFF_OPTION );
103         allowed_options.add( MAPPINGS_DIR_OPTION );
104         allowed_options.add( MAPPINGS_SUFFIX_OPTION );
105         final String dissallowed_options = cla.validateAllowedOptionsAsString( allowed_options );
106         if ( dissallowed_options.length() > 0 ) {
107             ForesterUtil.fatalError( "unknown option(s): " + dissallowed_options );
108         }
109         final File gene_trees_file = cla.getFile( 0 );
110         final boolean use_dir;
111         File indir = null;
112         File outdir = null;
113         if ( gene_trees_file.isDirectory() ) {
114             if ( !gene_trees_file.exists() ) {
115                 ForesterUtil.fatalError( "gene trees directory \"" + gene_trees_file + "\" does not exist" );
116             }
117             if ( gene_trees_file.listFiles().length < 1 ) {
118                 ForesterUtil.fatalError( "gene trees directory \"" + gene_trees_file + "\" is empty" );
119             }
120             use_dir = true;
121             indir = gene_trees_file;
122         }
123         else {
124             use_dir = false;
125         }
126         final File species_tree_file = cla.getFile( 1 );
127         File orthology_outtable = null;
128         if ( use_dir ) {
129             outdir = cla.getFile( 2 );
130         }
131         else {
132             orthology_outtable = cla.getFile( 2 );
133         }
134         File logfile;
135         if ( use_dir ) {
136             if ( ( cla.getNumberOfNames() < 4 ) ) {
137                 System.out.println();
138                 System.out.println( "error: incorrect number of arguments" );
139                 System.out.println();
140                 printHelp();
141             }
142             logfile = cla.getFile( 3 );
143             if ( logfile.exists() ) {
144                 ForesterUtil.fatalError( "\"" + logfile + "\" already exists" );
145             }
146         }
147         else {
148             if ( cla.getNumberOfNames() > 3 ) {
149                 logfile = cla.getFile( 3 );
150                 if ( logfile.exists() ) {
151                     ForesterUtil.fatalError( "\"" + logfile + "\" already exists" );
152                 }
153             }
154             else {
155                 logfile = null;
156             }
157         }
158         boolean sdir = false;
159         if ( cla.isOptionSet( USE_SDIR ) ) {
160             if ( cla.isOptionHasAValue( USE_SDIR ) ) {
161                 ForesterUtil.fatalError( "no value allowed for -" + USE_SDIR );
162             }
163             sdir = true;
164             if ( !use_dir && logfile != null ) {
165                 ForesterUtil.fatalError( "no logfile output for SDIR algorithm" );
166             }
167         }
168         String outgroup = null;
169         if ( cla.isOptionSet( OUTGROUP ) ) {
170             if ( sdir ) {
171                 ForesterUtil.fatalError( "no outgroup option for SDIR algorithm" );
172             }
173             if ( use_dir ) {
174                 ForesterUtil.fatalError( "no outgroup option for operating on gene trees directory" );
175             }
176             if ( !cla.isOptionHasAValue( OUTGROUP ) ) {
177                 ForesterUtil.fatalError( "no value for -" + OUTGROUP );
178             }
179             outgroup = cla.getOptionValueAsCleanString( OUTGROUP );
180         }
181         REROOTING rerooting = REROOTING.BY_ALGORITHM;
182         if ( cla.isOptionSet( REROOTING_OPT ) ) {
183             if ( !cla.isOptionHasAValue( REROOTING_OPT ) ) {
184                 ForesterUtil.fatalError( "no value for -" + REROOTING_OPT );
185             }
186             if ( sdir ) {
187                 ForesterUtil.fatalError( "no re-rooting option for SDIR algorithm" );
188             }
189             final String rerooting_str = cla.getOptionValueAsCleanString( REROOTING_OPT ).toLowerCase();
190             if ( rerooting_str.equals( "none" ) ) {
191                 rerooting = REROOTING.NONE;
192             }
193             else if ( rerooting_str.equals( "midpoint" ) ) {
194                 rerooting = REROOTING.MIDPOINT;
195             }
196             else if ( rerooting_str.equals( "outgroup" ) ) {
197                 if ( use_dir ) {
198                     ForesterUtil.fatalError( "no outgroup option for operating on gene trees directory" );
199                 }
200                 rerooting = REROOTING.OUTGROUP;
201             }
202             else {
203                 ForesterUtil
204                         .fatalError( "values for re-rooting are: 'none', 'midpoint', or 'outgroup' (minizming duplications is default)" );
205             }
206         }
207         if ( ForesterUtil.isEmpty( outgroup ) && ( rerooting == REROOTING.OUTGROUP ) ) {
208             ForesterUtil.fatalError( "selected re-rooting by outgroup, but outgroup not set" );
209         }
210         if ( !ForesterUtil.isEmpty( outgroup ) && ( rerooting != REROOTING.OUTGROUP ) ) {
211             ForesterUtil.fatalError( "outgroup set, but selected re-rooting by other approach" );
212         }
213         int gt_first = RIO.DEFAULT_RANGE;
214         int gt_last = RIO.DEFAULT_RANGE;
215         if ( cla.isOptionSet( GT_FIRST ) ) {
216             if ( sdir ) {
217                 ForesterUtil.fatalError( "no gene tree range option for SDIR algorithm" );
218             }
219             if ( !cla.isOptionHasAValue( GT_FIRST ) ) {
220                 ForesterUtil.fatalError( "no value for -" + GT_FIRST );
221             }
222             try {
223                 gt_first = cla.getOptionValueAsInt( GT_FIRST );
224             }
225             catch ( final IOException e ) {
226                 ForesterUtil.fatalError( "could not parse integer for -" + GT_FIRST + " option" );
227             }
228             if ( gt_first < 0 ) {
229                 ForesterUtil.fatalError( "attempt to set index of first tree to analyze to: " + gt_first );
230             }
231         }
232         if ( cla.isOptionSet( GT_LAST ) ) {
233             if ( sdir ) {
234                 ForesterUtil.fatalError( "no gene tree range option for SDIR algorithm" );
235             }
236             if ( !cla.isOptionHasAValue( GT_LAST ) ) {
237                 ForesterUtil.fatalError( "no value for -" + GT_LAST );
238             }
239             try {
240                 gt_last = cla.getOptionValueAsInt( GT_LAST );
241             }
242             catch ( final IOException e ) {
243                 ForesterUtil.fatalError( "could not parse integer for -" + GT_LAST + " option" );
244             }
245             if ( gt_last < 0 ) {
246                 ForesterUtil.fatalError( "attempt to set index of last tree to analyze to: " + gt_last );
247             }
248         }
249         if ( ( ( gt_last != RIO.DEFAULT_RANGE ) && ( gt_first != RIO.DEFAULT_RANGE ) ) && ( ( gt_last < gt_first ) ) ) {
250             ForesterUtil.fatalError( "attempt to set range (0-based) of gene to analyze to: from " + gt_first + " to "
251                     + gt_last );
252         }
253         double ortholog_group_cutoff = ORTHOLOG_GROUPS_CUTOFF_DEFAULT;
254         if ( cla.isOptionSet( ORTHOLOG_GROUPS_CUTOFF_OPTION ) ) {
255             if ( sdir ) {
256                 ForesterUtil.fatalError( "ortholog groups cutoff for SDIR algorithm" );
257             }
258             if ( !cla.isOptionHasAValue( ORTHOLOG_GROUPS_CUTOFF_OPTION ) ) {
259                 ForesterUtil.fatalError( "no value for -" + ORTHOLOG_GROUPS_CUTOFF_OPTION );
260             }
261             try {
262                 ortholog_group_cutoff = cla.getOptionValueAsDouble( ORTHOLOG_GROUPS_CUTOFF_OPTION );
263             }
264             catch ( final IOException e ) {
265                 ForesterUtil.fatalError( "could not parse double for -" + ORTHOLOG_GROUPS_CUTOFF_OPTION + " option" );
266             }
267             if ( ortholog_group_cutoff < 0 ) {
268                 ForesterUtil.fatalError( "attempt to set ortholog groups cutoff to: " + ortholog_group_cutoff );
269             }
270             if ( ortholog_group_cutoff > 1 ) {
271                 ForesterUtil.fatalError( "attempt to set ortholog groups cutoff to: " + ortholog_group_cutoff );
272             }
273         }
274         if ( !use_dir ) {
275             ForesterUtil.fatalErrorIfFileNotReadable( gene_trees_file );
276         }
277         final String gene_trees_suffix;
278         if ( cla.isOptionSet( GENE_TREES_SUFFIX_OPTION ) ) {
279             if ( !use_dir ) {
280                 ForesterUtil.fatalError( "no gene tree suffix option when operating on indivual gene trees" );
281             }
282             if ( !cla.isOptionHasAValue( GENE_TREES_SUFFIX_OPTION ) ) {
283                 ForesterUtil.fatalError( "no value for -" + GENE_TREES_SUFFIX_OPTION );
284             }
285             gene_trees_suffix = cla.getOptionValueAsCleanString( GENE_TREES_SUFFIX_OPTION );
286         }
287         else {
288             gene_trees_suffix = GENE_TREES_SUFFIX_DEFAULT;
289         }
290         final boolean perform_id_mapping;
291         final File id_mapping_dir;
292         if ( cla.isOptionSet( MAPPINGS_DIR_OPTION ) ) {
293             id_mapping_dir = new File( cla.getOptionValue( MAPPINGS_DIR_OPTION ) );
294             perform_id_mapping = true;
295             if ( !use_dir ) {
296                 ForesterUtil.fatalError( "no id mapping when operating on indivual gene trees" );
297             }
298             if ( !id_mapping_dir.exists() ) {
299                 ForesterUtil.fatalError( "id mappings directory \"" + id_mapping_dir + "\" does not exist" );
300             }
301             if ( !id_mapping_dir.isDirectory() ) {
302                 ForesterUtil.fatalError( "id mappings directory \"" + id_mapping_dir + "\" is not a directory" );
303             }
304             if ( id_mapping_dir.listFiles().length < 1 ) {
305                 ForesterUtil.fatalError( "id mappings directory \"" + id_mapping_dir + "\" is empty" );
306             }
307         }
308         else {
309             id_mapping_dir = null;
310             perform_id_mapping = false;
311         }
312         final String id_mapping_suffix;
313         if ( cla.isOptionSet( MAPPINGS_SUFFIX_OPTION ) ) {
314             if ( !use_dir ) {
315                 ForesterUtil.fatalError( "no id mapping file suffix option when operating on indivual gene trees" );
316             }
317             if ( !perform_id_mapping ) {
318                 ForesterUtil.fatalError( "no id mapping directory given" );
319             }
320             if ( !cla.isOptionHasAValue( MAPPINGS_SUFFIX_OPTION ) ) {
321                 ForesterUtil.fatalError( "no value for -" + MAPPINGS_SUFFIX_OPTION );
322             }
323             id_mapping_suffix = cla.getOptionValueAsCleanString( MAPPINGS_SUFFIX_OPTION );
324         }
325         else {
326             id_mapping_suffix = MAPPINGS_SUFFIX_DEFAULT;
327         }
328         ForesterUtil.fatalErrorIfFileNotReadable( species_tree_file );
329         if ( !use_dir && orthology_outtable.exists() ) {
330             ForesterUtil.fatalError( "\"" + orthology_outtable + "\" already exists" );
331         }
332         long time = 0;
333         try {
334             if ( use_dir ) {
335                 System.out.println( "Gene trees in-dir                   :\t" + indir.getCanonicalPath() );
336                 System.out.println( "Gene trees suffix                   :\t" + gene_trees_suffix );
337             }
338             else {
339                 System.out.println( "Gene trees                          :\t" + gene_trees_file.getCanonicalPath() );
340             }
341             System.out.println( "Species tree                        :\t" + species_tree_file.getCanonicalPath() );
342         }
343         catch ( final IOException e ) {
344             ForesterUtil.fatalError( e.getLocalizedMessage() );
345         }
346         if ( perform_id_mapping ) {
347             try {
348                 System.out.println( "Id mappings in-dir                  :\t" + id_mapping_dir.getCanonicalPath() );
349             }
350             catch ( IOException e ) {
351                 ForesterUtil.fatalError( e.getLocalizedMessage() );
352             }
353             System.out.println( "Id mappings suffix                  :\t" + id_mapping_suffix );
354         }
355         if ( use_dir ) {
356             System.out.println( "Out-dir                             :\t" + outdir );
357         }
358         else {
359             System.out.println( "All vs all orthology results table  :\t" + orthology_outtable );
360         }
361         if ( logfile != null ) {
362             System.out.println( "Logfile                             :\t" + logfile );
363         }
364         System.out.println( "Ortholog groups cutoff              :\t" + ortholog_group_cutoff );
365         if ( gt_first != RIO.DEFAULT_RANGE ) {
366             System.out.println( "First gene tree to analyze          :\t" + gt_first );
367         }
368         if ( gt_last != RIO.DEFAULT_RANGE ) {
369             System.out.println( "Last gene tree to analyze           :\t" + gt_last );
370         }
371         String rerooting_str = "";
372         switch ( rerooting ) {
373             case BY_ALGORITHM: {
374                 rerooting_str = "by minimizing duplications";
375                 break;
376             }
377             case MIDPOINT: {
378                 rerooting_str = "by midpoint method";
379                 break;
380             }
381             case OUTGROUP: {
382                 rerooting_str = "by outgroup: " + outgroup;
383                 break;
384             }
385             case NONE: {
386                 rerooting_str = "none";
387                 break;
388             }
389         }
390         System.out.println( "Re-rooting                          : \t" + rerooting_str );
391         if ( !sdir ) {
392             System.out.println( "Non binary species tree             :\tallowed" );
393         }
394         else {
395             System.out.println( "Non binary species tree             :\tdisallowed" );
396         }
397         time = System.currentTimeMillis();
398         final ALGORITHM algorithm;
399         if ( sdir ) {
400             algorithm = ALGORITHM.SDIR;
401         }
402         else {
403             algorithm = ALGORITHM.GSDIR;
404         }
405         EasyWriter log = null;
406         if ( use_dir ) {
407             if ( outdir.exists() ) {
408                 if ( !outdir.isDirectory() ) {
409                     ForesterUtil.fatalError( PRG_NAME,
410                                              "out-directory [" + outdir + "] already exists but is not a directory" );
411                 }
412             }
413             else {
414                 final boolean success = outdir.mkdirs();
415                 if ( !success ) {
416                     ForesterUtil.fatalError( PRG_NAME, "could not create out-directory [" + outdir + "]" );
417                 }
418             }
419             final String species_tree_file_name = species_tree_file.getName();
420             final File gene_trees_files[] = indir.listFiles( new FilenameFilter() {
421
422                 @Override
423                 public boolean accept( final File dir, final String name ) {
424                     return ( ( name.endsWith( gene_trees_suffix ) ) && !( name.equals( species_tree_file_name ) ) );
425                 }
426             } );
427             if ( gene_trees_files.length < 1 ) {
428                 ForesterUtil.fatalError( PRG_NAME,
429                                          "in-directory [" + indir
430                                                  + "] does not contain any gene tree files with suffix "
431                                                  + gene_trees_suffix );
432             }
433             try {
434                 log = ForesterUtil.createEasyWriter( logfile );
435             }
436             catch ( final IOException e ) {
437                 ForesterUtil.fatalError( PRG_NAME, "could not create [" + logfile + "]" );
438             }
439             Arrays.sort( gene_trees_files );
440             try {
441                 log.print( "# program" );
442                 log.print( "\t" );
443                 log.print( PRG_NAME );
444                 log.println();
445                 log.print( "# version" );
446                 log.print( "\t" );
447                 log.print( PRG_VERSION );
448                 log.println();
449                 log.print( "# date" );
450                 log.print( "\t" );
451                 log.print( PRG_DATE );
452                 log.println();
453                 log.print( "# Algorithm " );
454                 log.print( "\t" );
455                 log.print( algorithm.toString() );
456                 log.println();
457                 log.print( "# Gene trees in-dir" );
458                 log.print( "\t" );
459                 log.print( indir.getCanonicalPath() );
460                 log.println();
461                 log.print( "# Gene trees suffix" );
462                 log.print( "\t" );
463                 log.print( gene_trees_suffix );
464                 log.println();
465                 log.print( "# Species tree" );
466                 log.print( "\t" );
467                 log.print( species_tree_file.getCanonicalPath() );
468                 log.println();
469                 log.print( "# Out-dir" );
470                 log.print( "\t" );
471                 log.print( outdir.getCanonicalPath() );
472                 log.println();
473                 log.print( "# Logfile" );
474                 log.print( "\t" );
475                 log.print( logfile.getCanonicalPath() );
476                 log.println();
477                 log.print( "# Ortholog groups cutoff" );
478                 log.print( "\t" );
479                 log.print( Double.toString( ortholog_group_cutoff ) );
480                 log.println();
481                 if ( gt_first != RIO.DEFAULT_RANGE ) {
482                     log.print( "# First gene tree to analyze" );
483                     log.print( "\t" );
484                     log.print( Integer.toString( gt_first ) );
485                     log.println();
486                 }
487                 if ( gt_last != RIO.DEFAULT_RANGE ) {
488                     log.print( "# Last gene tree to analyze" );
489                     log.print( "\t" );
490                     log.print( Integer.toString( gt_last ) );
491                     log.println();
492                 }
493                 log.print( "# Re-rooting" );
494                 log.print( "\t" );
495                 log.print( rerooting_str );
496                 log.println();
497                 log.print( "# Non binary species tree" );
498                 log.print( "\t" );
499                 if ( !sdir ) {
500                     log.print( "allowed" );
501                 }
502                 else {
503                     log.print( "disallowed" );
504                 }
505                 log.println();
506                 log.println();
507                 log.print( "NAME" );
508                 log.print( "\t" );
509                 log.print( "EXT NODES" );
510                 log.print( "\t" );
511                 log.print( ortholog_group_cutoff + " O GROUPS" );
512                 log.print( "\t" );
513                 log.print( "0.05 O GROUPS" );
514                 log.print( "\t" );
515                 log.print( "0.25 O GROUPS" );
516                 log.print( "\t" );
517                 log.print( "0.5 O GROUPS" );
518                 log.print( "\t" );
519                 log.print( "0.75 O GROUPS" );
520                 log.print( "\t" );
521                 log.print( "0.95 O GROUPS" );
522                 log.print( "\t" );
523                 if ( true ) { //TODO
524                     log.print( "BEST TREE DUP" );
525                     log.print( "\t" );
526                 }
527                 log.print( "MEDIAN DUP" );
528                 log.print( "\t" );
529                 log.print( "MEAN DUP" );
530                 log.print( "\t" );
531                 log.print( "MEAN DUP SD" );
532                 log.print( "\t" );
533                 log.print( "MIN DUP" );
534                 log.print( "\t" );
535                 log.print( "MAX DUP" );
536                 log.print( "\t" );
537                 log.print( "REMOVED EXT NODES" );
538                 log.print( "\t" );
539                 log.print( "N" );
540                 log.println();
541             }
542             catch ( IOException e ) {
543                 ForesterUtil.fatalError( PRG_NAME, e.getLocalizedMessage() );
544             }
545             int counter = 1;
546             for( final File gf : gene_trees_files ) {
547                 String outname = gf.getName();
548                 System.out
549                         .print( "\r                                                                                            " );
550                 System.out.print( "\r" + counter + "/" + gene_trees_files.length + ": " + outname );
551                 counter++;
552                 if ( outname.indexOf( "." ) > 0 ) {
553                     outname = outname.substring( 0, outname.lastIndexOf( "." ) );
554                 }
555                 try {
556                     RIOUtil.executeAnalysis( gf,
557                                              species_tree_file,
558                                              new File( outdir.getCanonicalFile() + "/" + outname
559                                                      + ORTHO_OUTTABLE_SUFFIX ),
560                                              new File( outdir.getCanonicalFile() + "/" + outname
561                                                      + ORTHO_OUTTABLE_WITH_MAP_SUFFIX ),
562                                              new File( outdir.getCanonicalFile() + "/" + outname
563                                                      + ORTHOLOG_GROUPS_SUFFIX ),
564                                              new File( outdir.getCanonicalFile() + "/" + outname + LOGFILE_SUFFIX ),
565                                              outgroup,
566                                              rerooting,
567                                              gt_first,
568                                              gt_last,
569                                              new File( outdir.getCanonicalFile() + "/" + outname
570                                                      + STRIPPED_SPECIES_TREE_SUFFIX ),
571                                              new File( outdir.getCanonicalFile() + "/" + outname
572                                                      + OUT_MIN_DUP_GENE_TREE_SUFFIX ),
573                                              new File( outdir.getCanonicalFile() + "/" + outname
574                                                      + OUT_MED_DUP_GENE_TREE_SUFFIX ),
575                                              true,
576                                              algorithm,
577                                              true,
578                                              log,
579                                              ortholog_group_cutoff,
580                                              perform_id_mapping,
581                                              id_mapping_dir,
582                                              id_mapping_suffix );
583                 }
584                 catch ( IOException e ) {
585                     ForesterUtil.fatalError( PRG_NAME, e.getLocalizedMessage() );
586                 }
587             }
588             System.out
589                     .print( "\r                                                                                        " );
590             System.out.println();
591         }
592         else {
593             String outname = orthology_outtable.toString();
594             if ( outname.indexOf( "." ) > 0 ) {
595                 outname = outname.substring( 0, outname.lastIndexOf( "." ) );
596             }
597             RIOUtil.executeAnalysis( gene_trees_file,
598                                      species_tree_file,
599                                      orthology_outtable,
600                                      null,
601                                      new File( outname + ORTHOLOG_GROUPS_SUFFIX ),
602                                      logfile,
603                                      outgroup,
604                                      rerooting,
605                                      gt_first,
606                                      gt_last,
607                                      new File( outname + STRIPPED_SPECIES_TREE_SUFFIX ),
608                                      new File( outname + OUT_MIN_DUP_GENE_TREE_SUFFIX ),
609                                      new File( outname + OUT_MED_DUP_GENE_TREE_SUFFIX ),
610                                      algorithm == ALGORITHM.GSDIR,
611                                      algorithm,
612                                      false,
613                                      null,
614                                      ortholog_group_cutoff,
615                                      false,
616                                      null,
617                                      null );
618         }
619         if ( !use_dir ) {
620             time = System.currentTimeMillis() - time;
621             System.out.println( "Time                                :\t" + time + "ms" );
622         }
623         else {
624             try {
625                 log.close();
626             }
627             catch ( IOException e ) {
628                 ForesterUtil.fatalError( PRG_NAME, e.getLocalizedMessage() );
629             }
630             time = System.currentTimeMillis() - time;
631             System.out.println( "Time                                :\t" + time + "ms" );
632         }
633         System.exit( 0 );
634     }
635
636     private final static void printHelp() {
637         System.out.println( "Usage" );
638         System.out.println();
639         System.out.println( PRG_NAME
640                 + " [options] <gene trees infile> <species tree infile> <all vs all orthology table outfile> [logfile]" );
641         System.out.println();
642         System.out.println( PRG_NAME + " [options] <gene trees indir> <species tree infile> <outdir> <logfile>" );
643         System.out.println();
644         System.out.println();
645         System.out.println( " Options" );
646         System.out.println( "  -" + GT_FIRST + "=<first>     : first gene tree to analyze (0-based index)" );
647         System.out.println( "  -" + GT_LAST + "=<last>      : last gene tree to analyze (0-based index)" );
648         System.out.println( "  -" + ORTHOLOG_GROUPS_CUTOFF_OPTION
649                 + "=<cutoff>    : cutoff value for ortholog groups (default: " + ORTHOLOG_GROUPS_CUTOFF_DEFAULT + ")" );
650         System.out.println( "  -" + REROOTING_OPT
651                 + "=<re-rooting>: re-rooting method for gene trees, possible values or 'none', 'midpoint'," );
652         System.out.println( "                   or 'outgroup' (default: by minizming duplications)" );
653         System.out.println( "  -" + OUTGROUP
654                 + "=<outgroup>  : for rooting by outgroup, name of outgroup (external gene tree node)" );
655         System.out.println( "  -" + USE_SDIR
656                 + "             : to use SDIR instead of GSDIR (faster, but non-binary species trees are" );
657         System.out.println( "                   disallowed, as are most options)" );
658         System.out.println( "  -" + GENE_TREES_SUFFIX_OPTION
659                 + "=<suffix>    : suffix for gene trees when operating on gene tree directories (default: "
660                 + GENE_TREES_SUFFIX_DEFAULT + ")" );
661         System.out.println( "  -" + MAPPINGS_DIR_OPTION + "=<dir>       : directory for id mapping files" );
662         System.out.println( "  -" + MAPPINGS_SUFFIX_OPTION + "=<suffix>   : suffix for id mapping files (default: "
663                 + MAPPINGS_SUFFIX_DEFAULT + ")" );
664         System.out.println();
665         System.out.println( " Formats" );
666         System.out
667                 .println( "  The gene trees, as well as the species tree, ideally are in phyloXML (www.phyloxml.org) format," );
668         System.out
669                 .println( "  but can also be in New Hamphshire (Newick) or Nexus format as long as species information can be" );
670         System.out
671                 .println( "  extracted from the gene names (e.g. \"HUMAN\" from \"BCL2_HUMAN\") and matched to a single species" );
672         System.out.println( "  in the species tree." );
673         System.out.println();
674         System.out.println( " Examples" );
675         System.out.println( "  rio -s gene_trees.nh species.xml outtable.tsv" );
676         System.out.println( "  rio gene_trees.nh species.xml outtable.tsv log.txt" );
677         System.out.println( "  rio -c=0.9 -f=10 -l=100 -r=none gene_trees.xml species.xml outtable.tsv log.txt" );
678         System.out.println( "  rio -g=.xml gene_trees_dir species.xml out_dir log.tsv" );
679         System.out.println( "  rio -g=.mlt -m=id_maps_dir -ms=.nim -c=0.8 gene_trees_dir species.xml out_dir log.tsv" );
680         System.out.println( "  rio -m=id_maps_dir -c=0.8 gene_trees_dir species.xml out_dir log.tsv" );
681         System.out.println();
682         System.exit( -1 );
683     }
684 }