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.Msa.MSA_FORMAT;
14 import org.forester.msa.MsaMethods;
15 import org.forester.msa_compactor.MsaCompactor;
16 import org.forester.util.CommandLineArguments;
17 import org.forester.util.ForesterUtil;
19 public class msa_compactor {
21 final static private String HELP_OPTION_1 = "help";
22 final static private String HELP_OPTION_2 = "h";
23 final static private String REMOVE_WORST_OFFENDERS_OPTION = "w";
24 final static private String AV_GAPINESS_OPTION = "a";
25 final static private String STEP_OPTION = "s";
26 final static private String LENGTH_OPTION = "l";
27 final static private String REALIGN_OPTION = "r";
28 final static private String PATH_TO_MAFFT_OPTION = "mafft";
29 final static private String DO_NOT_NORMALIZE_FOR_EFF_LENGTH_OPTION = "nn";
30 final static private String PRG_NAME = "msa_compactor";
31 final static private String PRG_DESC = "multiple sequnce aligment compactor";
32 final static private String PRG_VERSION = "0.01";
33 final static private String PRG_DATE = "140314";
34 final static private String E_MAIL = "phylosoft@gmail.com";
35 final static private String WWW = "https://sites.google.com/site/cmzmasek/home/software/forester";
37 public static void main( final String args[] ) {
39 final CommandLineArguments cla = new CommandLineArguments( args );
40 if ( cla.isOptionSet( HELP_OPTION_1 ) || cla.isOptionSet( HELP_OPTION_2 ) || ( cla.getNumberOfNames() != 2 ) ) {
44 final File in = cla.getFile( 0 );
45 final File out = cla.getFile( 1 );
46 int worst_remove = -1;
50 boolean realign = false;
52 String path_to_mafft = null;
53 final List<String> allowed_options = new ArrayList<String>();
54 allowed_options.add( REMOVE_WORST_OFFENDERS_OPTION );
55 allowed_options.add( AV_GAPINESS_OPTION );
56 allowed_options.add( LENGTH_OPTION );
57 allowed_options.add( REALIGN_OPTION );
58 allowed_options.add( DO_NOT_NORMALIZE_FOR_EFF_LENGTH_OPTION );
59 allowed_options.add( STEP_OPTION );
60 allowed_options.add( PATH_TO_MAFFT_OPTION );
61 final String dissallowed_options = cla.validateAllowedOptionsAsString( allowed_options );
62 if ( dissallowed_options.length() > 0 ) {
63 ForesterUtil.fatalError( PRG_NAME, "unknown option(s): " + dissallowed_options );
65 if ( cla.isOptionSet( REMOVE_WORST_OFFENDERS_OPTION ) ) {
66 worst_remove = cla.getOptionValueAsInt( REMOVE_WORST_OFFENDERS_OPTION );
68 if ( cla.isOptionSet( AV_GAPINESS_OPTION ) ) {
69 av = cla.getOptionValueAsDouble( AV_GAPINESS_OPTION );
71 if ( cla.isOptionSet( LENGTH_OPTION ) ) {
72 length = cla.getOptionValueAsInt( LENGTH_OPTION );
74 if ( cla.isOptionSet( STEP_OPTION ) ) {
75 step = cla.getOptionValueAsInt( STEP_OPTION );
77 if ( cla.isOptionSet( REALIGN_OPTION ) ) {
80 if ( cla.isOptionSet( PATH_TO_MAFFT_OPTION ) ) {
82 ForesterUtil.fatalError( PRG_NAME, "no need to indicate path to MAFFT without realigning" );
84 path_to_mafft = cla.getOptionValueAsCleanString( PATH_TO_MAFFT_OPTION );
86 if ( cla.isOptionSet( DO_NOT_NORMALIZE_FOR_EFF_LENGTH_OPTION ) ) {
89 // else if ( cla.isOptionSet( STEP_OPTION ) && cla.isOptionSet( WINDOW_OPTION ) ) {
90 // step = cla.getOptionValueAsInt( STEP_OPTION );
91 // window = cla.getOptionValueAsInt( WINDOW_OPTION );
98 final FileInputStream is = new FileInputStream( in );
99 if ( FastaParser.isLikelyFasta( in ) ) {
100 msa = FastaParser.parseMsa( is );
103 msa = GeneralMsaParser.parse( is );
107 if ( ForesterUtil.isEmpty( path_to_mafft ) ) {
108 path_to_mafft = MsaCompactor.guessPathToMafft();
110 checkPathToMafft( path_to_mafft );
113 MsaCompactor mc = null;
114 if ( worst_remove > 0 ) {
115 mc = MsaCompactor.removeWorstOffenders( msa, worst_remove, realign, norm );
118 mc = MsaCompactor.reduceGapAverage( msa, av, step, realign, out, 50 );
120 else if ( length > 0 ) {
121 mc = MsaCompactor.reduceLength( msa, length, step, realign );
123 System.out.println( MsaMethods.calcGapRatio( mc.getMsa() ) );
124 for( final String id : mc.getRemovedSeqIds() ) {
125 System.out.println( id );
127 mc.writeMsa( out, MSA_FORMAT.PHYLIP, ".aln" );
129 catch ( final Exception e ) {
131 ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
135 private static void checkPathToMafft( String path_to_mafft ) {
136 if ( !ForesterUtil.isEmpty( path_to_mafft ) && MsaInferrer.isInstalled( path_to_mafft ) ) {
137 ForesterUtil.programMessage( PRG_NAME, "using MAFFT at \"" + path_to_mafft + "\"" );
140 if ( ForesterUtil.isEmpty( path_to_mafft ) ) {
141 ForesterUtil.fatalError( PRG_NAME, "no MAFFT executable found, use -\"" + PATH_TO_MAFFT_OPTION + "=<path to MAFFT>\" option" );
144 ForesterUtil.fatalError( PRG_NAME, "no MAFFT executable at \"" + path_to_mafft + "\"" );
149 private static void printHelp() {
150 ForesterUtil.printProgramInformation( PRG_NAME,
156 ForesterUtil.getForesterLibraryInformation() );
157 final String path_to_mafft = MsaCompactor.guessPathToMafft();
158 String mafft_comment;
159 if ( !ForesterUtil.isEmpty( path_to_mafft ) ) {
160 mafft_comment = " (" + path_to_mafft + ")";
163 mafft_comment = " (no path to MAFFT found, use -\"" + PATH_TO_MAFFT_OPTION + "=<path to MAFFT>\" option";
166 System.out.println( "Usage:" );
167 System.out.println();
168 System.out.println( PRG_NAME + " <options> <msa input file>" );
169 System.out.println();
170 System.out.println( " options: " );
171 System.out.println();
172 System.out.println( " -" + REMOVE_WORST_OFFENDERS_OPTION + "=<integer> number of sequences to remove" );
173 System.out.println( " -" + REALIGN_OPTION + " to realign using MAFFT" + mafft_comment );
174 System.out.println();
175 System.out.println();
176 System.out.println();