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.text.SimpleDateFormat;
31 import java.util.ArrayList;
32 import java.util.Date;
33 import java.util.List;
35 import org.forester.io.parsers.PhylogenyParser;
36 import org.forester.io.parsers.nhx.NHXParser;
37 import org.forester.io.parsers.phyloxml.PhyloXmlParser;
38 import org.forester.io.parsers.util.ParserUtils;
39 import org.forester.io.writers.PhylogenyWriter;
40 import org.forester.phylogeny.Phylogeny;
41 import org.forester.phylogeny.PhylogenyMethods;
42 import org.forester.phylogeny.PhylogenyNode;
43 import org.forester.phylogeny.factories.ParserBasedPhylogenyFactory;
44 import org.forester.phylogeny.factories.PhylogenyFactory;
45 import org.forester.sdi.GSDI;
46 import org.forester.sdi.SDI;
47 import org.forester.sdi.SDI.TaxonomyComparisonBase;
48 import org.forester.sdi.SDIse;
49 import org.forester.sdi.SdiException;
50 import org.forester.util.CommandLineArguments;
51 import org.forester.util.EasyWriter;
52 import org.forester.util.ForesterConstants;
53 import org.forester.util.ForesterUtil;
55 public final class gsdi {
57 private enum BASE_ALGORITHM {
60 final static public boolean REPLACE_UNDERSCORES_IN_NH_SPECIES_TREE = true;
61 final static private String ALLOW_STRIPPING_OF_GENE_TREE_OPTION = "g";
62 final static private String SDI_OPTION = "b";
63 final static private String MOST_PARSIMONIOUS_OPTION = "m";
64 final static private String GUESS_FORMAT_OF_SPECIES_TREE = "q";
65 final static private String HELP_OPTION_1 = "help";
66 final static private String HELP_OPTION_2 = "h";
67 final static private String DEFAULT_OUTFILE_SUFFIX = "_gsdi_out.xml";
68 final static private String SUFFIX_FOR_LIST_OF_STIPPED_GENE_TREE_NODES = "_stripped_gene_tree_nodes.txt";
69 final static private String SUFFIX_FOR_SPECIES_TREE_USED = "_species_tree_used.xml";
70 final static private String LOGFILE_SUFFIX = "_gsdi_log.txt";
71 final static private String PRG_NAME = "gsdi";
72 final static private String PRG_VERSION = "0.901";
73 final static private String PRG_DATE = "120608";
74 final static private String PRG_DESC = "general speciation duplication inference";
75 final static private String E_MAIL = "phylosoft@gmail.com";
76 final static private String WWW = "www.phylosoft.org/forester";
78 public static void main( final String args[] ) {
80 ForesterUtil.printProgramInformation( PRG_NAME,
86 ForesterUtil.getForesterLibraryInformation() );
87 CommandLineArguments cla = null;
89 cla = new CommandLineArguments( args );
91 catch ( final Exception e ) {
92 ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
94 if ( cla.isOptionSet( gsdi.HELP_OPTION_1 ) || cla.isOptionSet( gsdi.HELP_OPTION_2 ) ) {
99 else if ( ( args.length < 2 ) || ( cla.getNumberOfNames() < 2 ) || ( cla.getNumberOfNames() > 3 ) ) {
100 System.out.println();
101 System.out.println( "Wrong number of arguments." );
102 System.out.println();
106 final List<String> allowed_options = new ArrayList<String>();
107 allowed_options.add( gsdi.SDI_OPTION );
108 allowed_options.add( gsdi.GUESS_FORMAT_OF_SPECIES_TREE );
109 allowed_options.add( gsdi.MOST_PARSIMONIOUS_OPTION );
110 allowed_options.add( gsdi.ALLOW_STRIPPING_OF_GENE_TREE_OPTION );
111 final String dissallowed_options = cla.validateAllowedOptionsAsString( allowed_options );
112 if ( dissallowed_options.length() > 0 ) {
113 ForesterUtil.fatalError( gsdi.PRG_NAME, "unknown option(s): " + dissallowed_options );
117 catch ( final IOException e ) {
118 ForesterUtil.fatalError( gsdi.PRG_NAME, e.getMessage() );
122 private static void execute( final CommandLineArguments cla ) throws IOException {
123 BASE_ALGORITHM base_algorithm = BASE_ALGORITHM.GSDI;
124 boolean most_parsimonous_duplication_model = false;
125 boolean species_tree_in_phyloxml = true;
126 boolean allow_stripping_of_gene_tree = false;
127 if ( cla.isOptionSet( gsdi.SDI_OPTION ) ) {
128 base_algorithm = BASE_ALGORITHM.SDI;
130 if ( cla.isOptionSet( gsdi.MOST_PARSIMONIOUS_OPTION ) ) {
131 if ( base_algorithm != BASE_ALGORITHM.GSDI ) {
132 ForesterUtil.fatalError( gsdi.PRG_NAME, "Can only use most parsimonious duplication mode with GSDI" );
134 most_parsimonous_duplication_model = true;
136 if ( cla.isOptionSet( gsdi.GUESS_FORMAT_OF_SPECIES_TREE ) ) {
137 species_tree_in_phyloxml = false;
139 if ( cla.isOptionSet( gsdi.ALLOW_STRIPPING_OF_GENE_TREE_OPTION ) ) {
140 if ( base_algorithm != BASE_ALGORITHM.GSDI ) {
141 ForesterUtil.fatalError( gsdi.PRG_NAME, "Can only allow stripping of gene tree with GSDI" );
143 allow_stripping_of_gene_tree = true;
145 Phylogeny species_tree = null;
146 Phylogeny gene_tree = null;
147 File gene_tree_file = null;
148 File species_tree_file = null;
149 File out_file = null;
150 File log_file = null;
151 EasyWriter log_writer = null;
153 gene_tree_file = cla.getFile( 0 );
154 species_tree_file = cla.getFile( 1 );
155 if ( cla.getNumberOfNames() == 3 ) {
156 out_file = cla.getFile( 2 );
159 out_file = new File( ForesterUtil.removeSuffix( gene_tree_file.toString() ) + DEFAULT_OUTFILE_SUFFIX );
161 log_file = new File( ForesterUtil.removeSuffix( out_file.toString() ) + LOGFILE_SUFFIX );
163 catch ( final IllegalArgumentException e ) {
164 ForesterUtil.fatalError( gsdi.PRG_NAME, "error in command line: " + e.getMessage() );
166 if ( ForesterUtil.isReadableFile( gene_tree_file ) != null ) {
167 ForesterUtil.fatalError( gsdi.PRG_NAME, ForesterUtil.isReadableFile( gene_tree_file ) );
169 if ( ForesterUtil.isReadableFile( species_tree_file ) != null ) {
170 ForesterUtil.fatalError( gsdi.PRG_NAME, ForesterUtil.isReadableFile( species_tree_file ) );
172 if ( ForesterUtil.isWritableFile( out_file ) != null ) {
173 ForesterUtil.fatalError( gsdi.PRG_NAME, ForesterUtil.isWritableFile( out_file ) );
175 if ( ForesterUtil.isWritableFile( log_file ) != null ) {
176 ForesterUtil.fatalError( gsdi.PRG_NAME, ForesterUtil.isWritableFile( log_file ) );
179 log_writer = ForesterUtil.createEasyWriter( log_file );
181 catch ( final IOException e ) {
182 ForesterUtil.fatalError( gsdi.PRG_NAME, "Failed to create [" + log_file + "]: " + e.getMessage() );
185 final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
186 gene_tree = factory.create( gene_tree_file, new PhyloXmlParser() )[ 0 ];
188 catch ( final IOException e ) {
189 ForesterUtil.fatalError( gsdi.PRG_NAME,
190 "Failed to read gene tree from [" + gene_tree_file + "]: " + e.getMessage() );
193 final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
194 if ( species_tree_in_phyloxml ) {
195 species_tree = factory.create( species_tree_file, new PhyloXmlParser() )[ 0 ];
198 final PhylogenyParser p = ParserUtils.createParserDependingOnFileType( species_tree_file, true );
199 if ( REPLACE_UNDERSCORES_IN_NH_SPECIES_TREE && ( p instanceof NHXParser ) ) {
200 ( ( NHXParser ) p ).setReplaceUnderscores( true );
202 species_tree = factory.create( species_tree_file, p )[ 0 ];
203 final TaxonomyComparisonBase comp_base = GSDI.determineTaxonomyComparisonBase( gene_tree );
204 switch ( comp_base ) {
205 case SCIENTIFIC_NAME:
207 .transferNodeNameToField( species_tree,
208 PhylogenyMethods.PhylogenyNodeField.TAXONOMY_SCIENTIFIC_NAME,
212 PhylogenyMethods.transferNodeNameToField( species_tree,
213 PhylogenyMethods.PhylogenyNodeField.TAXONOMY_CODE,
217 PhylogenyMethods.transferNodeNameToField( species_tree,
218 PhylogenyMethods.PhylogenyNodeField.TAXONOMY_ID,
222 ForesterUtil.fatalError( gsdi.PRG_NAME, "unable to determine comparison base" );
226 catch ( final IOException e ) {
227 ForesterUtil.fatalError( gsdi.PRG_NAME,
228 "Failed to read species tree from [" + gene_tree_file + "]: " + e.getMessage() );
230 gene_tree.setRooted( true );
231 species_tree.setRooted( true );
232 if ( !gene_tree.isCompletelyBinary() ) {
233 log_writer.println( "User Error: gene tree is not completely binary" );
235 ForesterUtil.fatalError( gsdi.PRG_NAME, "gene tree is not completely binary" );
237 if ( base_algorithm != BASE_ALGORITHM.GSDI ) {
238 if ( !species_tree.isCompletelyBinary() ) {
239 log_writer.println( "User Error: species tree is not completely binary, use GSDI instead" );
241 ForesterUtil.fatalError( gsdi.PRG_NAME, "species tree is not completely binary, use GSDI instead" );
245 // gene_tree = Helper.createBalancedTree( 10 );
246 // species_tree = Helper.createBalancedTree( 13 );
247 // species_tree = Helper.createUnbalancedTree( 1024 );
248 // gene_tree = Helper.createUnbalancedTree( 8192 );
249 // species_tree = gene_tree.copyTree();
250 // gene_tree = species_tree.copyTree();
251 // Helper.numberSpeciesInOrder( species_tree );
252 // Helper.numberSpeciesInOrder( gene_tree );
253 // Helper.randomizeSpecies( 1, 8192, gene_tree );
254 // Helper.intervalNumberSpecies( gene_tree, 4096 );
255 // Helper.numberSpeciesInDescOrder( gene_tree );
256 log_writer.println( PRG_NAME + " - " + PRG_DESC );
257 log_writer.println( " version : " + PRG_VERSION );
258 log_writer.println( " date : " + PRG_DATE );
259 log_writer.println( " forester version: " + ForesterConstants.FORESTER_VERSION );
260 log_writer.println( "Start time: " + new SimpleDateFormat( "yyyyMMdd HH:mm:ss" ).format( new Date() ) );
261 log_writer.println( "Gene tree file: " + gene_tree_file.getCanonicalPath() );
262 log_writer.println( "Gene tree name: "
263 + ( ForesterUtil.isEmpty( gene_tree.getName() ) ? "" : gene_tree.getName() ) );
264 log_writer.println( "Species tree file: " + species_tree_file.getCanonicalPath() );
265 log_writer.println( "Species tree name: "
266 + ( ForesterUtil.isEmpty( species_tree.getName() ) ? "" : gene_tree.getName() ) );
267 System.out.println();
269 final long start_time = new Date().getTime();
271 if ( base_algorithm == BASE_ALGORITHM.GSDI ) {
272 System.out.println();
273 System.out.println( "Use most parsimonous duplication model: " + most_parsimonous_duplication_model );
274 System.out.println( "Allow stripping of gene tree nodes : " + allow_stripping_of_gene_tree );
275 log_writer.println( "Use most parsimonous duplication model: " + most_parsimonous_duplication_model );
276 log_writer.write( "Allow stripping of gene tree nodes : " + allow_stripping_of_gene_tree );
278 sdi = new GSDI( gene_tree,
280 most_parsimonous_duplication_model,
281 allow_stripping_of_gene_tree,
285 System.out.println();
286 System.out.println( "Using SDIse algorithm" );
287 log_writer.println( "Using SDIse algorithm" );
289 sdi = new SDIse( gene_tree, species_tree );
292 catch ( final SdiException e ) {
293 log_writer.println( "User Error: " + e.getLocalizedMessage() );
295 ForesterUtil.fatalError( PRG_NAME, e.getLocalizedMessage() );
297 catch ( final IOException e ) {
298 log_writer.println( "Error: " + e );
300 ForesterUtil.fatalError( PRG_NAME, e.toString() );
302 catch ( final Exception e ) {
303 log_writer.println( "Error: " + e );
308 System.out.println();
309 System.out.println( "Running time (excluding I/O): " + ( new Date().getTime() - start_time ) + "ms" );
310 log_writer.println( "Running time (excluding I/O): " + ( new Date().getTime() - start_time ) + "ms" );
312 final PhylogenyWriter writer = new PhylogenyWriter();
313 writer.toPhyloXML( out_file, gene_tree, 0 );
315 catch ( final IOException e ) {
316 ForesterUtil.fatalError( PRG_NAME, "Failed to write to [" + out_file + "]: " + e.getMessage() );
318 System.out.println();
319 System.out.println( "Wrote resulting gene tree to: " + out_file );
320 System.out.println();
321 log_writer.println( "Wrote resulting gene tree to: " + out_file );
322 if ( base_algorithm == BASE_ALGORITHM.SDI ) {
323 sdi.computeMappingCostL();
324 System.out.println( "Mapping cost : " + sdi.computeMappingCostL() );
325 log_writer.println( "Mapping cost : " + sdi.computeMappingCostL() );
327 System.out.println( "Number of duplications : " + sdi.getDuplicationsSum() );
328 log_writer.println( "Number of duplications : " + sdi.getDuplicationsSum() );
329 if ( ( base_algorithm == BASE_ALGORITHM.GSDI ) ) {
330 final GSDI gsdi = ( GSDI ) sdi;
331 final File species_tree_used_file = new File( out_file + SUFFIX_FOR_SPECIES_TREE_USED );
333 final PhylogenyWriter writer = new PhylogenyWriter();
334 writer.toPhyloXML( species_tree_used_file, gsdi.getSpeciesTree(), 0 );
336 catch ( final IOException e ) {
337 ForesterUtil.fatalError( PRG_NAME,
338 "Failed to write to [" + species_tree_used_file + "]: " + e.getMessage() );
340 System.out.println();
341 System.out.println( "Wrote used species tree to: " + species_tree_used_file );
342 System.out.println();
343 log_writer.println( "Wrote used species tree to: " + species_tree_used_file );
344 if ( !most_parsimonous_duplication_model ) {
345 final int duplications = gsdi.getSpeciationOrDuplicationEventsSum();
346 System.out.println( "Number of potential duplications: " + duplications );
347 log_writer.println( "Number of potential duplications: " + duplications );
349 final int spec = gsdi.getSpeciationsSum();
350 System.out.println( "Number of speciations : " + spec );
351 log_writer.println( "Number of speciations : " + spec );
352 for( final PhylogenyNode n : gsdi.getMappedExternalSpeciesTreeNodes() ) {
353 System.out.println( n.toString() );
356 System.out.println();
358 // some stat on gene tree:
360 // number of external nodes, strppided nodes
361 // some stats on sepcies tree, external nodes,
364 // how many of which are polytomies
366 // if ( allow_stripping_of_gene_tree ) {
367 // stripped x nodes, y external nodes remain
371 private static void print_help() {
372 System.out.println( "Usage: " + gsdi.PRG_NAME
373 + " [-options] <gene tree in phyloXML format> <species tree in phyloXML format> [outfile]" );
374 System.out.println();
375 System.out.println( "Options:" );
376 // System.out.println( " -" + gsdi.ALLOW_STRIPPING_OF_GENE_TREE_OPTION + ": to allow stripping of gene tree nodes without a matching species in the species tree (writes list of stripped nodes to " + );
377 System.out.println( " -" + gsdi.SDI_OPTION + ": to use SDI algorithm instead of GSDI algorithm" );//TODO gsdi.ALLOW_STRIPPING_OF_GENE_TREE_OPTION not allowed
378 System.out.println( " -" + gsdi.MOST_PARSIMONIOUS_OPTION
379 + ": use most parimonious duplication model for GSDI: " );
380 System.out.println( " assign nodes as speciations which would otherwise be assiged" );
381 System.out.println( " as unknown because of polytomies in the species tree" );
382 System.out.println( " -" + gsdi.GUESS_FORMAT_OF_SPECIES_TREE
383 + ": to allow species tree in other formats than phyloXML (i.e. Newick, NHX, Nexus)" );
384 System.out.println();
385 System.out.println( "Species tree:" );
387 .println( " In phyloXML format (unless option -"
388 + gsdi.GUESS_FORMAT_OF_SPECIES_TREE
389 + " is used, in which case the species matching between gene tree and species tree must be via scientific name), with taxonomy data in appropriate fields" );
390 System.out.println();
391 System.out.println( "Gene tree:" );
392 System.out.println( " In phyloXM format, with taxonomy and sequence data in appropriate fields" );
393 System.out.println();
394 System.out.println( "Example:" );
395 // System.out.println( "gsdi
396 // System.out.println();
397 System.out.println( "Note -- GSDI algorithm is under development" );
398 System.out.println();