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;
34 import java.util.SortedMap;
35 import java.util.SortedSet;
36 import java.util.TreeMap;
37 import java.util.TreeSet;
39 import org.forester.io.parsers.PhylogenyParser;
40 import org.forester.io.parsers.nhx.NHXParser;
41 import org.forester.io.parsers.phyloxml.PhyloXmlDataFormatException;
42 import org.forester.io.parsers.phyloxml.PhyloXmlParser;
43 import org.forester.io.parsers.util.ParserUtils;
44 import org.forester.io.writers.PhylogenyWriter;
45 import org.forester.phylogeny.Phylogeny;
46 import org.forester.phylogeny.PhylogenyMethods;
47 import org.forester.phylogeny.PhylogenyNode;
48 import org.forester.phylogeny.factories.ParserBasedPhylogenyFactory;
49 import org.forester.phylogeny.factories.PhylogenyFactory;
50 import org.forester.sdi.GSDI;
51 import org.forester.sdi.SDI;
52 import org.forester.sdi.SDI.TaxonomyComparisonBase;
53 import org.forester.sdi.SDIException;
54 import org.forester.sdi.SDIse;
55 import org.forester.util.CommandLineArguments;
56 import org.forester.util.EasyWriter;
57 import org.forester.util.ForesterConstants;
58 import org.forester.util.ForesterUtil;
60 public final class gsdi {
62 private enum BASE_ALGORITHM {
65 final static public boolean REPLACE_UNDERSCORES_IN_NH_SPECIES_TREE = true;
66 final static private String ALLOW_STRIPPING_OF_GENE_TREE_OPTION = "g";
67 final static private String SDISE_OPTION = "b";
68 final static private String MOST_PARSIMONIOUS_OPTION = "m";
69 final static private String GUESS_FORMAT_OF_SPECIES_TREE = "q";
70 final static private String HELP_OPTION_1 = "help";
71 final static private String HELP_OPTION_2 = "h";
72 final static private String DEFAULT_OUTFILE_SUFFIX = "_gsdi_out.xml";
73 final static private String SUFFIX_FOR_SPECIES_TREE_USED = "_species_tree_used.xml";
74 final static private String LOGFILE_SUFFIX = "_gsdi_log.txt";
75 final static private String PRG_NAME = "gsdi";
76 final static private String PRG_VERSION = "1.000";
77 final static private String PRG_DATE = "120608";
78 final static private String PRG_DESC = "general speciation duplication inference";
79 final static private String E_MAIL = "phylosoft@gmail.com";
80 final static private String WWW = "www.phylosoft.org/forester";
82 public static void main( final String args[] ) {
84 ForesterUtil.printProgramInformation( PRG_NAME,
90 ForesterUtil.getForesterLibraryInformation() );
91 CommandLineArguments cla = null;
93 cla = new CommandLineArguments( args );
95 catch ( final Exception e ) {
96 ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
98 if ( cla.isOptionSet( gsdi.HELP_OPTION_1 ) || cla.isOptionSet( gsdi.HELP_OPTION_2 ) ) {
103 else if ( ( args.length < 2 ) || ( cla.getNumberOfNames() < 2 ) || ( cla.getNumberOfNames() > 3 ) ) {
104 System.out.println();
105 System.out.println( "Wrong number of arguments." );
106 System.out.println();
110 final List<String> allowed_options = new ArrayList<String>();
111 allowed_options.add( gsdi.SDISE_OPTION );
112 allowed_options.add( gsdi.GUESS_FORMAT_OF_SPECIES_TREE );
113 allowed_options.add( gsdi.MOST_PARSIMONIOUS_OPTION );
114 allowed_options.add( gsdi.ALLOW_STRIPPING_OF_GENE_TREE_OPTION );
115 final String dissallowed_options = cla.validateAllowedOptionsAsString( allowed_options );
116 if ( dissallowed_options.length() > 0 ) {
117 ForesterUtil.fatalError( gsdi.PRG_NAME, "unknown option(s): " + dissallowed_options );
121 catch ( final IOException e ) {
122 ForesterUtil.fatalError( gsdi.PRG_NAME, e.getMessage() );
126 private static void execute( final CommandLineArguments cla ) throws IOException {
127 BASE_ALGORITHM base_algorithm = BASE_ALGORITHM.GSDI;
128 boolean most_parsimonous_duplication_model = false;
129 boolean species_tree_in_phyloxml = true;
130 boolean allow_stripping_of_gene_tree = false;
131 if ( cla.isOptionSet( gsdi.SDISE_OPTION ) ) {
132 base_algorithm = BASE_ALGORITHM.SDI;
134 if ( cla.isOptionSet( gsdi.MOST_PARSIMONIOUS_OPTION ) ) {
135 if ( base_algorithm != BASE_ALGORITHM.GSDI ) {
136 ForesterUtil.fatalError( gsdi.PRG_NAME, "Can only use most parsimonious duplication mode with GSDI" );
138 most_parsimonous_duplication_model = true;
140 if ( cla.isOptionSet( gsdi.GUESS_FORMAT_OF_SPECIES_TREE ) ) {
141 species_tree_in_phyloxml = false;
143 if ( cla.isOptionSet( gsdi.ALLOW_STRIPPING_OF_GENE_TREE_OPTION ) ) {
144 if ( base_algorithm != BASE_ALGORITHM.GSDI ) {
145 ForesterUtil.fatalError( gsdi.PRG_NAME, "Can only allow stripping of gene tree with GSDI" );
147 allow_stripping_of_gene_tree = true;
149 Phylogeny species_tree = null;
150 Phylogeny gene_tree = null;
151 File gene_tree_file = null;
152 File species_tree_file = null;
153 File out_file = null;
154 File log_file = null;
155 EasyWriter log_writer = null;
157 gene_tree_file = cla.getFile( 0 );
158 species_tree_file = cla.getFile( 1 );
159 if ( cla.getNumberOfNames() == 3 ) {
160 out_file = cla.getFile( 2 );
163 out_file = new File( ForesterUtil.removeSuffix( gene_tree_file.toString() ) + DEFAULT_OUTFILE_SUFFIX );
165 log_file = new File( ForesterUtil.removeSuffix( out_file.toString() ) + LOGFILE_SUFFIX );
167 catch ( final IllegalArgumentException e ) {
168 ForesterUtil.fatalError( gsdi.PRG_NAME, "error in command line: " + e.getMessage() );
170 if ( ForesterUtil.isReadableFile( gene_tree_file ) != null ) {
171 ForesterUtil.fatalError( gsdi.PRG_NAME, ForesterUtil.isReadableFile( gene_tree_file ) );
173 if ( ForesterUtil.isReadableFile( species_tree_file ) != null ) {
174 ForesterUtil.fatalError( gsdi.PRG_NAME, ForesterUtil.isReadableFile( species_tree_file ) );
176 if ( ForesterUtil.isWritableFile( out_file ) != null ) {
177 ForesterUtil.fatalError( gsdi.PRG_NAME, ForesterUtil.isWritableFile( out_file ) );
179 if ( ForesterUtil.isWritableFile( log_file ) != null ) {
180 ForesterUtil.fatalError( gsdi.PRG_NAME, ForesterUtil.isWritableFile( log_file ) );
183 log_writer = ForesterUtil.createEasyWriter( log_file );
185 catch ( final IOException e ) {
186 ForesterUtil.fatalError( gsdi.PRG_NAME, "Failed to create [" + log_file + "]: " + e.getMessage() );
189 final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
190 gene_tree = factory.create( gene_tree_file, new PhyloXmlParser() )[ 0 ];
192 catch ( final IOException e ) {
193 ForesterUtil.fatalError( gsdi.PRG_NAME,
194 "Failed to read gene tree from [" + gene_tree_file + "]: " + e.getMessage() );
197 final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
198 if ( species_tree_in_phyloxml ) {
199 species_tree = factory.create( species_tree_file, new PhyloXmlParser() )[ 0 ];
202 final PhylogenyParser p = ParserUtils.createParserDependingOnFileType( species_tree_file, true );
203 if ( REPLACE_UNDERSCORES_IN_NH_SPECIES_TREE && ( p instanceof NHXParser ) ) {
204 ( ( NHXParser ) p ).setReplaceUnderscores( true );
206 species_tree = factory.create( species_tree_file, p )[ 0 ];
207 final TaxonomyComparisonBase comp_base = GSDI.determineTaxonomyComparisonBase( gene_tree );
208 switch ( comp_base ) {
209 case SCIENTIFIC_NAME:
212 .transferNodeNameToField( species_tree,
213 PhylogenyMethods.PhylogenyNodeField.TAXONOMY_SCIENTIFIC_NAME,
216 catch ( final PhyloXmlDataFormatException e ) {
217 ForesterUtil.fatalError( gsdi.PRG_NAME,
218 "Failed to transfer general node name to scientific name, in ["
219 + species_tree_file + "]: " + e.getMessage() );
225 .transferNodeNameToField( species_tree,
226 PhylogenyMethods.PhylogenyNodeField.TAXONOMY_CODE,
229 catch ( final PhyloXmlDataFormatException e ) {
230 ForesterUtil.fatalError( gsdi.PRG_NAME,
231 "Failed to transfer general node name to taxonomy code, in ["
232 + species_tree_file + "]: " + e.getMessage() );
237 PhylogenyMethods.transferNodeNameToField( species_tree,
238 PhylogenyMethods.PhylogenyNodeField.TAXONOMY_ID,
241 catch ( final PhyloXmlDataFormatException e ) {
242 ForesterUtil.fatalError( gsdi.PRG_NAME,
243 "Failed to transfer general node name to taxonomy id, in ["
244 + species_tree_file + "]: " + e.getMessage() );
248 ForesterUtil.fatalError( gsdi.PRG_NAME, "unable to determine comparison base" );
252 catch ( final IOException e ) {
253 ForesterUtil.fatalError( gsdi.PRG_NAME, "Failed to read species tree from [" + species_tree_file + "]: "
256 gene_tree.setRooted( true );
257 species_tree.setRooted( true );
258 if ( !gene_tree.isCompletelyBinary() ) {
259 log_writer.println( "User Error: gene tree is not completely binary" );
261 ForesterUtil.fatalError( gsdi.PRG_NAME, "gene tree is not completely binary" );
263 if ( base_algorithm != BASE_ALGORITHM.GSDI ) {
264 if ( !species_tree.isCompletelyBinary() ) {
265 log_writer.println( "User Error: species tree is not completely binary, use GSDI instead" );
267 ForesterUtil.fatalError( gsdi.PRG_NAME, "species tree is not completely binary, use GSDI instead" );
270 log_writer.println( PRG_NAME + " - " + PRG_DESC );
271 log_writer.println( " version : " + PRG_VERSION );
272 log_writer.println( " date : " + PRG_DATE );
273 log_writer.println( " forester version: " + ForesterConstants.FORESTER_VERSION );
274 log_writer.println();
275 log_writer.println( "Start time : "
276 + new SimpleDateFormat( "yyyyMMdd HH:mm:ss" ).format( new Date() ) );
277 System.out.println( "Start time : "
278 + new SimpleDateFormat( "yyyyMMdd HH:mm:ss" ).format( new Date() ) );
279 log_writer.println( "Gene tree file : " + gene_tree_file.getCanonicalPath() );
280 System.out.println( "Gene tree file : " + gene_tree_file.getCanonicalPath() );
281 log_writer.println( "Gene tree name : "
282 + ( ForesterUtil.isEmpty( gene_tree.getName() ) ? "" : gene_tree.getName() ) );
283 System.out.println( "Gene tree name : "
284 + ( ForesterUtil.isEmpty( gene_tree.getName() ) ? "" : gene_tree.getName() ) );
285 log_writer.println( "Species tree file : " + species_tree_file.getCanonicalPath() );
286 System.out.println( "Species tree file : " + species_tree_file.getCanonicalPath() );
287 log_writer.println( "Species tree name : "
288 + ( ForesterUtil.isEmpty( species_tree.getName() ) ? "" : gene_tree.getName() ) );
289 System.out.println( "Species tree name : "
290 + ( ForesterUtil.isEmpty( species_tree.getName() ) ? "" : gene_tree.getName() ) );
292 final long start_time = new Date().getTime();
294 if ( base_algorithm == BASE_ALGORITHM.GSDI ) {
295 System.out.println( "Use most parsimonous duplication model : " + most_parsimonous_duplication_model );
296 System.out.println( "Allow stripping of gene tree nodes : " + allow_stripping_of_gene_tree );
297 log_writer.println( "Use most parsimonous duplication model : " + most_parsimonous_duplication_model );
298 log_writer.println( "Allow stripping of gene tree nodes : " + allow_stripping_of_gene_tree );
300 sdi = new GSDI( gene_tree,
302 most_parsimonous_duplication_model,
303 allow_stripping_of_gene_tree,
307 System.out.println();
308 System.out.println( "Using SDIse algorithm" );
309 log_writer.println( "Using SDIse algorithm" );
311 sdi = new SDIse( gene_tree, species_tree );
314 catch ( final SDIException e ) {
315 log_writer.println( "User Error: " + e.getLocalizedMessage() );
317 ForesterUtil.fatalError( PRG_NAME, e.getLocalizedMessage() );
319 catch ( final IOException e ) {
320 log_writer.println( "Error: " + e );
322 ForesterUtil.fatalError( PRG_NAME, e.toString() );
324 catch ( final Exception e ) {
325 log_writer.println( "Error: " + e );
330 System.out.println( "Running time (excluding I/O) : " + ( new Date().getTime() - start_time )
332 log_writer.println( "Running time (excluding I/O) : " + ( new Date().getTime() - start_time )
334 if ( ( base_algorithm == BASE_ALGORITHM.GSDI ) ) {
335 final GSDI gsdi = ( GSDI ) sdi;
336 System.out.println( "Mapping based on : " + gsdi.getTaxCompBase() );
337 log_writer.println( "Mapping based on : " + gsdi.getTaxCompBase() );
340 final PhylogenyWriter writer = new PhylogenyWriter();
341 writer.toPhyloXML( out_file, gene_tree, 0 );
343 catch ( final IOException e ) {
344 ForesterUtil.fatalError( PRG_NAME, "Failed to write to [" + out_file + "]: " + e.getMessage() );
346 System.out.println( "Wrote resulting gene tree to : " + out_file );
347 log_writer.println( "Wrote resulting gene tree to : " + out_file );
348 if ( base_algorithm == BASE_ALGORITHM.SDI ) {
349 sdi.computeMappingCostL();
350 System.out.println( "Mapping cost : " + sdi.computeMappingCostL() );
351 log_writer.println( "Mapping cost : " + sdi.computeMappingCostL() );
353 else if ( ( base_algorithm == BASE_ALGORITHM.GSDI ) ) {
354 final GSDI gsdi = ( GSDI ) sdi;
355 final File species_tree_used_file = new File( out_file + SUFFIX_FOR_SPECIES_TREE_USED );
357 final PhylogenyWriter writer = new PhylogenyWriter();
358 writer.toPhyloXML( species_tree_used_file, gsdi.getSpeciesTree(), 0 );
360 catch ( final IOException e ) {
361 ForesterUtil.fatalError( PRG_NAME,
362 "Failed to write to [" + species_tree_used_file + "]: " + e.getMessage() );
364 System.out.println( "Wrote (stripped) species tree to : " + species_tree_used_file );
365 log_writer.println( "Wrote (stripped) species tree to : " + species_tree_used_file );
367 System.out.println( "Number of external nodes in gene tree : " + gene_tree.getNumberOfExternalNodes() );
368 log_writer.println( "Number of external nodes in gene tree : " + gene_tree.getNumberOfExternalNodes() );
369 System.out.println( "Number of external nodes in species tree : "
370 + sdi.getSpeciesTree().getNumberOfExternalNodes() );
371 log_writer.println( "Number of external nodes in species tree : "
372 + sdi.getSpeciesTree().getNumberOfExternalNodes() );
373 if ( ( base_algorithm == BASE_ALGORITHM.GSDI ) ) {
374 final GSDI gsdi = ( GSDI ) sdi;
375 final int poly = PhylogenyMethods.countNumberOfPolytomies( gsdi.getSpeciesTree() );
376 System.out.println( "Number of polytomies in species tree : " + poly );
377 log_writer.println( "Number of polytomies in species tree : " + poly );
378 System.out.println( "External nodes stripped from gene tree : "
379 + gsdi.getStrippedExternalGeneTreeNodes().size() );
380 log_writer.println( "External nodes stripped from gene tree : "
381 + gsdi.getStrippedExternalGeneTreeNodes().size() );
382 System.out.println( "External nodes stripped from species tree: "
383 + gsdi.getStrippedSpeciesTreeNodes().size() );
384 log_writer.println( "External nodes stripped from species tree: "
385 + gsdi.getStrippedSpeciesTreeNodes().size() );
387 System.out.println();
388 System.out.println( "Number of duplications : " + sdi.getDuplicationsSum() );
389 log_writer.println( "Number of duplications : " + sdi.getDuplicationsSum() );
390 if ( ( base_algorithm == BASE_ALGORITHM.GSDI ) ) {
391 final GSDI gsdi = ( GSDI ) sdi;
392 if ( !most_parsimonous_duplication_model ) {
393 final int u = gsdi.getSpeciationOrDuplicationEventsSum();
394 System.out.println( "Number of potential duplications : " + u );
395 log_writer.println( "Number of potential duplications : " + u );
397 System.out.println( "Number of speciations : " + gsdi.getSpeciationsSum() );
398 log_writer.println( "Number of speciations : " + gsdi.getSpeciationsSum() );
399 log_writer.println();
400 printMappedNodesToLog( log_writer, gsdi );
401 log_writer.println();
402 printStrippedGeneTreeNodesToLog( log_writer, gsdi );
403 log_writer.println();
404 printStrippedSpeciesTreeNodesToLog( log_writer, gsdi );
406 System.out.println();
407 System.out.println( "Wrote log to : " + log_file );
408 System.out.println();
412 private static void printMappedNodesToLog( final EasyWriter log_writer, final GSDI gsdi ) throws IOException {
413 final SortedSet<String> ss = new TreeSet<String>();
414 for( final PhylogenyNode n : gsdi.getMappedExternalSpeciesTreeNodes() ) {
415 ss.add( n.toString() );
417 log_writer.println( "The following " + ss.size() + " species were used: " );
418 for( final String s : ss ) {
419 log_writer.println( " " + s );
423 private static void printStrippedGeneTreeNodesToLog( final EasyWriter log_writer, final GSDI gsdi )
425 final SortedMap<String, Integer> sm = new TreeMap<String, Integer>();
426 for( final PhylogenyNode n : gsdi.getStrippedExternalGeneTreeNodes() ) {
427 final String s = n.toString();
428 if ( sm.containsKey( s ) ) {
429 sm.put( s, sm.get( s ) + 1 );
435 log_writer.println( "The following " + sm.size() + " nodes were stripped from the gene tree: " );
436 for( final String s : sm.keySet() ) {
437 final int count = sm.get( s );
439 log_writer.println( " " + s );
442 log_writer.println( " " + s + " [" + count + "]" );
447 private static void printStrippedSpeciesTreeNodesToLog( final EasyWriter log_writer, final GSDI gsdi )
449 final SortedSet<String> ss = new TreeSet<String>();
450 for( final PhylogenyNode n : gsdi.getStrippedSpeciesTreeNodes() ) {
451 ss.add( n.toString() );
453 log_writer.println( "The following " + ss.size() + " nodes were stripped from the species tree: " );
454 for( final String n : ss ) {
455 log_writer.println( " " + n );
459 private static void print_help() {
460 System.out.println( "Usage: " + gsdi.PRG_NAME
461 + " [-options] <gene tree in phyloXML format> <species tree> [outfile]" );
462 System.out.println();
463 System.out.println( "Options:" );
464 System.out.println( " -" + gsdi.ALLOW_STRIPPING_OF_GENE_TREE_OPTION
465 + ": to allow stripping of gene tree nodes without a matching species" );
466 System.out.println( " -" + gsdi.MOST_PARSIMONIOUS_OPTION
467 + ": use most parimonious duplication model for GSDI: " );
468 System.out.println( " assign nodes as speciations which would otherwise be assiged" );
469 System.out.println( " as potential duplications dueof polytomies in the species tree" );
470 System.out.println( " -" + gsdi.GUESS_FORMAT_OF_SPECIES_TREE
471 + ": to allow species tree in other formats than phyloXML (i.e. Newick, NHX, Nexus)" );
472 System.out.println( " -" + gsdi.SDISE_OPTION
473 + ": to use SDIse algorithm instead of GSDI algorithm (for binary species trees)" );
474 System.out.println();
475 System.out.println( "Gene tree:" );
476 System.out.println( " in phyloXM format, with taxonomy and sequence data in appropriate fields" );
477 System.out.println();
478 System.out.println( "Species tree:" );
479 System.out.println( " in phyloXML format (unless option -" + gsdi.GUESS_FORMAT_OF_SPECIES_TREE + " is used)" );
480 System.out.println();
481 System.out.println( "Example: gsdi -" + ALLOW_STRIPPING_OF_GENE_TREE_OPTION
482 + " gene_tree.xml tree_of_life.xml out.xml" );
483 System.out.println();