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.BasicMsa;
35 import org.forester.msa.DeleteableMsa;
36 import org.forester.msa.MsaInferrer;
37 import org.forester.msa_compactor.MsaCompactor;
38 import org.forester.util.CommandLineArguments;
39 import org.forester.util.ForesterUtil;
41 public class msa_compactor {
43 final static private String HELP_OPTION_1 = "help";
44 final static private String HELP_OPTION_2 = "h";
45 final static private String REMOVE_WORST_OFFENDERS_OPTION = "r";
46 final static private String AV_GAPINESS_OPTION = "g";
47 final static private String STEP_OPTION = "s";
48 final static private String LENGTH_OPTION = "l";
49 final static private String REALIGN_OPTION = "a";
50 final static private String PATH_TO_MAFFT_OPTION = "mafft";
51 final static private String DO_NOT_NORMALIZE_FOR_EFF_LENGTH_OPTION = "nn";
52 final static private String PRG_NAME = "msa_compactor";
53 final static private String PRG_DESC = "multiple sequence aligment compactor";
54 final static private String PRG_VERSION = "0.01";
55 final static private String PRG_DATE = "140316";
56 final static private String E_MAIL = "phylosoft@gmail.com";
57 final static private String WWW = "https://sites.google.com/site/cmzmasek/home/software/forester";
59 public static void main( final String args[] ) {
61 final CommandLineArguments cla = new CommandLineArguments( args );
62 if ( cla.isOptionSet( HELP_OPTION_1 ) || cla.isOptionSet( HELP_OPTION_2 )
63 || ( ( cla.getNumberOfNames() < 1 ) || ( cla.getNumberOfNames() > 2 ) ) ) {
67 final File in = cla.getFile( 0 );
69 if ( cla.getNumberOfNames() > 1 ) {
70 out = cla.getFile( 1 );
72 int worst_remove = -1;
76 boolean realign = false;
78 String path_to_mafft = null;
79 final List<String> allowed_options = new ArrayList<String>();
80 allowed_options.add( REMOVE_WORST_OFFENDERS_OPTION );
81 allowed_options.add( AV_GAPINESS_OPTION );
82 allowed_options.add( LENGTH_OPTION );
83 allowed_options.add( REALIGN_OPTION );
84 allowed_options.add( DO_NOT_NORMALIZE_FOR_EFF_LENGTH_OPTION );
85 allowed_options.add( STEP_OPTION );
86 allowed_options.add( PATH_TO_MAFFT_OPTION );
87 final String dissallowed_options = cla.validateAllowedOptionsAsString( allowed_options );
88 if ( dissallowed_options.length() > 0 ) {
89 ForesterUtil.fatalError( PRG_NAME, "unknown option(s): " + dissallowed_options );
91 DeleteableMsa msa = null;
92 final FileInputStream is = new FileInputStream( in );
93 if ( FastaParser.isLikelyFasta( in ) ) {
94 msa = new DeleteableMsa( ( BasicMsa ) FastaParser.parseMsa( is ) );
97 msa = new DeleteableMsa( ( BasicMsa ) GeneralMsaParser.parse( is ) );
99 if ( cla.isOptionSet( REMOVE_WORST_OFFENDERS_OPTION ) ) {
100 worst_remove = cla.getOptionValueAsInt( REMOVE_WORST_OFFENDERS_OPTION );
101 if ( ( worst_remove < 1 ) || ( worst_remove >= msa.getNumberOfSequences() - 1 ) ) {
102 ForesterUtil.fatalError( PRG_NAME, "number of worst offender sequences to remove is out of range: "
106 if ( cla.isOptionSet( AV_GAPINESS_OPTION ) ) {
107 if ( cla.isOptionSet( REMOVE_WORST_OFFENDERS_OPTION ) ) {
111 av_gap = cla.getOptionValueAsDouble( AV_GAPINESS_OPTION );
112 if ( ( av_gap < 0 ) || ( av_gap >= 1 ) ) {
113 ForesterUtil.fatalError( PRG_NAME, "target gap-ratio is out of range: " + av_gap );
116 if ( cla.isOptionSet( LENGTH_OPTION ) ) {
117 if ( cla.isOptionSet( REMOVE_WORST_OFFENDERS_OPTION ) || cla.isOptionSet( AV_GAPINESS_OPTION ) ) {
121 length = cla.getOptionValueAsInt( LENGTH_OPTION );
122 if ( ( length < 2 ) || ( length >= msa.getLength() ) ) {
123 ForesterUtil.fatalError( PRG_NAME, "target length is out of range: " + length );
126 if ( cla.isOptionSet( STEP_OPTION ) ) {
127 step = cla.getOptionValueAsInt( STEP_OPTION );
129 || ( ( step > msa.getNumberOfSequences() ) || ( ( worst_remove > 0 ) && ( step > worst_remove ) ) ) ) {
130 ForesterUtil.fatalError( PRG_NAME, "value for step is out of range: " + step );
133 if ( cla.isOptionSet( REALIGN_OPTION ) ) {
136 if ( cla.isOptionSet( PATH_TO_MAFFT_OPTION ) ) {
138 ForesterUtil.fatalError( PRG_NAME, "no need to indicate path to MAFFT without realigning" );
140 path_to_mafft = cla.getOptionValueAsCleanString( PATH_TO_MAFFT_OPTION );
142 if ( cla.isOptionSet( DO_NOT_NORMALIZE_FOR_EFF_LENGTH_OPTION ) ) {
146 if ( ForesterUtil.isEmpty( path_to_mafft ) ) {
147 path_to_mafft = MsaCompactor.guessPathToMafft();
149 checkPathToMafft( path_to_mafft );
151 if ( worst_remove > 0 ) {
152 MsaCompactor.removeWorstOffenders( msa, worst_remove, step, realign, norm, path_to_mafft, out );
154 else if ( av_gap > 0 ) {
155 MsaCompactor.reduceGapAverage( msa, av_gap, step, realign, norm, path_to_mafft, out );
157 else if ( length > 0 ) {
158 // TODO if < shortest seq -> error
159 MsaCompactor.reduceLength( msa, length, step, realign, norm, path_to_mafft, out );
162 MsaCompactor.chart( msa, step, realign, norm, path_to_mafft );
165 catch ( final Exception e ) {
167 ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
171 private static void checkPathToMafft( final String path_to_mafft ) {
172 if ( !ForesterUtil.isEmpty( path_to_mafft ) && MsaInferrer.isInstalled( path_to_mafft ) ) {
173 ForesterUtil.programMessage( PRG_NAME, "using MAFFT at \"" + path_to_mafft + "\"" );
176 if ( ForesterUtil.isEmpty( path_to_mafft ) ) {
177 ForesterUtil.fatalError( PRG_NAME, "no MAFFT executable found, use -\"" + PATH_TO_MAFFT_OPTION
178 + "=<path to MAFFT>\" option" );
181 ForesterUtil.fatalError( PRG_NAME, "no MAFFT executable at \"" + path_to_mafft + "\"" );
186 private static void printHelp() {
187 ForesterUtil.printProgramInformation( PRG_NAME,
193 ForesterUtil.getForesterLibraryInformation() );
194 final String path_to_mafft = MsaCompactor.guessPathToMafft();
195 String mafft_comment;
196 if ( !ForesterUtil.isEmpty( path_to_mafft ) ) {
197 mafft_comment = " (using " + path_to_mafft + ")";
200 mafft_comment = " (no path to MAFFT found, use -\"" + PATH_TO_MAFFT_OPTION + "=<path to MAFFT>\" option";
202 System.out.println( "Usage:" );
203 System.out.println();
204 System.out.println( PRG_NAME + " <options> <msa input file> <output file>" );
205 System.out.println();
206 System.out.println( " options: " );
207 System.out.println();
208 System.out.println( " -" + REMOVE_WORST_OFFENDERS_OPTION
209 + "=<integer> number of worst offender sequences to remove" );
210 System.out.println( " -" + LENGTH_OPTION + "=<integer> target MSA length" );
211 System.out.println( " -" + AV_GAPINESS_OPTION + "=<decimal> target gap-ratio (0.0-1.0)" );
212 System.out.println( " -" + STEP_OPTION + "=<integer> step (for output and re-aligning)" );
213 System.out.println( " -" + REALIGN_OPTION + " to realign using MAFFT" + mafft_comment );
214 System.out.println();
215 System.out.println();
216 System.out.println();