| #include "samtools.pysam.h" |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| #include <config.h> |
|
|
| #include <stdlib.h> |
| #include <stdint.h> |
| #include <string.h> |
| #include <stdio.h> |
| #include <errno.h> |
| #include <zlib.h> |
| #include "bedidx.h" |
|
|
| #include "htslib/ksort.h" |
|
|
| #include "htslib/kseq.h" |
| KSTREAM_INIT(gzFile, gzread, 8192) |
|
|
| static inline int lt_pair_pos(hts_pair_pos_t a, hts_pair_pos_t b) { |
| if (a.beg == b.beg) return a.end < b.end; |
| return a.beg < b.beg; |
| } |
| KSORT_INIT_STATIC(hts_pair_pos_t, hts_pair_pos_t, lt_pair_pos) |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| typedef struct { |
| int n, m; |
| hts_pair_pos_t *a; |
| int *idx; |
| int filter; |
| } bed_reglist_t; |
|
|
| #include "htslib/khash.h" |
| KHASH_MAP_INIT_STR(reg, bed_reglist_t) |
|
|
| typedef kh_reg_t reghash_t; |
|
|
| #if 0 |
| |
| static void bed_print(void *reg_hash) { |
| reghash_t *h = (reghash_t *)reg_hash; |
| bed_reglist_t *p; |
| khint_t k; |
| int i; |
| const char *reg; |
|
|
| if (!h) { |
| fprintf(samtools_stdout, "Hash table is empty!\n"); |
| return; |
| } |
| for (k = kh_begin(h); k < kh_end(h); k++) { |
| if (kh_exist(h,k)) { |
| reg = kh_key(h,k); |
| fprintf(samtools_stdout, "Region: '%s'\n", reg); |
| if ((p = &kh_val(h,k)) != NULL && p->n > 0) { |
| fprintf(samtools_stdout, "Filter: %d\n", p->filter); |
| for (i=0; i<p->n; i++) { |
| fprintf(samtools_stdout, "\tinterval[%d]: %"PRIhts_pos"-%"PRIhts_pos"\n", |
| i,p->a[i].beg,p->a[i].end); |
| } |
| } else { |
| fprintf(samtools_stdout, "Region '%s' has no intervals!\n", reg); |
| } |
| } |
| } |
| } |
| #endif |
|
|
| static int *bed_index_core(int n, hts_pair_pos_t *a) |
| { |
| int i, j, l, *idx, *new_idx; |
| l = 0; idx = 0; |
| for (i = 0; i < n; ++i) { |
| hts_pos_t beg, end; |
| beg = a[i].beg >> LIDX_SHIFT; end = a[i].end >> LIDX_SHIFT; |
| if (l < end + 1) { |
| int old_l = l; |
| l = end + 1; |
| kroundup32(l); |
| new_idx = realloc(idx, l * sizeof(*idx)); |
| if (!new_idx) { |
| free(idx); |
| return NULL; |
| } |
| idx = new_idx; |
|
|
| for (j = old_l; j < l; ++j) |
| idx[j] = -1; |
| } |
|
|
| for (j = beg; j < end+1; ++j) |
| if (idx[j] < 0) |
| idx[j] = i; |
| } |
| return idx; |
| } |
|
|
| static void bed_index(void *_h) |
| { |
| reghash_t *h = (reghash_t*)_h; |
| khint_t k; |
| for (k = 0; k < kh_end(h); ++k) { |
| if (kh_exist(h, k)) { |
| bed_reglist_t *p = &kh_val(h, k); |
| if (p->idx) free(p->idx); |
| ks_introsort(hts_pair_pos_t, p->n, p->a); |
| p->idx = bed_index_core(p->n, p->a); |
| } |
| } |
| } |
|
|
| static int bed_minoff(const bed_reglist_t *p, hts_pos_t beg, hts_pos_t end) { |
| int i, min_off=0; |
|
|
| if (p && p->idx) { |
| min_off = (beg>>LIDX_SHIFT >= p->n)? p->idx[p->n-1] : p->idx[beg>>LIDX_SHIFT]; |
| if (min_off < 0) { |
| hts_pos_t n = beg>>LIDX_SHIFT; |
| if (n > p->n) |
| n = p->n; |
| for (i = n - 1; i >= 0; --i) |
| if (p->idx[i] >= 0) |
| break; |
| min_off = i >= 0? p->idx[i] : 0; |
| } |
| } |
|
|
| return min_off; |
| } |
|
|
| static int bed_overlap_core(const bed_reglist_t *p, hts_pos_t beg, hts_pos_t end) |
| { |
| int i, min_off; |
| if (p->n == 0) return 0; |
| min_off = bed_minoff(p, beg, end); |
|
|
| for (i = min_off; i < p->n; ++i) { |
| if (p->a[i].beg >= end) break; |
| if (p->a[i].end > beg && p->a[i].beg < end) |
| return 1; |
| } |
| return 0; |
| } |
|
|
| int bed_overlap(const void *_h, const char *chr, hts_pos_t beg, hts_pos_t end) |
| { |
| const reghash_t *h = (const reghash_t*)_h; |
| khint_t k; |
| if (!h) return 0; |
| k = kh_get(reg, h, chr); |
| if (k == kh_end(h)) return 0; |
| return bed_overlap_core(&kh_val(h, k), beg, end); |
| } |
|
|
| |
| |
| |
| |
| |
|
|
| void bed_unify(void *reg_hash) { |
|
|
| int i, j, new_n; |
| reghash_t *h; |
| bed_reglist_t *p; |
|
|
| if (!reg_hash) |
| return; |
|
|
| h = (reghash_t *)reg_hash; |
|
|
| for (i = kh_begin(h); i < kh_end(h); i++) { |
| if (!kh_exist(h,i) || !(p = &kh_val(h,i)) || !(p->n)) |
| continue; |
|
|
| for (new_n = 0, j = 1; j < p->n; j++) { |
| if (p->a[new_n].end < p->a[j].beg) { |
| p->a[++new_n] = p->a[j]; |
| } else { |
| if (p->a[new_n].end < p->a[j].end) |
| p->a[new_n].end = p->a[j].end; |
| } |
| } |
|
|
| p->n = ++new_n; |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| void *bed_read(const char *fn) |
| { |
| reghash_t *h = kh_init(reg); |
| gzFile fp; |
| kstream_t *ks = NULL; |
| int dret; |
| unsigned int line = 0, save_errno; |
| kstring_t str = { 0, 0, NULL }; |
|
|
| if (NULL == h) return NULL; |
| |
| fp = strcmp(fn, "-")? gzopen(fn, "r") : gzdopen(fileno(stdin), "r"); |
| if (fp == 0) return 0; |
| ks = ks_init(fp); |
| if (NULL == ks) goto fail; |
| int ks_len; |
| while ((ks_len = ks_getuntil(ks, KS_SEP_LINE, &str, &dret)) >= 0) { |
| char *ref = str.s, *ref_end; |
| uint64_t beg = 0, end = 0; |
| int num = 0; |
| khint_t k; |
| bed_reglist_t *p; |
|
|
| if (ks_len == 0) |
| continue; |
|
|
| line++; |
| while (*ref && isspace(*ref)) ref++; |
| if ('\0' == *ref) continue; |
| if ('#' == *ref) continue; |
| ref_end = ref; |
| while (*ref_end && !isspace(*ref_end)) ref_end++; |
| if ('\0' != *ref_end) { |
| *ref_end = '\0'; |
| num = sscanf(ref_end + 1, "%"SCNu64" %"SCNu64, &beg, &end); |
| } |
| if (1 == num) { |
| end = beg--; |
| } |
| if (num < 1 || end < beg) { |
| |
| |
| |
| if (0 == strcmp(ref, "browser")) continue; |
| if (0 == strcmp(ref, "track")) continue; |
| if (num < 1) { |
| fprintf(samtools_stderr, |
| "[bed_read] Parse error reading \"%s\" at line %u\n", |
| fn, line); |
| } else { |
| fprintf(samtools_stderr, |
| "[bed_read] Parse error reading \"%s\" at line %u : " |
| "end (%"PRIu64") must not be less " |
| "than start (%"PRIu64")\n", |
| fn, line, end, beg); |
| } |
| errno = 0; |
| goto fail; |
| } |
|
|
| |
| k = kh_get(reg, h, ref); |
| if (k == kh_end(h)) { |
| int ret; |
| char *s = strdup(ref); |
| if (NULL == s) goto fail; |
| k = kh_put(reg, h, s, &ret); |
| if (-1 == ret) { |
| free(s); |
| goto fail; |
| } |
| memset(&kh_val(h, k), 0, sizeof(bed_reglist_t)); |
| } |
| p = &kh_val(h, k); |
|
|
| |
| if (p->n == p->m) { |
| p->m = p->m ? p->m<<1 : 4; |
| hts_pair_pos_t *new_a = realloc(p->a, p->m * sizeof(p->a[0])); |
| if (NULL == new_a) goto fail; |
| p->a = new_a; |
| } |
| p->a[p->n].beg = beg; |
| p->a[p->n++].end = end; |
| } |
| |
| |
|
|
| if (gzclose(fp) != Z_OK) { |
| fp = NULL; |
| goto fail; |
| } |
| ks_destroy(ks); |
| free(str.s); |
| bed_index(h); |
| |
| return h; |
| fail: |
| save_errno = errno; |
| if (ks) ks_destroy(ks); |
| if (fp) gzclose(fp); |
| free(str.s); |
| bed_destroy(h); |
| errno = save_errno; |
| return NULL; |
| } |
|
|
| void bed_destroy(void *_h) |
| { |
| reghash_t *h; |
| khint_t k; |
|
|
| if (!_h) |
| return; |
|
|
| h = (reghash_t*)_h; |
| for (k = 0; k < kh_end(h); ++k) { |
| if (kh_exist(h, k)) { |
| free(kh_val(h, k).a); |
| free(kh_val(h, k).idx); |
| free((char*)kh_key(h, k)); |
| } |
| } |
| kh_destroy(reg, h); |
| } |
|
|
| static void *bed_insert(void *reg_hash, char *reg, hts_pos_t beg, hts_pos_t end) { |
|
|
| reghash_t *h; |
| khint_t k; |
| bed_reglist_t *p; |
|
|
| if (!reg_hash) |
| return NULL; |
|
|
| h = (reghash_t *)reg_hash; |
|
|
| |
| k = kh_get(reg, h, reg); |
| if (k == kh_end(h)) { |
| int ret; |
| char *s = strdup(reg); |
| if (NULL == s) goto fail; |
| k = kh_put(reg, h, s, &ret); |
| if (-1 == ret) { |
| free(s); |
| goto fail; |
| } |
| memset(&kh_val(h, k), 0, sizeof(bed_reglist_t)); |
| } |
| p = &kh_val(h, k); |
|
|
| |
| if (p->n == p->m) { |
| p->m = p->m ? p->m<<1 : 4; |
| hts_pair_pos_t *new_a = realloc(p->a, p->m * sizeof(p->a[0])); |
| if (NULL == new_a) goto fail; |
| p->a = new_a; |
| } |
| p->a[p->n].beg = beg; |
| p->a[p->n++].end = end; |
|
|
| fail: |
| return h; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
|
|
| static void *bed_filter(void *reg_hash, void *tmp_hash) { |
|
|
| reghash_t *h; |
| reghash_t *t; |
| bed_reglist_t *p, *q; |
| khint_t l, k; |
| hts_pair_pos_t *new_a; |
| int i, j, new_n, min_off; |
| const char *reg; |
| hts_pos_t beg, end; |
|
|
| h = (reghash_t *)reg_hash; |
| t = (reghash_t *)tmp_hash; |
| if (!h) |
| return NULL; |
| if (!t) |
| return h; |
|
|
| for (l = kh_begin(t); l < kh_end(t); l++) { |
| if (!kh_exist(t,l) || !(q = &kh_val(t,l)) || !(q->n)) |
| continue; |
|
|
| reg = kh_key(t,l); |
| k = kh_get(reg, h, reg); |
| if (k == kh_end(h) || !(p = &kh_val(h, k)) || !(p->n)) |
| continue; |
|
|
| new_a = calloc(q->n + p->n, sizeof(new_a[0])); |
| if (!new_a) |
| return NULL; |
| new_n = 0; |
|
|
| for (i = 0; i < q->n; i++) { |
| beg = q->a[i].beg; |
| end = q->a[i].end; |
|
|
| min_off = bed_minoff(p, beg, end); |
| for (j = min_off; j < p->n; ++j) { |
| if (p->a[j].beg >= end) break; |
| if (p->a[j].end > beg && p->a[j].beg < end) { |
| new_a[new_n].beg = MAX(p->a[j].beg, beg); |
| new_a[new_n++].end = MIN(p->a[j].end, end); |
| } |
| } |
| } |
|
|
| if (new_n > 0) { |
| free(p->a); |
| p->a = new_a; |
| p->n = new_n; |
| p->m = new_n; |
| p->filter = FILTERED; |
| } else { |
| free(new_a); |
| p->filter = ALL; |
| } |
| } |
|
|
| return h; |
| } |
|
|
| void *bed_hash_regions(void *reg_hash, char **regs, int first, int last, int *op) { |
|
|
| reghash_t *h = (reghash_t *)reg_hash; |
| reghash_t *t = NULL; |
|
|
| int i; |
| char reg[1024]; |
| const char *q; |
| int beg, end; |
|
|
| if (h) { |
| t = kh_init(reg); |
| if (!t) { |
| fprintf(samtools_stderr, "Error when creating the temporary region hash table!\n"); |
| return NULL; |
| } |
| } else { |
| h = kh_init(reg); |
| if (!h) { |
| fprintf(samtools_stderr, "Error when creating the region hash table!\n"); |
| return NULL; |
| } |
| *op = 1; |
| } |
|
|
| for (i=first; i<last; i++) { |
|
|
| |
| |
| |
| |
| |
| q = hts_parse_reg(regs[i], &beg, &end); |
| if (q) { |
| if ((int)(q - regs[i] + 1) > 1024) { |
| fprintf(samtools_stderr, "Region name '%s' is too long (bigger than %d).\n", regs[i], 1024); |
| continue; |
| } |
| strncpy(reg, regs[i], q - regs[i]); |
| reg[q - regs[i]] = 0; |
| } else { |
| |
| if (strlen(regs[i]) + 1 > 1024) { |
| fprintf(samtools_stderr, "Region name '%s' is too long (bigger than %d).\n", regs[i], 1024); |
| continue; |
| } |
| strcpy(reg, regs[i]); |
| beg = 0; end = INT_MAX; |
| } |
|
|
| |
| if (*op && !(bed_insert(h, reg, beg, end))) { |
| fprintf(samtools_stderr, "Error when inserting region='%s' in the bed hash table at address=%p!\n", regs[i], h); |
| } |
| |
| |
| if (!(*op) && !(bed_insert(t, reg, beg, end))) { |
| fprintf(samtools_stderr, "Error when inserting region='%s' in the temporary hash table at address=%p!\n", regs[i], t); |
| } |
| } |
|
|
| if (!(*op)) { |
| bed_index(t); |
| bed_unify(t); |
| h = bed_filter(h, t); |
| bed_destroy(t); |
| } |
|
|
| if (h) { |
| bed_index(h); |
| bed_unify(h); |
| } |
|
|
| return h; |
| } |
|
|
| const char* bed_get(void *reg_hash, int i, int filter) { |
|
|
| reghash_t *h; |
| bed_reglist_t *p; |
|
|
| if (!reg_hash) |
| return NULL; |
|
|
| h = (reghash_t *)reg_hash; |
| if (!kh_exist(h,i) || !(p = &kh_val(h,i)) || (p->filter < filter)) |
| return NULL; |
|
|
| return kh_key(h, i); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
|
|
| hts_reglist_t *bed_reglist(void *reg_hash, int filter, int *n_reg) { |
|
|
| reghash_t *h; |
| bed_reglist_t *p; |
| khint_t i; |
| hts_reglist_t *reglist = NULL; |
| int count = 0; |
| int j; |
|
|
| if (!reg_hash) |
| return NULL; |
|
|
| h = (reghash_t *)reg_hash; |
|
|
| for (i = kh_begin(h); i < kh_end(h); i++) { |
| if (!kh_exist(h,i) || !(p = &kh_val(h,i)) || (p->filter < filter)) |
| continue; |
| count++; |
| } |
| if (!count) |
| return NULL; |
|
|
| reglist = (hts_reglist_t *)calloc(count, sizeof(hts_reglist_t)); |
| if (!reglist) |
| return NULL; |
|
|
| *n_reg = count; |
| count = 0; |
|
|
| for (i = kh_begin(h); i < kh_end(h) && count < *n_reg; i++) { |
| if (!kh_exist(h,i) || !(p = &kh_val(h,i)) || (p->filter < filter)) |
| continue; |
|
|
| reglist[count].reg = kh_key(h,i); |
| reglist[count].intervals = (hts_pair32_t *)calloc(p->n, sizeof(hts_pair32_t)); |
| if(!(reglist[count].intervals)) { |
| hts_reglist_free(reglist, count); |
| return NULL; |
| } |
| reglist[count].count = p->n; |
| reglist[count].max_end = 0; |
|
|
| for (j = 0; j < p->n; j++) { |
| reglist[count].intervals[j].beg = p->a[j].beg; |
| reglist[count].intervals[j].end = p->a[j].end; |
|
|
| if (reglist[count].intervals[j].end > reglist[count].max_end) |
| reglist[count].max_end = reglist[count].intervals[j].end; |
| } |
| count++; |
| } |
|
|
| return reglist; |
| } |
|
|