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