bcftools.1 and samtools.1 were merged in upstream release 0.1.17.
[samtools.git] / bam2bcf.c
1 #include <math.h>
2 #include <stdint.h>
3 #include "bam.h"
4 #include "kstring.h"
5 #include "bam2bcf.h"
6 #include "errmod.h"
7 #include "bcftools/bcf.h"
8
9 extern  void ks_introsort_uint32_t(size_t n, uint32_t a[]);
10
11 #define CALL_ETA 0.03f
12 #define CALL_MAX 256
13 #define CALL_DEFTHETA 0.83f
14 #define DEF_MAPQ 20
15
16 #define CAP_DIST 25
17
18 bcf_callaux_t *bcf_call_init(double theta, int min_baseQ)
19 {
20         bcf_callaux_t *bca;
21         if (theta <= 0.) theta = CALL_DEFTHETA;
22         bca = calloc(1, sizeof(bcf_callaux_t));
23         bca->capQ = 60;
24         bca->openQ = 40; bca->extQ = 20; bca->tandemQ = 100;
25         bca->min_baseQ = min_baseQ;
26         bca->e = errmod_init(1. - theta);
27         bca->min_frac = 0.002;
28         bca->min_support = 1;
29         return bca;
30 }
31
32 void bcf_call_destroy(bcf_callaux_t *bca)
33 {
34         if (bca == 0) return;
35         errmod_destroy(bca->e);
36         free(bca->bases); free(bca->inscns); free(bca);
37 }
38 /* ref_base is the 4-bit representation of the reference base. It is
39  * negative if we are looking at an indel. */
40 int bcf_call_glfgen(int _n, const bam_pileup1_t *pl, int ref_base, bcf_callaux_t *bca, bcf_callret1_t *r)
41 {
42         int i, n, ref4, is_indel, ori_depth = 0;
43         memset(r, 0, sizeof(bcf_callret1_t));
44         if (ref_base >= 0) {
45                 ref4 = bam_nt16_nt4_table[ref_base];
46                 is_indel = 0;
47         } else ref4 = 4, is_indel = 1;
48         if (_n == 0) return -1;
49         // enlarge the bases array if necessary
50         if (bca->max_bases < _n) {
51                 bca->max_bases = _n;
52                 kroundup32(bca->max_bases);
53                 bca->bases = (uint16_t*)realloc(bca->bases, 2 * bca->max_bases);
54         }
55         // fill the bases array
56         memset(r, 0, sizeof(bcf_callret1_t));
57         for (i = n = 0; i < _n; ++i) {
58                 const bam_pileup1_t *p = pl + i;
59                 int q, b, mapQ, baseQ, is_diff, min_dist, seqQ;
60                 // set base
61                 if (p->is_del || p->is_refskip || (p->b->core.flag&BAM_FUNMAP)) continue;
62                 ++ori_depth;
63                 baseQ = q = is_indel? p->aux&0xff : (int)bam1_qual(p->b)[p->qpos]; // base/indel quality
64                 seqQ = is_indel? (p->aux>>8&0xff) : 99;
65                 if (q < bca->min_baseQ) continue;
66                 if (q > seqQ) q = seqQ;
67                 mapQ = p->b->core.qual < 255? p->b->core.qual : DEF_MAPQ; // special case for mapQ==255
68                 mapQ = mapQ < bca->capQ? mapQ : bca->capQ;
69                 if (q > mapQ) q = mapQ;
70                 if (q > 63) q = 63;
71                 if (q < 4) q = 4;
72                 if (!is_indel) {
73                         b = bam1_seqi(bam1_seq(p->b), p->qpos); // base
74                         b = bam_nt16_nt4_table[b? b : ref_base]; // b is the 2-bit base
75                         is_diff = (ref4 < 4 && b == ref4)? 0 : 1;
76                 } else {
77                         b = p->aux>>16&0x3f;
78                         is_diff = (b != 0);
79                 }
80                 bca->bases[n++] = q<<5 | (int)bam1_strand(p->b)<<4 | b;
81                 // collect annotations
82                 if (b < 4) r->qsum[b] += q;
83                 ++r->anno[0<<2|is_diff<<1|bam1_strand(p->b)];
84                 min_dist = p->b->core.l_qseq - 1 - p->qpos;
85                 if (min_dist > p->qpos) min_dist = p->qpos;
86                 if (min_dist > CAP_DIST) min_dist = CAP_DIST;
87                 r->anno[1<<2|is_diff<<1|0] += baseQ;
88                 r->anno[1<<2|is_diff<<1|1] += baseQ * baseQ;
89                 r->anno[2<<2|is_diff<<1|0] += mapQ;
90                 r->anno[2<<2|is_diff<<1|1] += mapQ * mapQ;
91                 r->anno[3<<2|is_diff<<1|0] += min_dist;
92                 r->anno[3<<2|is_diff<<1|1] += min_dist * min_dist;
93         }
94         r->depth = n; r->ori_depth = ori_depth;
95         // glfgen
96         errmod_cal(bca->e, n, 5, bca->bases, r->p);
97         return r->depth;
98 }
99
100 int bcf_call_combine(int n, const bcf_callret1_t *calls, int ref_base /*4-bit*/, bcf_call_t *call)
101 {
102         int ref4, i, j, qsum[4];
103         int64_t tmp;
104         if (ref_base >= 0) {
105                 call->ori_ref = ref4 = bam_nt16_nt4_table[ref_base];
106                 if (ref4 > 4) ref4 = 4;
107         } else call->ori_ref = -1, ref4 = 0;
108         // calculate qsum
109         memset(qsum, 0, 4 * sizeof(int));
110         for (i = 0; i < n; ++i)
111                 for (j = 0; j < 4; ++j)
112                         qsum[j] += calls[i].qsum[j];
113         for (j = 0; j < 4; ++j) qsum[j] = qsum[j] << 2 | j;
114         // find the top 2 alleles
115         for (i = 1; i < 4; ++i) // insertion sort
116                 for (j = i; j > 0 && qsum[j] < qsum[j-1]; --j)
117                         tmp = qsum[j], qsum[j] = qsum[j-1], qsum[j-1] = tmp;
118         // set the reference allele and alternative allele(s)
119         for (i = 0; i < 5; ++i) call->a[i] = -1;
120         call->unseen = -1;
121         call->a[0] = ref4;
122         for (i = 3, j = 1; i >= 0; --i) {
123                 if ((qsum[i]&3) != ref4) {
124                         if (qsum[i]>>2 != 0) call->a[j++] = qsum[i]&3;
125                         else break;
126                 }
127         }
128         if (ref_base >= 0) { // for SNPs, find the "unseen" base
129                 if (((ref4 < 4 && j < 4) || (ref4 == 4 && j < 5)) && i >= 0)
130                         call->unseen = j, call->a[j++] = qsum[i]&3;
131                 call->n_alleles = j;
132         } else {
133                 call->n_alleles = j;
134                 if (call->n_alleles == 1) return -1; // no reliable supporting read. stop doing anything
135         }
136         // set the PL array
137         if (call->n < n) {
138                 call->n = n;
139                 call->PL = realloc(call->PL, 15 * n);
140         }
141         {
142                 int x, g[15], z;
143                 double sum_min = 0.;
144                 x = call->n_alleles * (call->n_alleles + 1) / 2;
145                 // get the possible genotypes
146                 for (i = z = 0; i < call->n_alleles; ++i)
147                         for (j = 0; j <= i; ++j)
148                                 g[z++] = call->a[j] * 5 + call->a[i];
149                 for (i = 0; i < n; ++i) {
150                         uint8_t *PL = call->PL + x * i;
151                         const bcf_callret1_t *r = calls + i;
152                         float min = 1e37;
153                         for (j = 0; j < x; ++j)
154                                 if (min > r->p[g[j]]) min = r->p[g[j]];
155                         sum_min += min;
156                         for (j = 0; j < x; ++j) {
157                                 int y;
158                                 y = (int)(r->p[g[j]] - min + .499);
159                                 if (y > 255) y = 255;
160                                 PL[j] = y;
161                         }
162                 }
163 //              if (ref_base < 0) fprintf(stderr, "%d,%d,%f,%d\n", call->n_alleles, x, sum_min, call->unseen);
164                 call->shift = (int)(sum_min + .499);
165         }
166         // combine annotations
167         memset(call->anno, 0, 16 * sizeof(int));
168         for (i = call->depth = call->ori_depth = 0, tmp = 0; i < n; ++i) {
169                 call->depth += calls[i].depth;
170                 call->ori_depth += calls[i].ori_depth;
171                 for (j = 0; j < 16; ++j) call->anno[j] += calls[i].anno[j];
172         }
173         return 0;
174 }
175
176 int bcf_call2bcf(int tid, int pos, bcf_call_t *bc, bcf1_t *b, bcf_callret1_t *bcr, int is_SP,
177                                  const bcf_callaux_t *bca, const char *ref)
178 {
179         extern double kt_fisher_exact(int n11, int n12, int n21, int n22, double *_left, double *_right, double *two);
180         kstring_t s;
181         int i, j;
182         b->n_smpl = bc->n;
183         b->tid = tid; b->pos = pos; b->qual = 0;
184         s.s = b->str; s.m = b->m_str; s.l = 0;
185         kputc('\0', &s);
186         if (bc->ori_ref < 0) { // an indel
187                 // write REF
188                 kputc(ref[pos], &s);
189                 for (j = 0; j < bca->indelreg; ++j) kputc(ref[pos+1+j], &s);
190                 kputc('\0', &s);
191                 // write ALT
192                 kputc(ref[pos], &s);
193                 for (i = 1; i < 4; ++i) {
194                         if (bc->a[i] < 0) break;
195                         if (i > 1) {
196                                 kputc(',', &s); kputc(ref[pos], &s);
197                         }
198                         if (bca->indel_types[bc->a[i]] < 0) { // deletion
199                                 for (j = -bca->indel_types[bc->a[i]]; j < bca->indelreg; ++j)
200                                         kputc(ref[pos+1+j], &s);
201                         } else { // insertion; cannot be a reference unless a bug
202                                 char *inscns = &bca->inscns[bc->a[i] * bca->maxins];
203                                 for (j = 0; j < bca->indel_types[bc->a[i]]; ++j)
204                                         kputc("ACGTN"[(int)inscns[j]], &s);
205                                 for (j = 0; j < bca->indelreg; ++j) kputc(ref[pos+1+j], &s);
206                         }
207                 }
208                 kputc('\0', &s);
209         } else { // a SNP
210                 kputc("ACGTN"[bc->ori_ref], &s); kputc('\0', &s);
211                 for (i = 1; i < 5; ++i) {
212                         if (bc->a[i] < 0) break;
213                         if (i > 1) kputc(',', &s);
214                         kputc(bc->unseen == i? 'X' : "ACGT"[bc->a[i]], &s);
215                 }
216                 kputc('\0', &s);
217         }
218         kputc('\0', &s);
219         // INFO
220         if (bc->ori_ref < 0) kputs("INDEL;", &s);
221         kputs("DP=", &s); kputw(bc->ori_depth, &s); kputs(";I16=", &s);
222         for (i = 0; i < 16; ++i) {
223                 if (i) kputc(',', &s);
224                 kputw(bc->anno[i], &s);
225         }
226         kputc('\0', &s);
227         // FMT
228         kputs("PL", &s);
229         if (bcr) {
230                 kputs(":DP", &s);
231                 if (is_SP) kputs(":SP", &s);
232         }
233         kputc('\0', &s);
234         b->m_str = s.m; b->str = s.s; b->l_str = s.l;
235         bcf_sync(b);
236         memcpy(b->gi[0].data, bc->PL, b->gi[0].len * bc->n);
237         if (bcr) {
238                 uint16_t *dp = (uint16_t*)b->gi[1].data;
239                 int32_t *sp = is_SP? b->gi[2].data : 0;
240                 for (i = 0; i < bc->n; ++i) {
241                         bcf_callret1_t *p = bcr + i;
242                         dp[i] = p->depth < 0xffff? p->depth : 0xffff;
243                         if (is_SP) {
244                                 if (p->anno[0] + p->anno[1] < 2 || p->anno[2] + p->anno[3] < 2
245                                         || p->anno[0] + p->anno[2] < 2 || p->anno[1] + p->anno[3] < 2)
246                                 {
247                                         sp[i] = 0;
248                                 } else {
249                                         double left, right, two;
250                                         int x;
251                                         kt_fisher_exact(p->anno[0], p->anno[1], p->anno[2], p->anno[3], &left, &right, &two);
252                                         x = (int)(-4.343 * log(two) + .499);
253                                         if (x > 255) x = 255;
254                                         sp[i] = x;
255                                 }
256                         }
257                 }
258         }
259         return 0;
260 }