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.Msa.MSA_FORMAT;
13 import org.forester.msa.MsaInferrer;
14 import org.forester.msa_compactor.MsaCompactor;
15 import org.forester.util.CommandLineArguments;
16 import org.forester.util.ForesterUtil;
18 public class msa_compactor {
20 final static private String HELP_OPTION_1 = "help";
21 final static private String HELP_OPTION_2 = "h";
22 final static private String REMOVE_WORST_OFFENDERS_OPTION = "r";
23 final static private String AV_GAPINESS_OPTION = "g";
24 final static private String STEP_OPTION = "s";
25 final static private String LENGTH_OPTION = "l";
26 final static private String REALIGN_OPTION = "a";
27 final static private String PATH_TO_MAFFT_OPTION = "mafft";
28 final static private String DO_NOT_NORMALIZE_FOR_EFF_LENGTH_OPTION = "nn";
29 final static private String PRG_NAME = "msa_compactor";
30 final static private String PRG_DESC = "multiple sequnce aligment compactor";
31 final static private String PRG_VERSION = "0.01";
32 final static private String PRG_DATE = "140314";
33 final static private String E_MAIL = "phylosoft@gmail.com";
34 final static private String WWW = "https://sites.google.com/site/cmzmasek/home/software/forester";
36 public static void main( final String args[] ) {
38 final CommandLineArguments cla = new CommandLineArguments( args );
39 if ( cla.isOptionSet( HELP_OPTION_1 ) || cla.isOptionSet( HELP_OPTION_2 ) || ( cla.getNumberOfNames() != 2 ) ) {
43 final File in = cla.getFile( 0 );
44 final File out = cla.getFile( 1 );
45 int worst_remove = -1;
49 boolean realign = false;
51 String path_to_mafft = null;
52 final List<String> allowed_options = new ArrayList<String>();
53 allowed_options.add( REMOVE_WORST_OFFENDERS_OPTION );
54 allowed_options.add( AV_GAPINESS_OPTION );
55 allowed_options.add( LENGTH_OPTION );
56 allowed_options.add( REALIGN_OPTION );
57 allowed_options.add( DO_NOT_NORMALIZE_FOR_EFF_LENGTH_OPTION );
58 allowed_options.add( STEP_OPTION );
59 allowed_options.add( PATH_TO_MAFFT_OPTION );
60 final String dissallowed_options = cla.validateAllowedOptionsAsString( allowed_options );
61 if ( dissallowed_options.length() > 0 ) {
62 ForesterUtil.fatalError( PRG_NAME, "unknown option(s): " + dissallowed_options );
64 if ( cla.isOptionSet( REMOVE_WORST_OFFENDERS_OPTION ) ) {
65 worst_remove = cla.getOptionValueAsInt( REMOVE_WORST_OFFENDERS_OPTION );
67 if ( cla.isOptionSet( AV_GAPINESS_OPTION ) ) {
68 av = cla.getOptionValueAsDouble( AV_GAPINESS_OPTION );
70 if ( cla.isOptionSet( LENGTH_OPTION ) ) {
71 length = cla.getOptionValueAsInt( LENGTH_OPTION );
73 if ( cla.isOptionSet( STEP_OPTION ) ) {
74 step = cla.getOptionValueAsInt( STEP_OPTION );
76 if ( cla.isOptionSet( REALIGN_OPTION ) ) {
79 if ( cla.isOptionSet( PATH_TO_MAFFT_OPTION ) ) {
81 ForesterUtil.fatalError( PRG_NAME, "no need to indicate path to MAFFT without realigning" );
83 path_to_mafft = cla.getOptionValueAsCleanString( PATH_TO_MAFFT_OPTION );
85 if ( cla.isOptionSet( DO_NOT_NORMALIZE_FOR_EFF_LENGTH_OPTION ) ) {
88 // else if ( cla.isOptionSet( STEP_OPTION ) && cla.isOptionSet( WINDOW_OPTION ) ) {
89 // step = cla.getOptionValueAsInt( STEP_OPTION );
90 // window = cla.getOptionValueAsInt( WINDOW_OPTION );
97 if ( ForesterUtil.isEmpty( path_to_mafft ) ) {
98 path_to_mafft = MsaCompactor.guessPathToMafft();
100 checkPathToMafft( path_to_mafft );
103 final FileInputStream is = new FileInputStream( in );
104 if ( FastaParser.isLikelyFasta( in ) ) {
105 msa = FastaParser.parseMsa( is );
108 msa = GeneralMsaParser.parse( is );
110 MsaCompactor mc = null;
111 if ( worst_remove > 0 ) {
112 mc = MsaCompactor.removeWorstOffenders( msa, worst_remove, realign, norm, path_to_mafft );
115 mc = MsaCompactor.reduceGapAverage( msa, av, step, realign, out, 50, path_to_mafft );
117 else if ( length > 0 ) {
118 mc = MsaCompactor.reduceLength( msa, length, step, realign, path_to_mafft );
120 //System.out.println( MsaMethods.calcGapRatio( mc.getMsa() ) );
121 // for( final String id : mc.getRemovedSeqIds() ) {
122 // System.out.println( id );
124 mc.writeMsa( out, MSA_FORMAT.PHYLIP, ".aln" );
126 catch ( final Exception e ) {
128 ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
132 private static void checkPathToMafft( final String path_to_mafft ) {
133 if ( !ForesterUtil.isEmpty( path_to_mafft ) && MsaInferrer.isInstalled( path_to_mafft ) ) {
134 ForesterUtil.programMessage( PRG_NAME, "using MAFFT at \"" + path_to_mafft + "\"" );
137 if ( ForesterUtil.isEmpty( path_to_mafft ) ) {
138 ForesterUtil.fatalError( PRG_NAME, "no MAFFT executable found, use -\"" + PATH_TO_MAFFT_OPTION
139 + "=<path to MAFFT>\" option" );
142 ForesterUtil.fatalError( PRG_NAME, "no MAFFT executable at \"" + path_to_mafft + "\"" );
147 private static void printHelp() {
148 ForesterUtil.printProgramInformation( PRG_NAME,
154 ForesterUtil.getForesterLibraryInformation() );
155 final String path_to_mafft = MsaCompactor.guessPathToMafft();
156 String mafft_comment;
157 if ( !ForesterUtil.isEmpty( path_to_mafft ) ) {
158 mafft_comment = " (using " + path_to_mafft + ")";
161 mafft_comment = " (no path to MAFFT found, use -\"" + PATH_TO_MAFFT_OPTION + "=<path to MAFFT>\" option";
163 System.out.println( "Usage:" );
164 System.out.println();
165 System.out.println( PRG_NAME + " <options> <msa input file> <output file>" );
166 System.out.println();
167 System.out.println( " options: " );
168 System.out.println();
169 System.out.println( " -" + REMOVE_WORST_OFFENDERS_OPTION
170 + "=<integer> number of worst offender sequences to remove" );
171 System.out.println( " -" + REALIGN_OPTION + " to realign using MAFFT" + mafft_comment );
172 System.out.println();
173 System.out.println();
174 System.out.println();