708b734d17221da27b83eb6c8ca13615419c6c11
[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.util.ArrayList;
31 import java.util.Date;
32 import java.util.List;
33
34 import org.forester.archaeopteryx.Archaeopteryx;
35 import org.forester.io.parsers.PhylogenyParser;
36 import org.forester.io.parsers.phyloxml.PhyloXmlParser;
37 import org.forester.io.parsers.util.ParserUtils;
38 import org.forester.io.writers.PhylogenyWriter;
39 import org.forester.phylogeny.Phylogeny;
40 import org.forester.phylogeny.PhylogenyMethods;
41 import org.forester.phylogeny.factories.ParserBasedPhylogenyFactory;
42 import org.forester.phylogeny.factories.PhylogenyFactory;
43 import org.forester.sdi.GSDI;
44 import org.forester.sdi.SDI;
45 import org.forester.sdi.SDIse;
46 import org.forester.util.CommandLineArguments;
47 import org.forester.util.ForesterUtil;
48
49 public final class gsdi {
50
51     final static private String STRIP_OPTION                 = "s";
52     final static private String SDI_OPTION                   = "b";
53     final static private String MOST_PARSIMONIOUS_OPTION     = "m";
54     final static private String GUESS_FORMAT_OF_SPECIES_TREE = "q";
55     final static private String HELP_OPTION_1                = "help";
56     final static private String HELP_OPTION_2                = "h";
57     final static private String DEFAULT_OUTFILE              = "gsdi_out.phylo.xml";
58     final static private String PRG_NAME                     = "gsdi";
59     final static private String PRG_VERSION                  = "0.4";
60     final static private String PRG_DATE                     = "120607";
61     final static private String PRG_DESC                     = "general speciation duplication inference";
62     final static private String E_MAIL                       = "phylosoft@gmail.com";
63     final static private String WWW                          = "www.phylosoft.org/forester";
64
65     public static void main( final String args[] ) {
66         ForesterUtil.printProgramInformation( PRG_NAME,
67                                               PRG_DESC,
68                                               PRG_VERSION,
69                                               PRG_DATE,
70                                               E_MAIL,
71                                               WWW,
72                                               ForesterUtil.getForesterLibraryInformation() );
73         CommandLineArguments cla = null;
74         try {
75             cla = new CommandLineArguments( args );
76         }
77         catch ( final Exception e ) {
78             ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
79         }
80         if ( cla.isOptionSet( gsdi.HELP_OPTION_1 ) || cla.isOptionSet( gsdi.HELP_OPTION_2 ) ) {
81             System.out.println();
82             gsdi.print_help();
83             System.exit( 0 );
84         }
85         else if ( ( args.length < 2 ) || ( cla.getNumberOfNames() < 2 ) || ( cla.getNumberOfNames() > 3 ) ) {
86             System.out.println();
87             System.out.println( "Wrong number of arguments." );
88             System.out.println();
89             gsdi.print_help();
90             System.exit( -1 );
91         }
92         final List<String> allowed_options = new ArrayList<String>();
93         allowed_options.add( gsdi.STRIP_OPTION );
94         allowed_options.add( gsdi.SDI_OPTION );
95         allowed_options.add( gsdi.GUESS_FORMAT_OF_SPECIES_TREE );
96         allowed_options.add( gsdi.MOST_PARSIMONIOUS_OPTION );
97         final String dissallowed_options = cla.validateAllowedOptionsAsString( allowed_options );
98         if ( dissallowed_options.length() > 0 ) {
99             ForesterUtil.fatalError( gsdi.PRG_NAME, "unknown option(s): " + dissallowed_options );
100         }
101         boolean use_sdise = false;
102         boolean strip = false;
103         boolean most_parsimonous_duplication_model = false;
104         boolean species_tree_in_phyloxml = true;
105         if ( cla.isOptionSet( gsdi.STRIP_OPTION ) ) {
106             strip = true;
107         }
108         if ( cla.isOptionSet( gsdi.SDI_OPTION ) ) {
109             use_sdise = true;
110         }
111         if ( cla.isOptionSet( gsdi.MOST_PARSIMONIOUS_OPTION ) ) {
112             if ( use_sdise ) {
113                 ForesterUtil.fatalError( gsdi.PRG_NAME, "Can only use most parsimonious duplication mode with GSDI" );
114             }
115             most_parsimonous_duplication_model = true;
116         }
117         if ( cla.isOptionSet( gsdi.GUESS_FORMAT_OF_SPECIES_TREE ) ) {
118             species_tree_in_phyloxml = false;
119         }
120         Phylogeny species_tree = null;
121         Phylogeny gene_tree = null;
122         File gene_tree_file = null;
123         File species_tree_file = null;
124         File out_file = null;
125         try {
126             gene_tree_file = cla.getFile( 0 );
127             species_tree_file = cla.getFile( 1 );
128             if ( cla.getNumberOfNames() == 3 ) {
129                 out_file = cla.getFile( 2 );
130             }
131             else {
132                 out_file = new File( gsdi.DEFAULT_OUTFILE );
133             }
134         }
135         catch ( final IllegalArgumentException e ) {
136             ForesterUtil.fatalError( gsdi.PRG_NAME, "error in command line: " + e.getMessage() );
137         }
138         if ( ForesterUtil.isReadableFile( gene_tree_file ) != null ) {
139             ForesterUtil.fatalError( gsdi.PRG_NAME, ForesterUtil.isReadableFile( gene_tree_file ) );
140         }
141         if ( ForesterUtil.isReadableFile( species_tree_file ) != null ) {
142             ForesterUtil.fatalError( gsdi.PRG_NAME, ForesterUtil.isReadableFile( species_tree_file ) );
143         }
144         if ( ForesterUtil.isWritableFile( out_file ) != null ) {
145             ForesterUtil.fatalError( gsdi.PRG_NAME, ForesterUtil.isWritableFile( out_file ) );
146         }
147         try {
148             final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
149             if ( species_tree_in_phyloxml ) {
150                 species_tree = factory.create( species_tree_file, new PhyloXmlParser() )[ 0 ];
151             }
152             else {
153                 final PhylogenyParser p = ParserUtils.createParserDependingOnFileType( species_tree_file, true );
154                 species_tree = factory.create( species_tree_file, p )[ 0 ];
155                 PhylogenyMethods.transferNodeNameToField( species_tree,
156                                                           PhylogenyMethods.PhylogenyNodeField.TAXONOMY_SCIENTIFIC_NAME );
157               
158             }
159         }
160         catch ( final IOException e ) {
161             ForesterUtil.fatalError( gsdi.PRG_NAME,
162                                      "Failed to read species tree from [" + gene_tree_file + "]: " + e.getMessage() );
163         }
164         
165         try {
166             final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
167             gene_tree = factory.create( gene_tree_file, new PhyloXmlParser() )[ 0 ];
168         }
169         catch ( final IOException e ) {
170             ForesterUtil.fatalError( gsdi.PRG_NAME,
171                                      "Failed to read gene tree from [" + gene_tree_file + "]: " + e.getMessage() );
172         }
173         gene_tree.setRooted( true );
174         species_tree.setRooted( true );
175         if ( !gene_tree.isCompletelyBinary() ) {
176             ForesterUtil.fatalError( gsdi.PRG_NAME, "gene tree (\"" + gene_tree_file + "\") is not completely binary" );
177         }
178         if ( use_sdise ) {
179             if ( !species_tree.isCompletelyBinary() ) {
180                 ForesterUtil.fatalError( gsdi.PRG_NAME, "species tree (\"" + species_tree_file
181                         + "\") is not completely binary" );
182             }
183         }
184         // For timing.
185         // gene_tree = Helper.createBalancedTree( 10 );
186         // species_tree = Helper.createBalancedTree( 13 );
187         // species_tree = Helper.createUnbalancedTree( 1024 );
188         // gene_tree = Helper.createUnbalancedTree( 8192 );
189         // species_tree = gene_tree.copyTree();
190         // gene_tree = species_tree.copyTree();
191         // Helper.numberSpeciesInOrder( species_tree );
192         // Helper.numberSpeciesInOrder( gene_tree );
193         // Helper.randomizeSpecies( 1, 8192, gene_tree );
194         // Helper.intervalNumberSpecies( gene_tree, 4096 );
195         // Helper.numberSpeciesInDescOrder( gene_tree );
196         System.out.println();
197         System.out.println( "Strip species tree: " + strip );
198         SDI sdi = null;
199         final long start_time = new Date().getTime();
200         try {
201             if ( use_sdise ) {
202                 System.out.println();
203                 System.out.println( "Using SDIse algorithm" );
204                 sdi = new SDIse( gene_tree, species_tree );
205             }
206             else {
207                 System.out.println();
208                 System.out.println( "Using GSDI algorithm" );
209                 System.out.println();
210                 System.out.println( "Use most parsimonous duplication model: " + most_parsimonous_duplication_model );
211                 sdi = new GSDI( gene_tree, species_tree, most_parsimonous_duplication_model );
212             }
213         }
214         catch ( final Exception e ) {
215             ForesterUtil.fatalError( PRG_NAME, e.getLocalizedMessage() );
216         }
217         System.out.println();
218         System.out.println( "Running time (excluding I/O): " + ( new Date().getTime() - start_time ) + "ms" );
219         try {
220             final PhylogenyWriter writer = new PhylogenyWriter();
221             writer.toPhyloXML( out_file, gene_tree, 0 );
222         }
223         catch ( final IOException e ) {
224             ForesterUtil.fatalError( PRG_NAME, "Failed to write to [" + out_file + "]: " + e.getMessage() );
225         }
226         System.out.println();
227         System.out.println( "Successfully wrote resulting gene tree to: " + out_file );
228         System.out.println();
229         if ( use_sdise ) {
230             sdi.computeMappingCostL();
231             System.out.println( "Mapping cost                    : " + sdi.computeMappingCostL() );
232         }
233         System.out.println( "Number of duplications          : " + sdi.getDuplicationsSum() );
234         if ( !use_sdise && !most_parsimonous_duplication_model ) {
235             System.out.println( "Number of potential duplications: "
236                     + ( ( GSDI ) sdi ).getSpeciationOrDuplicationEventsSum() );
237         }
238         if ( !use_sdise ) {
239             System.out.println( "Number speciations              : " + ( ( GSDI ) sdi ).getSpeciationsSum() );
240         }
241         System.out.println();
242     }
243
244     private static void print_help() {
245         System.out.println( "Usage: " + gsdi.PRG_NAME
246                 + " [-options] <gene tree in phyloXML format> <species tree in phyloXML format> [outfile]" );
247         System.out.println();
248         System.out.println( "Options:" );
249         System.out.println( " -" + gsdi.STRIP_OPTION + ": to strip the species tree prior to duplication inference" );
250         System.out.println( " -" + gsdi.SDI_OPTION + ": to use SDI algorithm instead of GSDI algorithm" );
251         System.out.println( " -" + gsdi.MOST_PARSIMONIOUS_OPTION
252                 + ": use most parimonious duplication model for GSDI: " );
253         System.out.println( "     assign nodes as speciations which would otherwise be assiged" );
254         System.out.println( "     as unknown because of polytomies in the species tree" );
255         System.out.println( " -" + gsdi.GUESS_FORMAT_OF_SPECIES_TREE + ": to allow species tree in other formats than" );
256         System.out.println( "     phyloXML (Newick, NHX, Nexus)" );
257         System.out.println();
258         System.out.println( "Species tree:" );
259         System.out.println( " In phyloXML format (unless option " + gsdi.GUESS_FORMAT_OF_SPECIES_TREE
260                 + " is used), with taxonomy data in appropriate fields" );
261         System.out.println();
262         System.out.println( "Gene tree:" );
263         System.out.println( " In phyloXM format, with taxonomy and sequence data in appropriate fields" );
264         System.out.println();
265         System.out.println( "Note -- GSDI algorithm is under development" );
266         System.out.println();
267     }
268 }