annotate java-genomics-toolkit/src/edu/unc/genomics/nucleosomes/GreedyCaller.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.nucleosomes;
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 import edu.unc.genomics.CommandLineTool;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
15 import edu.unc.genomics.CommandLineToolException;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
16 import edu.unc.genomics.ReadablePathValidator;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
17 import edu.unc.genomics.io.WigFile;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
18 import edu.unc.genomics.io.WigFileException;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
19 import edu.unc.utils.SortUtils;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
20
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
21 public class GreedyCaller extends CommandLineTool {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
22
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
23 private static final Logger log = Logger.getLogger(GreedyCaller.class);
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
24
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
25 private static final int CHUNK_SIZE = 500_000;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
26
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
27 @Parameter(names = {"-d", "--dyads"}, description = "Dyad counts file", required = true, validateWith = ReadablePathValidator.class)
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
28 public WigFile dyadsFile;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
29 @Parameter(names = {"-s", "--smoothed"}, description = "Smoothed dyad counts file", required = true, validateWith = ReadablePathValidator.class)
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
30 public WigFile smoothedDyadsFile;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
31 @Parameter(names = {"-n", "--size"}, description = "Nucleosome size (bp)")
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
32 public int nucleosomeSize = 147;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
33 @Parameter(names = {"-o", "--output"}, description = "Output file", required = true)
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
34 public Path outputFile;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
35
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
36 public void run() throws IOException {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
37 int halfNuc = nucleosomeSize / 2;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
38
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
39 try (BufferedWriter writer = Files.newBufferedWriter(outputFile, Charset.defaultCharset())) {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
40 for (String chr : smoothedDyadsFile.chromosomes()) {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
41 log.debug("Processing chromosome "+chr);
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
42 int chunkStart = smoothedDyadsFile.getChrStart(chr);
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
43 int chrStop = smoothedDyadsFile.getChrStop(chr);
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
44 while (chunkStart < chrStop) {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
45 int chunkStop = chunkStart + CHUNK_SIZE;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
46 int paddedStart = chunkStart - nucleosomeSize;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
47 int paddedStop = chunkStop + nucleosomeSize;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
48 log.debug("Processing chunk "+chunkStart+"-"+chunkStop);
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
49
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
50 log.debug("Loading data and sorting");
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
51 Iterator<WigItem> dyadsIter;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
52 Iterator<WigItem> smoothedIter;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
53 try {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
54 dyadsIter = dyadsFile.query(chr, paddedStart, paddedStop);
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
55 smoothedIter = smoothedDyadsFile.query(chr, paddedStart, paddedStop);
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
56 } catch (IOException | WigFileException e) {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
57 e.printStackTrace();
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
58 throw new CommandLineToolException("Error loading data from Wig file");
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 float[] dyads = WigFile.flattenData(dyadsIter, paddedStart, paddedStop);
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
62 float[] smoothed = WigFile.flattenData(smoothedIter, paddedStart, paddedStop);
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
63 int[] sortedIndices = SortUtils.indexSort(smoothed);
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
64
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
65 // Proceed through the data in descending order
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
66 log.debug("Calling nucleosomes");
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
67 for (int j = sortedIndices.length; j >= 0; j++) {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
68 int i = sortedIndices[j];
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
69 int dyad = paddedStart + i;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
70
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
71 if (smoothed[i] > 0) {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
72 int nucStart = Math.max(1, dyad-halfNuc);
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
73 int nucStop = Math.min(dyad+halfNuc, chrStop);
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
74 NucleosomeCall call = new NucleosomeCall(chr, nucStart, nucStop);
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
75 call.setDyad(dyad);
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
76
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
77 double occupancy = 0;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
78 double weightedSum = 0;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
79 double smoothedSum = 0;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
80 double sumOfSquares = 0;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
81 for (int bp = nucStart; bp <= nucStop; bp++) {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
82 occupancy += dyads[bp-paddedStart];
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
83 weightedSum += bp * dyads[bp-paddedStart];
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
84 smoothedSum += smoothed[bp-paddedStart];
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
85 sumOfSquares += bp * bp * dyads[bp-paddedStart];
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
86 }
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
87 call.setOccupancy(occupancy);
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
88
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
89 if (occupancy > 0) {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
90 call.setDyadMean(Math.round(weightedSum/occupancy));
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
91 call.setConditionalPosition(smoothed[i] / smoothedSum);
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
92 double variance = (sumOfSquares - weightedSum*call.getDyadMean()) / occupancy;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
93 call.setDyadStdev(Math.sqrt(variance));
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
94
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
95 // Only write nucleosomes within the current chunk to disk
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
96 if (chunkStart <= dyad && dyad <= chunkStop) {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
97 writer.write(call.toString());
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
98 writer.newLine();
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
99 }
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
100
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
101 // Don't allow nucleosome calls overlapping this nucleosome
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
102 int low = Math.max(i-nucleosomeSize, 0);
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
103 int high = Math.min(i-nucleosomeSize, paddedStop-1);
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
104 for (int k = low; k <= high; k++) {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
105 smoothed[k] = 0;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
106 }
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
107 }
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
108 }
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
109 }
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
110
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
111 chunkStart = chunkStop + 1;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
112 }
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
113 }
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
114 }
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
115 }
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
116
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
117 public static void main(String[] args) {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
118 new GreedyCaller().instanceMain(args);
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
119 }
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
120 }