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