in progress
[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.PhyloXmlParser;
38 import org.forester.io.parsers.util.ParserUtils;
39 import org.forester.io.writers.PhylogenyWriter;
40 import org.forester.phylogeny.Phylogeny;
41 import org.forester.phylogeny.PhylogenyMethods;
42 import org.forester.phylogeny.PhylogenyNode;
43 import org.forester.phylogeny.factories.ParserBasedPhylogenyFactory;
44 import org.forester.phylogeny.factories.PhylogenyFactory;
45 import org.forester.sdi.GSDI;
46 import org.forester.sdi.SDI;
47 import org.forester.sdi.SDI.TaxonomyComparisonBase;
48 import org.forester.sdi.SDIse;
49 import org.forester.sdi.SdiException;
50 import org.forester.util.CommandLineArguments;
51 import org.forester.util.EasyWriter;
52 import org.forester.util.ForesterConstants;
53 import org.forester.util.ForesterUtil;
54
55 public final class gsdi {
56
57     private enum BASE_ALGORITHM {
58         GSDI, SDI
59     }
60     final static public boolean REPLACE_UNDERSCORES_IN_NH_SPECIES_TREE     = true;
61     final static private String ALLOW_STRIPPING_OF_GENE_TREE_OPTION        = "g";
62     final static private String SDI_OPTION                                 = "b";
63     final static private String MOST_PARSIMONIOUS_OPTION                   = "m";
64     final static private String GUESS_FORMAT_OF_SPECIES_TREE               = "q";
65     final static private String HELP_OPTION_1                              = "help";
66     final static private String HELP_OPTION_2                              = "h";
67     final static private String DEFAULT_OUTFILE_SUFFIX                     = "_gsdi_out.xml";
68     final static private String SUFFIX_FOR_LIST_OF_STIPPED_GENE_TREE_NODES = "_stripped_gene_tree_nodes.txt";
69     final static private String SUFFIX_FOR_SPECIES_TREE_USED               = "_species_tree_used.xml";
70     final static private String LOGFILE_SUFFIX                             = "_gsdi_log.txt";
71     final static private String PRG_NAME                                   = "gsdi";
72     final static private String PRG_VERSION                                = "0.901";
73     final static private String PRG_DATE                                   = "120608";
74     final static private String PRG_DESC                                   = "general speciation duplication inference";
75     final static private String E_MAIL                                     = "phylosoft@gmail.com";
76     final static private String WWW                                        = "www.phylosoft.org/forester";
77
78     public static void main( final String args[] ) {
79         try {
80             ForesterUtil.printProgramInformation( PRG_NAME,
81                                                   PRG_DESC,
82                                                   PRG_VERSION,
83                                                   PRG_DATE,
84                                                   E_MAIL,
85                                                   WWW,
86                                                   ForesterUtil.getForesterLibraryInformation() );
87             CommandLineArguments cla = null;
88             try {
89                 cla = new CommandLineArguments( args );
90             }
91             catch ( final Exception e ) {
92                 ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
93             }
94             if ( cla.isOptionSet( gsdi.HELP_OPTION_1 ) || cla.isOptionSet( gsdi.HELP_OPTION_2 ) ) {
95                 System.out.println();
96                 gsdi.print_help();
97                 System.exit( 0 );
98             }
99             else if ( ( args.length < 2 ) || ( cla.getNumberOfNames() < 2 ) || ( cla.getNumberOfNames() > 3 ) ) {
100                 System.out.println();
101                 System.out.println( "Wrong number of arguments." );
102                 System.out.println();
103                 gsdi.print_help();
104                 System.exit( -1 );
105             }
106             final List<String> allowed_options = new ArrayList<String>();
107             allowed_options.add( gsdi.SDI_OPTION );
108             allowed_options.add( gsdi.GUESS_FORMAT_OF_SPECIES_TREE );
109             allowed_options.add( gsdi.MOST_PARSIMONIOUS_OPTION );
110             allowed_options.add( gsdi.ALLOW_STRIPPING_OF_GENE_TREE_OPTION );
111             final String dissallowed_options = cla.validateAllowedOptionsAsString( allowed_options );
112             if ( dissallowed_options.length() > 0 ) {
113                 ForesterUtil.fatalError( gsdi.PRG_NAME, "unknown option(s): " + dissallowed_options );
114             }
115             execute( cla );
116         }
117         catch ( final IOException e ) {
118             ForesterUtil.fatalError( gsdi.PRG_NAME, e.getMessage() );
119         }
120     }
121
122     private static void execute( final CommandLineArguments cla ) throws IOException {
123         BASE_ALGORITHM base_algorithm = BASE_ALGORITHM.GSDI;
124         boolean most_parsimonous_duplication_model = false;
125         boolean species_tree_in_phyloxml = true;
126         boolean allow_stripping_of_gene_tree = false;
127         if ( cla.isOptionSet( gsdi.SDI_OPTION ) ) {
128             base_algorithm = BASE_ALGORITHM.SDI;
129         }
130         if ( cla.isOptionSet( gsdi.MOST_PARSIMONIOUS_OPTION ) ) {
131             if ( base_algorithm != BASE_ALGORITHM.GSDI ) {
132                 ForesterUtil.fatalError( gsdi.PRG_NAME, "Can only use most parsimonious duplication mode with GSDI" );
133             }
134             most_parsimonous_duplication_model = true;
135         }
136         if ( cla.isOptionSet( gsdi.GUESS_FORMAT_OF_SPECIES_TREE ) ) {
137             species_tree_in_phyloxml = false;
138         }
139         if ( cla.isOptionSet( gsdi.ALLOW_STRIPPING_OF_GENE_TREE_OPTION ) ) {
140             if ( base_algorithm != BASE_ALGORITHM.GSDI ) {
141                 ForesterUtil.fatalError( gsdi.PRG_NAME, "Can only allow stripping of gene tree with GSDI" );
142             }
143             allow_stripping_of_gene_tree = true;
144         }
145         Phylogeny species_tree = null;
146         Phylogeny gene_tree = null;
147         File gene_tree_file = null;
148         File species_tree_file = null;
149         File out_file = null;
150         File log_file = null;
151         EasyWriter log_writer = null;
152         try {
153             gene_tree_file = cla.getFile( 0 );
154             species_tree_file = cla.getFile( 1 );
155             if ( cla.getNumberOfNames() == 3 ) {
156                 out_file = cla.getFile( 2 );
157             }
158             else {
159                 out_file = new File( ForesterUtil.removeSuffix( gene_tree_file.toString() ) + DEFAULT_OUTFILE_SUFFIX );
160             }
161             log_file = new File( ForesterUtil.removeSuffix( out_file.toString() ) + LOGFILE_SUFFIX );
162         }
163         catch ( final IllegalArgumentException e ) {
164             ForesterUtil.fatalError( gsdi.PRG_NAME, "error in command line: " + e.getMessage() );
165         }
166         if ( ForesterUtil.isReadableFile( gene_tree_file ) != null ) {
167             ForesterUtil.fatalError( gsdi.PRG_NAME, ForesterUtil.isReadableFile( gene_tree_file ) );
168         }
169         if ( ForesterUtil.isReadableFile( species_tree_file ) != null ) {
170             ForesterUtil.fatalError( gsdi.PRG_NAME, ForesterUtil.isReadableFile( species_tree_file ) );
171         }
172         if ( ForesterUtil.isWritableFile( out_file ) != null ) {
173             ForesterUtil.fatalError( gsdi.PRG_NAME, ForesterUtil.isWritableFile( out_file ) );
174         }
175         if ( ForesterUtil.isWritableFile( log_file ) != null ) {
176             ForesterUtil.fatalError( gsdi.PRG_NAME, ForesterUtil.isWritableFile( log_file ) );
177         }
178         try {
179             log_writer = ForesterUtil.createEasyWriter( log_file );
180         }
181         catch ( final IOException e ) {
182             ForesterUtil.fatalError( gsdi.PRG_NAME, "Failed to create [" + log_file + "]: " + e.getMessage() );
183         }
184         try {
185             final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
186             gene_tree = factory.create( gene_tree_file, new PhyloXmlParser() )[ 0 ];
187         }
188         catch ( final IOException e ) {
189             ForesterUtil.fatalError( gsdi.PRG_NAME,
190                                      "Failed to read gene tree from [" + gene_tree_file + "]: " + e.getMessage() );
191         }
192         try {
193             final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
194             if ( species_tree_in_phyloxml ) {
195                 species_tree = factory.create( species_tree_file, new PhyloXmlParser() )[ 0 ];
196             }
197             else {
198                 final PhylogenyParser p = ParserUtils.createParserDependingOnFileType( species_tree_file, true );
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 = GSDI.determineTaxonomyComparisonBase( gene_tree );
204                 switch ( comp_base ) {
205                     case SCIENTIFIC_NAME:
206                         PhylogenyMethods
207                                 .transferNodeNameToField( species_tree,
208                                                           PhylogenyMethods.PhylogenyNodeField.TAXONOMY_ID_UNIPROT_1,
209                                                           true );
210                         break;
211                     case CODE:
212                         PhylogenyMethods.transferNodeNameToField( species_tree,
213                                                                   PhylogenyMethods.PhylogenyNodeField.TAXONOMY_CODE,
214                                                                   true );
215                         break;
216                     case ID:
217                         PhylogenyMethods.transferNodeNameToField( species_tree,
218                                                                   PhylogenyMethods.PhylogenyNodeField.TAXONOMY_ID,
219                                                                   true );
220                         break;
221                     default:
222                         ForesterUtil.fatalError( gsdi.PRG_NAME, "unable to determine comparison base" );
223                 }
224             }
225         }
226         catch ( final IOException e ) {
227             ForesterUtil.fatalError( gsdi.PRG_NAME,
228                                      "Failed to read species tree from [" + gene_tree_file + "]: " + e.getMessage() );
229         }
230         gene_tree.setRooted( true );
231         species_tree.setRooted( true );
232         if ( !gene_tree.isCompletelyBinary() ) {
233             log_writer.println( "User Error: gene tree is not completely binary" );
234             log_writer.close();
235             ForesterUtil.fatalError( gsdi.PRG_NAME, "gene tree is not completely binary" );
236         }
237         if ( base_algorithm != BASE_ALGORITHM.GSDI ) {
238             if ( !species_tree.isCompletelyBinary() ) {
239                 log_writer.println( "User Error: species tree is not completely binary, use GSDI instead" );
240                 log_writer.close();
241                 ForesterUtil.fatalError( gsdi.PRG_NAME, "species tree is not completely binary, use GSDI instead" );
242             }
243         }
244         // For timing.
245         // gene_tree = Helper.createBalancedTree( 10 );
246         // species_tree = Helper.createBalancedTree( 13 );
247         // species_tree = Helper.createUnbalancedTree( 1024 );
248         // gene_tree = Helper.createUnbalancedTree( 8192 );
249         // species_tree = gene_tree.copyTree();
250         // gene_tree = species_tree.copyTree();
251         // Helper.numberSpeciesInOrder( species_tree );
252         // Helper.numberSpeciesInOrder( gene_tree );
253         // Helper.randomizeSpecies( 1, 8192, gene_tree );
254         // Helper.intervalNumberSpecies( gene_tree, 4096 );
255         // Helper.numberSpeciesInDescOrder( gene_tree );
256         log_writer.println( PRG_NAME + " - " + PRG_DESC );
257         log_writer.println( "  version         : " + PRG_VERSION );
258         log_writer.println( "  date            : " + PRG_DATE );
259         log_writer.println( "  forester version: " + ForesterConstants.FORESTER_VERSION );
260         log_writer.println( "Start time: " + new SimpleDateFormat( "yyyyMMdd HH:mm:ss" ).format( new Date() ) );
261         log_writer.println( "Gene tree file: " + gene_tree_file.getCanonicalPath() );
262         log_writer.println( "Gene tree name: "
263                 + ( ForesterUtil.isEmpty( gene_tree.getName() ) ? "" : gene_tree.getName() ) );
264         log_writer.println( "Species tree file: " + species_tree_file.getCanonicalPath() );
265         log_writer.println( "Species tree name: "
266                 + ( ForesterUtil.isEmpty( species_tree.getName() ) ? "" : gene_tree.getName() ) );
267         System.out.println();
268         SDI sdi = null;
269         final long start_time = new Date().getTime();
270         try {
271             if ( base_algorithm == BASE_ALGORITHM.GSDI ) {
272                 System.out.println();
273                 System.out.println( "Use most parsimonous duplication model: " + most_parsimonous_duplication_model );
274                 System.out.println( "Allow stripping of gene tree nodes    : " + allow_stripping_of_gene_tree );
275                 log_writer.println( "Use most parsimonous duplication model: " + most_parsimonous_duplication_model );
276                 log_writer.write( "Allow stripping of gene tree nodes    : " + allow_stripping_of_gene_tree );
277                 log_writer.flush();
278                 sdi = new GSDI( gene_tree,
279                                 species_tree,
280                                 most_parsimonous_duplication_model,
281                                 allow_stripping_of_gene_tree,
282                                 true );
283             }
284             else {
285                 System.out.println();
286                 System.out.println( "Using SDIse algorithm" );
287                 log_writer.println( "Using SDIse algorithm" );
288                 log_writer.flush();
289                 sdi = new SDIse( gene_tree, species_tree );
290             }
291         }
292         catch ( final SdiException e ) {
293             log_writer.println( "User Error: " + e.getLocalizedMessage() );
294             log_writer.close();
295             ForesterUtil.fatalError( PRG_NAME, e.getLocalizedMessage() );
296         }
297         catch ( final IOException e ) {
298             log_writer.println( "Error: " + e );
299             log_writer.close();
300             ForesterUtil.fatalError( PRG_NAME, e.toString() );
301         }
302         catch ( final Exception e ) {
303             log_writer.println( "Error: " + e );
304             log_writer.close();
305             e.printStackTrace();
306             System.exit( -1 );
307         }
308         System.out.println();
309         System.out.println( "Running time (excluding I/O): " + ( new Date().getTime() - start_time ) + "ms" );
310         log_writer.println( "Running time (excluding I/O): " + ( new Date().getTime() - start_time ) + "ms" );
311         try {
312             final PhylogenyWriter writer = new PhylogenyWriter();
313             writer.toPhyloXML( out_file, gene_tree, 0 );
314         }
315         catch ( final IOException e ) {
316             ForesterUtil.fatalError( PRG_NAME, "Failed to write to [" + out_file + "]: " + e.getMessage() );
317         }
318         System.out.println();
319         System.out.println( "Wrote resulting gene tree to: " + out_file );
320         System.out.println();
321         log_writer.println( "Wrote resulting gene tree to: " + out_file );
322         if ( base_algorithm == BASE_ALGORITHM.SDI ) {
323             sdi.computeMappingCostL();
324             System.out.println( "Mapping cost                    : " + sdi.computeMappingCostL() );
325             log_writer.println( "Mapping cost                    : " + sdi.computeMappingCostL() );
326         }
327         System.out.println( "Number of duplications          : " + sdi.getDuplicationsSum() );
328         log_writer.println( "Number of duplications          : " + sdi.getDuplicationsSum() );
329         if ( ( base_algorithm == BASE_ALGORITHM.GSDI ) ) {
330             final GSDI gsdi = ( GSDI ) sdi;
331             final File species_tree_used_file = new File( out_file + SUFFIX_FOR_SPECIES_TREE_USED );
332             try {
333                 final PhylogenyWriter writer = new PhylogenyWriter();
334                 writer.toPhyloXML( species_tree_used_file, gsdi.getSpeciesTree(), 0 );
335             }
336             catch ( final IOException e ) {
337                 ForesterUtil.fatalError( PRG_NAME,
338                                          "Failed to write to [" + species_tree_used_file + "]: " + e.getMessage() );
339             }
340             System.out.println();
341             System.out.println( "Wrote used species tree to: " + species_tree_used_file );
342             System.out.println();
343             log_writer.println( "Wrote used species tree to: " + species_tree_used_file );
344             if ( !most_parsimonous_duplication_model ) {
345                 final int duplications = gsdi.getSpeciationOrDuplicationEventsSum();
346                 System.out.println( "Number of potential duplications: " + duplications );
347                 log_writer.println( "Number of potential duplications: " + duplications );
348             }
349             final int spec = gsdi.getSpeciationsSum();
350             System.out.println( "Number of speciations            : " + spec );
351             log_writer.println( "Number of speciations            : " + spec );
352             for( final PhylogenyNode n : gsdi.getMappedExternalSpeciesTreeNodes() ) {
353                 System.out.println( n.toString() );
354             }
355         }
356         System.out.println();
357         log_writer.close();
358         //  some stat on gene tree:
359         //      filename, name
360         //      number of external nodes, strppided nodes
361         //  some stats on sepcies tree, external nodes,
362         //  filename, name
363         //  internal nodes
364         //  how many of which are polytomies
365         //wrote log file to
366         //  if ( allow_stripping_of_gene_tree ) {
367         //      stripped x nodes, y external nodes remain
368         //  }
369     }
370
371     private static void print_help() {
372         System.out.println( "Usage: " + gsdi.PRG_NAME
373                 + " [-options] <gene tree in phyloXML format> <species tree in phyloXML format> [outfile]" );
374         System.out.println();
375         System.out.println( "Options:" );
376         //    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 " + );
377         System.out.println( " -" + gsdi.SDI_OPTION + ": to use SDI algorithm instead of GSDI algorithm" );//TODO gsdi.ALLOW_STRIPPING_OF_GENE_TREE_OPTION not allowed
378         System.out.println( " -" + gsdi.MOST_PARSIMONIOUS_OPTION
379                 + ": use most parimonious duplication model for GSDI: " );
380         System.out.println( "     assign nodes as speciations which would otherwise be assiged" );
381         System.out.println( "     as unknown because of polytomies in the species tree" );
382         System.out.println( " -" + gsdi.GUESS_FORMAT_OF_SPECIES_TREE
383                 + ": to allow species tree in other formats than phyloXML (i.e. Newick, NHX, Nexus)" );
384         System.out.println();
385         System.out.println( "Species tree:" );
386         System.out
387                 .println( " In phyloXML format (unless option -"
388                         + gsdi.GUESS_FORMAT_OF_SPECIES_TREE
389                         + " 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" );
390         System.out.println();
391         System.out.println( "Gene tree:" );
392         System.out.println( " In phyloXM format, with taxonomy and sequence data in appropriate fields" );
393         System.out.println();
394         System.out.println( "Example:" );
395         //  System.out.println( "gsdi 
396         //    System.out.println();
397         System.out.println( "Note -- GSDI algorithm is under development" );
398         System.out.println();
399     }
400 }