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.Msa.MSA_FORMAT;
13 import org.forester.msa.MsaMethods;
14 import org.forester.msa_compactor.MsaCompactor;
15 import org.forester.util.CommandLineArguments;
16 import org.forester.util.ForesterUtil;
17
18 public class msa_compactor {
19
20     final static private String HELP_OPTION_1                          = "help";
21     final static private String HELP_OPTION_2                          = "h";
22     final static private String REMOVE_WORST_OFFENDERS_OPTION          = "w";
23     final static private String AV_GAPINESS_OPTION                     = "a";
24     final static private String STEP_OPTION                            = "s";
25     final static private String LENGTH_OPTION                          = "l";
26     final static private String REALIGN_OPTION                         = "r";
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             final List<String> allowed_options = new ArrayList<String>();
51             allowed_options.add( REMOVE_WORST_OFFENDERS_OPTION );
52             allowed_options.add( AV_GAPINESS_OPTION );
53             allowed_options.add( LENGTH_OPTION );
54             allowed_options.add( REALIGN_OPTION );
55             allowed_options.add( DO_NOT_NORMALIZE_FOR_EFF_LENGTH_OPTION );
56             allowed_options.add( STEP_OPTION );
57             final String dissallowed_options = cla.validateAllowedOptionsAsString( allowed_options );
58             if ( dissallowed_options.length() > 0 ) {
59                 ForesterUtil.fatalError( PRG_NAME, "unknown option(s): " + dissallowed_options );
60             }
61             if ( cla.isOptionSet( REMOVE_WORST_OFFENDERS_OPTION ) ) {
62                 worst_remove = cla.getOptionValueAsInt( REMOVE_WORST_OFFENDERS_OPTION );
63             }
64             if ( cla.isOptionSet( AV_GAPINESS_OPTION ) ) {
65                 av = cla.getOptionValueAsDouble( AV_GAPINESS_OPTION );
66             }
67             if ( cla.isOptionSet( LENGTH_OPTION ) ) {
68                 length = cla.getOptionValueAsInt( LENGTH_OPTION );
69             }
70             if ( cla.isOptionSet( STEP_OPTION ) ) {
71                 step = cla.getOptionValueAsInt( STEP_OPTION );
72             }
73             if ( cla.isOptionSet( REALIGN_OPTION ) ) {
74                 realign = true;
75             }
76             if ( cla.isOptionSet( DO_NOT_NORMALIZE_FOR_EFF_LENGTH_OPTION ) ) {
77                 norm = false;
78             }
79             //            else if ( cla.isOptionSet( STEP_OPTION ) && cla.isOptionSet( WINDOW_OPTION ) ) {
80             //                step = cla.getOptionValueAsInt( STEP_OPTION );
81             //                window = cla.getOptionValueAsInt( WINDOW_OPTION );
82             //            }
83             //            else {
84             //                printHelp();
85             //                System.exit( 0 );
86             //            }
87             Msa msa = null;
88             final FileInputStream is = new FileInputStream( in );
89             if ( FastaParser.isLikelyFasta( in ) ) {
90                 msa = FastaParser.parseMsa( is );
91             }
92             else {
93                 msa = GeneralMsaParser.parse( is );
94             }
95             MsaCompactor mc = null;
96             if ( worst_remove > 0 ) {
97                 mc = MsaCompactor.removeWorstOffenders( msa, worst_remove, realign, norm );
98             }
99             else if ( av > 0 ) {
100                 mc = MsaCompactor.reduceGapAverage( msa, av, step, realign, out, 50 );
101             }
102             else if ( length > 0 ) {
103                 mc = MsaCompactor.reduceLength( msa, length, step, realign );
104             }
105             System.out.println( MsaMethods.calcGapRatio( mc.getMsa() ) );
106             for( final String id : mc.getRemovedSeqIds() ) {
107                 System.out.println( id );
108             }
109             mc.writeMsa( out, MSA_FORMAT.PHYLIP, ".aln" );
110         }
111         catch ( final Exception e ) {
112             e.printStackTrace();
113             ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
114         }
115     }
116
117     private static void printHelp() {
118         ForesterUtil.printProgramInformation( PRG_NAME,
119                                               PRG_DESC,
120                                               PRG_VERSION,
121                                               PRG_DATE,
122                                               E_MAIL,
123                                               WWW,
124                                               ForesterUtil.getForesterLibraryInformation() );
125         System.out.println( "Usage:" );
126         System.out.println();
127         System.out.println( PRG_NAME + " <options> <msa input file>" );
128         System.out.println();
129         System.out.println( " options: " );
130         System.out.println();
131         System.out.println( "   -" + REMOVE_WORST_OFFENDERS_OPTION + "=<integer>  number of sequences to remove" );
132         System.out.println( "   -" + REALIGN_OPTION + " to realign using " );
133         System.out.println();
134         System.out.println();
135         System.out.println();
136     }
137 }