Imported Upstream version 0.5
[pysam.git] / samtools / bcftools / bcfutils.c.pysam.c
1 #include "pysam.h"
2
3 #include <string.h>
4 #include <math.h>
5 #include "bcf.h"
6 #include "kstring.h"
7 #include "khash.h"
8 KHASH_MAP_INIT_STR(str2id, int)
9
10 void *bcf_build_refhash(bcf_hdr_t *h)
11 {
12         khash_t(str2id) *hash;
13         int i, ret;
14         hash = kh_init(str2id);
15         for (i = 0; i < h->n_ref; ++i) {
16                 khint_t k;
17                 k = kh_put(str2id, hash, h->ns[i], &ret); // FIXME: check ret
18                 kh_val(hash, k) = i;
19         }
20         return hash;
21 }
22
23 void *bcf_str2id_init()
24 {
25         return kh_init(str2id);
26 }
27
28 void bcf_str2id_destroy(void *_hash)
29 {
30         khash_t(str2id) *hash = (khash_t(str2id)*)_hash;
31         if (hash) kh_destroy(str2id, hash); // Note that strings are not freed.
32 }
33
34 void bcf_str2id_thorough_destroy(void *_hash)
35 {
36         khash_t(str2id) *hash = (khash_t(str2id)*)_hash;
37         khint_t k;
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);
42 }
43
44 int bcf_str2id(void *_hash, const char *str)
45 {
46         khash_t(str2id) *hash = (khash_t(str2id)*)_hash;
47         khint_t k;
48         if (!hash) return -1;
49         k = kh_get(str2id, hash, str);
50         return k == kh_end(hash)? -1 : kh_val(hash, k);
51 }
52
53 int bcf_str2id_add(void *_hash, const char *str)
54 {
55         khint_t k;
56         int ret;
57         khash_t(str2id) *hash = (khash_t(str2id)*)_hash;
58         if (!hash) return -1;
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);
63 }
64
65 int bcf_shrink_alt(bcf1_t *b, int n)
66 {
67         char *p;
68         int i, j, k, n_smpl = b->n_smpl;
69         if (b->n_alleles <= n) return -1;
70         // update ALT
71         if (n > 1) {
72                 for (p = b->alt, k = 1; *p; ++p)
73                         if (*p == ',' && ++k == n) break;
74                 *p = '\0';
75         } else p = b->alt, *p = '\0';
76         ++p;
77         memmove(p, b->flt, b->str + b->l_str - b->flt);
78         b->l_str -= b->flt - p;
79         // update PL
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];
89                         }
90                 } // FIXME: to add GL
91         }
92         b->n_alleles = n;
93         bcf_sync(b);
94         return 0;
95 }
96
97 int bcf_gl2pl(bcf1_t *b)
98 {
99         char *p;
100         int i, n_smpl = b->n_smpl;
101         bcf_ginfo_t *g;
102         float *d0;
103         uint8_t *d1;
104         if (strstr(b->fmt, "PL")) return -1;
105         if ((p = strstr(b->fmt, "GL")) == 0) return -1;
106         *p = 'P';
107         for (i = 0; i < b->n_gi; ++i)
108                 if (b->gi[i].fmt == bcf_str2int("GL", 2))
109                         break;
110         g = b->gi + i;
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;
117                 if (x < 0) x = 0;
118                 d1[i] = x;
119         }
120         return 0;
121 }
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)
125 {
126         char *s;
127         int i;
128         uint32_t tmp;
129         bcf_ginfo_t gt;
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...
137         gt = b->gi[i];
138         // move GT to the first
139         for (; i > 0; --i) b->gi[i] = b->gi[i-1];
140         b->gi[0] = gt;
141         memmove(b->fmt + 3, b->fmt, s + 1 - b->fmt);
142         b->fmt[0] = 'G'; b->fmt[1] = 'T'; b->fmt[2] = ':';
143         return 0;
144 }
145
146 int bcf_fix_pl(bcf1_t *b)
147 {
148         int i;
149         uint32_t tmp;
150         uint8_t *PL, *swap;
151         bcf_ginfo_t *gi;
152         // pinpoint PL
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;
157         // prepare
158         gi = b->gi + i;
159         PL = (uint8_t*)gi->data;
160         swap = alloca(gi->len);
161         // loop through individuals
162         for (i = 0; i < b->n_smpl; ++i) {
163                 int k, l, x;
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++];
169         }
170         return 0;
171 }
172
173 int bcf_smpl_covered(const bcf1_t *b)
174 {
175         int i, j, n = 0;
176         uint32_t tmp;
177         bcf_ginfo_t *gi;
178         // pinpoint PL
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]
184         gi = b->gi + i;
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)
188                         if (PLi[j]) break;
189                 if (j < gi->len) ++n;
190         }
191         return n;
192 }
193
194 static void *locate_field(const bcf1_t *b, const char *fmt, int l)
195 {
196         int i;
197         uint32_t tmp;
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;
202 }
203
204 int bcf_anno_max(bcf1_t *b)
205 {
206         int k, max_gq, max_sp, n_het;
207         kstring_t str;
208         uint8_t *gt, *gq;
209         int32_t *sp;
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);
215         if (sp)
216                 for (k = 0; k < b->n_smpl; ++k)
217                         if (gt[k]&0x3f)
218                                 max_sp = max_sp > (int)sp[k]? max_sp : sp[k];
219         if (gq)
220                 for (k = 0; k < b->n_smpl; ++k)
221                         if (gt[k]&0x3f)
222                                 max_gq = max_gq > (int)gq[k]? max_gq : gq[k];
223         for (k = 0; k < b->n_smpl; ++k) {
224                 int a1, a2;
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;
229                 }
230         }
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);
237         free(str.s);
238         return 0;
239 }
240
241 // FIXME: only data are shuffled; the header is NOT
242 int bcf_shuffle(bcf1_t *b, int seed)
243 {
244         int i, j, *a;
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) {
249                 int tmp;
250                 j = (int)(drand48() * i);
251                 tmp = a[j]; a[j] = a[i-1]; a[i-1] = tmp;
252         }
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);
259                 free(gi->data);
260                 gi->data = swap;
261         }
262         free(a);
263         return 0;
264 }
265
266 bcf_hdr_t *bcf_hdr_subsam(const bcf_hdr_t *h0, int n, char *const* samples, int *list)
267 {
268         int i, ret, j;
269         khint_t k;
270         bcf_hdr_t *h;
271         khash_t(str2id) *hash;
272         kstring_t s;
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);
277                 kh_val(hash, k) = i;
278         }
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);
284                 }
285         }
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));
289         *h = *h0;
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;
294         bcf_hdr_sync(h);
295         return h;
296 }
297
298 int bcf_subsam(int n_smpl, int *list, bcf1_t *b)
299 {
300         int i, j;
301         for (j = 0; j < b->n_gi; ++j) {
302                 bcf_ginfo_t *gi = b->gi + j;
303                 uint8_t *swap;
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);
307                 free(gi->data);
308                 gi->data = swap;
309         }
310         b->n_smpl = n_smpl;
311         return 0;
312 }