annotate pyPRADA_1.2/tools/samtools-0.1.16/bedidx.c @ 3:f17965495ec9 draft default tip

Uploaded
author siyuan
date Tue, 11 Mar 2014 12:14:01 -0400
parents acc2ca1a3ba4
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
1 #include <stdlib.h>
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
2 #include <stdint.h>
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
3 #include <string.h>
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
4 #include <stdio.h>
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
5 #include <zlib.h>
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
6
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
7 #include "ksort.h"
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
8 KSORT_INIT_GENERIC(uint64_t)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
9
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
10 #include "kseq.h"
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
11 KSTREAM_INIT(gzFile, gzread, 8192)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
12
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
13 typedef struct {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
14 int n, m;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
15 uint64_t *a;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
16 int *idx;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
17 } bed_reglist_t;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
18
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
19 #include "khash.h"
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
20 KHASH_MAP_INIT_STR(reg, bed_reglist_t)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
21
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
22 #define LIDX_SHIFT 13
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
23
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
24 typedef kh_reg_t reghash_t;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
25
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
26 int *bed_index_core(int n, uint64_t *a, int *n_idx)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
27 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
28 int i, j, m, *idx;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
29 m = *n_idx = 0; idx = 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
30 for (i = 0; i < n; ++i) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
31 int beg, end;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
32 beg = a[i]>>32 >> LIDX_SHIFT; end = ((uint32_t)a[i]) >> LIDX_SHIFT;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
33 if (m < end + 1) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
34 int oldm = m;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
35 m = end + 1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
36 kroundup32(m);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
37 idx = realloc(idx, m * sizeof(int));
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
38 for (j = oldm; j < m; ++j) idx[j] = -1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
39 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
40 if (beg == end) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
41 if (idx[beg] < 0) idx[beg] = i;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
42 } else {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
43 for (j = beg; j <= end; ++j)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
44 if (idx[j] < 0) idx[j] = i;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
45 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
46 *n_idx = end + 1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
47 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
48 return idx;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
49 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
50
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
51 void bed_index(void *_h)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
52 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
53 reghash_t *h = (reghash_t*)_h;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
54 khint_t k;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
55 for (k = 0; k < kh_end(h); ++k) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
56 if (kh_exist(h, k)) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
57 bed_reglist_t *p = &kh_val(h, k);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
58 if (p->idx) free(p->idx);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
59 ks_introsort(uint64_t, p->n, p->a);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
60 p->idx = bed_index_core(p->n, p->a, &p->m);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
61 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
62 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
63 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
64
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
65 int bed_overlap_core(const bed_reglist_t *p, int beg, int end)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
66 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
67 int i, min_off;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
68 if (p->n == 0) return 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
69 min_off = (beg>>LIDX_SHIFT >= p->n)? p->idx[p->n-1] : p->idx[beg>>LIDX_SHIFT];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
70 if (min_off < 0) { // TODO: this block can be improved, but speed should not matter too much here
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
71 int n = beg>>LIDX_SHIFT;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
72 if (n > p->n) n = p->n;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
73 for (i = n - 1; i >= 0; --i)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
74 if (p->idx[i] >= 0) break;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
75 min_off = i >= 0? p->idx[i] : 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
76 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
77 for (i = min_off; i < p->n; ++i) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
78 if ((int)(p->a[i]>>32) >= end) break; // out of range; no need to proceed
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
79 if ((int32_t)p->a[i] > beg && (int32_t)(p->a[i]>>32) < end)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
80 return 1; // find the overlap; return
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
81 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
82 return 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
83 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
84
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
85 int bed_overlap(const void *_h, const char *chr, int beg, int end)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
86 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
87 const reghash_t *h = (const reghash_t*)_h;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
88 khint_t k;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
89 if (!h) return 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
90 k = kh_get(reg, h, chr);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
91 if (k == kh_end(h)) return 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
92 return bed_overlap_core(&kh_val(h, k), beg, end);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
93 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
94
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
95 void *bed_read(const char *fn)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
96 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
97 reghash_t *h = kh_init(reg);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
98 gzFile fp;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
99 kstream_t *ks;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
100 int dret;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
101 kstring_t *str;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
102 // read the list
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
103 fp = strcmp(fn, "-")? gzopen(fn, "r") : gzdopen(fileno(stdin), "r");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
104 if (fp == 0) return 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
105 str = calloc(1, sizeof(kstring_t));
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
106 ks = ks_init(fp);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
107 while (ks_getuntil(ks, 0, str, &dret) >= 0) { // read the chr name
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
108 int beg = -1, end = -1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
109 bed_reglist_t *p;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
110 khint_t k = kh_get(reg, h, str->s);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
111 if (k == kh_end(h)) { // absent from the hash table
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
112 int ret;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
113 char *s = strdup(str->s);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
114 k = kh_put(reg, h, s, &ret);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
115 memset(&kh_val(h, k), 0, sizeof(bed_reglist_t));
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
116 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
117 p = &kh_val(h, k);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
118 if (dret != '\n') { // if the lines has other characters
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
119 if (ks_getuntil(ks, 0, str, &dret) > 0 && isdigit(str->s[0])) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
120 beg = atoi(str->s); // begin
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
121 if (dret != '\n') {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
122 if (ks_getuntil(ks, 0, str, &dret) > 0 && isdigit(str->s[0]))
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
123 end = atoi(str->s); // end
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
124 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
125 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
126 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
127 if (dret != '\n') while ((dret = ks_getc(ks)) > 0 && dret != '\n'); // skip the rest of the line
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
128 if (end < 0 && beg > 0) end = beg, beg = beg - 1; // if there is only one column
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
129 if (beg >= 0 && end > beg) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
130 if (p->n == p->m) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
131 p->m = p->m? p->m<<1 : 4;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
132 p->a = realloc(p->a, p->m * 8);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
133 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
134 p->a[p->n++] = (uint64_t)beg<<32 | end;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
135 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
136 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
137 ks_destroy(ks);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
138 gzclose(fp);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
139 free(str->s); free(str);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
140 bed_index(h);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
141 return h;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
142 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
143
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
144 void bed_destroy(void *_h)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
145 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
146 reghash_t *h = (reghash_t*)_h;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
147 khint_t k;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
148 for (k = 0; k < kh_end(h); ++k) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
149 if (kh_exist(h, k)) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
150 free(kh_val(h, k).a);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
151 free(kh_val(h, k).idx);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
152 free((char*)kh_key(h, k));
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
153 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
154 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
155 kh_destroy(reg, h);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
156 }