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.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;
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";
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 );
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" );
108 boolean sdir = false;
109 if ( cla.isOptionSet( USE_SDIR ) ) {
111 if ( logfile != null ) {
112 ForesterUtil.fatalError( PRG_NAME, "logfile output only for GSDIR algorithm" );
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 );
120 if ( logfile != null ) {
121 System.out.println( "Logfile : " + logfile );
123 System.out.println( "Non binary species tree : allowed (GSDIR algorithm)" );
126 System.out.println( "Non binary species tree : disallowed (SDIR algorithm)" );
128 time = System.currentTimeMillis();
129 Phylogeny species_tree = null;
131 final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
132 species_tree = factory.create( species_tree_file, new PhyloXmlParser() )[ 0 ];
134 catch ( final Exception e ) {
138 if ( !species_tree.isRooted() ) {
139 ForesterUtil.fatalError( PRG_NAME, "species tree is not rooted" );
141 final ALGORITHM algorithm;
143 algorithm = ALGORITHM.SDIR;
146 algorithm = ALGORITHM.GSDIR;
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() );
153 tableOutput( othology_outtable, rio );
154 if ( ( algorithm == ALGORITHM.GSDIR ) && ( logfile != null ) ) {
155 writeLogFile( logfile, rio );
158 catch ( final RIOException e ) {
159 ForesterUtil.fatalError( PRG_NAME, e.getLocalizedMessage() );
161 catch ( final SDIException e ) {
162 ForesterUtil.fatalError( PRG_NAME, e.getLocalizedMessage() );
164 catch ( final IOException e ) {
165 ForesterUtil.fatalError( PRG_NAME, e.getLocalizedMessage() );
167 catch ( final Exception e ) {
168 ForesterUtil.unexpectedFatalError( PRG_NAME, e );
170 time = System.currentTimeMillis() - time;
171 ForesterUtil.programMessage( PRG_NAME, "time: " + time + "ms" );
172 ForesterUtil.programMessage( PRG_NAME, "OK" );
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() ) {
184 rn.add( t.getTaxonomyCode() );
188 rn.add( t.getIdentifier().toString() );
191 case SCIENTIFIC_NAME: {
192 rn.add( t.getScientificName() );
197 for( final String s : rn ) {
201 out.println( "Some information about duplication numbers in gene trees:" );
202 out.println( rio.getLog().toString() );
204 ForesterUtil.programMessage( PRG_NAME, "wrote log to \"" + logfile + "\"" );
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 );
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 ) {
218 w.print( m.getLabel( i ) );
221 for( int x = 0; x < m.size(); ++x ) {
222 w.print( m.getLabel( x ) );
223 for( int y = 0; y < m.size(); ++y ) {
226 if ( m.get( x, y ) != rio.getNumberOfSamples() ) {
227 ForesterUtil.unexpectedFatalError( PRG_NAME, "diagonal value is off" );
232 w.print( df.format( ( ( double ) m.get( x, y ) ) / rio.getNumberOfSamples() ) );
238 ForesterUtil.programMessage( PRG_NAME, "wrote table to \"" + table_outfile + "\"" );
241 private final static void printHelp() {
242 System.out.println( "Usage" );
243 System.out.println();
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." );
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();