annotate java-genomics-toolkit/src/edu/unc/genomics/wigmath/Add.java @ 0:1daf3026d231

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