1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.utgenome.util;
24
25 import java.util.Iterator;
26 import java.util.PriorityQueue;
27
28 import org.utgenome.gwt.utgb.client.bio.Interval;
29 import org.utgenome.gwt.utgb.client.bio.OnGenome;
30 import org.utgenome.gwt.utgb.client.canvas.IntervalTree;
31 import org.xerial.util.log.Logger;
32
33
34
35
36
37
38
39 public class ReadDepth {
40
41 private static Logger _logger = Logger.getLogger(ReadDepth.class);
42
43 private int startCursor = 0;
44 private int readCount = 0;
45 private IntervalTree<Interval> intervals = new IntervalTree<Interval>();
46
47 private PriorityQueue<Integer> boundary = new PriorityQueue<Integer>();
48 private int currentDepth = 0;
49
50 private final DepthOutput out;
51
52 protected ReadDepth(DepthOutput out) {
53 this.out = out;
54 }
55
56 public static interface DepthOutput {
57 public void reportDepth(String chr, int start, int end, int depth) throws Exception;
58 }
59
60
61
62
63
64
65
66
67 public static void compute(String chr, Iterator<OnGenome> cursor, DepthOutput out) throws Exception {
68 new ReadDepth(out).computeDepth(chr, cursor);
69 }
70
71 protected void computeDepth(String chr, Iterator<OnGenome> cursor) throws Exception {
72
73 for (; cursor.hasNext();) {
74 OnGenome read = cursor.next();
75 readCount++;
76
77 if (_logger.isDebugEnabled() && readCount > 0 && (readCount % 10000) == 0) {
78 _logger.debug(String.format("processed %d reads", readCount));
79 }
80
81 int start = read.getStart();
82 int end = read.getEnd();
83 boundary.add(end);
84
85 for (; !boundary.isEmpty();) {
86 int readEnd = boundary.peek();
87 if (readEnd > start)
88 break;
89
90 reportDepth(chr, startCursor, readEnd, currentDepth);
91 currentDepth--;
92 startCursor = readEnd;
93 boundary.poll();
94 }
95
96 if (startCursor < start)
97 reportDepth(chr, startCursor, start, currentDepth);
98 startCursor = start;
99 currentDepth++;
100 }
101
102 for (; !boundary.isEmpty();) {
103 int readEnd = boundary.peek();
104 reportDepth(chr, startCursor, readEnd, currentDepth);
105 currentDepth--;
106 startCursor = readEnd;
107 boundary.poll();
108 }
109
110 }
111
112 private void reportDepth(String chr, int start, int end, int depth) throws Exception {
113 out.reportDepth(chr, start, end, depth);
114 }
115
116 }