"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
35 import org.forester.datastructures.IntMatrix;
36 import org.forester.io.parsers.phyloxml.PhyloXmlParser;
37 import org.forester.phylogeny.Phylogeny;
38 import org.forester.phylogeny.factories.ParserBasedPhylogenyFactory;
39 import org.forester.phylogeny.factories.PhylogenyFactory;
40 import org.forester.rio.RIO;
41 import org.forester.rio.RIOException;
42 import org.forester.sdi.SDI.ALGORITHM;
43 import org.forester.sdi.SDIException;
44 import org.forester.util.CommandLineArguments;
45 import org.forester.util.EasyWriter;
46 import org.forester.util.ForesterUtil;
47
48 public class rio {
49
50     final static private String PRG_NAME      = "rio";
51     final static private String PRG_VERSION   = "3.00 beta 4";
52     final static private String PRG_DATE      = "2012.12.10";
53     final static private String E_MAIL        = "czmasek@burnham.org";
54     final static private String WWW           = "www.phylosoft.org/forester/";
55     final static private String HELP_OPTION_1 = "help";
56     final static private String HELP_OPTION_2 = "h";
57     final static private String USE_SDIR      = "b";
58
59     public static void main( final String[] args ) {
60         ForesterUtil.printProgramInformation( PRG_NAME,
61                                               "resampled inference of orthologs",
62                                               PRG_VERSION,
63                                               PRG_DATE,
64                                               E_MAIL,
65                                               WWW,
66                                               ForesterUtil.getForesterLibraryInformation() );
67         CommandLineArguments cla = null;
68         try {
69             cla = new CommandLineArguments( args );
70         }
71         catch ( final Exception e ) {
72             ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
73         }
74         if ( cla.isOptionSet( HELP_OPTION_1 ) || cla.isOptionSet( HELP_OPTION_2 ) || ( args.length == 0 ) ) {
75             printHelp();
76         }
77         if ( ( args.length < 2 ) || ( args.length > 10 ) ) {
78             System.out.println();
79             System.out.println( "[" + PRG_NAME + "] incorrect number of arguments" );
80             System.out.println();
81             printHelp();
82         }
83         final List<String> allowed_options = new ArrayList<String>();
84         allowed_options.add( USE_SDIR );
85         final String dissallowed_options = cla.validateAllowedOptionsAsString( allowed_options );
86         if ( dissallowed_options.length() > 0 ) {
87             ForesterUtil.fatalError( PRG_NAME, "unknown option(s): " + dissallowed_options );
88         }
89         final File gene_trees_file = cla.getFile( 0 );
90         final File species_tree_file = cla.getFile( 1 );
91         final File othology_outtable = cla.getFile( 2 );
92         final File logfile;
93         if ( cla.getNumberOfNames() > 3 ) {
94             logfile = cla.getFile( 3 );
95         }
96         else {
97             logfile = null;
98         }
99         ForesterUtil.fatalErrorIfFileNotReadable( PRG_NAME, gene_trees_file );
100         ForesterUtil.fatalErrorIfFileNotReadable( PRG_NAME, species_tree_file );
101         if ( othology_outtable.exists() ) {
102             ForesterUtil.fatalError( PRG_NAME, "\"" + othology_outtable + "\" already exists" );
103         }
104         boolean sdir = false;
105         if ( cla.isOptionSet( USE_SDIR ) ) {
106             sdir = true;
107         }
108         long time = 0;
109         System.out.println( "Gene trees                : " + gene_trees_file );
110         System.out.println( "Species tree              : " + species_tree_file );
111         System.out.println( "All vs all orthology table: " + othology_outtable );
112         if ( !sdir ) {
113             System.out.println( "Non binary species tree   : allowed (GSDIR algorithm)" );
114         }
115         else {
116             System.out.println( "Non binary species tree   : disallowed (SDIR algorithm)" );
117         }
118         System.out.println();
119         time = System.currentTimeMillis();
120         Phylogeny species_tree = null;
121         try {
122             final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
123             species_tree = factory.create( species_tree_file, new PhyloXmlParser() )[ 0 ];
124         }
125         catch ( final Exception e ) {
126             e.printStackTrace();
127             System.exit( -1 );
128         }
129         if ( !species_tree.isRooted() ) {
130             ForesterUtil.fatalError( PRG_NAME, "species tree is not rooted" );
131         }
132         final ALGORITHM algorithm;
133         if ( sdir ) {
134             algorithm = ALGORITHM.SDIR;
135         }
136         else {
137             algorithm = ALGORITHM.GSDIR;
138         }
139         try {
140             final RIO rio = new RIO( gene_trees_file, species_tree, algorithm );
141             tableOutput( othology_outtable, rio );
142         }
143         catch ( final RIOException e ) {
144             ForesterUtil.fatalError( PRG_NAME, e.getLocalizedMessage() );
145         }
146         catch ( final SDIException e ) {
147             ForesterUtil.fatalError( PRG_NAME, e.getLocalizedMessage() );
148         }
149         catch ( final IOException e ) {
150             ForesterUtil.fatalError( PRG_NAME, e.getLocalizedMessage() );
151         }
152         catch ( final Exception e ) {
153             ForesterUtil.unexpectedFatalError( PRG_NAME, e );
154         }
155         if ( othology_outtable != null ) {
156             ForesterUtil.programMessage( PRG_NAME, "wrote results to \"" + othology_outtable + "\"" );
157         }
158         time = System.currentTimeMillis() - time;
159         ForesterUtil.programMessage( PRG_NAME, "time: " + time + "ms" );
160         ForesterUtil.programMessage( PRG_NAME, "OK" );
161         System.exit( 0 );
162     }
163
164     private static void tableOutput( final File table_outfile, final RIO rio ) throws IOException, RIOException {
165         final IntMatrix m = RIO.calculateOrthologTable( rio.getAnalyzedGeneTrees(), true );
166         writeTable( table_outfile, rio, m );
167     }
168
169     private static void writeTable( final File table_outfile, final RIO rio, final IntMatrix m ) throws IOException {
170         final EasyWriter w = ForesterUtil.createEasyWriter( table_outfile );
171         final java.text.DecimalFormat df = new java.text.DecimalFormat( "0.###" );
172         df.setDecimalSeparatorAlwaysShown( false );
173         for( int i = 0; i < m.size(); ++i ) {
174             w.print( "\t" );
175             w.print( m.getLabel( i ) );
176         }
177         w.println();
178         for( int x = 0; x < m.size(); ++x ) {
179             w.print( m.getLabel( x ) );
180             for( int y = 0; y < m.size(); ++y ) {
181                 w.print( "\t" );
182                 if ( x == y ) {
183                     if ( m.get( x, y ) != rio.getNumberOfSamples() ) {
184                         ForesterUtil.unexpectedFatalError( PRG_NAME, "diagonal value is off" );
185                     }
186                     w.print( "-" );
187                 }
188                 else {
189                     w.print( df.format( ( ( double ) m.get( x, y ) ) / rio.getNumberOfSamples() ) );
190                 }
191             }
192             w.println();
193         }
194         w.close();
195         ForesterUtil.programMessage( PRG_NAME, "wrote table to \"" + table_outfile + "\"" );
196     }
197
198     private final static void printHelp() {
199         System.out.println( "Usage" );
200         System.out.println();
201         System.out
202                 .println( PRG_NAME
203                         + " [options] <gene trees infile> <species tree infile> <all vs all orthology table outfile> [logfile]" );
204         System.out.println();
205         System.out.println( " Options" );
206         System.out.println( "  -" + USE_SDIR
207                 + "  : to use SDIR instead of GSDIR (faster, but non-binary species trees are disallowed)" );
208         System.out.println();
209         System.out.println( " Formats" );
210         System.out.println( "  The species tree is expected to be in phyloXML format." );
211         System.out
212                 .println( "  The gene trees ideally are in phyloXML as well, but can also be in New Hamphshire (Newick)" );
213         System.out.println( "  or Nexus format as long as species information can be extracted from the gene names" );
214         System.out.println( "  (e.g. \"HUMAN\" from \"BCL2_HUMAN\")." );
215         System.out.println();
216         System.out.println( " Examples" );
217         System.out.println( "  \"rio gene_trees.nh species.xml outtable.tsv log.txt\"" );
218         System.out.println();
219         System.out.println( " More information: http://code.google.com/p/forester/wiki/RIO" );
220         System.out.println();
221         System.exit( -1 );
222     }
223 }