2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
5 // Copyright (C) 2012 Christian M. Zmasek
6 // Copyright (C) 2012 Sanford Burnham Medical Research Institute
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.
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.
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
23 // Contact: phylosoft @ gmail . com
24 // WWW: www.phylosoft.org/forester
26 package org.forester.application;
29 import java.io.FileInputStream;
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;
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 = "mcc";
48 final static private String PRG_DESC = "msa consensus conservation";
49 final static private String PRG_VERSION = "1.00";
50 final static private String PRG_DATE = "2012.05.18";
51 final static private String E_MAIL = "phylosoft@gmail.com";
52 final static private String WWW = "www.phylosoft.org/forester/";
54 public static void main( final String args[] ) {
56 final CommandLineArguments cla = new CommandLineArguments( args );
57 if ( cla.isOptionSet( HELP_OPTION_1 ) || cla.isOptionSet( HELP_OPTION_2 ) || ( args.length != 3 ) ) {
61 final File in = cla.getFile( 0 );
66 if ( cla.isOptionSet( FROM_OPTION ) && cla.isOptionSet( TO_OPTION ) ) {
67 from = cla.getOptionValueAsInt( FROM_OPTION );
68 to = cla.getOptionValueAsInt( TO_OPTION );
70 else if ( cla.isOptionSet( STEP_OPTION ) && cla.isOptionSet( WINDOW_OPTION ) ) {
71 step = cla.getOptionValueAsInt( STEP_OPTION );
72 window = cla.getOptionValueAsInt( WINDOW_OPTION );
79 msa = GeneralMsaParser.parse( new FileInputStream( in ) );
80 if ( cla.isOptionSet( FROM_OPTION ) ) {
81 singleCalc( in, from, to, msa );
84 windowedCalcs( window, step, msa );
87 catch ( final Exception e ) {
88 ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
92 private static void printHelp() {
93 ForesterUtil.printProgramInformation( PRG_NAME,
99 ForesterUtil.getForesterLibraryInformation() );
100 System.out.println( "Usage:" );
101 System.out.println();
102 System.out.println( PRG_NAME + " <options> <msa input file>" );
103 System.out.println();
104 System.out.println( " options: " );
105 System.out.println();
106 System.out.println( " -" + FROM_OPTION + "=<integer>: from (msa column)" );
107 System.out.println( " -" + TO_OPTION + "=<integer>: to (msa column)" );
108 System.out.println( " or" );
109 System.out.println( " -" + WINDOW_OPTION + "=<integer>: window size (msa columns)" );
110 System.out.println( " -" + STEP_OPTION + "=<integer>: step size (msa columns)" );
111 System.out.println();
112 System.out.println();
113 System.out.println();
116 private static void windowedCalcs( int window, int step, final Msa msa ) {
123 final double id_ratios[] = new double[ msa.getLength() ];
124 for( int i = 0; i <= msa.getLength() - 1; ++i ) {
125 id_ratios[ i ] = MsaMethods.calculateIdentityRatio( msa, i );
131 for( int i = 0; i <= msa.getLength() - 1; i += step ) {
132 int to = i + window - 1;
133 if ( to > ( msa.getLength() - 1 ) ) {
134 to = msa.getLength() - 1;
136 final DescriptiveStatistics stats = calc( i, to, id_ratios );
137 final double mean = stats.arithmeticMean();
138 final String pos = i + "-" + to;
139 System.out.print( pos );
140 System.out.print( ":\t" );
141 System.out.print( mean );
142 if ( stats.getN() > 2 ) {
143 System.out.print( "\t" );
144 System.out.print( stats.median() );
145 System.out.print( "\t" );
146 System.out.print( stats.sampleStandardDeviation() );
148 System.out.println();
158 System.out.println( "Min: " + min_pos + ": " + min );
159 System.out.println( "Max: " + max_pos + ": " + max );
162 private static void singleCalc( final File in, int from, int to, final Msa msa ) {
166 if ( to > ( msa.getLength() - 1 ) ) {
167 to = msa.getLength() - 1;
169 final DescriptiveStatistics stats = calc( from, to, msa );
170 System.out.println( in.toString() + ": " + from + "-" + to + ":" );
171 System.out.println();
172 System.out.println( stats.toString() );
175 private static DescriptiveStatistics calc( final int from, final int to, final Msa msa ) {
176 final DescriptiveStatistics stats = new BasicDescriptiveStatistics();
177 for( int c = from; c <= to; ++c ) {
178 stats.addValue( MsaMethods.calculateIdentityRatio( msa, c ) );
183 private static DescriptiveStatistics calc( final int from, final int to, final double id_ratios[] ) {
184 final DescriptiveStatistics stats = new BasicDescriptiveStatistics();
185 for( int c = from; c <= to; ++c ) {
186 stats.addValue( id_ratios[ c ] );