Mercurial > repos > timpalpant > java_genomics_toolkit
comparison java-genomics-toolkit/src/edu/unc/genomics/ngs/BaseAlignCounts.java @ 0:1daf3026d231
Upload alpha version
author | timpalpant |
---|---|
date | Mon, 13 Feb 2012 21:55:55 -0500 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:1daf3026d231 |
---|---|
1 package edu.unc.genomics.ngs; | |
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.CommandLineTool; | |
17 import edu.unc.genomics.Interval; | |
18 import edu.unc.genomics.io.IntervalFile; | |
19 | |
20 public class BaseAlignCounts extends CommandLineTool { | |
21 | |
22 private static final Logger log = Logger.getLogger(BaseAlignCounts.class); | |
23 | |
24 @Parameter(names = {"-i", "--input"}, description = "Input file (reads)", required = true) | |
25 public IntervalFile<? extends Interval> intervalFile; | |
26 @Parameter(names = {"-a", "--assembly"}, description = "Genome assembly", required = true) | |
27 public Assembly assembly; | |
28 @Parameter(names = {"-x", "--extend"}, description = "Extend reads from 5' end (default = read length)") | |
29 public Integer extend; | |
30 @Parameter(names = {"-o", "--output"}, description = "Output file (Wig)", required = true) | |
31 public Path outputFile; | |
32 | |
33 @Override | |
34 public void run() throws IOException { | |
35 log.debug("Initializing output file"); | |
36 int mapped = 0; | |
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 | |
58 Iterator<? extends Interval> it = intervalFile.query(chr, start, stop); | |
59 while (it.hasNext()) { | |
60 Interval entry = it.next(); | |
61 int entryStop = entry.getStop(); | |
62 if (extend != null) { | |
63 if (entry.isWatson()) { | |
64 entryStop = entry.getStart() + extend; | |
65 } else { | |
66 entryStop = entry.getStart() - extend; | |
67 } | |
68 } | |
69 | |
70 for (int i = entry.getStart(); i <= entryStop; i++) { | |
71 count[i-start]++; | |
72 } | |
73 mapped++; | |
74 } | |
75 | |
76 // Write the count at each base pair to the output file | |
77 for (int i = 0; i < count.length; i++) { | |
78 writer.write(count[i]); | |
79 writer.newLine(); | |
80 } | |
81 | |
82 // Process the next chunk | |
83 start = stop + 1; | |
84 } | |
85 } | |
86 } | |
87 | |
88 log.info("Mapped "+mapped+" reads"); | |
89 } | |
90 | |
91 public static void main(String[] args) { | |
92 new BaseAlignCounts().instanceMain(args); | |
93 } | |
94 } |