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 );
63 if ( cla.isOptionSet( REMOVE_WORST_OFFENDERS_OPTION ) ) {
64 worst_remove = cla.getOptionValueAsInt( REMOVE_WORST_OFFENDERS_OPTION );
66 if ( cla.isOptionSet( AV_GAPINESS_OPTION ) ) {
67 if ( cla.isOptionSet( REMOVE_WORST_OFFENDERS_OPTION ) ) {
71 av = cla.getOptionValueAsDouble( AV_GAPINESS_OPTION );
73 if ( cla.isOptionSet( LENGTH_OPTION ) ) {
74 if ( cla.isOptionSet( REMOVE_WORST_OFFENDERS_OPTION ) || cla.isOptionSet( AV_GAPINESS_OPTION ) ) {
78 length = cla.getOptionValueAsInt( LENGTH_OPTION );
80 if ( cla.isOptionSet( STEP_OPTION ) ) {
81 step = cla.getOptionValueAsInt( STEP_OPTION );
83 if ( cla.isOptionSet( REALIGN_OPTION ) ) {
86 if ( cla.isOptionSet( PATH_TO_MAFFT_OPTION ) ) {
88 ForesterUtil.fatalError( PRG_NAME, "no need to indicate path to MAFFT without realigning" );
90 path_to_mafft = cla.getOptionValueAsCleanString( PATH_TO_MAFFT_OPTION );
92 if ( cla.isOptionSet( DO_NOT_NORMALIZE_FOR_EFF_LENGTH_OPTION ) ) {
96 if ( ForesterUtil.isEmpty( path_to_mafft ) ) {
97 path_to_mafft = MsaCompactor.guessPathToMafft();
99 checkPathToMafft( path_to_mafft );
102 final FileInputStream is = new FileInputStream( in );
103 if ( FastaParser.isLikelyFasta( in ) ) {
104 msa = FastaParser.parseMsa( is );
107 msa = GeneralMsaParser.parse( is );
109 if ( worst_remove > 0 ) {
110 MsaCompactor.removeWorstOffenders( msa, worst_remove, step, realign, norm, path_to_mafft, out );
113 MsaCompactor.reduceGapAverage( msa, av, step, realign, norm, path_to_mafft, out );
115 else if ( length > 0 ) {
116 if ( length >= msa.getLength() ) {
117 ForesterUtil.fatalError( PRG_NAME, "target MSA length (" + length
118 + ") is greater than or equal to MSA original length (" + msa.getLength() + ")" );
120 // TODO if < shortest seq -> error
121 MsaCompactor.reduceLength( msa, length, step, realign, norm, path_to_mafft, out );
124 catch ( final Exception e ) {
126 ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
130 private static void checkPathToMafft( final String path_to_mafft ) {
131 if ( !ForesterUtil.isEmpty( path_to_mafft ) && MsaInferrer.isInstalled( path_to_mafft ) ) {
132 ForesterUtil.programMessage( PRG_NAME, "using MAFFT at \"" + path_to_mafft + "\"" );
135 if ( ForesterUtil.isEmpty( path_to_mafft ) ) {
136 ForesterUtil.fatalError( PRG_NAME, "no MAFFT executable found, use -\"" + PATH_TO_MAFFT_OPTION
137 + "=<path to MAFFT>\" option" );
140 ForesterUtil.fatalError( PRG_NAME, "no MAFFT executable at \"" + path_to_mafft + "\"" );
145 private static void printHelp() {
146 ForesterUtil.printProgramInformation( PRG_NAME,
152 ForesterUtil.getForesterLibraryInformation() );
153 final String path_to_mafft = MsaCompactor.guessPathToMafft();
154 String mafft_comment;
155 if ( !ForesterUtil.isEmpty( path_to_mafft ) ) {
156 mafft_comment = " (using " + path_to_mafft + ")";
159 mafft_comment = " (no path to MAFFT found, use -\"" + PATH_TO_MAFFT_OPTION + "=<path to MAFFT>\" option";
161 System.out.println( "Usage:" );
162 System.out.println();
163 System.out.println( PRG_NAME + " <options> <msa input file> <output file>" );
164 System.out.println();
165 System.out.println( " options: " );
166 System.out.println();
167 System.out.println( " -" + REMOVE_WORST_OFFENDERS_OPTION
168 + "=<integer> number of worst offender sequences to remove" );
169 System.out.println( " -" + LENGTH_OPTION + "=<integer> target MSA length" );
170 System.out.println( " -" + AV_GAPINESS_OPTION + "=<decimal> target gap-ratio (0.0-1.0)" );
171 System.out.println( " -" + STEP_OPTION + "=<integer> step (for output and re-aligning)" );
172 System.out.println( " -" + REALIGN_OPTION + " to realign using MAFFT" + mafft_comment );
173 System.out.println();
174 System.out.println();
175 System.out.println();