inprogress
[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.Msa.MSA_FORMAT;
36 import org.forester.msa.MsaInferrer;
37 import org.forester.msa.MsaMethods;
38 import org.forester.msa_compactor.MsaCompactor;
39 import org.forester.util.CommandLineArguments;
40 import org.forester.util.DescriptiveStatistics;
41 import org.forester.util.ForesterUtil;
42
43 public class msa_compactor {
44
45     final static private String HELP_OPTION_1                          = "help";
46     final static private String HELP_OPTION_2                          = "h";
47     final static private String REMOVE_WORST_OFFENDERS_OPTION          = "r";
48     final static private String AV_GAPINESS_OPTION                     = "g";
49     final static private String STEP_OPTION                            = "s";
50     final static private String LENGTH_OPTION                          = "l";
51     final static private String REALIGN_OPTION                         = "a";
52     //
53     final static private String STEP_FOR_DIAGNOSTICS_OPTION            = "sd";
54     final static private String MIN_LENGTH_OPTION                      = "ml";
55     final static private String GAP_RATIO_LENGTH_OPTION                = "gr";
56     final static private String REPORT_ALN_MEAN_IDENTITY               = "q";
57     final static private String OUTPUT_FORMAT_PHYLIP_OPTION            = "f";
58     final static private String OUTPUT_REMOVED_SEQS_OPTION             = "ro";
59     final static private String MAFFT_OPTIONS                          = "mo";
60     //        
61     final static private String PATH_TO_MAFFT_OPTION                   = "mafft";
62     final static private String DO_NOT_NORMALIZE_FOR_EFF_LENGTH_OPTION = "nn";
63     final static private String PRG_NAME                               = "msa_compactor";
64     final static private String PRG_DESC                               = "multiple sequence aligment compactor";
65     final static private String PRG_VERSION                            = "0.02";
66     final static private String PRG_DATE                               = "140316";
67     final static private String E_MAIL                                 = "czmasek@sanfordburham.org";
68     final static private String WWW                                    = "https://sites.google.com/site/cmzmasek/home/software/forester";
69
70     public static void main( final String args[] ) {
71         try {
72             final CommandLineArguments cla = new CommandLineArguments( args );
73             if ( cla.isOptionSet( HELP_OPTION_1 ) || cla.isOptionSet( HELP_OPTION_2 )
74                     || ( ( cla.getNumberOfNames() < 1 ) || ( cla.getNumberOfNames() > 2 ) ) ) {
75                 printHelp();
76                 System.exit( 0 );
77             }
78             final File in = cla.getFile( 0 );
79             File out = null;
80             if ( cla.getNumberOfNames() > 1 ) {
81                 out = cla.getFile( 1 );
82             }
83             int worst_remove = -1;
84             double av_gap = -1;
85             int length = -1;
86             int step = -1;
87             boolean realign = false;
88             boolean norm = true;
89             String path_to_mafft = null;
90             int step_for_diagnostics = -1;
91             int min_length = -1;
92             double gap_ratio = -1;
93             boolean report_aln_mean_identity = false;
94             MSA_FORMAT output_format = MSA_FORMAT.FASTA;
95             File removed_seqs_out_base = null;
96             String mafft_options = "--auto";
97             final List<String> allowed_options = new ArrayList<String>();
98             allowed_options.add( REMOVE_WORST_OFFENDERS_OPTION );
99             allowed_options.add( AV_GAPINESS_OPTION );
100             allowed_options.add( LENGTH_OPTION );
101             allowed_options.add( REALIGN_OPTION );
102             allowed_options.add( DO_NOT_NORMALIZE_FOR_EFF_LENGTH_OPTION );
103             allowed_options.add( STEP_OPTION );
104             allowed_options.add( PATH_TO_MAFFT_OPTION );
105             allowed_options.add( STEP_FOR_DIAGNOSTICS_OPTION );
106             allowed_options.add( MIN_LENGTH_OPTION );
107             allowed_options.add( GAP_RATIO_LENGTH_OPTION );
108             allowed_options.add( REPORT_ALN_MEAN_IDENTITY );
109             allowed_options.add( OUTPUT_FORMAT_PHYLIP_OPTION );
110             allowed_options.add( OUTPUT_REMOVED_SEQS_OPTION );
111             allowed_options.add( MAFFT_OPTIONS );
112             final String dissallowed_options = cla.validateAllowedOptionsAsString( allowed_options );
113             if ( dissallowed_options.length() > 0 ) {
114                 ForesterUtil.fatalError( PRG_NAME, "unknown option(s): " + dissallowed_options );
115             }
116             DeleteableMsa msa = null;
117             final FileInputStream is = new FileInputStream( in );
118             if ( FastaParser.isLikelyFasta( in ) ) {
119                 msa = DeleteableMsa.createInstance( FastaParser.parseMsa( is ) );
120             }
121             else {
122                 msa = DeleteableMsa.createInstance( GeneralMsaParser.parse( is ) );
123             }
124             final DescriptiveStatistics initial_msa_stats = MsaMethods.calculateEffectiveLengthStatistics( msa );
125             System.out.println( initial_msa_stats.toString() );
126             if ( cla.isOptionSet( REMOVE_WORST_OFFENDERS_OPTION ) ) {
127                 worst_remove = cla.getOptionValueAsInt( REMOVE_WORST_OFFENDERS_OPTION );
128                 if ( ( worst_remove < 1 ) || ( worst_remove >= msa.getNumberOfSequences() - 1 ) ) {
129                     ForesterUtil.fatalError( PRG_NAME, "number of worst offender sequences to remove is out of range: "
130                             + worst_remove );
131                 }
132             }
133             if ( cla.isOptionSet( AV_GAPINESS_OPTION ) ) {
134                 if ( cla.isOptionSet( REMOVE_WORST_OFFENDERS_OPTION ) ) {
135                     printHelp();
136                     System.exit( 0 );
137                 }
138                 av_gap = cla.getOptionValueAsDouble( AV_GAPINESS_OPTION );
139                 if ( ( av_gap < 0 ) || ( av_gap >= 1 ) ) {
140                     ForesterUtil.fatalError( PRG_NAME, "target gap-ratio is out of range: " + av_gap );
141                 }
142             }
143             if ( cla.isOptionSet( LENGTH_OPTION ) ) {
144                 if ( cla.isOptionSet( REMOVE_WORST_OFFENDERS_OPTION ) || cla.isOptionSet( AV_GAPINESS_OPTION ) ) {
145                     printHelp();
146                     System.exit( 0 );
147                 }
148                 length = cla.getOptionValueAsInt( LENGTH_OPTION );
149                 if ( ( length < 2 ) || ( length >= msa.getLength() ) ) {
150                     ForesterUtil.fatalError( PRG_NAME, "target length is out of range: " + length );
151                 }
152             }
153             if ( cla.isOptionSet( STEP_OPTION ) ) {
154                 step = cla.getOptionValueAsInt( STEP_OPTION );
155                 if ( ( step < 1 )
156                         || ( ( step > msa.getNumberOfSequences() ) || ( ( worst_remove > 0 ) && ( step > worst_remove ) ) ) ) {
157                     ForesterUtil.fatalError( PRG_NAME, "value for step is out of range: " + step );
158                 }
159             }
160             if ( cla.isOptionSet( REALIGN_OPTION ) ) {
161                 realign = true;
162             }
163             if ( cla.isOptionSet( PATH_TO_MAFFT_OPTION ) ) {
164                 if ( !realign ) {
165                     ForesterUtil.fatalError( PRG_NAME, "no need to indicate path to MAFFT without realigning" );
166                 }
167                 path_to_mafft = cla.getOptionValueAsCleanString( PATH_TO_MAFFT_OPTION );
168             }
169             if ( cla.isOptionSet( DO_NOT_NORMALIZE_FOR_EFF_LENGTH_OPTION ) ) {
170                 norm = false;
171             }
172             //
173             if ( cla.isOptionSet( STEP_FOR_DIAGNOSTICS_OPTION ) ) {
174                 step_for_diagnostics = cla.getOptionValueAsInt( STEP_FOR_DIAGNOSTICS_OPTION );
175                 if ( ( step_for_diagnostics < 1 )
176                         || ( ( step_for_diagnostics > msa.getNumberOfSequences() ) || ( ( worst_remove > 0 ) && ( step_for_diagnostics > worst_remove ) ) ) ) {
177                     ForesterUtil.fatalError( PRG_NAME, "value for diagnostic step is out of range: "
178                             + step_for_diagnostics );
179                 }
180             }
181             if ( cla.isOptionSet( MIN_LENGTH_OPTION ) ) {
182                 min_length = cla.getOptionValueAsInt( MIN_LENGTH_OPTION );
183                 if ( ( min_length < 2 ) || ( min_length > initial_msa_stats.getMax() ) ) {
184                     ForesterUtil.fatalError( PRG_NAME, "value for minimal sequence length is out of range: "
185                             + min_length );
186                 }
187             }
188             if ( cla.isOptionSet( MIN_LENGTH_OPTION ) ) {
189                 gap_ratio = cla.getOptionValueAsDouble( GAP_RATIO_LENGTH_OPTION );
190                 if ( ( gap_ratio < 0 ) || ( gap_ratio > 1 ) ) {
191                     ForesterUtil.fatalError( PRG_NAME, "gap ratio is out of range: " + gap_ratio );
192                 }
193             }
194             if ( cla.isOptionSet( REPORT_ALN_MEAN_IDENTITY ) ) {
195                 report_aln_mean_identity = true;
196             }
197             if ( cla.isOptionSet( OUTPUT_FORMAT_PHYLIP_OPTION ) ) {
198                 output_format = MSA_FORMAT.PHYLIP;
199             }
200             if ( cla.isOptionSet( OUTPUT_REMOVED_SEQS_OPTION ) ) {
201                 String s = cla.getOptionValueAsCleanString( OUTPUT_REMOVED_SEQS_OPTION );
202                 removed_seqs_out_base = new File( s );
203             }
204             if ( realign ) {
205                 if ( ForesterUtil.isEmpty( path_to_mafft ) ) {
206                     path_to_mafft = MsaCompactor.guessPathToMafft();
207                 }
208                 checkPathToMafft( path_to_mafft );
209                 if ( cla.isOptionSet( MAFFT_OPTIONS ) ) {
210                     mafft_options = cla.getOptionValueAsCleanString( MAFFT_OPTIONS );
211                     if ( ForesterUtil.isEmpty( mafft_options ) || mafft_options.length() < 3 ) {
212                         ForesterUtil.fatalError( PRG_NAME, "gap ratio is out of range: " + gap_ratio );
213                     }
214                 }
215             }
216             ForesterUtil.printProgramInformation( PRG_NAME,
217                                                   PRG_DESC,
218                                                   PRG_VERSION,
219                                                   PRG_DATE,
220                                                   E_MAIL,
221                                                   WWW,
222                                                   ForesterUtil.getForesterLibraryInformation() );
223             //
224             System.out.println( "Input MSA                            : " + in );
225             if ( out != null ) {
226                 System.out.println( "Output                               : " + out );
227             }
228             else {
229                 System.out.println( "Output                               : n/a" );
230             }
231             if ( removed_seqs_out_base != null ) {
232                 System.out.println( "Write removed sequences to           : " + removed_seqs_out_base );
233             }
234             if ( worst_remove > 0 ) {
235                 System.out.println( "Number of worst offenders to remove  : " + worst_remove );
236             }
237             else if ( av_gap > 0 ) {
238                 System.out.println( "Target gap-ratio                     : " + av_gap );
239             }
240             else if ( length > 0 ) {
241                 System.out.println( "Target MSA length                    : " + length );
242             }
243             else {
244                 System.out.println( "Chart and diagnostics only           : true" );
245             }
246             if ( out != null || removed_seqs_out_base != null ) {
247                 System.out.println( "Output format                        : "
248                         + ( output_format == MSA_FORMAT.FASTA ? "fasta" : "phylip" ) );
249             }
250             System.out.println( "Step for output and re-aligning)     : " + step );
251             System.out.println( "Step for diagnostics reports         : " + step_for_diagnostics );
252             System.out.println( "Report mean identity (\"MSA quality\") : " + report_aln_mean_identity );
253             if ( !norm ) {
254                 System.out.println( "Normalize                            : " + norm );
255             }
256             System.out.println( "Realign                              : " + realign );
257             if ( realign ) {
258                 System.out.println( "MAFFT options                        : " + mafft_options );
259             }
260             if ( min_length > -1 ) {
261                 System.out.println( "Minimal effective sequence length    : " + min_length );
262             }
263             if ( gap_ratio > -1 ) {
264                 System.out.println( "Maximum allowed gap ratio per column : " + gap_ratio );
265             }
266             System.out.println();
267             //
268             if ( worst_remove > 0 ) {
269                 MsaCompactor.removeWorstOffenders( msa, worst_remove, step, realign, norm, path_to_mafft, out );
270             }
271             else if ( av_gap > 0 ) {
272                 MsaCompactor.reduceGapAverage( msa, av_gap, step, realign, norm, path_to_mafft, out );
273             }
274             else if ( length > 0 ) {
275                 // TODO if < shortest seq -> error
276                 MsaCompactor.reduceLength( msa, length, step, realign, norm, path_to_mafft, out );
277             }
278             else {
279                 MsaCompactor.chart( msa, step, realign, norm, path_to_mafft );
280             }
281         }
282         catch ( final Exception e ) {
283             e.printStackTrace();
284             ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
285         }
286     }
287
288     private static void checkPathToMafft( final String path_to_mafft ) {
289         if ( !ForesterUtil.isEmpty( path_to_mafft ) && MsaInferrer.isInstalled( path_to_mafft ) ) {
290             ForesterUtil.programMessage( PRG_NAME, "using MAFFT at \"" + path_to_mafft + "\"" );
291         }
292         else {
293             if ( ForesterUtil.isEmpty( path_to_mafft ) ) {
294                 ForesterUtil.fatalError( PRG_NAME, "no MAFFT executable found, use -\"" + PATH_TO_MAFFT_OPTION
295                         + "=<path to MAFFT>\" option" );
296             }
297             else {
298                 ForesterUtil.fatalError( PRG_NAME, "no MAFFT executable at \"" + path_to_mafft + "\"" );
299             }
300         }
301     }
302
303     private static void printHelp() {
304         ForesterUtil.printProgramInformation( PRG_NAME,
305                                               PRG_DESC,
306                                               PRG_VERSION,
307                                               PRG_DATE,
308                                               E_MAIL,
309                                               WWW,
310                                               ForesterUtil.getForesterLibraryInformation() );
311         final String path_to_mafft = MsaCompactor.guessPathToMafft();
312         String mafft_comment;
313         if ( !ForesterUtil.isEmpty( path_to_mafft ) ) {
314             mafft_comment = " (using " + path_to_mafft + ")";
315         }
316         else {
317             mafft_comment = " (no path to MAFFT found, use -\"" + PATH_TO_MAFFT_OPTION + "=<path to MAFFT>\" option";
318         }
319         System.out.println( "Usage:" );
320         System.out.println();
321         System.out.println( PRG_NAME + " <options> <msa input file> <output file>" );
322         System.out.println();
323         System.out.println( " options: " );
324         System.out.println();
325         System.out.println( "   -" + REMOVE_WORST_OFFENDERS_OPTION
326                 + "=<integer>   number of worst offender sequences to remove" );
327         System.out.println( "   -" + LENGTH_OPTION + "=<integer>   target MSA length" );
328         System.out.println( "   -" + AV_GAPINESS_OPTION + "=<decimal>   target gap-ratio (0.0-1.0)" );
329         System.out.println( "   -" + STEP_OPTION + "=<integer>   step for output and re-aligning (default: 1)" );
330         System.out.println( "   -" + REALIGN_OPTION + "             to realign using MAFFT" + mafft_comment );
331         System.out.println( "   -" + MAFFT_OPTIONS + "=<string>   options for MAFFT (default: --auto)" );
332         System.out.println( "   -" + STEP_FOR_DIAGNOSTICS_OPTION
333                 + "=<integer>  step for diagnostics reports (default: 1)" );
334         System.out.println( "   -" + MIN_LENGTH_OPTION
335                 + "=<integer>  minimal effecive sequence length (for deleting of shorter sequences)" );
336         System.out.println( "   -" + GAP_RATIO_LENGTH_OPTION
337                 + "=<decimal>  maximal allowed gap ratio per column (for deleting of columms) (0.0-1.0)" );
338         System.out.println( "   -" + REPORT_ALN_MEAN_IDENTITY
339                 + "             to report mean identity diagnostic (not recommended for very large alignments)" );
340         System.out.println( "   -" + OUTPUT_FORMAT_PHYLIP_OPTION
341                 + "             to write output alignments in phylip format instead of fasta" );
342         System.out.println( "   -" + OUTPUT_REMOVED_SEQS_OPTION + "=<file>     to output the removed sequences" );
343         System.out.println();
344         System.out.println();
345         System.out.println();
346     }
347 }