Debian Codegraph @

bcftools/regidx.c.pysam.c

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