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 sequence aligment compactor";
30     final static private String PRG_VERSION                            = "0.01";
31     final static private String PRG_DATE                               = "140316";
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             if ( realign ) {
88                 if ( ForesterUtil.isEmpty( path_to_mafft ) ) {
89                     path_to_mafft = MsaCompactor.guessPathToMafft();
90                 }
91                 checkPathToMafft( path_to_mafft );
92             }
93             Msa msa = null;
94             final FileInputStream is = new FileInputStream( in );
95             if ( FastaParser.isLikelyFasta( in ) ) {
96                 msa = FastaParser.parseMsa( is );
97             }
98             else {
99                 msa = GeneralMsaParser.parse( is );
100             }
101             MsaCompactor mc = null;
102             if ( worst_remove > 0 ) {
103                 mc = MsaCompactor.removeWorstOffenders( msa, worst_remove, step, realign, norm, path_to_mafft, out );
104             }
105             else if ( av > 0 ) {
106                 mc = MsaCompactor.reduceGapAverage( msa, av, step, realign, norm, path_to_mafft, out );
107             }
108             else if ( length > 0 ) {
109                 if ( length >= msa.getLength() ) {
110                     ForesterUtil.fatalError( PRG_NAME, "target MSA length (" + length
111                             + ") is greater than or equal to MSA original length (" + msa.getLength() + ")" );
112                 }
113                 // TODO if < shortest seq -> error
114                 mc = MsaCompactor.reduceLength( msa, length, step, realign, norm, path_to_mafft, out );
115             }
116         }
117         catch ( final Exception e ) {
118             e.printStackTrace();
119             ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
120         }
121     }
122
123     private static void checkPathToMafft( final String path_to_mafft ) {
124         if ( !ForesterUtil.isEmpty( path_to_mafft ) && MsaInferrer.isInstalled( path_to_mafft ) ) {
125             ForesterUtil.programMessage( PRG_NAME, "using MAFFT at \"" + path_to_mafft + "\"" );
126         }
127         else {
128             if ( ForesterUtil.isEmpty( path_to_mafft ) ) {
129                 ForesterUtil.fatalError( PRG_NAME, "no MAFFT executable found, use -\"" + PATH_TO_MAFFT_OPTION
130                         + "=<path to MAFFT>\" option" );
131             }
132             else {
133                 ForesterUtil.fatalError( PRG_NAME, "no MAFFT executable at \"" + path_to_mafft + "\"" );
134             }
135         }
136     }
137
138     private static void printHelp() {
139         ForesterUtil.printProgramInformation( PRG_NAME,
140                                               PRG_DESC,
141                                               PRG_VERSION,
142                                               PRG_DATE,
143                                               E_MAIL,
144                                               WWW,
145                                               ForesterUtil.getForesterLibraryInformation() );
146         final String path_to_mafft = MsaCompactor.guessPathToMafft();
147         String mafft_comment;
148         if ( !ForesterUtil.isEmpty( path_to_mafft ) ) {
149             mafft_comment = " (using " + path_to_mafft + ")";
150         }
151         else {
152             mafft_comment = " (no path to MAFFT found, use -\"" + PATH_TO_MAFFT_OPTION + "=<path to MAFFT>\" option";
153         }
154         System.out.println( "Usage:" );
155         System.out.println();
156         System.out.println( PRG_NAME + " <options> <msa input file> <output file>" );
157         System.out.println();
158         System.out.println( " options: " );
159         System.out.println();
160         System.out.println( "   -" + REMOVE_WORST_OFFENDERS_OPTION
161                 + "=<integer>  number of worst offender sequences to remove" );
162         System.out.println( "   -" + LENGTH_OPTION + "=<integer>  target MSA length" );
163         System.out.println( "   -" + AV_GAPINESS_OPTION + "=<decimal>  target gap-ratio (0.0-1.0)" );
164         System.out.println( "   -" + STEP_OPTION + "=<integer>  step (for output and re-aligning)" );
165         System.out.println( "   -" + REALIGN_OPTION + "            to realign using MAFFT" + mafft_comment );
166         System.out.println();
167         System.out.println();
168         System.out.println();
169     }
170 }