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