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