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