annotate java-genomics-toolkit/src/edu/unc/genomics/wigmath/MovingAverageSmooth.java @ 0:1daf3026d231

Upload alpha version
author timpalpant
date Mon, 13 Feb 2012 21:55:55 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
1 package edu.unc.genomics.wigmath;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
2
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
3 import java.io.IOException;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
4 import java.util.Iterator;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
5
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
6 import org.apache.commons.math.stat.descriptive.DescriptiveStatistics;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
7 import org.apache.log4j.Logger;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
8 import org.broad.igv.bbfile.WigItem;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
9
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
10 import com.beust.jcommander.Parameter;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
11
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
12 import edu.unc.genomics.PositiveIntegerValidator;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
13 import edu.unc.genomics.io.WigFile;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
14 import edu.unc.genomics.io.WigFileException;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
15
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
16 public class MovingAverageSmooth extends WigMathTool {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
17
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
18 private static final Logger log = Logger.getLogger(MovingAverageSmooth.class);
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
19
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
20 @Parameter(names = {"-i", "--input"}, description = "Input file", required = true)
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
21 public WigFile inputFile;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
22 @Parameter(names = {"-w", "--width"}, description = "Width of kernel (bp)")
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
23 public int width = 10;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
24
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
25 WigFile input;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
26 DescriptiveStatistics stats;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
27
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
28 @Override
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
29 public void setup() {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
30 inputs.add(inputFile);
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
31
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
32 log.debug("Initializing statistics");
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
33 stats = new DescriptiveStatistics();
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
34 stats.setWindowSize(width);
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
35 }
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
36
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
37 @Override
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
38 public float[] compute(String chr, int start, int stop) throws IOException, WigFileException {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
39 log.debug("Smoothing chunk "+chr+":"+start+"-"+stop);
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
40 // Pad the query so that we can provide values for the ends
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
41 int queryStart = Math.max(start-width/2, input.getChrStart(chr));
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
42 int queryStop = Math.min(stop+width/2, input.getChrStop(chr));
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
43 Iterator<WigItem> result = input.query(chr, queryStart, queryStop);
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
44 float[] data = WigFile.flattenData(result, queryStart, queryStop);
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
45
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
46 float[] smoothed = new float[stop-start+1];
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
47 for (int bp = start; bp <= stop; bp++) {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
48 stats.addValue(data[bp-queryStart]);
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
49 if (bp-start-width/2 >= 0) {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
50 smoothed[bp-start-width/2] = (float) stats.getMean();
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
51 }
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
52 }
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
53
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
54 return smoothed;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
55 }
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
56
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
57
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
58 /**
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
59 * @param args
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
60 * @throws WigFileException
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
61 * @throws IOException
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
62 */
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
63 public static void main(String[] args) throws IOException, WigFileException {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
64 new MovingAverageSmooth().instanceMain(args);
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
65 }
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
66
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
67 }