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.FilenameFilter;
30 import java.io.IOException;
31 import java.util.ArrayList;
32 import java.util.Arrays;
33 import java.util.List;
35 import org.forester.io.parsers.phyloxml.PhyloXmlParser;
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.SDIx;
40 import org.forester.util.CommandLineArguments;
41 import org.forester.util.ForesterUtil;
45 final static private String HELP_OPTION_1 = "help";
46 final static private String HELP_OPTION_2 = "h";
47 final static private String PRG_NAME = "sdix";
48 final static private String PRG_VERSION = "0.001 alpha";
49 final static private String PRG_DATE = "2009.10.14";
51 public static void main( final String args[] ) {
52 ForesterUtil.printProgramInformation( PRG_NAME, PRG_VERSION, PRG_DATE );
54 CommandLineArguments cla = null;
56 cla = new CommandLineArguments( args );
58 catch ( final Exception e ) {
59 ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
61 if ( cla.isOptionSet( HELP_OPTION_1 ) || cla.isOptionSet( HELP_OPTION_2 ) ) {
66 else if ( ( args.length != 3 ) ) {
68 System.out.println( "wrong number of arguments" );
73 final List<String> allowed_options = new ArrayList<String>();
74 final String dissallowed_options = cla.validateAllowedOptionsAsString( allowed_options );
75 if ( dissallowed_options.length() > 0 ) {
76 ForesterUtil.fatalError( PRG_NAME, "unknown option(s): " + dissallowed_options );
78 File gene_trees_dir = null;
79 File species_trees_file = null;
80 //File out_file = null;
82 Phylogeny[] species_trees = null;
84 gene_trees_dir = cla.getFile( 0 );
85 species_trees_file = cla.getFile( 1 );
86 out_dir = cla.getFile( 2 );
88 catch ( final IllegalArgumentException e ) {
89 ForesterUtil.fatalError( PRG_NAME, "error in command line: " + e.getMessage() );
91 if ( ForesterUtil.isReadableFile( species_trees_file ) != null ) {
92 ForesterUtil.fatalError( PRG_NAME, ForesterUtil.isReadableFile( species_trees_file ) );
94 if ( !gene_trees_dir.isDirectory() || !gene_trees_dir.canRead() ) {
95 ForesterUtil.fatalError( PRG_NAME, "cannot read gene trees from [" + gene_trees_dir + "]" );
97 // if ( ForesterUtil.isWritableFile( out_file ) != null ) {
98 // ForesterUtil.fatalError( PRG_NAME, ForesterUtil.isWritableFile( out_file ) );
100 if ( !out_dir.exists() ) {
101 boolean success = false;
103 success = out_dir.mkdir();
105 catch ( final Exception e ) {
106 ForesterUtil.fatalError( PRG_NAME, "failed to create [" + out_dir + "] [" + e.getMessage() + "]" );
109 ForesterUtil.fatalError( PRG_NAME, "failed to create [" + out_dir + "]" );
113 final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
114 species_trees = factory.create( species_trees_file, new PhyloXmlParser() );
116 catch ( final IOException e ) {
117 ForesterUtil.fatalError( PRG_NAME, "failed to read species trees from [" + species_trees_file + "] ["
118 + e.getMessage() + "]" );
120 if ( ( species_trees == null ) || ( species_trees.length < 1 ) ) {
121 ForesterUtil.fatalError( PRG_NAME, "failed to read species trees from [" + species_trees_file + "]" );
123 ForesterUtil.programMessage( PRG_NAME, "read in " + species_trees.length + " species trees from ["
124 + species_trees_file + "]" );
125 final FilenameFilter filter = new FilenameFilter() {
127 public boolean accept( final File dir, final String name ) {
128 return ( !name.startsWith( "." ) && !name.startsWith( "00_" ) && name.endsWith( ".xml" ) );
131 final String[] gene_tree_names = gene_trees_dir.list( filter );
132 Arrays.sort( gene_tree_names );
133 final List<File> gene_tree_files = new ArrayList<File>();
134 for( final String gene_tree_name : gene_tree_names ) {
135 final File gene_tree_file = new File( gene_trees_dir + ForesterUtil.FILE_SEPARATOR + gene_tree_name );
136 if ( !gene_tree_file.isDirectory() ) {
137 gene_tree_files.add( gene_tree_file );
140 ForesterUtil.programMessage( PRG_NAME, "going to analyze " + gene_tree_files.size() + " gene trees from ["
141 + gene_trees_dir + "]" );
142 final SDIx shin = new SDIx();
144 shin.method1( gene_tree_files, species_trees, out_dir );
146 catch ( final IOException e ) {
147 ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
150 ForesterUtil.programMessage( PRG_NAME, "OK" );
151 // System.out.println();
152 // System.out.println( "Strip species tree: " + strip );
154 // final long start_time = new Date().getTime();
156 // if ( use_sdise ) {
157 // System.out.println();
158 // System.out.println( "Using SDIse algorithm." );
159 // sdi = new SDIse( gene_tree, species_tree );
162 // System.out.println();
163 // System.out.println( "Using GSDI algorithm." );
164 // System.out.println();
165 // System.out.println( "Use most parsimonous duplication model: " + most_parsimonous_duplication_model );
166 // sdi = new GSDI( gene_tree, species_tree, most_parsimonous_duplication_model );
169 // catch ( final Exception e ) {
170 // ForesterUtil.unexpectedFatalError( PRG_NAME, e );
172 // System.out.println();
173 // System.out.println( "Running time (excluding I/O): " + ( new Date().getTime() - start_time ) + "ms" );
175 // final PhylogenyWriter writer = new PhylogenyWriter();
176 // writer.toPhyloXML( out_file, gene_tree, 1 );
178 // catch ( final IOException e ) {
179 // ForesterUtil.fatalError( PRG_NAME, "Failed to write to \"" + out_file + "\" [" + e.getMessage() + "]" );
181 // System.out.println();
182 // System.out.println( "Successfully wrote resulting gene tree to: " + out_file );
183 // System.out.println();
184 // System.out.println();
187 private static void print_help() {
188 System.out.println( "Usage: " + PRG_NAME + " [-options] <gene trees dir> <species tree file name> <outdir>" );
189 System.out.println();
190 System.out.println( "Options:" );
191 System.out.println();