import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;

import org.apache.commons.math3.stat.descriptive.SummaryStatistics;

/**
 * An efficient method to calculate percentiles of coverage values
 * that doesn't require holding them all in memory or sorting them.
 * <p>
 * It relies on some limitations regarding coverage values:
 * <li>They are integers
 * <li>We usually know the approximate range, and don't care about it
 *     above a certain value. For example, we are unlikely to observe
 *     values above 1000 and those that do are unlikely to affect the
 *     50'th percentile.
 * 
 * @author simon.sadedin@mcri.edu.au
 */
public class CoverageStats extends graxxia.IntegerStats {
    
    private static final long serialVersionUID = 1L;

    public CoverageStats(int maxPercentileValue, InputStream inStream)
            throws IOException {
        super(maxPercentileValue, inStream);
    }

    public CoverageStats(int maxPercentileValue, Iterable covValues) {
        super(maxPercentileValue, covValues);
    }

    public CoverageStats(int maxPercentileValue) {
        super(maxPercentileValue);
    }
}

@@ -16,32 +16,53 @@
 BufferedReader;%0A
+import java.io.File;%0A
 import java.io.I
@@ -151,19 +151,119 @@
 ava.
-util.Arrays
+nio.file.Files;%0Aimport java.util.Arrays;%0Aimport java.util.StringTokenizer;%0Aimport java.util.zip.GZIPInputStream
 ;%0A%0Ai
@@ -330,16 +330,56 @@
 stics;%0A%0A
+import groovy.transform.CompileStatic;%0A%0A
 /**%0A * A
@@ -996,16 +996,58 @@
 ID = 1L;
+%0A    %0A    private double totalBases = -1d;
 %0A%0A    pu
@@ -1412,10 +1412,1277 @@
 ;%0A    %7D%0A
+    %0A    public double fractionAbove(int coverageLevel) %7B%0A        if(totalBases%3C0d) %7B%0A            long total = 0L;%0A            for(int i=0; i%3Cthis.values.length; ++i) %7B%0A                total += values%5Bi%5D;%0A            %7D%0A            totalBases = (double)total;%0A        %7D%0A        %0A        long total = 0;%0A        for(int i=coverageLevel+1; i%3Cvalues.length; ++i) %7B%0A            total += values%5Bi%5D;%0A        %7D%0A        return total / totalBases;%0A//        this.values%5Bx..-1%5D.sum() / totalBases %0A    %7D%0A    %0A    @CompileStatic%0A    static CoverageStats from(InputStream stream) throws IOException %7B%0A        CoverageStats result = new CoverageStats(1000);%0A        BufferedReader reader = new BufferedReader(new InputStreamReader(stream));%0A        try %7B%0A            String line = reader.readLine();%0A            while(line != null) %7B%0A                String covValue = line.substring(line.lastIndexOf('%5Ct')+1);%0A                int intValue = Integer.parseInt(covValue);%0A                result.addValue(intValue);%0A                line = reader.readLine();%0A            %7D%0A        %7D%0A        finally %7B%0A            try %7B%0A                reader.close();%0A            %7D%0A            catch(Exception e) %7B%0A                // nothing%0A            %7D%0A        %7D%0A        return result;%0A    %7D        %0A
 %7D%0A

support for BEDTools coverage and fraction gt