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