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