18fa912639be2dc3c927295092ac3d7ff06ef07c
[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) 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
9 // All rights reserved
10 //
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.
15 //
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.
20 //
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
24 //
25 // Contact: phylosoft @ gmail . com
26 // WWW: www.phylosoft.org/forester
27
28 package org.forester.application;
29
30 import java.io.File;
31 import java.io.FileWriter;
32 import java.io.PrintWriter;
33 import java.util.ArrayList;
34 import java.util.List;
35
36 import org.forester.io.parsers.phyloxml.PhyloXmlParser;
37 import org.forester.phylogeny.Phylogeny;
38 import org.forester.phylogeny.factories.ParserBasedPhylogenyFactory;
39 import org.forester.phylogeny.factories.PhylogenyFactory;
40 import org.forester.sdi.RIO;
41 import org.forester.util.CommandLineArguments;
42 import org.forester.util.ForesterUtil;
43
44 public class rio {
45
46     final static private String PRG_NAME              = "rio";
47     final static private String PRG_VERSION           = "3.00 beta 1";
48     final static private String PRG_DATE              = "2010.01.15";
49     final static private String E_MAIL                = "czmasek@burnham.org";
50     final static private String WWW                   = "www.phylosoft.org/forester/";
51     final static private String HELP_OPTION_1         = "help";
52     final static private String HELP_OPTION_2         = "h";
53     final static private String QUERY_OPTION          = "q";
54     final static private String SORT_OPTION           = "s";
55     final static private String OUTPUT_ULTRA_P_OPTION = "u";
56     final static private String CUTOFF_ULTRA_P_OPTION = "cu";
57     final static private String CUTOFF_ORTHO_OPTION   = "co";
58     final static private String TABLE_OUTPUT_OPTION   = "t";
59
60     public static void main( final String[] args ) {
61         ForesterUtil.printProgramInformation( PRG_NAME,
62                                               "resampled inference of orthologs",
63                                               PRG_VERSION,
64                                               PRG_DATE,
65                                               E_MAIL,
66                                               WWW,
67                                               ForesterUtil.getForesterLibraryInformation() );
68         CommandLineArguments cla = null;
69         try {
70             cla = new CommandLineArguments( args );
71         }
72         catch ( final Exception e ) {
73             ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
74         }
75         if ( cla.isOptionSet( HELP_OPTION_1 ) || cla.isOptionSet( HELP_OPTION_2 ) || ( args.length == 0 ) ) {
76             printHelp();
77             System.exit( 0 );
78         }
79         if ( ( args.length < 3 ) || ( args.length > 10 ) ) {
80             System.out.println();
81             System.out.println( "[" + PRG_NAME + "] incorrect number of arguments" );
82             System.out.println();
83             printHelp();
84             System.exit( -1 );
85         }
86         final List<String> allowed_options = new ArrayList<String>();
87         allowed_options.add( QUERY_OPTION );
88         allowed_options.add( SORT_OPTION );
89         allowed_options.add( CUTOFF_ULTRA_P_OPTION );
90         allowed_options.add( CUTOFF_ORTHO_OPTION );
91         allowed_options.add( TABLE_OUTPUT_OPTION );
92         allowed_options.add( OUTPUT_ULTRA_P_OPTION );
93         final String dissallowed_options = cla.validateAllowedOptionsAsString( allowed_options );
94         if ( dissallowed_options.length() > 0 ) {
95             ForesterUtil.fatalError( PRG_NAME, "unknown option(s): " + dissallowed_options );
96         }
97         final File multiple_trees_file = cla.getFile( 0 );
98         final File species_tree_file = cla.getFile( 1 );
99         final File outfile = cla.getFile( 2 );
100         ForesterUtil.fatalErrorIfFileNotReadable( PRG_NAME, multiple_trees_file );
101         ForesterUtil.fatalErrorIfFileNotReadable( PRG_NAME, species_tree_file );
102         if ( outfile.exists() ) {
103             ForesterUtil.fatalError( PRG_NAME, "[" + outfile + "] already exists" );
104         }
105         String seq_name = null;
106         if ( cla.isOptionSet( QUERY_OPTION ) ) {
107             seq_name = cla.getOptionValue( QUERY_OPTION );
108         }
109         File table_outfile = null;
110         if ( cla.isOptionSet( TABLE_OUTPUT_OPTION ) ) {
111             table_outfile = new File( cla.getOptionValue( TABLE_OUTPUT_OPTION ) );
112             if ( table_outfile.exists() ) {
113                 ForesterUtil.fatalError( PRG_NAME, "[" + outfile + "] already exists" );
114             }
115         }
116         boolean output_ultraparalogs = false;
117         if ( cla.isOptionSet( OUTPUT_ULTRA_P_OPTION ) ) {
118             output_ultraparalogs = true;
119         }
120         double t_orthologs = 0.0;
121         double threshold_ultra_paralogs = 0.0;
122         int sort = 2;
123         try {
124             if ( cla.isOptionSet( CUTOFF_ORTHO_OPTION ) ) {
125                 t_orthologs = cla.getOptionValueAsDouble( CUTOFF_ORTHO_OPTION );
126             }
127             if ( cla.isOptionSet( CUTOFF_ULTRA_P_OPTION ) ) {
128                 threshold_ultra_paralogs = cla.getOptionValueAsDouble( CUTOFF_ULTRA_P_OPTION );
129             }
130             if ( cla.isOptionSet( SORT_OPTION ) ) {
131                 sort = cla.getOptionValueAsInt( SORT_OPTION );
132             }
133         }
134         catch ( final Exception e ) {
135             ForesterUtil.fatalError( PRG_NAME, "error in command line: " + e.getLocalizedMessage() );
136         }
137         if ( sort < 0 ) {
138             sort = 0;
139         }
140         else if ( sort > 2 ) {
141             sort = 2;
142         }
143         long time = 0;
144         System.out.println( "\n" );
145         System.out.println( "Gene trees:                              " + multiple_trees_file );
146         System.out.println( "Species tree:                            " + species_tree_file );
147         System.out.println( "Query:                                     " + seq_name );
148         System.out.println( "Outfile:                                      " + outfile );
149         System.out.println( "Outfile:                                      " + table_outfile );
150         System.out.println( "Sort:                                         " + sort );
151         System.out.println( "Threshold orthologs:                          " + t_orthologs );
152         if ( output_ultraparalogs ) {
153             System.out.println( "Threshold ultra paralogs:                     " + threshold_ultra_paralogs );
154         }
155         time = System.currentTimeMillis();
156         Phylogeny species_tree = null;
157         try {
158             final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
159             species_tree = factory.create( species_tree_file, new PhyloXmlParser() )[ 0 ];
160         }
161         catch ( final Exception e ) {
162             e.printStackTrace();
163             System.exit( -1 );
164         }
165         if ( !species_tree.isRooted() ) {
166             ForesterUtil.printErrorMessage( PRG_NAME, "Species tree is not rooted" );
167             System.exit( -1 );
168         }
169         if ( !species_tree.isCompletelyBinary() ) {
170             ForesterUtil.printErrorMessage( PRG_NAME, "Species tree is not completely binary" );
171             System.exit( -1 );
172         }
173         final RIO rio_instance = new RIO();
174         final StringBuffer output = new StringBuffer();
175         PrintWriter out = null;
176         try {
177             rio_instance.inferOrthologs( multiple_trees_file, species_tree.copy(), seq_name );
178             output.append( rio_instance.inferredOrthologsToString( seq_name, sort, t_orthologs ) );
179             if ( output_ultraparalogs ) {
180                 output.append( "\n\nUltra paralogs:\n" );
181                 output.append( rio_instance.inferredUltraParalogsToString( seq_name, threshold_ultra_paralogs ) );
182             }
183             output.append( "\n\nSort priority: " + RIO.getOrder( sort ) );
184             output.append( "\nExt nodes    : " + rio_instance.getExtNodesOfAnalyzedGeneTrees() );
185             output.append( "\nSamples      : " + rio_instance.getNumberOfSamples() + "\n" );
186             out = new PrintWriter( new FileWriter( outfile ), true );
187         }
188         catch ( final Exception e ) {
189             ForesterUtil.printErrorMessage( PRG_NAME, e.getLocalizedMessage() );
190             e.printStackTrace();
191             System.exit( -1 );
192         }
193         out.println( output );
194         out.close();
195         ForesterUtil.programMessage( PRG_NAME, "wrote results to \"" + outfile + "\"" );
196         time = System.currentTimeMillis() - time;
197         ForesterUtil.programMessage( PRG_NAME, "time: " + time + "ms" );
198         ForesterUtil.programMessage( PRG_NAME, "OK." );
199         System.exit( 0 );
200     }
201
202     private final static void printHelp() {
203         System.out.println( "Usage:" );
204         System.out.println();
205         System.out.println( PRG_NAME + " [options] <gene trees file> <species tree file> [outfile]" );
206         System.out.println();
207         System.out.println( "options:" );
208         System.out.println();
209         //        System.out.println( " -" + STRICT_OPTION
210         //                + "    : strict [default: non-strict]: all nodes between 'target' and 'evaluators' must match" );
211         //        System.out.println( " -" + NORMALIZE_OPTION
212         //                + "=<d>: normalize to this value (e.g. 100 for most bootstrap analyses) [default: no normalization]" );
213         //        System.out.println( " -" + FIRST_OPTION + "=<i>: first evaluator topology to use (0-based) [default: 0]" );
214         //        System.out.println( " -" + LAST_OPTION
215         //                + "=<i>: last evaluator topology to use (0-based) [default: use all until final topology]" );
216         //        System.out.println();
217         //        System.out.println( "M= (String) Multiple gene tree file (mandatory)" );
218         //        System.out.println( "N= (String) Query sequence name (mandatory)" );
219         //        System.out.println( "S= (String) Species tree file (mandatory)" );
220         //        System.out.println( "O= (String) Output file name -- overwritten without warning! (mandatory)" );
221         //        System.out.println( "P= (int)    Sort priority" );
222         //        System.out.println( "L= (double) Threshold orthologs for output" );
223         //        System.out.println( " Sort priority (\"P=\"):" );
224         System.out.println( RIO.getOrderHelp().toString() );
225         System.out.println();
226         System.out
227                 .println( " Example: \"rio -q=D_NEMVE -s=1 -t=out -u Bcl-2_e1_20_mafft_05_40_fme.mlt species.xml out\"" );
228         System.out.println();
229     }
230 }