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