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;
34 import java.util.SortedSet;
35 import java.util.TreeSet;
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.SDIException;
47 import org.forester.sdi.SDIutil.ALGORITHM;
48 import org.forester.util.CommandLineArguments;
49 import org.forester.util.EasyWriter;
50 import org.forester.util.ForesterUtil;
54 final static private String PRG_NAME = "rio";
55 final static private String PRG_VERSION = "4.000 beta 2";
56 final static private String PRG_DATE = "2012.12.14";
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";
63 public static void main( final String[] args ) {
64 ForesterUtil.printProgramInformation( PRG_NAME,
65 "resampled inference of orthologs",
70 ForesterUtil.getForesterLibraryInformation() );
71 CommandLineArguments cla = null;
73 cla = new CommandLineArguments( args );
75 catch ( final Exception e ) {
76 ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
78 if ( cla.isOptionSet( HELP_OPTION_1 ) || cla.isOptionSet( HELP_OPTION_2 ) || ( args.length == 0 ) ) {
81 if ( ( args.length < 3 ) || ( args.length > 5 ) ) {
83 System.out.println( "[" + PRG_NAME + "] incorrect number of arguments" );
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 );
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 );
97 if ( cla.getNumberOfNames() > 3 ) {
98 logfile = cla.getFile( 3 );
99 if ( logfile.exists() ) {
100 ForesterUtil.fatalError( PRG_NAME, "\"" + logfile + "\" already exists" );
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" );
111 boolean sdir = false;
112 if ( cla.isOptionSet( USE_SDIR ) ) {
114 if ( logfile != null ) {
115 ForesterUtil.fatalError( PRG_NAME, "logfile output only for GSDIR algorithm" );
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 );
123 if ( logfile != null ) {
124 System.out.println( "Logfile : " + logfile );
126 System.out.println( "Non binary species tree : allowed (GSDIR algorithm)" );
129 System.out.println( "Non binary species tree : disallowed (SDIR algorithm)" );
131 time = System.currentTimeMillis();
132 Phylogeny species_tree = null;
134 final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
135 species_tree = factory.create( species_tree_file, new PhyloXmlParser() )[ 0 ];
137 catch ( final Exception e ) {
141 if ( !species_tree.isRooted() ) {
142 ForesterUtil.fatalError( PRG_NAME, "species tree is not rooted" );
144 final ALGORITHM algorithm;
146 algorithm = ALGORITHM.SDIR;
149 algorithm = ALGORITHM.GSDIR;
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() );
156 tableOutput( othology_outtable, rio );
157 if ( ( algorithm == ALGORITHM.GSDIR ) && ( logfile != null ) ) {
158 writeLogFile( logfile, rio );
161 catch ( final RIOException e ) {
162 ForesterUtil.fatalError( PRG_NAME, e.getLocalizedMessage() );
164 catch ( final SDIException e ) {
165 ForesterUtil.fatalError( PRG_NAME, e.getLocalizedMessage() );
167 catch ( final IOException e ) {
168 ForesterUtil.fatalError( PRG_NAME, e.getLocalizedMessage() );
170 catch ( final Exception e ) {
171 ForesterUtil.unexpectedFatalError( PRG_NAME, e );
173 time = System.currentTimeMillis() - time;
174 ForesterUtil.programMessage( PRG_NAME, "time: " + time + "ms" );
175 ForesterUtil.programMessage( PRG_NAME, "OK" );
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() ) {
187 rn.add( t.getTaxonomyCode() );
191 rn.add( t.getIdentifier().toString() );
194 case SCIENTIFIC_NAME: {
195 rn.add( t.getScientificName() );
200 for( final String s : rn ) {
204 out.println( "Some information about duplication numbers in gene trees:" );
205 out.println( rio.getLog().toString() );
207 ForesterUtil.programMessage( PRG_NAME, "wrote log to \"" + logfile + "\"" );
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 );
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 ) {
221 w.print( m.getLabel( i ) );
224 for( int x = 0; x < m.size(); ++x ) {
225 w.print( m.getLabel( x ) );
226 for( int y = 0; y < m.size(); ++y ) {
229 if ( m.get( x, y ) != rio.getAnalyzedGeneTrees().length ) {
230 ForesterUtil.unexpectedFatalError( PRG_NAME, "diagonal value is off" );
235 w.print( df.format( ( ( double ) m.get( x, y ) ) / rio.getAnalyzedGeneTrees().length ) );
241 ForesterUtil.programMessage( PRG_NAME, "wrote table to \"" + table_outfile + "\"" );
244 private final static void printHelp() {
245 System.out.println( "Usage" );
246 System.out.println();
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." );
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();