package com.secret.fastalign.main;

import java.io.BufferedReader;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.TreeMap;

import com.secret.fastalign.utils.Utils;

public class GetHistogramStats {
	private static final NumberFormat nf = new DecimalFormat(
			"############.####");
	private static final int NUM_SD = 7;
	private TreeMap<Integer, Long> histogram = new TreeMap<Integer, Long>();
	private double percent = 0.99;
	private double mean = 0;
	private double stdev = 0;
	private int cut = 0;

	public GetHistogramStats(String fileName, double p) {
		try {
			BufferedReader bf = Utils.getFile(fileName, "hist");
			String line = null;

			while ((line = bf.readLine()) != null) {
				String[] split = line.trim().split("\\s+");
				int val = Integer.parseInt(split[0]);
				long count = Long.parseLong(split[1]);
				histogram.put(val, count);
			}
			bf.close();
			percent = p;
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void process() throws NumberFormatException, IOException {
		double variance = 0;
		double sum = 0;
		int total = 0;

		for (int val : histogram.keySet()) {
			long count = histogram.get(val);
			for (int i = 0; i < count; i++) {
				total++;
				double delta = (val - mean);
				mean += (delta / total);
				variance += delta * (val - mean);
				sum += val;
			}
		}
		variance /= total;
		stdev = Math.sqrt(variance);

		double runningSum = 0;
		for (int val : histogram.keySet()) {
			long count = histogram.get(val);
			runningSum += (double) val * count;
			if ((runningSum / sum) > percent) {
				cut = val;
				break;
			}
		}
	}

	@Override
	public String toString() {
		return nf.format(mean) + "\t" + nf.format(stdev) + "\t" + "\t" + cut
				+ "\t" + nf.format(mean + NUM_SD * stdev);
	}
	
	public static void main(String[] args) throws NumberFormatException, IOException {
		GetHistogramStats s = new GetHistogramStats(args[0], Double.parseDouble(args[1]));
		s.process();
		System.out.println(s.toString());
	}
}

@@ -881,16 +881,21 @@
 %5D);%0A%09%09%09%09
+this.
 histogra
@@ -936,16 +936,21 @@
 e();%0A%09%09%09
+this.
 percent 
@@ -1185,32 +1185,37 @@
 %09%09%09long count = 
+this.
 histogram.get(va
@@ -1286,32 +1286,37 @@
  delta = (val - 
+this.
 mean);%0A%09%09%09%09mean 
@@ -1310,16 +1310,21 @@
 n);%0A%09%09%09%09
+this.
 mean += 
@@ -1371,16 +1371,21 @@
  (val - 
+this.
 mean);%0A%09
@@ -1431,16 +1431,21 @@
 otal;%0A%09%09
+this.
 stdev = 
@@ -1500,32 +1500,37 @@
 %09for (int val : 
+this.
 histogram.keySet
@@ -1551,16 +1551,21 @@
 count = 
+this.
 histogra
@@ -1643,16 +1643,21 @@
  sum) %3E 
+this.
 percent)
@@ -1663,16 +1663,21 @@
 ) %7B%0A%09%09%09%09
+this.
 cut = va
@@ -1757,24 +1757,29 @@
 n nf.format(
+this.
 mean) + %22%5Ct%22
@@ -1791,16 +1791,21 @@
 .format(
+this.
 stdev) +
@@ -1819,16 +1819,21 @@
  %22%5Ct%22 + 
+this.
 cut%0A%09%09%09%09
@@ -1851,16 +1851,21 @@
 .format(
+this.
 mean + N
@@ -1872,16 +1872,21 @@
 UM_SD * 
+this.
 stdev);%0A

Update to use long and reference class variables with this