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.PhylogenyParser;
35 import org.forester.io.parsers.util.ParserUtils;
36 import org.forester.io.writers.PhylogenyWriter;
37 import org.forester.phylogeny.Phylogeny;
38 import org.forester.phylogeny.factories.ParserBasedPhylogenyFactory;
39 import org.forester.phylogeny.factories.PhylogenyFactory;
40 import org.forester.sdi.GSDI;
41 import org.forester.sdi.SDI;
42 import org.forester.sdi.SDIse;
43 import org.forester.util.CommandLineArguments;
44 import org.forester.util.ForesterUtil;
48 final static private String STRIP_OPTION = "s";
49 final static private String SDISE_OPTION = "b";
50 final static private String MOST_PARSIMONIOUS_OPTION = "m";
51 final static private String HELP_OPTION_1 = "help";
52 final static private String HELP_OPTION_2 = "h";
53 final static private String DEFAULT_OUTFILE = "sdi_out.xml";
54 final static private String PRG_NAME = "sdi";
55 final static private String PRG_VERSION = "alpha 0.3";
56 final static private String PRG_DATE = "2008.03.04";
58 public static void main( final String args[] ) {
59 ForesterUtil.printProgramInformation( PRG_NAME, PRG_VERSION, PRG_DATE );
60 CommandLineArguments cla = null;
62 cla = new CommandLineArguments( args );
64 catch ( final Exception e ) {
65 ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
67 if ( cla.isOptionSet( HELP_OPTION_1 ) || cla.isOptionSet( HELP_OPTION_2 ) ) {
72 else if ( ( args.length < 2 ) || ( cla.getNumberOfNames() < 2 ) || ( cla.getNumberOfNames() > 3 ) ) {
74 System.out.println( "Wrong number of arguments." );
79 final List<String> allowed_options = new ArrayList<String>();
80 allowed_options.add( STRIP_OPTION );
81 allowed_options.add( SDISE_OPTION );
82 allowed_options.add( MOST_PARSIMONIOUS_OPTION );
83 final String dissallowed_options = cla.validateAllowedOptionsAsString( allowed_options );
84 if ( dissallowed_options.length() > 0 ) {
85 ForesterUtil.fatalError( PRG_NAME, "unknown option(s): " + dissallowed_options );
87 boolean use_sdise = false;
88 boolean strip = false;
89 boolean most_parsimonous_duplication_model = false;
90 if ( cla.isOptionSet( STRIP_OPTION ) ) {
93 if ( cla.isOptionSet( SDISE_OPTION ) ) {
96 if ( cla.isOptionSet( MOST_PARSIMONIOUS_OPTION ) ) {
98 ForesterUtil.fatalError( PRG_NAME, "Can only use most parsimonious duplication mode with GSDI" );
100 most_parsimonous_duplication_model = true;
102 Phylogeny species_tree = null;
103 Phylogeny gene_tree = null;
104 File gene_tree_file = null;
105 File species_tree_file = null;
106 File out_file = null;
108 gene_tree_file = cla.getFile( 0 );
109 species_tree_file = cla.getFile( 1 );
110 if ( cla.getNumberOfNames() == 3 ) {
111 out_file = cla.getFile( 2 );
114 out_file = new File( DEFAULT_OUTFILE );
117 catch ( final IllegalArgumentException e ) {
118 ForesterUtil.fatalError( PRG_NAME, "error in command line: " + e.getMessage() );
120 if ( ForesterUtil.isReadableFile( gene_tree_file ) != null ) {
121 ForesterUtil.fatalError( PRG_NAME, ForesterUtil.isReadableFile( gene_tree_file ) );
123 if ( ForesterUtil.isReadableFile( species_tree_file ) != null ) {
124 ForesterUtil.fatalError( PRG_NAME, ForesterUtil.isReadableFile( species_tree_file ) );
126 if ( ForesterUtil.isWritableFile( out_file ) != null ) {
127 ForesterUtil.fatalError( PRG_NAME, ForesterUtil.isWritableFile( out_file ) );
130 final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
131 final PhylogenyParser pp = ParserUtils.createParserDependingOnFileType( species_tree_file, true );
132 species_tree = factory.create( species_tree_file, pp )[ 0 ];
134 catch ( final IOException e ) {
135 ForesterUtil.fatalError( PRG_NAME,
136 "Failed to read species tree from \"" + gene_tree_file + "\" [" + e.getMessage()
140 final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
141 final PhylogenyParser pp = ParserUtils.createParserDependingOnFileType( gene_tree_file, true );
142 gene_tree = factory.create( gene_tree_file, pp )[ 0 ];
144 catch ( final IOException e ) {
145 ForesterUtil.fatalError( PRG_NAME,
146 "Failed to read gene tree from \"" + gene_tree_file + "\" [" + e.getMessage()
149 gene_tree.setRooted( true );
150 species_tree.setRooted( true );
151 if ( !gene_tree.isCompletelyBinary() ) {
152 ForesterUtil.fatalError( PRG_NAME, "gene tree is not completely binary." );
155 if ( !species_tree.isCompletelyBinary() ) {
156 ForesterUtil.fatalError( PRG_NAME, "species tree is not completely binary." );
160 // gene_tree = Helper.createBalancedTree( 10 );
161 // species_tree = Helper.createBalancedTree( 13 );
162 // species_tree = Helper.createUnbalancedTree( 1024 );
163 // gene_tree = Helper.createUnbalancedTree( 8192 );
164 // species_tree = gene_tree.copyTree();
165 // gene_tree = species_tree.copyTree();
166 // Helper.numberSpeciesInOrder( species_tree );
167 // Helper.numberSpeciesInOrder( gene_tree );
168 // Helper.randomizeSpecies( 1, 8192, gene_tree );
169 // Helper.intervalNumberSpecies( gene_tree, 4096 );
170 // Helper.numberSpeciesInDescOrder( gene_tree );
171 System.out.println();
172 System.out.println( "Strip species tree: " + strip );
174 final long start_time = new Date().getTime();
177 System.out.println();
178 System.out.println( "Using SDIse algorithm." );
179 sdi = new SDIse( gene_tree, species_tree );
182 System.out.println();
183 System.out.println( "Using GSDI algorithm." );
184 System.out.println();
185 System.out.println( "Use most parsimonous duplication model: " + most_parsimonous_duplication_model );
186 sdi = new GSDI( gene_tree, species_tree, most_parsimonous_duplication_model );
189 catch ( final Exception e ) {
190 ForesterUtil.unexpectedFatalError( PRG_NAME, e );
192 System.out.println();
193 System.out.println( "Running time (excluding I/O): " + ( new Date().getTime() - start_time ) + "ms" );
195 final PhylogenyWriter writer = new PhylogenyWriter();
196 writer.toPhyloXML( out_file, gene_tree, 1 );
198 catch ( final IOException e ) {
199 ForesterUtil.fatalError( PRG_NAME, "Failed to write to \"" + out_file + "\" [" + e.getMessage() + "]" );
201 System.out.println();
202 System.out.println( "Successfully wrote resulting gene tree to: " + out_file );
203 System.out.println();
204 // if ( use_sdise ) {
205 // computeMappingCostL();
206 // System.out.println( "Mapping cost : " + computeMappingCostL() );
208 // System.out.println( "Number of duplications : " + getDuplicationsSum() );
209 if ( !use_sdise && !most_parsimonous_duplication_model ) {
210 System.out.println( "Number of potential duplications: "
211 + ( ( GSDI ) sdi ).getSpeciationOrDuplicationEventsSum() );
214 System.out.println( "Number speciations : " + ( ( GSDI ) sdi ).getSpeciationsSum() );
216 System.out.println();
217 } // main( final String args[] )
219 private static void print_help() {
220 System.out.println( "Usage: \"" + PRG_NAME
221 + " [-options] <gene tree file name> <species tree file name> [outfile name]\"" );
222 System.out.println();
223 System.out.println( "Options:" );
224 System.out.println( " -" + STRIP_OPTION + ": to strip the species tree prior to duplication inference" );
225 System.out.println( " -" + SDISE_OPTION
226 + ": to use SDIse algorithm instead of GSDI algorithm (for binary trees only, faster)" );
227 System.out.println( " -" + MOST_PARSIMONIOUS_OPTION + ": use most parimonious duplication model for GSDI: " );
228 System.out.println( " assign nodes as speciations which would otherwise be assiged" );
229 System.out.println( " as unknown because of polytomies in the species tree" );
230 System.out.println();
231 System.out.println( "Species tree file" );
232 System.out.println( " In NHX format, with species names in species name fields unless -n option" );
233 System.out.println( " is used." );
234 System.out.println();
235 System.out.println( "Gene tree file" );
236 System.out.println( " In NHX format, with species names in species name fields and sequence names" );
237 System.out.println( " in sequence name fields." );
238 System.out.println();
239 System.out.println( "!! WARNING: GSDI algorithm is under development, please use SDIse (-b) instead !!" );
240 System.out.println();