in progress
[jalview.git] / forester / java / src / org / forester / application / msa_quality.java
1
2 package org.forester.application;
3
4 import java.io.File;
5 import java.io.FileInputStream;
6
7 import org.forester.io.parsers.GeneralMsaParser;
8 import org.forester.msa.Msa;
9 import org.forester.msa.MsaMethods;
10 import org.forester.util.BasicDescriptiveStatistics;
11 import org.forester.util.CommandLineArguments;
12 import org.forester.util.DescriptiveStatistics;
13
14 public class msa_quality {
15
16     public static void main( final String args[] ) {
17         try {
18             final CommandLineArguments cla = new CommandLineArguments( args );
19             // if ( cla.isOptionSet( HELP_OPTION_1 ) || cla.isOptionSet( HELP_OPTION_2 ) || ( args.length == 0 ) ) {
20             //     printHelp();
21             //     System.exit( 0 );
22             // }
23             final File in = cla.getFile( 0 );
24             int from = 0;
25             int to = 0;
26             int window = 0;
27             int step = 0;
28             if ( cla.isOptionSet( "f" ) && cla.isOptionSet( "t" ) ) {
29                 from = cla.getOptionValueAsInt( "f" );
30                 to = cla.getOptionValueAsInt( "t" );
31             }
32             else if ( cla.isOptionSet( "s" ) && cla.isOptionSet( "w" ) ) {
33                 step = cla.getOptionValueAsInt( "s" );
34                 window = cla.getOptionValueAsInt( "w" );
35             }
36             else {
37             }
38             Msa msa = null;
39             msa = GeneralMsaParser.parse( new FileInputStream( in ) );
40             if ( cla.isOptionSet( "f" ) && cla.isOptionSet( "t" ) ) {
41                 singleCalc( in, from, to, msa );
42             }
43             else {
44                 windowedCalcs( window, step, msa );
45             }
46         }
47         catch ( final Exception e ) {
48             e.printStackTrace();
49             // ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
50         }
51     }
52
53     private static void windowedCalcs( int window, int step, final Msa msa ) {
54         if ( window < 1 ) {
55             window = 1;
56         }
57         if ( step < 1 ) {
58             step = 1;
59         }
60         String min_pos = "";
61         String max_pos = "";
62         double min = 1;
63         double max = 0;
64         for( int i = 0; i <= msa.getLength() - 1; i += step ) {
65             int to = i + window - 1;
66             if ( to > ( msa.getLength() - 1 ) ) {
67                 to = msa.getLength() - 1;
68             }
69             final DescriptiveStatistics stats = calc( i, to, msa );
70             final double mean = stats.arithmeticMean();
71             final String pos = i + "-" + to;
72             System.out.print( pos );
73             System.out.print( ":\t" );
74             System.out.print( mean );
75             if ( window > 2 ) {
76                 System.out.print( "\t" );
77                 System.out.print( stats.median() );
78                 System.out.print( "\t" );
79                 System.out.print( stats.sampleStandardDeviation() );
80             }
81             System.out.println();
82             if ( mean > max ) {
83                 max = mean;
84                 max_pos = pos;
85             }
86             if ( mean < min ) {
87                 min = mean;
88                 min_pos = pos;
89             }
90         }
91         System.out.println( "Min: " + min_pos + ": " + min );
92         System.out.println( "Max: " + max_pos + ": " + max );
93     }
94
95     private static void singleCalc( final File in, int from, int to, final Msa msa ) {
96         if ( from < 0 ) {
97             from = 0;
98         }
99         if ( to > ( msa.getLength() - 1 ) ) {
100             to = msa.getLength() - 1;
101         }
102         final DescriptiveStatistics stats = calc( from, to, msa );
103         System.out.println( in.toString() + ": " + from + "-" + to + ":" );
104         System.out.println();
105         System.out.println( stats.toString() );
106     }
107
108     private static DescriptiveStatistics calc( final int from, final int to, final Msa msa ) {
109         final DescriptiveStatistics stats = new BasicDescriptiveStatistics();
110         for( int c = from; c <= to; ++c ) {
111             stats.addValue( MsaMethods.calculateIdentityRatio( msa, c ) );
112         }
113         return stats;
114     }
115 }