Mercurial > repos > timpalpant > java_genomics_toolkit
diff src/edu/unc/genomics/ngs/IntervalLengthDistribution.java @ 2:e16016635b2a
Uploaded
author | timpalpant |
---|---|
date | Mon, 13 Feb 2012 22:12:06 -0500 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/edu/unc/genomics/ngs/IntervalLengthDistribution.java Mon Feb 13 22:12:06 2012 -0500 @@ -0,0 +1,59 @@ +package edu.unc.genomics.ngs; + +import java.io.BufferedWriter; +import java.io.IOException; +import java.nio.charset.Charset; +import java.nio.file.Files; +import java.nio.file.Path; + +import org.apache.commons.math.stat.Frequency; +import org.apache.log4j.Logger; + +import com.beust.jcommander.Parameter; + +import edu.unc.genomics.CommandLineTool; +import edu.unc.genomics.Interval; +import edu.unc.genomics.io.IntervalFile; + +public class IntervalLengthDistribution extends CommandLineTool { + + private static final Logger log = Logger.getLogger(IntervalLengthDistribution.class); + + @Parameter(names = {"-i", "--input"}, description = "Interval file", required = true) + public IntervalFile<? extends Interval> inputFile; + @Parameter(names = {"-o", "--output"}, description = "Output file", required = true) + public Path outputFile; + + + @Override + public void run() throws IOException { + log.debug("Generating histogram of interval lengths"); + Frequency freq = new Frequency(); + int min = Integer.MAX_VALUE; + int max = -1; + for (Interval i : inputFile) { + int L = i.length(); + freq.addValue(L); + + if (L < min) { + min = L; + } + if (L > max) { + max = L; + } + } + + log.debug("Writing histogram output"); + try (BufferedWriter writer = Files.newBufferedWriter(outputFile, Charset.defaultCharset())) { + for (int i = min; i <= max; i++) { + writer.write(i+"\t"+freq.getCount(i)); + writer.newLine(); + } + } + } + + public static void main(String[] args) { + new IntervalLengthDistribution().instanceMain(args); + } + +}