in progress
[jalview.git] / forester / java / src / org / forester / application / sdix.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.FilenameFilter;
30 import java.io.IOException;
31 import java.util.ArrayList;
32 import java.util.Arrays;
33 import java.util.List;
34
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;
42
43 public class sdix {
44
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";
50
51     public static void main( final String args[] ) {
52         ForesterUtil.printProgramInformation( PRG_NAME, PRG_VERSION, PRG_DATE );
53         System.out.println();
54         CommandLineArguments cla = null;
55         try {
56             cla = new CommandLineArguments( args );
57         }
58         catch ( final Exception e ) {
59             ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
60         }
61         if ( cla.isOptionSet( HELP_OPTION_1 ) || cla.isOptionSet( HELP_OPTION_2 ) ) {
62             System.out.println();
63             print_help();
64             System.exit( 0 );
65         }
66         else if ( ( args.length != 3 ) ) {
67             System.out.println();
68             System.out.println( "wrong number of arguments" );
69             System.out.println();
70             print_help();
71             System.exit( -1 );
72         }
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 );
77         }
78         File gene_trees_dir = null;
79         File species_trees_file = null;
80         //File out_file = null;
81         File out_dir = null;
82         Phylogeny[] species_trees = null;
83         try {
84             gene_trees_dir = cla.getFile( 0 );
85             species_trees_file = cla.getFile( 1 );
86             out_dir = cla.getFile( 2 );
87         }
88         catch ( final IllegalArgumentException e ) {
89             ForesterUtil.fatalError( PRG_NAME, "error in command line: " + e.getMessage() );
90         }
91         if ( ForesterUtil.isReadableFile( species_trees_file ) != null ) {
92             ForesterUtil.fatalError( PRG_NAME, ForesterUtil.isReadableFile( species_trees_file ) );
93         }
94         if ( !gene_trees_dir.isDirectory() || !gene_trees_dir.canRead() ) {
95             ForesterUtil.fatalError( PRG_NAME, "cannot read gene trees from [" + gene_trees_dir + "]" );
96         }
97         // if ( ForesterUtil.isWritableFile( out_file ) != null ) {
98         //     ForesterUtil.fatalError( PRG_NAME, ForesterUtil.isWritableFile( out_file ) );
99         // }
100         if ( !out_dir.exists() ) {
101             boolean success = false;
102             try {
103                 success = out_dir.mkdir();
104             }
105             catch ( final Exception e ) {
106                 ForesterUtil.fatalError( PRG_NAME, "failed to create [" + out_dir + "] [" + e.getMessage() + "]" );
107             }
108             if ( !success ) {
109                 ForesterUtil.fatalError( PRG_NAME, "failed to create [" + out_dir + "]" );
110             }
111         }
112         try {
113             final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
114             species_trees = factory.create( species_trees_file, new PhyloXmlParser() );
115         }
116         catch ( final IOException e ) {
117             ForesterUtil.fatalError( PRG_NAME,
118                                      "failed to read species trees from [" + species_trees_file + "] ["
119                                              + e.getMessage() + "]" );
120         }
121         if ( ( species_trees == null ) || ( species_trees.length < 1 ) ) {
122             ForesterUtil.fatalError( PRG_NAME, "failed to read species trees from [" + species_trees_file + "]" );
123         }
124         ForesterUtil.programMessage( PRG_NAME, "read in " + species_trees.length + " species trees from ["
125                 + species_trees_file + "]" );
126         final FilenameFilter filter = new FilenameFilter() {
127
128             @Override
129             public boolean accept( final File dir, final String name ) {
130                 return ( !name.startsWith( "." ) && !name.startsWith( "00_" ) && name.endsWith( ".xml" ) );
131             }
132         };
133         final String[] gene_tree_names = gene_trees_dir.list( filter );
134         Arrays.sort( gene_tree_names );
135         final List<File> gene_tree_files = new ArrayList<File>();
136         for( final String gene_tree_name : gene_tree_names ) {
137             final File gene_tree_file = new File( gene_trees_dir + ForesterUtil.FILE_SEPARATOR + gene_tree_name );
138             if ( !gene_tree_file.isDirectory() ) {
139                 gene_tree_files.add( gene_tree_file );
140             }
141         }
142         ForesterUtil.programMessage( PRG_NAME, "going to analyze " + gene_tree_files.size() + " gene trees from ["
143                 + gene_trees_dir + "]" );
144         final SDIx shin = new SDIx();
145         try {
146             shin.method1( gene_tree_files, species_trees, out_dir );
147         }
148         catch ( final IOException e ) {
149             ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
150             e.printStackTrace();
151         }
152         ForesterUtil.programMessage( PRG_NAME, "OK" );
153         //        System.out.println();
154         //        System.out.println( "Strip species tree: " + strip );
155         //        SDI sdi = null;
156         //        final long start_time = new Date().getTime();
157         //        try {
158         //            if ( use_sdise ) {
159         //                System.out.println();
160         //                System.out.println( "Using SDIse algorithm." );
161         //                sdi = new SDIse( gene_tree, species_tree );
162         //            }
163         //            else {
164         //                System.out.println();
165         //                System.out.println( "Using GSDI algorithm." );
166         //                System.out.println();
167         //                System.out.println( "Use most parsimonous duplication model: " + most_parsimonous_duplication_model );
168         //                sdi = new GSDI( gene_tree, species_tree, most_parsimonous_duplication_model );
169         //            }
170         //        }
171         //        catch ( final Exception e ) {
172         //            ForesterUtil.unexpectedFatalError( PRG_NAME, e );
173         //        }
174         //        System.out.println();
175         //        System.out.println( "Running time (excluding I/O): " + ( new Date().getTime() - start_time ) + "ms" );
176         //        try {
177         //            final PhylogenyWriter writer = new PhylogenyWriter();
178         //            writer.toPhyloXML( out_file, gene_tree, 1 );
179         //        }
180         //        catch ( final IOException e ) {
181         //            ForesterUtil.fatalError( PRG_NAME, "Failed to write to \"" + out_file + "\" [" + e.getMessage() + "]" );
182         //        }
183         //        System.out.println();
184         //        System.out.println( "Successfully wrote resulting gene tree to: " + out_file );
185         //        System.out.println();
186         //        System.out.println();
187     }
188
189     private static void print_help() {
190         System.out.println( "Usage: " + PRG_NAME + " [-options] <gene trees dir> <species tree file name> <outdir>" );
191         System.out.println();
192         System.out.println( "Options:" );
193         System.out.println();
194     }
195 }