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