annotate src/edu/unc/genomics/wigmath/ZScore.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.util.Iterator;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
5
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
6 import org.apache.log4j.Logger;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
7 import org.broad.igv.bbfile.WigItem;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
8
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
9 import com.beust.jcommander.Parameter;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
10
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
11 import edu.unc.genomics.CommandLineToolException;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
12 import edu.unc.genomics.io.WigFile;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
13 import edu.unc.genomics.io.WigFileException;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
14
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
15 public class ZScore extends WigMathTool {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
16
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
17 private static final Logger log = Logger.getLogger(ZScore.class);
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
18
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
19 @Parameter(names = {"-i", "--input"}, description = "Input file", required = true)
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
20 public WigFile inputFile;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
21
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
22 double mean;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
23 double stdev;
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 inputs.add(inputFile);
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
28
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
29 mean = inputFile.mean();
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
30 stdev = inputFile.stdev();
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
31 if(stdev == 0) {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
32 log.error("Cannot Z-score a file with stdev = 0!");
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
33 throw new CommandLineToolException("Cannot Z-score a file with stdev = 0!");
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
34 }
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
35 }
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
36
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
37 @Override
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
38 public float[] compute(String chr, int start, int stop) throws IOException, WigFileException {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
39 log.debug("Computing difference for chunk "+chr+":"+start+"-"+stop);
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
40 Iterator<WigItem> data = inputFile.query(chr, start, stop);
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
41 float[] result = WigFile.flattenData(data, start, stop);
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
42
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
43 for (int i = 0; i < result.length; i++) {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
44 result[i] = (float)((result[i] - mean) / stdev);
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
45 }
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
46
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
47 return result;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
48 }
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
49
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
50
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
51 /**
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
52 * @param args
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
53 * @throws WigFileException
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
54 * @throws IOException
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
55 */
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
56 public static void main(String[] args) throws IOException, WigFileException {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
57 new ZScore().instanceMain(args);
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
58 }
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
59
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
60 }