in progress
[jalview.git] / forester / java / src / org / forester / application / msa_quality.java
index 0ffd63d..27e9a56 100644 (file)
@@ -3,48 +3,113 @@ package org.forester.application;
 
 import java.io.File;
 import java.io.FileInputStream;
-import java.io.FileNotFoundException;
-import java.io.IOException;
-import java.util.SortedMap;
 
 import org.forester.io.parsers.GeneralMsaParser;
 import org.forester.msa.Msa;
 import org.forester.msa.MsaMethods;
+import org.forester.util.BasicDescriptiveStatistics;
 import org.forester.util.CommandLineArguments;
+import org.forester.util.DescriptiveStatistics;
 
 public class msa_quality {
 
     public static void main( final String args[] ) {
-        CommandLineArguments cla = null;
         try {
-            cla = new CommandLineArguments( args );
+            final CommandLineArguments cla = new CommandLineArguments( args );
+            // if ( cla.isOptionSet( HELP_OPTION_1 ) || cla.isOptionSet( HELP_OPTION_2 ) || ( args.length == 0 ) ) {
+            //     printHelp();
+            //     System.exit( 0 );
+            // }
+            final File in = cla.getFile( 0 );
+            int from = 0;
+            int to = 0;
+            int window = 0;
+            int step = 0;
+            if ( cla.isOptionSet( "f" ) && cla.isOptionSet( "t" ) ) {
+                from = cla.getOptionValueAsInt( "f" );
+                to = cla.getOptionValueAsInt( "t" );
+            }
+            else if ( cla.isOptionSet( "s" ) && cla.isOptionSet( "w" ) ) {
+                step = cla.getOptionValueAsInt( "s" );
+                window = cla.getOptionValueAsInt( "w" );
+            }
+            else {
+            }
+            Msa msa = null;
+            msa = GeneralMsaParser.parse( new FileInputStream( in ) );
+            if ( cla.isOptionSet( "f" ) && cla.isOptionSet( "t" ) ) {
+                singleCalc( in, from, to, msa );
+            }
+            else {
+                windowedCalcs( window, step, msa );
+            }
         }
         catch ( final Exception e ) {
+            e.printStackTrace();
             // ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
         }
-        // if ( cla.isOptionSet( HELP_OPTION_1 ) || cla.isOptionSet( HELP_OPTION_2 ) || ( args.length == 0 ) ) {
-        //     printHelp();
-        //     System.exit( 0 );
-        // }
-        final File in = cla.getFile( 0 );
-        Msa msa = null;
-        try {
-            msa = GeneralMsaParser.parse( new FileInputStream( in ) );
+    }
+
+    private static void windowedCalcs( int window, int step, final Msa msa ) {
+        if ( window < 1 ) {
+            window = 1;
         }
-        catch ( final FileNotFoundException e ) {
-            // TODO Auto-generated catch block
-            e.printStackTrace();
+        if ( step < 1 ) {
+            step = 1;
         }
-        catch ( final IOException e ) {
-            // TODO Auto-generated catch block
-            e.printStackTrace();
+        String min_pos = "";
+        String max_pos = "";
+        double min = 1;
+        double max = 0;
+        for( int i = 0; i <= msa.getLength() - 1; i += step ) {
+            int to = i + window - 1;
+            if ( to > ( msa.getLength() - 1 ) ) {
+                to = msa.getLength() - 1;
+            }
+            final DescriptiveStatistics stats = calc( i, to, msa );
+            final double mean = stats.arithmeticMean();
+            final String pos = i + "-" + to;
+            System.out.print( pos );
+            System.out.print( ":\t" );
+            System.out.print( mean );
+            if ( stats.getN() > 2 ) {
+                System.out.print( "\t" );
+                System.out.print( stats.median() );
+                System.out.print( "\t" );
+                System.out.print( stats.sampleStandardDeviation() );
+            }
+            System.out.println();
+            if ( mean > max ) {
+                max = mean;
+                max_pos = pos;
+            }
+            if ( mean < min ) {
+                min = mean;
+                min_pos = pos;
+            }
+        }
+        System.out.println( "Min: " + min_pos + ": " + min );
+        System.out.println( "Max: " + max_pos + ": " + max );
+    }
+
+    private static void singleCalc( final File in, int from, int to, final Msa msa ) {
+        if ( from < 0 ) {
+            from = 0;
         }
-        final int end = 2;
-        final int start = 6;
-        for( int c = start; c <= end; ++c ) {
-            final SortedMap<Character, Integer> dist = MsaMethods.calculateResidueDestributionPerColumn( msa, c );
-            final char majority_char = ' ';
-            final int majority_count = 0;
+        if ( to > ( msa.getLength() - 1 ) ) {
+            to = msa.getLength() - 1;
+        }
+        final DescriptiveStatistics stats = calc( from, to, msa );
+        System.out.println( in.toString() + ": " + from + "-" + to + ":" );
+        System.out.println();
+        System.out.println( stats.toString() );
+    }
+
+    private static DescriptiveStatistics calc( final int from, final int to, final Msa msa ) {
+        final DescriptiveStatistics stats = new BasicDescriptiveStatistics();
+        for( int c = from; c <= to; ++c ) {
+            stats.addValue( MsaMethods.calculateIdentityRatio( msa, c ) );
         }
+        return stats;
     }
 }