-
Notifications
You must be signed in to change notification settings - Fork 0
/
bcf_chunked_reader.cpp
344 lines (300 loc) · 10.8 KB
/
bcf_chunked_reader.cpp
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
/* The MIT License
Copyright (c) 2013 Adrian Tan <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "bcf_chunked_reader.h"
BCFChunkedReader::BCFChunkedReader(const char* input_pattern_name, const char* input_ref_file, const char* input_interval_list, int32_t unit, genomeLoci* pIntervals) : chunk(input_pattern_name, input_ref_file, input_interval_list, unit, pIntervals) {
init(pIntervals);
}
BCFChunkedReader::BCFChunkedReader(const char* input_vcf_file_name, genomeLoci* pIntervals) : chunk(input_vcf_file_name, pIntervals) {
init(pIntervals);
}
BCFChunkedReader::BCFChunkedReader(const char* input_pattern_name, const char* input_ref_file, int32_t unit_chunk, genomeLoci* pIntervals) : chunk(input_pattern_name, input_ref_file, unit_chunk, pIntervals) {
init(pIntervals);
}
BCFChunkedReader::BCFChunkedReader(const char* input_pattern_name, const char* input_interval_list, genomeLoci* pIntervals) : chunk(input_pattern_name, input_interval_list, pIntervals) {
init(pIntervals);
}
void BCFChunkedReader::init(const char* input_pattern_name, const char* input_ref_file, const char* input_interval_list, int32_t unit, genomeLoci* pIntervals) {
chunk.init(input_pattern_name, input_ref_file, input_interval_list, unit, pIntervals);
init(pIntervals);
}
void BCFChunkedReader::init(genomeLoci* pIntervals) {
//notice("[%s]", __PRETTY_FUNCTION__);
file = NULL;
hdr = NULL;
idx = NULL;
tbx = NULL;
itr = NULL;
if ( pIntervals ) {
this->target_intervals = *pIntervals;
this->target_intervals.resolveOverlaps();
}
//index_loaded = false;
//intervals_present = this->target_intervals.empty() ? false : true;
if ( !open_current_file() ) {
error("[%s:%d %s] Cannot open any of the input file. Last file name is %s\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, chunk.current_file_name.c_str());
}
}
bool BCFChunkedReader::jump_to(const char* chr, int32_t pos1) {
//notice("[%s:%d %s] started", __FILE__, __LINE__, __PRETTY_FUNCTION__);
std::pair<bool,bool> ret = chunk.jumpTo(chr,pos1);
//notice("[%s:%d %s] jumped", __FILE__, __LINE__, __PRETTY_FUNCTION__);
if ( !ret.first ) return false;
if ( ret.second )
open_current_file();
//notice("[%s:%d %s] Finished open_current_file()", __FILE__, __LINE__, __PRETTY_FUNCTION__);
int32_t end = chunk.chromosome_is_chunked ? chunk.chunk_intervals.it->end0 : INT_MAX;
if ( !target_intervals.empty() ) {
if ( chunk.chunk_intervals.it->overlaps(*target_intervals.it) ) {
if ( chunk.chunk_intervals.it->end0 > target_intervals.it->end0 )
end = target_intervals.it->end0;
}
}
//notice("[%s:%d %s] pos1 = %d, end = %d", __FILE__, __LINE__, __PRETTY_FUNCTION__, pos1, end);
if ( end < pos1 ) {
return false;
}
//load_index();
//notice("[%s:%d %s] Finished load_index()", __FILE__, __LINE__, __PRETTY_FUNCTION__);
char buf[65536];
sprintf(buf,"%s:%d-%d",chr, pos1, end);
load_index();
if ( ftype.format == bcf ) {
//notice("[%s:%d %s] Querying %s", __FILE__, __LINE__, __PRETTY_FUNCTION__, buf);
if ( itr ) hts_itr_destroy(itr);
itr = bcf_itr_querys(idx, hdr, buf);
if (itr)
return true;
}
else if (ftype.format==vcf && ftype.compression==bgzf) {
if ( itr ) hts_itr_destroy(itr);
itr = tbx_itr_querys(tbx, buf);
if (itr)
return true;
}
return false;
}
bool BCFChunkedReader::load_index() {
if (ftype.format==bcf) {
if ( idx == NULL ) {
if ( ( idx = bcf_index_load(chunk.current_file_name.c_str()) ) == NULL ) {
warning("[E:%s] index cannot be loaded for %s for random access\n", __PRETTY_FUNCTION__, chunk.current_file_name.c_str());
return false;
}
}
}
else if (ftype.format==vcf) {
if (ftype.compression==bgzf) {
if ( tbx == NULL ) {
if ( (tbx = tbx_index_load(chunk.current_file_name.c_str())) == NULL ) {
warning("[E:%s] index cannot be loaded for %s for random access\n", __PRETTY_FUNCTION__, chunk.current_file_name.c_str());
return false;
}
}
}
else
error("[E:%s] no random access support for non-BGZF VCF file: %s\n", __PRETTY_FUNCTION__, chunk.current_file_name.c_str());
}
return true;
}
bool BCFChunkedReader::open_current_file() {
close(false);
//notice("[%s], opening file %s",__PRETTY_FUNCTION__,chunk.current_file_name.c_str());
// attempts to load the first file
file = hts_open(chunk.current_file_name.c_str(), "r");
while( file == NULL ) {
notice("Skipping to read non-existent file %s", chunk.current_file_name.c_str());
if ( chunk.setNextFileName() ) {
file = hts_open(chunk.current_file_name.c_str(), "r");
}
else {
return false;
}
}
ftype = file->format;
if (ftype.format!=vcf && ftype.format!=bcf)
error("[%s:%d %s] Not a VCF/BCF file: %s\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, chunk.current_file_name.c_str());
s.l = s.m = 0; s.s = NULL;
if (file==NULL) error("[%s:%d %s] Something went wrong. Open file hanle is lost", __FILE__, __LINE__, __PRETTY_FUNCTION__);
if ( hdr ) bcf_hdr_destroy(hdr);
hdr = bcf_alt_hdr_read(file);
if (!hdr) error("[%s:%d %s]. Failed reading the BCF/VCF header from %s", __FILE__, __LINE__, __PRETTY_FUNCTION__, chunk.current_file_name.c_str());
// do not load the index right now. Load only when needed.
/*
if (ftype.format==bcf) {
if ( intervals_present ) {
if ( ( idx = bcf_index_load(chunk.current_file_name.c_str()) ) != NULL )
index_loaded = true;
else
error("[E:%s] index cannot be loaded for %s for random access\n", __PRETTY_FUNCTION__, chunk.current_file_name.c_str());
}
}
else if (ftype.format==vcf) {
if (ftype.compression==bgzf) {
if ( intervals_present ) {
if ((tbx = tbx_index_load(chunk.current_file_name.c_str())))
index_loaded = true;
else
error("[E:%s] index cannot be loaded for %s for random access\n", __PRETTY_FUNCTION__, chunk.current_file_name.c_str());
}
}
else {
if (intervals_present)
error("[E:%s] no random access support for non-BGZF VCF file: %s\n", __PRETTY_FUNCTION__, chunk.current_file_name.c_str());
}
}
*/
if ( !target_intervals.empty() ) {
target_intervals.rewind();
while( !initialize_current_interval() ) {
if ( target_intervals.isend() ) {
error("[E:%s] no overlapping interval found in : %s\n", __PRETTY_FUNCTION__, chunk.current_file_name.c_str());
}
target_intervals.next();
}
}
//notice("[%s], Finishing.. returning true",__PRETTY_FUNCTION__);
return true;
//random_access_enabled = intervals_present && index_loaded;
}
/**
* Gets sequence name of a record.
*/
const char* BCFChunkedReader::get_seqname(bcf1_t *v)
{
return bcf_get_chrom(hdr, v);
};
/**
* Checks if index is loaded.
*/
//bool BCFChunkedReader::is_index_loaded()
//{
// return index_loaded;
//}
/**
* Gets bcf header.
*/
bcf_hdr_t* BCFChunkedReader::get_hdr()
{
return hdr;
};
/**
* Initialize next interval.
* Returns false only if all intervals are accessed.
*/
bool BCFChunkedReader::initialize_current_interval() {
if ( chunk.chunk_intervals.empty() || ( chunk.chunk_intervals.it->overlaps(*target_intervals.it) ) ) {
char buf[65536];
sprintf(buf,"%s:%d-%d",target_intervals.it->chrom.c_str(),
(chunk.chunk_intervals.empty() || chunk.chunk_intervals.it->beg1 < target_intervals.it->beg1 ) ? target_intervals.it->beg1 : chunk.chunk_intervals.it->beg1,
(chunk.chunk_intervals.empty() || chunk.chunk_intervals.it->end0 > target_intervals.it->end0 ) ? target_intervals.it->end0 : chunk.chunk_intervals.it->end0);
load_index();
if ( ftype.format == bcf ) {
if ( itr ) hts_itr_destroy(itr);
itr = bcf_itr_querys(idx, hdr, buf);
if (itr)
return true;
}
else if (ftype.format==vcf && ftype.compression==bgzf) {
if ( itr ) hts_itr_destroy(itr);
itr = tbx_itr_querys(tbx, buf);
if (itr)
return true;
}
}
return false;
};
/**
* Reads next record, hides the random access of different regions from the user.
*/
bool BCFChunkedReader::read(bcf1_t *v) {
//bcf_clear(v);
//notice("BCFChunkedReader::read() called");
if ( itr ) { // when reading by indices
while( true ) { // loop forever..
// first,
if ( itr && ( ( ( ftype.format == bcf ) && ( bcf_itr_next(file,itr,v) >= 0 ) ) ||
( ( ftype.format == vcf ) && ( tbx_itr_next(file,tbx,itr,&s) >= 0 ) ) ) ) {
if ( ftype.format == vcf )
vcf_parse1(&s, hdr, v);
notice("Iterated a variant itr = %x, v->pos = %d, chr = %s, beg1 = %d, end0 = %d", itr, v->pos, chunk.chunk_intervals.it->chrom.c_str(), chunk.chunk_intervals.it->beg1, chunk.chunk_intervals.it->end0);
if ( ( v->pos + 1 < chunk.chunk_intervals.it->beg1 ) || ( v->pos + 1 > chunk.chunk_intervals.it->end0 ) )
continue;
return true;
}
else {
notice("looking at next target intervals. itr = %x", itr);
if ( target_intervals.next() ) {
notice("Initializing current interval");
return initialize_current_interval();
}
else {
notice("checking next file");
if ( chunk.setNextFileName() ) {
notice("setNextFileName returned true");
if ( open_current_file() ) {
// do nothing; current interval will be initialized
}
else return false;
}
else return false;
}
}
}
}
else {
//notice("iterator absent");
if (bcf_read(file, hdr, v)==0)
return true;
else if ( chunk.setNextFileName() ) {
if ( open_current_file() ) {
return (read(v));
}
else
return false;
}
else
return false;
}
}
/**
* Closes the file.
*/
void BCFChunkedReader::close(bool destroy_hdr)
{
if ( file ) {
bcf_close(file);
file = NULL;
}
if ( destroy_hdr && hdr ) {
bcf_hdr_destroy(hdr);
hdr = NULL;
}
if ( idx ) {
hts_idx_destroy(idx);
idx = NULL;
}
if ( tbx ) {
tbx_destroy(tbx);
tbx = NULL;
}
if ( itr ) {
hts_itr_destroy(itr);
itr = NULL;
}
}