0
|
1 #include <math.h>
|
|
2 #include <assert.h>
|
|
3 #include "bam.h"
|
|
4 #include "bam_maqcns.h"
|
|
5 #include "ksort.h"
|
|
6 #include "errmod.h"
|
|
7 #include "kaln.h"
|
|
8 KSORT_INIT_GENERIC(uint32_t)
|
|
9
|
|
10 #define INDEL_WINDOW_SIZE 50
|
|
11 #define INDEL_EXT_DEP 0.9
|
|
12
|
|
13 typedef struct __bmc_aux_t {
|
|
14 int max;
|
|
15 uint32_t *info;
|
|
16 uint16_t *info16;
|
|
17 errmod_t *em;
|
|
18 } bmc_aux_t;
|
|
19
|
|
20 typedef struct {
|
|
21 float esum[4], fsum[4];
|
|
22 uint32_t c[4];
|
|
23 } glf_call_aux_t;
|
|
24
|
|
25 /*
|
|
26 P(<b1,b2>) = \theta \sum_{i=1}^{N-1} 1/i
|
|
27 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]
|
|
28 p_k = 1/k / \sum_{i=1}^{N-1} 1/i
|
|
29 */
|
|
30 static void cal_het(bam_maqcns_t *aa)
|
|
31 {
|
|
32 int k, n1, n2;
|
|
33 double sum_harmo; // harmonic sum
|
|
34 double poly_rate;
|
|
35
|
|
36 free(aa->lhet);
|
|
37 aa->lhet = (double*)calloc(256 * 256, sizeof(double));
|
|
38 sum_harmo = 0.0;
|
|
39 for (k = 1; k <= aa->n_hap - 1; ++k)
|
|
40 sum_harmo += 1.0 / k;
|
|
41 for (n1 = 0; n1 < 256; ++n1) {
|
|
42 for (n2 = 0; n2 < 256; ++n2) {
|
|
43 long double sum = 0.0;
|
|
44 double lC = aa->errmod == BAM_ERRMOD_SOAP? 0 : lgamma(n1+n2+1) - lgamma(n1+1) - lgamma(n2+1);
|
|
45 for (k = 1; k <= aa->n_hap - 1; ++k) {
|
|
46 double pk = 1.0 / k / sum_harmo;
|
|
47 double log1 = log((double)k/aa->n_hap);
|
|
48 double log2 = log(1.0 - (double)k/aa->n_hap);
|
|
49 sum += pk * 0.5 * (expl(log1*n2) * expl(log2*n1) + expl(log1*n1) * expl(log2*n2));
|
|
50 }
|
|
51 aa->lhet[n1<<8|n2] = lC + logl(sum);
|
|
52 }
|
|
53 }
|
|
54 poly_rate = aa->het_rate * sum_harmo;
|
|
55 aa->q_r = -4.343 * log(2.0 * poly_rate / (1.0 - poly_rate));
|
|
56 }
|
|
57
|
|
58 /** initialize the helper structure */
|
|
59 static void cal_coef(bam_maqcns_t *aa)
|
|
60 {
|
|
61 int k, n, q;
|
|
62 long double sum_a[257], b[256], q_c[256], tmp[256], fk2[256];
|
|
63 double *lC;
|
|
64
|
|
65 if (aa->errmod == BAM_ERRMOD_MAQ2) return; // no need to do the following
|
|
66 // aa->lhet will be allocated and initialized
|
|
67 free(aa->fk); free(aa->coef);
|
|
68 aa->coef = 0;
|
|
69 aa->fk = (double*)calloc(256, sizeof(double));
|
|
70 aa->fk[0] = fk2[0] = 1.0;
|
|
71 for (n = 1; n != 256; ++n) {
|
|
72 aa->fk[n] = pow(aa->theta, n) * (1.0 - aa->eta) + aa->eta;
|
|
73 fk2[n] = aa->fk[n>>1]; // this is an approximation, assuming reads equally likely come from both strands
|
|
74 }
|
|
75 if (aa->errmod == BAM_ERRMOD_SOAP) return;
|
|
76 aa->coef = (double*)calloc(256*256*64, sizeof(double));
|
|
77 lC = (double*)calloc(256 * 256, sizeof(double));
|
|
78 for (n = 1; n != 256; ++n)
|
|
79 for (k = 1; k <= n; ++k)
|
|
80 lC[n<<8|k] = lgamma(n+1) - lgamma(k+1) - lgamma(n-k+1);
|
|
81 for (q = 1; q != 64; ++q) {
|
|
82 double e = pow(10.0, -q/10.0);
|
|
83 double le = log(e);
|
|
84 double le1 = log(1.0-e);
|
|
85 for (n = 1; n != 256; ++n) {
|
|
86 double *coef = aa->coef + (q<<16|n<<8);
|
|
87 sum_a[n+1] = 0.0;
|
|
88 for (k = n; k >= 0; --k) { // a_k = \sum_{i=k}^n C^n_k \epsilon^k (1-\epsilon)^{n-k}
|
|
89 sum_a[k] = sum_a[k+1] + expl(lC[n<<8|k] + k*le + (n-k)*le1);
|
|
90 b[k] = sum_a[k+1] / sum_a[k];
|
|
91 if (b[k] > 0.99) b[k] = 0.99;
|
|
92 }
|
|
93 for (k = 0; k != n; ++k) // log(\bar\beta_{nk}(\bar\epsilon)^{f_k})
|
|
94 q_c[k] = -4.343 * fk2[k] * logl(b[k] / e);
|
|
95 for (k = 1; k != n; ++k) q_c[k] += q_c[k-1]; // \prod_{i=0}^k c_i
|
|
96 for (k = 0; k <= n; ++k) { // powl() in 64-bit mode seems broken on my Mac OS X 10.4.9
|
|
97 tmp[k] = -4.343 * logl(1.0 - expl(fk2[k] * logl(b[k])));
|
|
98 coef[k] = (k? q_c[k-1] : 0) + tmp[k]; // this is the final c_{nk}
|
|
99 }
|
|
100 }
|
|
101 }
|
|
102 free(lC);
|
|
103 }
|
|
104
|
|
105 bam_maqcns_t *bam_maqcns_init()
|
|
106 {
|
|
107 bam_maqcns_t *bm;
|
|
108 bm = (bam_maqcns_t*)calloc(1, sizeof(bam_maqcns_t));
|
|
109 bm->aux = (bmc_aux_t*)calloc(1, sizeof(bmc_aux_t));
|
|
110 bm->het_rate = 0.001;
|
|
111 bm->theta = 0.83f;
|
|
112 bm->n_hap = 2;
|
|
113 bm->eta = 0.03;
|
|
114 bm->cap_mapQ = 60;
|
|
115 bm->min_baseQ = 13;
|
|
116 return bm;
|
|
117 }
|
|
118
|
|
119 void bam_maqcns_prepare(bam_maqcns_t *bm)
|
|
120 {
|
|
121 if (bm->errmod == BAM_ERRMOD_MAQ2) bm->aux->em = errmod_init(1. - bm->theta);
|
|
122 cal_coef(bm); cal_het(bm);
|
|
123 }
|
|
124
|
|
125 void bam_maqcns_destroy(bam_maqcns_t *bm)
|
|
126 {
|
|
127 if (bm == 0) return;
|
|
128 free(bm->lhet); free(bm->fk); free(bm->coef); free(bm->aux->info); free(bm->aux->info16);
|
|
129 if (bm->aux->em) errmod_destroy(bm->aux->em);
|
|
130 free(bm->aux); free(bm);
|
|
131 }
|
|
132
|
|
133 glf1_t *bam_maqcns_glfgen(int _n, const bam_pileup1_t *pl, uint8_t ref_base, bam_maqcns_t *bm)
|
|
134 {
|
|
135 glf_call_aux_t *b = 0;
|
|
136 int i, j, k, w[8], c, n;
|
|
137 glf1_t *g = (glf1_t*)calloc(1, sizeof(glf1_t));
|
|
138 float p[16], min_p = 1e30;
|
|
139 uint64_t rms;
|
|
140
|
|
141 g->ref_base = ref_base;
|
|
142 if (_n == 0) return g;
|
|
143
|
|
144 // construct aux array
|
|
145 if (bm->aux->max < _n) {
|
|
146 bm->aux->max = _n;
|
|
147 kroundup32(bm->aux->max);
|
|
148 bm->aux->info = (uint32_t*)realloc(bm->aux->info, 4 * bm->aux->max);
|
|
149 bm->aux->info16 = (uint16_t*)realloc(bm->aux->info16, 2 * bm->aux->max);
|
|
150 }
|
|
151 for (i = n = 0, rms = 0; i < _n; ++i) {
|
|
152 const bam_pileup1_t *p = pl + i;
|
|
153 uint32_t q, x = 0, qq;
|
|
154 uint16_t y = 0;
|
|
155 if (p->is_del || p->is_refskip || (p->b->core.flag&BAM_FUNMAP)) continue;
|
|
156 q = (uint32_t)bam1_qual(p->b)[p->qpos];
|
|
157 if (q < bm->min_baseQ) continue;
|
|
158 x |= (uint32_t)bam1_strand(p->b) << 18 | q << 8 | p->b->core.qual;
|
|
159 y |= bam1_strand(p->b)<<4;
|
|
160 if (p->b->core.qual < q) q = p->b->core.qual;
|
|
161 c = p->b->core.qual < bm->cap_mapQ? p->b->core.qual : bm->cap_mapQ;
|
|
162 rms += c * c;
|
|
163 x |= q << 24;
|
|
164 y |= q << 5;
|
|
165 qq = bam1_seqi(bam1_seq(p->b), p->qpos);
|
|
166 q = bam_nt16_nt4_table[qq? qq : ref_base];
|
|
167 if (!p->is_del && !p->is_refskip && q < 4) x |= 1 << 21 | q << 16, y |= q;
|
|
168 bm->aux->info16[n] = y;
|
|
169 bm->aux->info[n++] = x;
|
|
170 }
|
|
171 rms = (uint8_t)(sqrt((double)rms / n) + .499);
|
|
172 if (bm->errmod == BAM_ERRMOD_MAQ2) {
|
|
173 errmod_cal(bm->aux->em, n, 4, bm->aux->info16, p);
|
|
174 goto goto_glf;
|
|
175 }
|
|
176 ks_introsort(uint32_t, n, bm->aux->info);
|
|
177 // generate esum and fsum
|
|
178 b = (glf_call_aux_t*)calloc(1, sizeof(glf_call_aux_t));
|
|
179 for (k = 0; k != 8; ++k) w[k] = 0;
|
|
180 for (j = n - 1; j >= 0; --j) { // calculate esum and fsum
|
|
181 uint32_t info = bm->aux->info[j];
|
|
182 if (info>>24 < 4 && (info>>8&0x3f) != 0) info = 4<<24 | (info&0xffffff);
|
|
183 k = info>>16&7;
|
|
184 if (info>>24 > 0) {
|
|
185 b->esum[k&3] += bm->fk[w[k]] * (info>>24);
|
|
186 b->fsum[k&3] += bm->fk[w[k]];
|
|
187 if (w[k] < 0xff) ++w[k];
|
|
188 ++b->c[k&3];
|
|
189 }
|
|
190 }
|
|
191 // rescale ->c[]
|
|
192 for (j = c = 0; j != 4; ++j) c += b->c[j];
|
|
193 if (c > 255) {
|
|
194 for (j = 0; j != 4; ++j) b->c[j] = (int)(254.0 * b->c[j] / c + 0.5);
|
|
195 for (j = c = 0; j != 4; ++j) c += b->c[j];
|
|
196 }
|
|
197 if (bm->errmod == BAM_ERRMOD_MAQ) {
|
|
198 // generate likelihood
|
|
199 for (j = 0; j != 4; ++j) {
|
|
200 // homozygous
|
|
201 float tmp1, tmp3;
|
|
202 int tmp2, bar_e;
|
|
203 for (k = 0, tmp1 = tmp3 = 0.0, tmp2 = 0; k != 4; ++k) {
|
|
204 if (j == k) continue;
|
|
205 tmp1 += b->esum[k]; tmp2 += b->c[k]; tmp3 += b->fsum[k];
|
|
206 }
|
|
207 if (tmp2) {
|
|
208 bar_e = (int)(tmp1 / tmp3 + 0.5);
|
|
209 if (bar_e < 4) bar_e = 4; // should not happen
|
|
210 if (bar_e > 63) bar_e = 63;
|
|
211 p[j<<2|j] = tmp1 + bm->coef[bar_e<<16|c<<8|tmp2];
|
|
212 } else p[j<<2|j] = 0.0; // all the bases are j
|
|
213 // heterozygous
|
|
214 for (k = j + 1; k < 4; ++k) {
|
|
215 for (i = 0, tmp2 = 0, tmp1 = tmp3 = 0.0; i != 4; ++i) {
|
|
216 if (i == j || i == k) continue;
|
|
217 tmp1 += b->esum[i]; tmp2 += b->c[i]; tmp3 += b->fsum[i];
|
|
218 }
|
|
219 if (tmp2) {
|
|
220 bar_e = (int)(tmp1 / tmp3 + 0.5);
|
|
221 if (bar_e < 4) bar_e = 4;
|
|
222 if (bar_e > 63) bar_e = 63;
|
|
223 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];
|
|
224 } 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
|
|
225 }
|
|
226 //
|
|
227 for (k = 0; k != 4; ++k)
|
|
228 if (p[j<<2|k] < 0.0) p[j<<2|k] = 0.0;
|
|
229 }
|
|
230
|
|
231 { // fix p[k<<2|k]
|
|
232 float max1, max2, min1, min2;
|
|
233 int max_k, min_k;
|
|
234 max_k = min_k = -1;
|
|
235 max1 = max2 = -1.0; min1 = min2 = 1e30;
|
|
236 for (k = 0; k < 4; ++k) {
|
|
237 if (b->esum[k] > max1) {
|
|
238 max2 = max1; max1 = b->esum[k]; max_k = k;
|
|
239 } else if (b->esum[k] > max2) max2 = b->esum[k];
|
|
240 }
|
|
241 for (k = 0; k < 4; ++k) {
|
|
242 if (p[k<<2|k] < min1) {
|
|
243 min2 = min1; min1 = p[k<<2|k]; min_k = k;
|
|
244 } else if (p[k<<2|k] < min2) min2 = p[k<<2|k];
|
|
245 }
|
|
246 if (max1 > max2 && (min_k != max_k || min1 + 1.0 > min2))
|
|
247 p[max_k<<2|max_k] = min1 > 1.0? min1 - 1.0 : 0.0;
|
|
248 }
|
|
249 } else if (bm->errmod == BAM_ERRMOD_SOAP) { // apply the SOAP model
|
|
250 // generate likelihood
|
|
251 for (j = 0; j != 4; ++j) {
|
|
252 float tmp;
|
|
253 // homozygous
|
|
254 for (k = 0, tmp = 0.0; k != 4; ++k)
|
|
255 if (j != k) tmp += b->esum[k];
|
|
256 p[j<<2|j] = tmp;
|
|
257 // heterozygous
|
|
258 for (k = j + 1; k < 4; ++k) {
|
|
259 for (i = 0, tmp = 0.0; i != 4; ++i)
|
|
260 if (i != j && i != k) tmp += b->esum[i];
|
|
261 p[j<<2|k] = p[k<<2|j] = -4.343 * bm->lhet[b->c[j]<<8|b->c[k]] + tmp;
|
|
262 }
|
|
263 }
|
|
264 }
|
|
265
|
|
266 goto_glf:
|
|
267 // convert necessary information to glf1_t
|
|
268 g->ref_base = ref_base; g->max_mapQ = rms;
|
|
269 g->depth = n > 16777215? 16777215 : n;
|
|
270 for (j = 0; j != 4; ++j)
|
|
271 for (k = j; k < 4; ++k)
|
|
272 if (p[j<<2|k] < min_p) min_p = p[j<<2|k];
|
|
273 g->min_lk = min_p > 255.0? 255 : (int)(min_p + 0.5);
|
|
274 for (j = c = 0; j != 4; ++j)
|
|
275 for (k = j; k < 4; ++k)
|
|
276 g->lk[c++] = p[j<<2|k]-min_p > 255.0? 255 : (int)(p[j<<2|k]-min_p + 0.5);
|
|
277
|
|
278 free(b);
|
|
279 return g;
|
|
280 }
|
|
281
|
|
282 uint32_t glf2cns(const glf1_t *g, int q_r)
|
|
283 {
|
|
284 int i, j, k, p[10], ref4;
|
|
285 uint32_t x = 0;
|
|
286 ref4 = bam_nt16_nt4_table[g->ref_base];
|
|
287 for (i = k = 0; i < 4; ++i)
|
|
288 for (j = i; j < 4; ++j) {
|
|
289 int prior = (i == ref4 && j == ref4? 0 : i == ref4 || j == ref4? q_r : q_r + 3);
|
|
290 p[k] = (g->lk[k] + prior)<<4 | i<<2 | j;
|
|
291 ++k;
|
|
292 }
|
|
293 for (i = 1; i < 10; ++i) // insertion sort
|
|
294 for (j = i; j > 0 && p[j] < p[j-1]; --j)
|
|
295 k = p[j], p[j] = p[j-1], p[j-1] = k;
|
|
296 x = (1u<<(p[0]&3) | 1u<<(p[0]>>2&3)) << 28; // the best genotype
|
|
297 x |= (uint32_t)g->max_mapQ << 16; // rms mapQ
|
|
298 x |= ((p[1]>>4) - (p[0]>>4) < 256? (p[1]>>4) - (p[0]>>4) : 255) << 8; // consensus Q
|
|
299 for (k = 0; k < 10; ++k)
|
|
300 if ((p[k]&0xf) == (ref4<<2|ref4)) break;
|
|
301 if (k == 10) k = 9;
|
|
302 x |= (p[k]>>4) - (p[0]>>4) < 256? (p[k]>>4) - (p[0]>>4) : 255; // snp Q
|
|
303 return x;
|
|
304 }
|
|
305
|
|
306 uint32_t bam_maqcns_call(int n, const bam_pileup1_t *pl, bam_maqcns_t *bm)
|
|
307 {
|
|
308 glf1_t *g;
|
|
309 uint32_t x;
|
|
310 if (n) {
|
|
311 g = bam_maqcns_glfgen(n, pl, 0xf, bm);
|
|
312 x = g->depth == 0? (0xfU<<28 | 0xfU<<24) : glf2cns(g, (int)(bm->q_r + 0.5));
|
|
313 free(g);
|
|
314 } else x = 0xfU<<28 | 0xfU<<24;
|
|
315 return x;
|
|
316 }
|
|
317
|
|
318 /************** *****************/
|
|
319
|
|
320 bam_maqindel_opt_t *bam_maqindel_opt_init()
|
|
321 {
|
|
322 bam_maqindel_opt_t *mi = (bam_maqindel_opt_t*)calloc(1, sizeof(bam_maqindel_opt_t));
|
|
323 mi->q_indel = 40;
|
|
324 mi->r_indel = 0.00015;
|
|
325 mi->r_snp = 0.001;
|
|
326 //
|
|
327 mi->mm_penalty = 3;
|
|
328 mi->indel_err = 4;
|
|
329 mi->ambi_thres = 10;
|
|
330 return mi;
|
|
331 }
|
|
332
|
|
333 void bam_maqindel_ret_destroy(bam_maqindel_ret_t *mir)
|
|
334 {
|
|
335 if (mir == 0) return;
|
|
336 free(mir->s[0]); free(mir->s[1]); free(mir);
|
|
337 }
|
|
338
|
|
339 int bam_tpos2qpos(const bam1_core_t *c, const uint32_t *cigar, int32_t tpos, int is_left, int32_t *_tpos)
|
|
340 {
|
|
341 int k, x = c->pos, y = 0, last_y = 0;
|
|
342 *_tpos = c->pos;
|
|
343 for (k = 0; k < c->n_cigar; ++k) {
|
|
344 int op = cigar[k] & BAM_CIGAR_MASK;
|
|
345 int l = cigar[k] >> BAM_CIGAR_SHIFT;
|
|
346 if (op == BAM_CMATCH) {
|
|
347 if (c->pos > tpos) return y;
|
|
348 if (x + l > tpos) {
|
|
349 *_tpos = tpos;
|
|
350 return y + (tpos - x);
|
|
351 }
|
|
352 x += l; y += l;
|
|
353 last_y = y;
|
|
354 } else if (op == BAM_CINS || op == BAM_CSOFT_CLIP) y += l;
|
|
355 else if (op == BAM_CDEL || op == BAM_CREF_SKIP) {
|
|
356 if (x + l > tpos) {
|
|
357 *_tpos = is_left? x : x + l;
|
|
358 return y;
|
|
359 }
|
|
360 x += l;
|
|
361 }
|
|
362 }
|
|
363 *_tpos = x;
|
|
364 return last_y;
|
|
365 }
|
|
366
|
|
367 #define MINUS_CONST 0x10000000
|
|
368
|
|
369 bam_maqindel_ret_t *bam_maqindel(int n, int pos, const bam_maqindel_opt_t *mi, const bam_pileup1_t *pl, const char *ref,
|
|
370 int _n_types, int *_types)
|
|
371 {
|
|
372 int i, j, n_types, *types, left, right, max_rd_len = 0;
|
|
373 bam_maqindel_ret_t *ret = 0;
|
|
374 // if there is no proposed indel, check if there is an indel from the alignment
|
|
375 if (_n_types == 0) {
|
|
376 for (i = 0; i < n; ++i) {
|
|
377 const bam_pileup1_t *p = pl + i;
|
|
378 if (!(p->b->core.flag&BAM_FUNMAP) && p->indel != 0) break;
|
|
379 }
|
|
380 if (i == n) return 0; // no indel
|
|
381 }
|
|
382 { // calculate how many types of indels are available (set n_types and types)
|
|
383 int m;
|
|
384 uint32_t *aux;
|
|
385 aux = (uint32_t*)calloc(n + _n_types + 1, 4);
|
|
386 m = 0;
|
|
387 aux[m++] = MINUS_CONST; // zero indel is always a type
|
|
388 for (i = 0; i < n; ++i) {
|
|
389 const bam_pileup1_t *p = pl + i;
|
|
390 if (!(p->b->core.flag&BAM_FUNMAP) && p->indel != 0)
|
|
391 aux[m++] = MINUS_CONST + p->indel;
|
|
392 j = bam_cigar2qlen(&p->b->core, bam1_cigar(p->b));
|
|
393 if (j > max_rd_len) max_rd_len = j;
|
|
394 }
|
|
395 if (_n_types) // then also add this to aux[]
|
|
396 for (i = 0; i < _n_types; ++i)
|
|
397 if (_types[i]) aux[m++] = MINUS_CONST + _types[i];
|
|
398 ks_introsort(uint32_t, m, aux);
|
|
399 // squeeze out identical types
|
|
400 for (i = 1, n_types = 1; i < m; ++i)
|
|
401 if (aux[i] != aux[i-1]) ++n_types;
|
|
402 types = (int*)calloc(n_types, sizeof(int));
|
|
403 j = 0;
|
|
404 types[j++] = aux[0] - MINUS_CONST;
|
|
405 for (i = 1; i < m; ++i) {
|
|
406 if (aux[i] != aux[i-1])
|
|
407 types[j++] = aux[i] - MINUS_CONST;
|
|
408 }
|
|
409 free(aux);
|
|
410 }
|
|
411 { // calculate left and right boundary
|
|
412 left = pos > INDEL_WINDOW_SIZE? pos - INDEL_WINDOW_SIZE : 0;
|
|
413 right = pos + INDEL_WINDOW_SIZE;
|
|
414 if (types[0] < 0) right -= types[0];
|
|
415 // in case the alignments stand out the reference
|
|
416 for (i = pos; i < right; ++i)
|
|
417 if (ref[i] == 0) break;
|
|
418 right = i;
|
|
419 }
|
|
420 { // the core part
|
|
421 char *ref2, *rs, *inscns = 0;
|
|
422 int qr_snp, k, l, *score, *pscore, max_ins = types[n_types-1];
|
|
423 qr_snp = (int)(-4.343 * log(mi->r_snp) + .499);
|
|
424 if (max_ins > 0) { // get the consensus of inserted sequences
|
|
425 int *inscns_aux = (int*)calloc(4 * n_types * max_ins, sizeof(int));
|
|
426 // count occurrences
|
|
427 for (i = 0; i < n_types; ++i) {
|
|
428 if (types[i] <= 0) continue; // not insertion
|
|
429 for (j = 0; j < n; ++j) {
|
|
430 const bam_pileup1_t *p = pl + j;
|
|
431 if (!(p->b->core.flag&BAM_FUNMAP) && p->indel == types[i]) {
|
|
432 for (k = 1; k <= p->indel; ++k) {
|
|
433 int c = bam_nt16_nt4_table[bam1_seqi(bam1_seq(p->b), p->qpos + k)];
|
|
434 if (c < 4) ++inscns_aux[i*max_ins*4 + (k-1)*4 + c];
|
|
435 }
|
|
436 }
|
|
437 }
|
|
438 }
|
|
439 // construct the consensus of inserted sequence
|
|
440 inscns = (char*)calloc(n_types * max_ins, sizeof(char));
|
|
441 for (i = 0; i < n_types; ++i) {
|
|
442 for (j = 0; j < types[i]; ++j) {
|
|
443 int max = 0, max_k = -1, *ia = inscns_aux + i*max_ins*4 + j*4;
|
|
444 for (k = 0; k < 4; ++k) {
|
|
445 if (ia[k] > max) {
|
|
446 max = ia[k];
|
|
447 max_k = k;
|
|
448 }
|
|
449 }
|
|
450 inscns[i*max_ins + j] = max? 1<<max_k : 15;
|
|
451 }
|
|
452 }
|
|
453 free(inscns_aux);
|
|
454 }
|
|
455 // calculate score
|
|
456 ref2 = (char*)calloc(right - left + types[n_types-1] + 2, 1);
|
|
457 rs = (char*)calloc(right - left + max_rd_len + types[n_types-1] + 2, 1);
|
|
458 score = (int*)calloc(n_types * n, sizeof(int));
|
|
459 pscore = (int*)calloc(n_types * n, sizeof(int));
|
|
460 for (i = 0; i < n_types; ++i) {
|
|
461 ka_param_t ap = ka_param_blast;
|
|
462 ap.band_width = 2 * types[n_types - 1] + 2;
|
|
463 ap.gap_end_ext = 0;
|
|
464 // write ref2
|
|
465 for (k = 0, j = left; j <= pos; ++j)
|
|
466 ref2[k++] = bam_nt16_nt4_table[bam_nt16_table[(int)ref[j]]];
|
|
467 if (types[i] <= 0) j += -types[i];
|
|
468 else for (l = 0; l < types[i]; ++l)
|
|
469 ref2[k++] = bam_nt16_nt4_table[(int)inscns[i*max_ins + l]];
|
|
470 if (types[0] < 0) { // mask deleted sequences
|
|
471 int jj, tmp = types[i] >= 0? -types[0] : -types[0] + types[i];
|
|
472 for (jj = 0; jj < tmp && j < right && ref[j]; ++jj, ++j)
|
|
473 ref2[k++] = 4;
|
|
474 }
|
|
475 for (; j < right && ref[j]; ++j)
|
|
476 ref2[k++] = bam_nt16_nt4_table[bam_nt16_table[(int)ref[j]]];
|
|
477 if (j < right) right = j;
|
|
478 // calculate score for each read
|
|
479 for (j = 0; j < n; ++j) {
|
|
480 const bam_pileup1_t *p = pl + j;
|
|
481 int qbeg, qend, tbeg, tend;
|
|
482 if (p->b->core.flag & BAM_FUNMAP) continue;
|
|
483 qbeg = bam_tpos2qpos(&p->b->core, bam1_cigar(p->b), left, 0, &tbeg);
|
|
484 qend = bam_tpos2qpos(&p->b->core, bam1_cigar(p->b), right, 1, &tend);
|
|
485 assert(tbeg >= left);
|
|
486 for (l = qbeg; l < qend; ++l)
|
|
487 rs[l - qbeg] = bam_nt16_nt4_table[bam1_seqi(bam1_seq(p->b), l)];
|
|
488 {
|
|
489 int x, y, n_acigar, ps;
|
|
490 uint32_t *acigar;
|
|
491 ps = 0;
|
|
492 if (tend - tbeg + types[i] <= 0) {
|
|
493 score[i*n+j] = -(1<<20);
|
|
494 pscore[i*n+j] = 1<<20;
|
|
495 continue;
|
|
496 }
|
|
497 acigar = ka_global_core((uint8_t*)ref2 + tbeg - left, tend - tbeg + types[i], (uint8_t*)rs, qend - qbeg, &ap, &score[i*n+j], &n_acigar);
|
|
498 x = tbeg - left; y = 0;
|
|
499 for (l = 0; l < n_acigar; ++l) {
|
|
500 int op = acigar[l]&0xf;
|
|
501 int len = acigar[l]>>4;
|
|
502 if (op == BAM_CMATCH) {
|
|
503 int k;
|
|
504 for (k = 0; k < len; ++k)
|
|
505 if (ref2[x+k] != rs[y+k] && ref2[x+k] < 4)
|
|
506 ps += bam1_qual(p->b)[y+k] < qr_snp? bam1_qual(p->b)[y+k] : qr_snp;
|
|
507 x += len; y += len;
|
|
508 } else if (op == BAM_CINS || op == BAM_CSOFT_CLIP) {
|
|
509 if (op == BAM_CINS && l > 0 && l < n_acigar - 1) ps += mi->q_indel * len;
|
|
510 y += len;
|
|
511 } else if (op == BAM_CDEL) {
|
|
512 if (l > 0 && l < n_acigar - 1) ps += mi->q_indel * len;
|
|
513 x += len;
|
|
514 }
|
|
515 }
|
|
516 pscore[i*n+j] = ps;
|
|
517 /*if (1) { // for debugging only
|
|
518 fprintf(stderr, "id=%d, pos=%d, type=%d, j=%d, score=%d, psore=%d, %d, %d, %d, %d, %d, ",
|
|
519 j, pos+1, types[i], j, score[i*n+j], pscore[i*n+j], tbeg, tend, qbeg, qend, mi->q_indel);
|
|
520 for (l = 0; l < n_acigar; ++l) fprintf(stderr, "%d%c", acigar[l]>>4, "MIDS"[acigar[l]&0xf]);
|
|
521 fprintf(stderr, "\n");
|
|
522 for (l = 0; l < tend - tbeg + types[i]; ++l) fputc("ACGTN"[ref2[l+tbeg-left]], stderr);
|
|
523 fputc('\n', stderr);
|
|
524 for (l = 0; l < qend - qbeg; ++l) fputc("ACGTN"[rs[l]], stderr);
|
|
525 fputc('\n', stderr);
|
|
526 }*/
|
|
527 free(acigar);
|
|
528 }
|
|
529 }
|
|
530 }
|
|
531 { // get final result
|
|
532 int *sum, max1, max2, max1_i, max2_i;
|
|
533 // pick up the best two score
|
|
534 sum = (int*)calloc(n_types, sizeof(int));
|
|
535 for (i = 0; i < n_types; ++i)
|
|
536 for (j = 0; j < n; ++j)
|
|
537 sum[i] += -pscore[i*n+j];
|
|
538 max1 = max2 = -0x7fffffff; max1_i = max2_i = -1;
|
|
539 for (i = 0; i < n_types; ++i) {
|
|
540 if (sum[i] > max1) {
|
|
541 max2 = max1; max2_i = max1_i; max1 = sum[i]; max1_i = i;
|
|
542 } else if (sum[i] > max2) {
|
|
543 max2 = sum[i]; max2_i = i;
|
|
544 }
|
|
545 }
|
|
546 free(sum);
|
|
547 // write ret
|
|
548 ret = (bam_maqindel_ret_t*)calloc(1, sizeof(bam_maqindel_ret_t));
|
|
549 ret->indel1 = types[max1_i]; ret->indel2 = types[max2_i];
|
|
550 ret->s[0] = (char*)calloc(abs(ret->indel1) + 2, 1);
|
|
551 ret->s[1] = (char*)calloc(abs(ret->indel2) + 2, 1);
|
|
552 // write indel sequence
|
|
553 if (ret->indel1 > 0) {
|
|
554 ret->s[0][0] = '+';
|
|
555 for (k = 0; k < ret->indel1; ++k)
|
|
556 ret->s[0][k+1] = bam_nt16_rev_table[(int)inscns[max1_i*max_ins + k]];
|
|
557 } else if (ret->indel1 < 0) {
|
|
558 ret->s[0][0] = '-';
|
|
559 for (k = 0; k < -ret->indel1 && ref[pos + k + 1]; ++k)
|
|
560 ret->s[0][k+1] = ref[pos + k + 1];
|
|
561 } else ret->s[0][0] = '*';
|
|
562 if (ret->indel2 > 0) {
|
|
563 ret->s[1][0] = '+';
|
|
564 for (k = 0; k < ret->indel2; ++k)
|
|
565 ret->s[1][k+1] = bam_nt16_rev_table[(int)inscns[max2_i*max_ins + k]];
|
|
566 } else if (ret->indel2 < 0) {
|
|
567 ret->s[1][0] = '-';
|
|
568 for (k = 0; k < -ret->indel2 && ref[pos + k + 1]; ++k)
|
|
569 ret->s[1][k+1] = ref[pos + k + 1];
|
|
570 } else ret->s[1][0] = '*';
|
|
571 // write count
|
|
572 for (i = 0; i < n; ++i) {
|
|
573 const bam_pileup1_t *p = pl + i;
|
|
574 if (p->indel == ret->indel1) ++ret->cnt1;
|
|
575 else if (p->indel == ret->indel2) ++ret->cnt2;
|
|
576 else ++ret->cnt_anti;
|
|
577 }
|
|
578 { // write gl[]
|
|
579 int tmp, seq_err = 0;
|
|
580 double x = 1.0;
|
|
581 tmp = max1_i - max2_i;
|
|
582 if (tmp < 0) tmp = -tmp;
|
|
583 for (j = 0; j < tmp + 1; ++j) x *= INDEL_EXT_DEP;
|
|
584 seq_err = mi->q_indel * (1.0 - x) / (1.0 - INDEL_EXT_DEP);
|
|
585 ret->gl[0] = ret->gl[1] = 0;
|
|
586 for (j = 0; j < n; ++j) {
|
|
587 int s1 = pscore[max1_i*n + j], s2 = pscore[max2_i*n + j];
|
|
588 //fprintf(stderr, "id=%d, %d, %d, %d, %d, %d\n", j, pl[j].b->core.pos+1, types[max1_i], types[max2_i], s1, s2);
|
|
589 if (s1 > s2) ret->gl[0] += s1 - s2 < seq_err? s1 - s2 : seq_err;
|
|
590 else ret->gl[1] += s2 - s1 < seq_err? s2 - s1 : seq_err;
|
|
591 }
|
|
592 }
|
|
593 // write cnt_ref and cnt_ambi
|
|
594 if (max1_i != 0 && max2_i != 0) {
|
|
595 for (j = 0; j < n; ++j) {
|
|
596 int diff1 = score[j] - score[max1_i * n + j];
|
|
597 int diff2 = score[j] - score[max2_i * n + j];
|
|
598 if (diff1 > 0 && diff2 > 0) ++ret->cnt_ref;
|
|
599 else if (diff1 == 0 || diff2 == 0) ++ret->cnt_ambi;
|
|
600 }
|
|
601 }
|
|
602 }
|
|
603 free(score); free(pscore); free(ref2); free(rs); free(inscns);
|
|
604 }
|
|
605 { // call genotype
|
|
606 int q[3], qr_indel = (int)(-4.343 * log(mi->r_indel) + 0.5);
|
|
607 int min1, min2, min1_i;
|
|
608 q[0] = ret->gl[0] + (ret->s[0][0] != '*'? 0 : 0) * qr_indel;
|
|
609 q[1] = ret->gl[1] + (ret->s[1][0] != '*'? 0 : 0) * qr_indel;
|
|
610 q[2] = n * 3 + (ret->s[0][0] == '*' || ret->s[1][0] == '*'? 1 : 1) * qr_indel;
|
|
611 min1 = min2 = 0x7fffffff; min1_i = -1;
|
|
612 for (i = 0; i < 3; ++i) {
|
|
613 if (q[i] < min1) {
|
|
614 min2 = min1; min1 = q[i]; min1_i = i;
|
|
615 } else if (q[i] < min2) min2 = q[i];
|
|
616 }
|
|
617 ret->gt = min1_i;
|
|
618 ret->q_cns = min2 - min1;
|
|
619 // set q_ref
|
|
620 if (ret->gt < 2) ret->q_ref = (ret->s[ret->gt][0] == '*')? 0 : q[1-ret->gt] - q[ret->gt] - qr_indel - 3;
|
|
621 else ret->q_ref = (ret->s[0][0] == '*')? q[0] - q[2] : q[1] - q[2];
|
|
622 if (ret->q_ref < 0) ret->q_ref = 0;
|
|
623 }
|
|
624 free(types);
|
|
625 return ret;
|
|
626 }
|