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