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.rio.RIO.REROOTING;
47 import org.forester.sdi.SDIException;
48 import org.forester.sdi.SDIutil.ALGORITHM;
49 import org.forester.util.BasicDescriptiveStatistics;
50 import org.forester.util.CommandLineArguments;
51 import org.forester.util.EasyWriter;
52 import org.forester.util.ForesterUtil;
56 final static private String PRG_NAME = "rio";
57 final static private String PRG_VERSION = "4.000 beta 2";
58 final static private String PRG_DATE = "2012.12.14";
59 final static private String E_MAIL = "czmasek@burnham.org";
60 final static private String WWW = "www.phylosoft.org/forester/";
61 final static private String HELP_OPTION_1 = "help";
62 final static private String HELP_OPTION_2 = "h";
63 final static private String USE_SDIR = "b";
65 public static void main( final String[] args ) {
66 ForesterUtil.printProgramInformation( PRG_NAME,
67 "resampled inference of orthologs",
72 ForesterUtil.getForesterLibraryInformation() );
73 CommandLineArguments cla = null;
75 cla = new CommandLineArguments( args );
77 catch ( final Exception e ) {
78 ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
80 if ( cla.isOptionSet( HELP_OPTION_1 ) || cla.isOptionSet( HELP_OPTION_2 ) || ( args.length == 0 ) ) {
83 if ( ( args.length < 3 ) || ( args.length > 5 ) ) {
85 System.out.println( "[" + PRG_NAME + "] incorrect number of arguments" );
89 final List<String> allowed_options = new ArrayList<String>();
90 allowed_options.add( USE_SDIR );
91 final String dissallowed_options = cla.validateAllowedOptionsAsString( allowed_options );
92 if ( dissallowed_options.length() > 0 ) {
93 ForesterUtil.fatalError( PRG_NAME, "unknown option(s): " + dissallowed_options );
95 final File gene_trees_file = cla.getFile( 0 );
96 final File species_tree_file = cla.getFile( 1 );
97 final File othology_outtable = cla.getFile( 2 );
99 if ( cla.getNumberOfNames() > 3 ) {
100 logfile = cla.getFile( 3 );
101 if ( logfile.exists() ) {
102 ForesterUtil.fatalError( PRG_NAME, "\"" + logfile + "\" already exists" );
108 String outgroup = "";
109 ForesterUtil.fatalErrorIfFileNotReadable( PRG_NAME, gene_trees_file );
110 ForesterUtil.fatalErrorIfFileNotReadable( PRG_NAME, species_tree_file );
111 if ( othology_outtable.exists() ) {
112 ForesterUtil.fatalError( PRG_NAME, "\"" + othology_outtable + "\" already exists" );
114 boolean sdir = false;
115 if ( cla.isOptionSet( USE_SDIR ) ) {
117 if ( logfile != null ) {
118 ForesterUtil.fatalError( PRG_NAME, "logfile output only for GSDIR algorithm" );
122 System.out.println( "Gene trees : " + gene_trees_file );
123 System.out.println( "Species tree : " + species_tree_file );
124 System.out.println( "All vs all orthology table: " + othology_outtable );
126 if ( logfile != null ) {
127 System.out.println( "Logfile : " + logfile );
129 System.out.println( "Non binary species tree : allowed (GSDIR algorithm)" );
132 System.out.println( "Non binary species tree : disallowed (SDIR algorithm)" );
134 time = System.currentTimeMillis();
135 Phylogeny species_tree = null;
137 final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
138 species_tree = factory.create( species_tree_file, new PhyloXmlParser() )[ 0 ];
140 catch ( final Exception e ) {
144 if ( !species_tree.isRooted() ) {
145 ForesterUtil.fatalError( PRG_NAME, "species tree is not rooted" );
147 final ALGORITHM algorithm;
149 algorithm = ALGORITHM.SDIR;
152 algorithm = ALGORITHM.GSDIR;
155 final RIO rio = new RIO( gene_trees_file, species_tree, algorithm, REROOTING.BY_ALGORITHM, outgroup , logfile != null, true );
156 if ( algorithm == ALGORITHM.GSDIR ) {
157 ForesterUtil.programMessage( PRG_NAME, "taxonomy linking based on: " + rio.getGSDIRtaxCompBase() );
159 tableOutput( othology_outtable, rio );
160 if ( ( algorithm == ALGORITHM.GSDIR ) && ( logfile != null ) ) {
161 writeLogFile( logfile, rio );
163 final BasicDescriptiveStatistics stats = rio.getDuplicationsStatistics();
164 ForesterUtil.programMessage( PRG_NAME, "Mean: " + stats.arithmeticMean() + "(" + stats.sampleStandardDeviation() + ")" );
165 ForesterUtil.programMessage( PRG_NAME, "Min: " + (int) stats.getMin() );
166 ForesterUtil.programMessage( PRG_NAME, "Max: " + (int) stats.getMax() );
169 catch ( final RIOException e ) {
170 ForesterUtil.fatalError( PRG_NAME, e.getLocalizedMessage() );
172 catch ( final SDIException e ) {
173 ForesterUtil.fatalError( PRG_NAME, e.getLocalizedMessage() );
175 catch ( final IOException e ) {
176 ForesterUtil.fatalError( PRG_NAME, e.getLocalizedMessage() );
178 catch ( final Exception e ) {
179 ForesterUtil.unexpectedFatalError( PRG_NAME, e );
182 time = System.currentTimeMillis() - time;
183 ForesterUtil.programMessage( PRG_NAME, "time: " + time + "ms" );
184 ForesterUtil.programMessage( PRG_NAME, "OK" );
188 private static void writeLogFile( final File logfile, final RIO rio ) throws IOException {
189 final EasyWriter out = ForesterUtil.createEasyWriter( logfile );
190 out.println( "Species stripped from gene trees:" );
191 final SortedSet<String> rn = new TreeSet<String>();
192 for( final PhylogenyNode n : rio.getRemovedGeneTreeNodes() ) {
193 final Taxonomy t = n.getNodeData().getTaxonomy();
194 switch ( rio.getGSDIRtaxCompBase() ) {
196 rn.add( t.getTaxonomyCode() );
200 rn.add( t.getIdentifier().toString() );
203 case SCIENTIFIC_NAME: {
204 rn.add( t.getScientificName() );
209 for( final String s : rn ) {
213 out.println( "Some information about duplication numbers in gene trees:" );
214 out.println( rio.getLog().toString() );
216 ForesterUtil.programMessage( PRG_NAME, "wrote log to \"" + logfile + "\"" );
219 private static void tableOutput( final File table_outfile, final RIO rio ) throws IOException, RIOException {
220 final IntMatrix m = RIO.calculateOrthologTable( rio.getAnalyzedGeneTrees(), true );
221 writeTable( table_outfile, rio, m );
224 private static void writeTable( final File table_outfile, final RIO rio, final IntMatrix m ) throws IOException {
225 final EasyWriter w = ForesterUtil.createEasyWriter( table_outfile );
226 final java.text.DecimalFormat df = new java.text.DecimalFormat( "0.###" );
227 df.setDecimalSeparatorAlwaysShown( false );
228 for( int i = 0; i < m.size(); ++i ) {
230 w.print( m.getLabel( i ) );
233 for( int x = 0; x < m.size(); ++x ) {
234 w.print( m.getLabel( x ) );
235 for( int y = 0; y < m.size(); ++y ) {
238 if ( m.get( x, y ) != rio.getAnalyzedGeneTrees().length ) {
239 ForesterUtil.unexpectedFatalError( PRG_NAME, "diagonal value is off" );
244 w.print( df.format( ( ( double ) m.get( x, y ) ) / rio.getAnalyzedGeneTrees().length ) );
250 ForesterUtil.programMessage( PRG_NAME, "wrote table to \"" + table_outfile + "\"" );
253 private final static void printHelp() {
254 System.out.println( "Usage" );
255 System.out.println();
258 + " [options] <gene trees infile> <species tree infile> <all vs all orthology table outfile> [logfile]" );
259 System.out.println();
260 System.out.println( " Options" );
261 System.out.println( " -" + USE_SDIR
262 + " : to use SDIR instead of GSDIR (faster, but non-binary species trees are disallowed)" );
263 System.out.println();
264 System.out.println( " Formats" );
265 System.out.println( " The species tree is expected to be in phyloXML format." );
267 .println( " The gene trees ideally are in phyloXML as well, but can also be in New Hamphshire (Newick)" );
268 System.out.println( " or Nexus format as long as species information can be extracted from the gene names" );
269 System.out.println( " (e.g. \"HUMAN\" from \"BCL2_HUMAN\")." );
270 System.out.println();
271 System.out.println( " Examples" );
272 System.out.println( " \"rio gene_trees.nh species.xml outtable.tsv log.txt\"" );
273 System.out.println();
274 System.out.println( " More information: http://code.google.com/p/forester/wiki/RIO" );
275 System.out.println();