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 av = cla.getOptionValueAsDouble( AV_GAPINESS_OPTION );
69 if ( cla.isOptionSet( LENGTH_OPTION ) ) {
70 length = cla.getOptionValueAsInt( LENGTH_OPTION );
72 if ( cla.isOptionSet( STEP_OPTION ) ) {
73 step = cla.getOptionValueAsInt( STEP_OPTION );
75 if ( cla.isOptionSet( REALIGN_OPTION ) ) {
78 if ( cla.isOptionSet( PATH_TO_MAFFT_OPTION ) ) {
80 ForesterUtil.fatalError( PRG_NAME, "no need to indicate path to MAFFT without realigning" );
82 path_to_mafft = cla.getOptionValueAsCleanString( PATH_TO_MAFFT_OPTION );
84 if ( cla.isOptionSet( DO_NOT_NORMALIZE_FOR_EFF_LENGTH_OPTION ) ) {
88 if ( ForesterUtil.isEmpty( path_to_mafft ) ) {
89 path_to_mafft = MsaCompactor.guessPathToMafft();
91 checkPathToMafft( path_to_mafft );
94 final FileInputStream is = new FileInputStream( in );
95 if ( FastaParser.isLikelyFasta( in ) ) {
96 msa = FastaParser.parseMsa( is );
99 msa = GeneralMsaParser.parse( is );
101 MsaCompactor mc = null;
102 if ( worst_remove > 0 ) {
103 mc = MsaCompactor.removeWorstOffenders( msa, worst_remove, step, realign, norm, path_to_mafft, out );
106 mc = MsaCompactor.reduceGapAverage( msa, av, step, realign, norm, path_to_mafft, out );
108 else if ( length > 0 ) {
109 if ( length >= msa.getLength() ) {
110 ForesterUtil.fatalError( PRG_NAME, "target MSA length (" + length
111 + ") is greater than or equal to MSA original length (" + msa.getLength() + ")" );
113 // TODO if < shortest seq -> error
114 mc = MsaCompactor.reduceLength( msa, length, step, realign, norm, path_to_mafft, out );
117 catch ( final Exception e ) {
119 ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
123 private static void checkPathToMafft( final String path_to_mafft ) {
124 if ( !ForesterUtil.isEmpty( path_to_mafft ) && MsaInferrer.isInstalled( path_to_mafft ) ) {
125 ForesterUtil.programMessage( PRG_NAME, "using MAFFT at \"" + path_to_mafft + "\"" );
128 if ( ForesterUtil.isEmpty( path_to_mafft ) ) {
129 ForesterUtil.fatalError( PRG_NAME, "no MAFFT executable found, use -\"" + PATH_TO_MAFFT_OPTION
130 + "=<path to MAFFT>\" option" );
133 ForesterUtil.fatalError( PRG_NAME, "no MAFFT executable at \"" + path_to_mafft + "\"" );
138 private static void printHelp() {
139 ForesterUtil.printProgramInformation( PRG_NAME,
145 ForesterUtil.getForesterLibraryInformation() );
146 final String path_to_mafft = MsaCompactor.guessPathToMafft();
147 String mafft_comment;
148 if ( !ForesterUtil.isEmpty( path_to_mafft ) ) {
149 mafft_comment = " (using " + path_to_mafft + ")";
152 mafft_comment = " (no path to MAFFT found, use -\"" + PATH_TO_MAFFT_OPTION + "=<path to MAFFT>\" option";
154 System.out.println( "Usage:" );
155 System.out.println();
156 System.out.println( PRG_NAME + " <options> <msa input file> <output file>" );
157 System.out.println();
158 System.out.println( " options: " );
159 System.out.println();
160 System.out.println( " -" + REMOVE_WORST_OFFENDERS_OPTION
161 + "=<integer> number of worst offender sequences to remove" );
162 System.out.println( " -" + LENGTH_OPTION + "=<integer> target MSA length" );
163 System.out.println( " -" + AV_GAPINESS_OPTION + "=<decimal> target gap-ratio (0.0-1.0)" );
164 System.out.println( " -" + STEP_OPTION + "=<integer> step (for output and re-aligning)" );
165 System.out.println( " -" + REALIGN_OPTION + " to realign using MAFFT" + mafft_comment );
166 System.out.println();
167 System.out.println();
168 System.out.println();