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.#" );
52 final private static NumberFormat NF_4 = new DecimalFormat( "0.####" );
54 NF_1.setRoundingMode( RoundingMode.HALF_UP );
55 NF_4.setRoundingMode( RoundingMode.HALF_UP );
57 final static private String HELP_OPTION_1 = "help";
58 final static private String HELP_OPTION_2 = "h";
59 final static private String REMOVE_WORST_OFFENDERS_OPTION = "r";
60 final static private String AV_GAPINESS_OPTION = "g";
61 final static private String STEP_OPTION = "s";
62 final static private String LENGTH_OPTION = "l";
63 final static private String REALIGN_OPTION = "a";
64 final static private String INFO_ONLY_OPTION = "i";
66 final static private String STEP_FOR_DIAGNOSTICS_OPTION = "sd";
67 final static private String MIN_LENGTH_OPTION = "ml";
68 final static private String GAP_RATIO_LENGTH_OPTION = "gr";
69 final static private String REPORT_ENTROPY = "e";
70 final static private String OUTPUT_FORMAT_OPTION = "f";
71 final static private String OUTPUT_REMOVED_SEQS_OPTION = "ro";
72 final static private String MAFFT_OPTIONS = "mo";
73 final static private String PERFORM_PHYLOGENETIC_INFERENCE = "t";
75 final static private String PATH_TO_MAFFT_OPTION = "mafft";
76 final static private String DO_NOT_NORMALIZE_FOR_EFF_LENGTH_OPTION = "nn";
77 final static private String PRG_NAME = "msa_compactor";
78 final static private String PRG_DESC = "multiple sequence aligment compactor";
79 final static private String PRG_VERSION = "0.3";
80 final static private String PRG_DATE = "140508";
81 final static private String E_MAIL = "czmasek@sanfordburham.org";
82 final static private String WWW = "https://sites.google.com/site/cmzmasek/home/software/forester";
84 public static void main( final String args[] ) {
86 final CommandLineArguments cla = new CommandLineArguments( args );
87 if ( cla.isOptionSet( HELP_OPTION_1 ) || cla.isOptionSet( HELP_OPTION_2 )
88 || ( ( cla.getNumberOfNames() < 1 ) || ( cla.getNumberOfNames() > 2 ) ) ) {
92 final File in = cla.getFile( 0 );
94 if ( cla.getNumberOfNames() > 1 ) {
95 out = cla.getFile( 1 );
97 int worst_remove = -1;
101 boolean realign = false;
103 String path_to_mafft = null;
104 int step_for_diagnostics = 1;
106 double gap_ratio = -1;
107 boolean report_entropy = false;
108 MSA_FORMAT output_format = MSA_FORMAT.FASTA;
109 File removed_seqs_out_base = null;
110 String mafft_options = "--auto";
111 boolean perform_phylogenetic_inference = false;
112 final List<String> allowed_options = new ArrayList<String>();
113 allowed_options.add( REMOVE_WORST_OFFENDERS_OPTION );
114 allowed_options.add( AV_GAPINESS_OPTION );
115 allowed_options.add( LENGTH_OPTION );
116 allowed_options.add( REALIGN_OPTION );
117 allowed_options.add( DO_NOT_NORMALIZE_FOR_EFF_LENGTH_OPTION );
118 allowed_options.add( STEP_OPTION );
119 allowed_options.add( PATH_TO_MAFFT_OPTION );
120 allowed_options.add( STEP_FOR_DIAGNOSTICS_OPTION );
121 allowed_options.add( MIN_LENGTH_OPTION );
122 allowed_options.add( GAP_RATIO_LENGTH_OPTION );
123 allowed_options.add( REPORT_ENTROPY );
124 allowed_options.add( OUTPUT_FORMAT_OPTION );
125 allowed_options.add( OUTPUT_REMOVED_SEQS_OPTION );
126 allowed_options.add( MAFFT_OPTIONS );
127 allowed_options.add( PERFORM_PHYLOGENETIC_INFERENCE );
128 allowed_options.add( INFO_ONLY_OPTION );
129 final String dissallowed_options = cla.validateAllowedOptionsAsString( allowed_options );
130 if ( dissallowed_options.length() > 0 ) {
131 ForesterUtil.fatalError( PRG_NAME, "unknown option(s): " + dissallowed_options );
133 DeleteableMsa msa = null;
134 final FileInputStream is = new FileInputStream( in );
135 if ( FastaParser.isLikelyFasta( in ) ) {
136 msa = DeleteableMsa.createInstance( FastaParser.parseMsa( is ) );
139 msa = DeleteableMsa.createInstance( GeneralMsaParser.parse( is ) );
141 final DescriptiveStatistics initial_msa_stats = MsaMethods.calculateEffectiveLengthStatistics( msa );
142 if (cla.isOptionSet( INFO_ONLY_OPTION ) ) {
143 printInfo( in, msa, initial_msa_stats );
147 final boolean chart_only = ( !cla.isOptionSet( LENGTH_OPTION ) )
148 && ( !cla.isOptionSet( REMOVE_WORST_OFFENDERS_OPTION ) )
149 && ( !cla.isOptionSet( AV_GAPINESS_OPTION ) && ( !cla.isOptionSet( MIN_LENGTH_OPTION ) ) );
150 if ( !chart_only && ( out == null ) ) {
151 ForesterUtil.fatalError( PRG_NAME, "outfile file missing" );
153 if ( cla.isOptionSet( REMOVE_WORST_OFFENDERS_OPTION ) ) {
154 worst_remove = cla.getOptionValueAsInt( REMOVE_WORST_OFFENDERS_OPTION );
155 if ( ( worst_remove < 1 ) || ( worst_remove >= ( msa.getNumberOfSequences() - 1 ) ) ) {
156 ForesterUtil.fatalError( PRG_NAME, "number of worst offender sequences to remove is out of range: "
160 if ( cla.isOptionSet( AV_GAPINESS_OPTION ) ) {
161 if ( cla.isOptionSet( REMOVE_WORST_OFFENDERS_OPTION ) ) {
165 av_gap = cla.getOptionValueAsDouble( AV_GAPINESS_OPTION );
166 if ( ( av_gap < 0 ) || ( av_gap >= 1 ) ) {
167 ForesterUtil.fatalError( PRG_NAME, "target gap-ratio is out of range: " + av_gap );
170 if ( cla.isOptionSet( LENGTH_OPTION ) ) {
171 if ( cla.isOptionSet( REMOVE_WORST_OFFENDERS_OPTION ) || cla.isOptionSet( AV_GAPINESS_OPTION ) ) {
175 length = cla.getOptionValueAsInt( LENGTH_OPTION );
176 if ( length >= msa.getLength() ) {
177 ForesterUtil.fatalError( PRG_NAME,
178 "target length is out of range [longer than MSA (" + msa.getLength()
181 else if ( length < initial_msa_stats.getMin() ) {
182 ForesterUtil.fatalError( PRG_NAME,
183 "target length is out of range [shorter than the shortest sequence ("
184 + initial_msa_stats.getMin() + ") ]: " + length );
187 if ( cla.isOptionSet( MIN_LENGTH_OPTION ) ) {
188 if ( cla.isOptionSet( LENGTH_OPTION ) || cla.isOptionSet( REMOVE_WORST_OFFENDERS_OPTION )
189 || cla.isOptionSet( AV_GAPINESS_OPTION ) || cla.isOptionSet( STEP_OPTION )
190 || cla.isOptionSet( REALIGN_OPTION ) || cla.isOptionSet( PATH_TO_MAFFT_OPTION )
191 || cla.isOptionSet( STEP_FOR_DIAGNOSTICS_OPTION ) || cla.isOptionSet( REPORT_ENTROPY )
192 || cla.isOptionSet( OUTPUT_REMOVED_SEQS_OPTION )
193 || cla.isOptionSet( PERFORM_PHYLOGENETIC_INFERENCE ) ) {
197 min_length = cla.getOptionValueAsInt( MIN_LENGTH_OPTION );
198 if ( ( min_length < 2 ) || ( min_length > initial_msa_stats.getMax() ) ) {
199 ForesterUtil.fatalError( PRG_NAME, "value for minimal sequence length is out of range: "
203 if ( cla.isOptionSet( STEP_OPTION ) ) {
204 step = cla.getOptionValueAsInt( STEP_OPTION );
206 || ( ( step > msa.getNumberOfSequences() ) || ( ( worst_remove > 0 ) && ( step > worst_remove ) ) ) ) {
207 ForesterUtil.fatalError( PRG_NAME, "value for step is out of range: " + step );
210 if ( cla.isOptionSet( REALIGN_OPTION ) ) {
213 if ( cla.isOptionSet( PATH_TO_MAFFT_OPTION ) ) {
215 ForesterUtil.fatalError( PRG_NAME, "no need to indicate path to MAFFT without realigning" );
217 path_to_mafft = cla.getOptionValueAsCleanString( PATH_TO_MAFFT_OPTION );
219 if ( cla.isOptionSet( DO_NOT_NORMALIZE_FOR_EFF_LENGTH_OPTION ) ) {
222 if ( cla.isOptionSet( STEP_FOR_DIAGNOSTICS_OPTION ) ) {
223 step_for_diagnostics = cla.getOptionValueAsInt( STEP_FOR_DIAGNOSTICS_OPTION );
224 if ( ( step_for_diagnostics < 1 )
225 || ( ( step_for_diagnostics > msa.getNumberOfSequences() ) || ( ( worst_remove > 0 ) && ( step_for_diagnostics > worst_remove ) ) ) ) {
226 ForesterUtil.fatalError( PRG_NAME, "value for diagnostic step is out of range: "
227 + step_for_diagnostics );
230 if ( cla.isOptionSet( GAP_RATIO_LENGTH_OPTION ) ) {
231 gap_ratio = cla.getOptionValueAsDouble( GAP_RATIO_LENGTH_OPTION );
232 if ( ( gap_ratio < 0 ) || ( gap_ratio > 1 ) ) {
233 ForesterUtil.fatalError( PRG_NAME, "gap ratio is out of range: " + gap_ratio );
236 if ( cla.isOptionSet( REPORT_ENTROPY ) ) {
237 report_entropy = true;
239 if ( cla.isOptionSet( OUTPUT_FORMAT_OPTION ) ) {
240 final String fs = cla.getOptionValueAsCleanString( OUTPUT_FORMAT_OPTION );
241 if ( fs.equalsIgnoreCase( "p" ) ) {
242 output_format = MSA_FORMAT.PHYLIP;
244 else if ( fs.equalsIgnoreCase( "f" ) ) {
245 output_format = MSA_FORMAT.FASTA;
247 else if ( fs.equalsIgnoreCase( "n" ) ) {
248 output_format = MSA_FORMAT.NEXUS;
251 ForesterUtil.fatalError( PRG_NAME, "illegal or empty output format option: " + fs );
254 if ( cla.isOptionSet( OUTPUT_REMOVED_SEQS_OPTION ) ) {
255 final String s = cla.getOptionValueAsCleanString( OUTPUT_REMOVED_SEQS_OPTION );
256 removed_seqs_out_base = new File( s );
259 if ( ForesterUtil.isEmpty( path_to_mafft ) ) {
260 path_to_mafft = MsaCompactor.guessPathToMafft();
262 checkPathToMafft( path_to_mafft );
263 if ( cla.isOptionSet( MAFFT_OPTIONS ) ) {
264 mafft_options = cla.getOptionValueAsCleanString( MAFFT_OPTIONS );
265 if ( ForesterUtil.isEmpty( mafft_options ) || ( mafft_options.length() < 3 ) ) {
266 ForesterUtil.fatalError( PRG_NAME, "illegal or empty MAFFT options: " + mafft_options );
270 else if ( cla.isOptionSet( MAFFT_OPTIONS ) ) {
271 ForesterUtil.fatalError( PRG_NAME, "no need to indicate MAFFT options without realigning" );
273 if ( cla.isOptionSet( PERFORM_PHYLOGENETIC_INFERENCE ) ) {
274 perform_phylogenetic_inference = true;
277 if ( ( out != null ) || ( removed_seqs_out_base != null ) ) {
279 .fatalError( PRG_NAME,
280 "chart only, no outfile(s) produced, thus no need to indicate output file(s)" );
282 if ( !realign && cla.isOptionSet( STEP_OPTION ) ) {
283 ForesterUtil.fatalError( PRG_NAME,
284 "chart only, no re-aligning, thus no need to use step for output and re-aligning; use -"
285 + STEP_FOR_DIAGNOSTICS_OPTION + " instead" );
288 if ( perform_phylogenetic_inference ) {
289 if ( step_for_diagnostics != 1 ) {
290 ForesterUtil.fatalError( PRG_NAME,
291 "step for diagnostics reports needs to be set to 1 for tree calculation" );
294 printInfo( in, msa, initial_msa_stats );
296 System.out.println( "Output : " + out );
299 if ( removed_seqs_out_base != null ) {
300 System.out.println( "Write removed sequences to : " + removed_seqs_out_base );
302 if ( worst_remove > 0 ) {
303 System.out.println( "Number of worst offenders to remove : " + worst_remove );
306 System.out.println( "Target gap-ratio : " + av_gap );
309 System.out.println( "Target MSA length : " + length );
311 if ( min_length > 1 ) {
312 System.out.println( "Minimal effective sequence length : " + min_length );
314 if ( gap_ratio > -1 ) {
315 System.out.println( "Maximum allowed gap ratio per column : " + gap_ratio );
317 if ( ( out != null ) || ( removed_seqs_out_base != null ) ) {
318 System.out.print( "Output format : " );
319 if ( output_format == MSA_FORMAT.FASTA ) {
320 System.out.println( "fasta" );
322 else if ( output_format == MSA_FORMAT.PHYLIP ) {
323 System.out.println( "phylip" );
325 else if ( output_format == MSA_FORMAT.NEXUS ) {
326 System.out.println( "nexus" );
329 if ( min_length == -1 ) {
330 if ( chart_only && !realign ) {
331 System.out.println( "Step for output and re-aligning : n/a" );
335 System.out.println( "Step for re-aligning : " + step );
338 System.out.println( "Step for output and re-aligning : " + step );
341 System.out.println( "Step for diagnostics reports : " + step_for_diagnostics );
342 System.out.println( "Calculate normalized Shannon Entropy : " + report_entropy );
344 System.out.println( "Normalize : " + norm );
346 System.out.println( "Realign with MAFFT : " + realign );
348 System.out.println( "MAFFT options : " + mafft_options );
350 System.out.println( "Simple tree (Kimura distances, NJ) : " + perform_phylogenetic_inference );
352 System.out.println();
353 final int initial_number_of_seqs = msa.getNumberOfSequences();
354 List<MsaProperties> msa_props = null;
355 final MsaCompactor mc = new MsaCompactor( msa );
356 mc.setInfileName( in.getName() );
357 if ( ( worst_remove > 0 ) || ( av_gap > 0 ) || ( length > 0 ) || ( min_length != -1 ) ) {
358 mc.setOutputFormat( output_format );
359 mc.setOutFileBase( out );
361 if ( min_length != -1 ) {
362 mc.removeSequencesByMinimalLength( min_length );
365 mc.setPeformPhylogenticInference( perform_phylogenetic_inference );
366 if ( removed_seqs_out_base != null ) {
367 mc.setRemovedSeqsOutBase( removed_seqs_out_base );
370 mc.setRealign( realign );
372 mc.setPathToMafft( path_to_mafft );
373 mc.setMafftOptions( mafft_options );
376 mc.setStepForDiagnostics( step_for_diagnostics );
377 mc.setCalculateNormalizedShannonEntropy( report_entropy );
378 if ( worst_remove > 0 ) {
379 msa_props = mc.removeWorstOffenders( worst_remove );
381 else if ( av_gap > 0 ) {
382 msa_props = mc.removeViaGapAverage( av_gap );
384 else if ( length > 0 ) {
385 msa_props = mc.removeViaLength( length );
388 msa_props = mc.chart( step, realign, norm );
390 Chart.display( msa_props, initial_number_of_seqs, report_entropy, in.getName() );
393 catch ( final IllegalArgumentException iae ) {
394 // iae.printStackTrace(); //TODO remove me
395 ForesterUtil.fatalError( PRG_NAME, iae.getMessage() );
397 catch ( final IOException ioe ) {
398 // ioe.printStackTrace(); //TODO remove me
399 ForesterUtil.fatalError( PRG_NAME, ioe.getMessage() );
401 catch ( final Exception e ) {
402 ForesterUtil.unexpectedFatalError( PRG_NAME, e );
406 private static void printInfo( final File in, DeleteableMsa msa, final DescriptiveStatistics initial_msa_stats ) {
407 ForesterUtil.printProgramInformation( PRG_NAME,
413 ForesterUtil.getForesterLibraryInformation() );
414 System.out.println( "Input MSA : " + in );
415 System.out.println( " MSA length : " + msa.getLength() );
416 System.out.println( " Number of sequences : " + msa.getNumberOfSequences() );
417 System.out.println( " Median sequence length : " + NF_1.format( initial_msa_stats.median() ) );
418 System.out.println( " Mean sequence length : "
419 + NF_1.format( initial_msa_stats.arithmeticMean() ) );
420 System.out.println( " Max sequence length : " + ( ( int ) initial_msa_stats.getMax() ) );
421 System.out.println( " Min sequence length : " + ( ( int ) initial_msa_stats.getMin() ) );
422 System.out.println( " Gap ratio : "
423 + NF_4.format( MsaMethods.calcGapRatio( msa ) ) );
424 System.out.println( " Normalized Shannon Entropy (entn21): "
425 + NF_4.format( MsaMethods.calcNormalizedShannonsEntropy( 21, msa ) ) );
428 private static void checkPathToMafft( final String path_to_mafft ) {
429 if ( !ForesterUtil.isEmpty( path_to_mafft ) && MsaInferrer.isInstalled( path_to_mafft ) ) {
432 if ( ForesterUtil.isEmpty( path_to_mafft ) ) {
433 ForesterUtil.fatalError( PRG_NAME, "no MAFFT executable found, use -\"" + PATH_TO_MAFFT_OPTION
434 + "=<path to MAFFT>\" option" );
437 ForesterUtil.fatalError( PRG_NAME, "no MAFFT executable at \"" + path_to_mafft + "\"" );
442 private static void printHelp() {
443 ForesterUtil.printProgramInformation( PRG_NAME,
449 ForesterUtil.getForesterLibraryInformation() );
450 final String path_to_mafft = MsaCompactor.guessPathToMafft();
451 String mafft_comment;
452 if ( !ForesterUtil.isEmpty( path_to_mafft ) ) {
453 mafft_comment = " (using " + path_to_mafft + ")";
456 mafft_comment = " (no path to MAFFT found, use -\"" + PATH_TO_MAFFT_OPTION + "=<path to MAFFT>\" option";
458 System.out.println( "Usage:" );
459 System.out.println();
460 System.out.println( PRG_NAME + " [options] <msa input file> [output file base]" );
461 System.out.println();
462 System.out.println( " options: " );
463 System.out.println();
464 System.out.println( " -" + INFO_ONLY_OPTION
465 + " to only display same basic information about the MSA" );
466 System.out.println( " -" + REMOVE_WORST_OFFENDERS_OPTION
467 + "=<integer> number of worst offender sequences to remove" );
468 System.out.println( " -" + LENGTH_OPTION + "=<integer> target MSA length" );
469 System.out.println( " -" + AV_GAPINESS_OPTION + "=<decimal> target gap-ratio (0.0-1.0)" );
470 System.out.println( " -" + REALIGN_OPTION + " to realign using MAFFT" + mafft_comment );
471 System.out.println( " -" + MAFFT_OPTIONS + "=<string> options for MAFFT (default: --auto)" );
472 System.out.println( " -" + STEP_OPTION + "=<integer> step for output and re-aligning (default: 1)" );
473 System.out.println( " -" + STEP_FOR_DIAGNOSTICS_OPTION
474 + "=<integer> step for diagnostics reports (default: 1)" );
475 System.out.println( " -" + REPORT_ENTROPY
476 + " to calculate normalized Shannon Entropy (not recommended for very large alignments)" );
477 System.out.println( " -" + OUTPUT_FORMAT_OPTION
478 + "=<f|p|n> format for output alignments: f for fasta (default), p for phylip, or n for nexus" );
479 System.out.println( " -" + OUTPUT_REMOVED_SEQS_OPTION + "=<file> to output the removed sequences" );
480 System.out.println( " -" + MIN_LENGTH_OPTION
481 + "=<integer> minimal effecive sequence length (for deleting of shorter sequences)" );
482 System.out.println( " -" + GAP_RATIO_LENGTH_OPTION
483 + "=<decimal> maximal allowed gap ratio per column (for deleting of columms) (0.0-1.0)" );
484 System.out.println( " -" + PERFORM_PHYLOGENETIC_INFERENCE
485 + " to calculate a simple phylogenetic tree (Kimura distances, NJ)" );
486 System.out.println();
487 System.out.println();
488 System.out.println();