annotate src/edu/unc/genomics/wigmath/GaussianSmooth.java @ 2:e16016635b2a

Uploaded
author timpalpant
date Mon, 13 Feb 2012 22:12:06 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
1 package edu.unc.genomics.wigmath;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
2
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
3 import java.io.IOException;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
4 import java.util.Iterator;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
5
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
6 import org.apache.log4j.Logger;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
7 import org.broad.igv.bbfile.WigItem;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
8
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
9 import com.beust.jcommander.Parameter;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
10
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
11 import edu.unc.genomics.io.WigFile;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
12 import edu.unc.genomics.io.WigFileException;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
13
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
14 public class GaussianSmooth extends WigMathTool {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
15
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
16 private static final Logger log = Logger.getLogger(GaussianSmooth.class);
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
17
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
18 @Parameter(names = {"-i", "--input"}, description = "Input file", required = true)
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
19 public WigFile inputFile;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
20 @Parameter(names = {"-s", "--stdev"}, description = "Standard deviation of Gaussian (bp)")
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
21 public int stdev = 20;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
22
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
23 float[] filter;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
24
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
25 @Override
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
26 public void setup() {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
27 inputs.add(inputFile);
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
28
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
29 // Use a window size equal to +/- 3 SD's
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
30 filter = new float[6*stdev+1];
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
31 float sum = 0;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
32 for (int i = 0; i < filter.length; i++) {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
33 float x = i - 3*stdev;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
34 float value = (float) Math.exp(-(x*x) / (2*stdev*stdev));
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
35 filter[i] = value;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
36 sum += value;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
37 }
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
38 for (int i = 0; i < filter.length; i++) {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
39 filter[i] /= sum;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
40 }
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
41 }
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
42
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
43 @Override
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
44 public float[] compute(String chr, int start, int stop) throws IOException, WigFileException {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
45 log.debug("Smoothing chunk "+chr+":"+start+"-"+stop);
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
46
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
47 // Pad the query for smoothing
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
48 int paddedStart = Math.max(start-3*stdev, inputFile.getChrStart(chr));
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
49 int paddedStop = Math.min(stop+3*stdev, inputFile.getChrStop(chr));
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
50 Iterator<WigItem> result = inputFile.query(chr, paddedStart, paddedStop);
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
51 float[] data = WigFile.flattenData(result, start-3*stdev, stop+3*stdev);
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
52
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
53 // Convolve the data with the filter
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
54 float[] smoothed = new float[stop-start+1];
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
55 for (int i = 0; i < smoothed.length; i++) {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
56 for (int j = 0; j < filter.length; j++) {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
57 smoothed[i] += data[i+j] * filter[j];
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
58 }
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
59 }
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
60
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
61 return smoothed;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
62 }
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
63
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
64
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
65 /**
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
66 * @param args
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
67 * @throws WigFileException
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
68 * @throws IOException
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
69 */
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
70 public static void main(String[] args) throws IOException, WigFileException {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
71 new GaussianSmooth().instanceMain(args);
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
72 }
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
73
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
74 }