2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
5 // Copyright (C) 2014 Christian M. Zmasek
6 // Copyright (C) 2014 Sanford-Burnham Medical Research Institute
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.
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.
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
23 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
25 package org.forester.application;
28 import java.io.FileInputStream;
29 import java.util.ArrayList;
30 import java.util.List;
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.MsaCompactor2;
41 import org.forester.msa_compactor.MsaProperties;
42 import org.forester.util.CommandLineArguments;
43 import org.forester.util.DescriptiveStatistics;
44 import org.forester.util.ForesterUtil;
46 public class msa_compactor {
48 final static private String HELP_OPTION_1 = "help";
49 final static private String HELP_OPTION_2 = "h";
50 final static private String REMOVE_WORST_OFFENDERS_OPTION = "r";
51 final static private String AV_GAPINESS_OPTION = "g";
52 final static private String STEP_OPTION = "s";
53 final static private String LENGTH_OPTION = "l";
54 final static private String REALIGN_OPTION = "a";
56 final static private String STEP_FOR_DIAGNOSTICS_OPTION = "sd";
57 final static private String MIN_LENGTH_OPTION = "ml";
58 final static private String GAP_RATIO_LENGTH_OPTION = "gr";
59 final static private String REPORT_ALN_MEAN_IDENTITY = "q";
60 final static private String OUTPUT_FORMAT_PHYLIP_OPTION = "f";
61 final static private String OUTPUT_REMOVED_SEQS_OPTION = "ro";
62 final static private String MAFFT_OPTIONS = "mo";
64 final static private String PATH_TO_MAFFT_OPTION = "mafft";
65 final static private String DO_NOT_NORMALIZE_FOR_EFF_LENGTH_OPTION = "nn";
66 final static private String PRG_NAME = "msa_compactor";
67 final static private String PRG_DESC = "multiple sequence aligment compactor";
68 final static private String PRG_VERSION = "0.02";
69 final static private String PRG_DATE = "140316";
70 final static private String E_MAIL = "czmasek@sanfordburham.org";
71 final static private String WWW = "https://sites.google.com/site/cmzmasek/home/software/forester";
73 public static void main( final String args[] ) {
75 final CommandLineArguments cla = new CommandLineArguments( args );
76 if ( cla.isOptionSet( HELP_OPTION_1 ) || cla.isOptionSet( HELP_OPTION_2 )
77 || ( ( cla.getNumberOfNames() < 1 ) || ( cla.getNumberOfNames() > 2 ) ) ) {
81 final File in = cla.getFile( 0 );
83 if ( cla.getNumberOfNames() > 1 ) {
84 out = cla.getFile( 1 );
86 int worst_remove = -1;
90 boolean realign = false;
92 String path_to_mafft = null;
93 int step_for_diagnostics = -1;
95 double gap_ratio = -1;
96 boolean report_aln_mean_identity = false;
97 MSA_FORMAT output_format = MSA_FORMAT.FASTA;
98 File removed_seqs_out_base = null;
99 String mafft_options = "--auto";
100 final List<String> allowed_options = new ArrayList<String>();
101 allowed_options.add( REMOVE_WORST_OFFENDERS_OPTION );
102 allowed_options.add( AV_GAPINESS_OPTION );
103 allowed_options.add( LENGTH_OPTION );
104 allowed_options.add( REALIGN_OPTION );
105 allowed_options.add( DO_NOT_NORMALIZE_FOR_EFF_LENGTH_OPTION );
106 allowed_options.add( STEP_OPTION );
107 allowed_options.add( PATH_TO_MAFFT_OPTION );
108 allowed_options.add( STEP_FOR_DIAGNOSTICS_OPTION );
109 allowed_options.add( MIN_LENGTH_OPTION );
110 allowed_options.add( GAP_RATIO_LENGTH_OPTION );
111 allowed_options.add( REPORT_ALN_MEAN_IDENTITY );
112 allowed_options.add( OUTPUT_FORMAT_PHYLIP_OPTION );
113 allowed_options.add( OUTPUT_REMOVED_SEQS_OPTION );
114 allowed_options.add( MAFFT_OPTIONS );
115 final String dissallowed_options = cla.validateAllowedOptionsAsString( allowed_options );
116 if ( dissallowed_options.length() > 0 ) {
117 ForesterUtil.fatalError( PRG_NAME, "unknown option(s): " + dissallowed_options );
119 DeleteableMsa msa = null;
120 final FileInputStream is = new FileInputStream( in );
121 if ( FastaParser.isLikelyFasta( in ) ) {
122 msa = DeleteableMsa.createInstance( FastaParser.parseMsa( is ) );
125 msa = DeleteableMsa.createInstance( GeneralMsaParser.parse( is ) );
127 final DescriptiveStatistics initial_msa_stats = MsaMethods.calculateEffectiveLengthStatistics( msa );
128 System.out.println( initial_msa_stats.toString() );
129 if ( cla.isOptionSet( REMOVE_WORST_OFFENDERS_OPTION ) ) {
130 worst_remove = cla.getOptionValueAsInt( REMOVE_WORST_OFFENDERS_OPTION );
131 if ( ( worst_remove < 1 ) || ( worst_remove >= msa.getNumberOfSequences() - 1 ) ) {
132 ForesterUtil.fatalError( PRG_NAME, "number of worst offender sequences to remove is out of range: "
136 if ( cla.isOptionSet( AV_GAPINESS_OPTION ) ) {
137 if ( cla.isOptionSet( REMOVE_WORST_OFFENDERS_OPTION ) ) {
141 av_gap = cla.getOptionValueAsDouble( AV_GAPINESS_OPTION );
142 if ( ( av_gap < 0 ) || ( av_gap >= 1 ) ) {
143 ForesterUtil.fatalError( PRG_NAME, "target gap-ratio is out of range: " + av_gap );
146 if ( cla.isOptionSet( LENGTH_OPTION ) ) {
147 if ( cla.isOptionSet( REMOVE_WORST_OFFENDERS_OPTION ) || cla.isOptionSet( AV_GAPINESS_OPTION ) ) {
151 length = cla.getOptionValueAsInt( LENGTH_OPTION );
152 if ( ( length < 2 ) || ( length >= msa.getLength() ) ) {
153 ForesterUtil.fatalError( PRG_NAME, "target length is out of range: " + length );
156 if ( cla.isOptionSet( STEP_OPTION ) ) {
157 step = cla.getOptionValueAsInt( STEP_OPTION );
159 || ( ( step > msa.getNumberOfSequences() ) || ( ( worst_remove > 0 ) && ( step > worst_remove ) ) ) ) {
160 ForesterUtil.fatalError( PRG_NAME, "value for step is out of range: " + step );
163 if ( cla.isOptionSet( REALIGN_OPTION ) ) {
166 if ( cla.isOptionSet( PATH_TO_MAFFT_OPTION ) ) {
168 ForesterUtil.fatalError( PRG_NAME, "no need to indicate path to MAFFT without realigning" );
170 path_to_mafft = cla.getOptionValueAsCleanString( PATH_TO_MAFFT_OPTION );
172 if ( cla.isOptionSet( DO_NOT_NORMALIZE_FOR_EFF_LENGTH_OPTION ) ) {
176 if ( cla.isOptionSet( STEP_FOR_DIAGNOSTICS_OPTION ) ) {
177 step_for_diagnostics = cla.getOptionValueAsInt( STEP_FOR_DIAGNOSTICS_OPTION );
178 if ( ( step_for_diagnostics < 1 )
179 || ( ( step_for_diagnostics > msa.getNumberOfSequences() ) || ( ( worst_remove > 0 ) && ( step_for_diagnostics > worst_remove ) ) ) ) {
180 ForesterUtil.fatalError( PRG_NAME, "value for diagnostic step is out of range: "
181 + step_for_diagnostics );
184 if ( cla.isOptionSet( MIN_LENGTH_OPTION ) ) {
185 min_length = cla.getOptionValueAsInt( MIN_LENGTH_OPTION );
186 if ( ( min_length < 2 ) || ( min_length > initial_msa_stats.getMax() ) ) {
187 ForesterUtil.fatalError( PRG_NAME, "value for minimal sequence length is out of range: "
191 if ( cla.isOptionSet( MIN_LENGTH_OPTION ) ) {
192 gap_ratio = cla.getOptionValueAsDouble( GAP_RATIO_LENGTH_OPTION );
193 if ( ( gap_ratio < 0 ) || ( gap_ratio > 1 ) ) {
194 ForesterUtil.fatalError( PRG_NAME, "gap ratio is out of range: " + gap_ratio );
197 if ( cla.isOptionSet( REPORT_ALN_MEAN_IDENTITY ) ) {
198 report_aln_mean_identity = true;
200 if ( cla.isOptionSet( OUTPUT_FORMAT_PHYLIP_OPTION ) ) {
201 output_format = MSA_FORMAT.PHYLIP;
203 if ( cla.isOptionSet( OUTPUT_REMOVED_SEQS_OPTION ) ) {
204 String s = cla.getOptionValueAsCleanString( OUTPUT_REMOVED_SEQS_OPTION );
205 removed_seqs_out_base = new File( s );
208 if ( ForesterUtil.isEmpty( path_to_mafft ) ) {
209 path_to_mafft = MsaCompactor.guessPathToMafft();
211 checkPathToMafft( path_to_mafft );
212 if ( cla.isOptionSet( MAFFT_OPTIONS ) ) {
213 mafft_options = cla.getOptionValueAsCleanString( MAFFT_OPTIONS );
214 if ( ForesterUtil.isEmpty( mafft_options ) || mafft_options.length() < 3 ) {
215 ForesterUtil.fatalError( PRG_NAME, "gap ratio is out of range: " + gap_ratio );
219 ForesterUtil.printProgramInformation( PRG_NAME,
225 ForesterUtil.getForesterLibraryInformation() );
227 System.out.println( "Input MSA : " + in );
229 System.out.println( "Output : " + out );
232 System.out.println( "Output : n/a" );
234 if ( removed_seqs_out_base != null ) {
235 System.out.println( "Write removed sequences to : " + removed_seqs_out_base );
237 if ( worst_remove > 0 ) {
238 System.out.println( "Number of worst offenders to remove : " + worst_remove );
240 else if ( av_gap > 0 ) {
241 System.out.println( "Target gap-ratio : " + av_gap );
243 else if ( length > 0 ) {
244 System.out.println( "Target MSA length : " + length );
247 System.out.println( "Chart and diagnostics only : true" );
249 if ( out != null || removed_seqs_out_base != null ) {
250 System.out.println( "Output format : "
251 + ( output_format == MSA_FORMAT.FASTA ? "fasta" : "phylip" ) );
253 System.out.println( "Step for output and re-aligning) : " + step );
254 System.out.println( "Step for diagnostics reports : " + step_for_diagnostics );
255 System.out.println( "Report mean identity (\"MSA quality\") : " + report_aln_mean_identity );
257 System.out.println( "Normalize : " + norm );
259 System.out.println( "Realign : " + realign );
261 System.out.println( "MAFFT options : " + mafft_options );
263 if ( min_length > -1 ) {
264 System.out.println( "Minimal effective sequence length : " + min_length );
266 if ( gap_ratio > -1 ) {
267 System.out.println( "Maximum allowed gap ratio per column : " + gap_ratio );
269 System.out.println();
272 if ( worst_remove > 0 ) {
273 final MsaCompactor2 mc = new MsaCompactor2( msa );
274 mc.setRealign( realign );
276 mc.setPathToMafft( path_to_mafft );
279 mc.setOutFileBase( out );
281 mc.removeWorstOffenders( worst_remove, true );
283 else if ( av_gap > 0 ) {
284 final MsaCompactor2 mc = new MsaCompactor2( msa );
285 mc.setRealign( realign );
287 mc.setPathToMafft( path_to_mafft );
290 mc.setOutFileBase( out );
292 mc.removeViaGapAverage( av_gap, true );
294 else if ( length > 0 ) {
295 // TODO if < shortest seq -> error
296 final MsaCompactor2 mc = new MsaCompactor2( msa );
297 mc.setRealign( realign );
299 mc.setPathToMafft( path_to_mafft );
302 mc.setOutFileBase( out );
304 mc.removeViaLength( length, true );
307 //MsaCompactor.chart( msa, step, realign, norm, path_to_mafft );
308 final int initial_number_of_seqs = msa.getNumberOfSequences();
309 final MsaCompactor2 mc = new MsaCompactor2( msa );
310 mc.setRealign( realign );
312 mc.setPathToMafft( path_to_mafft );
315 mc.setOutFileBase( out );
317 final List<MsaProperties> msa_props = mc.chart( step, realign, norm, true );
318 Chart.display( msa_props, initial_number_of_seqs );
321 catch ( final Exception e ) {
323 ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
327 private static void checkPathToMafft( final String path_to_mafft ) {
328 if ( !ForesterUtil.isEmpty( path_to_mafft ) && MsaInferrer.isInstalled( path_to_mafft ) ) {
329 ForesterUtil.programMessage( PRG_NAME, "using MAFFT at \"" + path_to_mafft + "\"" );
332 if ( ForesterUtil.isEmpty( path_to_mafft ) ) {
333 ForesterUtil.fatalError( PRG_NAME, "no MAFFT executable found, use -\"" + PATH_TO_MAFFT_OPTION
334 + "=<path to MAFFT>\" option" );
337 ForesterUtil.fatalError( PRG_NAME, "no MAFFT executable at \"" + path_to_mafft + "\"" );
342 private static void printHelp() {
343 ForesterUtil.printProgramInformation( PRG_NAME,
349 ForesterUtil.getForesterLibraryInformation() );
350 final String path_to_mafft = MsaCompactor.guessPathToMafft();
351 String mafft_comment;
352 if ( !ForesterUtil.isEmpty( path_to_mafft ) ) {
353 mafft_comment = " (using " + path_to_mafft + ")";
356 mafft_comment = " (no path to MAFFT found, use -\"" + PATH_TO_MAFFT_OPTION + "=<path to MAFFT>\" option";
358 System.out.println( "Usage:" );
359 System.out.println();
360 System.out.println( PRG_NAME + " <options> <msa input file> <output file>" );
361 System.out.println();
362 System.out.println( " options: " );
363 System.out.println();
364 System.out.println( " -" + REMOVE_WORST_OFFENDERS_OPTION
365 + "=<integer> number of worst offender sequences to remove" );
366 System.out.println( " -" + LENGTH_OPTION + "=<integer> target MSA length" );
367 System.out.println( " -" + AV_GAPINESS_OPTION + "=<decimal> target gap-ratio (0.0-1.0)" );
368 System.out.println( " -" + STEP_OPTION + "=<integer> step for output and re-aligning (default: 1)" );
369 System.out.println( " -" + REALIGN_OPTION + " to realign using MAFFT" + mafft_comment );
370 System.out.println( " -" + MAFFT_OPTIONS + "=<string> options for MAFFT (default: --auto)" );
371 System.out.println( " -" + STEP_FOR_DIAGNOSTICS_OPTION
372 + "=<integer> step for diagnostics reports (default: 1)" );
373 System.out.println( " -" + MIN_LENGTH_OPTION
374 + "=<integer> minimal effecive sequence length (for deleting of shorter sequences)" );
375 System.out.println( " -" + GAP_RATIO_LENGTH_OPTION
376 + "=<decimal> maximal allowed gap ratio per column (for deleting of columms) (0.0-1.0)" );
377 System.out.println( " -" + REPORT_ALN_MEAN_IDENTITY
378 + " to report mean identity diagnostic (not recommended for very large alignments)" );
379 System.out.println( " -" + OUTPUT_FORMAT_PHYLIP_OPTION
380 + " to write output alignments in phylip format instead of fasta" );
381 System.out.println( " -" + OUTPUT_REMOVED_SEQS_OPTION + "=<file> to output the removed sequences" );
382 System.out.println();
383 System.out.println();
384 System.out.println();