Debian Codegraph @

bcftools/regidx.c

raw symbols SCIP
1 /*
2 Copyright (C) 2014-2018 Genome Research Ltd.
3
4 Author: Petr Danecek <[email protected]>
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 THE SOFTWARE.
23 */
24
25 #include <strings.h>
26 #include <htslib/hts.h>
27 #include <htslib/kstring.h>
28 #include <htslib/kseq.h>
29 #include <htslib/khash_str2int.h>
30 #include "regidx.h"
31
32 #define MAX_COOR_0 REGIDX_MAX // CSI and hts_itr_query limit, 0-based
33
34 #define iBIN(x) ((x)>>13)
35
36 typedef struct
37 {
38 uint32_t beg, end;
39 }
40 reg_t;
41
42 typedef struct
43 {
44 uint32_t pos, ireg; // y-coordinate and a pointer to reglist.reg and reglist.dat
45 }
46 pos_t;
47
48 typedef struct _reglist_t reglist_t;
49
50 typedef struct
51 {
52 uint32_t beg, end, ireg; // query coordinates and the active region
53 regidx_t *ridx;
54 reglist_t *list;
55 int active;
56 }
57 _itr_t;
58
59 // List of regions for one chromosome.
60 struct _reglist_t
61 {
62 uint32_t *idx, nidx; // index to list.reg+1
63 uint32_t nreg, mreg; // n:used, m:allocated
64 reg_t *reg; // regions
65 void *dat; // payload data
66 char *seq; // sequence name
67 int unsorted;
68
69 };
70
71 // Container of all sequences
72 struct _regidx_t
73 {
74 int nseq, mseq; // n:used, m:alloced
75 reglist_t *seq; // regions for each sequence
76 void *seq2regs; // hash for fast lookup from chr name to regions
77 char **seq_names;
78 regidx_free_f free; // function to free any data allocated by regidx_parse_f
79 regidx_parse_f parse; // parse one input line
80 void *usr; // user data to pass to regidx_parse_f
81 int payload_size;
82 void *payload; // temporary payload data set by regidx_parse_f (sequence is not known beforehand)
83 kstring_t str;
84 };
85
86 int regidx_seq_nregs(regidx_t *idx, const char *seq)
87 {
88 int iseq;
89 if ( khash_str2int_get(idx->seq2regs, seq, &iseq)!=0 ) return 0; // no such sequence
90 return idx->seq[iseq].nreg;
91 }
92
93 int regidx_nregs(regidx_t *idx)
94 {
95 int i, nreg = 0;
96 for (i=0; i<idx->nseq; i++) nreg += idx->seq[i].nreg;
97 return nreg;
98 }
99
100 char **regidx_seq_names(regidx_t *idx, int *n)
101 {
102 *n = idx->nseq;
103 return idx->seq_names;
104 }
105
106 int regidx_insert_list(regidx_t *idx, char *line, char delim)
107 {
108 kstring_t tmp = {0,0,0};
109 char *ss = line;
110 while ( *ss )
111 {
112 char *se = ss;
113 while ( *se && *se!=delim ) se++;
114 tmp.l = 0;
115 kputsn(ss, se-ss, &tmp);
116 if ( regidx_insert(idx,tmp.s) < 0 )
117 {
118 free(tmp.s);
119 return -1;
120 }
121 if ( !*se ) break;
122 ss = se+1;
123 }
124 free(tmp.s);
125 return 0;
126 }
127
128 static inline int cmp_regs(reg_t *a, reg_t *b)
129 {
130 if ( a->beg < b->beg ) return -1;
131 if ( a->beg > b->beg ) return 1;
132 if ( a->end < b->end ) return 1; // longer intervals come first
133 if ( a->end > b->end ) return -1;
134 return 0;
135 }
136 static int cmp_reg_ptrs(const void *a, const void *b)
137 {
138 return cmp_regs((reg_t*)a,(reg_t*)b);
139 }
140 static int cmp_reg_ptrs2(const void *a, const void *b)
141 {
142 return cmp_regs(*((reg_t**)a),*((reg_t**)b));
143 }
144
145 inline int regidx_push(regidx_t *idx, char *chr_beg, char *chr_end, uint32_t beg, uint32_t end, void *payload)
146 {
147 if ( beg > MAX_COOR_0 ) beg = MAX_COOR_0;
148 if ( end > MAX_COOR_0 ) end = MAX_COOR_0;
149
150 int rid;
151 idx->str.l = 0;
152 kputsn(chr_beg, chr_end-chr_beg+1, &idx->str);
153 if ( khash_str2int_get(idx->seq2regs, idx->str.s, &rid)!=0 )
154 {
155 // new chromosome
156 idx->nseq++;
157 int m_prev = idx->mseq;
158 hts_expand0(reglist_t,idx->nseq,idx->mseq,idx->seq);
159 hts_expand0(char*,idx->nseq,m_prev,idx->seq_names);
160 idx->seq_names[idx->nseq-1] = strdup(idx->str.s);
161 rid = khash_str2int_inc(idx->seq2regs, idx->seq_names[idx->nseq-1]);
162 }
163
164 reglist_t *list = &idx->seq[rid];
165 list->seq = idx->seq_names[rid];
166 list->nreg++;
167 int mreg = list->mreg;
168 hts_expand(reg_t,list->nreg,list->mreg,list->reg);
169 list->reg[list->nreg-1].beg = beg;
170 list->reg[list->nreg-1].end = end;
171 if ( idx->payload_size )
172 {
173 if ( mreg != list->mreg ) list->dat = realloc(list->dat,idx->payload_size*list->mreg);
174 memcpy((char *)list->dat + idx->payload_size*(list->nreg-1), payload, idx->payload_size);
175 }
176 if ( !list->unsorted && list->nreg>1 && cmp_regs(&list->reg[list->nreg-2],&list->reg[list->nreg-1])>0 ) list->unsorted = 1;
177 return 0;
178 }
179
180 int regidx_insert(regidx_t *idx, char *line)
181 {
182 if ( !line ) return 0;
183 char *chr_from, *chr_to;
184 uint32_t beg,end;
185 int ret = idx->parse(line,&chr_from,&chr_to,&beg,&end,idx->payload,idx->usr);
186 if ( ret==-2 ) return -1; // error
187 if ( ret==-1 ) return 0; // skip the line
188 regidx_push(idx, chr_from,chr_to,beg,end,idx->payload);
189 return 0;
190 }
191
192 regidx_t *regidx_init_string(const char *str, regidx_parse_f parser, regidx_free_f free_f, size_t payload_size, void *usr_dat)
193 {
194 regidx_t *idx = (regidx_t*) calloc(1,sizeof(regidx_t));
195 if ( !idx ) return NULL;
196
197 idx->free = free_f;
198 idx->parse = parser ? parser : regidx_parse_tab;
199 idx->usr = usr_dat;
200 idx->seq2regs = khash_str2int_init();
201 idx->payload_size = payload_size;
202 if ( payload_size ) idx->payload = malloc(payload_size);
203
204 kstring_t tmp = {0,0,0};
205 const char *ss = str;
206 while ( *ss )
207 {
208 while ( *ss && isspace(*ss) ) ss++;
209 const char *se = ss;
210 while ( *se && *se!='\r' && *se!='\n' ) se++;
211 tmp.l = 0;
212 kputsn(ss, se-ss, &tmp);
213 regidx_insert(idx,tmp.s);
214 while ( *se && isspace(*se) ) se++;
215 ss = se;
216 }
217 free(tmp.s);
218 return idx;
219 }
220
221 regidx_t *regidx_init(const char *fname, regidx_parse_f parser, regidx_free_f free_f, size_t payload_size, void *usr_dat)
222 {
223 if ( !parser )
224 {
225 if ( !fname ) parser = regidx_parse_tab;
226 else
227 {
228 int len = strlen(fname);
229 if ( len>=7 && !strcasecmp(".bed.gz",fname+len-7) )
230 parser = regidx_parse_bed;
231 else if ( len>=8 && !strcasecmp(".bed.bgz",fname+len-8) )
232 parser = regidx_parse_bed;
233 else if ( len>=4 && !strcasecmp(".bed",fname+len-4) )
234 parser = regidx_parse_bed;
235 else if ( len>=4 && !strcasecmp(".vcf",fname+len-4) )
236 parser = regidx_parse_vcf;
237 else if ( len>=7 && !strcasecmp(".vcf.gz",fname+len-7) )
238 parser = regidx_parse_vcf;
239 else
240 parser = regidx_parse_tab;
241 }
242 }
243
244 regidx_t *idx = (regidx_t*) calloc(1,sizeof(regidx_t));
245 idx->free = free_f;
246 idx->parse = parser;
247 idx->usr = usr_dat;
248 idx->seq2regs = khash_str2int_init();
249 idx->payload_size = payload_size;
250 if ( payload_size ) idx->payload = malloc(payload_size);
251
252 if ( !fname ) return idx;
253
254 kstring_t str = {0,0,0};
255
256 htsFile *fp = hts_open(fname,"r");
257 if ( !fp ) goto error;
258
259 while ( hts_getline(fp, KS_SEP_LINE, &str) > 0 )
260 {
261 if ( regidx_insert(idx, str.s) ) goto error;
262 }
263
264 free(str.s);
265 if ( hts_close(fp)!=0 )
266 {
267 fprintf(stderr,"[%s] Error: close failed .. %s\n", __func__,fname);
268 goto error;
269 }
270 return idx;
271
272 error:
273 free(str.s);
274 if ( fp ) hts_close(fp);
275 regidx_destroy(idx);
276 return NULL;
277 }
278
279 void regidx_destroy(regidx_t *idx)
280 {
281 int i, j;
282 for (i=0; i<idx->nseq; i++)
283 {
284 reglist_t *list = &idx->seq[i];
285 if ( idx->free )
286 {
287 for (j=0; j<list->nreg; j++)
288 idx->free((char *)list->dat + idx->payload_size*j);
289 }
290 free(list->dat);
291 free(list->reg);
292 free(list->idx);
293 }
294 free(idx->seq_names);
295 free(idx->seq);
296 free(idx->str.s);
297 free(idx->payload);
298 khash_str2int_destroy_free(idx->seq2regs);
299 free(idx);
300 }
301
302 int _reglist_build_index(regidx_t *regidx, reglist_t *list)
303 {
304 int i;
305 if ( list->unsorted )
306 {
307 if ( !regidx->payload_size )
308 qsort(list->reg,list->nreg,sizeof(reg_t),cmp_reg_ptrs);
309 else
310 {
311 reg_t **ptr = (reg_t**) malloc(sizeof(reg_t*)*list->nreg);
312 for (i=0; i<list->nreg; i++) ptr[i] = list->reg + i;
313 qsort(ptr,list->nreg,sizeof(*ptr),cmp_reg_ptrs2);
314
315 void *tmp_dat = malloc(regidx->payload_size*list->nreg);
316 for (i=0; i<list->nreg; i++)
317 {
318 size_t iori = ptr[i] - list->reg;
319 memcpy((char *)tmp_dat+i*regidx->payload_size,
320 (char *)list->dat+iori*regidx->payload_size,
321 regidx->payload_size);
322 }
323 free(list->dat);
324 list->dat = tmp_dat;
325
326 reg_t *tmp_reg = (reg_t*) malloc(sizeof(reg_t)*list->nreg);
327 for (i=0; i<list->nreg; i++)
328 {
329 size_t iori = ptr[i] - list->reg;
330 tmp_reg[i] = list->reg[iori];
331 }
332 free(ptr);
333 free(list->reg);
334 list->reg = tmp_reg;
335 list->mreg = list->nreg;
336 }
337 list->unsorted = 0;
338 }
339
340 list->nidx = 0;
341 int j,k, midx = 0;
342 for (j=0; j<list->nreg; j++)
343 {
344 int ibeg = iBIN(list->reg[j].beg);
345 int iend = iBIN(list->reg[j].end);
346 if ( midx <= iend )
347 {
348 int old_midx = midx;
349 midx = iend + 1;
350 kroundup32(midx);
351 list->idx = (uint32_t*) realloc(list->idx, midx*sizeof(uint32_t));
352 memset(list->idx+old_midx, 0, sizeof(uint32_t)*(midx-old_midx));
353 }
354 if ( ibeg==iend )
355 {
356 if ( !list->idx[ibeg] ) list->idx[ibeg] = j + 1;
357 }
358 else
359 {
360 for (k=ibeg; k<=iend; k++)
361 if ( !list->idx[k] ) list->idx[k] = j + 1;
362 }
363 if ( list->nidx < iend+1 ) list->nidx = iend+1;
364 }
365
366 return 0;
367 }
368
369 int regidx_overlap(regidx_t *regidx, const char *chr, uint32_t beg, uint32_t end, regitr_t *regitr)
370 {
371 if ( regitr ) regitr->seq = NULL;
372
373 int iseq, ireg;
374 if ( khash_str2int_get(regidx->seq2regs, chr, &iseq)!=0 ) return 0; // no such sequence
375
376 reglist_t *list = &regidx->seq[iseq];
377 if ( !list->nreg ) return 0;
378
379 if ( list->nreg==1 )
380 {
381 if ( beg > list->reg[0].end ) return 0;
382 if ( end < list->reg[0].beg ) return 0;
383 ireg = 0;
384 }
385 else
386 {
387 if ( !list->idx )
388 _reglist_build_index(regidx,list);
389
390 int ibeg = iBIN(beg);
391 if ( ibeg >= list->nidx ) return 0; // beg is too big
392
393 // find a matching region
394 uint32_t i = list->idx[ibeg];
395 if ( !i )
396 {
397 int iend = iBIN(end);
398 if ( iend > list->nidx ) iend = list->nidx;
399 for (i=ibeg; i<=iend; i++)
400 if ( list->idx[i] ) break;
401 if ( i>iend ) return 0;
402 i = list->idx[i];
403 }
404 for (ireg=i-1; ireg<list->nreg; ireg++)
405 {
406 if ( list->reg[ireg].beg > end ) return 0; // no match, past the query region
407 if ( list->reg[ireg].end >= beg && list->reg[ireg].beg <= end ) break; // found
408 }
409
410 if ( ireg >= list->nreg ) return 0; // no match
411 }
412
413 if ( !regitr ) return 1; // match, but no more info to save
414
415 // may need to iterate over the matching regions later
416 _itr_t *itr = (_itr_t*)regitr->itr;
417 itr->ridx = regidx;
418 itr->list = list;
419 itr->beg = beg;
420 itr->end = end;
421 itr->ireg = ireg;
422 itr->active = 0;
423
424 regitr->seq = list->seq;
425 regitr->beg = list->reg[ireg].beg;
426 regitr->end = list->reg[ireg].end;
427 if ( regidx->payload_size )
428 regitr->payload = (char *)list->dat + regidx->payload_size*ireg;
429
430 return 1;
431 }
432
433 int regidx_parse_bed(const char *line, char **chr_beg, char **chr_end, uint32_t *beg, uint32_t *end, void *payload, void *usr)
434 {
435 char *ss = (char*) line;
436 while ( *ss && isspace(*ss) ) ss++;
437 if ( !*ss ) return -1; // skip blank lines
438 if ( *ss=='#' ) return -1; // skip comments
439
440 char *se = ss;
441 while ( *se && !isspace(*se) ) se++;
442
443 *chr_beg = ss;
444 *chr_end = se-1;
445
446 if ( !*se )
447 {
448 // just the chromosome name
449 *beg = 0;
450 *end = MAX_COOR_0;
451 return 0;
452 }
453
454 ss = se+1;
455 *beg = strtod(ss, &se);
456 if ( ss==se ) { fprintf(stderr,"Could not parse bed line: %s\n", line); return -2; }
457
458 ss = se+1;
459 *end = strtod(ss, &se) - 1;
460 if ( ss==se ) { fprintf(stderr,"Could not parse bed line: %s\n", line); return -2; }
461
462 return 0;
463 }
464
465 int regidx_parse_tab(const char *line, char **chr_beg, char **chr_end, uint32_t *beg, uint32_t *end, void *payload, void *usr)
466 {
467 char *ss = (char*) line;
468 while ( *ss && isspace(*ss) ) ss++;
469 if ( !*ss ) return -1; // skip blank lines
470 if ( *ss=='#' ) return -1; // skip comments
471
472 char *se = ss;
473 while ( *se && !isspace(*se) ) se++;
474
475 *chr_beg = ss;
476 *chr_end = se-1;
477
478 if ( !*se )
479 {
480 // just the chromosome name
481 *beg = 0;
482 *end = MAX_COOR_0;
483 return 0;
484 }
485
486 ss = se+1;
487 *beg = strtod(ss, &se);
488 if ( ss==se ) { fprintf(stderr,"Could not parse tab line: %s\n", line); return -2; }
489 if ( *beg==0 ) { fprintf(stderr,"Could not parse tab line, expected 1-based coordinate: %s\n", line); return -2; }
490 (*beg)--;
491
492 if ( !se[0] || !se[1] )
493 *end = *beg;
494 else
495 {
496 ss = se+1;
497 *end = strtod(ss, &se);
498 if ( ss==se || (*se && !isspace(*se)) ) *end = *beg;
499 else if ( *end==0 ) { fprintf(stderr,"Could not parse tab line, expected 1-based coordinate: %s\n", line); return -2; }
500 else (*end)--;
501 }
502 return 0;
503 }
504
505 int regidx_parse_vcf(const char *line, char **chr_beg, char **chr_end, uint32_t *beg, uint32_t *end, void *payload, void *usr)
506 {
507 int ret = regidx_parse_tab(line, chr_beg, chr_end, beg, end, payload, usr);
508 if ( !ret ) *end = *beg;
509 return ret;
510 }
511
512 int regidx_parse_reg(const char *line, char **chr_beg, char **chr_end, uint32_t *beg, uint32_t *end, void *payload, void *usr)
513 {
514 char *ss = (char*) line;
515 while ( *ss && isspace(*ss) ) ss++;
516 if ( !*ss ) return -1; // skip blank lines
517 if ( *ss=='#' ) return -1; // skip comments
518
519 char *se = ss;
520 while ( *se && *se!=':' ) se++;
521
522 *chr_beg = ss;
523 *chr_end = se-1;
524
525 if ( !*se )
526 {
527 *beg = 0;
528 *end = MAX_COOR_0;
529 return 0;
530 }
531
532 ss = se+1;
533 *beg = strtod(ss, &se);
534 if ( ss==se ) { fprintf(stderr,"Could not parse reg line: %s\n", line); return -2; }
535 if ( *beg==0 ) { fprintf(stderr,"Could not parse reg line, expected 1-based coordinate: %s\n", line); return -2; }
536 (*beg)--;
537
538 if ( !se[0] || !se[1] )
539 *end = se[0]=='-' ? MAX_COOR_0 : *beg;
540 else
541 {
542 ss = se+1;
543 *end = strtod(ss, &se);
544 if ( ss==se ) *end = *beg;
545 else if ( *end==0 ) { fprintf(stderr,"Could not parse reg line, expected 1-based coordinate: %s\n", line); return -2; }
546 else (*end)--;
547 }
548 return 0;
549 }
550
551 regitr_t *regitr_init(regidx_t *regidx)
552 {
553 regitr_t *regitr = (regitr_t*) calloc(1,sizeof(regitr_t));
554 regitr->itr = (_itr_t*) calloc(1,sizeof(_itr_t));
555 _itr_t *itr = (_itr_t*) regitr->itr;
556 itr->ridx = regidx;
557 itr->list = NULL;
558 return regitr;
559 }
560
561 void regitr_reset(regidx_t *regidx, regitr_t *regitr)
562 {
563 _itr_t *itr = (_itr_t*) regitr->itr;
564 memset(itr,0,sizeof(_itr_t));
565 itr->ridx = regidx;
566 }
567
568 void regitr_destroy(regitr_t *regitr)
569 {
570 free(regitr->itr);
571 free(regitr);
572 }
573
574 int regitr_overlap(regitr_t *regitr)
575 {
576 if ( !regitr->seq ) return 0;
577
578 _itr_t *itr = (_itr_t*) regitr->itr;
579 if ( !itr->active )
580 {
581 // is this the first call after regidx_overlap?
582 itr->active = 1;
583 itr->ireg++;
584 return 1;
585 }
586
587 reglist_t *list = itr->list;
588
589 int i;
590 for (i=itr->ireg; i<list->nreg; i++)
591 {
592 if ( list->reg[i].beg > itr->end ) return 0; // no match, past the query region
593 if ( list->reg[i].end >= itr->beg && list->reg[i].beg <= itr->end ) break; // found
594 }
595
596 if ( i >= list->nreg ) return 0; // no match
597
598 itr->ireg = i + 1;
599 regitr->seq = list->seq;
600 regitr->beg = list->reg[i].beg;
601 regitr->end = list->reg[i].end;
602 if ( itr->ridx->payload_size )
603 regitr->payload = (char *)list->dat + itr->ridx->payload_size*i;
604
605 return 1;
606 }
607
608 int regitr_loop(regitr_t *regitr)
609 {
610 _itr_t *itr = (_itr_t*) regitr->itr;
611 regidx_t *regidx = itr->ridx;
612
613 if ( !itr->list ) // first time here
614 {
615 itr->list = regidx->seq;
616 itr->ireg = 0;
617 }
618
619 size_t iseq = itr->list - regidx->seq;
620 if ( iseq >= regidx->nseq ) return 0;
621
622 if ( itr->ireg >= itr->list->nreg )
623 {
624 iseq++;
625 if ( iseq >= regidx->nseq ) return 0; // no more sequences, done
626 itr->ireg = 0;
627 itr->list = &regidx->seq[iseq];
628 }
629
630 regitr->seq = itr->list->seq;
631 regitr->beg = itr->list->reg[itr->ireg].beg;
632 regitr->end = itr->list->reg[itr->ireg].end;
633 if ( regidx->payload_size )
634 regitr->payload = (char *)itr->list->dat + regidx->payload_size*itr->ireg;
635 itr->ireg++;
636
637 return 1;
638 }
639
640
641 void regitr_copy(regitr_t *dst, regitr_t *src)
642 {
643 _itr_t *dst_itr = (_itr_t*) dst->itr;
644 _itr_t *src_itr = (_itr_t*) src->itr;
645 *dst_itr = *src_itr;
646 *dst = *src;
647 dst->itr = dst_itr;
648 }
649
650