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 static void *locate_field(const bcf1_t *b, const char *fmt, int l)
148 tmp = bcf_str2int(fmt, l);
149 for (i = 0; i < b->n_gi; ++i)
150 if (b->gi[i].fmt == tmp) break;
151 return i == b->n_gi? 0 : b->gi[i].data;
154 int bcf_anno_max(bcf1_t *b)
156 int k, max_gq, max_sp, n_het;
160 max_gq = max_sp = n_het = 0;
161 gt = locate_field(b, "GT", 2);
162 if (gt == 0) return -1;
163 gq = locate_field(b, "GQ", 2);
164 sp = locate_field(b, "SP", 2);
166 for (k = 0; k < b->n_smpl; ++k)
168 max_sp = max_sp > (int)sp[k]? max_sp : sp[k];
170 for (k = 0; k < b->n_smpl; ++k)
172 max_gq = max_gq > (int)gq[k]? max_gq : gq[k];
173 for (k = 0; k < b->n_smpl; ++k) {
175 a1 = gt[k]&7; a2 = gt[k]>>3&7;
176 if ((!a1 && a2) || (!a2 && a1)) { // a het
177 if (gq == 0) ++n_het;
178 else if (gq[k] >= 20) ++n_het;
181 if (n_het) max_sp -= (int)(4.343 * log(n_het) + .499);
182 if (max_sp < 0) max_sp = 0;
183 memset(&str, 0, sizeof(kstring_t));
184 if (*b->info) kputc(';', &str);
185 ksprintf(&str, "MXSP=%d;MXGQ=%d", max_sp, max_gq);
186 bcf_append_info(b, str.s, str.l);
191 bcf_hdr_t *bcf_hdr_subsam(const bcf_hdr_t *h0, int n, char *const* samples, int *list)
196 khash_t(str2id) *hash;
198 s.l = s.m = 0; s.s = 0;
199 hash = kh_init(str2id);
200 for (i = 0; i < n; ++i)
201 k = kh_put(str2id, hash, samples[i], &ret);
202 for (i = j = 0; i < h0->n_smpl; ++i) {
203 if (kh_get(str2id, hash, h0->sns[i]) != kh_end(hash)) {
205 kputs(h0->sns[i], &s); kputc('\0', &s);
208 if (j < n) fprintf(stderr, "<%s> %d samples in the list but not in BCF.", __func__, n - j);
209 kh_destroy(str2id, hash);
210 h = calloc(1, sizeof(bcf_hdr_t));
212 h->ns = 0; h->sns = 0;
213 h->name = malloc(h->l_nm); memcpy(h->name, h0->name, h->l_nm);
214 h->txt = calloc(1, h->l_txt + 1); memcpy(h->txt, h0->txt, h->l_txt);
215 h->l_smpl = s.l; h->sname = s.s;
220 int bcf_subsam(int n_smpl, int *list, bcf1_t *b) // list MUST BE sorted
223 for (j = 0; j < b->n_gi; ++j) {
224 bcf_ginfo_t *gi = b->gi + j;
225 for (i = 0; i < n_smpl; ++i)
226 if (i != list[i]) memcpy((uint8_t*)gi->data + i * gi->len, (uint8_t*)gi->data + list[i] * gi->len, gi->len);