annotate src/edu/unc/genomics/visualization/IntervalAverager.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.visualization;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
2
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
3 import java.io.BufferedWriter;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
4 import java.io.IOException;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
5 import java.nio.charset.Charset;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
6 import java.nio.file.Files;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
7 import java.nio.file.Path;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
8 import java.util.Iterator;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
9 import java.util.List;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
10
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
11 import org.apache.commons.lang3.ArrayUtils;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
12 import org.apache.log4j.Logger;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
13 import org.broad.igv.bbfile.WigItem;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
14
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
15 import com.beust.jcommander.Parameter;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
16
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
17 import edu.unc.genomics.BedEntry;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
18 import edu.unc.genomics.CommandLineTool;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
19 import edu.unc.genomics.ReadablePathValidator;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
20 import edu.unc.genomics.io.BedFile;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
21 import edu.unc.genomics.io.WigFile;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
22 import edu.unc.genomics.io.WigFileException;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
23
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
24 public class IntervalAverager extends CommandLineTool {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
25
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
26 private static final Logger log = Logger.getLogger(IntervalAverager.class);
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
27
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
28 @Parameter(names = {"-i", "--input"}, description = "Input file (Wig)", required = true)
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
29 public WigFile inputFile;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
30 @Parameter(names = {"-l", "--loci"}, description = "Loci file (Bed)", required = true, validateWith = ReadablePathValidator.class)
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
31 public Path lociFile;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
32 @Parameter(names = {"-o", "--output"}, description = "Output file (matrix2png format)", required = true)
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
33 public Path outputFile;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
34
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
35 private List<BedEntry> loci;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
36
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
37 @Override
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
38 public void run() throws IOException {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
39 log.debug("Loading alignment intervals");
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
40 try (BedFile bed = new BedFile(lociFile)) {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
41 loci = bed.loadAll();
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
42 }
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
43
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
44 // Compute the matrix dimensions
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
45 int leftMax = Integer.MIN_VALUE;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
46 int rightMax = Integer.MIN_VALUE;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
47 for (BedEntry entry : loci) {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
48 int left = Math.abs(entry.getValue().intValue()-entry.getStart());
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
49 int right = Math.abs(entry.getValue().intValue()-entry.getStop());
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
50 if (left > leftMax) {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
51 leftMax = left;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
52 }
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
53 if (right > rightMax) {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
54 rightMax = right;
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 int m = loci.size();
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
59 int n = leftMax + rightMax + 1;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
60 int alignmentPoint = leftMax;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
61 log.info("Intervals aligned into: " + m+"x"+n + " matrix");
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
62 log.info("Alignment point: " + alignmentPoint);
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
63
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
64 float[] sum = new float[n];
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
65 int[] counts = new int[n];
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
66 int count = 0, skipped = 0;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
67 log.debug("Iterating over all intervals");
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
68 for (BedEntry entry : loci) {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
69 Iterator<WigItem> result = null;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
70 try {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
71 result = inputFile.query(entry);
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
72 } catch (WigFileException e) {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
73 skipped++;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
74 continue;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
75 }
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
76
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
77 float[] data = WigFile.flattenData(result, entry.getStart(), entry.getStop());
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
78 // Reverse if on the Crick strand
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
79 if (entry.isCrick()) {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
80 ArrayUtils.reverse(data);
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
81 }
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
82
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
83 // Locus alignment point (entry value) should be positioned over the global alignment point
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
84 int n1 = alignmentPoint - Math.abs(entry.getValue().intValue()-entry.getStart());
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
85 int n2 = alignmentPoint + Math.abs(entry.getValue().intValue()-entry.getStop());
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
86 for (int bp = n1; bp <= n2; bp++) {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
87 sum[bp] += data[bp-n1];
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
88 counts[bp]++;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
89 }
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
90 }
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
91
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
92 inputFile.close();
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
93 log.info(count + " intervals processed");
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
94 log.info(skipped + " intervals skipped");
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
95
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
96 log.debug("Computing average");
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
97 float[] avg = new float[n];
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
98 for (int i = 0; i < n; i++) {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
99 if (counts[i] == 0) {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
100 avg[i] = Float.NaN;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
101 } else {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
102 avg[i] = sum[i] / counts[i];
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
103 }
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
104 }
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
105
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
106 log.debug("Writing average to output");
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
107 try (BufferedWriter writer = Files.newBufferedWriter(outputFile, Charset.defaultCharset())) {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
108 for (int i = 0; i < n; i++) {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
109 writer.write(i-alignmentPoint + "\t" + avg[i]);
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
110 writer.newLine();
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
111 }
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
112 }
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
113 }
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
114
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
115 public static void main(String[] args) {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
116 new IntervalAverager().instanceMain(args);
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
117 }
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
118 }