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 SDI_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_LIST_OF_STIPPED_GENE_TREE_NODES = "_stripped_gene_tree_nodes.txt";
74 final static private String SUFFIX_FOR_SPECIES_TREE_USED = "_species_tree_used.xml";
75 final static private String LOGFILE_SUFFIX = "_gsdi_log.txt";
76 final static private String PRG_NAME = "gsdi";
77 final static private String PRG_VERSION = "0.901";
78 final static private String PRG_DATE = "120608";
79 final static private String PRG_DESC = "general speciation duplication inference";
80 final static private String E_MAIL = "phylosoft@gmail.com";
81 final static private String WWW = "www.phylosoft.org/forester";
83 public static void main( final String args[] ) {
85 ForesterUtil.printProgramInformation( PRG_NAME,
91 ForesterUtil.getForesterLibraryInformation() );
92 CommandLineArguments cla = null;
94 cla = new CommandLineArguments( args );
96 catch ( final Exception e ) {
97 ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
99 if ( cla.isOptionSet( gsdi.HELP_OPTION_1 ) || cla.isOptionSet( gsdi.HELP_OPTION_2 ) ) {
100 System.out.println();
104 else if ( ( args.length < 2 ) || ( cla.getNumberOfNames() < 2 ) || ( cla.getNumberOfNames() > 3 ) ) {
105 System.out.println();
106 System.out.println( "Wrong number of arguments." );
107 System.out.println();
111 final List<String> allowed_options = new ArrayList<String>();
112 allowed_options.add( gsdi.SDI_OPTION );
113 allowed_options.add( gsdi.GUESS_FORMAT_OF_SPECIES_TREE );
114 allowed_options.add( gsdi.MOST_PARSIMONIOUS_OPTION );
115 allowed_options.add( gsdi.ALLOW_STRIPPING_OF_GENE_TREE_OPTION );
116 final String dissallowed_options = cla.validateAllowedOptionsAsString( allowed_options );
117 if ( dissallowed_options.length() > 0 ) {
118 ForesterUtil.fatalError( gsdi.PRG_NAME, "unknown option(s): " + dissallowed_options );
122 catch ( final IOException e ) {
123 ForesterUtil.fatalError( gsdi.PRG_NAME, e.getMessage() );
127 private static void execute( final CommandLineArguments cla ) throws IOException {
128 BASE_ALGORITHM base_algorithm = BASE_ALGORITHM.GSDI;
129 boolean most_parsimonous_duplication_model = false;
130 boolean species_tree_in_phyloxml = true;
131 boolean allow_stripping_of_gene_tree = false;
132 if ( cla.isOptionSet( gsdi.SDI_OPTION ) ) {
133 base_algorithm = BASE_ALGORITHM.SDI;
135 if ( cla.isOptionSet( gsdi.MOST_PARSIMONIOUS_OPTION ) ) {
136 if ( base_algorithm != BASE_ALGORITHM.GSDI ) {
137 ForesterUtil.fatalError( gsdi.PRG_NAME, "Can only use most parsimonious duplication mode with GSDI" );
139 most_parsimonous_duplication_model = true;
141 if ( cla.isOptionSet( gsdi.GUESS_FORMAT_OF_SPECIES_TREE ) ) {
142 species_tree_in_phyloxml = false;
144 if ( cla.isOptionSet( gsdi.ALLOW_STRIPPING_OF_GENE_TREE_OPTION ) ) {
145 if ( base_algorithm != BASE_ALGORITHM.GSDI ) {
146 ForesterUtil.fatalError( gsdi.PRG_NAME, "Can only allow stripping of gene tree with GSDI" );
148 allow_stripping_of_gene_tree = true;
150 Phylogeny species_tree = null;
151 Phylogeny gene_tree = null;
152 File gene_tree_file = null;
153 File species_tree_file = null;
154 File out_file = null;
155 File log_file = null;
156 EasyWriter log_writer = null;
158 gene_tree_file = cla.getFile( 0 );
159 species_tree_file = cla.getFile( 1 );
160 if ( cla.getNumberOfNames() == 3 ) {
161 out_file = cla.getFile( 2 );
164 out_file = new File( ForesterUtil.removeSuffix( gene_tree_file.toString() ) + DEFAULT_OUTFILE_SUFFIX );
166 log_file = new File( ForesterUtil.removeSuffix( out_file.toString() ) + LOGFILE_SUFFIX );
168 catch ( final IllegalArgumentException e ) {
169 ForesterUtil.fatalError( gsdi.PRG_NAME, "error in command line: " + e.getMessage() );
171 if ( ForesterUtil.isReadableFile( gene_tree_file ) != null ) {
172 ForesterUtil.fatalError( gsdi.PRG_NAME, ForesterUtil.isReadableFile( gene_tree_file ) );
174 if ( ForesterUtil.isReadableFile( species_tree_file ) != null ) {
175 ForesterUtil.fatalError( gsdi.PRG_NAME, ForesterUtil.isReadableFile( species_tree_file ) );
177 if ( ForesterUtil.isWritableFile( out_file ) != null ) {
178 ForesterUtil.fatalError( gsdi.PRG_NAME, ForesterUtil.isWritableFile( out_file ) );
180 if ( ForesterUtil.isWritableFile( log_file ) != null ) {
181 ForesterUtil.fatalError( gsdi.PRG_NAME, ForesterUtil.isWritableFile( log_file ) );
184 log_writer = ForesterUtil.createEasyWriter( log_file );
186 catch ( final IOException e ) {
187 ForesterUtil.fatalError( gsdi.PRG_NAME, "Failed to create [" + log_file + "]: " + e.getMessage() );
190 final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
191 gene_tree = factory.create( gene_tree_file, new PhyloXmlParser() )[ 0 ];
193 catch ( final IOException e ) {
194 ForesterUtil.fatalError( gsdi.PRG_NAME,
195 "Failed to read gene tree from [" + gene_tree_file + "]: " + e.getMessage() );
198 final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
199 if ( species_tree_in_phyloxml ) {
200 species_tree = factory.create( species_tree_file, new PhyloXmlParser() )[ 0 ];
203 final PhylogenyParser p = ParserUtils.createParserDependingOnFileType( species_tree_file, true );
204 if ( REPLACE_UNDERSCORES_IN_NH_SPECIES_TREE && ( p instanceof NHXParser ) ) {
205 ( ( NHXParser ) p ).setReplaceUnderscores( true );
207 species_tree = factory.create( species_tree_file, p )[ 0 ];
208 final TaxonomyComparisonBase comp_base = GSDI.determineTaxonomyComparisonBase( gene_tree );
209 switch ( comp_base ) {
210 case SCIENTIFIC_NAME:
213 .transferNodeNameToField( species_tree,
214 PhylogenyMethods.PhylogenyNodeField.TAXONOMY_SCIENTIFIC_NAME,
217 catch ( final PhyloXmlDataFormatException e ) {
218 ForesterUtil.fatalError( gsdi.PRG_NAME,
219 "Failed to transfer general node name to scientific name, in ["
220 + species_tree_file + "]: " + e.getMessage() );
226 .transferNodeNameToField( species_tree,
227 PhylogenyMethods.PhylogenyNodeField.TAXONOMY_CODE,
230 catch ( final PhyloXmlDataFormatException e ) {
231 ForesterUtil.fatalError( gsdi.PRG_NAME,
232 "Failed to transfer general node name to taxonomy code, in ["
233 + species_tree_file + "]: " + e.getMessage() );
238 PhylogenyMethods.transferNodeNameToField( species_tree,
239 PhylogenyMethods.PhylogenyNodeField.TAXONOMY_ID,
242 catch ( final PhyloXmlDataFormatException e ) {
243 ForesterUtil.fatalError( gsdi.PRG_NAME,
244 "Failed to transfer general node name to taxonomy id, in ["
245 + species_tree_file + "]: " + e.getMessage() );
249 ForesterUtil.fatalError( gsdi.PRG_NAME, "unable to determine comparison base" );
253 catch ( final IOException e ) {
254 ForesterUtil.fatalError( gsdi.PRG_NAME, "Failed to read species tree from [" + species_tree_file + "]: "
257 gene_tree.setRooted( true );
258 species_tree.setRooted( true );
259 if ( !gene_tree.isCompletelyBinary() ) {
260 log_writer.println( "User Error: gene tree is not completely binary" );
262 ForesterUtil.fatalError( gsdi.PRG_NAME, "gene tree is not completely binary" );
264 if ( base_algorithm != BASE_ALGORITHM.GSDI ) {
265 if ( !species_tree.isCompletelyBinary() ) {
266 log_writer.println( "User Error: species tree is not completely binary, use GSDI instead" );
268 ForesterUtil.fatalError( gsdi.PRG_NAME, "species tree is not completely binary, use GSDI instead" );
271 log_writer.println( PRG_NAME + " - " + PRG_DESC );
272 log_writer.println( " version : " + PRG_VERSION );
273 log_writer.println( " date : " + PRG_DATE );
274 log_writer.println( " forester version: " + ForesterConstants.FORESTER_VERSION );
275 log_writer.println();
276 log_writer.println( "Start time : "
277 + new SimpleDateFormat( "yyyyMMdd HH:mm:ss" ).format( new Date() ) );
278 System.out.println( "Start time : "
279 + new SimpleDateFormat( "yyyyMMdd HH:mm:ss" ).format( new Date() ) );
280 log_writer.println( "Gene tree file : " + gene_tree_file.getCanonicalPath() );
281 System.out.println( "Gene tree file : " + gene_tree_file.getCanonicalPath() );
282 log_writer.println( "Gene tree name : "
283 + ( ForesterUtil.isEmpty( gene_tree.getName() ) ? "" : gene_tree.getName() ) );
284 System.out.println( "Gene tree name : "
285 + ( ForesterUtil.isEmpty( gene_tree.getName() ) ? "" : gene_tree.getName() ) );
286 log_writer.println( "Species tree file : " + species_tree_file.getCanonicalPath() );
287 System.out.println( "Species tree file : " + species_tree_file.getCanonicalPath() );
288 log_writer.println( "Species tree name : "
289 + ( ForesterUtil.isEmpty( species_tree.getName() ) ? "" : gene_tree.getName() ) );
290 System.out.println( "Species tree name : "
291 + ( ForesterUtil.isEmpty( species_tree.getName() ) ? "" : gene_tree.getName() ) );
293 final long start_time = new Date().getTime();
295 if ( base_algorithm == BASE_ALGORITHM.GSDI ) {
296 System.out.println( "Use most parsimonous duplication model : " + most_parsimonous_duplication_model );
297 System.out.println( "Allow stripping of gene tree nodes : " + allow_stripping_of_gene_tree );
298 log_writer.println( "Use most parsimonous duplication model : " + most_parsimonous_duplication_model );
299 log_writer.println( "Allow stripping of gene tree nodes : " + allow_stripping_of_gene_tree );
301 sdi = new GSDI( gene_tree,
303 most_parsimonous_duplication_model,
304 allow_stripping_of_gene_tree,
308 System.out.println();
309 System.out.println( "Using SDIse algorithm" );
310 log_writer.println( "Using SDIse algorithm" );
312 sdi = new SDIse( gene_tree, species_tree );
315 catch ( final SDIException e ) {
316 log_writer.println( "User Error: " + e.getLocalizedMessage() );
318 ForesterUtil.fatalError( PRG_NAME, e.getLocalizedMessage() );
320 catch ( final IOException e ) {
321 log_writer.println( "Error: " + e );
323 ForesterUtil.fatalError( PRG_NAME, e.toString() );
325 catch ( final Exception e ) {
326 log_writer.println( "Error: " + e );
331 System.out.println( "Running time (excluding I/O) : " + ( new Date().getTime() - start_time )
333 log_writer.println( "Running time (excluding I/O) : " + ( new Date().getTime() - start_time )
335 if ( ( base_algorithm == BASE_ALGORITHM.GSDI ) ) {
336 final GSDI gsdi = ( GSDI ) sdi;
337 System.out.println( "Mapping based on : " + gsdi.getTaxCompBase() );
338 log_writer.println( "Mapping based on : " + gsdi.getTaxCompBase() );
341 final PhylogenyWriter writer = new PhylogenyWriter();
342 writer.toPhyloXML( out_file, gene_tree, 0 );
344 catch ( final IOException e ) {
345 ForesterUtil.fatalError( PRG_NAME, "Failed to write to [" + out_file + "]: " + e.getMessage() );
347 System.out.println( "Wrote resulting gene tree to : " + out_file );
348 log_writer.println( "Wrote resulting gene tree to : " + out_file );
349 if ( base_algorithm == BASE_ALGORITHM.SDI ) {
350 sdi.computeMappingCostL();
351 System.out.println( "Mapping cost : " + sdi.computeMappingCostL() );
352 log_writer.println( "Mapping cost : " + sdi.computeMappingCostL() );
354 else if ( ( base_algorithm == BASE_ALGORITHM.GSDI ) ) {
355 final GSDI gsdi = ( GSDI ) sdi;
356 final File species_tree_used_file = new File( out_file + SUFFIX_FOR_SPECIES_TREE_USED );
358 final PhylogenyWriter writer = new PhylogenyWriter();
359 writer.toPhyloXML( species_tree_used_file, gsdi.getSpeciesTree(), 0 );
361 catch ( final IOException e ) {
362 ForesterUtil.fatalError( PRG_NAME,
363 "Failed to write to [" + species_tree_used_file + "]: " + e.getMessage() );
365 System.out.println( "Wrote (stripped) species tree to : " + species_tree_used_file );
366 log_writer.println( "Wrote (stripped) species tree to : " + species_tree_used_file );
368 System.out.println( "Number of external nodes in gene tree : " + gene_tree.getNumberOfExternalNodes() );
369 log_writer.println( "Number of external nodes in gene tree : " + gene_tree.getNumberOfExternalNodes() );
370 System.out.println( "Number of external nodes in species tree : "
371 + sdi.getSpeciesTree().getNumberOfExternalNodes() );
372 log_writer.println( "Number of external nodes in species tree : "
373 + sdi.getSpeciesTree().getNumberOfExternalNodes() );
374 if ( ( base_algorithm == BASE_ALGORITHM.GSDI ) ) {
375 final GSDI gsdi = ( GSDI ) sdi;
376 final int poly = PhylogenyMethods.countNumberOfPolytomies( gsdi.getSpeciesTree() );
377 System.out.println( "Number of polytomies in species tree : " + poly );
378 log_writer.println( "Number of polytomies in species tree : " + poly );
379 System.out.println( "External nodes stripped from gene tree : "
380 + gsdi.getStrippedExternalGeneTreeNodes().size() );
381 log_writer.println( "External nodes stripped from gene tree : "
382 + gsdi.getStrippedExternalGeneTreeNodes().size() );
383 System.out.println( "External nodes stripped from species tree: "
384 + gsdi.getStrippedSpeciesTreeNodes().size() );
385 log_writer.println( "External nodes stripped from species tree: "
386 + gsdi.getStrippedSpeciesTreeNodes().size() );
388 System.out.println();
389 System.out.println( "Number of duplications : " + sdi.getDuplicationsSum() );
390 log_writer.println( "Number of duplications : " + sdi.getDuplicationsSum() );
391 if ( ( base_algorithm == BASE_ALGORITHM.GSDI ) ) {
392 final GSDI gsdi = ( GSDI ) sdi;
393 if ( !most_parsimonous_duplication_model ) {
394 final int u = gsdi.getSpeciationOrDuplicationEventsSum();
395 System.out.println( "Number of potential duplications : " + u );
396 log_writer.println( "Number of potential duplications : " + u );
398 System.out.println( "Number of speciations : " + gsdi.getSpeciationsSum() );
399 log_writer.println( "Number of speciations : " + gsdi.getSpeciationsSum() );
400 log_writer.println();
401 printMappedNodesToLog( log_writer, gsdi );
402 log_writer.println();
403 printStrippedGeneTreeNodesToLog( log_writer, gsdi );
404 log_writer.println();
405 printStrippedSpeciesTreeNodesToLog( log_writer, gsdi );
407 System.out.println();
408 System.out.println( "Wrote log to : " + log_file );
409 System.out.println();
413 private static void printMappedNodesToLog( final EasyWriter log_writer, final GSDI gsdi ) throws IOException {
414 final SortedSet<String> ss = new TreeSet<String>();
415 for( final PhylogenyNode n : gsdi.getMappedExternalSpeciesTreeNodes() ) {
416 ss.add( n.toString() );
418 log_writer.println( "The following " + ss.size() + " species were used: " );
419 for( final String s : ss ) {
420 log_writer.println( " " + s );
424 private static void printStrippedGeneTreeNodesToLog( final EasyWriter log_writer, final GSDI gsdi )
426 final SortedMap<String, Integer> sm = new TreeMap<String, Integer>();
427 for( final PhylogenyNode n : gsdi.getStrippedExternalGeneTreeNodes() ) {
428 final String s = n.toString();
429 if ( sm.containsKey( s ) ) {
430 sm.put( s, sm.get( s ) + 1 );
436 log_writer.println( "The following " + sm.size() + " nodes were stripped from the gene tree: " );
437 for( final String s : sm.keySet() ) {
438 final int count = sm.get( s );
440 log_writer.println( " " + s );
443 log_writer.println( " " + s + " [" + count + "]" );
448 private static void printStrippedSpeciesTreeNodesToLog( final EasyWriter log_writer, final GSDI gsdi )
450 final SortedSet<String> ss = new TreeSet<String>();
451 for( final PhylogenyNode n : gsdi.getStrippedSpeciesTreeNodes() ) {
452 ss.add( n.toString() );
454 log_writer.println( "The following " + ss.size() + " nodes were stripped from the species tree: " );
455 for( final String n : ss ) {
456 log_writer.println( " " + n );
460 private static void print_help() {
461 System.out.println( "Usage: " + gsdi.PRG_NAME
462 + " [-options] <gene tree in phyloXML format> <species tree in phyloXML format> [outfile]" );
463 System.out.println();
464 System.out.println( "Options:" );
465 // 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 " + );
466 System.out.println( " -" + gsdi.SDI_OPTION + ": to use SDI algorithm instead of GSDI algorithm" );//TODO gsdi.ALLOW_STRIPPING_OF_GENE_TREE_OPTION not allowed
467 System.out.println( " -" + gsdi.MOST_PARSIMONIOUS_OPTION
468 + ": use most parimonious duplication model for GSDI: " );
469 System.out.println( " assign nodes as speciations which would otherwise be assiged" );
470 System.out.println( " as unknown because of polytomies in the species tree" );
471 System.out.println( " -" + gsdi.GUESS_FORMAT_OF_SPECIES_TREE
472 + ": to allow species tree in other formats than phyloXML (i.e. Newick, NHX, Nexus)" );
473 System.out.println();
474 System.out.println( "Species tree:" );
476 .println( " In phyloXML format (unless option -"
477 + gsdi.GUESS_FORMAT_OF_SPECIES_TREE
478 + " 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" );
479 System.out.println();
480 System.out.println( "Gene tree:" );
481 System.out.println( " In phyloXM format, with taxonomy and sequence data in appropriate fields" );
482 System.out.println();
483 System.out.println( "Example:" );
484 // System.out.println( "gsdi
485 // System.out.println();
486 System.out.println( "Note -- GSDI algorithm is under development" );
487 System.out.println();