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.sdi.RIO;
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 = "3.00 beta 1";
51 final static private String PRG_DATE = "2012.11.27";
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 QUERY_OPTION = "q";
57 final static private String SORT_OPTION = "s";
58 final static private String OUTPUT_ULTRA_P_OPTION = "u";
59 final static private String CUTOFF_ULTRA_P_OPTION = "cu";
60 final static private String CUTOFF_ORTHO_OPTION = "co";
61 final static private String TABLE_OUTPUT_OPTION = "t";
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( PRG_NAME, e.getMessage() );
78 if ( cla.isOptionSet( HELP_OPTION_1 ) || cla.isOptionSet( HELP_OPTION_2 ) || ( args.length == 0 ) ) {
81 if ( ( args.length < 2 ) || ( args.length > 10 ) ) {
83 System.out.println( "[" + PRG_NAME + "] incorrect number of arguments" );
87 final List<String> allowed_options = new ArrayList<String>();
88 allowed_options.add( QUERY_OPTION );
89 allowed_options.add( SORT_OPTION );
90 allowed_options.add( CUTOFF_ULTRA_P_OPTION );
91 allowed_options.add( CUTOFF_ORTHO_OPTION );
92 allowed_options.add( TABLE_OUTPUT_OPTION );
93 allowed_options.add( OUTPUT_ULTRA_P_OPTION );
94 final String dissallowed_options = cla.validateAllowedOptionsAsString( allowed_options );
95 if ( dissallowed_options.length() > 0 ) {
96 ForesterUtil.fatalError( PRG_NAME, "unknown option(s): " + dissallowed_options );
98 final File gene_trees_file = cla.getFile( 0 );
99 final File species_tree_file = cla.getFile( 1 );
101 if ( cla.getNumberOfNames() > 2 ) {
102 outfile = cla.getFile( 2 );
104 ForesterUtil.fatalErrorIfFileNotReadable( PRG_NAME, gene_trees_file );
105 ForesterUtil.fatalErrorIfFileNotReadable( PRG_NAME, species_tree_file );
106 if ( outfile.exists() ) {
107 ForesterUtil.fatalError( PRG_NAME, "[" + outfile + "] already exists" );
110 if ( cla.isOptionSet( QUERY_OPTION ) ) {
111 query = cla.getOptionValue( QUERY_OPTION );
113 File table_outfile = null;
114 if ( cla.isOptionSet( TABLE_OUTPUT_OPTION ) ) {
115 table_outfile = new File( cla.getOptionValue( TABLE_OUTPUT_OPTION ) );
116 if ( table_outfile.exists() ) {
117 ForesterUtil.fatalError( PRG_NAME, "[" + table_outfile + "] already exists" );
120 boolean output_ultraparalogs = false;
121 if ( cla.isOptionSet( OUTPUT_ULTRA_P_OPTION ) ) {
122 output_ultraparalogs = true;
124 double cutoff_for_orthologs = 50;
125 double cutoff_for_ultra_paralogs = 50;
128 if ( cla.isOptionSet( CUTOFF_ORTHO_OPTION ) ) {
129 cutoff_for_orthologs = cla.getOptionValueAsDouble( CUTOFF_ORTHO_OPTION );
131 if ( cla.isOptionSet( CUTOFF_ULTRA_P_OPTION ) ) {
132 cutoff_for_ultra_paralogs = cla.getOptionValueAsDouble( CUTOFF_ULTRA_P_OPTION );
133 if ( !output_ultraparalogs ) {
137 if ( cla.isOptionSet( SORT_OPTION ) ) {
138 sort = cla.getOptionValueAsInt( SORT_OPTION );
141 catch ( final Exception e ) {
142 ForesterUtil.fatalError( PRG_NAME, "error in command line: " + e.getLocalizedMessage() );
144 if ( ( cutoff_for_orthologs < 0 ) || ( cutoff_for_ultra_paralogs < 0 ) || ( sort < 0 ) || ( sort > 2 ) ) {
147 if ( ( ( query == null ) && ( outfile != null ) ) || ( ( query != null ) && ( outfile == null ) ) ) {
150 if ( output_ultraparalogs && ( outfile == null ) ) {
154 System.out.println( "Gene trees : " + gene_trees_file );
155 System.out.println( "Species tree : " + species_tree_file );
156 if ( query != null ) {
157 System.out.println( "Query : " + query );
158 System.out.println( "Outfile : " + outfile );
159 System.out.println( "Sort : " + sort );
160 System.out.println( "Cutoff for orthologs : " + cutoff_for_orthologs );
161 if ( output_ultraparalogs ) {
162 System.out.println( "Cutoff for ultra paralogs : " + cutoff_for_ultra_paralogs );
165 if ( table_outfile != null ) {
166 System.out.println( "Table output : " + table_outfile );
168 System.out.println();
169 time = System.currentTimeMillis();
170 Phylogeny species_tree = null;
172 final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
173 species_tree = factory.create( species_tree_file, new PhyloXmlParser() )[ 0 ];
175 catch ( final Exception e ) {
179 if ( !species_tree.isRooted() ) {
180 ForesterUtil.printErrorMessage( PRG_NAME, "species tree is not rooted" );
183 if ( !species_tree.isCompletelyBinary() ) {
184 ForesterUtil.printErrorMessage( PRG_NAME, "species tree is not completely binary" );
189 if ( ForesterUtil.isEmpty( query ) ) {
190 rio = new RIO( gene_trees_file, species_tree );
193 rio = new RIO( gene_trees_file, species_tree, query );
195 if ( outfile != null ) {
196 final StringBuilder output = new StringBuilder();
197 output.append( rio.inferredOrthologsToString( query, sort, cutoff_for_orthologs ) );
198 if ( output_ultraparalogs ) {
199 output.append( "\n\nUltra paralogs:\n" );
200 output.append( rio.inferredUltraParalogsToString( query, cutoff_for_ultra_paralogs ) );
202 output.append( "\n\nSort priority: " + RIO.getOrder( sort ) );
203 output.append( "\nExt nodes : " + rio.getExtNodesOfAnalyzedGeneTrees() );
204 output.append( "\nSamples : " + rio.getNumberOfSamples() + "\n" );
205 final PrintWriter out = new PrintWriter( new FileWriter( outfile ), true );
206 out.println( output );
209 if ( table_outfile != null ) {
210 tableOutput( table_outfile, rio );
213 catch ( final Exception e ) {
214 ForesterUtil.printErrorMessage( PRG_NAME, e.getLocalizedMessage() );
218 if ( outfile != null ) {
219 ForesterUtil.programMessage( PRG_NAME, "wrote results to \"" + outfile + "\"" );
221 time = System.currentTimeMillis() - time;
222 ForesterUtil.programMessage( PRG_NAME, "time: " + time + "ms" );
223 ForesterUtil.programMessage( PRG_NAME, "OK" );
227 private static void tableOutput( final File table_outfile, final RIO rio ) throws IOException {
228 final IntMatrix m = RIO.calculateOrthologTable( rio.getAnalyzedGeneTrees() );
229 writeTable( table_outfile, rio, m );
232 private static void writeTable( final File table_outfile, final RIO rio, final IntMatrix m ) throws IOException {
233 final EasyWriter w = ForesterUtil.createEasyWriter( table_outfile );
234 final java.text.DecimalFormat df = new java.text.DecimalFormat( "0.###" );
235 df.setDecimalSeparatorAlwaysShown( false );
237 for( int i = 0; i < m.size(); ++i ) {
239 w.print( m.getLabel( i ) );
242 for( int x = 0; x < m.size(); ++x ) {
243 w.print( m.getLabel( x ) );
245 for( int y = 0; y < m.size(); ++y ) {
248 if ( m.get( x, y ) != rio.getNumberOfSamples() ) {
249 ForesterUtil.unexpectedFatalError( PRG_NAME, "diagonal value is off" );
254 w.print( df.format( ( ( double ) m.get( x, y ) ) / rio.getNumberOfSamples() ) );
260 ForesterUtil.programMessage( PRG_NAME, "wrote table to \"" + table_outfile + "\"" );
263 private final static void printHelp() {
264 System.out.println( "Usage" );
265 System.out.println();
266 System.out.println( PRG_NAME + " [options] <gene trees file> <species tree file> [outfile]" );
267 System.out.println();
268 System.out.println( " Options" );
269 System.out.println( " -" + CUTOFF_ORTHO_OPTION + " : cutoff for ortholog output (default: 50)" );
270 System.out.println( " -" + TABLE_OUTPUT_OPTION + " : file-name for output table" );
271 System.out.println( " -" + QUERY_OPTION + " : name for query (sequence/node)" );
272 System.out.println( " -" + SORT_OPTION + " : sort (default: 2)" );
273 System.out.println( " -" + OUTPUT_ULTRA_P_OPTION
274 + " : to output ultra-paralogs (species specific expansions/paralogs)" );
275 System.out.println( " -" + CUTOFF_ULTRA_P_OPTION + " : cutoff for ultra-paralog output (default: 50)" );
276 System.out.println();
277 System.out.println( " Sort" );
278 System.out.println( RIO.getOrderHelp().toString() );
279 System.out.println( " Formats" );
280 System.out.println( " The species tree is expected to be in phyloXML format." );
282 .println( " The gene trees ideally are in phyloXML as well, but can also be in New Hamphshire (Newick)" );
283 System.out.println( " or Nexus format as long as species information can be extracted from the gene names" );
284 System.out.println( " (e.g. \"HUMAN\" from \"BCL2_HUMAN\")." );
285 System.out.println();
286 System.out.println( " Examples" );
287 System.out.println( " \"rio gene_trees.nh species.xml outfile -q=BCL2_HUMAN -t=outtable -u -cu=60 -co=60\"" );
288 System.out.println( " \"rio gene_trees.nh species.xml -t=outtable\"" );
289 System.out.println();