inprogress (not working)
[jalview.git] / forester / java / src / org / forester / application / msa_compactor.java
1 // $Id:
2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
4 //
5 // Copyright (C) 2014 Christian M. Zmasek
6 // Copyright (C) 2014 Sanford-Burnham Medical Research Institute
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 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
24
25 package org.forester.application;
26
27 import java.io.File;
28 import java.io.FileInputStream;
29 import java.util.ArrayList;
30 import java.util.List;
31
32 import org.forester.io.parsers.FastaParser;
33 import org.forester.io.parsers.GeneralMsaParser;
34 import org.forester.msa.DeleteableMsa;
35 import org.forester.msa.MsaInferrer;
36 import org.forester.msa_compactor.MsaCompactor;
37 import org.forester.util.CommandLineArguments;
38 import org.forester.util.ForesterUtil;
39
40 public class msa_compactor {
41
42     final static private String HELP_OPTION_1                          = "help";
43     final static private String HELP_OPTION_2                          = "h";
44     final static private String REMOVE_WORST_OFFENDERS_OPTION          = "r";
45     final static private String AV_GAPINESS_OPTION                     = "g";
46     final static private String STEP_OPTION                            = "s";
47     final static private String LENGTH_OPTION                          = "l";
48     final static private String REALIGN_OPTION                         = "a";
49     //
50     final static private String STEP_FOR_DIAGNOSTICS_OPTION            = "sd";
51     final static private String MIN_LENGTH_OPTION                      = "ml";
52     final static private String GAP_RATIO_LENGTH_OPTION                = "gr";
53     final static private String REPORT_ALN_MEAN_IDENTITY               = "q";
54     final static private String OUTPUT_FORMAT_PHYLIP_OPTION            = "f";
55     final static private String OUTPUT_REMOVED_SEQS_OPTION             = "ro";
56     //
57     final static private String PATH_TO_MAFFT_OPTION                   = "mafft";
58     final static private String DO_NOT_NORMALIZE_FOR_EFF_LENGTH_OPTION = "nn";
59     final static private String PRG_NAME                               = "msa_compactor";
60     final static private String PRG_DESC                               = "multiple sequence aligment compactor";
61     final static private String PRG_VERSION                            = "0.01";
62     final static private String PRG_DATE                               = "140316";
63     final static private String E_MAIL                                 = "phylosoft@gmail.com";
64     final static private String WWW                                    = "https://sites.google.com/site/cmzmasek/home/software/forester";
65
66     public static void main( final String args[] ) {
67         try {
68             final CommandLineArguments cla = new CommandLineArguments( args );
69             if ( cla.isOptionSet( HELP_OPTION_1 ) || cla.isOptionSet( HELP_OPTION_2 )
70                     || ( ( cla.getNumberOfNames() < 1 ) || ( cla.getNumberOfNames() > 2 ) ) ) {
71                 printHelp();
72                 System.exit( 0 );
73             }
74             final File in = cla.getFile( 0 );
75             File out = null;
76             if ( cla.getNumberOfNames() > 1 ) {
77                 out = cla.getFile( 1 );
78             }
79             int worst_remove = -1;
80             double av_gap = -1;
81             int length = -1;
82             int step = -1;
83             boolean realign = false;
84             boolean norm = true;
85             String path_to_mafft = null;
86             //            final static private String STEP_FOR_DIAGNOSTICS_OPTION            = "sd";
87             //            final static private String MIN_LENGTH_OPTION                      = "ml";
88             //            final static private String GAP_RATIO_LENGTH_OPTION                = "gr";
89             //            final static private String REPORT_ALN_MEAN_IDENTITY               = "q";
90             //            final static private String OUTPUT_FORMAT_PHYLIP_OPTION            = "f";
91             //            final static private String OUTPUT_REMOVED_SEQS_OPTION             = "ro";
92             int step_for_diagnostics = -1;
93             int min_length = -1;
94             double gap_ratio = -1;
95             boolean report_aln_mean_identity = false;
96             Format output_format = FASTA;
97             final File roved_seqs_out_base = null;
98             //
99             if ( cla.isOptionSet( STEP_FOR_DIAGNOSTICS_OPTION ) ) {
100                 step_for_diagnostics = cla.getOptionValueAsInt( STEP_FOR_DIAGNOSTICS_OPTION );
101                 if ( ( step_for_diagnostics < 1 )
102                         || ( ( step_for_diagnostics > msa.getNumberOfSequences() ) || ( ( worst_remove > 0 ) && ( step_for_diagnostics > worst_remove ) ) ) ) {
103                     ForesterUtil.fatalError( PRG_NAME, "value for diagnostic step is out of range: "
104                             + step_for_diagnostics );
105                 }
106             }
107             if ( cla.isOptionSet( MIN_LENGTH_OPTION ) ) {
108                 min_length = cla.getOptionValueAsInt( MIN_LENGTH_OPTION );
109                 if ( ( min_length < 1 ) || ( min_length > longest_msa_seq ) ) {
110                     ForesterUtil.fatalError( PRG_NAME, "value for minimal sequence length is out of range: "
111                             + min_length );
112                 }
113             }
114             if ( cla.isOptionSet( MIN_LENGTH_OPTION ) ) {
115                 gap_ratio = cla.getOptionValueAsDouble( GAP_RATIO_LENGTH_OPTION );
116                 if ( ( gap_ratio < 0 ) || ( gap_ratio > 1 ) ) {
117                     ForesterUtil.fatalError( PRG_NAME, "gap ratio is out of range: " + gap_ratio );
118                 }
119             }
120             if ( cla.isOptionSet( REPORT_ALN_MEAN_IDENTITY ) ) {
121                 report_aln_mean_identity = true;
122             }
123             if ( cla.isOptionSet( OUTPUT_FORMAT_PHYLIP_OPTION ) ) {
124                 output_format = PHYLIP;
125             }
126             if ( cla.isOptionSet( OUTPUT_REMOVED_SEQS_OPTION ) ) {
127                 gap_ratio = cla.getOptionValueAsCleanString( OUTPUT_REMOVED_SEQS_OPTION );
128                 if ( ( gap_ratio < 0 ) || ( gap_ratio > 1 ) ) {
129                     ForesterUtil.fatalError( PRG_NAME, "gap ratio is out of range: " + gap_ratio );
130                 }
131             }
132             //
133             final List<String> allowed_options = new ArrayList<String>();
134             allowed_options.add( REMOVE_WORST_OFFENDERS_OPTION );
135             allowed_options.add( AV_GAPINESS_OPTION );
136             allowed_options.add( LENGTH_OPTION );
137             allowed_options.add( REALIGN_OPTION );
138             allowed_options.add( DO_NOT_NORMALIZE_FOR_EFF_LENGTH_OPTION );
139             allowed_options.add( STEP_OPTION );
140             allowed_options.add( PATH_TO_MAFFT_OPTION );
141             allowed_options.add( STEP_FOR_DIAGNOSTICS_OPTION );
142             allowed_options.add( MIN_LENGTH_OPTION );
143             allowed_options.add( GAP_RATIO_LENGTH_OPTION );
144             allowed_options.add( REPORT_ALN_MEAN_IDENTITY );
145             allowed_options.add( OUTPUT_FORMAT_PHYLIP_OPTION );
146             allowed_options.add( OUTPUT_REMOVED_SEQS_OPTION );
147             final String dissallowed_options = cla.validateAllowedOptionsAsString( allowed_options );
148             if ( dissallowed_options.length() > 0 ) {
149                 ForesterUtil.fatalError( PRG_NAME, "unknown option(s): " + dissallowed_options );
150             }
151             DeleteableMsa msa = null;
152             final FileInputStream is = new FileInputStream( in );
153             if ( FastaParser.isLikelyFasta( in ) ) {
154                 msa = DeleteableMsa.createInstance( FastaParser.parseMsa( is ) );
155             }
156             else {
157                 msa = DeleteableMsa.createInstance( GeneralMsaParser.parse( is ) );
158             }
159             if ( cla.isOptionSet( REMOVE_WORST_OFFENDERS_OPTION ) ) {
160                 worst_remove = cla.getOptionValueAsInt( REMOVE_WORST_OFFENDERS_OPTION );
161                 if ( ( worst_remove < 1 ) || ( worst_remove >= msa.getNumberOfSequences() - 1 ) ) {
162                     ForesterUtil.fatalError( PRG_NAME, "number of worst offender sequences to remove is out of range: "
163                             + worst_remove );
164                 }
165             }
166             if ( cla.isOptionSet( AV_GAPINESS_OPTION ) ) {
167                 if ( cla.isOptionSet( REMOVE_WORST_OFFENDERS_OPTION ) ) {
168                     printHelp();
169                     System.exit( 0 );
170                 }
171                 av_gap = cla.getOptionValueAsDouble( AV_GAPINESS_OPTION );
172                 if ( ( av_gap < 0 ) || ( av_gap >= 1 ) ) {
173                     ForesterUtil.fatalError( PRG_NAME, "target gap-ratio is out of range: " + av_gap );
174                 }
175             }
176             if ( cla.isOptionSet( LENGTH_OPTION ) ) {
177                 if ( cla.isOptionSet( REMOVE_WORST_OFFENDERS_OPTION ) || cla.isOptionSet( AV_GAPINESS_OPTION ) ) {
178                     printHelp();
179                     System.exit( 0 );
180                 }
181                 length = cla.getOptionValueAsInt( LENGTH_OPTION );
182                 if ( ( length < 2 ) || ( length >= msa.getLength() ) ) {
183                     ForesterUtil.fatalError( PRG_NAME, "target length is out of range: " + length );
184                 }
185             }
186             if ( cla.isOptionSet( STEP_OPTION ) ) {
187                 step = cla.getOptionValueAsInt( STEP_OPTION );
188                 if ( ( step < 1 )
189                         || ( ( step > msa.getNumberOfSequences() ) || ( ( worst_remove > 0 ) && ( step > worst_remove ) ) ) ) {
190                     ForesterUtil.fatalError( PRG_NAME, "value for step is out of range: " + step );
191                 }
192             }
193             if ( cla.isOptionSet( REALIGN_OPTION ) ) {
194                 realign = true;
195             }
196             if ( cla.isOptionSet( PATH_TO_MAFFT_OPTION ) ) {
197                 if ( !realign ) {
198                     ForesterUtil.fatalError( PRG_NAME, "no need to indicate path to MAFFT without realigning" );
199                 }
200                 path_to_mafft = cla.getOptionValueAsCleanString( PATH_TO_MAFFT_OPTION );
201             }
202             if ( cla.isOptionSet( DO_NOT_NORMALIZE_FOR_EFF_LENGTH_OPTION ) ) {
203                 norm = false;
204             }
205             if ( realign ) {
206                 if ( ForesterUtil.isEmpty( path_to_mafft ) ) {
207                     path_to_mafft = MsaCompactor.guessPathToMafft();
208                 }
209                 checkPathToMafft( path_to_mafft );
210             }
211             if ( worst_remove > 0 ) {
212                 MsaCompactor.removeWorstOffenders( msa, worst_remove, step, realign, norm, path_to_mafft, out );
213             }
214             else if ( av_gap > 0 ) {
215                 MsaCompactor.reduceGapAverage( msa, av_gap, step, realign, norm, path_to_mafft, out );
216             }
217             else if ( length > 0 ) {
218                 // TODO if < shortest seq -> error
219                 MsaCompactor.reduceLength( msa, length, step, realign, norm, path_to_mafft, out );
220             }
221             else {
222                 MsaCompactor.chart( msa, step, realign, norm, path_to_mafft );
223             }
224         }
225         catch ( final Exception e ) {
226             e.printStackTrace();
227             ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
228         }
229     }
230
231     private static void checkPathToMafft( final String path_to_mafft ) {
232         if ( !ForesterUtil.isEmpty( path_to_mafft ) && MsaInferrer.isInstalled( path_to_mafft ) ) {
233             ForesterUtil.programMessage( PRG_NAME, "using MAFFT at \"" + path_to_mafft + "\"" );
234         }
235         else {
236             if ( ForesterUtil.isEmpty( path_to_mafft ) ) {
237                 ForesterUtil.fatalError( PRG_NAME, "no MAFFT executable found, use -\"" + PATH_TO_MAFFT_OPTION
238                         + "=<path to MAFFT>\" option" );
239             }
240             else {
241                 ForesterUtil.fatalError( PRG_NAME, "no MAFFT executable at \"" + path_to_mafft + "\"" );
242             }
243         }
244     }
245
246     private static void printHelp() {
247         ForesterUtil.printProgramInformation( PRG_NAME,
248                                               PRG_DESC,
249                                               PRG_VERSION,
250                                               PRG_DATE,
251                                               E_MAIL,
252                                               WWW,
253                                               ForesterUtil.getForesterLibraryInformation() );
254         final String path_to_mafft = MsaCompactor.guessPathToMafft();
255         String mafft_comment;
256         if ( !ForesterUtil.isEmpty( path_to_mafft ) ) {
257             mafft_comment = " (using " + path_to_mafft + ")";
258         }
259         else {
260             mafft_comment = " (no path to MAFFT found, use -\"" + PATH_TO_MAFFT_OPTION + "=<path to MAFFT>\" option";
261         }
262         System.out.println( "Usage:" );
263         System.out.println();
264         System.out.println( PRG_NAME + " <options> <msa input file> <output file>" );
265         System.out.println();
266         System.out.println( " options: " );
267         System.out.println();
268         System.out.println( "   -" + REMOVE_WORST_OFFENDERS_OPTION
269                 + "=<integer>   number of worst offender sequences to remove" );
270         System.out.println( "   -" + LENGTH_OPTION + "=<integer>   target MSA length" );
271         System.out.println( "   -" + AV_GAPINESS_OPTION + "=<decimal>   target gap-ratio (0.0-1.0)" );
272         System.out.println( "   -" + STEP_OPTION + "=<integer>   step for output and re-aligning (default: 1)" );
273         System.out.println( "   -" + REALIGN_OPTION + "             to realign using MAFFT" + mafft_comment );
274         System.out.println( "   -" + STEP_FOR_DIAGNOSTICS_OPTION
275                 + "=<integer>  step for diagnostics reports (default: 1)" );
276         System.out.println( "   -" + MIN_LENGTH_OPTION
277                 + "=<integer>  minimal effecive sequence length (for deleting of shorter sequences)" );
278         System.out.println( "   -" + GAP_RATIO_LENGTH_OPTION
279                 + "=<decimal>  maximal allowed gap ratio per column (for deleting of columms) (0.0-1.0)" );
280         System.out.println( "   -" + REPORT_ALN_MEAN_IDENTITY
281                 + "             to report mean identity diagnostic (not recommended for very large alignments)" );
282         System.out.println( "   -" + OUTPUT_FORMAT_PHYLIP_OPTION
283                 + "             to write output alignments in phylip format instead of fasta" );
284         System.out.println( "   -" + OUTPUT_REMOVED_SEQS_OPTION + "=<file>     to output the removed sequences" );
285         System.out.println();
286         System.out.println();
287         System.out.println();
288     }
289 }