]> git.donarmstrong.com Git - samtools.git/blob - bam2bcf.c
* samtools-0.1.10-4 (r834)
[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 = 100;
24         bca->min_baseQ = min_baseQ;
25         bca->last_mnp_pos = -100;
26         bca->e = errmod_init(1. - theta);
27         return bca;
28 }
29
30 void bcf_call_destroy(bcf_callaux_t *bca)
31 {
32         if (bca == 0) return;
33         errmod_destroy(bca->e);
34         free(bca->bases); free(bca->inscns); free(bca);
35 }
36 /* Compute genotype likelihood by combining likelihood of each
37  * read. ref_base is the 4-bit representation of the reference base. It
38  * is negative if we are looking at an indel or an MNP. Indel and MNP
39  * are indistinguishable in this function. */
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 < bca->capQ? p->b->core.qual : bca->capQ;
68                 if (q > mapQ) q = mapQ;
69                 if (q > 63) q = 63;
70                 if (q < 4) q = 4;
71                 if (!is_indel) {
72                         b = bam1_seqi(bam1_seq(p->b), p->qpos); // base
73                         b = bam_nt16_nt4_table[b? b : ref_base]; // b is the 2-bit base
74                         is_diff = (ref4 < 4 && b == ref4)? 0 : 1;
75                 } else {
76                         b = p->aux>>16&0x3f;
77                         is_diff = (b != 0);
78                 }
79                 bca->bases[n++] = q<<5 | (int)bam1_strand(p->b)<<4 | b;
80                 // collect annotations
81                 r->qsum[b] += q;
82                 ++r->anno[0<<2|is_diff<<1|bam1_strand(p->b)];
83                 min_dist = p->b->core.l_qseq - 1 - p->qpos;
84                 if (min_dist > p->qpos) min_dist = p->qpos;
85                 if (min_dist > CAP_DIST) min_dist = CAP_DIST;
86                 r->anno[1<<2|is_diff<<1|0] += baseQ;
87                 r->anno[1<<2|is_diff<<1|1] += baseQ * baseQ;
88                 r->anno[2<<2|is_diff<<1|0] += mapQ;
89                 r->anno[2<<2|is_diff<<1|1] += mapQ * mapQ;
90                 r->anno[3<<2|is_diff<<1|0] += min_dist;
91                 r->anno[3<<2|is_diff<<1|1] += min_dist * min_dist;
92         }
93         r->depth = n; r->ori_depth = ori_depth;
94         // glfgen
95         errmod_cal(bca->e, n, 5, bca->bases, r->p);
96         return r->depth;
97 }
98 /* Combine individual calls. ref_base is a 4-bit A/C/G/T for a SNP, or
99  * B2B_REF_MNP, or B2B_REF_INDEL. Indel and MNP are indistinguishable in
100  * this function. */
101 int bcf_call_combine(int n, const bcf_callret1_t *calls, int ref_base /*4-bit*/, bcf_call_t *call)
102 {
103         int ref4, i, j, qsum[4];
104         int64_t tmp;
105         if (ref_base >= 0) {
106                 call->ori_ref = ref4 = bam_nt16_nt4_table[ref_base];
107                 if (ref4 > 4) ref4 = 4;
108         } else call->ori_ref = ref_base, ref4 = 0; // ref_base < 0
109         // calculate qsum
110         memset(qsum, 0, 4 * sizeof(int));
111         for (i = 0; i < n; ++i)
112                 for (j = 0; j < 4; ++j)
113                         qsum[j] += calls[i].qsum[j];
114         for (j = 0; j < 4; ++j) qsum[j] = qsum[j] << 2 | j;
115         // find the top 2 alleles
116         for (i = 1; i < 4; ++i) // insertion sort
117                 for (j = i; j > 0 && qsum[j] < qsum[j-1]; --j)
118                         tmp = qsum[j], qsum[j] = qsum[j-1], qsum[j-1] = tmp;
119         // set the reference allele and alternative allele(s)
120         for (i = 0; i < 5; ++i) call->a[i] = -1;
121         call->unseen = -1;
122         call->a[0] = ref4;
123         for (i = 3, j = 1; i >= 0; --i) {
124                 if ((qsum[i]&3) != ref4) {
125                         if (qsum[i]>>2 != 0) call->a[j++] = qsum[i]&3;
126                         else break;
127                 }
128         }
129         if (ref_base >= 0) { // for SNPs, find the "unseen" base
130                 if (((ref4 < 4 && j < 4) || (ref4 == 4 && j < 5)) && i >= 0)
131                         call->unseen = j, call->a[j++] = qsum[i]&3;
132                 call->n_alleles = j;
133         } else { // for indels and MNPs
134                 call->n_alleles = j;
135                 if (call->n_alleles == 1) return -1; // no reliable supporting read. stop doing anything
136         }
137         // set the PL array
138         if (call->n < n) {
139                 call->n = n;
140                 call->PL = realloc(call->PL, 15 * n);
141         }
142         {
143                 int x, g[15], z;
144                 double sum_min = 0.;
145                 x = call->n_alleles * (call->n_alleles + 1) / 2;
146                 // get the possible genotypes
147                 for (i = z = 0; i < call->n_alleles; ++i)
148                         for (j = i; j < call->n_alleles; ++j)
149                                 g[z++] = call->a[i] * 5 + call->a[j];
150                 for (i = 0; i < n; ++i) {
151                         uint8_t *PL = call->PL + x * i;
152                         const bcf_callret1_t *r = calls + i;
153                         float min = 1e37;
154                         for (j = 0; j < x; ++j)
155                                 if (min > r->p[g[j]]) min = r->p[g[j]];
156                         sum_min += min;
157                         for (j = 0; j < x; ++j) {
158                                 int y;
159                                 y = (int)(r->p[g[j]] - min + .499);
160                                 if (y > 255) y = 255;
161                                 PL[j] = y;
162                         }
163                 }
164 //              if (ref_base < 0) fprintf(stderr, "%d,%d,%f,%d\n", call->n_alleles, x, sum_min, call->unseen);
165                 call->shift = (int)(sum_min + .499);
166         }
167         // combine annotations
168         memset(call->anno, 0, 16 * sizeof(int));
169         for (i = call->depth = call->ori_depth = 0, tmp = 0; i < n; ++i) {
170                 call->depth += calls[i].depth;
171                 call->ori_depth += calls[i].ori_depth;
172                 for (j = 0; j < 16; ++j) call->anno[j] += calls[i].anno[j];
173         }
174         return 0;
175 }
176 /* Fill a bcf1_t record. The type of the record (SNP, MNP or INDEL) is
177  * determined by bca->ori_ref. */
178 int bcf_call2bcf(int tid, int pos, bcf_call_t *bc, bcf1_t *b, bcf_callret1_t *bcr, int is_SP,
179                                  const bcf_callaux_t *bca, const char *ref)
180 {
181         extern double kt_fisher_exact(int n11, int n12, int n21, int n22, double *_left, double *_right, double *two);
182         kstring_t s;
183         int i, j;
184         b->n_smpl = bc->n;
185         b->tid = tid; b->pos = pos; b->qual = 0;
186         s.s = b->str; s.m = b->m_str; s.l = 0;
187         kputc('\0', &s);
188         if (bc->ori_ref == B2B_REF_INDEL) { // an indel
189                 // write REF
190                 kputc(ref[pos], &s);
191                 for (j = 0; j < bca->indelreg; ++j) kputc(ref[pos+1+j], &s);
192                 kputc('\0', &s);
193                 // write ALT
194                 kputc(ref[pos], &s);
195                 for (i = 1; i < 4; ++i) {
196                         if (bc->a[i] < 0) break;
197                         if (i > 1) {
198                                 kputc(',', &s); kputc(ref[pos], &s);
199                         }
200                         if (bca->indel_types[bc->a[i]] < 0) { // deletion
201                                 for (j = -bca->indel_types[bc->a[i]]; j < bca->indelreg; ++j)
202                                         kputc(ref[pos+1+j], &s);
203                         } else { // insertion; cannot be a reference unless a bug
204                                 char *inscns = &bca->inscns[bc->a[i] * bca->maxins];
205                                 for (j = 0; j < bca->indel_types[bc->a[i]]; ++j)
206                                         kputc("ACGTN"[(int)inscns[j]], &s);
207                                 for (j = 0; j < bca->indelreg; ++j) kputc(ref[pos+1+j], &s);
208                         }
209                 }
210                 kputc('\0', &s);
211         } else if (bc->ori_ref == B2B_REF_MNP) {
212                 for (j = 0; j < bca->indelreg; ++j) kputc(ref[pos+j], &s);
213                 kputc('\0', &s);
214                 for (i = 1; i < 4; ++i) {
215                         if (bc->a[i] < 0) break;
216                         if (i > 1) kputc(',', &s);
217                         for (j = 0; j < bca->indelreg; ++j)
218                                 kputc("ACGT"[bca->indel_types[i]>>2*j&3], &s);
219                 }
220                 kputc('\0', &s);
221         } else { // a SNP
222                 kputc("ACGTN"[bc->ori_ref], &s); kputc('\0', &s);
223                 for (i = 1; i < 5; ++i) {
224                         if (bc->a[i] < 0) break;
225                         if (i > 1) kputc(',', &s);
226                         kputc(bc->unseen == i? 'X' : "ACGT"[bc->a[i]], &s);
227                 }
228                 kputc('\0', &s);
229         }
230         kputc('\0', &s);
231         // INFO
232         if (bc->ori_ref == B2B_REF_INDEL) kputs("INDEL;", &s);
233         else if (bc->ori_ref == B2B_REF_MNP) kputs("MNP;", &s);
234         kputs("DP=", &s); kputw(bc->ori_depth, &s); kputs(";I16=", &s);
235         for (i = 0; i < 16; ++i) {
236                 if (i) kputc(',', &s);
237                 kputw(bc->anno[i], &s);
238         }
239         kputc('\0', &s);
240         // FMT
241         kputs("PL", &s);
242         if (bcr) {
243                 kputs(":DP", &s);
244                 if (is_SP) kputs(":SP", &s);
245         }
246         kputc('\0', &s);
247         b->m_str = s.m; b->str = s.s; b->l_str = s.l;
248         bcf_sync(b);
249         memcpy(b->gi[0].data, bc->PL, b->gi[0].len * bc->n);
250         if (bcr) {
251                 uint16_t *dp = (uint16_t*)b->gi[1].data;
252                 uint8_t *sp = is_SP? b->gi[2].data : 0;
253                 for (i = 0; i < bc->n; ++i) {
254                         bcf_callret1_t *p = bcr + i;
255                         dp[i] = p->depth < 0xffff? p->depth : 0xffff;
256                         if (is_SP) {
257                                 if (p->anno[0] + p->anno[1] < 2 || p->anno[2] + p->anno[3] < 2
258                                         || p->anno[0] + p->anno[2] < 2 || p->anno[1] + p->anno[3] < 2)
259                                 {
260                                         sp[i] = 0;
261                                 } else {
262                                         double left, right, two;
263                                         int x;
264                                         kt_fisher_exact(p->anno[0], p->anno[1], p->anno[2], p->anno[3], &left, &right, &two);
265                                         x = (int)(-4.343 * log(two) + .499);
266                                         if (x > 255) x = 255;
267                                         sp[i] = x;
268                                 }
269                         }
270                 }
271         }
272         return 0;
273 }