8 KHASH_MAP_INIT_STR(str2id, int)
10 void *bcf_build_refhash(bcf_hdr_t *h)
12 khash_t(str2id) *hash;
14 hash = kh_init(str2id);
15 for (i = 0; i < h->n_ref; ++i) {
17 k = kh_put(str2id, hash, h->ns[i], &ret); // FIXME: check ret
23 void *bcf_str2id_init()
25 return kh_init(str2id);
28 void bcf_str2id_destroy(void *_hash)
30 khash_t(str2id) *hash = (khash_t(str2id)*)_hash;
31 if (hash) kh_destroy(str2id, hash); // Note that strings are not freed.
34 void bcf_str2id_thorough_destroy(void *_hash)
36 khash_t(str2id) *hash = (khash_t(str2id)*)_hash;
38 if (hash == 0) return;
39 for (k = 0; k < kh_end(hash); ++k)
40 if (kh_exist(hash, k)) free((char*)kh_key(hash, k));
41 kh_destroy(str2id, hash);
44 int bcf_str2id(void *_hash, const char *str)
46 khash_t(str2id) *hash = (khash_t(str2id)*)_hash;
49 k = kh_get(str2id, hash, str);
50 return k == kh_end(hash)? -1 : kh_val(hash, k);
53 int bcf_str2id_add(void *_hash, const char *str)
57 khash_t(str2id) *hash = (khash_t(str2id)*)_hash;
59 k = kh_put(str2id, hash, str, &ret);
60 if (ret == 0) return kh_val(hash, k);
61 kh_val(hash, k) = kh_size(hash) - 1;
62 return kh_val(hash, k);
65 int bcf_shrink_alt(bcf1_t *b, int n)
68 int i, j, k, n_smpl = b->n_smpl;
69 if (b->n_alleles <= n) return -1;
72 for (p = b->alt, k = 1; *p; ++p)
73 if (*p == ',' && ++k == n) break;
75 } else p = b->alt, *p = '\0';
77 memmove(p, b->flt, b->str + b->l_str - b->flt);
78 b->l_str -= b->flt - p;
80 for (i = 0; i < b->n_gi; ++i) {
81 bcf_ginfo_t *g = b->gi + i;
82 if (g->fmt == bcf_str2int("PL", 2)) {
83 int l, x = b->n_alleles * (b->n_alleles + 1) / 2;
84 uint8_t *d = (uint8_t*)g->data;
85 g->len = n * (n + 1) / 2;
86 for (l = k = 0; l < n_smpl; ++l) {
87 uint8_t *dl = d + l * x;
88 for (j = 0; j < g->len; ++j) d[k++] = dl[j];
97 int bcf_gl2pl(bcf1_t *b)
100 int i, n_smpl = b->n_smpl;
104 if (strstr(b->fmt, "PL")) return -1;
105 if ((p = strstr(b->fmt, "GL")) == 0) return -1;
107 for (i = 0; i < b->n_gi; ++i)
108 if (b->gi[i].fmt == bcf_str2int("GL", 2))
111 g->fmt = bcf_str2int("PL", 2);
112 g->len /= 4; // 4 == sizeof(float)
113 d0 = (float*)g->data; d1 = (uint8_t*)g->data;
114 for (i = 0; i < n_smpl * g->len; ++i) {
115 int x = (int)(-10. * d0[i] + .499);
116 if (x > 255) x = 255;
122 /* FIXME: this function will fail given AB:GTX:GT. BCFtools never
123 * produces such FMT, but others may do. */
124 int bcf_fix_gt(bcf1_t *b)
130 // check the presence of the GT FMT
131 if ((s = strstr(b->fmt, ":GT")) == 0) return 0; // no GT or GT is already the first
132 if (s[3] != '\0' && s[3] != ':') return 0; // :GTX in fact
133 tmp = bcf_str2int("GT", 2);
134 for (i = 0; i < b->n_gi; ++i)
135 if (b->gi[i].fmt == tmp) break;
136 if (i == b->n_gi) return 0; // no GT in b->gi; probably a bug...
138 // move GT to the first
139 for (; i > 0; --i) b->gi[i] = b->gi[i-1];
141 memmove(b->fmt + 3, b->fmt, s + 1 - b->fmt);
142 b->fmt[0] = 'G'; b->fmt[1] = 'T'; b->fmt[2] = ':';
146 int bcf_fix_pl(bcf1_t *b)
153 tmp = bcf_str2int("PL", 2);
154 for (i = 0; i < b->n_gi; ++i)
155 if (b->gi[i].fmt == tmp) break;
156 if (i == b->n_gi) return 0;
159 PL = (uint8_t*)gi->data;
160 swap = alloca(gi->len);
161 // loop through individuals
162 for (i = 0; i < b->n_smpl; ++i) {
164 uint8_t *PLi = PL + i * gi->len;
165 memcpy(swap, PLi, gi->len);
166 for (k = x = 0; k < b->n_alleles; ++k)
167 for (l = k; l < b->n_alleles; ++l)
168 PLi[l*(l+1)/2 + k] = swap[x++];
173 int bcf_smpl_covered(const bcf1_t *b)
179 tmp = bcf_str2int("PL", 2);
180 for (i = 0; i < b->n_gi; ++i)
181 if (b->gi[i].fmt == tmp) break;
182 if (i == b->n_gi) return 0;
183 // count how many samples having PL!=[0..0]
185 for (i = 0; i < b->n_smpl; ++i) {
186 uint8_t *PLi = ((uint8_t*)gi->data) + i * gi->len;
187 for (j = 0; j < gi->len; ++j)
189 if (j < gi->len) ++n;
194 static void *locate_field(const bcf1_t *b, const char *fmt, int l)
198 tmp = bcf_str2int(fmt, l);
199 for (i = 0; i < b->n_gi; ++i)
200 if (b->gi[i].fmt == tmp) break;
201 return i == b->n_gi? 0 : b->gi[i].data;
204 int bcf_anno_max(bcf1_t *b)
206 int k, max_gq, max_sp, n_het;
210 max_gq = max_sp = n_het = 0;
211 gt = locate_field(b, "GT", 2);
212 if (gt == 0) return -1;
213 gq = locate_field(b, "GQ", 2);
214 sp = locate_field(b, "SP", 2);
216 for (k = 0; k < b->n_smpl; ++k)
218 max_sp = max_sp > (int)sp[k]? max_sp : sp[k];
220 for (k = 0; k < b->n_smpl; ++k)
222 max_gq = max_gq > (int)gq[k]? max_gq : gq[k];
223 for (k = 0; k < b->n_smpl; ++k) {
225 a1 = gt[k]&7; a2 = gt[k]>>3&7;
226 if ((!a1 && a2) || (!a2 && a1)) { // a het
227 if (gq == 0) ++n_het;
228 else if (gq[k] >= 20) ++n_het;
231 if (n_het) max_sp -= (int)(4.343 * log(n_het) + .499);
232 if (max_sp < 0) max_sp = 0;
233 memset(&str, 0, sizeof(kstring_t));
234 if (*b->info) kputc(';', &str);
235 ksprintf(&str, "MXSP=%d;MXGQ=%d", max_sp, max_gq);
236 bcf_append_info(b, str.s, str.l);
241 // FIXME: only data are shuffled; the header is NOT
242 int bcf_shuffle(bcf1_t *b, int seed)
245 if (seed > 0) srand48(seed);
246 a = malloc(b->n_smpl * sizeof(int));
247 for (i = 0; i < b->n_smpl; ++i) a[i] = i;
248 for (i = b->n_smpl; i > 1; --i) {
250 j = (int)(drand48() * i);
251 tmp = a[j]; a[j] = a[i-1]; a[i-1] = tmp;
253 for (j = 0; j < b->n_gi; ++j) {
254 bcf_ginfo_t *gi = b->gi + j;
255 uint8_t *swap, *data = (uint8_t*)gi->data;
256 swap = malloc(gi->len * b->n_smpl);
257 for (i = 0; i < b->n_smpl; ++i)
258 memcpy(swap + gi->len * a[i], data + gi->len * i, gi->len);
266 bcf_hdr_t *bcf_hdr_subsam(const bcf_hdr_t *h0, int n, char *const* samples, int *list)
271 khash_t(str2id) *hash;
273 s.l = s.m = 0; s.s = 0;
274 hash = kh_init(str2id);
275 for (i = 0; i < h0->n_smpl; ++i) {
276 k = kh_put(str2id, hash, h0->sns[i], &ret);
279 for (i = j = 0; i < n; ++i) {
280 k = kh_get(str2id, hash, samples[i]);
281 if (k != kh_end(hash)) {
282 list[j++] = kh_val(hash, k);
283 kputs(samples[i], &s); kputc('\0', &s);
286 if (j < n) fprintf(pysamerr, "<%s> %d samples in the list but not in BCF.", __func__, n - j);
287 kh_destroy(str2id, hash);
288 h = calloc(1, sizeof(bcf_hdr_t));
290 h->ns = 0; h->sns = 0;
291 h->name = malloc(h->l_nm); memcpy(h->name, h0->name, h->l_nm);
292 h->txt = calloc(1, h->l_txt + 1); memcpy(h->txt, h0->txt, h->l_txt);
293 h->l_smpl = s.l; h->sname = s.s;
298 int bcf_subsam(int n_smpl, int *list, bcf1_t *b)
301 for (j = 0; j < b->n_gi; ++j) {
302 bcf_ginfo_t *gi = b->gi + j;
304 swap = malloc(gi->len * b->n_smpl);
305 for (i = 0; i < n_smpl; ++i)
306 memcpy(swap + i * gi->len, (uint8_t*)gi->data + list[i] * gi->len, gi->len);