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.io.IOException;
30 import java.math.RoundingMode;
31 import java.text.DecimalFormat;
32 import java.text.NumberFormat;
33 import java.util.ArrayList;
34 import java.util.List;
36 import org.forester.io.parsers.FastaParser;
37 import org.forester.io.parsers.GeneralMsaParser;
38 import org.forester.msa.DeleteableMsa;
39 import org.forester.msa.Msa.MSA_FORMAT;
40 import org.forester.msa.MsaInferrer;
41 import org.forester.msa.MsaMethods;
42 import org.forester.msa_compactor.Chart;
43 import org.forester.msa_compactor.MsaCompactor;
44 import org.forester.msa_compactor.MsaProperties;
45 import org.forester.util.CommandLineArguments;
46 import org.forester.util.DescriptiveStatistics;
47 import org.forester.util.ForesterUtil;
49 public class msa_compactor {
51 final private static NumberFormat NF_1 = new DecimalFormat( "#.0" );
53 NF_1.setRoundingMode( RoundingMode.HALF_UP );
55 final static private String HELP_OPTION_1 = "help";
56 final static private String HELP_OPTION_2 = "h";
57 final static private String REMOVE_WORST_OFFENDERS_OPTION = "r";
58 final static private String AV_GAPINESS_OPTION = "g";
59 final static private String STEP_OPTION = "s";
60 final static private String LENGTH_OPTION = "l";
61 final static private String REALIGN_OPTION = "a";
63 final static private String STEP_FOR_DIAGNOSTICS_OPTION = "sd";
64 final static private String MIN_LENGTH_OPTION = "ml";
65 final static private String GAP_RATIO_LENGTH_OPTION = "gr";
66 final static private String REPORT_ENTROPY = "e";
67 final static private String OUTPUT_FORMAT_PHYLIP_OPTION = "p";
68 final static private String OUTPUT_REMOVED_SEQS_OPTION = "ro";
69 final static private String MAFFT_OPTIONS = "mo";
70 final static private String PERFORM_PHYLOGENETIC_INFERENCE = "t";
72 final static private String PATH_TO_MAFFT_OPTION = "mafft";
73 final static private String DO_NOT_NORMALIZE_FOR_EFF_LENGTH_OPTION = "nn";
74 final static private String PRG_NAME = "msa_compactor";
75 final static private String PRG_DESC = "multiple sequence aligment compactor";
76 final static private String PRG_VERSION = "0.3";
77 final static private String PRG_DATE = "140508";
78 final static private String E_MAIL = "czmasek@sanfordburham.org";
79 final static private String WWW = "https://sites.google.com/site/cmzmasek/home/software/forester";
81 public static void main( final String args[] ) {
83 final CommandLineArguments cla = new CommandLineArguments( args );
84 if ( cla.isOptionSet( HELP_OPTION_1 ) || cla.isOptionSet( HELP_OPTION_2 )
85 || ( ( cla.getNumberOfNames() < 1 ) || ( cla.getNumberOfNames() > 2 ) ) ) {
89 final File in = cla.getFile( 0 );
91 if ( cla.getNumberOfNames() > 1 ) {
92 out = cla.getFile( 1 );
94 int worst_remove = -1;
98 boolean realign = false;
100 String path_to_mafft = null;
101 int step_for_diagnostics = 1;
103 double gap_ratio = -1;
104 boolean report_entropy = false;
105 MSA_FORMAT output_format = MSA_FORMAT.FASTA;
106 File removed_seqs_out_base = null;
107 String mafft_options = "--auto";
108 boolean perform_phylogenetic_inference = false;
109 final List<String> allowed_options = new ArrayList<String>();
110 allowed_options.add( REMOVE_WORST_OFFENDERS_OPTION );
111 allowed_options.add( AV_GAPINESS_OPTION );
112 allowed_options.add( LENGTH_OPTION );
113 allowed_options.add( REALIGN_OPTION );
114 allowed_options.add( DO_NOT_NORMALIZE_FOR_EFF_LENGTH_OPTION );
115 allowed_options.add( STEP_OPTION );
116 allowed_options.add( PATH_TO_MAFFT_OPTION );
117 allowed_options.add( STEP_FOR_DIAGNOSTICS_OPTION );
118 allowed_options.add( MIN_LENGTH_OPTION );
119 allowed_options.add( GAP_RATIO_LENGTH_OPTION );
120 allowed_options.add( REPORT_ENTROPY );
121 allowed_options.add( OUTPUT_FORMAT_PHYLIP_OPTION );
122 allowed_options.add( OUTPUT_REMOVED_SEQS_OPTION );
123 allowed_options.add( MAFFT_OPTIONS );
124 allowed_options.add( PERFORM_PHYLOGENETIC_INFERENCE );
125 final String dissallowed_options = cla.validateAllowedOptionsAsString( allowed_options );
126 if ( dissallowed_options.length() > 0 ) {
127 ForesterUtil.fatalError( PRG_NAME, "unknown option(s): " + dissallowed_options );
129 DeleteableMsa msa = null;
130 final FileInputStream is = new FileInputStream( in );
131 if ( FastaParser.isLikelyFasta( in ) ) {
132 msa = DeleteableMsa.createInstance( FastaParser.parseMsa( is ) );
135 msa = DeleteableMsa.createInstance( GeneralMsaParser.parse( is ) );
137 final DescriptiveStatistics initial_msa_stats = MsaMethods.calculateEffectiveLengthStatistics( msa );
138 final boolean chart_only = ( !cla.isOptionSet( LENGTH_OPTION ) )
139 && ( !cla.isOptionSet( REMOVE_WORST_OFFENDERS_OPTION ) )
140 && ( !cla.isOptionSet( AV_GAPINESS_OPTION ) );
141 if ( !chart_only && ( out == null ) ) {
142 ForesterUtil.fatalError( PRG_NAME, "outfile file missing" );
144 if ( cla.isOptionSet( REMOVE_WORST_OFFENDERS_OPTION ) ) {
145 worst_remove = cla.getOptionValueAsInt( REMOVE_WORST_OFFENDERS_OPTION );
146 if ( ( worst_remove < 1 ) || ( worst_remove >= msa.getNumberOfSequences() - 1 ) ) {
147 ForesterUtil.fatalError( PRG_NAME, "number of worst offender sequences to remove is out of range: "
151 if ( cla.isOptionSet( AV_GAPINESS_OPTION ) ) {
152 if ( cla.isOptionSet( REMOVE_WORST_OFFENDERS_OPTION ) ) {
156 av_gap = cla.getOptionValueAsDouble( AV_GAPINESS_OPTION );
157 if ( ( av_gap < 0 ) || ( av_gap >= 1 ) ) {
158 ForesterUtil.fatalError( PRG_NAME, "target gap-ratio is out of range: " + av_gap );
161 if ( cla.isOptionSet( LENGTH_OPTION ) ) {
162 if ( cla.isOptionSet( REMOVE_WORST_OFFENDERS_OPTION ) || cla.isOptionSet( AV_GAPINESS_OPTION ) ) {
166 length = cla.getOptionValueAsInt( LENGTH_OPTION );
167 if ( length >= msa.getLength() ) {
168 ForesterUtil.fatalError( PRG_NAME,
169 "target length is out of range [longer than MSA (" + msa.getLength()
172 else if ( length < initial_msa_stats.getMin() ) {
173 ForesterUtil.fatalError( PRG_NAME,
174 "target length is out of range [shorter than the shortest sequence ("
175 + initial_msa_stats.getMin() + ") ]: " + length );
178 if ( cla.isOptionSet( STEP_OPTION ) ) {
179 step = cla.getOptionValueAsInt( STEP_OPTION );
181 || ( ( step > msa.getNumberOfSequences() ) || ( ( worst_remove > 0 ) && ( step > worst_remove ) ) ) ) {
182 ForesterUtil.fatalError( PRG_NAME, "value for step is out of range: " + step );
185 if ( cla.isOptionSet( REALIGN_OPTION ) ) {
188 if ( cla.isOptionSet( PATH_TO_MAFFT_OPTION ) ) {
190 ForesterUtil.fatalError( PRG_NAME, "no need to indicate path to MAFFT without realigning" );
192 path_to_mafft = cla.getOptionValueAsCleanString( PATH_TO_MAFFT_OPTION );
194 if ( cla.isOptionSet( DO_NOT_NORMALIZE_FOR_EFF_LENGTH_OPTION ) ) {
197 if ( cla.isOptionSet( STEP_FOR_DIAGNOSTICS_OPTION ) ) {
198 step_for_diagnostics = cla.getOptionValueAsInt( STEP_FOR_DIAGNOSTICS_OPTION );
199 if ( ( step_for_diagnostics < 1 )
200 || ( ( step_for_diagnostics > msa.getNumberOfSequences() ) || ( ( worst_remove > 0 ) && ( step_for_diagnostics > worst_remove ) ) ) ) {
201 ForesterUtil.fatalError( PRG_NAME, "value for diagnostic step is out of range: "
202 + step_for_diagnostics );
205 if ( cla.isOptionSet( MIN_LENGTH_OPTION ) ) {
206 min_length = cla.getOptionValueAsInt( MIN_LENGTH_OPTION );
207 if ( ( min_length < 2 ) || ( min_length > initial_msa_stats.getMax() ) ) {
208 ForesterUtil.fatalError( PRG_NAME, "value for minimal sequence length is out of range: "
212 if ( cla.isOptionSet( GAP_RATIO_LENGTH_OPTION ) ) {
213 gap_ratio = cla.getOptionValueAsDouble( GAP_RATIO_LENGTH_OPTION );
214 if ( ( gap_ratio < 0 ) || ( gap_ratio > 1 ) ) {
215 ForesterUtil.fatalError( PRG_NAME, "gap ratio is out of range: " + gap_ratio );
218 if ( cla.isOptionSet( REPORT_ENTROPY ) ) {
219 report_entropy = true;
221 if ( cla.isOptionSet( OUTPUT_FORMAT_PHYLIP_OPTION ) ) {
222 output_format = MSA_FORMAT.PHYLIP;
224 if ( cla.isOptionSet( OUTPUT_REMOVED_SEQS_OPTION ) ) {
225 final String s = cla.getOptionValueAsCleanString( OUTPUT_REMOVED_SEQS_OPTION );
226 removed_seqs_out_base = new File( s );
229 if ( ForesterUtil.isEmpty( path_to_mafft ) ) {
230 path_to_mafft = MsaCompactor.guessPathToMafft();
232 checkPathToMafft( path_to_mafft );
233 if ( cla.isOptionSet( MAFFT_OPTIONS ) ) {
234 mafft_options = cla.getOptionValueAsCleanString( MAFFT_OPTIONS );
235 if ( ForesterUtil.isEmpty( mafft_options ) || ( mafft_options.length() < 3 ) ) {
236 ForesterUtil.fatalError( PRG_NAME, "illegal or empty MAFFT options: " + mafft_options );
240 else if ( cla.isOptionSet( MAFFT_OPTIONS ) ) {
241 ForesterUtil.fatalError( PRG_NAME, "no need to indicate MAFFT options without realigning" );
243 if ( cla.isOptionSet( PERFORM_PHYLOGENETIC_INFERENCE ) ) {
244 perform_phylogenetic_inference = true;
247 if ( ( out != null ) || ( removed_seqs_out_base != null ) ) {
249 .fatalError( PRG_NAME,
250 "chart only, no outfile(s) produced, thus no need to indicate output file(s)" );
252 if ( !realign && cla.isOptionSet( STEP_OPTION ) ) {
253 ForesterUtil.fatalError( PRG_NAME,
254 "chart only, no re-aligning, thus no need to use step for output and re-aligning; use -"
255 + STEP_FOR_DIAGNOSTICS_OPTION + " instead" );
258 if ( perform_phylogenetic_inference ) {
259 if ( step_for_diagnostics != 1 ) {
260 ForesterUtil.fatalError( PRG_NAME,
261 "step for diagnostics reports needs to be set to 1 for tree calculation" );
264 ForesterUtil.printProgramInformation( PRG_NAME,
270 ForesterUtil.getForesterLibraryInformation() );
271 System.out.println( "Input MSA : " + in );
272 System.out.println( " MSA length : " + msa.getLength() );
273 System.out.println( " Number of sequences : " + msa.getNumberOfSequences() );
274 System.out.println( " Median sequence length : " + NF_1.format( initial_msa_stats.median() ) );
275 System.out.println( " Mean sequence length : "
276 + NF_1.format( initial_msa_stats.arithmeticMean() ) );
277 System.out.println( " Max sequence length : " + ( ( int ) initial_msa_stats.getMax() ) );
278 System.out.println( " Min sequence length : " + ( ( int ) initial_msa_stats.getMin() ) );
280 System.out.println( "Output : " + out );
283 System.out.println( "Output : n/a" );
285 if ( removed_seqs_out_base != null ) {
286 System.out.println( "Write removed sequences to : " + removed_seqs_out_base );
288 if ( worst_remove > 0 ) {
289 System.out.println( "Number of worst offenders to remove : " + worst_remove );
292 System.out.println( "Target gap-ratio : " + av_gap );
295 System.out.println( "Target MSA length : " + length );
297 if ( min_length > 1 ) {
298 System.out.println( "Minimal effective sequence length : " + min_length );
300 if ( gap_ratio > -1 ) {
301 System.out.println( "Maximum allowed gap ratio per column : " + gap_ratio );
303 if ( ( out != null ) || ( removed_seqs_out_base != null ) ) {
304 System.out.println( "Output format : "
305 + ( output_format == MSA_FORMAT.FASTA ? "fasta" : "phylip" ) );
307 if ( chart_only && !realign ) {
308 System.out.println( "Step for output and re-aligning : n/a" );
312 System.out.println( "Step for re-aligning : " + step );
315 System.out.println( "Step for output and re-aligning : " + step );
318 System.out.println( "Step for diagnostics reports : " + step_for_diagnostics );
319 System.out.println( "Calculate normalized Shannon Entropy : " + report_entropy );
321 System.out.println( "Normalize : " + norm );
323 System.out.println( "Realign with MAFFT : " + realign );
325 System.out.println( "MAFFT options : " + mafft_options );
327 System.out.println( "Simple tree (Kimura distances, NJ) : " + perform_phylogenetic_inference );
328 System.out.println();
329 final int initial_number_of_seqs = msa.getNumberOfSequences();
330 List<MsaProperties> msa_props = null;
331 final MsaCompactor mc = new MsaCompactor( msa );
332 mc.setInfileName( in.getName() );
334 mc.setRealign( realign );
336 mc.setPathToMafft( path_to_mafft );
337 mc.setMafftOptions( mafft_options );
340 mc.setStepForDiagnostics( step_for_diagnostics );
341 mc.setCalculateNormalizedShannonEntropy( report_entropy );
342 mc.setPeformPhylogenticInference( perform_phylogenetic_inference );
343 if ( ( worst_remove > 0 ) || ( av_gap > 0 ) || ( length > 0 ) ) {
344 mc.setOutputFormat( output_format );
345 mc.setOutFileBase( out );
346 if ( removed_seqs_out_base != null ) {
347 mc.setRemovedSeqsOutBase( removed_seqs_out_base );
350 if ( min_length > 1 ) {
351 mc.removeSequencesByMinimalLength( min_length );
352 mc.writeMsa( new File( "removed" ) );
354 if ( worst_remove > 0 ) {
355 msa_props = mc.removeWorstOffenders( worst_remove );
357 else if ( av_gap > 0 ) {
358 msa_props = mc.removeViaGapAverage( av_gap );
360 else if ( length > 0 ) {
361 msa_props = mc.removeViaLength( length );
364 msa_props = mc.chart( step, realign, norm );
366 Chart.display( msa_props, initial_number_of_seqs, report_entropy, in.getName() );
368 catch ( final IllegalArgumentException iae ) {
369 // iae.printStackTrace(); //TODO remove me
370 ForesterUtil.fatalError( PRG_NAME, iae.getMessage() );
372 catch ( final IOException ioe ) {
373 // ioe.printStackTrace(); //TODO remove me
374 ForesterUtil.fatalError( PRG_NAME, ioe.getMessage() );
376 catch ( final Exception e ) {
377 ForesterUtil.unexpectedFatalError( PRG_NAME, e );
381 private static void checkPathToMafft( final String path_to_mafft ) {
382 if ( !ForesterUtil.isEmpty( path_to_mafft ) && MsaInferrer.isInstalled( path_to_mafft ) ) {
385 if ( ForesterUtil.isEmpty( path_to_mafft ) ) {
386 ForesterUtil.fatalError( PRG_NAME, "no MAFFT executable found, use -\"" + PATH_TO_MAFFT_OPTION
387 + "=<path to MAFFT>\" option" );
390 ForesterUtil.fatalError( PRG_NAME, "no MAFFT executable at \"" + path_to_mafft + "\"" );
395 private static void printHelp() {
396 ForesterUtil.printProgramInformation( PRG_NAME,
402 ForesterUtil.getForesterLibraryInformation() );
403 final String path_to_mafft = MsaCompactor.guessPathToMafft();
404 String mafft_comment;
405 if ( !ForesterUtil.isEmpty( path_to_mafft ) ) {
406 mafft_comment = " (using " + path_to_mafft + ")";
409 mafft_comment = " (no path to MAFFT found, use -\"" + PATH_TO_MAFFT_OPTION + "=<path to MAFFT>\" option";
411 System.out.println( "Usage:" );
412 System.out.println();
413 System.out.println( PRG_NAME + " <options> <msa input file> <output file base>" );
414 System.out.println();
415 System.out.println( " options: " );
416 System.out.println();
417 System.out.println( " -" + REMOVE_WORST_OFFENDERS_OPTION
418 + "=<integer> number of worst offender sequences to remove" );
419 System.out.println( " -" + LENGTH_OPTION + "=<integer> target MSA length" );
420 System.out.println( " -" + AV_GAPINESS_OPTION + "=<decimal> target gap-ratio (0.0-1.0)" );
421 System.out.println( " -" + REALIGN_OPTION + " to realign using MAFFT" + mafft_comment );
422 System.out.println( " -" + MAFFT_OPTIONS + "=<string> options for MAFFT (default: --auto)" );
423 System.out.println( " -" + STEP_OPTION + "=<integer> step for output and re-aligning (default: 1)" );
424 System.out.println( " -" + STEP_FOR_DIAGNOSTICS_OPTION
425 + "=<integer> step for diagnostics reports (default: 1)" );
426 System.out.println( " -" + REPORT_ENTROPY
427 + " to calculate normalized Shannon Entropy (not recommended for very large alignments)" );
428 System.out.println( " -" + OUTPUT_FORMAT_PHYLIP_OPTION
429 + " to write output alignments in phylip format instead of fasta" );
430 System.out.println( " -" + OUTPUT_REMOVED_SEQS_OPTION + "=<file> to output the removed sequences" );
431 System.out.println( " -" + MIN_LENGTH_OPTION
432 + "=<integer> minimal effecive sequence length (for deleting of shorter sequences)" );
433 System.out.println( " -" + GAP_RATIO_LENGTH_OPTION
434 + "=<decimal> maximal allowed gap ratio per column (for deleting of columms) (0.0-1.0)" );
435 System.out.println( " -" + PERFORM_PHYLOGENETIC_INFERENCE
436 + " to calculate a simple phylogenetic tree (Kimura distances, NJ)" );
437 System.out.println();
438 System.out.println();
439 System.out.println();