(no commit message)
[jalview.git] / forester / java / src / org / forester / application / msa_compactor.java
1 // $Id:
2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
4 //
5 // Copyright (C) 2014 Christian M. Zmasek
6 // Copyright (C) 2014 Sanford-Burnham Medical Research Institute
7 // All rights reserved
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 //
23 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
24
25 package org.forester.application;
26
27 import java.io.File;
28 import java.io.FileInputStream;
29 import java.util.ArrayList;
30 import java.util.List;
31
32 import org.forester.io.parsers.FastaParser;
33 import org.forester.io.parsers.GeneralMsaParser;
34 import org.forester.msa.Msa;
35 import org.forester.msa.MsaInferrer;
36 import org.forester.msa_compactor.MsaCompactor;
37 import org.forester.util.CommandLineArguments;
38 import org.forester.util.ForesterUtil;
39
40 public class msa_compactor {
41
42     final static private String HELP_OPTION_1                          = "help";
43     final static private String HELP_OPTION_2                          = "h";
44     final static private String REMOVE_WORST_OFFENDERS_OPTION          = "r";
45     final static private String AV_GAPINESS_OPTION                     = "g";
46     final static private String STEP_OPTION                            = "s";
47     final static private String LENGTH_OPTION                          = "l";
48     final static private String REALIGN_OPTION                         = "a";
49     final static private String PATH_TO_MAFFT_OPTION                   = "mafft";
50     final static private String DO_NOT_NORMALIZE_FOR_EFF_LENGTH_OPTION = "nn";
51     final static private String PRG_NAME                               = "msa_compactor";
52     final static private String PRG_DESC                               = "multiple sequence aligment compactor";
53     final static private String PRG_VERSION                            = "0.01";
54     final static private String PRG_DATE                               = "140316";
55     final static private String E_MAIL                                 = "phylosoft@gmail.com";
56     final static private String WWW                                    = "https://sites.google.com/site/cmzmasek/home/software/forester";
57
58     public static void main( final String args[] ) {
59         try {
60             final CommandLineArguments cla = new CommandLineArguments( args );
61             if ( cla.isOptionSet( HELP_OPTION_1 ) || cla.isOptionSet( HELP_OPTION_2 )
62                     || ( ( cla.getNumberOfNames() < 1 ) || ( cla.getNumberOfNames() > 2 ) ) ) {
63                 printHelp();
64                 System.exit( 0 );
65             }
66             final File in = cla.getFile( 0 );
67             File out = null;
68             if ( cla.getNumberOfNames() > 1 ) {
69                 out = cla.getFile( 1 );
70             }
71             int worst_remove = -1;
72             double av_gap = -1;
73             int length = -1;
74             int step = -1;
75             boolean realign = false;
76             boolean norm = true;
77             String path_to_mafft = null;
78             final List<String> allowed_options = new ArrayList<String>();
79             allowed_options.add( REMOVE_WORST_OFFENDERS_OPTION );
80             allowed_options.add( AV_GAPINESS_OPTION );
81             allowed_options.add( LENGTH_OPTION );
82             allowed_options.add( REALIGN_OPTION );
83             allowed_options.add( DO_NOT_NORMALIZE_FOR_EFF_LENGTH_OPTION );
84             allowed_options.add( STEP_OPTION );
85             allowed_options.add( PATH_TO_MAFFT_OPTION );
86             final String dissallowed_options = cla.validateAllowedOptionsAsString( allowed_options );
87             if ( dissallowed_options.length() > 0 ) {
88                 ForesterUtil.fatalError( PRG_NAME, "unknown option(s): " + dissallowed_options );
89             }
90             Msa msa = null;
91             final FileInputStream is = new FileInputStream( in );
92             if ( FastaParser.isLikelyFasta( in ) ) {
93                 msa = FastaParser.parseMsa( is );
94             }
95             else {
96                 msa = GeneralMsaParser.parse( is );
97             }
98             if ( cla.isOptionSet( REMOVE_WORST_OFFENDERS_OPTION ) ) {
99                 worst_remove = cla.getOptionValueAsInt( REMOVE_WORST_OFFENDERS_OPTION );
100                 if ( ( worst_remove < 1 ) || ( worst_remove >= msa.getNumberOfSequences() - 1 ) ) {
101                     ForesterUtil.fatalError( PRG_NAME, "number of worst offender sequences to remove is out of range: "
102                             + worst_remove );
103                 }
104             }
105             if ( cla.isOptionSet( AV_GAPINESS_OPTION ) ) {
106                 if ( cla.isOptionSet( REMOVE_WORST_OFFENDERS_OPTION ) ) {
107                     printHelp();
108                     System.exit( 0 );
109                 }
110                 av_gap = cla.getOptionValueAsDouble( AV_GAPINESS_OPTION );
111                 if ( ( av_gap < 0 ) || ( av_gap >= 1 ) ) {
112                     ForesterUtil.fatalError( PRG_NAME, "target gap-ratio is out of range: " + av_gap );
113                 }
114             }
115             if ( cla.isOptionSet( LENGTH_OPTION ) ) {
116                 if ( cla.isOptionSet( REMOVE_WORST_OFFENDERS_OPTION ) || cla.isOptionSet( AV_GAPINESS_OPTION ) ) {
117                     printHelp();
118                     System.exit( 0 );
119                 }
120                 length = cla.getOptionValueAsInt( LENGTH_OPTION );
121                 if ( ( length < 2 ) || ( length >= msa.getLength() ) ) {
122                     ForesterUtil.fatalError( PRG_NAME, "target length is out of range: " + length );
123                 }
124             }
125             if ( cla.isOptionSet( STEP_OPTION ) ) {
126                 step = cla.getOptionValueAsInt( STEP_OPTION );
127                 if ( ( step < 1 )
128                         || ( ( step > msa.getNumberOfSequences() ) || ( ( worst_remove > 0 ) && ( step > worst_remove ) ) ) ) {
129                     ForesterUtil.fatalError( PRG_NAME, "value for step is out of range: " + step );
130                 }
131             }
132             if ( cla.isOptionSet( REALIGN_OPTION ) ) {
133                 realign = true;
134             }
135             if ( cla.isOptionSet( PATH_TO_MAFFT_OPTION ) ) {
136                 if ( !realign ) {
137                     ForesterUtil.fatalError( PRG_NAME, "no need to indicate path to MAFFT without realigning" );
138                 }
139                 path_to_mafft = cla.getOptionValueAsCleanString( PATH_TO_MAFFT_OPTION );
140             }
141             if ( cla.isOptionSet( DO_NOT_NORMALIZE_FOR_EFF_LENGTH_OPTION ) ) {
142                 norm = false;
143             }
144             if ( realign ) {
145                 if ( ForesterUtil.isEmpty( path_to_mafft ) ) {
146                     path_to_mafft = MsaCompactor.guessPathToMafft();
147                 }
148                 checkPathToMafft( path_to_mafft );
149             }
150             if ( worst_remove > 0 ) {
151                 MsaCompactor.removeWorstOffenders( msa, worst_remove, step, realign, norm, path_to_mafft, out );
152             }
153             else if ( av_gap > 0 ) {
154                 MsaCompactor.reduceGapAverage( msa, av_gap, step, realign, norm, path_to_mafft, out );
155             }
156             else if ( length > 0 ) {
157                 // TODO if < shortest seq -> error
158                 MsaCompactor.reduceLength( msa, length, step, realign, norm, path_to_mafft, out );
159             }
160             else {
161                 MsaCompactor.chart( msa, realign, norm, path_to_mafft );
162             }
163         }
164         catch ( final Exception e ) {
165             e.printStackTrace();
166             ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
167         }
168     }
169
170     private static void checkPathToMafft( final String path_to_mafft ) {
171         if ( !ForesterUtil.isEmpty( path_to_mafft ) && MsaInferrer.isInstalled( path_to_mafft ) ) {
172             ForesterUtil.programMessage( PRG_NAME, "using MAFFT at \"" + path_to_mafft + "\"" );
173         }
174         else {
175             if ( ForesterUtil.isEmpty( path_to_mafft ) ) {
176                 ForesterUtil.fatalError( PRG_NAME, "no MAFFT executable found, use -\"" + PATH_TO_MAFFT_OPTION
177                         + "=<path to MAFFT>\" option" );
178             }
179             else {
180                 ForesterUtil.fatalError( PRG_NAME, "no MAFFT executable at \"" + path_to_mafft + "\"" );
181             }
182         }
183     }
184
185     private static void printHelp() {
186         ForesterUtil.printProgramInformation( PRG_NAME,
187                                               PRG_DESC,
188                                               PRG_VERSION,
189                                               PRG_DATE,
190                                               E_MAIL,
191                                               WWW,
192                                               ForesterUtil.getForesterLibraryInformation() );
193         final String path_to_mafft = MsaCompactor.guessPathToMafft();
194         String mafft_comment;
195         if ( !ForesterUtil.isEmpty( path_to_mafft ) ) {
196             mafft_comment = " (using " + path_to_mafft + ")";
197         }
198         else {
199             mafft_comment = " (no path to MAFFT found, use -\"" + PATH_TO_MAFFT_OPTION + "=<path to MAFFT>\" option";
200         }
201         System.out.println( "Usage:" );
202         System.out.println();
203         System.out.println( PRG_NAME + " <options> <msa input file> <output file>" );
204         System.out.println();
205         System.out.println( " options: " );
206         System.out.println();
207         System.out.println( "   -" + REMOVE_WORST_OFFENDERS_OPTION
208                 + "=<integer>  number of worst offender sequences to remove" );
209         System.out.println( "   -" + LENGTH_OPTION + "=<integer>  target MSA length" );
210         System.out.println( "   -" + AV_GAPINESS_OPTION + "=<decimal>  target gap-ratio (0.0-1.0)" );
211         System.out.println( "   -" + STEP_OPTION + "=<integer>  step (for output and re-aligning)" );
212         System.out.println( "   -" + REALIGN_OPTION + "            to realign using MAFFT" + mafft_comment );
213         System.out.println();
214         System.out.println();
215         System.out.println();
216     }
217 }