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