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