2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
5 // Copyright (C) 2008-2009 Christian M. Zmasek
6 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
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.
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.
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
23 // Contact: phylosoft @ gmail . com
24 // WWW: www.phylosoft.org/forester
26 package org.forester.application;
29 import java.io.IOException;
30 import java.util.ArrayList;
31 import java.util.Date;
32 import java.util.List;
34 import org.forester.io.parsers.PhylogenyParser;
35 import org.forester.io.parsers.nhx.NHXParser;
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;
49 public final class gsdi {
51 final static public boolean REPLACE_UNDERSCORES_IN_NH_SPECIES_TREE = true;
52 final static private String STRIP_OPTION = "s";
53 final static private String SDI_OPTION = "b";
54 final static private String MOST_PARSIMONIOUS_OPTION = "m";
55 final static private String GUESS_FORMAT_OF_SPECIES_TREE = "q";
56 final static private String HELP_OPTION_1 = "help";
57 final static private String HELP_OPTION_2 = "h";
58 final static private String DEFAULT_OUTFILE_SUFFIX = "_gsdi_out.phylo.xml";
59 final static private String PRG_NAME = "gsdi";
60 final static private String PRG_VERSION = "0.901";
61 final static private String PRG_DATE = "120608";
62 final static private String PRG_DESC = "general speciation duplication inference";
63 final static private String E_MAIL = "phylosoft@gmail.com";
64 final static private String WWW = "www.phylosoft.org/forester";
66 public static void main( final String args[] ) {
67 ForesterUtil.printProgramInformation( PRG_NAME,
73 ForesterUtil.getForesterLibraryInformation() );
74 CommandLineArguments cla = null;
76 cla = new CommandLineArguments( args );
78 catch ( final Exception e ) {
79 ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
81 if ( cla.isOptionSet( gsdi.HELP_OPTION_1 ) || cla.isOptionSet( gsdi.HELP_OPTION_2 ) ) {
86 else if ( ( args.length < 2 ) || ( cla.getNumberOfNames() < 2 ) || ( cla.getNumberOfNames() > 3 ) ) {
88 System.out.println( "Wrong number of arguments." );
93 final List<String> allowed_options = new ArrayList<String>();
94 allowed_options.add( gsdi.STRIP_OPTION );
95 allowed_options.add( gsdi.SDI_OPTION );
96 allowed_options.add( gsdi.GUESS_FORMAT_OF_SPECIES_TREE );
97 allowed_options.add( gsdi.MOST_PARSIMONIOUS_OPTION );
98 final String dissallowed_options = cla.validateAllowedOptionsAsString( allowed_options );
99 if ( dissallowed_options.length() > 0 ) {
100 ForesterUtil.fatalError( gsdi.PRG_NAME, "unknown option(s): " + dissallowed_options );
102 boolean use_sdise = false;
103 boolean strip = false;
104 boolean most_parsimonous_duplication_model = false;
105 boolean species_tree_in_phyloxml = true;
106 if ( cla.isOptionSet( gsdi.STRIP_OPTION ) ) {
109 if ( cla.isOptionSet( gsdi.SDI_OPTION ) ) {
112 if ( cla.isOptionSet( gsdi.MOST_PARSIMONIOUS_OPTION ) ) {
114 ForesterUtil.fatalError( gsdi.PRG_NAME, "Can only use most parsimonious duplication mode with GSDI" );
116 most_parsimonous_duplication_model = true;
118 if ( cla.isOptionSet( gsdi.GUESS_FORMAT_OF_SPECIES_TREE ) ) {
119 species_tree_in_phyloxml = false;
121 Phylogeny species_tree = null;
122 Phylogeny gene_tree = null;
123 File gene_tree_file = null;
124 File species_tree_file = null;
125 File out_file = null;
127 gene_tree_file = cla.getFile( 0 );
128 species_tree_file = cla.getFile( 1 );
129 if ( cla.getNumberOfNames() == 3 ) {
130 out_file = cla.getFile( 2 );
133 out_file = new File( ForesterUtil.removeSuffix( gene_tree_file.toString() ) + DEFAULT_OUTFILE_SUFFIX );
134 //out_file = new File( gsdi.DEFAULT_OUTFILE );
137 catch ( final IllegalArgumentException e ) {
138 ForesterUtil.fatalError( gsdi.PRG_NAME, "error in command line: " + e.getMessage() );
140 if ( ForesterUtil.isReadableFile( gene_tree_file ) != null ) {
141 ForesterUtil.fatalError( gsdi.PRG_NAME, ForesterUtil.isReadableFile( gene_tree_file ) );
143 if ( ForesterUtil.isReadableFile( species_tree_file ) != null ) {
144 ForesterUtil.fatalError( gsdi.PRG_NAME, ForesterUtil.isReadableFile( species_tree_file ) );
146 if ( ForesterUtil.isWritableFile( out_file ) != null ) {
147 ForesterUtil.fatalError( gsdi.PRG_NAME, ForesterUtil.isWritableFile( out_file ) );
150 final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
151 if ( species_tree_in_phyloxml ) {
152 species_tree = factory.create( species_tree_file, new PhyloXmlParser() )[ 0 ];
155 final PhylogenyParser p = ParserUtils.createParserDependingOnFileType( species_tree_file, true );
156 if ( REPLACE_UNDERSCORES_IN_NH_SPECIES_TREE && ( p instanceof NHXParser ) ) {
157 ( ( NHXParser ) p ).setReplaceUnderscores( true );
159 species_tree = factory.create( species_tree_file, p )[ 0 ];
160 PhylogenyMethods.transferNodeNameToField( species_tree,
161 PhylogenyMethods.PhylogenyNodeField.TAXONOMY_SCIENTIFIC_NAME );
164 catch ( final IOException e ) {
165 ForesterUtil.fatalError( gsdi.PRG_NAME,
166 "Failed to read species tree from [" + gene_tree_file + "]: " + e.getMessage() );
169 final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
170 gene_tree = factory.create( gene_tree_file, new PhyloXmlParser() )[ 0 ];
172 catch ( final IOException e ) {
173 ForesterUtil.fatalError( gsdi.PRG_NAME,
174 "Failed to read gene tree from [" + gene_tree_file + "]: " + e.getMessage() );
176 gene_tree.setRooted( true );
177 species_tree.setRooted( true );
178 if ( !gene_tree.isCompletelyBinary() ) {
179 ForesterUtil.fatalError( gsdi.PRG_NAME, "gene tree (\"" + gene_tree_file + "\") is not completely binary" );
182 if ( !species_tree.isCompletelyBinary() ) {
183 ForesterUtil.fatalError( gsdi.PRG_NAME, "species tree (\"" + species_tree_file
184 + "\") is not completely binary" );
188 // gene_tree = Helper.createBalancedTree( 10 );
189 // species_tree = Helper.createBalancedTree( 13 );
190 // species_tree = Helper.createUnbalancedTree( 1024 );
191 // gene_tree = Helper.createUnbalancedTree( 8192 );
192 // species_tree = gene_tree.copyTree();
193 // gene_tree = species_tree.copyTree();
194 // Helper.numberSpeciesInOrder( species_tree );
195 // Helper.numberSpeciesInOrder( gene_tree );
196 // Helper.randomizeSpecies( 1, 8192, gene_tree );
197 // Helper.intervalNumberSpecies( gene_tree, 4096 );
198 // Helper.numberSpeciesInDescOrder( gene_tree );
199 System.out.println();
200 System.out.println( "Strip species tree: " + strip );
202 final long start_time = new Date().getTime();
205 System.out.println();
206 System.out.println( "Using SDIse algorithm" );
207 sdi = new SDIse( gene_tree, species_tree );
210 System.out.println();
211 System.out.println( "Using GSDI algorithm" );
212 System.out.println();
213 System.out.println( "Use most parsimonous duplication model: " + most_parsimonous_duplication_model );
214 sdi = new GSDI( gene_tree, species_tree, most_parsimonous_duplication_model, true );
217 catch ( final Exception e ) {
218 ForesterUtil.fatalError( PRG_NAME, e.getLocalizedMessage() );
220 System.out.println();
221 System.out.println( "Running time (excluding I/O): " + ( new Date().getTime() - start_time ) + "ms" );
223 final PhylogenyWriter writer = new PhylogenyWriter();
224 writer.toPhyloXML( out_file, gene_tree, 0 );
226 catch ( final IOException e ) {
227 ForesterUtil.fatalError( PRG_NAME, "Failed to write to [" + out_file + "]: " + e.getMessage() );
229 System.out.println();
230 System.out.println( "Successfully wrote resulting gene tree to: " + out_file );
231 System.out.println();
233 sdi.computeMappingCostL();
234 System.out.println( "Mapping cost : " + sdi.computeMappingCostL() );
236 System.out.println( "Number of duplications : " + sdi.getDuplicationsSum() );
237 if ( !use_sdise && !most_parsimonous_duplication_model ) {
238 System.out.println( "Number of potential duplications: "
239 + ( ( GSDI ) sdi ).getSpeciationOrDuplicationEventsSum() );
242 System.out.println( "Number speciations : " + ( ( GSDI ) sdi ).getSpeciationsSum() );
244 System.out.println();
247 private static void print_help() {
248 System.out.println( "Usage: " + gsdi.PRG_NAME
249 + " [-options] <gene tree in phyloXML format> <species tree in phyloXML format> [outfile]" );
250 System.out.println();
251 System.out.println( "Options:" );
252 System.out.println( " -" + gsdi.STRIP_OPTION + ": to strip the species tree prior to duplication inference" );
253 System.out.println( " -" + gsdi.SDI_OPTION + ": to use SDI algorithm instead of GSDI algorithm" );
254 System.out.println( " -" + gsdi.MOST_PARSIMONIOUS_OPTION
255 + ": use most parimonious duplication model for GSDI: " );
256 System.out.println( " assign nodes as speciations which would otherwise be assiged" );
257 System.out.println( " as unknown because of polytomies in the species tree" );
258 System.out.println( " -" + gsdi.GUESS_FORMAT_OF_SPECIES_TREE + ": to allow species tree in other formats than" );
259 System.out.println( " phyloXML (Newick, NHX, Nexus)" );
260 System.out.println();
261 System.out.println( "Species tree:" );
262 System.out.println( " In phyloXML format (unless option -" + gsdi.GUESS_FORMAT_OF_SPECIES_TREE
263 + " is used), with taxonomy data in appropriate fields" );
264 System.out.println();
265 System.out.println( "Gene tree:" );
266 System.out.println( " In phyloXM format, with taxonomy and sequence data in appropriate fields" );
267 System.out.println();
268 System.out.println( "Note -- GSDI algorithm is under development" );
269 System.out.println();