2 package org.forester.application;
5 import java.io.FileInputStream;
6 import java.util.ArrayList;
9 import org.forester.io.parsers.FastaParser;
10 import org.forester.io.parsers.GeneralMsaParser;
11 import org.forester.msa.Msa;
12 import org.forester.msa.MsaInferrer;
13 import org.forester.msa_compactor.MsaCompactor;
14 import org.forester.util.CommandLineArguments;
15 import org.forester.util.ForesterUtil;
17 public class msa_compactor {
19 final static private String HELP_OPTION_1 = "help";
20 final static private String HELP_OPTION_2 = "h";
21 final static private String REMOVE_WORST_OFFENDERS_OPTION = "r";
22 final static private String AV_GAPINESS_OPTION = "g";
23 final static private String STEP_OPTION = "s";
24 final static private String LENGTH_OPTION = "l";
25 final static private String REALIGN_OPTION = "a";
26 final static private String PATH_TO_MAFFT_OPTION = "mafft";
27 final static private String DO_NOT_NORMALIZE_FOR_EFF_LENGTH_OPTION = "nn";
28 final static private String PRG_NAME = "msa_compactor";
29 final static private String PRG_DESC = "multiple sequence aligment compactor";
30 final static private String PRG_VERSION = "0.01";
31 final static private String PRG_DATE = "140316";
32 final static private String E_MAIL = "phylosoft@gmail.com";
33 final static private String WWW = "https://sites.google.com/site/cmzmasek/home/software/forester";
35 public static void main( final String args[] ) {
37 final CommandLineArguments cla = new CommandLineArguments( args );
38 if ( cla.isOptionSet( HELP_OPTION_1 ) || cla.isOptionSet( HELP_OPTION_2 ) || ( cla.getNumberOfNames() != 2 ) ) {
42 final File in = cla.getFile( 0 );
43 final File out = cla.getFile( 1 );
44 int worst_remove = -1;
48 boolean realign = false;
50 String path_to_mafft = null;
51 final List<String> allowed_options = new ArrayList<String>();
52 allowed_options.add( REMOVE_WORST_OFFENDERS_OPTION );
53 allowed_options.add( AV_GAPINESS_OPTION );
54 allowed_options.add( LENGTH_OPTION );
55 allowed_options.add( REALIGN_OPTION );
56 allowed_options.add( DO_NOT_NORMALIZE_FOR_EFF_LENGTH_OPTION );
57 allowed_options.add( STEP_OPTION );
58 allowed_options.add( PATH_TO_MAFFT_OPTION );
59 final String dissallowed_options = cla.validateAllowedOptionsAsString( allowed_options );
60 if ( dissallowed_options.length() > 0 ) {
61 ForesterUtil.fatalError( PRG_NAME, "unknown option(s): " + dissallowed_options );
64 final FileInputStream is = new FileInputStream( in );
65 if ( FastaParser.isLikelyFasta( in ) ) {
66 msa = FastaParser.parseMsa( is );
69 msa = GeneralMsaParser.parse( is );
71 if ( cla.isOptionSet( REMOVE_WORST_OFFENDERS_OPTION ) ) {
72 worst_remove = cla.getOptionValueAsInt( REMOVE_WORST_OFFENDERS_OPTION );
73 if ( ( worst_remove < 1 ) || ( worst_remove >= msa.getNumberOfSequences() - 1 ) ) {
74 ForesterUtil.fatalError( PRG_NAME, "number of worst offender sequences to remove is out of range: "
78 if ( cla.isOptionSet( AV_GAPINESS_OPTION ) ) {
79 if ( cla.isOptionSet( REMOVE_WORST_OFFENDERS_OPTION ) ) {
83 av_gap = cla.getOptionValueAsDouble( AV_GAPINESS_OPTION );
84 if ( ( av_gap < 0 ) || ( av_gap >= 1 ) ) {
85 ForesterUtil.fatalError( PRG_NAME, "target gap-ratio is out of range: " + av_gap );
88 if ( cla.isOptionSet( LENGTH_OPTION ) ) {
89 if ( cla.isOptionSet( REMOVE_WORST_OFFENDERS_OPTION ) || cla.isOptionSet( AV_GAPINESS_OPTION ) ) {
93 length = cla.getOptionValueAsInt( LENGTH_OPTION );
94 if ( ( length < 2 ) || ( length >= msa.getLength() ) ) {
95 ForesterUtil.fatalError( PRG_NAME, "target length is out of range: " + length );
98 if ( cla.isOptionSet( STEP_OPTION ) ) {
99 step = cla.getOptionValueAsInt( STEP_OPTION );
101 || ( ( step > msa.getNumberOfSequences() ) || ( ( worst_remove > 0 ) && ( step > worst_remove ) ) ) ) {
102 ForesterUtil.fatalError( PRG_NAME, "value for step is out of range: " + step );
105 if ( cla.isOptionSet( REALIGN_OPTION ) ) {
108 if ( cla.isOptionSet( PATH_TO_MAFFT_OPTION ) ) {
110 ForesterUtil.fatalError( PRG_NAME, "no need to indicate path to MAFFT without realigning" );
112 path_to_mafft = cla.getOptionValueAsCleanString( PATH_TO_MAFFT_OPTION );
114 if ( cla.isOptionSet( DO_NOT_NORMALIZE_FOR_EFF_LENGTH_OPTION ) ) {
118 if ( ForesterUtil.isEmpty( path_to_mafft ) ) {
119 path_to_mafft = MsaCompactor.guessPathToMafft();
121 checkPathToMafft( path_to_mafft );
123 if ( worst_remove > 0 ) {
124 MsaCompactor.removeWorstOffenders( msa, worst_remove, step, realign, norm, path_to_mafft, out );
126 else if ( av_gap > 0 ) {
127 MsaCompactor.reduceGapAverage( msa, av_gap, step, realign, norm, path_to_mafft, out );
129 else if ( length > 0 ) {
130 // TODO if < shortest seq -> error
131 MsaCompactor.reduceLength( msa, length, step, realign, norm, path_to_mafft, out );
134 catch ( final Exception e ) {
136 ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
140 private static void checkPathToMafft( final String path_to_mafft ) {
141 if ( !ForesterUtil.isEmpty( path_to_mafft ) && MsaInferrer.isInstalled( path_to_mafft ) ) {
142 ForesterUtil.programMessage( PRG_NAME, "using MAFFT at \"" + path_to_mafft + "\"" );
145 if ( ForesterUtil.isEmpty( path_to_mafft ) ) {
146 ForesterUtil.fatalError( PRG_NAME, "no MAFFT executable found, use -\"" + PATH_TO_MAFFT_OPTION
147 + "=<path to MAFFT>\" option" );
150 ForesterUtil.fatalError( PRG_NAME, "no MAFFT executable at \"" + path_to_mafft + "\"" );
155 private static void printHelp() {
156 ForesterUtil.printProgramInformation( PRG_NAME,
162 ForesterUtil.getForesterLibraryInformation() );
163 final String path_to_mafft = MsaCompactor.guessPathToMafft();
164 String mafft_comment;
165 if ( !ForesterUtil.isEmpty( path_to_mafft ) ) {
166 mafft_comment = " (using " + path_to_mafft + ")";
169 mafft_comment = " (no path to MAFFT found, use -\"" + PATH_TO_MAFFT_OPTION + "=<path to MAFFT>\" option";
171 System.out.println( "Usage:" );
172 System.out.println();
173 System.out.println( PRG_NAME + " <options> <msa input file> <output file>" );
174 System.out.println();
175 System.out.println( " options: " );
176 System.out.println();
177 System.out.println( " -" + REMOVE_WORST_OFFENDERS_OPTION
178 + "=<integer> number of worst offender sequences to remove" );
179 System.out.println( " -" + LENGTH_OPTION + "=<integer> target MSA length" );
180 System.out.println( " -" + AV_GAPINESS_OPTION + "=<decimal> target gap-ratio (0.0-1.0)" );
181 System.out.println( " -" + STEP_OPTION + "=<integer> step (for output and re-aligning)" );
182 System.out.println( " -" + REALIGN_OPTION + " to realign using MAFFT" + mafft_comment );
183 System.out.println();
184 System.out.println();
185 System.out.println();