6 KHASH_MAP_INIT_STR(str2id, int)
8 void *bcf_build_refhash(bcf_hdr_t *h)
10 khash_t(str2id) *hash;
12 hash = kh_init(str2id);
13 for (i = 0; i < h->n_ref; ++i) {
15 k = kh_put(str2id, hash, h->ns[i], &ret); // FIXME: check ret
21 void *bcf_str2id_init()
23 return kh_init(str2id);
26 void bcf_str2id_destroy(void *_hash)
28 khash_t(str2id) *hash = (khash_t(str2id)*)_hash;
29 if (hash) kh_destroy(str2id, hash); // Note that strings are not freed.
32 void bcf_str2id_thorough_destroy(void *_hash)
34 khash_t(str2id) *hash = (khash_t(str2id)*)_hash;
36 if (hash == 0) return;
37 for (k = 0; k < kh_end(hash); ++k)
38 if (kh_exist(hash, k)) free((char*)kh_key(hash, k));
39 kh_destroy(str2id, hash);
42 int bcf_str2id(void *_hash, const char *str)
44 khash_t(str2id) *hash = (khash_t(str2id)*)_hash;
47 k = kh_get(str2id, hash, str);
48 return k == kh_end(hash)? -1 : kh_val(hash, k);
51 int bcf_str2id_add(void *_hash, const char *str)
55 khash_t(str2id) *hash = (khash_t(str2id)*)_hash;
57 k = kh_put(str2id, hash, str, &ret);
58 if (ret == 0) return kh_val(hash, k);
59 kh_val(hash, k) = kh_size(hash) - 1;
60 return kh_val(hash, k);
63 int bcf_shrink_alt(bcf1_t *b, int n)
66 int i, j, k, n_smpl = b->n_smpl;
67 if (b->n_alleles <= n) return -1;
70 for (p = b->alt, k = 1; *p; ++p)
71 if (*p == ',' && ++k == n) break;
73 } else p = b->alt, *p = '\0';
75 memmove(p, b->flt, b->str + b->l_str - b->flt);
76 b->l_str -= b->flt - p;
78 for (i = 0; i < b->n_gi; ++i) {
79 bcf_ginfo_t *g = b->gi + i;
80 if (g->fmt == bcf_str2int("PL", 2)) {
81 int l, x = b->n_alleles * (b->n_alleles + 1) / 2;
82 uint8_t *d = (uint8_t*)g->data;
83 g->len = n * (n + 1) / 2;
84 for (l = k = 0; l < n_smpl; ++l) {
85 uint8_t *dl = d + l * x;
86 for (j = 0; j < g->len; ++j) d[k++] = dl[j];
95 int bcf_gl2pl(bcf1_t *b)
98 int i, n_smpl = b->n_smpl;
102 if (strstr(b->fmt, "PL")) return -1;
103 if ((p = strstr(b->fmt, "GL")) == 0) return -1;
105 for (i = 0; i < b->n_gi; ++i)
106 if (b->gi[i].fmt == bcf_str2int("GL", 2))
109 g->fmt = bcf_str2int("PL", 2);
110 g->len /= 4; // 4 == sizeof(float)
111 d0 = (float*)g->data; d1 = (uint8_t*)g->data;
112 for (i = 0; i < n_smpl * g->len; ++i) {
113 int x = (int)(-10. * d0[i] + .499);
114 if (x > 255) x = 255;
120 /* FIXME: this function will fail given AB:GTX:GT. BCFtools never
121 * produces such FMT, but others may do. */
122 int bcf_fix_gt(bcf1_t *b)
128 // check the presence of the GT FMT
129 if ((s = strstr(b->fmt, ":GT")) == 0) return 0; // no GT or GT is already the first
130 if (s[3] != '\0' && s[3] != ':') return 0; // :GTX in fact
131 tmp = bcf_str2int("GT", 2);
132 for (i = 0; i < b->n_gi; ++i)
133 if (b->gi[i].fmt == tmp) break;
134 if (i == b->n_gi) return 0; // no GT in b->gi; probably a bug...
136 // move GT to the first
137 for (; i > 0; --i) b->gi[i] = b->gi[i-1];
139 memmove(b->fmt + 3, b->fmt, s + 1 - b->fmt);
140 b->fmt[0] = 'G'; b->fmt[1] = 'T'; b->fmt[2] = ':';
144 int bcf_fix_pl(bcf1_t *b)
151 tmp = bcf_str2int("PL", 2);
152 for (i = 0; i < b->n_gi; ++i)
153 if (b->gi[i].fmt == tmp) break;
154 if (i == b->n_gi) return 0;
157 PL = (uint8_t*)gi->data;
158 swap = alloca(gi->len);
159 // loop through individuals
160 for (i = 0; i < b->n_smpl; ++i) {
162 uint8_t *PLi = PL + i * gi->len;
163 memcpy(swap, PLi, gi->len);
164 for (k = x = 0; k < b->n_alleles; ++k)
165 for (l = k; l < b->n_alleles; ++l)
166 PLi[l*(l+1)/2 + k] = swap[x++];
171 int bcf_smpl_covered(const bcf1_t *b)
177 tmp = bcf_str2int("PL", 2);
178 for (i = 0; i < b->n_gi; ++i)
179 if (b->gi[i].fmt == tmp) break;
180 if (i == b->n_gi) return 0;
181 // count how many samples having PL!=[0..0]
183 for (i = 0; i < b->n_smpl; ++i) {
184 uint8_t *PLi = ((uint8_t*)gi->data) + i * gi->len;
185 for (j = 0; j < gi->len; ++j)
187 if (j < gi->len) ++n;
192 static void *locate_field(const bcf1_t *b, const char *fmt, int l)
196 tmp = bcf_str2int(fmt, l);
197 for (i = 0; i < b->n_gi; ++i)
198 if (b->gi[i].fmt == tmp) break;
199 return i == b->n_gi? 0 : b->gi[i].data;
202 int bcf_anno_max(bcf1_t *b)
204 int k, max_gq, max_sp, n_het;
208 max_gq = max_sp = n_het = 0;
209 gt = locate_field(b, "GT", 2);
210 if (gt == 0) return -1;
211 gq = locate_field(b, "GQ", 2);
212 sp = locate_field(b, "SP", 2);
214 for (k = 0; k < b->n_smpl; ++k)
216 max_sp = max_sp > (int)sp[k]? max_sp : sp[k];
218 for (k = 0; k < b->n_smpl; ++k)
220 max_gq = max_gq > (int)gq[k]? max_gq : gq[k];
221 for (k = 0; k < b->n_smpl; ++k) {
223 a1 = gt[k]&7; a2 = gt[k]>>3&7;
224 if ((!a1 && a2) || (!a2 && a1)) { // a het
225 if (gq == 0) ++n_het;
226 else if (gq[k] >= 20) ++n_het;
229 if (n_het) max_sp -= (int)(4.343 * log(n_het) + .499);
230 if (max_sp < 0) max_sp = 0;
231 memset(&str, 0, sizeof(kstring_t));
232 if (*b->info) kputc(';', &str);
233 ksprintf(&str, "MXSP=%d;MXGQ=%d", max_sp, max_gq);
234 bcf_append_info(b, str.s, str.l);
239 // FIXME: only data are shuffled; the header is NOT
240 int bcf_shuffle(bcf1_t *b, int seed)
243 if (seed > 0) srand48(seed);
244 a = malloc(b->n_smpl * sizeof(int));
245 for (i = 0; i < b->n_smpl; ++i) a[i] = i;
246 for (i = b->n_smpl; i > 1; --i) {
248 j = (int)(drand48() * i);
249 tmp = a[j]; a[j] = a[i-1]; a[i-1] = tmp;
251 for (j = 0; j < b->n_gi; ++j) {
252 bcf_ginfo_t *gi = b->gi + j;
253 uint8_t *swap, *data = (uint8_t*)gi->data;
254 swap = malloc(gi->len * b->n_smpl);
255 for (i = 0; i < b->n_smpl; ++i)
256 memcpy(swap + gi->len * a[i], data + gi->len * i, gi->len);
264 bcf_hdr_t *bcf_hdr_subsam(const bcf_hdr_t *h0, int n, char *const* samples, int *list)
269 khash_t(str2id) *hash;
271 s.l = s.m = 0; s.s = 0;
272 hash = kh_init(str2id);
273 for (i = 0; i < h0->n_smpl; ++i) {
274 k = kh_put(str2id, hash, h0->sns[i], &ret);
277 for (i = j = 0; i < n; ++i) {
278 k = kh_get(str2id, hash, samples[i]);
279 if (k != kh_end(hash)) {
280 list[j++] = kh_val(hash, k);
281 kputs(samples[i], &s); kputc('\0', &s);
284 if (j < n) fprintf(stderr, "<%s> %d samples in the list but not in BCF.", __func__, n - j);
285 kh_destroy(str2id, hash);
286 h = calloc(1, sizeof(bcf_hdr_t));
288 h->ns = 0; h->sns = 0;
289 h->name = malloc(h->l_nm); memcpy(h->name, h0->name, h->l_nm);
290 h->txt = calloc(1, h->l_txt + 1); memcpy(h->txt, h0->txt, h->l_txt);
291 h->l_smpl = s.l; h->sname = s.s;
296 int bcf_subsam(int n_smpl, int *list, bcf1_t *b)
299 for (j = 0; j < b->n_gi; ++j) {
300 bcf_ginfo_t *gi = b->gi + j;
302 swap = malloc(gi->len * b->n_smpl);
303 for (i = 0; i < n_smpl; ++i)
304 memcpy(swap + i * gi->len, (uint8_t*)gi->data + list[i] * gi->len, gi->len);