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.Msa;
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";
49 final static private String PATH_TO_MAFFT_OPTION = "mafft";
50 final static private String DO_NOT_NORMALIZE_FOR_EFF_LENGTH_OPTION = "nn";
51 final static private String PRG_NAME = "msa_compactor";
52 final static private String PRG_DESC = "multiple sequence aligment compactor";
53 final static private String PRG_VERSION = "0.01";
54 final static private String PRG_DATE = "140316";
55 final static private String E_MAIL = "phylosoft@gmail.com";
56 final static private String WWW = "https://sites.google.com/site/cmzmasek/home/software/forester";
58 public static void main( final String args[] ) {
60 final CommandLineArguments cla = new CommandLineArguments( args );
61 if ( cla.isOptionSet( HELP_OPTION_1 ) || cla.isOptionSet( HELP_OPTION_2 )
62 || ( ( cla.getNumberOfNames() < 1 ) || ( cla.getNumberOfNames() > 2 ) ) ) {
66 final File in = cla.getFile( 0 );
68 if ( cla.getNumberOfNames() > 1 ) {
69 out = cla.getFile( 1 );
71 int worst_remove = -1;
75 boolean realign = false;
77 String path_to_mafft = null;
78 final List<String> allowed_options = new ArrayList<String>();
79 allowed_options.add( REMOVE_WORST_OFFENDERS_OPTION );
80 allowed_options.add( AV_GAPINESS_OPTION );
81 allowed_options.add( LENGTH_OPTION );
82 allowed_options.add( REALIGN_OPTION );
83 allowed_options.add( DO_NOT_NORMALIZE_FOR_EFF_LENGTH_OPTION );
84 allowed_options.add( STEP_OPTION );
85 allowed_options.add( PATH_TO_MAFFT_OPTION );
86 final String dissallowed_options = cla.validateAllowedOptionsAsString( allowed_options );
87 if ( dissallowed_options.length() > 0 ) {
88 ForesterUtil.fatalError( PRG_NAME, "unknown option(s): " + dissallowed_options );
91 final FileInputStream is = new FileInputStream( in );
92 if ( FastaParser.isLikelyFasta( in ) ) {
93 msa = FastaParser.parseMsa( is );
96 msa = GeneralMsaParser.parse( is );
98 if ( cla.isOptionSet( REMOVE_WORST_OFFENDERS_OPTION ) ) {
99 worst_remove = cla.getOptionValueAsInt( REMOVE_WORST_OFFENDERS_OPTION );
100 if ( ( worst_remove < 1 ) || ( worst_remove >= msa.getNumberOfSequences() - 1 ) ) {
101 ForesterUtil.fatalError( PRG_NAME, "number of worst offender sequences to remove is out of range: "
105 if ( cla.isOptionSet( AV_GAPINESS_OPTION ) ) {
106 if ( cla.isOptionSet( REMOVE_WORST_OFFENDERS_OPTION ) ) {
110 av_gap = cla.getOptionValueAsDouble( AV_GAPINESS_OPTION );
111 if ( ( av_gap < 0 ) || ( av_gap >= 1 ) ) {
112 ForesterUtil.fatalError( PRG_NAME, "target gap-ratio is out of range: " + av_gap );
115 if ( cla.isOptionSet( LENGTH_OPTION ) ) {
116 if ( cla.isOptionSet( REMOVE_WORST_OFFENDERS_OPTION ) || cla.isOptionSet( AV_GAPINESS_OPTION ) ) {
120 length = cla.getOptionValueAsInt( LENGTH_OPTION );
121 if ( ( length < 2 ) || ( length >= msa.getLength() ) ) {
122 ForesterUtil.fatalError( PRG_NAME, "target length is out of range: " + length );
125 if ( cla.isOptionSet( STEP_OPTION ) ) {
126 step = cla.getOptionValueAsInt( STEP_OPTION );
128 || ( ( step > msa.getNumberOfSequences() ) || ( ( worst_remove > 0 ) && ( step > worst_remove ) ) ) ) {
129 ForesterUtil.fatalError( PRG_NAME, "value for step is out of range: " + step );
132 if ( cla.isOptionSet( REALIGN_OPTION ) ) {
135 if ( cla.isOptionSet( PATH_TO_MAFFT_OPTION ) ) {
137 ForesterUtil.fatalError( PRG_NAME, "no need to indicate path to MAFFT without realigning" );
139 path_to_mafft = cla.getOptionValueAsCleanString( PATH_TO_MAFFT_OPTION );
141 if ( cla.isOptionSet( DO_NOT_NORMALIZE_FOR_EFF_LENGTH_OPTION ) ) {
145 if ( ForesterUtil.isEmpty( path_to_mafft ) ) {
146 path_to_mafft = MsaCompactor.guessPathToMafft();
148 checkPathToMafft( path_to_mafft );
150 if ( worst_remove > 0 ) {
151 MsaCompactor.removeWorstOffenders( msa, worst_remove, step, realign, norm, path_to_mafft, out );
153 else if ( av_gap > 0 ) {
154 MsaCompactor.reduceGapAverage( msa, av_gap, step, realign, norm, path_to_mafft, out );
156 else if ( length > 0 ) {
157 // TODO if < shortest seq -> error
158 MsaCompactor.reduceLength( msa, length, step, realign, norm, path_to_mafft, out );
161 MsaCompactor.chart( msa, realign, norm, path_to_mafft );
164 catch ( final Exception e ) {
166 ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
170 private static void checkPathToMafft( final String path_to_mafft ) {
171 if ( !ForesterUtil.isEmpty( path_to_mafft ) && MsaInferrer.isInstalled( path_to_mafft ) ) {
172 ForesterUtil.programMessage( PRG_NAME, "using MAFFT at \"" + path_to_mafft + "\"" );
175 if ( ForesterUtil.isEmpty( path_to_mafft ) ) {
176 ForesterUtil.fatalError( PRG_NAME, "no MAFFT executable found, use -\"" + PATH_TO_MAFFT_OPTION
177 + "=<path to MAFFT>\" option" );
180 ForesterUtil.fatalError( PRG_NAME, "no MAFFT executable at \"" + path_to_mafft + "\"" );
185 private static void printHelp() {
186 ForesterUtil.printProgramInformation( PRG_NAME,
192 ForesterUtil.getForesterLibraryInformation() );
193 final String path_to_mafft = MsaCompactor.guessPathToMafft();
194 String mafft_comment;
195 if ( !ForesterUtil.isEmpty( path_to_mafft ) ) {
196 mafft_comment = " (using " + path_to_mafft + ")";
199 mafft_comment = " (no path to MAFFT found, use -\"" + PATH_TO_MAFFT_OPTION + "=<path to MAFFT>\" option";
201 System.out.println( "Usage:" );
202 System.out.println();
203 System.out.println( PRG_NAME + " <options> <msa input file> <output file>" );
204 System.out.println();
205 System.out.println( " options: " );
206 System.out.println();
207 System.out.println( " -" + REMOVE_WORST_OFFENDERS_OPTION
208 + "=<integer> number of worst offender sequences to remove" );
209 System.out.println( " -" + LENGTH_OPTION + "=<integer> target MSA length" );
210 System.out.println( " -" + AV_GAPINESS_OPTION + "=<decimal> target gap-ratio (0.0-1.0)" );
211 System.out.println( " -" + STEP_OPTION + "=<integer> step (for output and re-aligning)" );
212 System.out.println( " -" + REALIGN_OPTION + " to realign using MAFFT" + mafft_comment );
213 System.out.println();
214 System.out.println();
215 System.out.println();