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