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.PhyloXmlDataFormatException;
38 import org.forester.io.parsers.phyloxml.PhyloXmlParser;
39 import org.forester.io.parsers.util.ParserUtils;
40 import org.forester.io.writers.PhylogenyWriter;
41 import org.forester.phylogeny.Phylogeny;
42 import org.forester.phylogeny.PhylogenyMethods;
43 import org.forester.phylogeny.PhylogenyNode;
44 import org.forester.phylogeny.factories.ParserBasedPhylogenyFactory;
45 import org.forester.phylogeny.factories.PhylogenyFactory;
46 import org.forester.sdi.GSDI;
47 import org.forester.sdi.SDI;
48 import org.forester.sdi.SDI.TaxonomyComparisonBase;
49 import org.forester.sdi.SDIException;
50 import org.forester.sdi.SDIse;
51 import org.forester.util.CommandLineArguments;
52 import org.forester.util.EasyWriter;
53 import org.forester.util.ForesterConstants;
54 import org.forester.util.ForesterUtil;
56 public final class gsdi {
58 private enum BASE_ALGORITHM {
61 final static public boolean REPLACE_UNDERSCORES_IN_NH_SPECIES_TREE = true;
62 final static private String ALLOW_STRIPPING_OF_GENE_TREE_OPTION = "g";
63 final static private String SDI_OPTION = "b";
64 final static private String MOST_PARSIMONIOUS_OPTION = "m";
65 final static private String GUESS_FORMAT_OF_SPECIES_TREE = "q";
66 final static private String HELP_OPTION_1 = "help";
67 final static private String HELP_OPTION_2 = "h";
68 final static private String DEFAULT_OUTFILE_SUFFIX = "_gsdi_out.xml";
69 final static private String SUFFIX_FOR_LIST_OF_STIPPED_GENE_TREE_NODES = "_stripped_gene_tree_nodes.txt";
70 final static private String SUFFIX_FOR_SPECIES_TREE_USED = "_species_tree_used.xml";
71 final static private String LOGFILE_SUFFIX = "_gsdi_log.txt";
72 final static private String PRG_NAME = "gsdi";
73 final static private String PRG_VERSION = "0.901";
74 final static private String PRG_DATE = "120608";
75 final static private String PRG_DESC = "general speciation duplication inference";
76 final static private String E_MAIL = "phylosoft@gmail.com";
77 final static private String WWW = "www.phylosoft.org/forester";
79 public static void main( final String args[] ) {
81 ForesterUtil.printProgramInformation( PRG_NAME,
87 ForesterUtil.getForesterLibraryInformation() );
88 CommandLineArguments cla = null;
90 cla = new CommandLineArguments( args );
92 catch ( final Exception e ) {
93 ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
95 if ( cla.isOptionSet( gsdi.HELP_OPTION_1 ) || cla.isOptionSet( gsdi.HELP_OPTION_2 ) ) {
100 else if ( ( args.length < 2 ) || ( cla.getNumberOfNames() < 2 ) || ( cla.getNumberOfNames() > 3 ) ) {
101 System.out.println();
102 System.out.println( "Wrong number of arguments." );
103 System.out.println();
107 final List<String> allowed_options = new ArrayList<String>();
108 allowed_options.add( gsdi.SDI_OPTION );
109 allowed_options.add( gsdi.GUESS_FORMAT_OF_SPECIES_TREE );
110 allowed_options.add( gsdi.MOST_PARSIMONIOUS_OPTION );
111 allowed_options.add( gsdi.ALLOW_STRIPPING_OF_GENE_TREE_OPTION );
112 final String dissallowed_options = cla.validateAllowedOptionsAsString( allowed_options );
113 if ( dissallowed_options.length() > 0 ) {
114 ForesterUtil.fatalError( gsdi.PRG_NAME, "unknown option(s): " + dissallowed_options );
118 catch ( final IOException e ) {
119 ForesterUtil.fatalError( gsdi.PRG_NAME, e.getMessage() );
123 private static void execute( final CommandLineArguments cla ) throws IOException {
124 BASE_ALGORITHM base_algorithm = BASE_ALGORITHM.GSDI;
125 boolean most_parsimonous_duplication_model = false;
126 boolean species_tree_in_phyloxml = true;
127 boolean allow_stripping_of_gene_tree = false;
128 if ( cla.isOptionSet( gsdi.SDI_OPTION ) ) {
129 base_algorithm = BASE_ALGORITHM.SDI;
131 if ( cla.isOptionSet( gsdi.MOST_PARSIMONIOUS_OPTION ) ) {
132 if ( base_algorithm != BASE_ALGORITHM.GSDI ) {
133 ForesterUtil.fatalError( gsdi.PRG_NAME, "Can only use most parsimonious duplication mode with GSDI" );
135 most_parsimonous_duplication_model = true;
137 if ( cla.isOptionSet( gsdi.GUESS_FORMAT_OF_SPECIES_TREE ) ) {
138 species_tree_in_phyloxml = false;
140 if ( cla.isOptionSet( gsdi.ALLOW_STRIPPING_OF_GENE_TREE_OPTION ) ) {
141 if ( base_algorithm != BASE_ALGORITHM.GSDI ) {
142 ForesterUtil.fatalError( gsdi.PRG_NAME, "Can only allow stripping of gene tree with GSDI" );
144 allow_stripping_of_gene_tree = true;
146 Phylogeny species_tree = null;
147 Phylogeny gene_tree = null;
148 File gene_tree_file = null;
149 File species_tree_file = null;
150 File out_file = null;
151 File log_file = null;
152 EasyWriter log_writer = null;
154 gene_tree_file = cla.getFile( 0 );
155 species_tree_file = cla.getFile( 1 );
156 if ( cla.getNumberOfNames() == 3 ) {
157 out_file = cla.getFile( 2 );
160 out_file = new File( ForesterUtil.removeSuffix( gene_tree_file.toString() ) + DEFAULT_OUTFILE_SUFFIX );
162 log_file = new File( ForesterUtil.removeSuffix( out_file.toString() ) + LOGFILE_SUFFIX );
164 catch ( final IllegalArgumentException e ) {
165 ForesterUtil.fatalError( gsdi.PRG_NAME, "error in command line: " + e.getMessage() );
167 if ( ForesterUtil.isReadableFile( gene_tree_file ) != null ) {
168 ForesterUtil.fatalError( gsdi.PRG_NAME, ForesterUtil.isReadableFile( gene_tree_file ) );
170 if ( ForesterUtil.isReadableFile( species_tree_file ) != null ) {
171 ForesterUtil.fatalError( gsdi.PRG_NAME, ForesterUtil.isReadableFile( species_tree_file ) );
173 if ( ForesterUtil.isWritableFile( out_file ) != null ) {
174 ForesterUtil.fatalError( gsdi.PRG_NAME, ForesterUtil.isWritableFile( out_file ) );
176 if ( ForesterUtil.isWritableFile( log_file ) != null ) {
177 ForesterUtil.fatalError( gsdi.PRG_NAME, ForesterUtil.isWritableFile( log_file ) );
180 log_writer = ForesterUtil.createEasyWriter( log_file );
182 catch ( final IOException e ) {
183 ForesterUtil.fatalError( gsdi.PRG_NAME, "Failed to create [" + log_file + "]: " + e.getMessage() );
186 final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
187 gene_tree = factory.create( gene_tree_file, new PhyloXmlParser() )[ 0 ];
189 catch ( final IOException e ) {
190 ForesterUtil.fatalError( gsdi.PRG_NAME,
191 "Failed to read gene tree from [" + gene_tree_file + "]: " + e.getMessage() );
194 final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
195 if ( species_tree_in_phyloxml ) {
196 species_tree = factory.create( species_tree_file, new PhyloXmlParser() )[ 0 ];
199 final PhylogenyParser p = ParserUtils.createParserDependingOnFileType( species_tree_file, true );
200 if ( REPLACE_UNDERSCORES_IN_NH_SPECIES_TREE && ( p instanceof NHXParser ) ) {
201 ( ( NHXParser ) p ).setReplaceUnderscores( true );
203 species_tree = factory.create( species_tree_file, p )[ 0 ];
204 final TaxonomyComparisonBase comp_base = GSDI.determineTaxonomyComparisonBase( gene_tree );
205 switch ( comp_base ) {
206 case SCIENTIFIC_NAME:
209 .transferNodeNameToField( species_tree,
210 PhylogenyMethods.PhylogenyNodeField.TAXONOMY_SCIENTIFIC_NAME,
213 catch ( final PhyloXmlDataFormatException e ) {
214 ForesterUtil.fatalError( gsdi.PRG_NAME,
215 "Failed to transfer general node name to scientific name, in ["
216 + species_tree_file + "]: " + e.getMessage() );
222 .transferNodeNameToField( species_tree,
223 PhylogenyMethods.PhylogenyNodeField.TAXONOMY_CODE,
226 catch ( final PhyloXmlDataFormatException e ) {
227 ForesterUtil.fatalError( gsdi.PRG_NAME,
228 "Failed to transfer general node name to taxonomy code, in ["
229 + species_tree_file + "]: " + e.getMessage() );
234 PhylogenyMethods.transferNodeNameToField( species_tree,
235 PhylogenyMethods.PhylogenyNodeField.TAXONOMY_ID,
238 catch ( final PhyloXmlDataFormatException e ) {
239 ForesterUtil.fatalError( gsdi.PRG_NAME,
240 "Failed to transfer general node name to taxonomy id, in ["
241 + species_tree_file + "]: " + e.getMessage() );
245 ForesterUtil.fatalError( gsdi.PRG_NAME, "unable to determine comparison base" );
249 catch ( final IOException e ) {
250 ForesterUtil.fatalError( gsdi.PRG_NAME, "Failed to read species tree from [" + species_tree_file + "]: "
253 gene_tree.setRooted( true );
254 species_tree.setRooted( true );
255 if ( !gene_tree.isCompletelyBinary() ) {
256 log_writer.println( "User Error: gene tree is not completely binary" );
258 ForesterUtil.fatalError( gsdi.PRG_NAME, "gene tree is not completely binary" );
260 if ( base_algorithm != BASE_ALGORITHM.GSDI ) {
261 if ( !species_tree.isCompletelyBinary() ) {
262 log_writer.println( "User Error: species tree is not completely binary, use GSDI instead" );
264 ForesterUtil.fatalError( gsdi.PRG_NAME, "species tree is not completely binary, use GSDI instead" );
268 // gene_tree = Helper.createBalancedTree( 10 );
269 // species_tree = Helper.createBalancedTree( 13 );
270 // species_tree = Helper.createUnbalancedTree( 1024 );
271 // gene_tree = Helper.createUnbalancedTree( 8192 );
272 // species_tree = gene_tree.copyTree();
273 // gene_tree = species_tree.copyTree();
274 // Helper.numberSpeciesInOrder( species_tree );
275 // Helper.numberSpeciesInOrder( gene_tree );
276 // Helper.randomizeSpecies( 1, 8192, gene_tree );
277 // Helper.intervalNumberSpecies( gene_tree, 4096 );
278 // Helper.numberSpeciesInDescOrder( gene_tree );
279 log_writer.println( PRG_NAME + " - " + PRG_DESC );
280 log_writer.println( " version : " + PRG_VERSION );
281 log_writer.println( " date : " + PRG_DATE );
282 log_writer.println( " forester version: " + ForesterConstants.FORESTER_VERSION );
283 log_writer.println( "Start time: " + new SimpleDateFormat( "yyyyMMdd HH:mm:ss" ).format( new Date() ) );
284 log_writer.println( "Gene tree file: " + gene_tree_file.getCanonicalPath() );
285 log_writer.println( "Gene tree name: "
286 + ( ForesterUtil.isEmpty( gene_tree.getName() ) ? "" : gene_tree.getName() ) );
287 log_writer.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();
292 final long start_time = new Date().getTime();
294 if ( base_algorithm == BASE_ALGORITHM.GSDI ) {
295 System.out.println();
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.write( "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();
332 System.out.println( "Running time (excluding I/O): " + ( new Date().getTime() - start_time ) + "ms" );
333 log_writer.println( "Running time (excluding I/O): " + ( new Date().getTime() - start_time ) + "ms" );
335 final PhylogenyWriter writer = new PhylogenyWriter();
336 writer.toPhyloXML( out_file, gene_tree, 0 );
338 catch ( final IOException e ) {
339 ForesterUtil.fatalError( PRG_NAME, "Failed to write to [" + out_file + "]: " + e.getMessage() );
341 System.out.println();
342 System.out.println( "Wrote resulting gene tree to: " + out_file );
343 System.out.println();
344 log_writer.println( "Wrote resulting gene tree to: " + out_file );
345 if ( base_algorithm == BASE_ALGORITHM.SDI ) {
346 sdi.computeMappingCostL();
347 System.out.println( "Mapping cost : " + sdi.computeMappingCostL() );
348 log_writer.println( "Mapping cost : " + sdi.computeMappingCostL() );
350 System.out.println( "Number of duplications : " + sdi.getDuplicationsSum() );
351 log_writer.println( "Number of duplications : " + sdi.getDuplicationsSum() );
352 if ( ( base_algorithm == BASE_ALGORITHM.GSDI ) ) {
353 final GSDI gsdi = ( GSDI ) sdi;
354 final File species_tree_used_file = new File( out_file + SUFFIX_FOR_SPECIES_TREE_USED );
356 final PhylogenyWriter writer = new PhylogenyWriter();
357 writer.toPhyloXML( species_tree_used_file, gsdi.getSpeciesTree(), 0 );
359 catch ( final IOException e ) {
360 ForesterUtil.fatalError( PRG_NAME,
361 "Failed to write to [" + species_tree_used_file + "]: " + e.getMessage() );
363 System.out.println();
364 System.out.println( "Wrote used species tree to: " + species_tree_used_file );
365 System.out.println();
366 log_writer.println( "Wrote used species tree to: " + species_tree_used_file );
367 if ( !most_parsimonous_duplication_model ) {
368 final int duplications = gsdi.getSpeciationOrDuplicationEventsSum();
369 System.out.println( "Number of potential duplications: " + duplications );
370 log_writer.println( "Number of potential duplications: " + duplications );
372 final int spec = gsdi.getSpeciationsSum();
373 System.out.println( "Number of speciations : " + spec );
374 log_writer.println( "Number of speciations : " + spec );
375 for( final PhylogenyNode n : gsdi.getMappedExternalSpeciesTreeNodes() ) {
376 System.out.println( n.toString() );
379 System.out.println();
381 // some stat on gene tree:
383 // number of external nodes, strppided nodes
384 // some stats on sepcies tree, external nodes,
387 // how many of which are polytomies
389 // if ( allow_stripping_of_gene_tree ) {
390 // stripped x nodes, y external nodes remain
394 private static void print_help() {
395 System.out.println( "Usage: " + gsdi.PRG_NAME
396 + " [-options] <gene tree in phyloXML format> <species tree in phyloXML format> [outfile]" );
397 System.out.println();
398 System.out.println( "Options:" );
399 // 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 " + );
400 System.out.println( " -" + gsdi.SDI_OPTION + ": to use SDI algorithm instead of GSDI algorithm" );//TODO gsdi.ALLOW_STRIPPING_OF_GENE_TREE_OPTION not allowed
401 System.out.println( " -" + gsdi.MOST_PARSIMONIOUS_OPTION
402 + ": use most parimonious duplication model for GSDI: " );
403 System.out.println( " assign nodes as speciations which would otherwise be assiged" );
404 System.out.println( " as unknown because of polytomies in the species tree" );
405 System.out.println( " -" + gsdi.GUESS_FORMAT_OF_SPECIES_TREE
406 + ": to allow species tree in other formats than phyloXML (i.e. Newick, NHX, Nexus)" );
407 System.out.println();
408 System.out.println( "Species tree:" );
410 .println( " In phyloXML format (unless option -"
411 + gsdi.GUESS_FORMAT_OF_SPECIES_TREE
412 + " 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" );
413 System.out.println();
414 System.out.println( "Gene tree:" );
415 System.out.println( " In phyloXM format, with taxonomy and sequence data in appropriate fields" );
416 System.out.println();
417 System.out.println( "Example:" );
418 // System.out.println( "gsdi
419 // System.out.println();
420 System.out.println( "Note -- GSDI algorithm is under development" );
421 System.out.println();