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.MsaCompactor;
13 import org.forester.msa.MsaMethods;
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 = "w";
22     final static private String AV_GAPINESS_OPTION            = "a";
23     final static private String LENGTH_OPTION                 = "l";
24     final static private String REALIGN_OPTION                = "r";
25     final static private String PRG_NAME                      = "msa_compactor";
26     final static private String PRG_DESC                      = "multiple sequnce aligment compactor";
27     final static private String PRG_VERSION                   = "0.90";
28     final static private String PRG_DATE                      = "2012.07.11";
29     final static private String E_MAIL                        = "phylosoft@gmail.com";
30     final static private String WWW                           = "www.phylosoft.org/forester/";
31
32     public static void main( final String args[] ) {
33         try {
34             final CommandLineArguments cla = new CommandLineArguments( args );
35             if ( cla.isOptionSet( HELP_OPTION_1 ) || cla.isOptionSet( HELP_OPTION_2 ) ) {
36                 printHelp();
37                 System.exit( 0 );
38             }
39             final File in = cla.getFile( 0 );
40             int worst_remove = -1;
41             double av = -1;
42             int length = -1;
43             final int step = 5;
44             boolean realign = false;
45             //            int to = 0;
46             //            int window = 0;
47             //            int step = 0;
48             final List<String> allowed_options = new ArrayList<String>();
49             allowed_options.add( REMOVE_WORST_OFFENDERS_OPTION );
50             allowed_options.add( AV_GAPINESS_OPTION );
51             allowed_options.add( LENGTH_OPTION );
52             allowed_options.add( REALIGN_OPTION );
53             final String dissallowed_options = cla.validateAllowedOptionsAsString( allowed_options );
54             if ( dissallowed_options.length() > 0 ) {
55                 ForesterUtil.fatalError( PRG_NAME, "unknown option(s): " + dissallowed_options );
56             }
57             if ( cla.isOptionSet( REMOVE_WORST_OFFENDERS_OPTION ) ) {
58                 worst_remove = cla.getOptionValueAsInt( REMOVE_WORST_OFFENDERS_OPTION );
59             }
60             if ( cla.isOptionSet( AV_GAPINESS_OPTION ) ) {
61                 av = cla.getOptionValueAsDouble( AV_GAPINESS_OPTION );
62             }
63             if ( cla.isOptionSet( LENGTH_OPTION ) ) {
64                 length = cla.getOptionValueAsInt( LENGTH_OPTION );
65             }
66             if ( cla.isOptionSet( REALIGN_OPTION ) ) {
67                 realign = true;
68             }
69             //            else if ( cla.isOptionSet( STEP_OPTION ) && cla.isOptionSet( WINDOW_OPTION ) ) {
70             //                step = cla.getOptionValueAsInt( STEP_OPTION );
71             //                window = cla.getOptionValueAsInt( WINDOW_OPTION );
72             //            }
73             //            else {
74             //                printHelp();
75             //                System.exit( 0 );
76             //            }
77             Msa msa = null;
78             final FileInputStream is = new FileInputStream( in );
79             if ( FastaParser.isLikelyFasta( in ) ) {
80                 msa = FastaParser.parseMsa( is );
81             }
82             else {
83                 msa = GeneralMsaParser.parse( is );
84             }
85             System.out.println( msa.toString() );
86             System.out.println( MsaMethods.calcBasicGapinessStatistics( msa ).arithmeticMean() );
87             MsaCompactor mc = null;
88             if ( worst_remove > 0 ) {
89                 mc = MsaCompactor.removeWorstOffenders( msa, worst_remove, realign );
90             }
91             else if ( av > 0 ) {
92                 mc = MsaCompactor.reduceGapAverage( msa, av, step, realign );
93             }
94             else if ( length > 0 ) {
95                 mc = MsaCompactor.reduceLength( msa, length, step, realign );
96             }
97             System.out.println( mc.getMsa().toString() );
98             System.out.println( MsaMethods.calcBasicGapinessStatistics( mc.getMsa() ).arithmeticMean() );
99             for( final String id : mc.getRemovedSeqIds() ) {
100                 System.out.println( id );
101             }
102         }
103         catch ( final Exception e ) {
104             e.printStackTrace();
105             ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
106         }
107     }
108
109     private static void printHelp() {
110         ForesterUtil.printProgramInformation( PRG_NAME,
111                                               PRG_DESC,
112                                               PRG_VERSION,
113                                               PRG_DATE,
114                                               E_MAIL,
115                                               WWW,
116                                               ForesterUtil.getForesterLibraryInformation() );
117         System.out.println( "Usage:" );
118         System.out.println();
119         System.out.println( PRG_NAME + " <options> <msa input file>" );
120         System.out.println();
121         System.out.println( " options: " );
122         System.out.println();
123         //        System.out.println( "   -" + FROM_OPTION + "=<integer>: from (msa column)" );
124         //        System.out.println( "   -" + TO_OPTION + "=<integer>: to (msa column)" );
125         //        System.out.println( "    or" );
126         //        System.out.println( "   -" + WINDOW_OPTION + "=<integer>: window size (msa columns)" );
127         System.out.println( "   -" + REMOVE_WORST_OFFENDERS_OPTION + "=<integer>: step size (msa columns)" );
128         System.out.println();
129         System.out.println();
130         System.out.println();
131     }
132 }