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