inprogress
[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.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;
17
18 public class msa_compactor {
19
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";
35
36     public static void main( final String args[] ) {
37         try {
38             final CommandLineArguments cla = new CommandLineArguments( args );
39             if ( cla.isOptionSet( HELP_OPTION_1 ) || cla.isOptionSet( HELP_OPTION_2 ) || ( cla.getNumberOfNames() != 2 ) ) {
40                 printHelp();
41                 System.exit( 0 );
42             }
43             final File in = cla.getFile( 0 );
44             final File out = cla.getFile( 1 );
45             int worst_remove = -1;
46             double av = -1;
47             int length = -1;
48             int step = 1;
49             boolean realign = false;
50             boolean norm = true;
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 );
63             }
64             if ( cla.isOptionSet( REMOVE_WORST_OFFENDERS_OPTION ) ) {
65                 worst_remove = cla.getOptionValueAsInt( REMOVE_WORST_OFFENDERS_OPTION );
66             }
67             if ( cla.isOptionSet( AV_GAPINESS_OPTION ) ) {
68                 av = cla.getOptionValueAsDouble( AV_GAPINESS_OPTION );
69             }
70             if ( cla.isOptionSet( LENGTH_OPTION ) ) {
71                 length = cla.getOptionValueAsInt( LENGTH_OPTION );
72             }
73             if ( cla.isOptionSet( STEP_OPTION ) ) {
74                 step = cla.getOptionValueAsInt( STEP_OPTION );
75             }
76             if ( cla.isOptionSet( REALIGN_OPTION ) ) {
77                 realign = true;
78             }
79             if ( cla.isOptionSet( PATH_TO_MAFFT_OPTION ) ) {
80                 if ( !realign ) {
81                     ForesterUtil.fatalError( PRG_NAME, "no need to indicate path to MAFFT without realigning" );
82                 }
83                 path_to_mafft = cla.getOptionValueAsCleanString( PATH_TO_MAFFT_OPTION );
84             }
85             if ( cla.isOptionSet( DO_NOT_NORMALIZE_FOR_EFF_LENGTH_OPTION ) ) {
86                 norm = false;
87             }
88             //            else if ( cla.isOptionSet( STEP_OPTION ) && cla.isOptionSet( WINDOW_OPTION ) ) {
89             //                step = cla.getOptionValueAsInt( STEP_OPTION );
90             //                window = cla.getOptionValueAsInt( WINDOW_OPTION );
91             //            }
92             //            else {
93             //                printHelp();
94             //                System.exit( 0 );
95             //            }
96             if ( realign ) {
97                 if ( ForesterUtil.isEmpty( path_to_mafft ) ) {
98                     path_to_mafft = MsaCompactor.guessPathToMafft();
99                 }
100                 checkPathToMafft( path_to_mafft );
101             }
102             Msa msa = null;
103             final FileInputStream is = new FileInputStream( in );
104             if ( FastaParser.isLikelyFasta( in ) ) {
105                 msa = FastaParser.parseMsa( is );
106             }
107             else {
108                 msa = GeneralMsaParser.parse( is );
109             }
110             MsaCompactor mc = null;
111             if ( worst_remove > 0 ) {
112                 mc = MsaCompactor.removeWorstOffenders( msa, worst_remove, realign, norm, path_to_mafft );
113             }
114             else if ( av > 0 ) {
115                 mc = MsaCompactor.reduceGapAverage( msa, av, step, realign, out, 50, path_to_mafft );
116             }
117             else if ( length > 0 ) {
118                 mc = MsaCompactor.reduceLength( msa, length, step, realign, path_to_mafft );
119             }
120             //System.out.println( MsaMethods.calcGapRatio( mc.getMsa() ) );
121             // for( final String id : mc.getRemovedSeqIds() ) {
122             //     System.out.println( id );
123             //}
124             mc.writeMsa( out, MSA_FORMAT.PHYLIP, ".aln" );
125         }
126         catch ( final Exception e ) {
127             e.printStackTrace();
128             ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
129         }
130     }
131
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 + "\"" );
135         }
136         else {
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" );
140             }
141             else {
142                 ForesterUtil.fatalError( PRG_NAME, "no MAFFT executable at \"" + path_to_mafft + "\"" );
143             }
144         }
145     }
146
147     private static void printHelp() {
148         ForesterUtil.printProgramInformation( PRG_NAME,
149                                               PRG_DESC,
150                                               PRG_VERSION,
151                                               PRG_DATE,
152                                               E_MAIL,
153                                               WWW,
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 + ")";
159         }
160         else {
161             mafft_comment = " (no path to MAFFT found, use -\"" + PATH_TO_MAFFT_OPTION + "=<path to MAFFT>\" option";
162         }
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();
175     }
176 }