comparison src/edu/unc/genomics/wigmath/Add.java @ 2:e16016635b2a

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