File: pileup.d

package info (click to toggle)
sambamba 1.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 3,528 kB
  • sloc: sh: 220; python: 166; ruby: 147; makefile: 103
file content (342 lines) | stat: -rw-r--r-- 8,335 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*
    This file is part of Sambamba.
    Copyright (C) 2017 Pjotr Prins <pjotr.prins@thebird.nl>

    Sambamba is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published
    by the Free Software Foundation; either version 2 of the License,
    or (at your option) any later version.

    Sambamba is distributed in the hope that it will be useful, but
    WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
    02111-1307 USA

*/
module bio.std.experimental.hts.pileup;

import std.conv;
import std.exception;
import std.stdio;
import std.traits;
import std.typecons;

import std.experimental.logger;

import bio.std.experimental.hts.constants;
import bio.core.utils.exception;

immutable ulong DEFAULT_BUFFER_SIZE = 1_000_000;

/**
   Cyclic buffer or ringbuffer based on Artem's original. Uses copy
   semantics to copy a read in a pre-allocated buffer. New items get
   added to the tail, and used items get popped from the head
   (FIFO). Basically it is empty when pointers align and full when
   head - tail equals length. Not that the pointers are of size_t
   which puts a theoretical limit on the number of items that can be
   pushed.
*/

import core.stdc.string : memcpy;

// alias ulong RingBufferIndex;

struct RingBufferIndex {
  alias Representation = ulong;
  private ulong value = 0;

  this(ulong v) {
    value = v;
  }

  // @disable this(this); // disable copy semantics;

  auto get() inout {
    return value;
  }

  auto max() @property {
    return value.max;
  }

  void opAssign(U)(U rhs) if (is(typeof(Checked!(T, Hook)(rhs)))) {
    value = rhs;
  }

  bool opEquals(U, this _)(U rhs) {
    return value == rhs;
  }

  auto opCmp(U, this _)(const U rhs) {
    return value < rhs ? -1 : value > rhs;
  }

  ulong opUnary(string s)() if (s == "++") {
    return ++value;
  }

}


struct RingBuffer(T) {

  T[] _items;
  RingBufferIndex _head;
  RingBufferIndex _tail;
  size_t max_size = 0;

  /** initializes round buffer of size $(D n) */
  this(size_t n) {
    _items = new T[n];
    // _items.reserve(n);
  }

  @disable this(this); // disable copy semantics;

  /*
  Does not work because data is no longer available!
  ~this() {
    // assert(is_empty); // make sure all items have been popped
  }
  */

  bool empty() @property @nogc nothrow const {
    return _tail == _head;
  }

  alias empty is_empty;

  auto ref front() @property {
    enforce(!is_empty, "ringbuffer is empty");
    return _items[_head.get() % $];
  }
  alias back last;

  auto ref back() @property {
    enforce(!is_empty, "ringbuffer is empty");
    return _items[(_tail.get() - 1) % $];
  }

  alias front first;

  bool is_tail(RingBufferIndex idx) {
    return idx == _tail.get()-1;
  }

  ref T get_at(RingBufferIndex idx) {
    enforce(!is_empty, "ringbuffer is empty");
    enforce(idx >= _head, "ringbuffer range error (idx before front)");
    enforce(idx != _tail, "ringbuffer range error (idx at end)");
    enforce(idx < _tail, "ringbuffer range error (idx after end)");
    return _items[idx.get() % $];
  }

  bool is_valid(RingBufferIndex idx) {
    enforce(!is_empty, "ringbuffer is empty");
    enforce(idx >= _head, "ringbuffer range error (idx before front)");
    enforce(idx != _tail, "ringbuffer range error (idx at end)");
    enforce(idx < _tail, "ringbuffer range error (idx after end)");
    return true;
  }

  // This function is a hack.
  void update_at(RingBufferIndex idx, T item) {
    is_valid(idx);
    _items[idx.get() % $] = item; // uses copy semantics
  }

  RingBufferIndex popFront() {
    enforce(!is_empty, "ringbuffer is empty");
    static if (__traits(compiles, _items[0].cleanupx)) {
      // write("x");
      _items[_head.get() % $].cleanup();
    }
    ++_head.value;
    return _head;
  }

  /// Puts item on the stack and returns the index
  RingBufferIndex put(T item) {
    enforce(!is_full, "ringbuffer is full - you need to expand buffer");
    enforce(_tail < _tail.max, "ringbuffer overflow");
    max_size = length > max_size ? length : max_size;
    _items[_tail.get() % $] = item; // uses copy semantics
    auto prev = _tail;
    ++_tail.value;
    return prev;
  }

