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: www.phylosoft.org/forester
28 package org.forester.application;
31 import java.io.FileWriter;
32 import java.io.IOException;
33 import java.io.PrintWriter;
34 import java.util.ArrayList;
35 import java.util.List;
37 import org.forester.datastructures.IntMatrix;
38 import org.forester.io.parsers.phyloxml.PhyloXmlParser;
39 import org.forester.phylogeny.Phylogeny;
40 import org.forester.phylogeny.factories.ParserBasedPhylogenyFactory;
41 import org.forester.phylogeny.factories.PhylogenyFactory;
42 import org.forester.rio.RIO;
43 import org.forester.rio.RIOException;
44 import org.forester.sdi.SDI;
45 import org.forester.sdi.SDI.ALGORITHM;
46 import org.forester.sdi.SDIException;
47 import org.forester.util.CommandLineArguments;
48 import org.forester.util.EasyWriter;
49 import org.forester.util.ForesterUtil;
53 final static private String PRG_NAME = "rio";
54 final static private String PRG_VERSION = "3.00 beta 4";
55 final static private String PRG_DATE = "2012.12.10";
56 final static private String E_MAIL = "czmasek@burnham.org";
57 final static private String WWW = "www.phylosoft.org/forester/";
58 final static private String HELP_OPTION_1 = "help";
59 final static private String HELP_OPTION_2 = "h";
60 final static private String QUERY_OPTION = "q";
61 final static private String SORT_OPTION = "s";
62 final static private String ALLOW_NON_BIN_SPECIES_TREE_OPTION = "g";
63 final static private String OUTPUT_ULTRA_P_OPTION = "u";
64 final static private String CUTOFF_ULTRA_P_OPTION = "cu";
65 final static private String CUTOFF_ORTHO_OPTION = "co";
66 final static private String TABLE_OUTPUT_OPTION = "t";
68 public static void main( final String[] args ) {
69 ForesterUtil.printProgramInformation( PRG_NAME,
70 "resampled inference of orthologs",
75 ForesterUtil.getForesterLibraryInformation() );
76 CommandLineArguments cla = null;
78 cla = new CommandLineArguments( args );
80 catch ( final Exception e ) {
81 ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
83 if ( cla.isOptionSet( HELP_OPTION_1 ) || cla.isOptionSet( HELP_OPTION_2 ) || ( args.length == 0 ) ) {
86 if ( ( args.length < 2 ) || ( args.length > 10 ) ) {
88 System.out.println( "[" + PRG_NAME + "] incorrect number of arguments" );
92 final List<String> allowed_options = new ArrayList<String>();
93 allowed_options.add( QUERY_OPTION );
94 allowed_options.add( SORT_OPTION );
95 allowed_options.add( CUTOFF_ULTRA_P_OPTION );
96 allowed_options.add( CUTOFF_ORTHO_OPTION );
97 allowed_options.add( TABLE_OUTPUT_OPTION );
98 allowed_options.add( OUTPUT_ULTRA_P_OPTION );
99 allowed_options.add( ALLOW_NON_BIN_SPECIES_TREE_OPTION );
100 final String dissallowed_options = cla.validateAllowedOptionsAsString( allowed_options );
101 if ( dissallowed_options.length() > 0 ) {
102 ForesterUtil.fatalError( PRG_NAME, "unknown option(s): " + dissallowed_options );
104 final File gene_trees_file = cla.getFile( 0 );
105 final File species_tree_file = cla.getFile( 1 );
107 if ( cla.getNumberOfNames() > 2 ) {
108 outfile = cla.getFile( 2 );
110 ForesterUtil.fatalErrorIfFileNotReadable( PRG_NAME, gene_trees_file );
111 ForesterUtil.fatalErrorIfFileNotReadable( PRG_NAME, species_tree_file );
112 if ( ( outfile != null ) && outfile.exists() ) {
113 ForesterUtil.fatalError( PRG_NAME, "[" + outfile + "] already exists" );
116 if ( cla.isOptionSet( QUERY_OPTION ) ) {
117 query = cla.getOptionValue( QUERY_OPTION );
119 File table_outfile = null;
120 if ( cla.isOptionSet( TABLE_OUTPUT_OPTION ) ) {
121 table_outfile = new File( cla.getOptionValue( TABLE_OUTPUT_OPTION ) );
122 if ( table_outfile.exists() ) {
123 ForesterUtil.fatalError( PRG_NAME, "[" + table_outfile + "] already exists" );
126 boolean output_ultraparalogs = false;
127 if ( cla.isOptionSet( OUTPUT_ULTRA_P_OPTION ) ) {
128 output_ultraparalogs = true;
130 boolean gsdir = false;
131 if ( cla.isOptionSet( ALLOW_NON_BIN_SPECIES_TREE_OPTION ) ) {
134 double cutoff_for_orthologs = 50;
135 double cutoff_for_ultra_paralogs = 50;
138 if ( cla.isOptionSet( CUTOFF_ORTHO_OPTION ) ) {
139 cutoff_for_orthologs = cla.getOptionValueAsDouble( CUTOFF_ORTHO_OPTION );
140 if ( query == null ) {
141 ForesterUtil.fatalError( PRG_NAME, "missing query name, type \"rio -h\" for help" );
143 if ( outfile == null ) {
144 ForesterUtil.fatalError( PRG_NAME, "missing outfile, type \"rio -h\" for help" );
147 if ( cla.isOptionSet( CUTOFF_ULTRA_P_OPTION ) ) {
148 cutoff_for_ultra_paralogs = cla.getOptionValueAsDouble( CUTOFF_ULTRA_P_OPTION );
149 output_ultraparalogs = true;
151 if ( cla.isOptionSet( SORT_OPTION ) ) {
152 sort = cla.getOptionValueAsInt( SORT_OPTION );
155 catch ( final Exception e ) {
156 ForesterUtil.fatalError( PRG_NAME, "error in command line: " + e.getLocalizedMessage() );
158 if ( ( cutoff_for_orthologs < 0 ) || ( cutoff_for_ultra_paralogs < 0 ) || ( sort < 0 ) || ( sort > 2 ) ) {
159 ForesterUtil.fatalError( PRG_NAME, "numberical option out of range, type \"rio -h\" for help" );
161 if ( ( ( query == null ) && ( ( outfile != null ) || output_ultraparalogs ) ) ) {
162 ForesterUtil.fatalError( PRG_NAME, "missing query name, type \"rio -h\" for help" );
164 if ( ( output_ultraparalogs && ( outfile == null ) ) || ( ( query != null ) && ( outfile == null ) ) ) {
165 ForesterUtil.fatalError( PRG_NAME, "missing outfile, type \"rio -h\" for help" );
168 System.out.println( "Gene trees : " + gene_trees_file );
169 System.out.println( "Species tree : " + species_tree_file );
171 System.out.println( "Non binary species tree : allowed (GSDIR algorithm)" );
174 System.out.println( "Non binary species tree : disallowed (SDIR algorithm)" );
176 if ( query != null ) {
177 System.out.println( "Query : " + query );
178 System.out.println( "Outfile : " + outfile );
179 System.out.println( "Sort : " + sort );
180 System.out.println( "Cutoff for orthologs : " + cutoff_for_orthologs );
181 if ( output_ultraparalogs ) {
182 System.out.println( "Cutoff for ultra paralogs : " + cutoff_for_ultra_paralogs );
185 if ( table_outfile != null ) {
186 System.out.println( "Table output : " + table_outfile );
188 System.out.println();
189 time = System.currentTimeMillis();
190 Phylogeny species_tree = null;
192 final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
193 species_tree = factory.create( species_tree_file, new PhyloXmlParser() )[ 0 ];
195 catch ( final Exception e ) {
199 if ( !species_tree.isRooted() ) {
200 ForesterUtil.fatalError( PRG_NAME, "species tree is not rooted" );
202 final SDI.ALGORITHM algorithm;
204 algorithm = ALGORITHM.GSDIR;
207 algorithm = ALGORITHM.SDIR;
211 if ( ForesterUtil.isEmpty( query ) ) {
212 rio = new RIO( gene_trees_file, species_tree, algorithm );
215 rio = new RIO( gene_trees_file, species_tree, query, algorithm );
217 if ( outfile != null ) {
218 final StringBuilder output = new StringBuilder();
219 output.append( rio.inferredOrthologsToString( query, sort, cutoff_for_orthologs ) );
220 if ( output_ultraparalogs ) {
221 output.append( "\n\nUltra paralogs:\n" );
222 output.append( rio.inferredUltraParalogsToString( query, cutoff_for_ultra_paralogs ) );
224 output.append( "\n\nSort priority: " + RIO.getOrder( sort ) );
225 output.append( "\nExt nodes : " + rio.getExtNodesOfAnalyzedGeneTrees() );
226 output.append( "\nSamples : " + rio.getNumberOfSamples() + "\n" );
227 final PrintWriter out = new PrintWriter( new FileWriter( outfile ), true );
228 out.println( output );
231 if ( table_outfile != null ) {
232 tableOutput( table_outfile, rio );
235 catch ( final RIOException e ) {
236 ForesterUtil.fatalError( PRG_NAME, e.getLocalizedMessage() );
238 catch ( final SDIException e ) {
239 ForesterUtil.fatalError( PRG_NAME, e.getLocalizedMessage() );
241 catch ( final IOException e ) {
242 ForesterUtil.fatalError( PRG_NAME, e.getLocalizedMessage() );
244 catch ( final Exception e ) {
245 ForesterUtil.unexpectedFatalError( PRG_NAME, e );
247 if ( outfile != null ) {
248 ForesterUtil.programMessage( PRG_NAME, "wrote results to \"" + outfile + "\"" );
250 time = System.currentTimeMillis() - time;
251 ForesterUtil.programMessage( PRG_NAME, "time: " + time + "ms" );
252 ForesterUtil.programMessage( PRG_NAME, "OK" );
256 private static void tableOutput( final File table_outfile, final RIO rio ) throws IOException, RIOException {
257 final IntMatrix m = RIO.calculateOrthologTable( rio.getAnalyzedGeneTrees(), true );
258 writeTable( table_outfile, rio, m );
261 private static void writeTable( final File table_outfile, final RIO rio, final IntMatrix m ) throws IOException {
262 final EasyWriter w = ForesterUtil.createEasyWriter( table_outfile );
263 final java.text.DecimalFormat df = new java.text.DecimalFormat( "0.###" );
264 df.setDecimalSeparatorAlwaysShown( false );
265 for( int i = 0; i < m.size(); ++i ) {
267 w.print( m.getLabel( i ) );
270 for( int x = 0; x < m.size(); ++x ) {
271 w.print( m.getLabel( x ) );
272 for( int y = 0; y < m.size(); ++y ) {
275 if ( m.get( x, y ) != rio.getNumberOfSamples() ) {
276 ForesterUtil.unexpectedFatalError( PRG_NAME, "diagonal value is off" );
281 w.print( df.format( ( ( double ) m.get( x, y ) ) / rio.getNumberOfSamples() ) );
287 ForesterUtil.programMessage( PRG_NAME, "wrote table to \"" + table_outfile + "\"" );
290 private final static void printHelp() {
291 System.out.println( "Usage" );
292 System.out.println();
293 System.out.println( PRG_NAME + " [options] <gene trees file> <species tree file> [outfile]" );
294 System.out.println();
295 System.out.println( " Options" );
296 System.out.println( " -" + ALLOW_NON_BIN_SPECIES_TREE_OPTION + " : to allow non-binary species tree" );
297 System.out.println( " -" + CUTOFF_ORTHO_OPTION + " : cutoff for ortholog output (default: 50)" );
298 System.out.println( " -" + TABLE_OUTPUT_OPTION
299 + " : file-name for output table of all vs. all ortholgy support" );
300 System.out.println( " -" + QUERY_OPTION
301 + " : name for query (sequence/node), if this is used, [outfile] is required as well" );
302 System.out.println( " -" + SORT_OPTION + " : sort (default: 1)" );
303 System.out.println( " -" + OUTPUT_ULTRA_P_OPTION
304 + " : to output ultra-paralogs (species specific expansions/paralogs)" );
305 System.out.println( " -" + CUTOFF_ULTRA_P_OPTION + " : cutoff for ultra-paralog output (default: 50)" );
306 System.out.println();
307 System.out.println( " Note" );
308 System.out.println( " Either output of all vs. all ortholgy support with -t=<output table> and/or output for" );
309 System.out.println( " one query sequence with -q=<query name> and a [outfile] are required." );
310 System.out.println();
311 System.out.println( " Sort" );
312 System.out.println( RIO.getOrderHelp().toString() );
313 System.out.println( " Formats" );
314 System.out.println( " The species tree is expected to be in phyloXML format." );
316 .println( " The gene trees ideally are in phyloXML as well, but can also be in New Hamphshire (Newick)" );
317 System.out.println( " or Nexus format as long as species information can be extracted from the gene names" );
318 System.out.println( " (e.g. \"HUMAN\" from \"BCL2_HUMAN\")." );
319 System.out.println();
320 System.out.println( " Examples" );
321 System.out.println( " \"rio gene_trees.nh species.xml outfile -q=BCL2_HUMAN -t=outtable -u -cu=60 -co=60\"" );
322 System.out.println( " \"rio gene_trees.nh species.xml -t=outtable\"" );
323 System.out.println();
324 System.out.println( " More information: http://code.google.com/p/forester/wiki/RIO" );
325 System.out.println();