3 #include "bam_maqcns.h"
5 KSORT_INIT_GENERIC(uint32_t)
7 typedef struct __bmc_aux_t {
13 float esum[4], fsum[4];
19 P(<b1,b2>) = \theta \sum_{i=1}^{N-1} 1/i
20 P(D|<b1,b2>) = \sum_{k=1}^{N-1} p_k 1/2 [(k/N)^n_2(1-k/N)^n_1 + (k/N)^n1(1-k/N)^n_2]
21 p_k = i/k / \sum_{i=1}^{N-1} 1/i
23 static void cal_het(bam_maqcns_t *aa)
26 double sum_harmo; // harmonic sum
28 double p1 = 0.0, p3 = 0.0; // just for testing
31 aa->lhet = (double*)calloc(256 * 256, sizeof(double));
33 for (k = 1; k <= aa->n_hap - 1; ++k)
35 for (n1 = 0; n1 < 256; ++n1) {
36 for (n2 = 0; n2 < 256; ++n2) {
37 long double sum = 0.0;
38 double lC = lgamma(n1+n2+1) - lgamma(n1+1) - lgamma(n2+1); // \binom{n1+n2}{n1}
39 for (k = 1; k <= aa->n_hap - 1; ++k) {
40 double pk = 1.0 / k / sum_harmo;
41 double log1 = log((double)k/aa->n_hap);
42 double log2 = log(1.0 - (double)k/aa->n_hap);
43 sum += pk * 0.5 * (expl(log1*n2) * expl(log2*n1) + expl(log1*n1) * expl(log2*n2));
45 aa->lhet[n1<<8|n2] = lC + logl(sum);
46 if (n1 == 17 && n2 == 3) p3 = lC + logl(expl(logl(0.5) * 20));
47 if (n1 == 19 && n2 == 1) p1 = lC + logl(expl(logl(0.5) * 20));
50 poly_rate = aa->het_rate * sum_harmo;
51 aa->q_r = -4.343 * log(2.0 * poly_rate / (1.0 - poly_rate));
54 /** initialize the helper structure */
55 static void cal_coef(bam_maqcns_t *aa)
58 long double sum_a[257], b[256], q_c[256], tmp[256], fk2[256];
61 lC = (double*)calloc(256 * 256, sizeof(double));
62 // aa->lhet will be allocated and initialized
63 free(aa->fk); free(aa->coef);
64 aa->fk = (double*)calloc(256, sizeof(double));
65 aa->coef = (double*)calloc(256*256*64, sizeof(double));
66 aa->fk[0] = fk2[0] = 1.0;
67 for (n = 1; n != 256; ++n) {
68 aa->fk[n] = pow(aa->theta, n) * (1.0 - aa->eta) + aa->eta;
69 fk2[n] = aa->fk[n>>1]; // this is an approximation, assuming reads equally likely come from both strands
71 for (n = 1; n != 256; ++n)
72 for (k = 1; k <= n; ++k)
73 lC[n<<8|k] = lgamma(n+1) - lgamma(k+1) - lgamma(n-k+1);
74 for (q = 1; q != 64; ++q) {
75 double e = pow(10.0, -q/10.0);
77 double le1 = log(1.0-e);
78 for (n = 1; n != 256; ++n) {
79 double *coef = aa->coef + (q<<16|n<<8);
81 for (k = n; k >= 0; --k) { // a_k = \sum_{i=k}^n C^n_k \epsilon^k (1-\epsilon)^{n-k}
82 sum_a[k] = sum_a[k+1] + expl(lC[n<<8|k] + k*le + (n-k)*le1);
83 b[k] = sum_a[k+1] / sum_a[k];
84 if (b[k] > 0.99) b[k] = 0.99;
86 for (k = 0; k != n; ++k) // log(\bar\beta_{nk}(\bar\epsilon)^{f_k})
87 q_c[k] = -4.343 * fk2[k] * logl(b[k] / e);
88 for (k = 1; k != n; ++k) q_c[k] += q_c[k-1]; // \prod_{i=0}^k c_i
89 for (k = 0; k <= n; ++k) { // powl() in 64-bit mode seems broken on my Mac OS X 10.4.9
90 tmp[k] = -4.343 * logl(1.0 - expl(fk2[k] * logl(b[k])));
91 coef[k] = (k? q_c[k-1] : 0) + tmp[k]; // this is the final c_{nk}
98 bam_maqcns_t *bam_maqcns_init()
101 bm = (bam_maqcns_t*)calloc(1, sizeof(bam_maqcns_t));
102 bm->aux = (bmc_aux_t*)calloc(1, sizeof(bmc_aux_t));
103 bm->het_rate = 0.001;
110 void bam_maqcns_prepare(bam_maqcns_t *bm)
112 cal_coef(bm); cal_het(bm);
115 void bam_maqcns_destroy(bam_maqcns_t *bm)
118 free(bm->lhet); free(bm->fk); free(bm->coef); free(bm->aux->info);
119 free(bm->aux); free(bm);
122 glf1_t *bam_maqcns_glfgen(int _n, const bam_pileup1_t *pl, uint8_t ref_base, bam_maqcns_t *bm)
125 int i, j, k, w[8], c, n;
126 glf1_t *g = (glf1_t*)calloc(1, sizeof(glf1_t));
127 float p[16], min_p = 1e30;
129 g->ref_base = ref_base;
130 if (_n == 0) return g;
132 // construct aux array
133 if (bm->aux->max < _n) {
135 kroundup32(bm->aux->max);
136 bm->aux->info = (uint32_t*)realloc(bm->aux->info, 4 * bm->aux->max);
138 for (i = n = 0; i < _n; ++i) {
139 const bam_pileup1_t *p = pl + i;
141 if (p->is_del || (p->b->core.flag&BAM_FUNMAP)) continue;
142 q = (uint32_t)bam1_qual(p->b)[p->qpos];
143 x |= (uint32_t)bam1_strand(p->b) << 18 | q << 8 | p->b->core.qual;
144 if (p->b->core.qual < q) q = p->b->core.qual;
146 q = bam_nt16_nt4_table[bam1_seqi(bam1_seq(p->b), p->qpos)];
147 if (!p->is_del && q < 4) x |= 1 << 21 | q << 16;
148 bm->aux->info[n++] = x;
150 ks_introsort(uint32_t, n, bm->aux->info);
151 // generate esum and fsum
152 b = (glf_call_aux_t*)calloc(1, sizeof(glf_call_aux_t));
153 for (k = 0; k != 8; ++k) w[k] = 0;
155 for (j = n - 1; j >= 0; --j) { // calculate esum and fsum
156 uint32_t info = bm->aux->info[j];
157 if (info>>24 < 4 && (info>>8&0x3f) != 0) info = 4<<24 | (info&0xffffff);
160 b->esum[k&3] += bm->fk[w[k]] * (info>>24);
161 b->fsum[k&3] += bm->fk[w[k]];
162 if (w[k] < 0xff) ++w[k];
165 if (b->mapQ_max < (info&0x7f)) b->mapQ_max = info&0x7f;
168 for (j = c = 0; j != 4; ++j) c += b->c[j];
170 for (j = 0; j != 4; ++j) b->c[j] = (int)(254.0 * b->c[j] / c + 0.5);
171 for (j = c = 0; j != 4; ++j) c += b->c[j];
173 // generate likelihood
174 for (j = 0; j != 4; ++j) {
178 for (k = 0, tmp1 = tmp3 = 0.0, tmp2 = 0; k != 4; ++k) {
179 if (j == k) continue;
180 tmp1 += b->esum[k]; tmp2 += b->c[k]; tmp3 += b->fsum[k];
183 bar_e = (int)(tmp1 / tmp3 + 0.5);
184 if (bar_e < 4) bar_e = 4; // should not happen
185 if (bar_e > 63) bar_e = 63;
186 p[j<<2|j] = tmp1 + bm->coef[bar_e<<16|c<<8|tmp2];
187 } else p[j<<2|j] = 0.0; // all the bases are j
189 for (k = j + 1; k < 4; ++k) {
190 for (i = 0, tmp2 = 0, tmp1 = tmp3 = 0.0; i != 4; ++i) {
191 if (i == j || i == k) continue;
192 tmp1 += b->esum[i]; tmp2 += b->c[i]; tmp3 += b->fsum[i];
195 bar_e = (int)(tmp1 / tmp3 + 0.5);
196 if (bar_e < 4) bar_e = 4;
197 if (bar_e > 63) bar_e = 63;
198 p[j<<2|k] = p[k<<2|j] = -4.343 * bm->lhet[b->c[j]<<8|b->c[k]] + tmp1 + bm->coef[bar_e<<16|c<<8|tmp2];
199 } else p[j<<2|k] = p[k<<2|j] = -4.343 * bm->lhet[b->c[j]<<8|b->c[k]]; // all the bases are either j or k
202 for (k = 0; k != 4; ++k)
203 if (p[j<<2|k] < 0.0) p[j<<2|k] = 0.0;
206 // convert necessary information to glf1_t
207 g->ref_base = ref_base; g->max_mapQ = b->mapQ_max;
208 g->depth = n > 16777215? 16777215 : n;
209 for (j = 0; j != 4; ++j)
210 for (k = j; k < 4; ++k)
211 if (p[j<<2|k] < min_p) min_p = p[j<<2|k];
212 g->min_lk = min_p > 255.0? 255 : (int)(min_p + 0.5);
213 for (j = c = 0; j != 4; ++j)
214 for (k = j; k < 4; ++k)
215 g->lk[c++] = p[j<<2|k]-min_p > 255.0? 255 : (int)(p[j<<2|k]-min_p + 0.5);
221 uint32_t glf2cns(const glf1_t *g, int q_r)
223 int i, j, k, tmp[16], min = 10000, min2 = 10000, min3 = 10000, min_g = -1, min_g2 = -1;
225 for (i = k = 0; i < 4; ++i)
226 for (j = i; j < 4; ++j) {
228 tmp[i<<2|j] = g->lk[k++] + (i == j? 0 : q_r);
230 for (i = 0; i < 16; ++i) {
231 if (tmp[i] < 0) continue;
233 min3 = min2; min2 = min; min = tmp[i]; min_g2 = min_g; min_g = i;
234 } else if (tmp[i] < min2) {
235 min3 = min2; min2 = tmp[i]; min_g2 = i;
236 } else if (tmp[i] < min3) min3 = tmp[i];
238 x = min_g >= 0? (1U<<(min_g>>2&3) | 1U<<(min_g&3)) << 28 : 0xf << 28;
239 x |= min_g2 >= 0? (1U<<(min_g2>>2&3) | 1U<<(min_g2&3)) << 24 : 0xf << 24;
240 x |= (uint32_t)g->max_mapQ << 16;
241 x |= min2 < 10000? (min2 - min < 256? min2 - min : 255) << 8 : 0xff << 8;
242 x |= min2 < 10000 && min3 < 10000? (min3 - min2 < 256? min3 - min2 : 255) : 0xff;
246 uint32_t bam_maqcns_call(int n, const bam_pileup1_t *pl, bam_maqcns_t *bm)
251 g = bam_maqcns_glfgen(n, pl, 0xf, bm);
252 x = glf2cns(g, (int)(bm->q_r + 0.5));
254 } else x = 0xfU<<28 | 0xfU<<24;
258 /************** *****************/
260 bam_maqindel_opt_t *bam_maqindel_opt_init()
262 bam_maqindel_opt_t *mi = (bam_maqindel_opt_t*)calloc(1, sizeof(bam_maqindel_opt_t));
269 void bam_maqindel_ret_destroy(bam_maqindel_ret_t *mir)
271 if (mir == 0) return;
272 free(mir->s1); free(mir->s2); free(mir);
275 #define MINUS_CONST 0x10000000
277 bam_maqindel_ret_t *bam_maqindel(int n, int pos, const bam_maqindel_opt_t *mi, const bam_pileup1_t *pl, const char *ref)
279 int i, j, n_types, *types, left, right;
280 bam_maqindel_ret_t *ret = 0;
281 for (i = 0; i < n; ++i) {
282 const bam_pileup1_t *p = pl + i;
283 if (!(p->b->core.flag&BAM_FUNMAP) && p->indel != 0) break;
285 if (i == n) return 0; // no indel
286 { // calculate how many types of indels are available (set n_types and types)
289 aux = (uint32_t*)calloc(n+1, 4);
291 aux[m++] = MINUS_CONST; // zero indel is always a type
292 for (i = 0; i < n; ++i) {
293 const bam_pileup1_t *p = pl + i;
294 if (!(p->b->core.flag&BAM_FUNMAP) && p->indel != 0)
295 aux[m++] = MINUS_CONST + p->indel;
297 ks_introsort(uint32_t, m, aux);
299 for (i = 1; i < m; ++i)
300 if (aux[i] != aux[i-1]) ++n_types;
301 types = (int*)calloc(n_types, sizeof(int));
303 types[j++] = aux[0] - MINUS_CONST;
304 for (i = 1; i < m; ++i) {
305 if (aux[i] != aux[i-1])
306 types[j++] = aux[i] - MINUS_CONST;
310 { // calculate left and right boundary
312 left = 0x7fffffff; right = 0;
313 for (i = 0; i < n; ++i) {
314 const bam_pileup1_t *p = pl + i;
315 if (!(p->b->core.flag&BAM_FUNMAP)) {
316 bam_segreg(pos, &p->b->core, bam1_cigar(p->b), &seg);
317 if (seg.tbeg < left) left = seg.tbeg;
318 if (seg.tend > right) right = seg.tend;
323 char *ref2, *inscns = 0;
324 int k, l, *score, max_ins = types[n_types-1];
325 ref2 = (char*)calloc(right - left + types[n_types-1] + 2, 1);
326 if (max_ins > 0) { // get the consensus of inserted sequences
327 int *inscns_aux = (int*)calloc(4 * n_types * max_ins, sizeof(int));
329 for (i = 0; i < n_types; ++i) {
330 if (types[i] <= 0) continue; // not insertion
331 for (j = 0; j < n; ++j) {
332 const bam_pileup1_t *p = pl + j;
333 if (!(p->b->core.flag&BAM_FUNMAP) && p->indel == types[i]) {
334 for (k = 1; k <= p->indel; ++k) {
335 int c = bam_nt16_nt4_table[bam1_seqi(bam1_seq(p->b), p->qpos + k)];
336 if (c < 4) ++inscns_aux[i*max_ins*4 + (k-1)*4 + c];
341 // construct the consensus
342 inscns = (char*)calloc(n_types * max_ins, sizeof(char));
343 for (i = 0; i < n_types; ++i) {
344 for (j = 0; j < types[i]; ++j) {
345 int max = 0, max_k = -1, *ia = inscns_aux + i*max_ins*4 + j*4;
346 for (k = 0; k < 4; ++k) {
352 inscns[i*max_ins + j] = max? 1<<max_k : 15;
358 score = (int*)calloc(n_types * n, sizeof(int));
359 for (i = 0; i < n_types; ++i) {
361 for (k = 0, j = left; j <= pos; ++j)
362 ref2[k++] = bam_nt16_table[(int)ref[j]];
363 if (types[i] <= 0) j += -types[i];
364 else for (l = 0; l < types[i]; ++l)
365 ref2[k++] = inscns[i*max_ins + l];
366 for (; j < right && ref[j]; ++j)
367 ref2[k++] = bam_nt16_table[(int)ref[j]];
368 // calculate score for each read
369 for (j = 0; j < n; ++j) {
370 const bam_pileup1_t *p = pl + j;
372 bam1_core_t *c = &p->b->core;
375 if (c->flag&BAM_FUNMAP) continue;
376 cigar = bam1_cigar(p->b);
377 bam_segreg(pos, c, cigar, &seg);
378 for (s = 0, l = seg.qbeg; c->pos + l < right && l < seg.qend; ++l) {
379 int cq = bam1_seqi(bam1_seq(p->b), l), ct;
380 ct = c->pos + l >= left? ref2[c->pos + l - left] : 15; // "<" should not happen if there is no bug
381 if (cq < 15 && ct < 15)
382 s += cq == ct? 1 : -mi->mm_penalty;
385 if (types[i] != 0) { // then try the other way to calculate the score
386 for (s = 0, l = seg.qbeg; c->pos + l + types[i] < right && l < seg.qend; ++l) {
387 int cq = bam1_seqi(bam1_seq(p->b), l), ct;
388 ct = c->pos + l + types[i] >= left? ref2[c->pos + l + types[i] - left] : 15;
389 if (cq < 15 && ct < 15)
390 s += cq == ct? 1 : -mi->mm_penalty;
393 if (score[i*n+j] < s) score[i*n+j] = s; // choose the higher of the two scores
394 if (types[i] != 0) score[i*n+j] -= mi->indel_err;
395 //printf("%d, %d, %d, %d\n", i, types[i], j, score[i*n+j]);
398 { // get final result
399 int *sum, max1, max2, max1_i, max2_i;
400 // pick up the best two score
401 sum = (int*)calloc(n_types, sizeof(int));
402 for (i = 0; i < n_types; ++i)
403 for (j = 0; j < n; ++j)
404 sum[i] += score[i*n+j];
405 max1 = max2 = -0x7fffffff; max1_i = max2_i = -1;
406 for (i = 0; i < n_types; ++i) {
408 max2 = max1; max2_i = max1_i; max1 = sum[i]; max1_i = i;
409 } else if (sum[i] > max2) {
410 max2 = sum[i]; max2_i = i;
415 ret = (bam_maqindel_ret_t*)calloc(1, sizeof(bam_maqindel_ret_t));
416 ret->indel1 = types[max1_i]; ret->indel2 = types[max2_i];
417 ret->s1 = (char*)calloc(abs(ret->indel1) + 2, 1);
418 ret->s2 = (char*)calloc(abs(ret->indel2) + 2, 1);
419 if (ret->indel1 > 0) {
421 for (k = 0; k < ret->indel1; ++k)
422 ret->s1[k+1] = bam_nt16_rev_table[(int)inscns[max1_i*max_ins + k]];
423 } else if (ret->indel1 < 0) {
425 for (k = 0; k < -ret->indel1 && ref[pos + k + 1]; ++k)
426 ret->s1[k+1] = ref[pos + k + 1];
427 } else ret->s1[0] = '*';
428 if (ret->indel2 > 0) {
430 for (k = 0; k < ret->indel2; ++k)
431 ret->s2[k+1] = bam_nt16_rev_table[(int)inscns[max2_i*max_ins + k]];
432 } else if (ret->indel2 < 0) {
434 for (k = 0; k < -ret->indel2 && ref[pos + k + 1]; ++k)
435 ret->s2[k+1] = ref[pos + k + 1];
436 } else ret->s2[0] = '*';
437 for (j = 0; j < n; ++j) {
438 if (score[max1_i*n+j] < 0 && score[max2_i*n+j] < 0) ++ret->cnt_anti;
440 int diff = score[max1_i*n+j] - score[max2_i*n+j];
441 if (diff > mi->ambi_thres) ++ret->cnt1;
442 else if (diff < -mi->ambi_thres) ++ret->cnt2;
443 else ++ret->cnt_ambi;
447 free(score); free(ref2); free(inscns);