annotate java-genomics-toolkit/src/edu/unc/genomics/nucleosomes/MapDyads.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
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
12 import com.beust.jcommander.Parameter;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
13
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
14 import edu.ucsc.genome.TrackHeader;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
15 import edu.unc.genomics.Assembly;
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.PositiveIntegerValidator;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
19 import edu.unc.genomics.io.IntervalFile;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
20
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
21 public class MapDyads 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(MapDyads.class);
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
24
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
25 @Parameter(names = {"-i", "--input"}, description = "Input file (reads)", required = true)
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
26 public IntervalFile<? extends Interval> inputFile;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
27 @Parameter(names = {"-s", "--size"}, description = "Mononucleosome length (default: read length)", validateWith = PositiveIntegerValidator.class)
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
28 public Integer nucleosomeSize;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
29 @Parameter(names = {"-a", "--assembly"}, description = "Genome assembly", required = true)
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
30 public Assembly assembly;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
31 @Parameter(names = {"-o", "--output"}, description = "Output file (Wig)", required = true)
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
32 public Path outputFile;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
33
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
34 @Override
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
35 public void run() throws IOException {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
36 log.debug("Initializing output file");
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
37 int mapped = 0;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
38 try (BufferedWriter writer = Files.newBufferedWriter(outputFile, Charset.defaultCharset())) {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
39 // Write the Wiggle track header to the output file
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
40 TrackHeader header = new TrackHeader("wiggle_0");
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
41 header.setName("Converted " + inputFile.getPath().getFileName());
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
42 header.setDescription("Converted " + inputFile.getPath().getFileName());
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
43 writer.write(header.toString());
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
44 writer.newLine();
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
45
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
46 // Process each chromosome in the assembly
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
47 for (String chr : assembly) {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
48 log.debug("Processing chromosome " + chr);
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
49 // Write the contig header to the output file
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
50 writer.write("fixedStep chrom="+chr+" start=1 step=1 span=1");
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
51 writer.newLine();
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
52
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
53 int start = 1;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
54 while (start < assembly.getChrLength(chr)) {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
55 int stop = start + DEFAULT_CHUNK_SIZE - 1;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
56 int length = stop - start + 1;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
57 int[] count = new int[length];
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
58
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
59 Iterator<? extends Interval> it = inputFile.query(chr, start, stop);
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
60 while (it.hasNext()) {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
61 Interval entry = it.next();
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
62 if (nucleosomeSize == null) {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
63 count[entry.center()-start]++;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
64 } else {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
65 count[entry.getStart()+nucleosomeSize-start]++;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
66 }
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
67 mapped++;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
68 }
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
69
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
70 // Write the average at each base pair to the output file
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
71 for (int i = 0; i < count.length; i++) {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
72 writer.write(count[i]);
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
73 writer.newLine();
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
74 }
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
75
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
76 // Process the next chunk
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
77 start = stop + 1;
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
78 }
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
79 }
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
80 }
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
81
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
82 log.info("Mapped "+mapped+" reads");
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
83 }
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
84
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
85 public static void main(String[] args) {
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
86 new MapDyads().instanceMain(args);
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
87 }
1daf3026d231 Upload alpha version
timpalpant
parents:
diff changeset
88 }