-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy pathwrappers.d
420 lines (351 loc) · 11.8 KB
/
wrappers.d
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
module cram.wrappers;
import cram.htslib, cram.exception;
import std.typecons, std.parallelism, std.range;
debug import std.stdio;
auto zeroChecked(alias func, T...)(string err_msg, auto ref T params) {
int ret = func(params);
if (!ret)
throw new CramException(err_msg);
}
struct RcPtr(T, alias Free) {
static struct Payload {
debug {
static size_t payload_counter;
size_t payload_id;
}
T* ptr;
this(T* ptr) {
this.ptr = ptr;
debug {
payload_id = ++payload_counter;
stderr.writeln("Init ", T.stringof, "* #", payload_id);
}
}
~this() {
debug {
stderr.writeln("Free ", T.stringof, "* #", payload_id);
}
Free(ptr);
}
alias ptr this;
this(this) { assert(false); }
void opAssign(Payload rhs) { assert(false); }
}
alias Data = RefCounted!(Payload, RefCountedAutoInitialize.no);
Data data;
alias data this;
bool isNull() @property const {
return !data.refCountedStore.isInitialized || data.ptr is null;
}
this(this)
{
static if (is(T == cram_slice)) {
debug writeln("COPIED #", data.payload_id + 1);
}
}
this(T* ptr) { data = Data(ptr); }
}
auto nullChecked(alias func, T...)(string err_msg, cram_fd* fd,
auto ref T other_params)
{
auto ptr = func(fd, other_params);
if (ptr !is null)
return ptr;
if (fd.err == 0)
return null;
throw new CramException(err_msg);
}
// instances of these types must be used within the same thread!
void my_cram_close(cram_fd* fd) {
import core.sys.posix.pthread;
pthread_mutex_destroy(&fd.metrics_lock);
pthread_mutex_destroy(&fd.ref_lock);
pthread_mutex_destroy(&fd.bam_list_lock);
cram_close(fd);
}
alias RcCramFd = RcPtr!(cram_fd, my_cram_close);
alias CramFd = RcCramFd;
CramFd openCram(string filename) {
import std.string : toStringz;
auto fd = cram_open(toStringz(filename), "rb");
if (fd == null)
throw new CramException("Can't open file " ~ filename);
cram_set_option(fd, cram_option.CRAM_OPT_DECODE_MD);
// initialize locks, but we will use the pool from D standard library
// instead of the htslib implementation
import core.sys.posix.pthread;
pthread_mutex_init(&fd.metrics_lock, null);
pthread_mutex_init(&fd.ref_lock, null);
pthread_mutex_init(&fd.bam_list_lock, null);
fd.shared_ref = 1;
fd.own_pool = 0;
fd.pool = null;
return CramFd(fd);
}
enum CramFilterResult {
skip, // skip over the object
pass, // the filter doesn't reject the object
stop // skip, and further iteration won't give any results
}
alias RcCramContainer = RcPtr!(cram_container, cram_free_container);
alias CramContainer = RcCramContainer;
alias CramContainerFilter = CramFilterResult delegate(cram_container*);
alias RcCramSlice = RcPtr!(cram_slice, cram_free_slice);
alias UndecodedSliceFilter = CramFilterResult delegate(cram_slice*);
struct CramSlice {
CramFd fd;
RcCramContainer container;
RcCramSlice slice;
alias slice this;
bool is_decoded() @property const {
return slice.crecs !is null;
}
}
struct CramContainerRange {
CramContainer front;
bool empty;
private CramFd _fd;
private CramContainerFilter _filter;
// the containers that doesn't pass the filter
// will be simply skipped without any decoding
this(CramFd fd, CramContainerFilter f)
{
_fd = fd;
_filter = f;
popFront();
}
void popFront() {
if (!front.isNull) {
// skip remaining blocks of previous container
while (front.curr_slice != front.max_slice) {
++front.curr_slice;
auto next_slice = cram_read_slice(_fd);
if (!next_slice)
throw new CramException("Failure in cram_read_slice");
cram_free_slice(next_slice);
}
}
auto err_msg = "Failed to read container header";
while (true) {
// read container header
debug writeln("cram_read_container");
auto ptr = nullChecked!cram_read_container(err_msg, _fd);
if (ptr is null) {
empty = true;
break;
}
front = CramContainer(ptr);
// apply the filter
if (_filter is null) break;
auto res = _filter(front);
if (res == CramFilterResult.stop) {
empty = true;
break;
}
if (res == CramFilterResult.pass) {
break;
}
if (-1 == cram_seek(_fd, front.length, 1))
throw new CramException("Failed to seek in CRAM file");
}
if (empty) return;
// read compression header block
err_msg = "Failed to read compression header block";
front.comp_hdr_block = nullChecked!cram_read_block(err_msg, _fd);
auto content_type = front.comp_hdr_block.content_type;
if (content_type != cram_content_type.COMPRESSION_HEADER)
throw new CramException(err_msg);
err_msg = "Failed to decode compression header";
front.comp_hdr = cram_decode_compression_header(_fd,
front.comp_hdr_block);
if (front.comp_hdr is null)
throw new CramException(err_msg);
if (!(front.comp_hdr.AP_delta)) {
// FIXME: in cram_decode.c, there's a mutex locked around this line,
// but it looks unnecessary
_fd.unsorted = 1;
}
}
}
auto containers(CramFd fd, CramContainerFilter f) {
return CramContainerRange(fd, f);
}
class UndecodedSliceRange {
private {
CramFd _fd;
ref CramContainer _container() @property {
assert(!_containers.empty);
return _containers.front;
}
CramContainerRange _containers;
UndecodedSliceFilter _sf;
}
this(CramFd fd, CramContainerFilter cf, UndecodedSliceFilter sf) {
_fd = fd;
_sf = sf;
_containers = containers(_fd, cf);
if (_containers.empty)
empty = true;
popFront();
}
CramSlice front;
bool empty;
// true == either front or empty is set
private bool readNextSliceFromCurrentContainer() {
assert(_container.curr_slice < _container.max_slice);
_container.curr_slice++;
auto err_msg = "Failure in cram_read_slice";
debug stderr.writeln("cram_read_slice (", _container.curr_slice,
"/", _container.max_slice, ")");
auto ptr = cram_read_slice(_fd);
if (ptr is null) {
throw new CramException(err_msg);
}
assert(ptr.hdr !is null);
ptr.last_apos = ptr.hdr.ref_seq_start;
if (_sf is null) { setupFront(ptr); return true; }
auto ret = _sf(ptr);
if (ret == CramFilterResult.pass) { setupFront(ptr); return true; }
if (ret == CramFilterResult.stop) { empty = true; return true; }
return false;
}
private void setupFront(cram_slice* ptr) {
front = CramSlice(_fd, _container, RcCramSlice(ptr));
}
void popFront() {
while (!empty) {
while (_container.curr_slice < _container.max_slice)
if (readNextSliceFromCurrentContainer())
return;
// no slice (satisfying the filter) in the current container
_containers.popFront();
if (_containers.empty) { empty = true; return; }
}
}
}
auto undecodedSlices(CramFd fd, CramContainerFilter cf,
UndecodedSliceFilter sf=null)
{
return new UndecodedSliceRange(fd, cf, sf);
}
void decodeSlice(cram_fd* fd, cram_container* c, cram_slice* s) {
auto err_msg = "Failure in cram_decode_slice";
// debug writeln("DECODING slice #", s.id + 1);
int ret = cram_decode_slice(fd, c, s, fd.header);
if (ret != 0)
throw new CramException(err_msg);
}
void decodeSlice(CramSlice slice) {
decodeSlice(slice.fd, slice.container, slice.data.ptr);
}
import bio.core.utils.roundbuf;
struct CramSliceDecoder(R)
if (isInputRange!R && is(ElementType!R == CramSlice))
{
private {
R _slices;
TaskPool _pool;
// FIXME: D arrays don't call element destructors when GC-d :(
RoundBuf!CramSlice _input_queue;
alias DecodeTask = Task!(decodeSlice,
cram_fd*, cram_container*, cram_slice*)*;
RoundBuf!DecodeTask _output_queue;
void putNextSliceIntoQueue() {
auto slice = _slices.front;
_input_queue.put(slice);
_slices.popFront();
// don't copy it between threads (ref. counting is not atomic)
cram_fd* fd = slice.fd;
cram_container* c = slice.container;
cram_slice* s = slice;
// debug writeln("PUT slice #", s.id + 1, " into queue");
version (serial) {
decodeSlice(fd, c, s);
} else {
auto t = task!decodeSlice(fd, c, s);
_pool.put(t);
_output_queue.put(t);
}
}
}
this(R slices, TaskPool pool) {
_slices = slices;
_pool = pool;
import std.algorithm : max;
auto size = max(1, pool.size) * 2;
_input_queue = RoundBuf!CramSlice(size);
_output_queue = RoundBuf!DecodeTask(size);
while (!_slices.empty && !_input_queue.full)
putNextSliceIntoQueue();
popFront();
}
CramSlice front;
bool empty;
void popFront() {
if (_input_queue.empty) {
empty = true;
return;
}
version (serial) {} else {
auto t = _output_queue.front;
_output_queue.popFront();
t.yieldForce(); // now _input_queue.front is decoded
}
front = _input_queue.front;
_input_queue.popFront();
// debug writeln("GET slice #", front.id + 1, " from queue");
if (!_slices.empty)
putNextSliceIntoQueue();
}
}
auto decode(R)(R slices, std.parallelism.TaskPool pool)
if(isInputRange!R && is(ElementType!R == CramSlice))
{
return CramSliceDecoder!R(slices, pool);
}
auto slices(CramFd fd, CramContainerFilter cf, UndecodedSliceFilter sf,
TaskPool pool)
{
auto undecoded_slices = new UndecodedSliceRange(fd, cf, sf);
return undecoded_slices.decode(pool);
}
// workaround for LDC #795 - doesn't use short-circuit evaluation
// TODO remove when 0.15.0 release comes out
auto joiner2(RoR)(RoR r)
if (isInputRange!RoR && isInputRange!(ElementType!RoR)) {
static struct Result {
private:
RoR _items;
ElementType!RoR _current;
public:
this(RoR r) {
_items = r;
_current = ElementType!RoR.init;
while (!_items.empty) {
_current = _items.front;
if (!_current.empty)
break;
_items.popFront();
}
}
@property auto empty() { return _items.empty; }
@property auto ref front() { assert(!empty); return _current.front; }
void popFront()
{
assert(!_current.empty);
_current.popFront();
if (_current.empty)
{
assert(!_items.empty);
_items.popFront();
while (!_items.empty) {
_current = _items.front;
if (!_current.empty)
break;
_items.popFront();
}
}
}
}
return Result(r);
}