7 #include "bcftools/bcf.h"
9 extern void ks_introsort_uint32_t(size_t n, uint32_t a[]);
11 #define CALL_ETA 0.03f
13 #define CALL_DEFTHETA 0.83f
18 bcf_callaux_t *bcf_call_init(double theta, int min_baseQ)
21 if (theta <= 0.) theta = CALL_DEFTHETA;
22 bca = calloc(1, sizeof(bcf_callaux_t));
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;
32 void bcf_call_destroy(bcf_callaux_t *bca)
35 errmod_destroy(bca->e);
36 free(bca->bases); free(bca->inscns); free(bca);
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)
42 static int *var_pos = NULL, nvar_pos = 0;
43 int i, n, ref4, is_indel, ori_depth = 0;
44 memset(r, 0, sizeof(bcf_callret1_t));
46 ref4 = bam_nt16_nt4_table[ref_base];
48 } else ref4 = 4, is_indel = 1;
49 if (_n == 0) return -1;
50 // enlarge the bases array if necessary
51 if (bca->max_bases < _n) {
53 kroundup32(bca->max_bases);
54 bca->bases = (uint16_t*)realloc(bca->bases, 2 * bca->max_bases);
56 // fill the bases array
57 memset(r, 0, sizeof(bcf_callret1_t));
58 for (i = n = 0; i < _n; ++i) {
59 const bam_pileup1_t *p = pl + i;
60 int q, b, mapQ, baseQ, is_diff, min_dist, seqQ;
62 if (p->is_del || p->is_refskip || (p->b->core.flag&BAM_FUNMAP)) continue;
64 baseQ = q = is_indel? p->aux&0xff : (int)bam1_qual(p->b)[p->qpos]; // base/indel quality
65 seqQ = is_indel? (p->aux>>8&0xff) : 99;
66 if (q < bca->min_baseQ) continue;
67 if (q > seqQ) q = seqQ;
68 mapQ = p->b->core.qual < 255? p->b->core.qual : DEF_MAPQ; // special case for mapQ==255
69 mapQ = mapQ < bca->capQ? mapQ : bca->capQ;
70 if (q > mapQ) q = mapQ;
74 b = bam1_seqi(bam1_seq(p->b), p->qpos); // base
75 b = bam_nt16_nt4_table[b? b : ref_base]; // b is the 2-bit base
76 is_diff = (ref4 < 4 && b == ref4)? 0 : 1;
81 bca->bases[n++] = q<<5 | (int)bam1_strand(p->b)<<4 | b;
82 // collect annotations
83 if (b < 4) r->qsum[b] += q;
84 ++r->anno[0<<2|is_diff<<1|bam1_strand(p->b)];
85 min_dist = p->b->core.l_qseq - 1 - p->qpos;
86 if (min_dist > p->qpos) min_dist = p->qpos;
87 if (min_dist > CAP_DIST) min_dist = CAP_DIST;
88 r->anno[1<<2|is_diff<<1|0] += baseQ;
89 r->anno[1<<2|is_diff<<1|1] += baseQ * baseQ;
90 r->anno[2<<2|is_diff<<1|0] += mapQ;
91 r->anno[2<<2|is_diff<<1|1] += mapQ * mapQ;
92 r->anno[3<<2|is_diff<<1|0] += min_dist;
93 r->anno[3<<2|is_diff<<1|1] += min_dist * min_dist;
95 r->depth = n; r->ori_depth = ori_depth;
97 errmod_cal(bca->e, n, 5, bca->bases, r->p);
99 // Calculate the Variant Distance Bias (make it optional?)
100 if ( nvar_pos < _n ) {
102 var_pos = realloc(var_pos,sizeof(int)*nvar_pos);
104 int alt_dp=0, read_len=0;
105 for (i=0; i<_n; i++) {
106 const bam_pileup1_t *p = pl + i;
107 if ( bam1_seqi(bam1_seq(p->b),p->qpos) == ref_base )
110 var_pos[alt_dp] = p->qpos;
111 if ( (bam1_cigar(p->b)[0]&BAM_CIGAR_MASK)==4 )
112 var_pos[alt_dp] -= bam1_cigar(p->b)[0]>>BAM_CIGAR_SHIFT;
115 read_len += p->b->core.l_qseq;
120 for (i=0; i<alt_dp; i++) {
121 for (j=0; j<i; j++) {
122 mvd += abs(var_pos[i] - var_pos[j]);
126 r->mvd[0] = n ? mvd/n : 0;
128 r->mvd[2] = alt_dp ? read_len/alt_dp : 0;
134 void calc_vdb(int n, const bcf_callret1_t *calls, bcf_call_t *call)
136 // Variant distance bias. Samples merged by means of DP-weighted average.
138 float weight=0, tot_prob=0;
143 int mvd = calls[i].mvd[0];
144 int dp = calls[i].mvd[1];
145 int read_len = calls[i].mvd[2];
147 if ( dp<2 ) continue;
153 prob = (mvd==0) ? 1.0/read_len : (read_len-mvd)*2.0/read_len/read_len;
157 // Sin, quite accurate approximation
158 float mu = read_len/2.9;
159 prob = mvd>2*mu ? 0 : sin(mvd*3.14/2/mu) / (4*mu/3.14);
163 // Scaled gaussian curve, crude approximation, but behaves well. Using fixed depth for bigger depths.
166 float sigma2 = (read_len/1.9/(dp+1)) * (read_len/1.9/(dp+1));
167 float norm = 1.125*sqrt(2*3.14*sigma2);
168 float mu = read_len/2.9;
170 prob = exp(-(mvd-mu)*(mvd-mu)/2/sigma2)/norm;
172 prob = exp(-(mvd-mu)*(mvd-mu)/3.125/sigma2)/norm;
175 //fprintf(stderr,"dp=%d mvd=%d read_len=%d -> prob=%f\n", dp,mvd,read_len,prob);
179 tot_prob = weight ? tot_prob/weight : 1;
180 //fprintf(stderr,"prob=%f\n", tot_prob);
181 call->vdb = tot_prob;
184 int bcf_call_combine(int n, const bcf_callret1_t *calls, int ref_base /*4-bit*/, bcf_call_t *call)
186 int ref4, i, j, qsum[4];
189 call->ori_ref = ref4 = bam_nt16_nt4_table[ref_base];
190 if (ref4 > 4) ref4 = 4;
191 } else call->ori_ref = -1, ref4 = 0;
193 memset(qsum, 0, 4 * sizeof(int));
194 for (i = 0; i < n; ++i)
195 for (j = 0; j < 4; ++j)
196 qsum[j] += calls[i].qsum[j];
197 for (j = 0; j < 4; ++j) qsum[j] = qsum[j] << 2 | j;
198 // find the top 2 alleles
199 for (i = 1; i < 4; ++i) // insertion sort
200 for (j = i; j > 0 && qsum[j] < qsum[j-1]; --j)
201 tmp = qsum[j], qsum[j] = qsum[j-1], qsum[j-1] = tmp;
202 // set the reference allele and alternative allele(s)
203 for (i = 0; i < 5; ++i) call->a[i] = -1;
206 for (i = 3, j = 1; i >= 0; --i) {
207 if ((qsum[i]&3) != ref4) {
208 if (qsum[i]>>2 != 0) call->a[j++] = qsum[i]&3;
212 if (ref_base >= 0) { // for SNPs, find the "unseen" base
213 if (((ref4 < 4 && j < 4) || (ref4 == 4 && j < 5)) && i >= 0)
214 call->unseen = j, call->a[j++] = qsum[i]&3;
218 if (call->n_alleles == 1) return -1; // no reliable supporting read. stop doing anything
223 call->PL = realloc(call->PL, 15 * n);
228 x = call->n_alleles * (call->n_alleles + 1) / 2;
229 // get the possible genotypes
230 for (i = z = 0; i < call->n_alleles; ++i)
231 for (j = 0; j <= i; ++j)
232 g[z++] = call->a[j] * 5 + call->a[i];
233 for (i = 0; i < n; ++i) {
234 uint8_t *PL = call->PL + x * i;
235 const bcf_callret1_t *r = calls + i;
237 for (j = 0; j < x; ++j)
238 if (min > r->p[g[j]]) min = r->p[g[j]];
240 for (j = 0; j < x; ++j) {
242 y = (int)(r->p[g[j]] - min + .499);
243 if (y > 255) y = 255;
247 // if (ref_base < 0) fprintf(stderr, "%d,%d,%f,%d\n", call->n_alleles, x, sum_min, call->unseen);
248 call->shift = (int)(sum_min + .499);
250 // combine annotations
251 memset(call->anno, 0, 16 * sizeof(int));
252 for (i = call->depth = call->ori_depth = 0, tmp = 0; i < n; ++i) {
253 call->depth += calls[i].depth;
254 call->ori_depth += calls[i].ori_depth;
255 for (j = 0; j < 16; ++j) call->anno[j] += calls[i].anno[j];
258 calc_vdb(n, calls, call);
263 int bcf_call2bcf(int tid, int pos, bcf_call_t *bc, bcf1_t *b, bcf_callret1_t *bcr, int is_SP,
264 const bcf_callaux_t *bca, const char *ref)
266 extern double kt_fisher_exact(int n11, int n12, int n21, int n22, double *_left, double *_right, double *two);
270 b->tid = tid; b->pos = pos; b->qual = 0;
271 s.s = b->str; s.m = b->m_str; s.l = 0;
273 if (bc->ori_ref < 0) { // an indel
276 for (j = 0; j < bca->indelreg; ++j) kputc(ref[pos+1+j], &s);
280 for (i = 1; i < 4; ++i) {
281 if (bc->a[i] < 0) break;
283 kputc(',', &s); kputc(ref[pos], &s);
285 if (bca->indel_types[bc->a[i]] < 0) { // deletion
286 for (j = -bca->indel_types[bc->a[i]]; j < bca->indelreg; ++j)
287 kputc(ref[pos+1+j], &s);
288 } else { // insertion; cannot be a reference unless a bug
289 char *inscns = &bca->inscns[bc->a[i] * bca->maxins];
290 for (j = 0; j < bca->indel_types[bc->a[i]]; ++j)
291 kputc("ACGTN"[(int)inscns[j]], &s);
292 for (j = 0; j < bca->indelreg; ++j) kputc(ref[pos+1+j], &s);
297 kputc("ACGTN"[bc->ori_ref], &s); kputc('\0', &s);
298 for (i = 1; i < 5; ++i) {
299 if (bc->a[i] < 0) break;
300 if (i > 1) kputc(',', &s);
301 kputc(bc->unseen == i? 'X' : "ACGT"[bc->a[i]], &s);
307 if (bc->ori_ref < 0) kputs("INDEL;", &s);
308 kputs("DP=", &s); kputw(bc->ori_depth, &s); kputs(";I16=", &s);
309 for (i = 0; i < 16; ++i) {
310 if (i) kputc(',', &s);
311 kputw(bc->anno[i], &s);
315 ksprintf(&s, ";VDB=%.4f", bc->vdb);
322 if (is_SP) kputs(":SP", &s);
325 b->m_str = s.m; b->str = s.s; b->l_str = s.l;
327 memcpy(b->gi[0].data, bc->PL, b->gi[0].len * bc->n);
329 uint16_t *dp = (uint16_t*)b->gi[1].data;
330 int32_t *sp = is_SP? b->gi[2].data : 0;
331 for (i = 0; i < bc->n; ++i) {
332 bcf_callret1_t *p = bcr + i;
333 dp[i] = p->depth < 0xffff? p->depth : 0xffff;
335 if (p->anno[0] + p->anno[1] < 2 || p->anno[2] + p->anno[3] < 2
336 || p->anno[0] + p->anno[2] < 2 || p->anno[1] + p->anno[3] < 2)
340 double left, right, two;
342 kt_fisher_exact(p->anno[0], p->anno[1], p->anno[2], p->anno[3], &left, &right, &two);
343 x = (int)(-4.343 * log(two) + .499);
344 if (x > 255) x = 255;