b5dc020b0e5be9ce3c51331117d0dfd4072262a6
[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.MsaCompactor;
39 import org.forester.util.CommandLineArguments;
40 import org.forester.util.DescriptiveStatistics;
41 import org.forester.util.ForesterUtil;
42
43 public class msa_compactor {
44
45     final static private String HELP_OPTION_1                          = "help";
46     final static private String HELP_OPTION_2                          = "h";
47     final static private String REMOVE_WORST_OFFENDERS_OPTION          = "r";
48     final static private String AV_GAPINESS_OPTION                     = "g";
49     final static private String STEP_OPTION                            = "s";
50     final static private String LENGTH_OPTION                          = "l";
51     final static private String REALIGN_OPTION                         = "a";
52     //
53     final static private String STEP_FOR_DIAGNOSTICS_OPTION            = "sd";
54     final static private String MIN_LENGTH_OPTION                      = "ml";
55     final static private String GAP_RATIO_LENGTH_OPTION                = "gr";
56     final static private String REPORT_ALN_MEAN_IDENTITY               = "q";
57     final static private String OUTPUT_FORMAT_PHYLIP_OPTION            = "f";
58     final static private String OUTPUT_REMOVED_SEQS_OPTION             = "ro";
59     //
60     final static private String PATH_TO_MAFFT_OPTION                   = "mafft";
61     final static private String DO_NOT_NORMALIZE_FOR_EFF_LENGTH_OPTION = "nn";
62     final static private String PRG_NAME                               = "msa_compactor";
63     final static private String PRG_DESC                               = "multiple sequence aligment compactor";
64     final static private String PRG_VERSION                            = "0.01";
65     final static private String PRG_DATE                               = "140316";
66     final static private String E_MAIL                                 = "phylosoft@gmail.com";
67     final static private String WWW                                    = "https://sites.google.com/site/cmzmasek/home/software/forester";
68
69     public static void main( final String args[] ) {
70         try {
71             final CommandLineArguments cla = new CommandLineArguments( args );
72             if ( cla.isOptionSet( HELP_OPTION_1 ) || cla.isOptionSet( HELP_OPTION_2 )
73                     || ( ( cla.getNumberOfNames() < 1 ) || ( cla.getNumberOfNames() > 2 ) ) ) {
74                 printHelp();
75                 System.exit( 0 );
76             }
77             final File in = cla.getFile( 0 );
78             File out = null;
79             if ( cla.getNumberOfNames() > 1 ) {
80                 out = cla.getFile( 1 );
81             }
82             int worst_remove = -1;
83             double av_gap = -1;
84             int length = -1;
85             int step = -1;
86             boolean realign = false;
87             boolean norm = true;
88             String path_to_mafft = null;
89             //            final static private String STEP_FOR_DIAGNOSTICS_OPTION            = "sd";
90             //            final static private String MIN_LENGTH_OPTION                      = "ml";
91             //            final static private String GAP_RATIO_LENGTH_OPTION                = "gr";
92             //            final static private String REPORT_ALN_MEAN_IDENTITY               = "q";
93             //            final static private String OUTPUT_FORMAT_PHYLIP_OPTION            = "f";
94             //            final static private String OUTPUT_REMOVED_SEQS_OPTION             = "ro";
95             int step_for_diagnostics = -1;
96             int min_length = -1;
97             double gap_ratio = -1;
98             boolean report_aln_mean_identity = false;
99             MSA_FORMAT output_format = MSA_FORMAT.FASTA;
100             final File roved_seqs_out_base = null;
101             final List<String> allowed_options = new ArrayList<String>();
102             allowed_options.add( REMOVE_WORST_OFFENDERS_OPTION );
103             allowed_options.add( AV_GAPINESS_OPTION );
104             allowed_options.add( LENGTH_OPTION );
105             allowed_options.add( REALIGN_OPTION );
106             allowed_options.add( DO_NOT_NORMALIZE_FOR_EFF_LENGTH_OPTION );
107             allowed_options.add( STEP_OPTION );
108             allowed_options.add( PATH_TO_MAFFT_OPTION );
109             allowed_options.add( STEP_FOR_DIAGNOSTICS_OPTION );
110             allowed_options.add( MIN_LENGTH_OPTION );
111             allowed_options.add( GAP_RATIO_LENGTH_OPTION );
112             allowed_options.add( REPORT_ALN_MEAN_IDENTITY );
113             allowed_options.add( OUTPUT_FORMAT_PHYLIP_OPTION );
114             allowed_options.add( OUTPUT_REMOVED_SEQS_OPTION );
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 );
118             }
119             DeleteableMsa msa = null;
120             final FileInputStream is = new FileInputStream( in );
121             if ( FastaParser.isLikelyFasta( in ) ) {
122                 msa = DeleteableMsa.createInstance( FastaParser.parseMsa( is ) );
123             }
124             else {
125                 msa = DeleteableMsa.createInstance( GeneralMsaParser.parse( is ) );
126             }
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: "
133                             + worst_remove );
134                 }
135             }
136             if ( cla.isOptionSet( AV_GAPINESS_OPTION ) ) {
137                 if ( cla.isOptionSet( REMOVE_WORST_OFFENDERS_OPTION ) ) {
138                     printHelp();
139                     System.exit( 0 );
140                 }
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 );
144                 }
145             }
146             if ( cla.isOptionSet( LENGTH_OPTION ) ) {
147                 if ( cla.isOptionSet( REMOVE_WORST_OFFENDERS_OPTION ) || cla.isOptionSet( AV_GAPINESS_OPTION ) ) {
148                     printHelp();
149                     System.exit( 0 );
150                 }
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 );
154                 }
155             }
156             if ( cla.isOptionSet( STEP_OPTION ) ) {
157                 step = cla.getOptionValueAsInt( STEP_OPTION );
158                 if ( ( step < 1 )
159                         || ( ( step > msa.getNumberOfSequences() ) || ( ( worst_remove > 0 ) && ( step > worst_remove ) ) ) ) {
160                     ForesterUtil.fatalError( PRG_NAME, "value for step is out of range: " + step );
161                 }
162             }
163             if ( cla.isOptionSet( REALIGN_OPTION ) ) {
164                 realign = true;
165             }
166             if ( cla.isOptionSet( PATH_TO_MAFFT_OPTION ) ) {
167                 if ( !realign ) {
168                     ForesterUtil.fatalError( PRG_NAME, "no need to indicate path to MAFFT without realigning" );
169                 }
170                 path_to_mafft = cla.getOptionValueAsCleanString( PATH_TO_MAFFT_OPTION );
171             }
172             if ( cla.isOptionSet( DO_NOT_NORMALIZE_FOR_EFF_LENGTH_OPTION ) ) {
173                 norm = false;
174             }
175             //
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 );
182                 }
183             }
184             if ( cla.isOptionSet( MIN_LENGTH_OPTION ) ) {
185                 min_length = cla.getOptionValueAsInt( MIN_LENGTH_OPTION );
186                 if ( ( min_length < 1 ) || ( min_length > initial_msa_stats.getMax() ) ) {
187                     ForesterUtil.fatalError( PRG_NAME, "value for minimal sequence length is out of range: "
188                             + min_length );
189                 }
190             }
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 );
195                 }
196             }
197             if ( cla.isOptionSet( REPORT_ALN_MEAN_IDENTITY ) ) {
198                 report_aln_mean_identity = true;
199             }
200             if ( cla.isOptionSet( OUTPUT_FORMAT_PHYLIP_OPTION ) ) {
201                 output_format = MSA_FORMAT.PHYLIP;
202             }
203             //            if ( cla.isOptionSet( OUTPUT_REMOVED_SEQS_OPTION ) ) {
204             //                gap_ratio = cla.getOptionValueAsCleanString( OUTPUT_REMOVED_SEQS_OPTION );
205             //                if ( ( gap_ratio < 0 ) || ( gap_ratio > 1 ) ) {
206             //                    ForesterUtil.fatalError( PRG_NAME, "gap ratio is out of range: " + gap_ratio );
207             //                }
208             //            }
209             //
210             if ( realign ) {
211                 if ( ForesterUtil.isEmpty( path_to_mafft ) ) {
212                     path_to_mafft = MsaCompactor.guessPathToMafft();
213                 }
214                 checkPathToMafft( path_to_mafft );
215             }
216             if ( worst_remove > 0 ) {
217                 MsaCompactor.removeWorstOffenders( msa, worst_remove, step, realign, norm, path_to_mafft, out );
218             }
219             else if ( av_gap > 0 ) {
220                 MsaCompactor.reduceGapAverage( msa, av_gap, step, realign, norm, path_to_mafft, out );
221             }
222             else if ( length > 0 ) {
223                 // TODO if < shortest seq -> error
224                 MsaCompactor.reduceLength( msa, length, step, realign, norm, path_to_mafft, out );
225             }
226             else {
227                 MsaCompactor.chart( msa, step, realign, norm, path_to_mafft );
228             }
229         }
230         catch ( final Exception e ) {
231             e.printStackTrace();
232             ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
233         }
234     }
235
236     private static void checkPathToMafft( final String path_to_mafft ) {
237         if ( !ForesterUtil.isEmpty( path_to_mafft ) && MsaInferrer.isInstalled( path_to_mafft ) ) {
238             ForesterUtil.programMessage( PRG_NAME, "using MAFFT at \"" + path_to_mafft + "\"" );
239         }
240         else {
241             if ( ForesterUtil.isEmpty( path_to_mafft ) ) {
242                 ForesterUtil.fatalError( PRG_NAME, "no MAFFT executable found, use -\"" + PATH_TO_MAFFT_OPTION
243                         + "=<path to MAFFT>\" option" );
244             }
245             else {
246                 ForesterUtil.fatalError( PRG_NAME, "no MAFFT executable at \"" + path_to_mafft + "\"" );
247             }
248         }
249     }
250
251     private static void printHelp() {
252         ForesterUtil.printProgramInformation( PRG_NAME,
253                                               PRG_DESC,
254                                               PRG_VERSION,
255                                               PRG_DATE,
256                                               E_MAIL,
257                                               WWW,
258                                               ForesterUtil.getForesterLibraryInformation() );
259         final String path_to_mafft = MsaCompactor.guessPathToMafft();
260         String mafft_comment;
261         if ( !ForesterUtil.isEmpty( path_to_mafft ) ) {
262             mafft_comment = " (using " + path_to_mafft + ")";
263         }
264         else {
265             mafft_comment = " (no path to MAFFT found, use -\"" + PATH_TO_MAFFT_OPTION + "=<path to MAFFT>\" option";
266         }
267         System.out.println( "Usage:" );
268         System.out.println();
269         System.out.println( PRG_NAME + " <options> <msa input file> <output file>" );
270         System.out.println();
271         System.out.println( " options: " );
272         System.out.println();
273         System.out.println( "   -" + REMOVE_WORST_OFFENDERS_OPTION
274                 + "=<integer>   number of worst offender sequences to remove" );
275         System.out.println( "   -" + LENGTH_OPTION + "=<integer>   target MSA length" );
276         System.out.println( "   -" + AV_GAPINESS_OPTION + "=<decimal>   target gap-ratio (0.0-1.0)" );
277         System.out.println( "   -" + STEP_OPTION + "=<integer>   step for output and re-aligning (default: 1)" );
278         System.out.println( "   -" + REALIGN_OPTION + "             to realign using MAFFT" + mafft_comment );
279         System.out.println( "   -" + STEP_FOR_DIAGNOSTICS_OPTION
280                 + "=<integer>  step for diagnostics reports (default: 1)" );
281         System.out.println( "   -" + MIN_LENGTH_OPTION
282                 + "=<integer>  minimal effecive sequence length (for deleting of shorter sequences)" );
283         System.out.println( "   -" + GAP_RATIO_LENGTH_OPTION
284                 + "=<decimal>  maximal allowed gap ratio per column (for deleting of columms) (0.0-1.0)" );
285         System.out.println( "   -" + REPORT_ALN_MEAN_IDENTITY
286                 + "             to report mean identity diagnostic (not recommended for very large alignments)" );
287         System.out.println( "   -" + OUTPUT_FORMAT_PHYLIP_OPTION
288                 + "             to write output alignments in phylip format instead of fasta" );
289         System.out.println( "   -" + OUTPUT_REMOVED_SEQS_OPTION + "=<file>     to output the removed sequences" );
290         System.out.println();
291         System.out.println();
292         System.out.println();
293     }
294 }