]> git.donarmstrong.com Git - samtools.git/blob - bam2bcf.c
* use the revised MAQ error model for mpileup
[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 #define END_DIST_THRES 11
10
11 extern  void ks_introsort_uint32_t(size_t n, uint32_t a[]);
12
13 #define CALL_ETA 0.03f
14 #define CALL_MAX 256
15 #define CALL_DEFTHETA 0.83f
16
17 struct __bcf_callaux_t {
18         int max_bases, capQ, min_baseQ;
19         uint16_t *bases;
20         errmod_t *e;
21 };
22
23 bcf_callaux_t *bcf_call_init(double theta, int min_baseQ)
24 {
25         bcf_callaux_t *bca;
26         if (theta <= 0.) theta = CALL_DEFTHETA;
27         bca = calloc(1, sizeof(bcf_callaux_t));
28         bca->capQ = 60;
29         bca->min_baseQ = min_baseQ;
30         bca->e = errmod_init(1. - theta);
31         return bca;
32 }
33
34 void bcf_call_destroy(bcf_callaux_t *bca)
35 {
36         if (bca == 0) return;
37         errmod_destroy(bca->e);
38         free(bca->bases); free(bca);
39 }
40
41 int bcf_call_glfgen(int _n, const bam_pileup1_t *pl, int ref_base /*4-bit*/, bcf_callaux_t *bca, bcf_callret1_t *r)
42 {
43         int i, k, n, ref4;
44         memset(r, 0, sizeof(bcf_callret1_t));
45         ref4 = bam_nt16_nt4_table[ref_base];
46         if (_n == 0) return -1;
47
48         // enlarge the bases array if necessary
49         if (bca->max_bases < _n) {
50                 bca->max_bases = _n;
51                 kroundup32(bca->max_bases);
52                 bca->bases = (uint16_t*)realloc(bca->bases, 2 * bca->max_bases);
53         }
54         // fill the bases array
55         memset(r->qsum, 0, 4 * sizeof(float));
56         for (i = n = 0, r->sum_Q2 = 0; i < _n; ++i) {
57                 const bam_pileup1_t *p = pl + i;
58                 int q, b, mapQ;
59                 int min_dist;
60                 // set base
61                 if (p->is_del || (p->b->core.flag&BAM_FUNMAP)) continue; // skip unmapped reads and deleted bases
62                 q = (int)bam1_qual(p->b)[p->qpos]; // base quality
63                 if (q < bca->min_baseQ) continue;
64                 mapQ = p->b->core.qual < bca->capQ? p->b->core.qual : bca->capQ;
65                 r->sum_Q2 += mapQ * mapQ;
66                 if (q > mapQ) q = mapQ;
67                 if (q > 63) q = 63;
68                 if (q < 4) q = 4;
69                 b = bam1_seqi(bam1_seq(p->b), p->qpos); // base
70                 b = bam_nt16_nt4_table[b? b : ref_base]; // b is the 2-bit base
71                 bca->bases[n++] = q<<5 | (int)bam1_strand(p->b)<<4 | b;
72                 // collect other information
73                 r->qsum[b] += q;
74                 k = (ref4 < 4 && b == ref4)? 0 : 1;
75                 k = k<<1 | bam1_strand(p->b);
76                 ++r->d[k];
77                 // calculate min_dist
78                 min_dist = p->b->core.l_qseq - 1 - p->qpos;
79                 if (min_dist > p->qpos) min_dist = p->qpos;
80                 k = (k&2) | (min_dist <= END_DIST_THRES);
81                 ++r->ed[k];
82         }
83         r->depth = n;
84         // glfgen
85         errmod_cal(bca->e, n, 5, bca->bases, r->p);
86         return r->depth;
87 }
88
89 int bcf_call_combine(int n, const bcf_callret1_t *calls, int ref_base /*4-bit*/, bcf_call_t *call)
90 {
91         int ref4, i, j, qsum[4];
92         int64_t tmp;
93         call->ori_ref = ref4 = bam_nt16_nt4_table[ref_base];
94         if (ref4 > 4) ref4 = 4;
95         // calculate esum
96         memset(qsum, 0, 4 * sizeof(int));
97         for (i = 0; i < n; ++i)
98                 for (j = 0; j < 4; ++j)
99                         qsum[j] += calls[i].qsum[j];
100         for (j = 0; j < 4; ++j) qsum[j] = qsum[j] << 2 | j;
101         // find the top 2 alleles
102         for (i = 1; i < 4; ++i) // insertion sort
103                 for (j = i; j > 0 && qsum[j] < qsum[j-1]; --j)
104                         tmp = qsum[j], qsum[j] = qsum[j-1], qsum[j-1] = tmp;
105         // set the reference allele and alternative allele(s)
106         for (i = 0; i < 5; ++i) call->a[i] = -1;
107         call->unseen = -1;
108         call->a[0] = ref4;
109         for (i = 3, j = 1; i >= 0; --i) {
110                 if ((qsum[i]&3) != ref4) {
111                         if (qsum[i]>>2 != 0) call->a[j++] = qsum[i]&3;
112                         else break;
113                 }
114         }
115         if (((ref4 < 4 && j < 4) || (ref4 == 4 && j < 5)) && i >= 0)
116                 call->unseen = j, call->a[j++] = qsum[i]&3;
117         call->n_alleles = j;
118         // set the PL array
119         if (call->n < n) {
120                 call->n = n;
121                 call->PL = realloc(call->PL, 15 * n);
122         }
123         {
124                 int x, g[15], z;
125                 double sum_min = 0.;
126                 x = call->n_alleles * (call->n_alleles + 1) / 2;
127                 // get the possible genotypes
128                 for (i = z = 0; i < call->n_alleles; ++i)
129                         for (j = i; j < call->n_alleles; ++j)
130                                 g[z++] = call->a[i] * 5 + call->a[j];
131                 for (i = 0; i < n; ++i) {
132                         uint8_t *PL = call->PL + x * i;
133                         const bcf_callret1_t *r = calls + i;
134                         float min = 1e37;
135                         for (j = 0; j < x; ++j)
136                                 if (min > r->p[g[j]]) min = r->p[g[j]];
137                         sum_min += min;
138                         for (j = 0; j < x; ++j) {
139                                 int y;
140                                 y = (int)(r->p[g[j]] - min + .499);
141                                 if (y > 255) y = 255;
142                                 PL[j] = y;
143                         }
144                 }
145                 call->shift = (int)(sum_min + .499);
146         }
147         // combine annotations
148         memset(call->d, 0, 4 * sizeof(int));
149         memset(call->ed, 0, 4 * sizeof(int));
150         for (i = call->depth = 0, tmp = 0; i < n; ++i) {
151                 call->depth += calls[i].depth;
152                 for (j = 0; j < 4; ++j) call->d[j] += calls[i].d[j], call->ed[j] += calls[i].ed[j];
153                 tmp += calls[i].sum_Q2;
154         }
155         call->rmsQ = (int)(sqrt((double)tmp / call->depth) + .499);
156         return 0;
157 }
158
159 int bcf_call2bcf(int tid, int pos, bcf_call_t *bc, bcf1_t *b)
160 {
161         kstring_t s;
162         int i;
163         b->tid = tid; b->pos = pos; b->qual = 0;
164         s.s = b->str; s.m = b->m_str; s.l = 0;
165         kputc('\0', &s);
166         kputc("ACGTN"[bc->ori_ref], &s); kputc('\0', &s);
167         for (i = 1; i < 5; ++i) {
168                 if (bc->a[i] < 0) break;
169                 if (i > 1) kputc(',', &s);
170                 kputc(bc->unseen == i? 'X' : "ACGT"[bc->a[i]], &s);
171         }
172         kputc('\0', &s);
173         kputc('\0', &s);
174         // INFO
175         kputs("MQ=", &s); kputw(bc->rmsQ, &s);
176         kputs(";DP4=", &s);
177         for (i = 0; i < 4; ++i) {
178                 if (i) kputc(',', &s);
179                 kputw(bc->d[i], &s);
180         }
181         kputs(";ED4=", &s);
182         for (i = 0; i < 4; ++i) {
183                 if (i) kputc(',', &s);
184                 kputw(bc->ed[i], &s);
185         }
186         kputc('\0', &s);
187         // FMT
188         kputs("PL", &s); kputc('\0', &s);
189         b->m_str = s.m; b->str = s.s; b->l_str = s.l;
190         bcf_sync(bc->n, b);
191         memcpy(b->gi[0].data, bc->PL, b->gi[0].len * bc->n);
192         return 0;
193 }