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.GSDII;
52 import org.forester.sdi.GSDIR;
53 import org.forester.sdi.SDIException;
54 import org.forester.sdi.SDIutil;
55 import org.forester.sdi.SDIutil.ALGORITHM;
56 import org.forester.sdi.SDIutil.TaxonomyComparisonBase;
57 import org.forester.util.CommandLineArguments;
58 import org.forester.util.EasyWriter;
59 import org.forester.util.ForesterConstants;
60 import org.forester.util.ForesterUtil;
62 public final class gsdi {
64 final static public boolean REPLACE_UNDERSCORES_IN_NH_SPECIES_TREE = true;
65 final static private String ALLOW_STRIPPING_OF_GENE_TREE_OPTION = "g";
66 final static private String GSDIR_OPTION = "r";
67 final static private String MOST_PARSIMONIOUS_OPTION = "m";
68 final static private String GUESS_FORMAT_OF_SPECIES_TREE = "q";
69 final static private String HELP_OPTION_1 = "help";
70 final static private String HELP_OPTION_2 = "h";
71 final static private String SUFFIX_FOR_SPECIES_TREE_USED = "_species_tree_used.xml";
72 final static private String LOGFILE_SUFFIX = "_gsdi_log.txt";
73 final static private String REMAPPED_SUFFIX = "_gsdi_remapped.txt";
74 final static private String PRG_NAME = "gsdi";
75 final static private String PRG_VERSION = "1.000";
76 final static private String PRG_DATE = "120629";
77 final static private String PRG_DESC = "general speciation duplication inference";
78 final static private String E_MAIL = "phylosoft@gmail.com";
79 final static private String WWW = "www.phylosoft.org/forester";
81 public static void main( final String args[] ) {
83 ForesterUtil.printProgramInformation( PRG_NAME,
89 ForesterUtil.getForesterLibraryInformation() );
90 CommandLineArguments cla = null;
92 cla = new CommandLineArguments( args );
94 catch ( final Exception e ) {
95 ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
97 if ( cla.isOptionSet( gsdi.HELP_OPTION_1 ) || cla.isOptionSet( gsdi.HELP_OPTION_2 ) ) {
102 else if ( ( args.length < 2 ) || ( cla.getNumberOfNames() != 3 ) ) {
103 System.out.println();
104 System.out.println( "Wrong number of arguments." );
105 System.out.println();
109 final List<String> allowed_options = new ArrayList<String>();
110 allowed_options.add( gsdi.GSDIR_OPTION );
111 allowed_options.add( gsdi.GUESS_FORMAT_OF_SPECIES_TREE );
112 allowed_options.add( gsdi.MOST_PARSIMONIOUS_OPTION );
113 allowed_options.add( gsdi.ALLOW_STRIPPING_OF_GENE_TREE_OPTION );
114 final String dissallowed_options = cla.validateAllowedOptionsAsString( allowed_options );
115 if ( dissallowed_options.length() > 0 ) {
116 ForesterUtil.fatalError( gsdi.PRG_NAME, "unknown option(s): " + dissallowed_options );
120 catch ( final IOException e ) {
121 ForesterUtil.fatalError( gsdi.PRG_NAME, e.getMessage() );
125 private static void execute( final CommandLineArguments cla ) throws IOException {
126 ALGORITHM base_algorithm = ALGORITHM.GSDI;
127 boolean most_parsimonous_duplication_model = false;
128 boolean allow_stripping_of_gene_tree = false;
129 if ( cla.isOptionSet( gsdi.GSDIR_OPTION ) ) {
130 base_algorithm = ALGORITHM.GSDIR;
132 if ( cla.isOptionSet( gsdi.MOST_PARSIMONIOUS_OPTION ) ) {
133 if ( base_algorithm == ALGORITHM.SDI ) {
134 ForesterUtil.fatalError( gsdi.PRG_NAME, "Cannot use most parsimonious duplication mode with SDI" );
136 most_parsimonous_duplication_model = true;
138 if ( cla.isOptionSet( gsdi.ALLOW_STRIPPING_OF_GENE_TREE_OPTION ) ) {
139 if ( base_algorithm == ALGORITHM.SDI ) {
140 ForesterUtil.fatalError( gsdi.PRG_NAME, "Cannot allow stripping of gene tree with SDI" );
142 allow_stripping_of_gene_tree = true;
144 Phylogeny species_tree = null;
145 Phylogeny gene_tree = null;
146 File gene_tree_file = null;
147 File species_tree_file = null;
148 File out_file = null;
149 File log_file = null;
150 EasyWriter log_writer = null;
152 gene_tree_file = cla.getFile( 0 );
153 species_tree_file = cla.getFile( 1 );
154 out_file = cla.getFile( 2 );
155 log_file = new File( ForesterUtil.removeSuffix( out_file.toString() ) + LOGFILE_SUFFIX );
157 catch ( final IllegalArgumentException e ) {
158 ForesterUtil.fatalError( gsdi.PRG_NAME, "error in command line: " + e.getMessage() );
160 if ( ForesterUtil.isReadableFile( gene_tree_file ) != null ) {
161 ForesterUtil.fatalError( gsdi.PRG_NAME, ForesterUtil.isReadableFile( gene_tree_file ) );
163 if ( ForesterUtil.isReadableFile( species_tree_file ) != null ) {
164 ForesterUtil.fatalError( gsdi.PRG_NAME, ForesterUtil.isReadableFile( species_tree_file ) );
166 if ( ForesterUtil.isWritableFile( out_file ) != null ) {
167 ForesterUtil.fatalError( gsdi.PRG_NAME, ForesterUtil.isWritableFile( out_file ) );
169 if ( ForesterUtil.isWritableFile( log_file ) != null ) {
170 ForesterUtil.fatalError( gsdi.PRG_NAME, ForesterUtil.isWritableFile( log_file ) );
173 log_writer = ForesterUtil.createEasyWriter( log_file );
175 catch ( final IOException e ) {
176 ForesterUtil.fatalError( gsdi.PRG_NAME, "Failed to create [" + log_file + "]: " + e.getMessage() );
179 final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
180 gene_tree = factory.create( gene_tree_file, new PhyloXmlParser() )[ 0 ];
182 catch ( final IOException e ) {
184 "Failed to read gene tree from [" + gene_tree_file + "]: " + e.getMessage(),
188 final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
189 final PhylogenyParser p = ParserUtils.createParserDependingOnFileType( species_tree_file, true );
190 if ( p instanceof PhyloXmlParser ) {
191 species_tree = factory.create( species_tree_file, p )[ 0 ];
194 if ( REPLACE_UNDERSCORES_IN_NH_SPECIES_TREE && ( p instanceof NHXParser ) ) {
195 ( ( NHXParser ) p ).setReplaceUnderscores( true );
197 species_tree = factory.create( species_tree_file, p )[ 0 ];
198 final TaxonomyComparisonBase comp_base = SDIutil.determineTaxonomyComparisonBase( gene_tree );
199 switch ( comp_base ) {
200 case SCIENTIFIC_NAME:
203 .transferNodeNameToField( species_tree,
204 PhylogenyMethods.PhylogenyNodeField.TAXONOMY_SCIENTIFIC_NAME,
207 catch ( final PhyloXmlDataFormatException e ) {
208 fatalError( "USER ERROR", "Failed to transfer general node name to scientific name, in ["
209 + species_tree_file + "]: " + e.getMessage(), log_writer );
215 .transferNodeNameToField( species_tree,
216 PhylogenyMethods.PhylogenyNodeField.TAXONOMY_CODE,
219 catch ( final PhyloXmlDataFormatException e ) {
220 fatalError( "USER ERROR", "Failed to transfer general node name to taxonomy code, in ["
221 + species_tree_file + "]: " + e.getMessage(), log_writer );
226 PhylogenyMethods.transferNodeNameToField( species_tree,
227 PhylogenyMethods.PhylogenyNodeField.TAXONOMY_ID,
230 catch ( final PhyloXmlDataFormatException e ) {
231 fatalError( "USER ERROR", "Failed to transfer general node name to taxonomy id, in ["
232 + species_tree_file + "]: " + e.getMessage(), log_writer );
236 fatalError( "UNEXPECTED ERROR", "unable to determine comparison base", log_writer );
240 catch ( final SDIException e ) {
241 fatalError( "user error", e.getMessage(), log_writer );
243 catch ( final IOException e ) {
245 "Failed to read species tree from [" + species_tree_file + "]: " + e.getMessage(),
248 gene_tree.setRooted( true );
249 species_tree.setRooted( true );
250 if ( !gene_tree.isCompletelyBinary() ) {
251 fatalError( "user error", "gene tree is not completely binary", log_writer );
253 if ( base_algorithm == ALGORITHM.SDI ) {
254 if ( !species_tree.isCompletelyBinary() ) {
255 fatalError( "user error",
256 "species tree is not completely binary, use GSDI or GSDIR instead",
260 log_writer.println( PRG_NAME + " - " + PRG_DESC );
261 log_writer.println( " version : " + PRG_VERSION );
262 log_writer.println( " date : " + PRG_DATE );
263 log_writer.println( " forester version: " + ForesterConstants.FORESTER_VERSION );
264 log_writer.println();
265 log_writer.println( "Start time : "
266 + new SimpleDateFormat( "yyyyMMdd HH:mm:ss" ).format( new Date() ) );
267 System.out.println( "Start time : "
268 + new SimpleDateFormat( "yyyyMMdd HH:mm:ss" ).format( new Date() ) );
269 log_writer.println( "Gene tree file : " + gene_tree_file.getCanonicalPath() );
270 System.out.println( "Gene tree file : " + gene_tree_file.getCanonicalPath() );
271 log_writer.println( "Gene tree name : "
272 + ( ForesterUtil.isEmpty( gene_tree.getName() ) ? "" : gene_tree.getName() ) );
273 System.out.println( "Gene tree name : "
274 + ( ForesterUtil.isEmpty( gene_tree.getName() ) ? "" : gene_tree.getName() ) );
275 log_writer.println( "Species tree file : " + species_tree_file.getCanonicalPath() );
276 System.out.println( "Species tree file : " + species_tree_file.getCanonicalPath() );
277 log_writer.println( "Species tree name : "
278 + ( ForesterUtil.isEmpty( species_tree.getName() ) ? "" : gene_tree.getName() ) );
279 System.out.println( "Species tree name : "
280 + ( ForesterUtil.isEmpty( species_tree.getName() ) ? "" : gene_tree.getName() ) );
282 final long start_time = new Date().getTime();
284 if ( base_algorithm == ALGORITHM.GSDI ) {
285 System.out.println( "Algorithm : GSDI" );
286 log_writer.println( "Algorithm : GSDI" );
288 else if ( base_algorithm == ALGORITHM.GSDIR ) {
289 System.out.println( "Algorithm : GSDIR" );
290 log_writer.println( "Algorithm : GSDIR" );
292 System.out.println( "Use most parsimonous duplication model : " + most_parsimonous_duplication_model );
293 System.out.println( "Allow stripping of gene tree nodes : " + allow_stripping_of_gene_tree );
294 log_writer.println( "Use most parsimonous duplication model : " + most_parsimonous_duplication_model );
295 log_writer.println( "Allow stripping of gene tree nodes : " + allow_stripping_of_gene_tree );
297 if ( base_algorithm == ALGORITHM.GSDI ) {
298 gsdii = new GSDI( gene_tree,
300 most_parsimonous_duplication_model,
301 allow_stripping_of_gene_tree,
304 else if ( base_algorithm == ALGORITHM.GSDIR ) {
305 gsdii = new GSDIR( gene_tree, species_tree, allow_stripping_of_gene_tree, true );
308 catch ( final SDIException e ) {
309 fatalError( "user error", e.getLocalizedMessage(), log_writer );
311 catch ( final IOException e ) {
312 fatalError( "error", e.toString(), log_writer );
314 catch ( final Exception e ) {
316 fatalError( "unexpected error", e.toString(), log_writer );
318 System.out.println( "Running time (excluding I/O) : " + ( new Date().getTime() - start_time )
320 log_writer.println( "Running time (excluding I/O) : " + ( new Date().getTime() - start_time )
322 System.out.println( "Mapping based on : " + gsdii.getTaxCompBase() );
323 log_writer.println( "Mapping based on : " + gsdii.getTaxCompBase() );
324 if ( ( base_algorithm == ALGORITHM.GSDIR ) ) {
325 final GSDIR gsdir = ( GSDIR ) gsdii;
326 System.out.println( "Duplications sum statistics : " + gsdir.getMinDuplicationsSum() );
327 log_writer.println( "Duplications sum statistics : " + gsdir.getMinDuplicationsSum() );
330 final PhylogenyWriter writer = new PhylogenyWriter();
331 if ( base_algorithm == ALGORITHM.GSDIR ) {
332 writer.toPhyloXML( out_file,
333 ( ( GSDIR ) gsdii ).getMinDuplicationsSumGeneTrees(),
335 ForesterUtil.LINE_SEPARATOR );
338 writer.toPhyloXML( out_file, gene_tree, 0 );
341 catch ( final IOException e ) {
342 ForesterUtil.fatalError( PRG_NAME,
343 "Failed to write to [" + out_file.getCanonicalPath() + "]: " + e.getMessage() );
345 System.out.println( "Wrote resulting gene tree to : " + out_file.getCanonicalPath() );
346 log_writer.println( "Wrote resulting gene tree to : " + out_file.getCanonicalPath() );
347 final File species_tree_used_file = new File( ForesterUtil.removeSuffix( out_file.toString() )
348 + SUFFIX_FOR_SPECIES_TREE_USED );
350 final PhylogenyWriter writer = new PhylogenyWriter();
351 writer.toPhyloXML( species_tree_used_file, species_tree, 0 );
353 catch ( final IOException e ) {
354 ForesterUtil.fatalError( PRG_NAME, "Failed to write to [" + species_tree_used_file.getCanonicalPath()
355 + "]: " + e.getMessage() );
357 System.out.println( "Wrote (stripped) species tree to : " + species_tree_used_file.getCanonicalPath() );
358 log_writer.println( "Wrote (stripped) species tree to : " + species_tree_used_file.getCanonicalPath() );
359 if ( ( gsdii.getReMappedScientificNamesFromGeneTree() != null )
360 && !gsdii.getReMappedScientificNamesFromGeneTree().isEmpty() ) {
361 System.out.println( "Number of gene tree species remapped : "
362 + gsdii.getReMappedScientificNamesFromGeneTree().size() );
363 log_writer.println( "Number of gene tree species remapped : "
364 + gsdii.getReMappedScientificNamesFromGeneTree().size() );
365 writeToRemappedFile( out_file, gsdii.getReMappedScientificNamesFromGeneTree(), log_writer );
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 : " + species_tree.getNumberOfExternalNodes() );
370 log_writer.println( "Number of external nodes in species tree : " + species_tree.getNumberOfExternalNodes() );
371 final int poly = PhylogenyMethods.countNumberOfPolytomies( species_tree );
372 System.out.println( "Number of polytomies in species tree : " + poly );
373 log_writer.println( "Number of polytomies in species tree : " + poly );
374 System.out.println( "External nodes stripped from gene tree : "
375 + gsdii.getStrippedExternalGeneTreeNodes().size() );
376 log_writer.println( "External nodes stripped from gene tree : "
377 + gsdii.getStrippedExternalGeneTreeNodes().size() );
378 System.out.println( "External nodes stripped from species tree: " + gsdii.getStrippedSpeciesTreeNodes().size() );
379 log_writer.println( "External nodes stripped from species tree: " + gsdii.getStrippedSpeciesTreeNodes().size() );
380 System.out.println();
381 System.out.println( "Number of speciations : " + gsdii.getSpeciationsSum() );
382 log_writer.println( "Number of speciations : " + gsdii.getSpeciationsSum() );
383 if ( ( base_algorithm == ALGORITHM.GSDIR ) ) {
384 final GSDIR gsdir = ( GSDIR ) gsdii;
385 System.out.println( "Minimal number of duplications : " + gsdir.getMinDuplicationsSum() );
386 log_writer.println( "Minimal number of duplications : " + gsdir.getMinDuplicationsSum() );
388 else if ( ( base_algorithm == ALGORITHM.GSDI ) ) {
389 final GSDI gsdi = ( GSDI ) gsdii;
390 System.out.println( "Number of duplications : " + gsdi.getDuplicationsSum() );
391 log_writer.println( "Number of duplications : " + gsdi.getDuplicationsSum() );
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 );
398 log_writer.println();
399 printMappedNodesToLog( log_writer, gsdii );
400 log_writer.println();
401 printStrippedGeneTreeNodesToLog( log_writer, gsdii );
402 System.out.println();
403 System.out.println( "Wrote log to : " + log_file.getCanonicalPath() );
404 System.out.println();
408 private static void writeToRemappedFile( final File out_file,
409 final SortedSet<String> remapped,
410 final EasyWriter log_writer ) throws IOException {
411 final File file = new File( ForesterUtil.removeSuffix( out_file.toString() ) + REMAPPED_SUFFIX );
412 final EasyWriter remapped_writer = ForesterUtil.createEasyWriter( file );
413 for( final String s : remapped ) {
414 remapped_writer.println( s );
416 remapped_writer.close();
417 System.out.println( "Wrote remapped gene tree species to : " + file.getCanonicalPath() );
418 log_writer.println( "Wrote remapped gene tree species to : " + file.getCanonicalPath() );
421 private static void printMappedNodesToLog( final EasyWriter log_writer, final GSDII gsdi ) throws IOException {
422 final SortedSet<String> ss = new TreeSet<String>();
423 for( final PhylogenyNode n : gsdi.getMappedExternalSpeciesTreeNodes() ) {
424 ss.add( n.toString() );
426 log_writer.println( "The following " + ss.size() + " species were used: " );
427 for( final String s : ss ) {
428 log_writer.println( " " + s );
432 private static void fatalError( final String type, final String msg, final EasyWriter log_writer ) {
435 log_writer.println();
436 log_writer.print( type.toUpperCase() + ": " );
437 log_writer.println( msg );
440 catch ( final IOException e ) {
443 ForesterUtil.fatalError( gsdi.PRG_NAME, msg );
446 private static void printStrippedGeneTreeNodesToLog( final EasyWriter log_writer, final GSDII gsdi )
448 final SortedMap<String, Integer> sm = new TreeMap<String, Integer>();
449 for( final PhylogenyNode n : gsdi.getStrippedExternalGeneTreeNodes() ) {
450 final String s = n.toString();
451 if ( sm.containsKey( s ) ) {
452 sm.put( s, sm.get( s ) + 1 );
458 log_writer.println( "The following " + sm.size() + " nodes were stripped from the gene tree: " );
459 for( final String s : sm.keySet() ) {
460 final int count = sm.get( s );
462 log_writer.println( " " + s );
465 log_writer.println( " " + s + " [" + count + "]" );
470 private static void print_help() {
471 System.out.println( "Usage: " + gsdi.PRG_NAME
472 + " [-options] <gene tree in phyloXML format> <species tree> <outfile>" );
473 System.out.println();
474 System.out.println( "Options:" );
475 System.out.println( " -" + gsdi.ALLOW_STRIPPING_OF_GENE_TREE_OPTION
476 + ": to allow stripping of gene tree nodes without a matching species" );
477 System.out.println( " -" + gsdi.MOST_PARSIMONIOUS_OPTION
478 + ": use most parimonious duplication model for GSDI: " );
479 System.out.println( " assign nodes as speciations which would otherwise be assiged" );
480 System.out.println( " as potential duplications due to polytomies in the species tree" );
481 System.out.println( " -" + gsdi.GUESS_FORMAT_OF_SPECIES_TREE
482 + ": to allow species tree in other formats than phyloXML (i.e. Newick, NHX, Nexus)" );
483 System.out.println( " -" + gsdi.GSDIR_OPTION
484 + ": to use GSDIR algorithm instead of GSDI algorithm (re-rooting)" );
485 System.out.println();
486 System.out.println( "Gene tree:" );
487 System.out.println( " in phyloXM format, with taxonomy and sequence data in appropriate fields" );
488 System.out.println();
489 System.out.println( "Species tree:" );
490 System.out.println( " in phyloXML format (unless option -" + gsdi.GUESS_FORMAT_OF_SPECIES_TREE + " is used)" );
491 System.out.println();
492 System.out.println( "Example: gsdi -" + ALLOW_STRIPPING_OF_GENE_TREE_OPTION
493 + " gene_tree.xml tree_of_life.xml out.xml" );
494 System.out.println();