annotate src/edu/unc/genomics/wigmath/Average.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 Average extends WigMathTool {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
19
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
20 private static final Logger log = Logger.getLogger(Average.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 int numFiles;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
26
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
27 @Override
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
28 public void setup() {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
29 if (inputFiles.size() < 2) {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
30 throw new CommandLineToolException("No reason to add < 2 files.");
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
31 }
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
32
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
33 log.debug("Initializing input files");
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
34 for (String inputFile : inputFiles) {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
35 try {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
36 addInputFile(WigFile.autodetect(Paths.get(inputFile)));
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
37 } catch (IOException | WigFileException e) {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
38 log.error("Error initializing input Wig file: " + inputFile);
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
39 e.printStackTrace();
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
40 throw new CommandLineToolException(e.getMessage());
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
41 }
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
42 }
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
43 log.debug("Initialized " + inputs.size() + " input files");
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
44
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
45 numFiles = inputs.size();
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
46 }
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
47
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
48 @Override
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
49 public float[] compute(String chr, int start, int stop) throws IOException, WigFileException {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
50 log.debug("Computing average for chunk "+chr+":"+start+"-"+stop);
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
51
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
52 int length = stop - start + 1;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
53 float[] avg = new float[length];
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
54
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
55 for (WigFile wig : inputs) {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
56 Iterator<WigItem> data = wig.query(chr, start, stop);
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
57 while (data.hasNext()) {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
58 WigItem item = data.next();
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
59 for (int i = item.getStartBase(); i <= item.getEndBase(); i++) {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
60 if (i-start >= 0 && i-start < avg.length) {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
61 avg[i-start] += item.getWigValue();
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 }
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
66
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
67 for (int i = 0; i < avg.length; i++) {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
68 avg[i] = avg[i] / numFiles;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
69 }
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
70
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
71 return avg;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
72 }
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
73
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
74
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
75 /**
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
76 * @param args
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
77 * @throws WigFileException
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
78 * @throws IOException
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
79 */
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
80 public static void main(String[] args) throws IOException, WigFileException {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
81 new Average().instanceMain(args);
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
82 }
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
83
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
84 }