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.IOException;
32 import java.util.ArrayList;
33 import java.util.List;
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;
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";
59 public static void main( final String[] args ) {
60 ForesterUtil.printProgramInformation( PRG_NAME,
61 "resampled inference of orthologs",
66 ForesterUtil.getForesterLibraryInformation() );
67 CommandLineArguments cla = null;
69 cla = new CommandLineArguments( args );
71 catch ( final Exception e ) {
72 ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
74 if ( cla.isOptionSet( HELP_OPTION_1 ) || cla.isOptionSet( HELP_OPTION_2 ) || ( args.length == 0 ) ) {
77 if ( ( args.length < 2 ) || ( args.length > 10 ) ) {
79 System.out.println( "[" + PRG_NAME + "] incorrect number of arguments" );
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 );
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 );
93 if ( cla.getNumberOfNames() > 3 ) {
94 logfile = cla.getFile( 3 );
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" );
104 boolean sdir = false;
105 if ( cla.isOptionSet( USE_SDIR ) ) {
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 );
113 System.out.println( "Non binary species tree : allowed (GSDIR algorithm)" );
116 System.out.println( "Non binary species tree : disallowed (SDIR algorithm)" );
118 System.out.println();
119 time = System.currentTimeMillis();
120 Phylogeny species_tree = null;
122 final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
123 species_tree = factory.create( species_tree_file, new PhyloXmlParser() )[ 0 ];
125 catch ( final Exception e ) {
129 if ( !species_tree.isRooted() ) {
130 ForesterUtil.fatalError( PRG_NAME, "species tree is not rooted" );
132 final ALGORITHM algorithm;
134 algorithm = ALGORITHM.SDIR;
137 algorithm = ALGORITHM.GSDIR;
140 final RIO rio = new RIO( gene_trees_file, species_tree, algorithm );
141 tableOutput( othology_outtable, rio );
143 catch ( final RIOException e ) {
144 ForesterUtil.fatalError( PRG_NAME, e.getLocalizedMessage() );
146 catch ( final SDIException e ) {
147 ForesterUtil.fatalError( PRG_NAME, e.getLocalizedMessage() );
149 catch ( final IOException e ) {
150 ForesterUtil.fatalError( PRG_NAME, e.getLocalizedMessage() );
152 catch ( final Exception e ) {
153 ForesterUtil.unexpectedFatalError( PRG_NAME, e );
155 if ( othology_outtable != null ) {
156 ForesterUtil.programMessage( PRG_NAME, "wrote results to \"" + othology_outtable + "\"" );
158 time = System.currentTimeMillis() - time;
159 ForesterUtil.programMessage( PRG_NAME, "time: " + time + "ms" );
160 ForesterUtil.programMessage( PRG_NAME, "OK" );
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 );
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 ) {
175 w.print( m.getLabel( i ) );
178 for( int x = 0; x < m.size(); ++x ) {
179 w.print( m.getLabel( x ) );
180 for( int y = 0; y < m.size(); ++y ) {
183 if ( m.get( x, y ) != rio.getNumberOfSamples() ) {
184 ForesterUtil.unexpectedFatalError( PRG_NAME, "diagonal value is off" );
189 w.print( df.format( ( ( double ) m.get( x, y ) ) / rio.getNumberOfSamples() ) );
195 ForesterUtil.programMessage( PRG_NAME, "wrote table to \"" + table_outfile + "\"" );
198 private final static void printHelp() {
199 System.out.println( "Usage" );
200 System.out.println();
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." );
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();