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