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