in progress
[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             Msa msa = null;
98             final FileInputStream is = new FileInputStream( in );
99             if ( FastaParser.isLikelyFasta( in ) ) {
100                 msa = FastaParser.parseMsa( is );
101             }
102             else {
103                 msa = GeneralMsaParser.parse( is );
104             }
105             
106             if ( realign ) {
107                 if ( ForesterUtil.isEmpty( path_to_mafft ) ) {
108                     path_to_mafft = MsaCompactor.guessPathToMafft();
109                 }
110                 if ( !ForesterUtil.isEmpty( path_to_mafft ) && MsaInferrer.isInstalled( path_to_mafft ) ) {
111                     ForesterUtil.programMessage( PRG_NAME, "using MAFFT at \"" + path_to_mafft + "\""  );
112                 }
113                 else {
114                     if ( ForesterUtil.isEmpty( path_to_mafft ) ) {
115                         ForesterUtil.fatalError( PRG_NAME, "no MAFFT executable found, use -\"" + PATH_TO_MAFFT_OPTION + "=<path to MAFFT>\" option" );
116                     }
117                     else {
118                         ForesterUtil.fatalError( PRG_NAME, "no MAFFT executable at \""  + path_to_mafft + "\""  );
119                     }
120                 }
121             }
122             
123             MsaCompactor mc = null;
124             if ( worst_remove > 0 ) {
125                 mc = MsaCompactor.removeWorstOffenders( msa, worst_remove, realign, norm );
126             }
127             else if ( av > 0 ) {
128                 mc = MsaCompactor.reduceGapAverage( msa, av, step, realign, out, 50 );
129             }
130             else if ( length > 0 ) {
131                 mc = MsaCompactor.reduceLength( msa, length, step, realign );
132             }
133             System.out.println( MsaMethods.calcGapRatio( mc.getMsa() ) );
134             for( final String id : mc.getRemovedSeqIds() ) {
135                 System.out.println( id );
136             }
137             mc.writeMsa( out, MSA_FORMAT.PHYLIP, ".aln" );
138         }
139         catch ( final Exception e ) {
140             e.printStackTrace();
141             ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
142         }
143     }
144
145     private static void printHelp() {
146         ForesterUtil.printProgramInformation( PRG_NAME,
147                                               PRG_DESC,
148                                               PRG_VERSION,
149                                               PRG_DATE,
150                                               E_MAIL,
151                                               WWW,
152                                               ForesterUtil.getForesterLibraryInformation() );
153         final String path_to_mafft = MsaCompactor.guessPathToMafft();
154         String mafft_comment;
155         if ( !ForesterUtil.isEmpty( path_to_mafft ) ) {
156             mafft_comment = " (" + path_to_mafft + ")";
157         }
158         else {
159             mafft_comment = " (no path to MAFFT found, use -\"" + PATH_TO_MAFFT_OPTION + "=<path to MAFFT>\" option";
160         }
161         
162         System.out.println( "Usage:" );
163         System.out.println();
164         System.out.println( PRG_NAME + " <options> <msa input file>" );
165         System.out.println();
166         System.out.println( " options: " );
167         System.out.println();
168         System.out.println( "   -" + REMOVE_WORST_OFFENDERS_OPTION + "=<integer>  number of sequences to remove" );
169         System.out.println( "   -" + REALIGN_OPTION + " to realign using MAFFT" + mafft_comment );
170         System.out.println();
171         System.out.println();
172         System.out.println();
173     }
174 }