4b2aaa243ff28898f0a2a3f65033afa8ea0afcb9
[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.BasicMsa;
35 import org.forester.msa.DeleteableMsa;
36 import org.forester.msa.MsaInferrer;
37 import org.forester.msa_compactor.MsaCompactor;
38 import org.forester.util.CommandLineArguments;
39 import org.forester.util.ForesterUtil;
40
41 public class msa_compactor {
42
43     final static private String HELP_OPTION_1                          = "help";
44     final static private String HELP_OPTION_2                          = "h";
45     final static private String REMOVE_WORST_OFFENDERS_OPTION          = "r";
46     final static private String AV_GAPINESS_OPTION                     = "g";
47     final static private String STEP_OPTION                            = "s";
48     final static private String LENGTH_OPTION                          = "l";
49     final static private String REALIGN_OPTION                         = "a";
50     final static private String PATH_TO_MAFFT_OPTION                   = "mafft";
51     final static private String DO_NOT_NORMALIZE_FOR_EFF_LENGTH_OPTION = "nn";
52     final static private String PRG_NAME                               = "msa_compactor";
53     final static private String PRG_DESC                               = "multiple sequence aligment compactor";
54     final static private String PRG_VERSION                            = "0.01";
55     final static private String PRG_DATE                               = "140316";
56     final static private String E_MAIL                                 = "phylosoft@gmail.com";
57     final static private String WWW                                    = "https://sites.google.com/site/cmzmasek/home/software/forester";
58
59     public static void main( final String args[] ) {
60         try {
61             final CommandLineArguments cla = new CommandLineArguments( args );
62             if ( cla.isOptionSet( HELP_OPTION_1 ) || cla.isOptionSet( HELP_OPTION_2 )
63                     || ( ( cla.getNumberOfNames() < 1 ) || ( cla.getNumberOfNames() > 2 ) ) ) {
64                 printHelp();
65                 System.exit( 0 );
66             }
67             final File in = cla.getFile( 0 );
68             File out = null;
69             if ( cla.getNumberOfNames() > 1 ) {
70                 out = cla.getFile( 1 );
71             }
72             int worst_remove = -1;
73             double av_gap = -1;
74             int length = -1;
75             int step = -1;
76             boolean realign = false;
77             boolean norm = true;
78             String path_to_mafft = null;
79             final List<String> allowed_options = new ArrayList<String>();
80             allowed_options.add( REMOVE_WORST_OFFENDERS_OPTION );
81             allowed_options.add( AV_GAPINESS_OPTION );
82             allowed_options.add( LENGTH_OPTION );
83             allowed_options.add( REALIGN_OPTION );
84             allowed_options.add( DO_NOT_NORMALIZE_FOR_EFF_LENGTH_OPTION );
85             allowed_options.add( STEP_OPTION );
86             allowed_options.add( PATH_TO_MAFFT_OPTION );
87             final String dissallowed_options = cla.validateAllowedOptionsAsString( allowed_options );
88             if ( dissallowed_options.length() > 0 ) {
89                 ForesterUtil.fatalError( PRG_NAME, "unknown option(s): " + dissallowed_options );
90             }
91             DeleteableMsa msa = null;
92             final FileInputStream is = new FileInputStream( in );
93             if ( FastaParser.isLikelyFasta( in ) ) {
94                 msa = new DeleteableMsa( ( BasicMsa ) FastaParser.parseMsa( is ) );
95             }
96             else {
97                 msa = new DeleteableMsa( ( BasicMsa ) GeneralMsaParser.parse( is ) );
98             }
99             if ( cla.isOptionSet( REMOVE_WORST_OFFENDERS_OPTION ) ) {
100                 worst_remove = cla.getOptionValueAsInt( REMOVE_WORST_OFFENDERS_OPTION );
101                 if ( ( worst_remove < 1 ) || ( worst_remove >= msa.getNumberOfSequences() - 1 ) ) {
102                     ForesterUtil.fatalError( PRG_NAME, "number of worst offender sequences to remove is out of range: "
103                             + worst_remove );
104                 }
105             }
106             if ( cla.isOptionSet( AV_GAPINESS_OPTION ) ) {
107                 if ( cla.isOptionSet( REMOVE_WORST_OFFENDERS_OPTION ) ) {
108                     printHelp();
109                     System.exit( 0 );
110                 }
111                 av_gap = cla.getOptionValueAsDouble( AV_GAPINESS_OPTION );
112                 if ( ( av_gap < 0 ) || ( av_gap >= 1 ) ) {
113                     ForesterUtil.fatalError( PRG_NAME, "target gap-ratio is out of range: " + av_gap );
114                 }
115             }
116             if ( cla.isOptionSet( LENGTH_OPTION ) ) {
117                 if ( cla.isOptionSet( REMOVE_WORST_OFFENDERS_OPTION ) || cla.isOptionSet( AV_GAPINESS_OPTION ) ) {
118                     printHelp();
119                     System.exit( 0 );
120                 }
121                 length = cla.getOptionValueAsInt( LENGTH_OPTION );
122                 if ( ( length < 2 ) || ( length >= msa.getLength() ) ) {
123                     ForesterUtil.fatalError( PRG_NAME, "target length is out of range: " + length );
124                 }
125             }
126             if ( cla.isOptionSet( STEP_OPTION ) ) {
127                 step = cla.getOptionValueAsInt( STEP_OPTION );
128                 if ( ( step < 1 )
129                         || ( ( step > msa.getNumberOfSequences() ) || ( ( worst_remove > 0 ) && ( step > worst_remove ) ) ) ) {
130                     ForesterUtil.fatalError( PRG_NAME, "value for step is out of range: " + step );
131                 }
132             }
133             if ( cla.isOptionSet( REALIGN_OPTION ) ) {
134                 realign = true;
135             }
136             if ( cla.isOptionSet( PATH_TO_MAFFT_OPTION ) ) {
137                 if ( !realign ) {
138                     ForesterUtil.fatalError( PRG_NAME, "no need to indicate path to MAFFT without realigning" );
139                 }
140                 path_to_mafft = cla.getOptionValueAsCleanString( PATH_TO_MAFFT_OPTION );
141             }
142             if ( cla.isOptionSet( DO_NOT_NORMALIZE_FOR_EFF_LENGTH_OPTION ) ) {
143                 norm = false;
144             }
145             if ( realign ) {
146                 if ( ForesterUtil.isEmpty( path_to_mafft ) ) {
147                     path_to_mafft = MsaCompactor.guessPathToMafft();
148                 }
149                 checkPathToMafft( path_to_mafft );
150             }
151             if ( worst_remove > 0 ) {
152                 MsaCompactor.removeWorstOffenders( msa, worst_remove, step, realign, norm, path_to_mafft, out );
153             }
154             else if ( av_gap > 0 ) {
155                 MsaCompactor.reduceGapAverage( msa, av_gap, step, realign, norm, path_to_mafft, out );
156             }
157             else if ( length > 0 ) {
158                 // TODO if < shortest seq -> error
159                 MsaCompactor.reduceLength( msa, length, step, realign, norm, path_to_mafft, out );
160             }
161             else {
162                 MsaCompactor.chart( msa, step, realign, norm, path_to_mafft );
163             }
164         }
165         catch ( final Exception e ) {
166             e.printStackTrace();
167             ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
168         }
169     }
170
171     private static void checkPathToMafft( final String path_to_mafft ) {
172         if ( !ForesterUtil.isEmpty( path_to_mafft ) && MsaInferrer.isInstalled( path_to_mafft ) ) {
173             ForesterUtil.programMessage( PRG_NAME, "using MAFFT at \"" + path_to_mafft + "\"" );
174         }
175         else {
176             if ( ForesterUtil.isEmpty( path_to_mafft ) ) {
177                 ForesterUtil.fatalError( PRG_NAME, "no MAFFT executable found, use -\"" + PATH_TO_MAFFT_OPTION
178                         + "=<path to MAFFT>\" option" );
179             }
180             else {
181                 ForesterUtil.fatalError( PRG_NAME, "no MAFFT executable at \"" + path_to_mafft + "\"" );
182             }
183         }
184     }
185
186     private static void printHelp() {
187         ForesterUtil.printProgramInformation( PRG_NAME,
188                                               PRG_DESC,
189                                               PRG_VERSION,
190                                               PRG_DATE,
191                                               E_MAIL,
192                                               WWW,
193                                               ForesterUtil.getForesterLibraryInformation() );
194         final String path_to_mafft = MsaCompactor.guessPathToMafft();
195         String mafft_comment;
196         if ( !ForesterUtil.isEmpty( path_to_mafft ) ) {
197             mafft_comment = " (using " + path_to_mafft + ")";
198         }
199         else {
200             mafft_comment = " (no path to MAFFT found, use -\"" + PATH_TO_MAFFT_OPTION + "=<path to MAFFT>\" option";
201         }
202         System.out.println( "Usage:" );
203         System.out.println();
204         System.out.println( PRG_NAME + " <options> <msa input file> <output file>" );
205         System.out.println();
206         System.out.println( " options: " );
207         System.out.println();
208         System.out.println( "   -" + REMOVE_WORST_OFFENDERS_OPTION
209                 + "=<integer>  number of worst offender sequences to remove" );
210         System.out.println( "   -" + LENGTH_OPTION + "=<integer>  target MSA length" );
211         System.out.println( "   -" + AV_GAPINESS_OPTION + "=<decimal>  target gap-ratio (0.0-1.0)" );
212         System.out.println( "   -" + STEP_OPTION + "=<integer>  step (for output and re-aligning)" );
213         System.out.println( "   -" + REALIGN_OPTION + "            to realign using MAFFT" + mafft_comment );
214         System.out.println();
215         System.out.println();
216         System.out.println();
217     }
218 }