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: https://sites.google.com/site/cmzmasek/home/software/forester
26 package org.forester.application;
29 import java.io.FilenameFilter;
30 import java.io.IOException;
31 import java.text.SimpleDateFormat;
32 import java.util.ArrayList;
33 import java.util.Arrays;
34 import java.util.Date;
35 import java.util.List;
36 import java.util.SortedMap;
37 import java.util.SortedSet;
38 import java.util.TreeMap;
39 import java.util.TreeSet;
41 import org.forester.io.parsers.nhx.NHXParser.TAXONOMY_EXTRACTION;
42 import org.forester.io.parsers.phyloxml.PhyloXmlDataFormatException;
43 import org.forester.io.parsers.phyloxml.PhyloXmlParser;
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.util.CommandLineArguments;
57 import org.forester.util.EasyWriter;
58 import org.forester.util.ForesterConstants;
59 import org.forester.util.ForesterUtil;
61 public final class gsdi {
63 final static public boolean REPLACE_UNDERSCORES_IN_NH_SPECIES_TREE = true;
64 final static private String ALLOW_STRIPPING_OF_GENE_TREE_OPTION = "g";
65 final static private String GSDIR_OPTION = "r";
66 final static private String MOST_PARSIMONIOUS_OPTION = "m";
67 final static private String SUFFIX_FOR_DIR_OPTION = "s";
68 final static private String GUESS_FORMAT_OF_SPECIES_TREE = "q";
69 final static private String TRANSFER_TAXONOMY_OPTION = "t";
70 final static private String HELP_OPTION_1 = "help";
71 final static private String HELP_OPTION_2 = "h";
72 final static private String SUFFIX_FOR_SPECIES_TREE_USED = "_species_tree_used.xml";
73 final static private String OUTTREE_SUFFIX = "_gsdir.xml";
74 final static private String LOGFILE_NAME = "00_gsdi_log.tsv";
75 final static private String LOGFILE_SUFFIX = "_gsdi_log.txt";
76 final static private String REMAPPED_SUFFIX = "_gsdi_remapped.txt";
77 final static private String PRG_NAME = "gsdi";
78 final static private String PRG_VERSION = "1.100";
79 final static private String PRG_DATE = "170403";
80 final static private String PRG_DESC = "general speciation duplication inference";
81 final static private String E_MAIL = "phyloxml@gmail.com";
82 final static private String WWW = "https://sites.google.com/site/cmzmasek/home/software/forester";
84 public static void main( final String args[] ) {
86 ForesterUtil.printProgramInformation( PRG_NAME,
92 ForesterUtil.getForesterLibraryInformation() );
93 CommandLineArguments cla = null;
95 cla = new CommandLineArguments( args );
97 catch ( final Exception e ) {
98 ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
100 if ( cla.isOptionSet( HELP_OPTION_1 ) || cla.isOptionSet( HELP_OPTION_2 ) ) {
101 System.out.println();
105 else if ( ( args.length < 2 ) || ( cla.getNumberOfNames() != 3 ) ) {
106 System.out.println();
107 System.out.println( "Wrong number of arguments." );
108 System.out.println();
112 final List<String> allowed_options = new ArrayList<String>();
113 allowed_options.add( GSDIR_OPTION );
114 allowed_options.add( GUESS_FORMAT_OF_SPECIES_TREE );
115 allowed_options.add( MOST_PARSIMONIOUS_OPTION );
116 allowed_options.add( ALLOW_STRIPPING_OF_GENE_TREE_OPTION );
117 allowed_options.add( TRANSFER_TAXONOMY_OPTION );
118 allowed_options.add( SUFFIX_FOR_DIR_OPTION );
119 final String dissallowed_options = cla.validateAllowedOptionsAsString( allowed_options );
120 if ( dissallowed_options.length() > 0 ) {
121 ForesterUtil.fatalError( PRG_NAME, "unknown option(s): " + dissallowed_options );
125 catch ( final IOException e ) {
126 ForesterUtil.fatalError( gsdi.PRG_NAME, e.getMessage() );
130 private final static void execute( final CommandLineArguments cla ) throws IOException {
131 ALGORITHM base_algorithm = ALGORITHM.GSDI;
132 boolean most_parsimonous_duplication_model = false;
133 boolean allow_stripping_of_gene_tree = false;
134 if ( cla.isOptionSet( GSDIR_OPTION ) ) {
135 base_algorithm = ALGORITHM.GSDIR;
137 if ( cla.isOptionSet( MOST_PARSIMONIOUS_OPTION ) ) {
138 if ( base_algorithm == ALGORITHM.SDI ) {
139 ForesterUtil.fatalError( PRG_NAME, "Cannot use most parsimonious duplication mode with SDI" );
141 most_parsimonous_duplication_model = true;
143 if ( cla.isOptionSet( ALLOW_STRIPPING_OF_GENE_TREE_OPTION ) ) {
144 if ( base_algorithm == ALGORITHM.SDI ) {
145 ForesterUtil.fatalError( PRG_NAME, "Cannot allow stripping of gene tree with SDI" );
147 allow_stripping_of_gene_tree = true;
149 boolean transfer_taxonomy = false;
150 if ( cla.isOptionSet( TRANSFER_TAXONOMY_OPTION ) ) {
151 transfer_taxonomy = true;
153 boolean use_gene_tree_dir = false;
154 final String gene_tree_suffix;
155 if ( cla.isOptionSet( SUFFIX_FOR_DIR_OPTION ) ) {
156 gene_tree_suffix = cla.getOptionValue( SUFFIX_FOR_DIR_OPTION );
157 use_gene_tree_dir = true;
160 gene_tree_suffix = null;
162 File gene_tree_file = null;
163 File species_tree_file = null;
164 File out_file = null;
165 File log_file = null;
168 gene_tree_file = cla.getFile( 0 );
169 species_tree_file = cla.getFile( 1 );
170 if ( use_gene_tree_dir ) {
171 out_dir = cla.getFile( 2 );
172 if ( out_dir.exists() ) {
173 if ( !out_dir.isDirectory() ) {
175 .fatalError( gsdi.PRG_NAME,
176 "out-directory [" + out_dir + "] already exists but is not a directory" );
180 final boolean success = out_dir.mkdirs();
182 ForesterUtil.fatalError( gsdi.PRG_NAME, "could not create out-directory [" + out_dir + "]" );
187 out_file = cla.getFile( 2 );
188 log_file = new File( ForesterUtil.removeSuffix( out_file.toString() ) + LOGFILE_SUFFIX );
191 catch ( final IllegalArgumentException e ) {
192 ForesterUtil.fatalError( PRG_NAME, "error in command line: " + e.getMessage() );
194 if ( use_gene_tree_dir ) {
195 final File indir = new File( gene_tree_file.toString() );
196 if ( !indir.exists() ) {
197 ForesterUtil.fatalError( gsdi.PRG_NAME, "in-directory [" + indir + "] does not exist" );
199 if ( !indir.isDirectory() ) {
200 ForesterUtil.fatalError( gsdi.PRG_NAME, "in-directory [" + indir + "] is not a directory" );
202 final String species_tree_file_name = species_tree_file.getName();
203 final File gene_tree_files[] = indir.listFiles( new FilenameFilter() {
206 public boolean accept( final File dir, final String name ) {
207 return ( ( name.endsWith( gene_tree_suffix ) ) && !( name.equals( species_tree_file_name ) ) );
210 if ( gene_tree_files.length < 1 ) {
211 ForesterUtil.fatalError( gsdi.PRG_NAME,
212 "in-directory [" + indir
213 + "] does not contain any gene tree files with suffix "
214 + gene_tree_suffix );
216 executeDir( base_algorithm,
217 most_parsimonous_duplication_model,
218 allow_stripping_of_gene_tree,
225 execute( base_algorithm,
226 most_parsimonous_duplication_model,
227 allow_stripping_of_gene_tree,
236 private final static void executeDir( final ALGORITHM base_algorithm,
237 final boolean most_parsimonous_duplication_model,
238 final boolean allow_stripping_of_gene_tree,
239 final boolean transfer_taxonomy,
240 final File gene_tree_files[],
241 final File species_tree_file,
244 final File log_file = new File( outdir, LOGFILE_NAME );
245 if ( ForesterUtil.isWritableFile( log_file ) != null ) {
246 ForesterUtil.fatalError( gsdi.PRG_NAME, ForesterUtil.isWritableFile( log_file ) );
248 EasyWriter log_writer = null;
250 log_writer = ForesterUtil.createEasyWriter( log_file );
252 catch ( final IOException e ) {
253 ForesterUtil.fatalError( gsdi.PRG_NAME, "Failed to create [" + log_file + "]: " + e.getMessage() );
255 log_writer.println( "# " + PRG_NAME );
256 log_writer.println( "# Version\t" + PRG_VERSION );
257 log_writer.println( "# Date\t" + PRG_DATE );
258 log_writer.println( "# Forester version\t" + ForesterConstants.FORESTER_VERSION );
259 log_writer.println( "# Species tree\t" + species_tree_file.getCanonicalPath() );
260 if ( base_algorithm == ALGORITHM.GSDI ) {
261 log_writer.println( "# Algorithm\tGSDI" );
263 else if ( base_algorithm == ALGORITHM.GSDIR ) {
264 log_writer.println( "# Algorithm\tGSDIR" );
266 log_writer.println( "# Use most parsimonous duplication model\t" + most_parsimonous_duplication_model );
267 log_writer.println( "# Allow stripping of gene tree nodes\t" + allow_stripping_of_gene_tree );
268 log_writer.println( "# Start time\t" + new SimpleDateFormat( "yyyyMMdd HH:mm:ss" ).format( new Date() ) );
269 log_writer.println();
270 log_writer.print( "Gene-tree file\t" );
271 log_writer.print( "Gene-tree name/#\t" );
272 log_writer.print( "Ext. nodes\t" );
273 log_writer.print( "Speciations\t" );
274 log_writer.print( "Duplications\t" );
275 if ( !most_parsimonous_duplication_model ) {
276 log_writer.print( "Spec. or Dup.\t" );
278 if ( allow_stripping_of_gene_tree ) {
279 log_writer.print( "Stripped gene-tree ext. nodes\t" );
281 log_writer.print( "Taxonomy mapping" );
282 log_writer.println();
284 Arrays.sort( gene_tree_files );
285 for( final File gene_tree_file : gene_tree_files ) {
286 String outname = gene_tree_file.getName();
287 if ( outname.indexOf( "." ) > 0 ) {
288 outname = outname.substring( 0, outname.lastIndexOf( "." ) );
290 outname = outname + OUTTREE_SUFFIX;
291 counter += executeOneTreeInDir( base_algorithm,
292 most_parsimonous_duplication_model,
293 allow_stripping_of_gene_tree,
297 new File( outdir, outname ),
300 System.out.print( "\r" + counter );
302 System.out.print( "\r" );
304 System.out.println( "Analyzed " + counter + " gene trees" );
305 System.out.println();
306 System.out.println( "Wrote log to: " + log_file.getCanonicalPath() );
307 System.out.println();
310 private final static int executeOneTreeInDir( final ALGORITHM base_algorithm,
311 final boolean most_parsimonous_duplication_model,
312 final boolean allow_stripping_of_gene_tree,
313 final boolean transfer_taxonomy,
314 final File gene_tree_file,
315 final File species_tree_file,
317 final EasyWriter log_writer )
319 if ( ForesterUtil.isReadableFile( gene_tree_file ) != null ) {
320 ForesterUtil.fatalError( gsdi.PRG_NAME, ForesterUtil.isReadableFile( gene_tree_file ) );
322 if ( ForesterUtil.isReadableFile( species_tree_file ) != null ) {
323 ForesterUtil.fatalError( gsdi.PRG_NAME, ForesterUtil.isReadableFile( species_tree_file ) );
325 if ( ForesterUtil.isWritableFile( out_file ) != null ) {
326 ForesterUtil.fatalError( gsdi.PRG_NAME, ForesterUtil.isWritableFile( out_file ) );
328 Phylogeny gene_trees[] = null;
330 final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
331 gene_trees = factory.create( gene_tree_file, PhyloXmlParser.createPhyloXmlParserXsdValidating() );
333 catch ( final IOException e ) {
335 "failed to read gene tree from [" + gene_tree_file + "]: " + e.getMessage(),
339 final List<Phylogeny> out_trees = new ArrayList<Phylogeny>();
340 for( final Phylogeny gene_tree : gene_trees ) {
341 if ( !gene_tree.isEmpty() && gene_tree.getNumberOfExternalNodes() > 1 ) {
342 Phylogeny species_tree = null;
344 species_tree = SDIutil.parseSpeciesTree( gene_tree,
346 REPLACE_UNDERSCORES_IN_NH_SPECIES_TREE,
348 TAXONOMY_EXTRACTION.NO );
350 catch ( final PhyloXmlDataFormatException e ) {
351 fatalError( "user error",
352 "failed to transfer general node name, in [" + species_tree_file + "]: "
356 catch ( final SDIException e ) {
357 fatalError( "user error", e.getMessage(), log_writer );
359 catch ( final IOException e ) {
361 "Failed to read species tree from [" + species_tree_file + "]: " + e.getMessage(),
364 gene_tree.setRooted( true );
365 species_tree.setRooted( true );
366 if ( !gene_tree.isCompletelyBinary() ) {
367 fatalError( "user error",
368 "gene tree [" + gene_tree_file + "] is not completely binary",
371 if ( base_algorithm == ALGORITHM.SDI ) {
372 if ( !species_tree.isCompletelyBinary() ) {
373 fatalError( "user error",
374 "species tree is not completely binary, use GSDI or GSDIR instead",
378 log_writer.print( gene_tree_file.getName() );
379 log_writer.print( "\t" );
380 log_writer.print( ( ForesterUtil.isEmpty( gene_tree.getName() ) ? "" : gene_tree.getName() ) );
381 if ( gene_trees.length > 1 ) {
382 log_writer.print( ( ForesterUtil.isEmpty( gene_tree.getName() ) ? Integer.toString( counter )
383 : ( ":" + Integer.toString( counter ) ) ) );
385 log_writer.print( "\t" );
388 if ( base_algorithm == ALGORITHM.GSDI ) {
389 gsdii = new GSDI( gene_tree,
391 most_parsimonous_duplication_model,
392 allow_stripping_of_gene_tree,
396 else if ( base_algorithm == ALGORITHM.GSDIR ) {
397 gsdii = new GSDIR( gene_tree,
399 allow_stripping_of_gene_tree,
404 catch ( final SDIException e ) {
405 fatalError( "user error", e.getLocalizedMessage(), log_writer );
407 catch ( final OutOfMemoryError e ) {
408 ForesterUtil.outOfMemoryError( e );
410 catch ( final Exception e ) {
412 fatalError( "unexpected error", e.toString(), log_writer );
414 if ( base_algorithm == ALGORITHM.GSDIR ) {
415 final Phylogeny gt = ( ( GSDIR ) gsdii ).getMinDuplicationsSumGeneTree();
416 gt.setRerootable( false );
420 gene_tree.setRerootable( false );
421 out_trees.add( gene_tree );
423 log_writer.print( gene_tree.getNumberOfExternalNodes() + "\t" );
424 log_writer.print( gsdii.getSpeciationsSum() + "\t" );
425 if ( ( base_algorithm == ALGORITHM.GSDIR ) ) {
426 final GSDIR gsdir = ( GSDIR ) gsdii;
427 log_writer.print( gsdir.getMinDuplicationsSum() + "\t" );
429 else if ( ( base_algorithm == ALGORITHM.GSDI ) ) {
430 final GSDI gsdi = ( GSDI ) gsdii;
431 log_writer.print( gsdi.getDuplicationsSum() + "\t" );
432 if ( !most_parsimonous_duplication_model ) {
433 log_writer.print( gsdi.getSpeciationOrDuplicationEventsSum() + "\t" );
436 if ( allow_stripping_of_gene_tree ) {
437 log_writer.print( gsdii.getStrippedExternalGeneTreeNodes().size() + "\t" );
439 log_writer.print( gsdii.getTaxCompBase().toString() );
440 log_writer.println();
446 final PhylogenyWriter writer = new PhylogenyWriter();
447 writer.toPhyloXML( out_file, out_trees, 0, ForesterUtil.LINE_SEPARATOR );
449 catch ( final IOException e ) {
451 .fatalError( PRG_NAME,
452 "Failed to write to [" + out_file.getCanonicalPath() + "]: " + e.getMessage() );
458 private final static void execute( final ALGORITHM base_algorithm,
459 final boolean most_parsimonous_duplication_model,
460 final boolean allow_stripping_of_gene_tree,
461 final boolean transfer_taxonomy,
462 final File gene_tree_file,
463 final File species_tree_file,
465 final File log_file )
467 if ( ForesterUtil.isReadableFile( gene_tree_file ) != null ) {
468 ForesterUtil.fatalError( gsdi.PRG_NAME, ForesterUtil.isReadableFile( gene_tree_file ) );
470 if ( ForesterUtil.isReadableFile( species_tree_file ) != null ) {
471 ForesterUtil.fatalError( gsdi.PRG_NAME, ForesterUtil.isReadableFile( species_tree_file ) );
473 if ( ForesterUtil.isWritableFile( out_file ) != null ) {
474 ForesterUtil.fatalError( gsdi.PRG_NAME, ForesterUtil.isWritableFile( out_file ) );
476 if ( ForesterUtil.isWritableFile( log_file ) != null ) {
477 ForesterUtil.fatalError( gsdi.PRG_NAME, ForesterUtil.isWritableFile( log_file ) );
479 EasyWriter log_writer = null;
481 log_writer = ForesterUtil.createEasyWriter( log_file );
483 catch ( final IOException e ) {
484 ForesterUtil.fatalError( gsdi.PRG_NAME, "Failed to create [" + log_file + "]: " + e.getMessage() );
486 Phylogeny species_tree = null;
487 Phylogeny gene_tree = null;
489 final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
490 gene_tree = factory.create( gene_tree_file, PhyloXmlParser.createPhyloXmlParserXsdValidating() )[ 0 ];
492 catch ( final IOException e ) {
494 "failed to read gene tree from [" + gene_tree_file + "]: " + e.getMessage(),
498 species_tree = SDIutil.parseSpeciesTree( gene_tree,
500 REPLACE_UNDERSCORES_IN_NH_SPECIES_TREE,
502 TAXONOMY_EXTRACTION.NO );
504 catch ( final PhyloXmlDataFormatException e ) {
505 fatalError( "user error",
506 "failed to transfer general node name, in [" + species_tree_file + "]: " + e.getMessage(),
509 catch ( final SDIException e ) {
510 fatalError( "user error", e.getMessage(), log_writer );
512 catch ( final IOException e ) {
514 "Failed to read species tree from [" + species_tree_file + "]: " + e.getMessage(),
517 gene_tree.setRooted( true );
518 species_tree.setRooted( true );
519 if ( !gene_tree.isCompletelyBinary() ) {
520 fatalError( "user error", "gene tree [" + gene_tree_file + "] is not completely binary", log_writer );
522 if ( base_algorithm == ALGORITHM.SDI ) {
523 if ( !species_tree.isCompletelyBinary() ) {
524 fatalError( "user error",
525 "species tree is not completely binary, use GSDI or GSDIR instead",
529 log_writer.println( PRG_NAME + " - " + PRG_DESC );
530 log_writer.println( " version : " + PRG_VERSION );
531 log_writer.println( " date : " + PRG_DATE );
532 log_writer.println( " forester version: " + ForesterConstants.FORESTER_VERSION );
533 log_writer.println();
534 log_writer.println( "Start time : "
535 + new SimpleDateFormat( "yyyyMMdd HH:mm:ss" ).format( new Date() ) );
536 System.out.println( "Start time : "
537 + new SimpleDateFormat( "yyyyMMdd HH:mm:ss" ).format( new Date() ) );
538 log_writer.println( "Gene tree file : " + gene_tree_file.getCanonicalPath() );
539 System.out.println( "Gene tree file : " + gene_tree_file.getCanonicalPath() );
540 log_writer.println( "Gene tree name : "
541 + ( ForesterUtil.isEmpty( gene_tree.getName() ) ? "" : gene_tree.getName() ) );
542 System.out.println( "Gene tree name : "
543 + ( ForesterUtil.isEmpty( gene_tree.getName() ) ? "" : gene_tree.getName() ) );
544 log_writer.println( "Species tree file : " + species_tree_file.getCanonicalPath() );
545 System.out.println( "Species tree file : " + species_tree_file.getCanonicalPath() );
546 log_writer.println( "Species tree name : "
547 + ( ForesterUtil.isEmpty( species_tree.getName() ) ? "" : gene_tree.getName() ) );
548 System.out.println( "Species tree name : "
549 + ( ForesterUtil.isEmpty( species_tree.getName() ) ? "" : gene_tree.getName() ) );
550 System.out.println( "Transfer taxonomy : " + transfer_taxonomy );
552 final long start_time = new Date().getTime();
554 if ( base_algorithm == ALGORITHM.GSDI ) {
555 System.out.println( "Algorithm : GSDI" );
556 log_writer.println( "Algorithm : GSDI" );
558 else if ( base_algorithm == ALGORITHM.GSDIR ) {
559 System.out.println( "Algorithm : GSDIR" );
560 log_writer.println( "Algorithm : GSDIR" );
562 System.out.println( "Use most parsimonous duplication model : " + most_parsimonous_duplication_model );
563 System.out.println( "Allow stripping of gene tree nodes : " + allow_stripping_of_gene_tree );
564 log_writer.println( "Use most parsimonous duplication model : " + most_parsimonous_duplication_model );
565 log_writer.println( "Allow stripping of gene tree nodes : " + allow_stripping_of_gene_tree );
567 if ( base_algorithm == ALGORITHM.GSDI ) {
568 gsdii = new GSDI( gene_tree,
570 most_parsimonous_duplication_model,
571 allow_stripping_of_gene_tree,
575 else if ( base_algorithm == ALGORITHM.GSDIR ) {
576 gsdii = new GSDIR( gene_tree, species_tree, allow_stripping_of_gene_tree, true, transfer_taxonomy );
579 catch ( final SDIException e ) {
580 fatalError( "user error", e.getLocalizedMessage(), log_writer );
582 catch ( final IOException e ) {
583 fatalError( "error", e.toString(), log_writer );
585 catch ( final OutOfMemoryError e ) {
586 ForesterUtil.outOfMemoryError( e );
588 catch ( final Exception e ) {
590 fatalError( "unexpected error", e.toString(), log_writer );
592 System.out.println( "Running time (excluding I/O) : " + ( new Date().getTime() - start_time )
594 log_writer.println( "Running time (excluding I/O) : " + ( new Date().getTime() - start_time )
596 System.out.println( "Mapping based on : " + gsdii.getTaxCompBase() );
597 log_writer.println( "Mapping based on : " + gsdii.getTaxCompBase() );
599 final PhylogenyWriter writer = new PhylogenyWriter();
600 if ( base_algorithm == ALGORITHM.GSDIR ) {
601 final Phylogeny gt = ( ( GSDIR ) gsdii ).getMinDuplicationsSumGeneTree();
602 gt.setRerootable( false );
603 writer.toPhyloXML( out_file, gt, 0 );
606 gene_tree.setRerootable( false );
607 writer.toPhyloXML( out_file, gene_tree, 0 );
610 catch ( final IOException e ) {
611 ForesterUtil.fatalError( PRG_NAME,
612 "Failed to write to [" + out_file.getCanonicalPath() + "]: " + e.getMessage() );
614 System.out.println( "Wrote resulting gene tree to : " + out_file.getCanonicalPath() );
615 log_writer.println( "Wrote resulting gene tree to : " + out_file.getCanonicalPath() );
616 final File species_tree_used_file = new File( ForesterUtil.removeSuffix( out_file.toString() )
617 + SUFFIX_FOR_SPECIES_TREE_USED );
619 final PhylogenyWriter writer = new PhylogenyWriter();
620 writer.toPhyloXML( species_tree_used_file, species_tree, 0 );
622 catch ( final IOException e ) {
623 ForesterUtil.fatalError( PRG_NAME,
624 "Failed to write to [" + species_tree_used_file.getCanonicalPath() + "]: "
627 System.out.println( "Wrote (stripped) species tree to : " + species_tree_used_file.getCanonicalPath() );
628 log_writer.println( "Wrote (stripped) species tree to : " + species_tree_used_file.getCanonicalPath() );
629 if ( ( gsdii.getReMappedScientificNamesFromGeneTree() != null )
630 && !gsdii.getReMappedScientificNamesFromGeneTree().isEmpty() ) {
631 System.out.println( "Number of gene tree species remapped : "
632 + gsdii.getReMappedScientificNamesFromGeneTree().size() );
633 log_writer.println( "Number of gene tree species remapped : "
634 + gsdii.getReMappedScientificNamesFromGeneTree().size() );
635 writeToRemappedFile( out_file, gsdii.getReMappedScientificNamesFromGeneTree(), log_writer );
637 System.out.println( "Number of external nodes in gene tree : " + gene_tree.getNumberOfExternalNodes() );
638 log_writer.println( "Number of external nodes in gene tree : " + gene_tree.getNumberOfExternalNodes() );
639 System.out.println( "Number of external nodes in species tree : " + species_tree.getNumberOfExternalNodes() );
640 log_writer.println( "Number of external nodes in species tree : " + species_tree.getNumberOfExternalNodes() );
641 final int poly = PhylogenyMethods.countNumberOfPolytomies( species_tree );
642 System.out.println( "Number of polytomies in species tree : " + poly );
643 log_writer.println( "Number of polytomies in species tree : " + poly );
644 System.out.println( "External nodes stripped from gene tree : "
645 + gsdii.getStrippedExternalGeneTreeNodes().size() );
646 log_writer.println( "External nodes stripped from gene tree : "
647 + gsdii.getStrippedExternalGeneTreeNodes().size() );
649 .println( "External nodes stripped from species tree: " + gsdii.getStrippedSpeciesTreeNodes().size() );
651 .println( "External nodes stripped from species tree: " + gsdii.getStrippedSpeciesTreeNodes().size() );
652 System.out.println();
653 System.out.println( "Number of speciations : " + gsdii.getSpeciationsSum() );
654 log_writer.println( "Number of speciations : " + gsdii.getSpeciationsSum() );
655 if ( ( base_algorithm == ALGORITHM.GSDIR ) ) {
656 final GSDIR gsdir = ( GSDIR ) gsdii;
657 System.out.println( "Minimal number of duplications : " + gsdir.getMinDuplicationsSum() );
658 log_writer.println( "Minimal number of duplications : " + gsdir.getMinDuplicationsSum() );
660 else if ( ( base_algorithm == ALGORITHM.GSDI ) ) {
661 final GSDI gsdi = ( GSDI ) gsdii;
662 System.out.println( "Number of duplications : " + gsdi.getDuplicationsSum() );
663 log_writer.println( "Number of duplications : " + gsdi.getDuplicationsSum() );
664 if ( !most_parsimonous_duplication_model ) {
665 final int u = gsdi.getSpeciationOrDuplicationEventsSum();
666 System.out.println( "Number of potential duplications : " + u );
667 log_writer.println( "Number of potential duplications : " + u );
670 log_writer.println();
671 printMappedNodesToLog( log_writer, gsdii );
672 log_writer.println();
673 printStrippedGeneTreeNodesToLog( log_writer, gsdii );
674 System.out.println();
675 System.out.println( "Wrote log to : " + log_file.getCanonicalPath() );
676 System.out.println();
680 private final static void fatalError( final String type, final String msg, final EasyWriter log_writer ) {
683 log_writer.println();
684 log_writer.print( type.toUpperCase() + ": " );
685 log_writer.println( msg );
688 catch ( final IOException e ) {
691 ForesterUtil.fatalError( gsdi.PRG_NAME, msg );
694 private final static void print_help() {
695 System.out.println( "Usage: " + PRG_NAME
696 + " [-options] <gene tree file, or gene trees in-directory> <species tree> <outfile, or out-directory>" );
697 System.out.println();
698 System.out.println( "Options:" );
699 System.out.println( " -" + ALLOW_STRIPPING_OF_GENE_TREE_OPTION
700 + " : to allow stripping of gene tree nodes without a matching species" );
701 System.out.println( " -" + MOST_PARSIMONIOUS_OPTION
702 + " : use most parimonious duplication model for GSDI: " );
703 System.out.println( " assign nodes as speciations which would otherwise be assiged" );
704 System.out.println( " as potential duplications due to polytomies in the species tree" );
705 System.out.println( " -" + GUESS_FORMAT_OF_SPECIES_TREE
706 + " : to allow species tree in other formats than phyloXML (i.e. Newick, NHX, Nexus)" );
707 System.out.println( " -" + GSDIR_OPTION
708 + " : to use GSDIR algorithm instead of GSDI algorithm (re-rooting)" );
709 System.out.println( " -" + TRANSFER_TAXONOMY_OPTION
710 + " : to transfer taxonomic data from species tree to gene tree" );
711 System.out.println( " -" + SUFFIX_FOR_DIR_OPTION
712 + "=<suffix>: suffix for gene trees for analyzing entire directory of trees" );
713 System.out.println();
714 System.out.println();
715 System.out.println( "Gene tree(s):" );
716 System.out.println( " in phyloXM format, with taxonomy and sequence data in appropriate fields" );
717 System.out.println();
718 System.out.println( "Species tree:" );
719 System.out.println( " in phyloXML format (unless option -" + GUESS_FORMAT_OF_SPECIES_TREE + " is used)" );
720 System.out.println();
721 System.out.println( "Examples: gsdi -" + ALLOW_STRIPPING_OF_GENE_TREE_OPTION
722 + " gene_tree.xml tree_of_life.xml out.xml" );
723 System.out.println( " gsdi -" + ALLOW_STRIPPING_OF_GENE_TREE_OPTION + " -" + SUFFIX_FOR_DIR_OPTION
724 + "=.xml" + " gene_tree_dir tree_of_life.xml out_dir" );
725 System.out.println( " gsdi -" + ALLOW_STRIPPING_OF_GENE_TREE_OPTION + " -" + MOST_PARSIMONIOUS_OPTION
726 + " -" + GSDIR_OPTION + " -" + TRANSFER_TAXONOMY_OPTION + " -" + SUFFIX_FOR_DIR_OPTION + "=.xml"
727 + " gene_tree_dir tree_of_life.xml out_dir" );
728 System.out.println();
731 private final static void printMappedNodesToLog( final EasyWriter log_writer, final GSDII gsdi )
733 final SortedSet<String> ss = new TreeSet<String>();
734 for( final PhylogenyNode n : gsdi.getMappedExternalSpeciesTreeNodes() ) {
735 ss.add( n.toString() );
737 log_writer.println( "The following " + ss.size() + " species were used: " );
738 for( final String s : ss ) {
739 log_writer.println( " " + s );
743 private final static void printStrippedGeneTreeNodesToLog( final EasyWriter log_writer, final GSDII gsdi )
745 final SortedMap<String, Integer> sm = new TreeMap<String, Integer>();
746 for( final PhylogenyNode n : gsdi.getStrippedExternalGeneTreeNodes() ) {
747 final String s = n.toString();
748 if ( sm.containsKey( s ) ) {
749 sm.put( s, sm.get( s ) + 1 );
755 log_writer.println( "The following " + sm.size() + " nodes were stripped from the gene tree: " );
756 for( final String s : sm.keySet() ) {
757 final int count = sm.get( s );
759 log_writer.println( " " + s );
762 log_writer.println( " " + s + " [" + count + "]" );
767 private final static void writeToRemappedFile( final File out_file,
768 final SortedSet<String> remapped,
769 final EasyWriter log_writer )
771 final File file = new File( ForesterUtil.removeSuffix( out_file.toString() ) + REMAPPED_SUFFIX );
772 final EasyWriter remapped_writer = ForesterUtil.createEasyWriter( file );
773 for( final String s : remapped ) {
774 remapped_writer.println( s );
776 remapped_writer.close();
777 System.out.println( "Wrote remapped gene tree species to : " + file.getCanonicalPath() );
778 log_writer.println( "Wrote remapped gene tree species to : " + file.getCanonicalPath() );