forked from samtools/bcftools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
regidx.c
650 lines (559 loc) · 18 KB
/
regidx.c
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
/*
Copyright (C) 2014-2018 Genome Research Ltd.
Author: Petr Danecek <[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 <strings.h>
#include <htslib/hts.h>
#include <htslib/kstring.h>
#include <htslib/kseq.h>
#include <htslib/khash_str2int.h>
#include "regidx.h"
#define MAX_COOR_0 REGIDX_MAX // CSI and hts_itr_query limit, 0-based
#define iBIN(x) ((x)>>13)
typedef struct
{
uint32_t beg, end;
}
reg_t;
typedef struct
{
uint32_t pos, ireg; // y-coordinate and a pointer to reglist.reg and reglist.dat
}
pos_t;
typedef struct _reglist_t reglist_t;
typedef struct
{
uint32_t beg, end, ireg; // query coordinates and the active region
regidx_t *ridx;
reglist_t *list;
int active;
}
_itr_t;
// List of regions for one chromosome.
struct _reglist_t
{
uint32_t *idx, nidx; // index to list.reg+1
uint32_t nreg, mreg; // n:used, m:allocated
reg_t *reg; // regions
void *dat; // payload data
char *seq; // sequence name
int unsorted;
};
// Container of all sequences
struct _regidx_t
{
int nseq, mseq; // n:used, m:alloced
reglist_t *seq; // regions for each sequence
void *seq2regs; // hash for fast lookup from chr name to regions
char **seq_names;
regidx_free_f free; // function to free any data allocated by regidx_parse_f
regidx_parse_f parse; // parse one input line
void *usr; // user data to pass to regidx_parse_f
int payload_size;
void *payload; // temporary payload data set by regidx_parse_f (sequence is not known beforehand)
kstring_t str;
};
int regidx_seq_nregs(regidx_t *idx, const char *seq)
{
int iseq;
if ( khash_str2int_get(idx->seq2regs, seq, &iseq)!=0 ) return 0; // no such sequence
return idx->seq[iseq].nreg;
}
int regidx_nregs(regidx_t *idx)
{
int i, nreg = 0;
for (i=0; i<idx->nseq; i++) nreg += idx->seq[i].nreg;
return nreg;
}
char **regidx_seq_names(regidx_t *idx, int *n)
{
*n = idx->nseq;
return idx->seq_names;
}
int regidx_insert_list(regidx_t *idx, char *line, char delim)
{
kstring_t tmp = {0,0,0};
char *ss = line;
while ( *ss )
{
char *se = ss;
while ( *se && *se!=delim ) se++;
tmp.l = 0;
kputsn(ss, se-ss, &tmp);
if ( regidx_insert(idx,tmp.s) < 0 )
{
free(tmp.s);
return -1;
}
if ( !*se ) break;
ss = se+1;
}
free(tmp.s);
return 0;
}
static inline int cmp_regs(reg_t *a, reg_t *b)
{
if ( a->beg < b->beg ) return -1;
if ( a->beg > b->beg ) return 1;
if ( a->end < b->end ) return 1; // longer intervals come first
if ( a->end > b->end ) return -1;
return 0;
}
static int cmp_reg_ptrs(const void *a, const void *b)
{
return cmp_regs((reg_t*)a,(reg_t*)b);
}
static int cmp_reg_ptrs2(const void *a, const void *b)
{
return cmp_regs(*((reg_t**)a),*((reg_t**)b));
}
inline int regidx_push(regidx_t *idx, char *chr_beg, char *chr_end, uint32_t beg, uint32_t end, void *payload)
{
if ( beg > MAX_COOR_0 ) beg = MAX_COOR_0;
if ( end > MAX_COOR_0 ) end = MAX_COOR_0;
int rid;
idx->str.l = 0;
kputsn(chr_beg, chr_end-chr_beg+1, &idx->str);
if ( khash_str2int_get(idx->seq2regs, idx->str.s, &rid)!=0 )
{
// new chromosome
idx->nseq++;
int m_prev = idx->mseq;
hts_expand0(reglist_t,idx->nseq,idx->mseq,idx->seq);
hts_expand0(char*,idx->nseq,m_prev,idx->seq_names);
idx->seq_names[idx->nseq-1] = strdup(idx->str.s);
rid = khash_str2int_inc(idx->seq2regs, idx->seq_names[idx->nseq-1]);
}
reglist_t *list = &idx->seq[rid];
list->seq = idx->seq_names[rid];
list->nreg++;
int mreg = list->mreg;
hts_expand(reg_t,list->nreg,list->mreg,list->reg);
list->reg[list->nreg-1].beg = beg;
list->reg[list->nreg-1].end = end;
if ( idx->payload_size )
{
if ( mreg != list->mreg ) list->dat = realloc(list->dat,idx->payload_size*list->mreg);
memcpy((char *)list->dat + idx->payload_size*(list->nreg-1), payload, idx->payload_size);
}
if ( !list->unsorted && list->nreg>1 && cmp_regs(&list->reg[list->nreg-2],&list->reg[list->nreg-1])>0 ) list->unsorted = 1;
return 0;
}
int regidx_insert(regidx_t *idx, char *line)
{
if ( !line ) return 0;
char *chr_from, *chr_to;
uint32_t beg,end;
int ret = idx->parse(line,&chr_from,&chr_to,&beg,&end,idx->payload,idx->usr);
if ( ret==-2 ) return -1; // error
if ( ret==-1 ) return 0; // skip the line
regidx_push(idx, chr_from,chr_to,beg,end,idx->payload);
return 0;
}
regidx_t *regidx_init_string(const char *str, regidx_parse_f parser, regidx_free_f free_f, size_t payload_size, void *usr_dat)
{
regidx_t *idx = (regidx_t*) calloc(1,sizeof(regidx_t));
if ( !idx ) return NULL;
idx->free = free_f;
idx->parse = parser ? parser : regidx_parse_tab;
idx->usr = usr_dat;
idx->seq2regs = khash_str2int_init();
idx->payload_size = payload_size;
if ( payload_size ) idx->payload = malloc(payload_size);
kstring_t tmp = {0,0,0};
const char *ss = str;
while ( *ss )
{
while ( *ss && isspace(*ss) ) ss++;
const char *se = ss;
while ( *se && *se!='\r' && *se!='\n' ) se++;
tmp.l = 0;
kputsn(ss, se-ss, &tmp);
regidx_insert(idx,tmp.s);
while ( *se && isspace(*se) ) se++;
ss = se;
}
free(tmp.s);
return idx;
}
regidx_t *regidx_init(const char *fname, regidx_parse_f parser, regidx_free_f free_f, size_t payload_size, void *usr_dat)
{
if ( !parser )
{
if ( !fname ) parser = regidx_parse_tab;
else
{
int len = strlen(fname);
if ( len>=7 && !strcasecmp(".bed.gz",fname+len-7) )
parser = regidx_parse_bed;
else if ( len>=8 && !strcasecmp(".bed.bgz",fname+len-8) )
parser = regidx_parse_bed;
else if ( len>=4 && !strcasecmp(".bed",fname+len-4) )
parser = regidx_parse_bed;
else if ( len>=4 && !strcasecmp(".vcf",fname+len-4) )
parser = regidx_parse_vcf;
else if ( len>=7 && !strcasecmp(".vcf.gz",fname+len-7) )
parser = regidx_parse_vcf;
else
parser = regidx_parse_tab;
}
}
regidx_t *idx = (regidx_t*) calloc(1,sizeof(regidx_t));
idx->free = free_f;
idx->parse = parser;
idx->usr = usr_dat;
idx->seq2regs = khash_str2int_init();
idx->payload_size = payload_size;
if ( payload_size ) idx->payload = malloc(payload_size);
if ( !fname ) return idx;
kstring_t str = {0,0,0};
htsFile *fp = hts_open(fname,"r");
if ( !fp ) goto error;
while ( hts_getline(fp, KS_SEP_LINE, &str) > 0 )
{
if ( regidx_insert(idx, str.s) ) goto error;
}
free(str.s);
if ( hts_close(fp)!=0 )
{
fprintf(stderr,"[%s] Error: close failed .. %s\n", __func__,fname);
goto error;
}
return idx;
error:
free(str.s);
if ( fp ) hts_close(fp);
regidx_destroy(idx);
return NULL;
}
void regidx_destroy(regidx_t *idx)
{
int i, j;
for (i=0; i<idx->nseq; i++)
{
reglist_t *list = &idx->seq[i];
if ( idx->free )
{
for (j=0; j<list->nreg; j++)
idx->free((char *)list->dat + idx->payload_size*j);
}
free(list->dat);
free(list->reg);
free(list->idx);
}
free(idx->seq_names);
free(idx->seq);
free(idx->str.s);
free(idx->payload);
khash_str2int_destroy_free(idx->seq2regs);
free(idx);
}
int _reglist_build_index(regidx_t *regidx, reglist_t *list)
{
int i;
if ( list->unsorted )
{
if ( !regidx->payload_size )
qsort(list->reg,list->nreg,sizeof(reg_t),cmp_reg_ptrs);
else
{
reg_t **ptr = (reg_t**) malloc(sizeof(reg_t*)*list->nreg);
for (i=0; i<list->nreg; i++) ptr[i] = list->reg + i;
qsort(ptr,list->nreg,sizeof(*ptr),cmp_reg_ptrs2);
void *tmp_dat = malloc(regidx->payload_size*list->nreg);
for (i=0; i<list->nreg; i++)
{
size_t iori = ptr[i] - list->reg;
memcpy((char *)tmp_dat+i*regidx->payload_size,
(char *)list->dat+iori*regidx->payload_size,
regidx->payload_size);
}
free(list->dat);
list->dat = tmp_dat;
reg_t *tmp_reg = (reg_t*) malloc(sizeof(reg_t)*list->nreg);
for (i=0; i<list->nreg; i++)
{
size_t iori = ptr[i] - list->reg;
tmp_reg[i] = list->reg[iori];
}
free(ptr);
free(list->reg);
list->reg = tmp_reg;
list->mreg = list->nreg;
}
list->unsorted = 0;
}
list->nidx = 0;
int j,k, midx = 0;
for (j=0; j<list->nreg; j++)
{
int ibeg = iBIN(list->reg[j].beg);
int iend = iBIN(list->reg[j].end);
if ( midx <= iend )
{
int old_midx = midx;
midx = iend + 1;
kroundup32(midx);
list->idx = (uint32_t*) realloc(list->idx, midx*sizeof(uint32_t));
memset(list->idx+old_midx, 0, sizeof(uint32_t)*(midx-old_midx));
}
if ( ibeg==iend )
{
if ( !list->idx[ibeg] ) list->idx[ibeg] = j + 1;
}
else
{
for (k=ibeg; k<=iend; k++)
if ( !list->idx[k] ) list->idx[k] = j + 1;
}
if ( list->nidx < iend+1 ) list->nidx = iend+1;
}
return 0;
}
int regidx_overlap(regidx_t *regidx, const char *chr, uint32_t beg, uint32_t end, regitr_t *regitr)
{
if ( regitr ) regitr->seq = NULL;
int iseq, ireg;
if ( khash_str2int_get(regidx->seq2regs, chr, &iseq)!=0 ) return 0; // no such sequence
reglist_t *list = ®idx->seq[iseq];
if ( !list->nreg ) return 0;
if ( list->nreg==1 )
{
if ( beg > list->reg[0].end ) return 0;
if ( end < list->reg[0].beg ) return 0;
ireg = 0;
}
else
{
if ( !list->idx )
_reglist_build_index(regidx,list);
int ibeg = iBIN(beg);
if ( ibeg >= list->nidx ) return 0; // beg is too big
// find a matching region
uint32_t i = list->idx[ibeg];
if ( !i )
{
int iend = iBIN(end);
if ( iend > list->nidx ) iend = list->nidx;
for (i=ibeg; i<=iend; i++)
if ( list->idx[i] ) break;
if ( i>iend ) return 0;
i = list->idx[i];
}
for (ireg=i-1; ireg<list->nreg; ireg++)
{
if ( list->reg[ireg].beg > end ) return 0; // no match, past the query region
if ( list->reg[ireg].end >= beg && list->reg[ireg].beg <= end ) break; // found
}
if ( ireg >= list->nreg ) return 0; // no match
}
if ( !regitr ) return 1; // match, but no more info to save
// may need to iterate over the matching regions later
_itr_t *itr = (_itr_t*)regitr->itr;
itr->ridx = regidx;
itr->list = list;
itr->beg = beg;
itr->end = end;
itr->ireg = ireg;
itr->active = 0;
regitr->seq = list->seq;
regitr->beg = list->reg[ireg].beg;
regitr->end = list->reg[ireg].end;
if ( regidx->payload_size )
regitr->payload = (char *)list->dat + regidx->payload_size*ireg;
return 1;
}
int regidx_parse_bed(const char *line, char **chr_beg, char **chr_end, uint32_t *beg, uint32_t *end, void *payload, void *usr)
{
char *ss = (char*) line;
while ( *ss && isspace(*ss) ) ss++;
if ( !*ss ) return -1; // skip blank lines
if ( *ss=='#' ) return -1; // skip comments
char *se = ss;
while ( *se && !isspace(*se) ) se++;
*chr_beg = ss;
*chr_end = se-1;
if ( !*se )
{
// just the chromosome name
*beg = 0;
*end = MAX_COOR_0;
return 0;
}
ss = se+1;
*beg = strtod(ss, &se);
if ( ss==se ) { fprintf(stderr,"Could not parse bed line: %s\n", line); return -2; }
ss = se+1;
*end = strtod(ss, &se) - 1;
if ( ss==se ) { fprintf(stderr,"Could not parse bed line: %s\n", line); return -2; }
return 0;
}
int regidx_parse_tab(const char *line, char **chr_beg, char **chr_end, uint32_t *beg, uint32_t *end, void *payload, void *usr)
{
char *ss = (char*) line;
while ( *ss && isspace(*ss) ) ss++;
if ( !*ss ) return -1; // skip blank lines
if ( *ss=='#' ) return -1; // skip comments
char *se = ss;
while ( *se && !isspace(*se) ) se++;
*chr_beg = ss;
*chr_end = se-1;
if ( !*se )
{
// just the chromosome name
*beg = 0;
*end = MAX_COOR_0;
return 0;
}
ss = se+1;
*beg = strtod(ss, &se);
if ( ss==se ) { fprintf(stderr,"Could not parse tab line: %s\n", line); return -2; }
if ( *beg==0 ) { fprintf(stderr,"Could not parse tab line, expected 1-based coordinate: %s\n", line); return -2; }
(*beg)--;
if ( !se[0] || !se[1] )
*end = *beg;
else
{
ss = se+1;
*end = strtod(ss, &se);
if ( ss==se || (*se && !isspace(*se)) ) *end = *beg;
else if ( *end==0 ) { fprintf(stderr,"Could not parse tab line, expected 1-based coordinate: %s\n", line); return -2; }
else (*end)--;
}
return 0;
}
int regidx_parse_vcf(const char *line, char **chr_beg, char **chr_end, uint32_t *beg, uint32_t *end, void *payload, void *usr)
{
int ret = regidx_parse_tab(line, chr_beg, chr_end, beg, end, payload, usr);
if ( !ret ) *end = *beg;
return ret;
}
int regidx_parse_reg(const char *line, char **chr_beg, char **chr_end, uint32_t *beg, uint32_t *end, void *payload, void *usr)
{
char *ss = (char*) line;
while ( *ss && isspace(*ss) ) ss++;
if ( !*ss ) return -1; // skip blank lines
if ( *ss=='#' ) return -1; // skip comments
char *se = ss;
while ( *se && *se!=':' ) se++;
*chr_beg = ss;
*chr_end = se-1;
if ( !*se )
{
*beg = 0;
*end = MAX_COOR_0;
return 0;
}
ss = se+1;
*beg = strtod(ss, &se);
if ( ss==se ) { fprintf(stderr,"Could not parse reg line: %s\n", line); return -2; }
if ( *beg==0 ) { fprintf(stderr,"Could not parse reg line, expected 1-based coordinate: %s\n", line); return -2; }
(*beg)--;
if ( !se[0] || !se[1] )
*end = se[0]=='-' ? MAX_COOR_0 : *beg;
else
{
ss = se+1;
*end = strtod(ss, &se);
if ( ss==se ) *end = *beg;
else if ( *end==0 ) { fprintf(stderr,"Could not parse reg line, expected 1-based coordinate: %s\n", line); return -2; }
else (*end)--;
}
return 0;
}
regitr_t *regitr_init(regidx_t *regidx)
{
regitr_t *regitr = (regitr_t*) calloc(1,sizeof(regitr_t));
regitr->itr = (_itr_t*) calloc(1,sizeof(_itr_t));
_itr_t *itr = (_itr_t*) regitr->itr;
itr->ridx = regidx;
itr->list = NULL;
return regitr;
}
void regitr_reset(regidx_t *regidx, regitr_t *regitr)
{
_itr_t *itr = (_itr_t*) regitr->itr;
memset(itr,0,sizeof(_itr_t));
itr->ridx = regidx;
}
void regitr_destroy(regitr_t *regitr)
{
free(regitr->itr);
free(regitr);
}
int regitr_overlap(regitr_t *regitr)
{
if ( !regitr->seq ) return 0;
_itr_t *itr = (_itr_t*) regitr->itr;
if ( !itr->active )
{
// is this the first call after regidx_overlap?
itr->active = 1;
itr->ireg++;
return 1;
}
reglist_t *list = itr->list;
int i;
for (i=itr->ireg; i<list->nreg; i++)
{
if ( list->reg[i].beg > itr->end ) return 0; // no match, past the query region
if ( list->reg[i].end >= itr->beg && list->reg[i].beg <= itr->end ) break; // found
}
if ( i >= list->nreg ) return 0; // no match
itr->ireg = i + 1;
regitr->seq = list->seq;
regitr->beg = list->reg[i].beg;
regitr->end = list->reg[i].end;
if ( itr->ridx->payload_size )
regitr->payload = (char *)list->dat + itr->ridx->payload_size*i;
return 1;
}
int regitr_loop(regitr_t *regitr)
{
_itr_t *itr = (_itr_t*) regitr->itr;
regidx_t *regidx = itr->ridx;
if ( !itr->list ) // first time here
{
itr->list = regidx->seq;
itr->ireg = 0;
}
size_t iseq = itr->list - regidx->seq;
if ( iseq >= regidx->nseq ) return 0;
if ( itr->ireg >= itr->list->nreg )
{
iseq++;
if ( iseq >= regidx->nseq ) return 0; // no more sequences, done
itr->ireg = 0;
itr->list = ®idx->seq[iseq];
}
regitr->seq = itr->list->seq;
regitr->beg = itr->list->reg[itr->ireg].beg;
regitr->end = itr->list->reg[itr->ireg].end;
if ( regidx->payload_size )
regitr->payload = (char *)itr->list->dat + regidx->payload_size*itr->ireg;
itr->ireg++;
return 1;
}
void regitr_copy(regitr_t *dst, regitr_t *src)
{
_itr_t *dst_itr = (_itr_t*) dst->itr;
_itr_t *src_itr = (_itr_t*) src->itr;
*dst_itr = *src_itr;
*dst = *src;
dst->itr = dst_itr;
}