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.MsaInferrer;
36 import org.forester.msa_compactor.MsaCompactor;
37 import org.forester.util.CommandLineArguments;
38 import org.forester.util.ForesterUtil;
40 public class msa_compactor {
42 final static private String HELP_OPTION_1 = "help";
43 final static private String HELP_OPTION_2 = "h";
44 final static private String REMOVE_WORST_OFFENDERS_OPTION = "r";
45 final static private String AV_GAPINESS_OPTION = "g";
46 final static private String STEP_OPTION = "s";
47 final static private String LENGTH_OPTION = "l";
48 final static private String REALIGN_OPTION = "a";
50 final static private String STEP_FOR_DIAGNOSTICS_OPTION = "sd";
51 final static private String MIN_LENGTH_OPTION = "ml";
52 final static private String GAP_RATIO_LENGTH_OPTION = "gr";
53 final static private String REPORT_ALN_MEAN_IDENTITY = "q";
54 final static private String OUTPUT_FORMAT_PHYLIP_OPTION = "f";
55 final static private String OUTPUT_REMOVED_SEQS_OPTION = "ro";
57 final static private String PATH_TO_MAFFT_OPTION = "mafft";
58 final static private String DO_NOT_NORMALIZE_FOR_EFF_LENGTH_OPTION = "nn";
59 final static private String PRG_NAME = "msa_compactor";
60 final static private String PRG_DESC = "multiple sequence aligment compactor";
61 final static private String PRG_VERSION = "0.01";
62 final static private String PRG_DATE = "140316";
63 final static private String E_MAIL = "phylosoft@gmail.com";
64 final static private String WWW = "https://sites.google.com/site/cmzmasek/home/software/forester";
66 public static void main( final String args[] ) {
68 final CommandLineArguments cla = new CommandLineArguments( args );
69 if ( cla.isOptionSet( HELP_OPTION_1 ) || cla.isOptionSet( HELP_OPTION_2 )
70 || ( ( cla.getNumberOfNames() < 1 ) || ( cla.getNumberOfNames() > 2 ) ) ) {
74 final File in = cla.getFile( 0 );
76 if ( cla.getNumberOfNames() > 1 ) {
77 out = cla.getFile( 1 );
79 int worst_remove = -1;
83 boolean realign = false;
85 String path_to_mafft = null;
86 // final static private String STEP_FOR_DIAGNOSTICS_OPTION = "sd";
87 // final static private String MIN_LENGTH_OPTION = "ml";
88 // final static private String GAP_RATIO_LENGTH_OPTION = "gr";
89 // final static private String REPORT_ALN_MEAN_IDENTITY = "q";
90 // final static private String OUTPUT_FORMAT_PHYLIP_OPTION = "f";
91 // final static private String OUTPUT_REMOVED_SEQS_OPTION = "ro";
92 int step_for_diagnostics = -1;
94 double gap_ratio = -1;
95 boolean report_aln_mean_identity = false;
96 Format output_format = FASTA;
97 final File roved_seqs_out_base = null;
99 if ( cla.isOptionSet( STEP_FOR_DIAGNOSTICS_OPTION ) ) {
100 step_for_diagnostics = cla.getOptionValueAsInt( STEP_FOR_DIAGNOSTICS_OPTION );
101 if ( ( step_for_diagnostics < 1 )
102 || ( ( step_for_diagnostics > msa.getNumberOfSequences() ) || ( ( worst_remove > 0 ) && ( step_for_diagnostics > worst_remove ) ) ) ) {
103 ForesterUtil.fatalError( PRG_NAME, "value for diagnostic step is out of range: "
104 + step_for_diagnostics );
107 if ( cla.isOptionSet( MIN_LENGTH_OPTION ) ) {
108 min_length = cla.getOptionValueAsInt( MIN_LENGTH_OPTION );
109 if ( ( min_length < 1 ) || ( min_length > longest_msa_seq ) ) {
110 ForesterUtil.fatalError( PRG_NAME, "value for minimal sequence length is out of range: "
114 if ( cla.isOptionSet( MIN_LENGTH_OPTION ) ) {
115 gap_ratio = cla.getOptionValueAsDouble( GAP_RATIO_LENGTH_OPTION );
116 if ( ( gap_ratio < 0 ) || ( gap_ratio > 1 ) ) {
117 ForesterUtil.fatalError( PRG_NAME, "gap ratio is out of range: " + gap_ratio );
120 if ( cla.isOptionSet( REPORT_ALN_MEAN_IDENTITY ) ) {
121 report_aln_mean_identity = true;
123 if ( cla.isOptionSet( OUTPUT_FORMAT_PHYLIP_OPTION ) ) {
124 output_format = PHYLIP;
126 if ( cla.isOptionSet( OUTPUT_REMOVED_SEQS_OPTION ) ) {
127 gap_ratio = cla.getOptionValueAsCleanString( OUTPUT_REMOVED_SEQS_OPTION );
128 if ( ( gap_ratio < 0 ) || ( gap_ratio > 1 ) ) {
129 ForesterUtil.fatalError( PRG_NAME, "gap ratio is out of range: " + gap_ratio );
133 final List<String> allowed_options = new ArrayList<String>();
134 allowed_options.add( REMOVE_WORST_OFFENDERS_OPTION );
135 allowed_options.add( AV_GAPINESS_OPTION );
136 allowed_options.add( LENGTH_OPTION );
137 allowed_options.add( REALIGN_OPTION );
138 allowed_options.add( DO_NOT_NORMALIZE_FOR_EFF_LENGTH_OPTION );
139 allowed_options.add( STEP_OPTION );
140 allowed_options.add( PATH_TO_MAFFT_OPTION );
141 allowed_options.add( STEP_FOR_DIAGNOSTICS_OPTION );
142 allowed_options.add( MIN_LENGTH_OPTION );
143 allowed_options.add( GAP_RATIO_LENGTH_OPTION );
144 allowed_options.add( REPORT_ALN_MEAN_IDENTITY );
145 allowed_options.add( OUTPUT_FORMAT_PHYLIP_OPTION );
146 allowed_options.add( OUTPUT_REMOVED_SEQS_OPTION );
147 final String dissallowed_options = cla.validateAllowedOptionsAsString( allowed_options );
148 if ( dissallowed_options.length() > 0 ) {
149 ForesterUtil.fatalError( PRG_NAME, "unknown option(s): " + dissallowed_options );
151 DeleteableMsa msa = null;
152 final FileInputStream is = new FileInputStream( in );
153 if ( FastaParser.isLikelyFasta( in ) ) {
154 msa = DeleteableMsa.createInstance( FastaParser.parseMsa( is ) );
157 msa = DeleteableMsa.createInstance( GeneralMsaParser.parse( is ) );
159 if ( cla.isOptionSet( REMOVE_WORST_OFFENDERS_OPTION ) ) {
160 worst_remove = cla.getOptionValueAsInt( REMOVE_WORST_OFFENDERS_OPTION );
161 if ( ( worst_remove < 1 ) || ( worst_remove >= msa.getNumberOfSequences() - 1 ) ) {
162 ForesterUtil.fatalError( PRG_NAME, "number of worst offender sequences to remove is out of range: "
166 if ( cla.isOptionSet( AV_GAPINESS_OPTION ) ) {
167 if ( cla.isOptionSet( REMOVE_WORST_OFFENDERS_OPTION ) ) {
171 av_gap = cla.getOptionValueAsDouble( AV_GAPINESS_OPTION );
172 if ( ( av_gap < 0 ) || ( av_gap >= 1 ) ) {
173 ForesterUtil.fatalError( PRG_NAME, "target gap-ratio is out of range: " + av_gap );
176 if ( cla.isOptionSet( LENGTH_OPTION ) ) {
177 if ( cla.isOptionSet( REMOVE_WORST_OFFENDERS_OPTION ) || cla.isOptionSet( AV_GAPINESS_OPTION ) ) {
181 length = cla.getOptionValueAsInt( LENGTH_OPTION );
182 if ( ( length < 2 ) || ( length >= msa.getLength() ) ) {
183 ForesterUtil.fatalError( PRG_NAME, "target length is out of range: " + length );
186 if ( cla.isOptionSet( STEP_OPTION ) ) {
187 step = cla.getOptionValueAsInt( STEP_OPTION );
189 || ( ( step > msa.getNumberOfSequences() ) || ( ( worst_remove > 0 ) && ( step > worst_remove ) ) ) ) {
190 ForesterUtil.fatalError( PRG_NAME, "value for step is out of range: " + step );
193 if ( cla.isOptionSet( REALIGN_OPTION ) ) {
196 if ( cla.isOptionSet( PATH_TO_MAFFT_OPTION ) ) {
198 ForesterUtil.fatalError( PRG_NAME, "no need to indicate path to MAFFT without realigning" );
200 path_to_mafft = cla.getOptionValueAsCleanString( PATH_TO_MAFFT_OPTION );
202 if ( cla.isOptionSet( DO_NOT_NORMALIZE_FOR_EFF_LENGTH_OPTION ) ) {
206 if ( ForesterUtil.isEmpty( path_to_mafft ) ) {
207 path_to_mafft = MsaCompactor.guessPathToMafft();
209 checkPathToMafft( path_to_mafft );
211 if ( worst_remove > 0 ) {
212 MsaCompactor.removeWorstOffenders( msa, worst_remove, step, realign, norm, path_to_mafft, out );
214 else if ( av_gap > 0 ) {
215 MsaCompactor.reduceGapAverage( msa, av_gap, step, realign, norm, path_to_mafft, out );
217 else if ( length > 0 ) {
218 // TODO if < shortest seq -> error
219 MsaCompactor.reduceLength( msa, length, step, realign, norm, path_to_mafft, out );
222 MsaCompactor.chart( msa, step, realign, norm, path_to_mafft );
225 catch ( final Exception e ) {
227 ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
231 private static void checkPathToMafft( final String path_to_mafft ) {
232 if ( !ForesterUtil.isEmpty( path_to_mafft ) && MsaInferrer.isInstalled( path_to_mafft ) ) {
233 ForesterUtil.programMessage( PRG_NAME, "using MAFFT at \"" + path_to_mafft + "\"" );
236 if ( ForesterUtil.isEmpty( path_to_mafft ) ) {
237 ForesterUtil.fatalError( PRG_NAME, "no MAFFT executable found, use -\"" + PATH_TO_MAFFT_OPTION
238 + "=<path to MAFFT>\" option" );
241 ForesterUtil.fatalError( PRG_NAME, "no MAFFT executable at \"" + path_to_mafft + "\"" );
246 private static void printHelp() {
247 ForesterUtil.printProgramInformation( PRG_NAME,
253 ForesterUtil.getForesterLibraryInformation() );
254 final String path_to_mafft = MsaCompactor.guessPathToMafft();
255 String mafft_comment;
256 if ( !ForesterUtil.isEmpty( path_to_mafft ) ) {
257 mafft_comment = " (using " + path_to_mafft + ")";
260 mafft_comment = " (no path to MAFFT found, use -\"" + PATH_TO_MAFFT_OPTION + "=<path to MAFFT>\" option";
262 System.out.println( "Usage:" );
263 System.out.println();
264 System.out.println( PRG_NAME + " <options> <msa input file> <output file>" );
265 System.out.println();
266 System.out.println( " options: " );
267 System.out.println();
268 System.out.println( " -" + REMOVE_WORST_OFFENDERS_OPTION
269 + "=<integer> number of worst offender sequences to remove" );
270 System.out.println( " -" + LENGTH_OPTION + "=<integer> target MSA length" );
271 System.out.println( " -" + AV_GAPINESS_OPTION + "=<decimal> target gap-ratio (0.0-1.0)" );
272 System.out.println( " -" + STEP_OPTION + "=<integer> step for output and re-aligning (default: 1)" );
273 System.out.println( " -" + REALIGN_OPTION + " to realign using MAFFT" + mafft_comment );
274 System.out.println( " -" + STEP_FOR_DIAGNOSTICS_OPTION
275 + "=<integer> step for diagnostics reports (default: 1)" );
276 System.out.println( " -" + MIN_LENGTH_OPTION
277 + "=<integer> minimal effecive sequence length (for deleting of shorter sequences)" );
278 System.out.println( " -" + GAP_RATIO_LENGTH_OPTION
279 + "=<decimal> maximal allowed gap ratio per column (for deleting of columms) (0.0-1.0)" );
280 System.out.println( " -" + REPORT_ALN_MEAN_IDENTITY
281 + " to report mean identity diagnostic (not recommended for very large alignments)" );
282 System.out.println( " -" + OUTPUT_FORMAT_PHYLIP_OPTION
283 + " to write output alignments in phylip format instead of fasta" );
284 System.out.println( " -" + OUTPUT_REMOVED_SEQS_OPTION + "=<file> to output the removed sequences" );
285 System.out.println();
286 System.out.println();
287 System.out.println();