annotate src/edu/unc/genomics/wigmath/Add.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.nio.file.Paths;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
5 import java.util.ArrayList;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
6 import java.util.Iterator;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
7 import java.util.List;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
8
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
9 import org.apache.log4j.Logger;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
10 import org.broad.igv.bbfile.WigItem;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
11
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
12 import com.beust.jcommander.Parameter;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
13
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
14 import edu.unc.genomics.CommandLineToolException;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
15 import edu.unc.genomics.io.WigFile;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
16 import edu.unc.genomics.io.WigFileException;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
17
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
18 public class Add extends WigMathTool {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
19
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
20 private static final Logger log = Logger.getLogger(Add.class);
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
21
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
22 @Parameter(description = "Input files", required = true)
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
23 public List<String> inputFiles = new ArrayList<String>();
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
24
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
25 @Override
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
26 public void setup() {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
27 if (inputFiles.size() < 2) {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
28 throw new CommandLineToolException("No reason to add < 2 files.");
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
29 }
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
30
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
31 log.debug("Initializing input files");
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
32 for (String inputFile : inputFiles) {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
33 try {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
34 addInputFile(WigFile.autodetect(Paths.get(inputFile)));
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
35 } catch (IOException | WigFileException e) {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
36 log.error("Error initializing input Wig file: " + inputFile);
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
37 e.printStackTrace();
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
38 throw new CommandLineToolException(e.getMessage());
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
39 }
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
40 }
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
41 log.debug("Initialized " + inputs.size() + " input files");
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
42 }
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
43
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
44 @Override
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
45 public float[] compute(String chr, int start, int stop) throws IOException, WigFileException {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
46 log.debug("Computing sum for chunk "+chr+":"+start+"-"+stop);
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
47
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
48 int length = stop - start + 1;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
49 float[] sum = new float[length];
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
50
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
51 for (WigFile wig : inputs) {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
52 Iterator<WigItem> data = wig.query(chr, start, stop);
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
53 while (data.hasNext()) {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
54 WigItem item = data.next();
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
55 for (int i = item.getStartBase(); i <= item.getEndBase(); i++) {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
56 if (i-start >= 0 && i-start < sum.length) {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
57 sum[i-start] += item.getWigValue();
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
58 }
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
59 }
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
60 }
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
61 }
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
62
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
63 return sum;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
64 }
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
65
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
66
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
67 /**
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
68 * @param args
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
69 * @throws WigFileException
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
70 * @throws IOException
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
71 */
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
72 public static void main(String[] args) throws IOException, WigFileException {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
73 new Add().instanceMain(args);
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
74 }
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
75
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
76 }