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