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