comparison java-genomics-toolkit/src/edu/unc/genomics/wigmath/Divide.java @ 0:1daf3026d231

Upload alpha version
author timpalpant
date Mon, 13 Feb 2012 21:55:55 -0500
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:1daf3026d231
1 package edu.unc.genomics.wigmath;
2
3 import java.io.IOException;
4 import java.util.Iterator;
5
6 import org.apache.log4j.Logger;
7 import org.broad.igv.bbfile.WigItem;
8
9 import com.beust.jcommander.Parameter;
10
11 import edu.unc.genomics.io.WigFile;
12 import edu.unc.genomics.io.WigFileException;
13
14 public class Divide extends WigMathTool {
15
16 private static final Logger log = Logger.getLogger(Divide.class);
17
18 @Parameter(names = {"-n", "--numerator"}, description = "Dividend / Numerator (file 1)", required = true)
19 public WigFile dividendFile;
20 @Parameter(names = {"-d", "--denominator"}, description = "Divisor / Denominator (file 2)", required = true)
21 public WigFile divisorFile;
22
23 @Override
24 public void setup() {
25 inputs.add(dividendFile);
26 inputs.add(divisorFile);
27 log.debug("Initialized " + inputs.size() + " input files");
28 }
29
30 @Override
31 public float[] compute(String chr, int start, int stop) throws IOException, WigFileException {
32 log.debug("Computing difference for chunk "+chr+":"+start+"-"+stop);
33
34 Iterator<WigItem> dividendData = dividendFile.query(chr, start, stop);
35 Iterator<WigItem> divisorData = divisorFile.query(chr, start, stop);
36
37 float[] result = WigFile.flattenData(dividendData, start, stop);
38 while (divisorData.hasNext()) {
39 WigItem item = divisorData.next();
40 for (int i = item.getStartBase(); i <= item.getEndBase(); i++) {
41 if (i-start >= 0 && i-start < result.length) {
42 if (item.getWigValue() != 0) {
43 result[i-start] /= item.getWigValue();
44 } else {
45 result[i-start] = Float.NaN;
46 }
47 }
48 }
49 }
50
51 return result;
52 }
53
54
55 /**
56 * @param args
57 * @throws WigFileException
58 * @throws IOException
59 */
60 public static void main(String[] args) throws IOException, WigFileException {
61 new Divide().instanceMain(args);
62 }
63
64 }