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