  ulong length() @property const {
    // writeln(_tail.get(),":",_head.get(),"= len ",_tail.get()-_head.get());
    return _tail.get() - _head.get();
  }

  bool is_full() @property const {
    return _items.length == length();
  }

  bool in_range(RingBufferIndex idx) @property const {
    return idx >= _head && idx < _tail;
  }

  ulong pushed() @property const {
    return _tail.value;
  }
  ulong popped() @property const {
    return _head.value;
  }

  @property void cleanup() {
    _head = RingBufferIndex();
    _tail = RingBufferIndex();
  }

  string toString() {
    string res = "ring ";
    for(RingBufferIndex i = _head; i<_tail; i++)
      res ~= to!string(get_at(i));
    return res;
  }

  @property string stats() {
    return "Ringbuffer pushed " ~ to!string(pushed) ~ " popped " ~ to!string(popped) ~ " max-size " ~
      to!string(max_size) // , "/", (pileup.ring.max_size+1)/pileup.ring.length);
      ;
  }
}

unittest {
    auto buf = RingBuffer!int(4);
    assert(buf.is_empty);

    buf.put(1);
    buf.put(2);
    assert(buf.length == 2);
    assert(buf.front == 1);
    buf.popFront(); // 1
    buf.popFront(); // 2
    buf.put(2);
    buf.put(1);
    buf.put(0);
    buf.put(3);
    assert(buf.is_full);
    assert(buf.front == 2);
    buf.popFront();
    assert(buf.front == 1);
    buf.put(4);
    buf.popFront();
    assert(buf.front == 0);
    buf.popFront();
    assert(buf.front == 3);
    buf.popFront();
    assert(buf.front == 4);
    buf.popFront();
    assert(buf.is_empty);
}

/**
   Represent a pileup of reads in a buffer.
*/

/*
  Read RingBuffer with current pointer, so you have three states
  (first, current, last).
*/

class PileUp(R) {
  RingBuffer!R ring;
  Nullable!RingBufferIndex current;

  this(ulong bufsize=DEFAULT_BUFFER_SIZE) {
    ring = RingBuffer!R(bufsize);
    set_current_to_head;
  }

  RingBufferIndex push(R r) { return ring.put(r); }
  bool empty() @property const { return ring.is_empty();}
  bool is_full() @property const { return ring.is_full();}
  RingBufferIndex popFront() { return ring.popFront(); }
  ref R front() { return ring.front(); }
  alias front leftmost;
  ref R rightmost() { return ring.back(); }
  ref R read(RingBufferIndex idx) {
    enforce(ring.in_range(idx), "idx should be set for PileUp.read");
    return ring.get_at(idx);
  }
  ref R read_current() {
    enforce(!current.isNull, "current should be set for PileUp.read_current");
    return read(current.get);
  }
  bool is_at_end(RingBufferIndex idx) { return ring.is_tail(idx); }

  @property void current_inc() {
    asserte(!empty);
    asserte(!ring.is_tail(current.get));
    RingBufferIndex i = current.get.get + 1;
    current = i;
  }

  @property void set_current_to_head() {
    current = ring._head; // note pileup can be empty
  }

  void current_reset() {
    current = RingBufferIndex();
  }

  @property bool current_is_tail() {
    return ring.is_tail(current.get);
  }

  void each(void delegate(R) dg) {
    auto idx = ring._head;
    while(!ring.is_tail(idx)) {
      auto r = read(idx);
      dg(r);
      idx++;
    }
  }

  void each_left_of_current(void delegate(RingBufferIndex, R) dg) {
    R cur = read_current;
    if (cur.is_unmapped) return;
    auto idx = ring._head;
    while(!ring.is_tail(idx)) {
      auto r = read(idx);
      if (r.is_mapped && r.end_pos >= cur.start_pos)
        return;
      dg(idx,r);
      idx++;
    }
  }

  void purge_while(bool delegate(R) dg) {
    while(!empty) {
      if (!dg(front))
        return; // skip the rest
      popFront();
    }
  }

  void purge(void delegate(R) dg) {
    while(!empty) {
      dg(front);
      popFront();
    }
    set_current_to_head();
  }

  @property string stats() {
    return ring.stats();
  }

  override string toString() {
    return stats;
  }
}