b030d7427a110077324728bf8cd7e2b4f98b77f8
[jalview.git] / forester / java / src / org / forester / application / msa_compactor.java
1
2 package org.forester.application;
3
4 import java.io.File;
5 import java.io.FileInputStream;
6 import java.util.ArrayList;
7 import java.util.List;
8
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;
18
19 public class msa_compactor {
20
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";
36
37     public static void main( final String args[] ) {
38         try {
39             final CommandLineArguments cla = new CommandLineArguments( args );
40             if ( cla.isOptionSet( HELP_OPTION_1 ) || cla.isOptionSet( HELP_OPTION_2 ) || ( cla.getNumberOfNames() != 2 ) ) {
41                 printHelp();
42                 System.exit( 0 );
43             }
44             final File in = cla.getFile( 0 );
45             final File out = cla.getFile( 1 );
46             int worst_remove = -1;
47             double av = -1;
48             int length = -1;
49             int step = 1;
50             boolean realign = false;
51             boolean norm = true;
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 );
64             }
65             if ( cla.isOptionSet( REMOVE_WORST_OFFENDERS_OPTION ) ) {
66                 worst_remove = cla.getOptionValueAsInt( REMOVE_WORST_OFFENDERS_OPTION );
67             }
68             if ( cla.isOptionSet( AV_GAPINESS_OPTION ) ) {
69                 av = cla.getOptionValueAsDouble( AV_GAPINESS_OPTION );
70             }
71             if ( cla.isOptionSet( LENGTH_OPTION ) ) {
72                 length = cla.getOptionValueAsInt( LENGTH_OPTION );
73             }
74             if ( cla.isOptionSet( STEP_OPTION ) ) {
75                 step = cla.getOptionValueAsInt( STEP_OPTION );
76             }
77             if ( cla.isOptionSet( REALIGN_OPTION ) ) {
78                 realign = true;
79             }
80             if ( cla.isOptionSet( PATH_TO_MAFFT_OPTION ) ) {
81                 if ( !realign ) {
82                     ForesterUtil.fatalError( PRG_NAME, "no need to indicate path to MAFFT without realigning" );
83                 }
84                 path_to_mafft = cla.getOptionValueAsCleanString( PATH_TO_MAFFT_OPTION );
85             }
86             if ( cla.isOptionSet( DO_NOT_NORMALIZE_FOR_EFF_LENGTH_OPTION ) ) {
87                 norm = false;
88             }
89             //            else if ( cla.isOptionSet( STEP_OPTION ) && cla.isOptionSet( WINDOW_OPTION ) ) {
90             //                step = cla.getOptionValueAsInt( STEP_OPTION );
91             //                window = cla.getOptionValueAsInt( WINDOW_OPTION );
92             //            }
93             //            else {
94             //                printHelp();
95             //                System.exit( 0 );
96             //            }
97             
98             if ( realign ) {
99                 if ( ForesterUtil.isEmpty( path_to_mafft ) ) {
100                     path_to_mafft = MsaCompactor.guessPathToMafft();
101                 }
102                 checkPathToMafft( path_to_mafft );
103             }
104             
105             
106             Msa msa = null;
107             final FileInputStream is = new FileInputStream( in );
108             if ( FastaParser.isLikelyFasta( in ) ) {
109                 msa = FastaParser.parseMsa( is );
110             }
111             else {
112                 msa = GeneralMsaParser.parse( is );
113             }
114             
115           
116             
117             MsaCompactor mc = null;
118             if ( worst_remove > 0 ) {
119                 mc = MsaCompactor.removeWorstOffenders( msa, worst_remove, realign, norm, path_to_mafft );
120             }
121             else if ( av > 0 ) {
122                 mc = MsaCompactor.reduceGapAverage( msa, av, step, realign, out, 50, path_to_mafft );
123             }
124             else if ( length > 0 ) {
125                 mc = MsaCompactor.reduceLength( msa, length, step, realign, path_to_mafft );
126             }
127             System.out.println( MsaMethods.calcGapRatio( mc.getMsa() ) );
128             for( final String id : mc.getRemovedSeqIds() ) {
129                 System.out.println( id );
130             }
131             mc.writeMsa( out, MSA_FORMAT.PHYLIP, ".aln" );
132         }
133         catch ( final Exception e ) {
134             e.printStackTrace();
135             ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
136         }
137     }
138
139     private static void checkPathToMafft( String path_to_mafft ) {
140         if ( !ForesterUtil.isEmpty( path_to_mafft ) && MsaInferrer.isInstalled( path_to_mafft ) ) {
141             ForesterUtil.programMessage( PRG_NAME, "using MAFFT at \"" + path_to_mafft + "\""  );
142         }
143         else {
144             if ( ForesterUtil.isEmpty( path_to_mafft ) ) {
145                 ForesterUtil.fatalError( PRG_NAME, "no MAFFT executable found, use -\"" + PATH_TO_MAFFT_OPTION + "=<path to MAFFT>\" option" );
146             }
147             else {
148                 ForesterUtil.fatalError( PRG_NAME, "no MAFFT executable at \""  + path_to_mafft + "\""  );
149             }
150         }
151     }
152
153     private static void printHelp() {
154         ForesterUtil.printProgramInformation( PRG_NAME,
155                                               PRG_DESC,
156                                               PRG_VERSION,
157                                               PRG_DATE,
158                                               E_MAIL,
159                                               WWW,
160                                               ForesterUtil.getForesterLibraryInformation() );
161         final String path_to_mafft = MsaCompactor.guessPathToMafft();
162         String mafft_comment;
163         if ( !ForesterUtil.isEmpty( path_to_mafft ) ) {
164             mafft_comment = " (" + path_to_mafft + ")";
165         }
166         else {
167             mafft_comment = " (no path to MAFFT found, use -\"" + PATH_TO_MAFFT_OPTION + "=<path to MAFFT>\" option";
168         }
169         
170         System.out.println( "Usage:" );
171         System.out.println();
172         System.out.println( PRG_NAME + " <options> <msa input file>" );
173         System.out.println();
174         System.out.println( " options: " );
175         System.out.println();
176         System.out.println( "   -" + REMOVE_WORST_OFFENDERS_OPTION + "=<integer>  number of sequences to remove" );
177         System.out.println( "   -" + REALIGN_OPTION + " to realign using MAFFT" + mafft_comment );
178         System.out.println();
179         System.out.println();
180         System.out.println();
181     }
182 }