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