improving GSDI, under construction...
[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.sdi.SdiException;
41 import org.forester.util.CommandLineArguments;
42 import org.forester.util.ForesterUtil;
43
44 public class sdix {
45
46     final static private String HELP_OPTION_1 = "help";
47     final static private String HELP_OPTION_2 = "h";
48     final static private String PRG_NAME      = "sdix";
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[] ) throws SdiException {
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,
119                                      "failed to read species trees from [" + species_trees_file + "] ["
120                                              + e.getMessage() + "]" );
121         }
122         if ( ( species_trees == null ) || ( species_trees.length < 1 ) ) {
123             ForesterUtil.fatalError( PRG_NAME, "failed to read species trees from [" + species_trees_file + "]" );
124         }
125         ForesterUtil.programMessage( PRG_NAME, "read in " + species_trees.length + " species trees from ["
126                 + species_trees_file + "]" );
127         final FilenameFilter filter = new FilenameFilter() {
128
129             @Override
130             public boolean accept( final File dir, final String name ) {
131                 return ( !name.startsWith( "." ) && !name.startsWith( "00_" ) && name.endsWith( ".xml" ) );
132             }
133         };
134         final String[] gene_tree_names = gene_trees_dir.list( filter );
135         Arrays.sort( gene_tree_names );
136         final List<File> gene_tree_files = new ArrayList<File>();
137         for( final String gene_tree_name : gene_tree_names ) {
138             final File gene_tree_file = new File( gene_trees_dir + ForesterUtil.FILE_SEPARATOR + gene_tree_name );
139             if ( !gene_tree_file.isDirectory() ) {
140                 gene_tree_files.add( gene_tree_file );
141             }
142         }
143         ForesterUtil.programMessage( PRG_NAME, "going to analyze " + gene_tree_files.size() + " gene trees from ["
144                 + gene_trees_dir + "]" );
145         final SDIx shin = new SDIx();
146         try {
147             shin.method1( gene_tree_files, species_trees, out_dir );
148         }
149         catch ( final IOException e ) {
150             ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
151             e.printStackTrace();
152         }
153         ForesterUtil.programMessage( PRG_NAME, "OK" );
154         //        System.out.println();
155         //        System.out.println( "Strip species tree: " + strip );
156         //        SDI sdi = null;
157         //        final long start_time = new Date().getTime();
158         //        try {
159         //            if ( use_sdise ) {
160         //                System.out.println();
161         //                System.out.println( "Using SDIse algorithm." );
162         //                sdi = new SDIse( gene_tree, species_tree );
163         //            }
164         //            else {
165         //                System.out.println();
166         //                System.out.println( "Using GSDI algorithm." );
167         //                System.out.println();
168         //                System.out.println( "Use most parsimonous duplication model: " + most_parsimonous_duplication_model );
169         //                sdi = new GSDI( gene_tree, species_tree, most_parsimonous_duplication_model );
170         //            }
171         //        }
172         //        catch ( final Exception e ) {
173         //            ForesterUtil.unexpectedFatalError( PRG_NAME, e );
174         //        }
175         //        System.out.println();
176         //        System.out.println( "Running time (excluding I/O): " + ( new Date().getTime() - start_time ) + "ms" );
177         //        try {
178         //            final PhylogenyWriter writer = new PhylogenyWriter();
179         //            writer.toPhyloXML( out_file, gene_tree, 1 );
180         //        }
181         //        catch ( final IOException e ) {
182         //            ForesterUtil.fatalError( PRG_NAME, "Failed to write to \"" + out_file + "\" [" + e.getMessage() + "]" );
183         //        }
184         //        System.out.println();
185         //        System.out.println( "Successfully wrote resulting gene tree to: " + out_file );
186         //        System.out.println();
187         //        System.out.println();
188     }
189
190     private static void print_help() {
191         System.out.println( "Usage: " + PRG_NAME + " [-options] <gene trees dir> <species tree file name> <outdir>" );
192         System.out.println();
193         System.out.println( "Options:" );
194         System.out.println();
195     }
196 }