renamed
[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, "Failed to read species tree from \"" + gene_tree_file + "\" ["
135                     + e.getMessage() + "]" );
136         }
137         try {
138             final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
139             final PhylogenyParser pp = ForesterUtil.createParserDependingOnFileType( gene_tree_file, true );
140             gene_tree = factory.create( gene_tree_file, pp )[ 0 ];
141         }
142         catch ( final IOException e ) {
143             ForesterUtil.fatalError( PRG_NAME, "Failed to read gene tree from \"" + gene_tree_file + "\" ["
144                     + e.getMessage() + "]" );
145         }
146         gene_tree.setRooted( true );
147         species_tree.setRooted( true );
148         if ( !gene_tree.isCompletelyBinary() ) {
149             ForesterUtil.fatalError( PRG_NAME, "gene tree is not completely binary." );
150         }
151         if ( use_sdise ) {
152             if ( !species_tree.isCompletelyBinary() ) {
153                 ForesterUtil.fatalError( PRG_NAME, "species tree is not completely binary." );
154             }
155         }
156         // For timing.
157         // gene_tree = Helper.createBalancedTree( 10 );
158         // species_tree = Helper.createBalancedTree( 13 );
159         // species_tree = Helper.createUnbalancedTree( 1024 );
160         // gene_tree = Helper.createUnbalancedTree( 8192 );
161         // species_tree = gene_tree.copyTree();
162         // gene_tree = species_tree.copyTree();
163         // Helper.numberSpeciesInOrder( species_tree );
164         // Helper.numberSpeciesInOrder( gene_tree );
165         // Helper.randomizeSpecies( 1, 8192, gene_tree );
166         // Helper.intervalNumberSpecies( gene_tree, 4096 );
167         // Helper.numberSpeciesInDescOrder( gene_tree );
168         System.out.println();
169         System.out.println( "Strip species tree: " + strip );
170         SDI sdi = null;
171         final long start_time = new Date().getTime();
172         try {
173             if ( use_sdise ) {
174                 System.out.println();
175                 System.out.println( "Using SDIse algorithm." );
176                 sdi = new SDIse( gene_tree, species_tree );
177             }
178             else {
179                 System.out.println();
180                 System.out.println( "Using GSDI algorithm." );
181                 System.out.println();
182                 System.out.println( "Use most parsimonous duplication model: " + most_parsimonous_duplication_model );
183                 sdi = new GSDI( gene_tree, species_tree, most_parsimonous_duplication_model );
184             }
185         }
186         catch ( final Exception e ) {
187             ForesterUtil.unexpectedFatalError( PRG_NAME, e );
188         }
189         System.out.println();
190         System.out.println( "Running time (excluding I/O): " + ( new Date().getTime() - start_time ) + "ms" );
191         try {
192             final PhylogenyWriter writer = new PhylogenyWriter();
193             writer.toPhyloXML( out_file, gene_tree, 1 );
194         }
195         catch ( final IOException e ) {
196             ForesterUtil.fatalError( PRG_NAME, "Failed to write to \"" + out_file + "\" [" + e.getMessage() + "]" );
197         }
198         System.out.println();
199         System.out.println( "Successfully wrote resulting gene tree to: " + out_file );
200         System.out.println();
201         //        if ( use_sdise ) {
202         //            computeMappingCostL();
203         //            System.out.println( "Mapping cost                    : " + computeMappingCostL() );
204         //        }
205         //        System.out.println( "Number of duplications          : " + getDuplicationsSum() );
206         if ( !use_sdise && !most_parsimonous_duplication_model ) {
207             System.out.println( "Number of potential duplications: "
208                     + ( ( GSDI ) sdi ).getSpeciationOrDuplicationEventsSum() );
209         }
210         if ( !use_sdise ) {
211             System.out.println( "Number speciations              : " + ( ( GSDI ) sdi ).getSpeciationsSum() );
212         }
213         System.out.println();
214     } // main( final String args[] )
215
216     private static void print_help() {
217         System.out.println( "Usage: \"" + PRG_NAME
218                 + " [-options] <gene tree file name> <species tree file name>  [outfile name]\"" );
219         System.out.println();
220         System.out.println( "Options:" );
221         System.out.println( " -" + STRIP_OPTION + ": to strip the species tree prior to duplication inference" );
222         System.out.println( " -" + SDISE_OPTION
223                 + ": to use SDIse algorithm instead of GSDI algorithm (for binary trees only, faster)" );
224         System.out.println( " -" + MOST_PARSIMONIOUS_OPTION + ": use most parimonious duplication model for GSDI: " );
225         System.out.println( "     assign nodes as speciations which would otherwise be assiged" );
226         System.out.println( "     as unknown because of polytomies in the species tree" );
227         System.out.println();
228         System.out.println( "Species tree file" );
229         System.out.println( " In NHX format, with species names in species name fields unless -n option" );
230         System.out.println( " is used." );
231         System.out.println();
232         System.out.println( "Gene tree file" );
233         System.out.println( " In NHX format, with species names in species name fields and sequence names" );
234         System.out.println( " in sequence name fields." );
235         System.out.println();
236         System.out.println( "!! WARNING: GSDI algorithm is under development, please use SDIse (-b) instead  !!" );
237         System.out.println();
238     } // print_help()
239 }