871658c757b21df9deb3fc8054f46b30b1ae45d0
[jalview.git] / forester / java / src / org / forester / application / gsdi.java
1 // $Id:
2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
4 //
5 // Copyright (C) 2008-2009 Christian M. Zmasek
6 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
7 // All rights reserved
8 //
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.
13 //
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.
18 //
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
22 //
23 // Contact: phylosoft @ gmail . com
24 // WWW: www.phylosoft.org/forester
25
26 package org.forester.application;
27
28 import java.io.File;
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;
38
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.GSDIR;
52 import org.forester.sdi.SDI;
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;
61
62 public final class gsdi {
63
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 SDISE_OPTION                           = "b";
67     final static private String GSDIR_OPTION                           = "r";
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 SUFFIX_FOR_SPECIES_TREE_USED           = "_species_tree_used.xml";
73     final static private String LOGFILE_SUFFIX                         = "_gsdi_log.txt";
74     final static private String REMAPPED_SUFFIX                        = "_gsdi_remapped.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                               = "120629";
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";
81
82     public static void main( final String args[] ) {
83         try {
84             ForesterUtil.printProgramInformation( PRG_NAME,
85                                                   PRG_DESC,
86                                                   PRG_VERSION,
87                                                   PRG_DATE,
88                                                   E_MAIL,
89                                                   WWW,
90                                                   ForesterUtil.getForesterLibraryInformation() );
91             CommandLineArguments cla = null;
92             try {
93                 cla = new CommandLineArguments( args );
94             }
95             catch ( final Exception e ) {
96                 ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
97             }
98             if ( cla.isOptionSet( gsdi.HELP_OPTION_1 ) || cla.isOptionSet( gsdi.HELP_OPTION_2 ) ) {
99                 System.out.println();
100                 gsdi.print_help();
101                 System.exit( 0 );
102             }
103             else if ( ( args.length < 2 ) || ( cla.getNumberOfNames() != 3 ) ) {
104                 System.out.println();
105                 System.out.println( "Wrong number of arguments." );
106                 System.out.println();
107                 gsdi.print_help();
108                 System.exit( -1 );
109             }
110             final List<String> allowed_options = new ArrayList<String>();
111             allowed_options.add( gsdi.SDISE_OPTION );
112             allowed_options.add( gsdi.GSDIR_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 );
119             }
120             execute( cla );
121         }
122         catch ( final IOException e ) {
123             ForesterUtil.fatalError( gsdi.PRG_NAME, e.getMessage() );
124         }
125     }
126
127     private static void execute( final CommandLineArguments cla ) throws IOException {
128         ALGORITHM base_algorithm = ALGORITHM.GSDI;
129         boolean most_parsimonous_duplication_model = false;
130         boolean allow_stripping_of_gene_tree = false;
131         if ( cla.isOptionSet( gsdi.GSDIR_OPTION ) ) {
132             base_algorithm = ALGORITHM.GSDIR;
133         }
134         else if ( cla.isOptionSet( gsdi.SDISE_OPTION ) ) {
135             base_algorithm = ALGORITHM.SDI;
136         }
137         if ( cla.isOptionSet( gsdi.MOST_PARSIMONIOUS_OPTION ) ) {
138             if ( base_algorithm == ALGORITHM.SDI ) {
139                 ForesterUtil.fatalError( gsdi.PRG_NAME, "Cannot use most parsimonious duplication mode with SDI" );
140             }
141             most_parsimonous_duplication_model = true;
142         }
143         if ( cla.isOptionSet( gsdi.ALLOW_STRIPPING_OF_GENE_TREE_OPTION ) ) {
144             if ( base_algorithm == ALGORITHM.SDI ) {
145                 ForesterUtil.fatalError( gsdi.PRG_NAME, "Cannot allow stripping of gene tree with SDI" );
146             }
147             allow_stripping_of_gene_tree = true;
148         }
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;
156         try {
157             gene_tree_file = cla.getFile( 0 );
158             species_tree_file = cla.getFile( 1 );
159             out_file = cla.getFile( 2 );
160             log_file = new File( ForesterUtil.removeSuffix( out_file.toString() ) + LOGFILE_SUFFIX );
161         }
162         catch ( final IllegalArgumentException e ) {
163             ForesterUtil.fatalError( gsdi.PRG_NAME, "error in command line: " + e.getMessage() );
164         }
165         if ( ForesterUtil.isReadableFile( gene_tree_file ) != null ) {
166             ForesterUtil.fatalError( gsdi.PRG_NAME, ForesterUtil.isReadableFile( gene_tree_file ) );
167         }
168         if ( ForesterUtil.isReadableFile( species_tree_file ) != null ) {
169             ForesterUtil.fatalError( gsdi.PRG_NAME, ForesterUtil.isReadableFile( species_tree_file ) );
170         }
171         if ( ForesterUtil.isWritableFile( out_file ) != null ) {
172             ForesterUtil.fatalError( gsdi.PRG_NAME, ForesterUtil.isWritableFile( out_file ) );
173         }
174         if ( ForesterUtil.isWritableFile( log_file ) != null ) {
175             ForesterUtil.fatalError( gsdi.PRG_NAME, ForesterUtil.isWritableFile( log_file ) );
176         }
177         try {
178             log_writer = ForesterUtil.createEasyWriter( log_file );
179         }
180         catch ( final IOException e ) {
181             ForesterUtil.fatalError( gsdi.PRG_NAME, "Failed to create [" + log_file + "]: " + e.getMessage() );
182         }
183         try {
184             final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
185             gene_tree = factory.create( gene_tree_file, new PhyloXmlParser() )[ 0 ];
186         }
187         catch ( final IOException e ) {
188             fatalError( "ERROR",
189                         "Failed to read gene tree from [" + gene_tree_file + "]: " + e.getMessage(),
190                         log_writer );
191         }
192         try {
193             final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
194             final PhylogenyParser p = ParserUtils.createParserDependingOnFileType( species_tree_file, true );
195             if ( p instanceof PhyloXmlParser ) {
196                 species_tree = factory.create( species_tree_file, p )[ 0 ];
197             }
198             else {
199                 if ( REPLACE_UNDERSCORES_IN_NH_SPECIES_TREE && ( p instanceof NHXParser ) ) {
200                     ( ( NHXParser ) p ).setReplaceUnderscores( true );
201                 }
202                 species_tree = factory.create( species_tree_file, p )[ 0 ];
203                 final TaxonomyComparisonBase comp_base = SDIutil.determineTaxonomyComparisonBase( gene_tree );
204                 switch ( comp_base ) {
205                     case SCIENTIFIC_NAME:
206                         try {
207                             PhylogenyMethods
208                                     .transferNodeNameToField( species_tree,
209                                                               PhylogenyMethods.PhylogenyNodeField.TAXONOMY_SCIENTIFIC_NAME,
210                                                               true );
211                         }
212                         catch ( final PhyloXmlDataFormatException e ) {
213                             fatalError( "USER ERROR", "Failed to transfer general node name to scientific name, in ["
214                                     + species_tree_file + "]: " + e.getMessage(), log_writer );
215                         }
216                         break;
217                     case CODE:
218                         try {
219                             PhylogenyMethods
220                                     .transferNodeNameToField( species_tree,
221                                                               PhylogenyMethods.PhylogenyNodeField.TAXONOMY_CODE,
222                                                               true );
223                         }
224                         catch ( final PhyloXmlDataFormatException e ) {
225                             fatalError( "USER ERROR", "Failed to transfer general node name to taxonomy code, in ["
226                                     + species_tree_file + "]: " + e.getMessage(), log_writer );
227                         }
228                         break;
229                     case ID:
230                         try {
231                             PhylogenyMethods.transferNodeNameToField( species_tree,
232                                                                       PhylogenyMethods.PhylogenyNodeField.TAXONOMY_ID,
233                                                                       true );
234                         }
235                         catch ( final PhyloXmlDataFormatException e ) {
236                             fatalError( "USER ERROR", "Failed to transfer general node name to taxonomy id, in ["
237                                     + species_tree_file + "]: " + e.getMessage(), log_writer );
238                         }
239                         break;
240                     default:
241                         fatalError( "UNEXPECTED ERROR", "unable to determine comparison base", log_writer );
242                 }
243             }
244         }
245         catch ( final IOException e ) {
246             fatalError( "ERROR",
247                         "Failed to read species tree from [" + species_tree_file + "]: " + e.getMessage(),
248                         log_writer );
249         }
250         gene_tree.setRooted( true );
251         species_tree.setRooted( true );
252         if ( !gene_tree.isCompletelyBinary() ) {
253             fatalError( "user error", "gene tree is not completely binary", log_writer );
254         }
255         if ( base_algorithm == ALGORITHM.SDI ) {
256             if ( !species_tree.isCompletelyBinary() ) {
257                 fatalError( "user error",
258                             "species tree is not completely binary, use GSDI or GSDIR instead",
259                             log_writer );
260             }
261         }
262         log_writer.println( PRG_NAME + " - " + PRG_DESC );
263         log_writer.println( "  version         : " + PRG_VERSION );
264         log_writer.println( "  date            : " + PRG_DATE );
265         log_writer.println( "  forester version: " + ForesterConstants.FORESTER_VERSION );
266         log_writer.println();
267         log_writer.println( "Start time                               : "
268                 + new SimpleDateFormat( "yyyyMMdd HH:mm:ss" ).format( new Date() ) );
269         System.out.println( "Start time                               : "
270                 + new SimpleDateFormat( "yyyyMMdd HH:mm:ss" ).format( new Date() ) );
271         log_writer.println( "Gene tree file                           : " + gene_tree_file.getCanonicalPath() );
272         System.out.println( "Gene tree file                           : " + gene_tree_file.getCanonicalPath() );
273         log_writer.println( "Gene tree name                           : "
274                 + ( ForesterUtil.isEmpty( gene_tree.getName() ) ? "" : gene_tree.getName() ) );
275         System.out.println( "Gene tree name                           : "
276                 + ( ForesterUtil.isEmpty( gene_tree.getName() ) ? "" : gene_tree.getName() ) );
277         log_writer.println( "Species tree file                        : " + species_tree_file.getCanonicalPath() );
278         System.out.println( "Species tree file                        : " + species_tree_file.getCanonicalPath() );
279         log_writer.println( "Species tree name                        : "
280                 + ( ForesterUtil.isEmpty( species_tree.getName() ) ? "" : gene_tree.getName() ) );
281         System.out.println( "Species tree name                        : "
282                 + ( ForesterUtil.isEmpty( species_tree.getName() ) ? "" : gene_tree.getName() ) );
283         Object sdi = null;
284         final long start_time = new Date().getTime();
285         try {
286             if ( ( base_algorithm == ALGORITHM.GSDI ) || ( base_algorithm == ALGORITHM.GSDIR ) ) {
287                 if ( base_algorithm == ALGORITHM.GSDI ) {
288                     System.out.println( "Algorithm       : GSDI" );
289                     log_writer.println( "Algorithm       : GSDI" );
290                 }
291                 else if ( base_algorithm == ALGORITHM.GSDIR ) {
292                     System.out.println( "Algorithm       : GSDIR" );
293                     log_writer.println( "Algorithm       : GSDIR" );
294                 }
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 );
299                 log_writer.flush();
300                 if ( base_algorithm == ALGORITHM.GSDI ) {
301                     sdi = new GSDI( gene_tree,
302                                     species_tree,
303                                     most_parsimonous_duplication_model,
304                                     allow_stripping_of_gene_tree,
305                                     true );
306                 }
307                 else if ( base_algorithm == ALGORITHM.GSDIR ) {
308                     sdi = new GSDIR( gene_tree, species_tree, allow_stripping_of_gene_tree, true );
309                 }
310             }
311             else {
312                 System.out.println();
313                 System.out.println( "Algorithm       : SDI" );
314                 log_writer.println( "Algorithm       : SDI" );
315                 log_writer.flush();
316                 sdi = new SDI( gene_tree, species_tree );
317             }
318         }
319         catch ( final SDIException e ) {
320             fatalError( "user error", e.getLocalizedMessage(), log_writer );
321         }
322         catch ( final IOException e ) {
323             fatalError( "error", e.toString(), log_writer );
324         }
325         catch ( final Exception e ) {
326             e.printStackTrace();
327             fatalError( "unexpected error", e.toString(), log_writer );
328         }
329         System.out.println( "Running time (excluding I/O)             : " + ( new Date().getTime() - start_time )
330                 + "ms" );
331         log_writer.println( "Running time (excluding I/O)             : " + ( new Date().getTime() - start_time )
332                 + "ms" );
333         if ( ( base_algorithm == ALGORITHM.GSDI ) ) {
334             final GSDI gsdi = ( GSDI ) sdi;
335             System.out.println( "Mapping based on                         : " + gsdi.getTaxCompBase() );
336             log_writer.println( "Mapping based on                         : " + gsdi.getTaxCompBase() );
337         }
338         if ( ( base_algorithm == ALGORITHM.GSDIR ) ) {
339             final GSDIR gsdir = ( GSDIR ) sdi;
340             System.out.println( "Mapping based on                         : " + gsdir.getTaxCompBase() );
341             log_writer.println( "Mapping based on                         : " + gsdir.getTaxCompBase() );
342             System.out.println( "Minimal duplications sum                 : " + gsdir.getMinDuplicationsSum() );
343             log_writer.println( "Minimal duplications sum                 : " + gsdir.getMinDuplicationsSum() );
344             System.out.println( "Duplications sum statistics              : " + gsdir.getMinDuplicationsSum() );
345             log_writer.println( "Duplications sum statistics              : " + gsdir.getMinDuplicationsSum() );
346         }
347         try {
348             final PhylogenyWriter writer = new PhylogenyWriter();
349             if ( base_algorithm == ALGORITHM.GSDIR ) {
350                 writer.toPhyloXML( out_file,
351                                    ( ( GSDIR ) sdi ).getMinDuplicationsSumGeneTrees(),
352                                    0,
353                                    ForesterUtil.LINE_SEPARATOR );
354             }
355             else {
356                 writer.toPhyloXML( out_file, gene_tree, 0 );
357             }
358         }
359         catch ( final IOException e ) {
360             ForesterUtil.fatalError( PRG_NAME,
361                                      "Failed to write to [" + out_file.getCanonicalPath() + "]: " + e.getMessage() );
362         }
363         System.out.println( "Wrote resulting gene tree to             : " + out_file.getCanonicalPath() );
364         log_writer.println( "Wrote resulting gene tree to             : " + out_file.getCanonicalPath() );
365         if ( base_algorithm == ALGORITHM.SDI ) {
366             sdi = sdi;
367             sdi.computeMappingCostL();
368             System.out.println( "Mapping cost                             : " + sdi.computeMappingCostL() );
369             log_writer.println( "Mapping cost                             : " + sdi.computeMappingCostL() );
370         }
371         else if ( ( base_algorithm == ALGORITHM.GSDI ) || ( base_algorithm == ALGORITHM.GSDIR ) ) {
372             final GSDI gsdi = ( GSDI ) sdi;
373             final File species_tree_used_file = new File( ForesterUtil.removeSuffix( out_file.toString() )
374                     + SUFFIX_FOR_SPECIES_TREE_USED );
375             try {
376                 final PhylogenyWriter writer = new PhylogenyWriter();
377                 writer.toPhyloXML( species_tree_used_file, gsdi.getSpeciesTree(), 0 );
378             }
379             catch ( final IOException e ) {
380                 ForesterUtil.fatalError( PRG_NAME, "Failed to write to [" + species_tree_used_file.getCanonicalPath()
381                         + "]: " + e.getMessage() );
382             }
383             System.out.println( "Wrote (stripped) species tree to         : "
384                     + species_tree_used_file.getCanonicalPath() );
385             log_writer.println( "Wrote (stripped) species tree to         : "
386                     + species_tree_used_file.getCanonicalPath() );
387             if ( ( gsdi.getReMappedScientificNamesFromGeneTree() != null )
388                     && !gsdi.getReMappedScientificNamesFromGeneTree().isEmpty() ) {
389                 System.out.println( "Number of gene tree species remapped     : "
390                         + gsdi.getReMappedScientificNamesFromGeneTree().size() );
391                 log_writer.println( "Number of gene tree species remapped     : "
392                         + gsdi.getReMappedScientificNamesFromGeneTree().size() );
393                 writeToRemappedFile( out_file, gsdi.getReMappedScientificNamesFromGeneTree(), log_writer );
394             }
395         }
396         System.out.println( "Number of external nodes in gene tree    : " + gene_tree.getNumberOfExternalNodes() );
397         log_writer.println( "Number of external nodes in gene tree    : " + gene_tree.getNumberOfExternalNodes() );
398         System.out.println( "Number of external nodes in species tree : "
399                 + sdi.getSpeciesTree().getNumberOfExternalNodes() );
400         log_writer.println( "Number of external nodes in species tree : "
401                 + sdi.getSpeciesTree().getNumberOfExternalNodes() );
402         if ( ( base_algorithm == ALGORITHM.GSDI ) ) {
403             final GSDI gsdi = ( GSDI ) sdi;
404             final int poly = PhylogenyMethods.countNumberOfPolytomies( gsdi.getSpeciesTree() );
405             System.out.println( "Number of polytomies in species tree     : " + poly );
406             log_writer.println( "Number of polytomies in species tree     : " + poly );
407             System.out.println( "External nodes stripped from gene tree   : "
408                     + gsdi.getStrippedExternalGeneTreeNodes().size() );
409             log_writer.println( "External nodes stripped from gene tree   : "
410                     + gsdi.getStrippedExternalGeneTreeNodes().size() );
411             System.out.println( "External nodes stripped from species tree: "
412                     + gsdi.getStrippedSpeciesTreeNodes().size() );
413             log_writer.println( "External nodes stripped from species tree: "
414                     + gsdi.getStrippedSpeciesTreeNodes().size() );
415         }
416         System.out.println();
417         System.out.println( "Number of duplications                   : " + sdi.getDuplicationsSum() );
418         log_writer.println( "Number of duplications                   : " + sdi.getDuplicationsSum() );
419         if ( ( base_algorithm == ALGORITHM.GSDI ) ) {
420             final GSDI gsdi = ( GSDI ) sdi;
421             if ( !most_parsimonous_duplication_model ) {
422                 final int u = gsdi.getSpeciationOrDuplicationEventsSum();
423                 System.out.println( "Number of potential duplications         : " + u );
424                 log_writer.println( "Number of potential duplications         : " + u );
425             }
426             System.out.println( "Number of speciations                    : " + gsdi.getSpeciationsSum() );
427             log_writer.println( "Number of speciations                    : " + gsdi.getSpeciationsSum() );
428             log_writer.println();
429             printMappedNodesToLog( log_writer, gsdi );
430             log_writer.println();
431             printStrippedGeneTreeNodesToLog( log_writer, gsdi );
432         }
433         System.out.println();
434         System.out.println( "Wrote log to                             : " + log_file.getCanonicalPath() );
435         System.out.println();
436         log_writer.close();
437     }
438
439     private static void writeToRemappedFile( final File out_file,
440                                              final SortedSet<String> remapped,
441                                              final EasyWriter log_writer ) throws IOException {
442         final File file = new File( ForesterUtil.removeSuffix( out_file.toString() ) + REMAPPED_SUFFIX );
443         final EasyWriter remapped_writer = ForesterUtil.createEasyWriter( file );
444         for( final String s : remapped ) {
445             remapped_writer.println( s );
446         }
447         remapped_writer.close();
448         System.out.println( "Wrote remapped gene tree species to      : " + file.getCanonicalPath() );
449         log_writer.println( "Wrote remapped gene tree species to      : " + file.getCanonicalPath() );
450     }
451
452     private static void printMappedNodesToLog( final EasyWriter log_writer, final GSDI gsdi ) throws IOException {
453         final SortedSet<String> ss = new TreeSet<String>();
454         for( final PhylogenyNode n : gsdi.getMappedExternalSpeciesTreeNodes() ) {
455             ss.add( n.toString() );
456         }
457         log_writer.println( "The following " + ss.size() + " species were used: " );
458         for( final String s : ss ) {
459             log_writer.println( "  " + s );
460         }
461     }
462
463     private static void fatalError( final String type, final String msg, final EasyWriter log_writer ) {
464         try {
465             log_writer.flush();
466             log_writer.println();
467             log_writer.print( type.toUpperCase() + ": " );
468             log_writer.println( msg );
469             log_writer.close();
470         }
471         catch ( final IOException e ) {
472             e.printStackTrace();
473         }
474         ForesterUtil.fatalError( gsdi.PRG_NAME, msg );
475     }
476
477     private static void printStrippedGeneTreeNodesToLog( final EasyWriter log_writer, final GSDI gsdi )
478             throws IOException {
479         final SortedMap<String, Integer> sm = new TreeMap<String, Integer>();
480         for( final PhylogenyNode n : gsdi.getStrippedExternalGeneTreeNodes() ) {
481             final String s = n.toString();
482             if ( sm.containsKey( s ) ) {
483                 sm.put( s, sm.get( s ) + 1 );
484             }
485             else {
486                 sm.put( s, 1 );
487             }
488         }
489         log_writer.println( "The following " + sm.size() + " nodes were stripped from the gene tree: " );
490         for( final String s : sm.keySet() ) {
491             final int count = sm.get( s );
492             if ( count == 1 ) {
493                 log_writer.println( "  " + s );
494             }
495             else {
496                 log_writer.println( "  " + s + " [" + count + "]" );
497             }
498         }
499     }
500
501     private static void print_help() {
502         System.out.println( "Usage: " + gsdi.PRG_NAME
503                 + " [-options] <gene tree in phyloXML format> <species tree> <outfile>" );
504         System.out.println();
505         System.out.println( "Options:" );
506         System.out.println( " -" + gsdi.ALLOW_STRIPPING_OF_GENE_TREE_OPTION
507                 + ": to allow stripping of gene tree nodes without a matching species" );
508         System.out.println( " -" + gsdi.MOST_PARSIMONIOUS_OPTION
509                 + ": use most parimonious duplication model for GSDI: " );
510         System.out.println( "     assign nodes as speciations which would otherwise be assiged" );
511         System.out.println( "     as potential duplications due to polytomies in the species tree" );
512         System.out.println( " -" + gsdi.GUESS_FORMAT_OF_SPECIES_TREE
513                 + ": to allow species tree in other formats than phyloXML (i.e. Newick, NHX, Nexus)" );
514         System.out.println( " -" + gsdi.SDISE_OPTION
515                 + ": to use SDIse algorithm instead of GSDI algorithm (for binary species trees)" );
516         System.out.println( " -" + gsdi.GSDIR_OPTION
517                 + ": to use GSDIR algorithm instead of GSDI algorithm (re-rooting)" );
518         System.out.println();
519         System.out.println( "Gene tree:" );
520         System.out.println( " in phyloXM format, with taxonomy and sequence data in appropriate fields" );
521         System.out.println();
522         System.out.println( "Species tree:" );
523         System.out.println( " in phyloXML format (unless option -" + gsdi.GUESS_FORMAT_OF_SPECIES_TREE + " is used)" );
524         System.out.println();
525         System.out.println( "Example: gsdi -" + ALLOW_STRIPPING_OF_GENE_TREE_OPTION
526                 + " gene_tree.xml tree_of_life.xml out.xml" );
527         System.out.println();
528     }
529 }