4c5d0cd84bdd40d503c3a7b9f5bee9bcab3c577b
[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
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.SDIse;
50 import org.forester.sdi.SdiException;
51 import org.forester.util.CommandLineArguments;
52 import org.forester.util.EasyWriter;
53 import org.forester.util.ForesterConstants;
54 import org.forester.util.ForesterUtil;
55
56 public final class gsdi {
57
58     private enum BASE_ALGORITHM {
59         GSDI, SDI
60     }
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";
78
79     public static void main( final String args[] ) {
80         try {
81             ForesterUtil.printProgramInformation( PRG_NAME,
82                                                   PRG_DESC,
83                                                   PRG_VERSION,
84                                                   PRG_DATE,
85                                                   E_MAIL,
86                                                   WWW,
87                                                   ForesterUtil.getForesterLibraryInformation() );
88             CommandLineArguments cla = null;
89             try {
90                 cla = new CommandLineArguments( args );
91             }
92             catch ( final Exception e ) {
93                 ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
94             }
95             if ( cla.isOptionSet( gsdi.HELP_OPTION_1 ) || cla.isOptionSet( gsdi.HELP_OPTION_2 ) ) {
96                 System.out.println();
97                 gsdi.print_help();
98                 System.exit( 0 );
99             }
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();
104                 gsdi.print_help();
105                 System.exit( -1 );
106             }
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 );
115             }
116             execute( cla );
117         }
118         catch ( final IOException e ) {
119             ForesterUtil.fatalError( gsdi.PRG_NAME, e.getMessage() );
120         }
121     }
122
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;
130         }
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" );
134             }
135             most_parsimonous_duplication_model = true;
136         }
137         if ( cla.isOptionSet( gsdi.GUESS_FORMAT_OF_SPECIES_TREE ) ) {
138             species_tree_in_phyloxml = false;
139         }
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" );
143             }
144             allow_stripping_of_gene_tree = true;
145         }
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;
153         try {
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 );
158             }
159             else {
160                 out_file = new File( ForesterUtil.removeSuffix( gene_tree_file.toString() ) + DEFAULT_OUTFILE_SUFFIX );
161             }
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             ForesterUtil.fatalError( gsdi.PRG_NAME,
191                                      "Failed to read gene tree from [" + gene_tree_file + "]: " + e.getMessage() );
192         }
193         try {
194             final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
195             if ( species_tree_in_phyloxml ) {
196                 species_tree = factory.create( species_tree_file, new PhyloXmlParser() )[ 0 ];
197             }
198             else {
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 );
202                 }
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:
207                         try {
208                             PhylogenyMethods
209                                     .transferNodeNameToField( species_tree,
210                                                               PhylogenyMethods.PhylogenyNodeField.TAXONOMY_SCIENTIFIC_NAME,
211                                                               true );
212                         }
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() );
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                             ForesterUtil.fatalError( gsdi.PRG_NAME,
228                                                      "Failed to transfer general node name to taxonomy code, in ["
229                                                              + species_tree_file + "]: " + e.getMessage() );
230                         }
231                         break;
232                     case ID:
233                         try {
234                             PhylogenyMethods.transferNodeNameToField( species_tree,
235                                                                       PhylogenyMethods.PhylogenyNodeField.TAXONOMY_ID,
236                                                                       true );
237                         }
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() );
242                         }
243                         break;
244                     default:
245                         ForesterUtil.fatalError( gsdi.PRG_NAME, "unable to determine comparison base" );
246                 }
247             }
248         }
249         catch ( final IOException e ) {
250             ForesterUtil.fatalError( gsdi.PRG_NAME, "Failed to read species tree from [" + species_tree_file + "]: "
251                     + e.getMessage() );
252         }
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" );
257             log_writer.close();
258             ForesterUtil.fatalError( gsdi.PRG_NAME, "gene tree is not completely binary" );
259         }
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" );
263                 log_writer.close();
264                 ForesterUtil.fatalError( gsdi.PRG_NAME, "species tree is not completely binary, use GSDI instead" );
265             }
266         }
267         // For timing.
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();
291         SDI sdi = null;
292         final long start_time = new Date().getTime();
293         try {
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 );
300                 log_writer.flush();
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 {
308                 System.out.println();
309                 System.out.println( "Using SDIse algorithm" );
310                 log_writer.println( "Using SDIse algorithm" );
311                 log_writer.flush();
312                 sdi = new SDIse( gene_tree, species_tree );
313             }
314         }
315         catch ( final SdiException e ) {
316             log_writer.println( "User Error: " + e.getLocalizedMessage() );
317             log_writer.close();
318             ForesterUtil.fatalError( PRG_NAME, e.getLocalizedMessage() );
319         }
320         catch ( final IOException e ) {
321             log_writer.println( "Error: " + e );
322             log_writer.close();
323             ForesterUtil.fatalError( PRG_NAME, e.toString() );
324         }
325         catch ( final Exception e ) {
326             log_writer.println( "Error: " + e );
327             log_writer.close();
328             e.printStackTrace();
329             System.exit( -1 );
330         }
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" );
334         try {
335             final PhylogenyWriter writer = new PhylogenyWriter();
336             writer.toPhyloXML( out_file, gene_tree, 0 );
337         }
338         catch ( final IOException e ) {
339             ForesterUtil.fatalError( PRG_NAME, "Failed to write to [" + out_file + "]: " + e.getMessage() );
340         }
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() );
349         }
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 );
355             try {
356                 final PhylogenyWriter writer = new PhylogenyWriter();
357                 writer.toPhyloXML( species_tree_used_file, gsdi.getSpeciesTree(), 0 );
358             }
359             catch ( final IOException e ) {
360                 ForesterUtil.fatalError( PRG_NAME,
361                                          "Failed to write to [" + species_tree_used_file + "]: " + e.getMessage() );
362             }
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 );
371             }
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() );
377             }
378         }
379         System.out.println();
380         log_writer.close();
381         //  some stat on gene tree:
382         //      filename, name
383         //      number of external nodes, strppided nodes
384         //  some stats on sepcies tree, external nodes,
385         //  filename, name
386         //  internal nodes
387         //  how many of which are polytomies
388         //wrote log file to
389         //  if ( allow_stripping_of_gene_tree ) {
390         //      stripped x nodes, y external nodes remain
391         //  }
392     }
393
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:" );
409         System.out
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();
422     }
423 }