annotate java-genomics-toolkit/src/edu/unc/genomics/ngs/PowerSpectrum.java @ 0:1daf3026d231

Upload alpha version
author timpalpant
date Mon, 13 Feb 2012 21:55:55 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
1 package edu.unc.genomics.ngs;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
2
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
3 import java.io.BufferedWriter;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
4 import java.io.IOException;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
5 import java.nio.charset.Charset;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
6 import java.nio.file.Files;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
7 import java.nio.file.Path;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
8 import java.util.Iterator;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
9
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
10 import org.apache.log4j.Logger;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
11 import org.broad.igv.bbfile.WigItem;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
12
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
13 import com.beust.jcommander.Parameter;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
14
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
15 import edu.emory.mathcs.jtransforms.fft.FloatFFT_1D;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
16 import edu.unc.genomics.CommandLineTool;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
17 import edu.unc.genomics.Interval;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
18 import edu.unc.genomics.io.IntervalFile;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
19 import edu.unc.genomics.io.WigFile;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
20 import edu.unc.genomics.io.WigFileException;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
21
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
22 public class PowerSpectrum extends CommandLineTool {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
23
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
24 private static final Logger log = Logger.getLogger(PowerSpectrum.class);
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
25
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
26 @Parameter(names = {"-i", "--input"}, description = "Input file (Wig)", required = true)
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
27 public WigFile inputFile;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
28 @Parameter(names = {"-l", "--loci"}, description = "Genomic loci (Bed format)", required = true)
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
29 public IntervalFile<? extends Interval> loci;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
30 @Parameter(names = {"-o", "--output"}, description = "Output file (tabular)", required = true)
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
31 public Path outputFile;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
32
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
33 /**
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
34 * Computes the power spectrum from FFT data
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
35 * taking into accound even/odd length arrays
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
36 * refer to JTransforms documentation for layout of the FFT data
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
37 * @param f
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
38 * @return
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
39 */
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
40 private float[] abs2(float[] f) {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
41 int n = f.length;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
42 float[] ps = new float[n/2+1];
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
43 // DC component
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
44 ps[0] = (f[0]*f[0]) / (n*n);
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
45
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
46 // Even
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
47 if (n % 2 == 0) {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
48 for (int k = 1; k < n/2; k++) {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
49 ps[k] = f[2*k]*f[2*k] + f[2*k+1]*f[2*k+1];
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
50 }
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
51 ps[n/2] = f[1]*f[1];
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
52 // Odd
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
53 } else {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
54 for (int k = 1; k < (n-1)/2; k++) {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
55 ps[k] = f[2*k]*f[2*k] + f[2*k+1]*f[2*k+1];
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
56 }
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
57
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
58 ps[(n-1)/2] = f[n-1]*f[n-1] + f[1]*f[1];
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
59 }
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
60
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
61 return ps;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
62 }
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
63
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
64 public void run() throws IOException {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
65 try (BufferedWriter writer = Files.newBufferedWriter(outputFile, Charset.defaultCharset())) {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
66 log.debug("Computing power spectrum for each window");
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
67 int skipped = 0;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
68 for (Interval interval : loci) {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
69 Iterator<WigItem> wigIter;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
70 try {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
71 wigIter = inputFile.query(interval);
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
72 } catch (IOException | WigFileException e) {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
73 log.debug("Skipping interval: " + interval.toString());
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
74 skipped++;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
75 continue;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
76 }
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
77
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
78 float[] data = WigFile.flattenData(wigIter, interval.getStart(), interval.getStop());
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
79 // Compute the power spectrum
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
80 FloatFFT_1D fft = new FloatFFT_1D(data.length);
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
81 fft.realForward(data);
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
82 float[] ps = abs2(data);
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
83 // and normalize the power spectrum
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
84 float sum = 0;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
85 for (int i = 1; i < ps.length; i++) {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
86 sum += ps[i];
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
87 }
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
88 for (int i = 1; i < ps.length; i++) {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
89 ps[i] /= sum;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
90 }
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
91
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
92 writer.write(interval.toBed());
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
93 for (int i = 1; i < Math.min(ps.length, 40); i++) {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
94 writer.write("\t"+ps[i]);
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
95 }
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
96 writer.newLine();
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
97 }
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
98
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
99 log.info("Skipped " + skipped + " intervals");
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
100 }
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
101 }
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
102
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
103 public static void main(String[] args) {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
104 new PowerSpectrum().instanceMain(args);
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
105 }
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
106 }