"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.IOException;
32 import java.util.ArrayList;
33 import java.util.List;
34 import java.util.SortedSet;
35 import java.util.TreeSet;
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.PhylogenyNode;
41 import org.forester.phylogeny.data.Taxonomy;
42 import org.forester.phylogeny.factories.ParserBasedPhylogenyFactory;
43 import org.forester.phylogeny.factories.PhylogenyFactory;
44 import org.forester.rio.RIO;
45 import org.forester.rio.RIOException;
46 import org.forester.sdi.SDI.ALGORITHM;
47 import org.forester.sdi.SDIException;
48 import org.forester.util.CommandLineArguments;
49 import org.forester.util.EasyWriter;
50 import org.forester.util.ForesterUtil;
51
52 public class rio {
53
54     final static private String PRG_NAME      = "rio";
55     final static private String PRG_VERSION   = "4.000 beta 1";
56     final static private String PRG_DATE      = "2012.12.11";
57     final static private String E_MAIL        = "czmasek@burnham.org";
58     final static private String WWW           = "www.phylosoft.org/forester/";
59     final static private String HELP_OPTION_1 = "help";
60     final static private String HELP_OPTION_2 = "h";
61     final static private String USE_SDIR      = "b";
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 < 3 ) || ( args.length > 5 ) ) {
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( USE_SDIR );
89         final String dissallowed_options = cla.validateAllowedOptionsAsString( allowed_options );
90         if ( dissallowed_options.length() > 0 ) {
91             ForesterUtil.fatalError( PRG_NAME, "unknown option(s): " + dissallowed_options );
92         }
93         final File gene_trees_file = cla.getFile( 0 );
94         final File species_tree_file = cla.getFile( 1 );
95         final File othology_outtable = cla.getFile( 2 );
96         final File logfile;
97         if ( cla.getNumberOfNames() > 3 ) {
98             logfile = cla.getFile( 3 );
99         }
100         else {
101             logfile = null;
102         }
103         ForesterUtil.fatalErrorIfFileNotReadable( PRG_NAME, gene_trees_file );
104         ForesterUtil.fatalErrorIfFileNotReadable( PRG_NAME, species_tree_file );
105         if ( othology_outtable.exists() ) {
106             ForesterUtil.fatalError( PRG_NAME, "\"" + othology_outtable + "\" already exists" );
107         }
108         boolean sdir = false;
109         if ( cla.isOptionSet( USE_SDIR ) ) {
110             sdir = true;
111             if ( logfile != null ) {
112                 ForesterUtil.fatalError( PRG_NAME, "logfile output only for GSDIR algorithm" );
113             }
114         }
115         long time = 0;
116         System.out.println( "Gene trees                : " + gene_trees_file );
117         System.out.println( "Species tree              : " + species_tree_file );
118         System.out.println( "All vs all orthology table: " + othology_outtable );
119         if ( !sdir ) {
120             if ( logfile != null ) {
121                 System.out.println( "Logfile                   : " + logfile );
122             }
123             System.out.println( "Non binary species tree   : allowed (GSDIR algorithm)" );
124         }
125         else {
126             System.out.println( "Non binary species tree   : disallowed (SDIR algorithm)" );
127         }
128         time = System.currentTimeMillis();
129         Phylogeny species_tree = null;
130         try {
131             final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
132             species_tree = factory.create( species_tree_file, new PhyloXmlParser() )[ 0 ];
133         }
134         catch ( final Exception e ) {
135             e.printStackTrace();
136             System.exit( -1 );
137         }
138         if ( !species_tree.isRooted() ) {
139             ForesterUtil.fatalError( PRG_NAME, "species tree is not rooted" );
140         }
141         final ALGORITHM algorithm;
142         if ( sdir ) {
143             algorithm = ALGORITHM.SDIR;
144         }
145         else {
146             algorithm = ALGORITHM.GSDIR;
147         }
148         try {
149             final RIO rio = new RIO( gene_trees_file, species_tree, algorithm, logfile != null );
150             if ( algorithm == ALGORITHM.GSDIR ) {
151                 System.out.println( "Taxonomy linking based on : " + rio.getGSDIRtaxCompBase() );
152             }
153             tableOutput( othology_outtable, rio );
154             if ( ( algorithm == ALGORITHM.GSDIR ) && ( logfile != null ) ) {
155                 writeLogFile( logfile, rio );
156             }
157         }
158         catch ( final RIOException e ) {
159             ForesterUtil.fatalError( PRG_NAME, e.getLocalizedMessage() );
160         }
161         catch ( final SDIException e ) {
162             ForesterUtil.fatalError( PRG_NAME, e.getLocalizedMessage() );
163         }
164         catch ( final IOException e ) {
165             ForesterUtil.fatalError( PRG_NAME, e.getLocalizedMessage() );
166         }
167         catch ( final Exception e ) {
168             ForesterUtil.unexpectedFatalError( PRG_NAME, e );
169         }
170         time = System.currentTimeMillis() - time;
171         ForesterUtil.programMessage( PRG_NAME, "time: " + time + "ms" );
172         ForesterUtil.programMessage( PRG_NAME, "OK" );
173         System.exit( 0 );
174     }
175
176     private static void writeLogFile( final File logfile, final RIO rio ) throws IOException {
177         final EasyWriter out = ForesterUtil.createEasyWriter( logfile );
178         out.println( "Species stripped from gene trees:" );
179         final SortedSet<String> rn = new TreeSet<String>();
180         for( final PhylogenyNode n : rio.getRemovedGeneTreeNodes() ) {
181             final Taxonomy t = n.getNodeData().getTaxonomy();
182             switch ( rio.getGSDIRtaxCompBase() ) {
183                 case CODE: {
184                     rn.add( t.getTaxonomyCode() );
185                     break;
186                 }
187                 case ID: {
188                     rn.add( t.getIdentifier().toString() );
189                     break;
190                 }
191                 case SCIENTIFIC_NAME: {
192                     rn.add( t.getScientificName() );
193                     break;
194                 }
195             }
196         }
197         for( final String s : rn ) {
198             out.println( s );
199         }
200         out.println();
201         out.println( "Some information about duplication numbers in gene trees:" );
202         out.println( rio.getLog().toString() );
203         out.close();
204         ForesterUtil.programMessage( PRG_NAME, "wrote log to \"" + logfile + "\"" );
205     }
206
207     private static void tableOutput( final File table_outfile, final RIO rio ) throws IOException, RIOException {
208         final IntMatrix m = RIO.calculateOrthologTable( rio.getAnalyzedGeneTrees(), true );
209         writeTable( table_outfile, rio, m );
210     }
211
212     private static void writeTable( final File table_outfile, final RIO rio, final IntMatrix m ) throws IOException {
213         final EasyWriter w = ForesterUtil.createEasyWriter( table_outfile );
214         final java.text.DecimalFormat df = new java.text.DecimalFormat( "0.###" );
215         df.setDecimalSeparatorAlwaysShown( false );
216         for( int i = 0; i < m.size(); ++i ) {
217             w.print( "\t" );
218             w.print( m.getLabel( i ) );
219         }
220         w.println();
221         for( int x = 0; x < m.size(); ++x ) {
222             w.print( m.getLabel( x ) );
223             for( int y = 0; y < m.size(); ++y ) {
224                 w.print( "\t" );
225                 if ( x == y ) {
226                     if ( m.get( x, y ) != rio.getNumberOfSamples() ) {
227                         ForesterUtil.unexpectedFatalError( PRG_NAME, "diagonal value is off" );
228                     }
229                     w.print( "-" );
230                 }
231                 else {
232                     w.print( df.format( ( ( double ) m.get( x, y ) ) / rio.getNumberOfSamples() ) );
233                 }
234             }
235             w.println();
236         }
237         w.close();
238         ForesterUtil.programMessage( PRG_NAME, "wrote table to \"" + table_outfile + "\"" );
239     }
240
241     private final static void printHelp() {
242         System.out.println( "Usage" );
243         System.out.println();
244         System.out
245                 .println( PRG_NAME
246                         + " [options] <gene trees infile> <species tree infile> <all vs all orthology table outfile> [logfile]" );
247         System.out.println();
248         System.out.println( " Options" );
249         System.out.println( "  -" + USE_SDIR
250                 + " : to use SDIR instead of GSDIR (faster, but non-binary species trees are disallowed)" );
251         System.out.println();
252         System.out.println( " Formats" );
253         System.out.println( "  The species tree is expected to be in phyloXML format." );
254         System.out
255                 .println( "  The gene trees ideally are in phyloXML as well, but can also be in New Hamphshire (Newick)" );
256         System.out.println( "  or Nexus format as long as species information can be extracted from the gene names" );
257         System.out.println( "  (e.g. \"HUMAN\" from \"BCL2_HUMAN\")." );
258         System.out.println();
259         System.out.println( " Examples" );
260         System.out.println( "  \"rio gene_trees.nh species.xml outtable.tsv log.txt\"" );
261         System.out.println();
262         System.out.println( " More information: http://code.google.com/p/forester/wiki/RIO" );
263         System.out.println();
264         System.exit( -1 );
265     }
266 }