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