"rio" work
[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.IOException;
33 import java.io.PrintWriter;
34 import java.util.ArrayList;
35 import java.util.List;
36
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;
46
47 public class rio {
48
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";
62
63     public static void main( final String[] args ) {
64         ForesterUtil.printProgramInformation( PRG_NAME,
65                                               "resampled inference of orthologs",
66                                               PRG_VERSION,
67                                               PRG_DATE,
68                                               E_MAIL,
69                                               WWW,
70                                               ForesterUtil.getForesterLibraryInformation() );
71         CommandLineArguments cla = null;
72         try {
73             cla = new CommandLineArguments( args );
74         }
75         catch ( final Exception e ) {
76             ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
77         }
78         if ( cla.isOptionSet( HELP_OPTION_1 ) || cla.isOptionSet( HELP_OPTION_2 ) || ( args.length == 0 ) ) {
79             printHelp();
80         }
81         if ( ( args.length < 2 ) || ( args.length > 10 ) ) {
82             System.out.println();
83             System.out.println( "[" + PRG_NAME + "] incorrect number of arguments" );
84             System.out.println();
85             printHelp();
86         }
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 );
97         }
98         final File gene_trees_file = cla.getFile( 0 );
99         final File species_tree_file = cla.getFile( 1 );
100         File outfile = null;
101         if ( cla.getNumberOfNames() > 2 ) {
102             outfile = cla.getFile( 2 );
103         }
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" );
108         }
109         String query = null;
110         if ( cla.isOptionSet( QUERY_OPTION ) ) {
111             query = cla.getOptionValue( QUERY_OPTION );
112         }
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" );
118             }
119         }
120         boolean output_ultraparalogs = false;
121         if ( cla.isOptionSet( OUTPUT_ULTRA_P_OPTION ) ) {
122             output_ultraparalogs = true;
123         }
124         double cutoff_for_orthologs = 50;
125         double cutoff_for_ultra_paralogs = 50;
126         int sort = 2;
127         try {
128             if ( cla.isOptionSet( CUTOFF_ORTHO_OPTION ) ) {
129                 cutoff_for_orthologs = cla.getOptionValueAsDouble( CUTOFF_ORTHO_OPTION );
130             }
131             if ( cla.isOptionSet( CUTOFF_ULTRA_P_OPTION ) ) {
132                 cutoff_for_ultra_paralogs = cla.getOptionValueAsDouble( CUTOFF_ULTRA_P_OPTION );
133                 if ( !output_ultraparalogs ) {
134                     printHelp();
135                 }
136             }
137             if ( cla.isOptionSet( SORT_OPTION ) ) {
138                 sort = cla.getOptionValueAsInt( SORT_OPTION );
139             }
140         }
141         catch ( final Exception e ) {
142             ForesterUtil.fatalError( PRG_NAME, "error in command line: " + e.getLocalizedMessage() );
143         }
144         if ( ( cutoff_for_orthologs < 0 ) || ( cutoff_for_ultra_paralogs < 0 ) || ( sort < 0 ) || ( sort > 2 ) ) {
145             printHelp();
146         }
147         if ( ( ( query == null ) && ( outfile != null ) ) || ( ( query != null ) && ( outfile == null ) ) ) {
148             printHelp();
149         }
150         if ( output_ultraparalogs && ( outfile == null ) ) {
151             printHelp();
152         }
153         long time = 0;
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 );
163             }
164         }
165         if ( table_outfile != null ) {
166             System.out.println( "Table output              : " + table_outfile );
167         }
168         System.out.println();
169         time = System.currentTimeMillis();
170         Phylogeny species_tree = null;
171         try {
172             final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
173             species_tree = factory.create( species_tree_file, new PhyloXmlParser() )[ 0 ];
174         }
175         catch ( final Exception e ) {
176             e.printStackTrace();
177             System.exit( -1 );
178         }
179         if ( !species_tree.isRooted() ) {
180             ForesterUtil.printErrorMessage( PRG_NAME, "species tree is not rooted" );
181             System.exit( -1 );
182         }
183         if ( !species_tree.isCompletelyBinary() ) {
184             ForesterUtil.printErrorMessage( PRG_NAME, "species tree is not completely binary" );
185             System.exit( -1 );
186         }
187         try {
188             RIO rio;
189             if ( ForesterUtil.isEmpty( query ) ) {
190                 rio = new RIO( gene_trees_file, species_tree );
191             }
192             else {
193                 rio = new RIO( gene_trees_file, species_tree, query );
194             }
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 ) );
201                 }
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 );
207                 out.close();
208             }
209             if ( table_outfile != null ) {
210                 tableOutput( table_outfile, rio );
211             }
212         }
213         catch ( final Exception e ) {
214             ForesterUtil.printErrorMessage( PRG_NAME, e.getLocalizedMessage() );
215             e.printStackTrace();
216             System.exit( -1 );
217         }
218         if ( outfile != null ) {
219             ForesterUtil.programMessage( PRG_NAME, "wrote results to \"" + outfile + "\"" );
220         }
221         time = System.currentTimeMillis() - time;
222         ForesterUtil.programMessage( PRG_NAME, "time: " + time + "ms" );
223         ForesterUtil.programMessage( PRG_NAME, "OK" );
224         System.exit( 0 );
225     }
226
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 );
230     }
231
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 );
236         w.print( "\t" );
237         for( int i = 0; i < m.size(); ++i ) {
238             w.print( "\t" );
239             w.print( m.getLabel( i ) );
240         }
241         w.println();
242         for( int x = 0; x < m.size(); ++x ) {
243             w.print( m.getLabel( x ) );
244             w.print( "\t" );
245             for( int y = 0; y < m.size(); ++y ) {
246                 w.print( "\t" );
247                 if ( x == y ) {
248                     if ( m.get( x, y ) != rio.getNumberOfSamples() ) {
249                         ForesterUtil.unexpectedFatalError( PRG_NAME, "diagonal value is off" );
250                     }
251                     w.print( "-" );
252                 }
253                 else {
254                     w.print( df.format( ( ( double ) m.get( x, y ) ) / rio.getNumberOfSamples() ) );
255                 }
256             }
257             w.println();
258         }
259         w.close();
260         ForesterUtil.programMessage( PRG_NAME, "wrote table to \"" + table_outfile + "\"" );
261     }
262
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();
270         System.out.println( " -" + CUTOFF_ORTHO_OPTION + " : cutoff for ortholog output (default: 50)" );
271         System.out.println( " -" + TABLE_OUTPUT_OPTION + "  : file-name for output table" );
272         System.out.println( " -" + QUERY_OPTION + "  : name for query (sequence/node)" );
273         System.out.println( " -" + SORT_OPTION + "  : sort (default: 2)" );
274         System.out.println( " -" + OUTPUT_ULTRA_P_OPTION
275                 + "  : to output ultra-paralogs (species specific expansions/paralogs)" );
276         System.out.println( " -" + CUTOFF_ULTRA_P_OPTION + " : cutoff for ultra-paralog output (default: 50)" );
277         System.out.println();
278         System.out.println( " sort:" );
279         System.out.println( RIO.getOrderHelp().toString() );
280         System.out.println();
281         System.out
282                 .println( " example: \"rio gene_trees.nh species.xml outfile -q=D_HUMAN -t=outtable -u -cu=60 -co=60\"" );
283         System.out.println();
284         System.exit( -1 );
285     }
286 }