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
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 // Contact: phylosoft @ gmail . com
24 // WWW: www.phylosoft.org/forester
26 package org.forester.application;
29 import java.io.IOException;
30 import java.util.ArrayList;
31 import java.util.Date;
32 import java.util.List;
34 import org.forester.io.parsers.phyloxml.PhyloXmlParser;
35 import org.forester.io.writers.PhylogenyWriter;
36 import org.forester.phylogeny.Phylogeny;
37 import org.forester.phylogeny.factories.ParserBasedPhylogenyFactory;
38 import org.forester.phylogeny.factories.PhylogenyFactory;
39 import org.forester.sdi.GSDI;
40 import org.forester.sdi.SDI;
41 import org.forester.sdi.SDIse;
42 import org.forester.util.CommandLineArguments;
43 import org.forester.util.ForesterUtil;
45 public final class sdi {
47 final static private String STRIP_OPTION = "s";
48 final static private String GSDI_OPTION = "g";
49 final static private String MOST_PARSIMONIOUS_OPTION = "m";
50 final static private String HELP_OPTION_1 = "help";
51 final static private String HELP_OPTION_2 = "h";
52 final static private String DEFAULT_OUTFILE = "sdi_out.xml";
53 final static private String PRG_NAME = "sdi";
54 final static private String PRG_VERSION = "beta 0.4";
55 final static private String PRG_DATE = "2009.01.22";
57 public static void main( final String args[] ) {
58 ForesterUtil.printProgramInformation( sdi.PRG_NAME, sdi.PRG_VERSION, sdi.PRG_DATE );
59 CommandLineArguments cla = null;
61 cla = new CommandLineArguments( args );
63 catch ( final Exception e ) {
64 ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
66 if ( cla.isOptionSet( sdi.HELP_OPTION_1 ) || cla.isOptionSet( sdi.HELP_OPTION_2 ) ) {
71 else if ( ( args.length < 2 ) || ( cla.getNumberOfNames() < 2 ) || ( cla.getNumberOfNames() > 3 ) ) {
73 System.out.println( "Wrong number of arguments." );
78 final List<String> allowed_options = new ArrayList<String>();
79 allowed_options.add( sdi.STRIP_OPTION );
80 allowed_options.add( sdi.GSDI_OPTION );
81 allowed_options.add( sdi.MOST_PARSIMONIOUS_OPTION );
82 final String dissallowed_options = cla.validateAllowedOptionsAsString( allowed_options );
83 if ( dissallowed_options.length() > 0 ) {
84 ForesterUtil.fatalError( sdi.PRG_NAME, "unknown option(s): " + dissallowed_options );
86 boolean use_sdise = true;
87 boolean strip = false;
88 boolean most_parsimonous_duplication_model = false;
89 if ( cla.isOptionSet( sdi.STRIP_OPTION ) ) {
92 if ( cla.isOptionSet( sdi.GSDI_OPTION ) ) {
95 if ( cla.isOptionSet( sdi.MOST_PARSIMONIOUS_OPTION ) ) {
97 ForesterUtil.fatalError( sdi.PRG_NAME, "Can only use most parsimonious duplication mode with GSDI" );
99 most_parsimonous_duplication_model = true;
101 Phylogeny species_tree = null;
102 Phylogeny gene_tree = null;
103 File gene_tree_file = null;
104 File species_tree_file = null;
105 File out_file = null;
107 gene_tree_file = cla.getFile( 0 );
108 species_tree_file = cla.getFile( 1 );
109 if ( cla.getNumberOfNames() == 3 ) {
110 out_file = cla.getFile( 2 );
113 out_file = new File( sdi.DEFAULT_OUTFILE );
116 catch ( final IllegalArgumentException e ) {
117 ForesterUtil.fatalError( sdi.PRG_NAME, "error in command line: " + e.getMessage() );
119 if ( ForesterUtil.isReadableFile( gene_tree_file ) != null ) {
120 ForesterUtil.fatalError( sdi.PRG_NAME, ForesterUtil.isReadableFile( gene_tree_file ) );
122 if ( ForesterUtil.isReadableFile( species_tree_file ) != null ) {
123 ForesterUtil.fatalError( sdi.PRG_NAME, ForesterUtil.isReadableFile( species_tree_file ) );
125 if ( ForesterUtil.isWritableFile( out_file ) != null ) {
126 ForesterUtil.fatalError( sdi.PRG_NAME, ForesterUtil.isWritableFile( out_file ) );
129 final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
130 species_tree = factory.create( species_tree_file, new PhyloXmlParser() )[ 0 ];
132 catch ( final IOException e ) {
133 ForesterUtil.fatalError( sdi.PRG_NAME, "Failed to read species tree from \"" + gene_tree_file + "\" ["
134 + e.getMessage() + "]" );
137 final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
138 gene_tree = factory.create( gene_tree_file, new PhyloXmlParser() )[ 0 ];
140 catch ( final IOException e ) {
141 ForesterUtil.fatalError( sdi.PRG_NAME, "Failed to read gene tree from \"" + gene_tree_file + "\" ["
142 + e.getMessage() + "]" );
144 gene_tree.setRooted( true );
145 species_tree.setRooted( true );
146 if ( !gene_tree.isCompletelyBinary() ) {
147 ForesterUtil.fatalError( sdi.PRG_NAME, "gene tree (\"" + gene_tree_file + "\") is not completely binary." );
150 if ( !species_tree.isCompletelyBinary() ) {
151 ForesterUtil.fatalError( sdi.PRG_NAME, "species tree (\"" + species_tree_file
152 + "\") is not completely binary." );
156 // gene_tree = Helper.createBalancedTree( 10 );
157 // species_tree = Helper.createBalancedTree( 13 );
158 // species_tree = Helper.createUnbalancedTree( 1024 );
159 // gene_tree = Helper.createUnbalancedTree( 8192 );
160 // species_tree = gene_tree.copyTree();
161 // gene_tree = species_tree.copyTree();
162 // Helper.numberSpeciesInOrder( species_tree );
163 // Helper.numberSpeciesInOrder( gene_tree );
164 // Helper.randomizeSpecies( 1, 8192, gene_tree );
165 // Helper.intervalNumberSpecies( gene_tree, 4096 );
166 // Helper.numberSpeciesInDescOrder( gene_tree );
167 System.out.println();
168 System.out.println( "Strip species tree: " + strip );
170 final long start_time = new Date().getTime();
173 System.out.println();
174 System.out.println( "Using SDIse algorithm." );
175 sdi = new SDIse( gene_tree, species_tree );
178 System.out.println();
179 System.out.println( "Using GSDI algorithm." );
180 System.out.println();
181 System.out.println( "Use most parsimonous duplication model: " + most_parsimonous_duplication_model );
182 sdi = new GSDI( gene_tree, species_tree, most_parsimonous_duplication_model );
185 catch ( final Exception e ) {
186 ForesterUtil.fatalError( PRG_NAME, e.getLocalizedMessage() );
188 System.out.println();
189 System.out.println( "Running time (excluding I/O): " + ( new Date().getTime() - start_time ) + "ms" );
191 final PhylogenyWriter writer = new PhylogenyWriter();
192 writer.toPhyloXML( out_file, gene_tree, 1 );
194 catch ( final IOException e ) {
195 ForesterUtil.fatalError( PRG_NAME, "Failed to write to \"" + out_file + "\" [" + e.getMessage() + "]" );
197 System.out.println();
198 System.out.println( "Successfully wrote resulting gene tree to: " + out_file );
199 System.out.println();
201 sdi.computeMappingCostL();
202 System.out.println( "Mapping cost : " + sdi.computeMappingCostL() );
204 System.out.println( "Number of duplications : " + sdi.getDuplicationsSum() );
205 if ( !use_sdise && !most_parsimonous_duplication_model ) {
206 System.out.println( "Number of potential duplications: "
207 + ( ( GSDI ) sdi ).getSpeciationOrDuplicationEventsSum() );
210 System.out.println( "Number speciations : " + ( ( GSDI ) sdi ).getSpeciationsSum() );
212 System.out.println();
213 } // main( final String args[] )
215 private static void print_help() {
216 System.out.println( "Usage: \"" + sdi.PRG_NAME
217 + " [-options] <gene tree in phyloXML format> <species tree in phyloXML format> [outfile]\"" );
218 System.out.println();
219 System.out.println( "Options:" );
220 System.out.println( " -" + sdi.STRIP_OPTION + ": to strip the species tree prior to duplication inference" );
221 System.out.println( " -" + sdi.GSDI_OPTION
222 + ": to use GSDI algorithm instead of SDIse algorithm (under development, not recommended)" );
224 .println( " -" + sdi.MOST_PARSIMONIOUS_OPTION + ": use most parimonious duplication model for GSDI: " );
225 System.out.println( " assign nodes as speciations which would otherwise be assiged" );
226 System.out.println( " as unknown because of polytomies in the species tree" );
227 System.out.println();
228 System.out.println( "Species tree:" );
229 System.out.println( " In phyloXML format, with taxonomy data in appropriate fields." );
230 System.out.println();
231 System.out.println( "Gene tree:" );
232 System.out.println( " In phyloXM format, with taxonomy and sequence data in appropriate fields." );
233 System.out.println();
235 .println( "!! WARNING: GSDI algorithm is under development (and possibly not correct), please use SDIse instead !!" );
236 System.out.println();