2
|
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 Subtract extends WigMathTool {
|
|
15
|
|
16 private static final Logger log = Logger.getLogger(Subtract.class);
|
|
17
|
|
18 @Parameter(names = {"-m", "--minuend"}, description = "Minuend (top - file 1)", required = true)
|
|
19 public WigFile minuendFile;
|
|
20 @Parameter(names = {"-s", "--subtrahend"}, description = "Subtrahend (bottom - file 2)", required = true)
|
|
21 public WigFile subtrahendFile;
|
|
22
|
|
23 @Override
|
|
24 public void setup() {
|
|
25 log.debug("Initializing input files");
|
|
26 inputs.add(minuendFile);
|
|
27 inputs.add(subtrahendFile);
|
|
28 log.debug("Initialized " + inputs.size() + " input files");
|
|
29 }
|
|
30
|
|
31 @Override
|
|
32 public float[] compute(String chr, int start, int stop) throws IOException, WigFileException {
|
|
33 log.debug("Computing difference for chunk "+chr+":"+start+"-"+stop);
|
|
34
|
|
35 Iterator<WigItem> minuendData = minuendFile.query(chr, start, stop);
|
|
36 Iterator<WigItem> subtrahendData = subtrahendFile.query(chr, start, stop);
|
|
37
|
|
38 float[] result = WigFile.flattenData(minuendData, start, stop);
|
|
39 while (subtrahendData.hasNext()) {
|
|
40 WigItem item = subtrahendData.next();
|
|
41 for (int i = item.getStartBase(); i <= item.getEndBase(); i++) {
|
|
42 if (i-start >= 0 && i-start < result.length) {
|
|
43 result[i-start] -= item.getWigValue();
|
|
44 }
|
|
45 }
|
|
46 }
|
|
47
|
|
48 return result;
|
|
49 }
|
|
50
|
|
51
|
|
52 /**
|
|
53 * @param args
|
|
54 * @throws WigFileException
|
|
55 * @throws IOException
|
|
56 */
|
|
57 public static void main(String[] args) throws IOException, WigFileException {
|
|
58 new Subtract().instanceMain(args);
|
|
59 }
|
|
60
|
|
61 }
|