in progress
[jalview.git] / forester / java / src / org / forester / application / sdi.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 sdi {
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                 = "sdi";
54     final static private String PRG_VERSION              = "beta 0.4";
55     final static private String PRG_DATE                 = "2009.01.22";
56
57     public static void main( final String args[] ) {
58         ForesterUtil.printProgramInformation( sdi.PRG_NAME, sdi.PRG_VERSION, sdi.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( sdi.HELP_OPTION_1 ) || cla.isOptionSet( sdi.HELP_OPTION_2 ) ) {
67             System.out.println();
68             sdi.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             sdi.print_help();
76             System.exit( -1 );
77         }
78         final List<String> allowed_options = new ArrayList<String>();
79         allowed_options.add( sdi.STRIP_OPTION );
80         allowed_options.add( sdi.GSDI_OPTION );
81         allowed_options.add( sdi.MOST_PARSIMONIOUS_OPTION );
82         final String dissallowed_options = cla.validateAllowedOptionsAsString( allowed_options );
83         if ( dissallowed_options.length() > 0 ) {
84             ForesterUtil.fatalError( sdi.PRG_NAME, "unknown option(s): " + dissallowed_options );
85         }
86         boolean use_sdise = true;
87         boolean strip = false;
88         boolean most_parsimonous_duplication_model = false;
89         if ( cla.isOptionSet( sdi.STRIP_OPTION ) ) {
90             strip = true;
91         }
92         if ( cla.isOptionSet( sdi.GSDI_OPTION ) ) {
93             use_sdise = false;
94         }
95         if ( cla.isOptionSet( sdi.MOST_PARSIMONIOUS_OPTION ) ) {
96             if ( use_sdise ) {
97                 ForesterUtil.fatalError( sdi.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( sdi.DEFAULT_OUTFILE );
114             }
115         }
116         catch ( final IllegalArgumentException e ) {
117             ForesterUtil.fatalError( sdi.PRG_NAME, "error in command line: " + e.getMessage() );
118         }
119         if ( ForesterUtil.isReadableFile( gene_tree_file ) != null ) {
120             ForesterUtil.fatalError( sdi.PRG_NAME, ForesterUtil.isReadableFile( gene_tree_file ) );
121         }
122         if ( ForesterUtil.isReadableFile( species_tree_file ) != null ) {
123             ForesterUtil.fatalError( sdi.PRG_NAME, ForesterUtil.isReadableFile( species_tree_file ) );
124         }
125         if ( ForesterUtil.isWritableFile( out_file ) != null ) {
126             ForesterUtil.fatalError( sdi.PRG_NAME, ForesterUtil.isWritableFile( out_file ) );
127         }
128         try {
129             final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
130             species_tree = factory.create( species_tree_file, new PhyloXmlParser() )[ 0 ];
131         }
132         catch ( final IOException e ) {
133             ForesterUtil.fatalError( sdi.PRG_NAME,
134                                      "Failed to read species tree from \"" + gene_tree_file + "\" [" + e.getMessage()
135                                              + "]" );
136         }
137         try {
138             final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
139             gene_tree = factory.create( gene_tree_file, new PhyloXmlParser() )[ 0 ];
140         }
141         catch ( final IOException e ) {
142             ForesterUtil.fatalError( sdi.PRG_NAME,
143                                      "Failed to read gene tree from \"" + gene_tree_file + "\" [" + e.getMessage()
144                                              + "]" );
145         }
146         gene_tree.setRooted( true );
147         species_tree.setRooted( true );
148         if ( !gene_tree.isCompletelyBinary() ) {
149             ForesterUtil.fatalError( sdi.PRG_NAME, "gene tree (\"" + gene_tree_file + "\") is not completely binary." );
150         }
151         if ( use_sdise ) {
152             if ( !species_tree.isCompletelyBinary() ) {
153                 ForesterUtil.fatalError( sdi.PRG_NAME, "species tree (\"" + species_tree_file
154                         + "\") is not completely binary." );
155             }
156         }
157         // For timing.
158         // gene_tree = Helper.createBalancedTree( 10 );
159         // species_tree = Helper.createBalancedTree( 13 );
160         // species_tree = Helper.createUnbalancedTree( 1024 );
161         // gene_tree = Helper.createUnbalancedTree( 8192 );
162         // species_tree = gene_tree.copyTree();
163         // gene_tree = species_tree.copyTree();
164         // Helper.numberSpeciesInOrder( species_tree );
165         // Helper.numberSpeciesInOrder( gene_tree );
166         // Helper.randomizeSpecies( 1, 8192, gene_tree );
167         // Helper.intervalNumberSpecies( gene_tree, 4096 );
168         // Helper.numberSpeciesInDescOrder( gene_tree );
169         System.out.println();
170         System.out.println( "Strip species tree: " + strip );
171         SDI sdi = null;
172         final long start_time = new Date().getTime();
173         try {
174             if ( use_sdise ) {
175                 System.out.println();
176                 System.out.println( "Using SDIse algorithm." );
177                 sdi = new SDIse( gene_tree, species_tree );
178             }
179             else {
180                 System.out.println();
181                 System.out.println( "Using GSDI algorithm." );
182                 System.out.println();
183                 System.out.println( "Use most parsimonous duplication model: " + most_parsimonous_duplication_model );
184                 sdi = new GSDI( gene_tree, species_tree, most_parsimonous_duplication_model );
185             }
186         }
187         catch ( final Exception e ) {
188             ForesterUtil.fatalError( PRG_NAME, e.getLocalizedMessage() );
189         }
190         System.out.println();
191         System.out.println( "Running time (excluding I/O): " + ( new Date().getTime() - start_time ) + "ms" );
192         try {
193             final PhylogenyWriter writer = new PhylogenyWriter();
194             writer.toPhyloXML( out_file, gene_tree, 1 );
195         }
196         catch ( final IOException e ) {
197             ForesterUtil.fatalError( PRG_NAME, "Failed to write to \"" + out_file + "\" [" + e.getMessage() + "]" );
198         }
199         System.out.println();
200         System.out.println( "Successfully wrote resulting gene tree to: " + out_file );
201         System.out.println();
202         if ( use_sdise ) {
203             sdi.computeMappingCostL();
204             System.out.println( "Mapping cost                    : " + sdi.computeMappingCostL() );
205         }
206         System.out.println( "Number of duplications          : " + sdi.getDuplicationsSum() );
207         if ( !use_sdise && !most_parsimonous_duplication_model ) {
208             System.out.println( "Number of potential duplications: "
209                     + ( ( GSDI ) sdi ).getSpeciationOrDuplicationEventsSum() );
210         }
211         if ( !use_sdise ) {
212             System.out.println( "Number speciations              : " + ( ( GSDI ) sdi ).getSpeciationsSum() );
213         }
214         System.out.println();
215     } // main( final String args[] )
216
217     private static void print_help() {
218         System.out.println( "Usage: \"" + sdi.PRG_NAME
219                 + " [-options] <gene tree in phyloXML format> <species tree in phyloXML format> [outfile]\"" );
220         System.out.println();
221         System.out.println( "Options:" );
222         System.out.println( " -" + sdi.STRIP_OPTION + ": to strip the species tree prior to duplication inference" );
223         System.out.println( " -" + sdi.GSDI_OPTION
224                 + ": to use GSDI algorithm instead of SDIse algorithm (under development, not recommended)" );
225         System.out
226                 .println( " -" + sdi.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:" );
231         System.out.println( " In phyloXML format, with taxonomy data in appropriate fields." );
232         System.out.println();
233         System.out.println( "Gene tree:" );
234         System.out.println( " In phyloXM format, with taxonomy and sequence data in appropriate fields." );
235         System.out.println();
236         System.out
237                 .println( "!! WARNING: GSDI algorithm is under development (and possibly not correct), please use SDIse instead !!" );
238         System.out.println();
239     }
240 }