in progress
[jalview.git] / forester / java / src / org / forester / application / msa_quality.java
1 // $Id:
2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
4 //
5 // Copyright (C) 2012 Christian M. Zmasek
6 // Copyright (C) 2012 Sanford Burnham Medical Research Institute
7 // All rights reserved
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 //
23 // Contact: phylosoft @ gmail . com
24 // WWW: www.phylosoft.org/forester
25
26 package org.forester.application;
27
28 import java.io.File;
29 import java.io.FileInputStream;
30
31 import org.forester.io.parsers.GeneralMsaParser;
32 import org.forester.msa.Msa;
33 import org.forester.msa.MsaMethods;
34 import org.forester.util.BasicDescriptiveStatistics;
35 import org.forester.util.CommandLineArguments;
36 import org.forester.util.DescriptiveStatistics;
37 import org.forester.util.ForesterUtil;
38
39 public class msa_quality {
40
41     final static private String HELP_OPTION_1 = "help";
42     final static private String HELP_OPTION_2 = "h";
43     final static private String FROM_OPTION   = "f";
44     final static private String TO_OPTION     = "t";
45     final static private String STEP_OPTION   = "s";
46     final static private String WINDOW_OPTION = "w";
47     final static private String PRG_NAME      = "msa_quality";
48     final static private String PRG_VERSION   = "1.00";
49     final static private String PRG_DATE      = "2012.05.18";
50     final static private String E_MAIL        = "phylosoft@gmail.com";
51     final static private String WWW           = "www.phylosoft.org/forester/";
52
53     public static void main( final String args[] ) {
54         try {
55             final CommandLineArguments cla = new CommandLineArguments( args );
56             if ( cla.isOptionSet( HELP_OPTION_1 ) || cla.isOptionSet( HELP_OPTION_2 ) || ( args.length != 3 ) ) {
57                 printHelp();
58                 System.exit( 0 );
59             }
60             final File in = cla.getFile( 0 );
61             int from = 0;
62             int to = 0;
63             int window = 0;
64             int step = 0;
65             if ( cla.isOptionSet( FROM_OPTION ) && cla.isOptionSet( TO_OPTION ) ) {
66                 from = cla.getOptionValueAsInt( FROM_OPTION );
67                 to = cla.getOptionValueAsInt( TO_OPTION );
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             msa = GeneralMsaParser.parse( new FileInputStream( in ) );
79             if ( cla.isOptionSet( FROM_OPTION ) ) {
80                 singleCalc( in, from, to, msa );
81             }
82             else {
83                 windowedCalcs( window, step, msa );
84             }
85         }
86         catch ( final Exception e ) {
87             ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
88         }
89     }
90
91     private static void printHelp() {
92         ForesterUtil.printProgramInformation( PRG_NAME, PRG_VERSION, PRG_DATE, E_MAIL, WWW );
93         System.out.println( "Usage:" );
94         System.out.println();
95         System.out.println( PRG_NAME + " <options> <msa input file>" );
96         System.out.println();
97         System.out.println( " options: " );
98         System.out.println();
99         System.out.println( "   -" + FROM_OPTION + "=<integer>: from (msa column)" );
100         System.out.println( "   -" + TO_OPTION + "=<integer>: to (msa column)" );
101         System.out.println( "    or" );
102         System.out.println( "   -" + WINDOW_OPTION + "=<integer>: window size (msa columns)" );
103         System.out.println( "   -" + STEP_OPTION + "=<integer>: step size (msa columns)" );
104         System.out.println();
105         System.out.println();
106         System.out.println();
107     }
108
109     private static void windowedCalcs( int window, int step, final Msa msa ) {
110         if ( window < 1 ) {
111             window = 1;
112         }
113         if ( step < 1 ) {
114             step = 1;
115         }
116         final double id_ratios[] = new double[ msa.getLength() ];
117         for( int i = 0; i <= msa.getLength() - 1; ++i ) {
118             id_ratios[ i ] = MsaMethods.calculateIdentityRatio( msa, i );
119         }
120         String min_pos = "";
121         String max_pos = "";
122         double min = 1;
123         double max = 0;
124         for( int i = 0; i <= msa.getLength() - 1; i += step ) {
125             int to = i + window - 1;
126             if ( to > ( msa.getLength() - 1 ) ) {
127                 to = msa.getLength() - 1;
128             }
129             final DescriptiveStatistics stats = calc( i, to, id_ratios );
130             final double mean = stats.arithmeticMean();
131             final String pos = i + "-" + to;
132             System.out.print( pos );
133             System.out.print( ":\t" );
134             System.out.print( mean );
135             if ( stats.getN() > 2 ) {
136                 System.out.print( "\t" );
137                 System.out.print( stats.median() );
138                 System.out.print( "\t" );
139                 System.out.print( stats.sampleStandardDeviation() );
140             }
141             System.out.println();
142             if ( mean > max ) {
143                 max = mean;
144                 max_pos = pos;
145             }
146             if ( mean < min ) {
147                 min = mean;
148                 min_pos = pos;
149             }
150         }
151         System.out.println( "Min: " + min_pos + ": " + min );
152         System.out.println( "Max: " + max_pos + ": " + max );
153     }
154
155     private static void singleCalc( final File in, int from, int to, final Msa msa ) {
156         if ( from < 0 ) {
157             from = 0;
158         }
159         if ( to > ( msa.getLength() - 1 ) ) {
160             to = msa.getLength() - 1;
161         }
162         final DescriptiveStatistics stats = calc( from, to, msa );
163         System.out.println( in.toString() + ": " + from + "-" + to + ":" );
164         System.out.println();
165         System.out.println( stats.toString() );
166     }
167
168     private static DescriptiveStatistics calc( final int from, final int to, final Msa msa ) {
169         final DescriptiveStatistics stats = new BasicDescriptiveStatistics();
170         for( int c = from; c <= to; ++c ) {
171             stats.addValue( MsaMethods.calculateIdentityRatio( msa, c ) );
172         }
173         return stats;
174     }
175
176     private static DescriptiveStatistics calc( final int from, final int to, final double id_ratios[] ) {
177         final DescriptiveStatistics stats = new BasicDescriptiveStatistics();
178         for( int c = from; c <= to; ++c ) {
179             stats.addValue( id_ratios[ c ] );
180         }
181         return stats;
182     }
183 }