12 KSORT_INIT_GENERIC(uint32_t)
14 #define MINUS_CONST 0x10000000
15 #define INDEL_WINDOW_SIZE 50
17 void *bcf_call_add_rg(void *_hash, const char *hdtext, const char *list)
19 const char *s, *p, *q, *r, *t;
21 if (list == 0 || hdtext == 0) return _hash;
22 if (_hash == 0) _hash = kh_init(rg);
23 hash = (khash_t(rg)*)_hash;
24 if ((s = strstr(hdtext, "@RG\t")) == 0) return hash;
26 t = strstr(s + 4, "@RG\t"); // the next @RG
27 if ((p = strstr(s, "\tID:")) != 0) p += 4;
28 if ((q = strstr(s, "\tPL:")) != 0) q += 4;
29 if (p && q && (t == 0 || (p < t && q < t))) { // ID and PL are both present
32 for (r = p; *r && *r != '\t' && *r != '\n'; ++r); lp = r - p;
33 for (r = q; *r && *r != '\t' && *r != '\n'; ++r); lq = r - q;
34 x = calloc((lp > lq? lp : lq) + 1, 1);
35 for (r = q; *r && *r != '\t' && *r != '\n'; ++r) x[r-q] = *r;
36 if (strstr(list, x)) { // insert ID to the hash table
39 for (r = p; *r && *r != '\t' && *r != '\n'; ++r) x[r-p] = *r;
41 k = kh_get(rg, hash, x);
42 if (k == kh_end(hash)) k = kh_put(rg, hash, x, &ret);
51 void bcf_call_del_rghash(void *_hash)
54 khash_t(rg) *hash = (khash_t(rg)*)_hash;
55 if (hash == 0) return;
56 for (k = kh_begin(hash); k < kh_end(hash); ++k)
57 if (kh_exist(hash, k))
58 free((char*)kh_key(hash, k));
62 static int tpos2qpos(const bam1_core_t *c, const uint32_t *cigar, int32_t tpos, int is_left, int32_t *_tpos)
64 int k, x = c->pos, y = 0, last_y = 0;
66 for (k = 0; k < c->n_cigar; ++k) {
67 int op = cigar[k] & BAM_CIGAR_MASK;
68 int l = cigar[k] >> BAM_CIGAR_SHIFT;
69 if (op == BAM_CMATCH || op == BAM_CEQUAL || op == BAM_CDIFF) {
70 if (c->pos > tpos) return y;
73 return y + (tpos - x);
77 } else if (op == BAM_CINS || op == BAM_CSOFT_CLIP) y += l;
78 else if (op == BAM_CDEL || op == BAM_CREF_SKIP) {
80 *_tpos = is_left? x : x + l;
89 // FIXME: check if the inserted sequence is consistent with the homopolymer run
90 // l is the relative gap length and l_run is the length of the homopolymer on the reference
91 static inline int est_seqQ(const bcf_callaux_t *bca, int l, int l_run)
94 q = bca->openQ + bca->extQ * (abs(l) - 1);
95 qh = l_run >= 3? (int)(bca->tandemQ * (double)abs(l) / l_run + .499) : 1000;
96 return q < qh? q : qh;
99 static inline int est_indelreg(int pos, const char *ref, int l, char *ins4)
101 int i, j, max = 0, max_i = pos, score = 0;
103 for (i = pos + 1, j = 0; ref[i]; ++i, ++j) {
104 if (ins4) score += (toupper(ref[i]) != "ACGTN"[(int)ins4[j%l]])? -10 : 1;
105 else score += (toupper(ref[i]) != toupper(ref[pos+1+j%l]))? -10 : 1;
106 if (score < 0) break;
107 if (max < score) max = score, max_i = i;
113 * @n: number of samples
115 int bcf_call_gap_prep(int n, int *n_plp, bam_pileup1_t **plp, int pos, bcf_callaux_t *bca, const char *ref,
118 int i, s, j, k, t, n_types, *types, max_rd_len, left, right, max_ins, *score1, *score2, max_ref2;
119 int N, K, l_run, ref_type, n_alt;
120 char *inscns = 0, *ref2, *query, **ref_sample;
121 khash_t(rg) *hash = (khash_t(rg)*)rghash;
122 if (ref == 0 || bca == 0) return -1;
123 // mark filtered reads
126 for (s = N = 0; s < n; ++s) {
127 for (i = 0; i < n_plp[s]; ++i) {
128 bam_pileup1_t *p = plp[s] + i;
129 const uint8_t *rg = bam_aux_get(p->b, "RG");
130 p->aux = 1; // filtered by default
132 khint_t k = kh_get(rg, hash, (const char*)(rg + 1));
133 if (k != kh_end(hash)) p->aux = 0, ++N; // not filtered
137 if (N == 0) return -1; // no reads left
139 // determine if there is a gap
140 for (s = N = 0; s < n; ++s) {
141 for (i = 0; i < n_plp[s]; ++i)
142 if (plp[s][i].indel != 0) break;
143 if (i < n_plp[s]) break;
145 if (s == n) return -1; // there is no indel at this position.
146 for (s = N = 0; s < n; ++s) N += n_plp[s]; // N is the total number of reads
147 { // find out how many types of indels are present
148 bca->max_support = bca->max_frac = 0;
149 int m, n_alt = 0, n_tot = 0, indel_support_ok = 0;
151 aux = calloc(N + 1, 4);
153 aux[m++] = MINUS_CONST; // zero indel is always a type
154 for (s = 0; s < n; ++s) {
156 for (i = 0; i < n_plp[s]; ++i) {
157 const bam_pileup1_t *p = plp[s] + i;
158 if (rghash == 0 || p->aux == 0) {
162 aux[m++] = MINUS_CONST + p->indel;
165 j = bam_cigar2qlen(&p->b->core, bam1_cigar(p->b));
166 if (j > max_rd_len) max_rd_len = j;
168 float frac = (float)na/nt;
169 if ( !indel_support_ok && na >= bca->min_support && frac >= bca->min_frac )
170 indel_support_ok = 1;
171 if ( na > bca->max_support && frac > 0 ) bca->max_support = na, bca->max_frac = frac;
175 // To prevent long stretches of N's to be mistaken for indels (sometimes thousands of bases),
176 // check the number of N's in the sequence and skip places where half or more reference bases are Ns.
177 int nN=0; for (i=pos; i-pos<max_rd_len && ref[i]; i++) if ( ref[i]=='N' ) nN++;
178 if ( nN*2>i ) { free(aux); return -1; }
180 ks_introsort(uint32_t, m, aux);
181 // squeeze out identical types
182 for (i = 1, n_types = 1; i < m; ++i)
183 if (aux[i] != aux[i-1]) ++n_types;
184 // Taking totals makes it hard to call rare indels
185 if ( !bca->per_sample_flt )
186 indel_support_ok = ( (float)n_alt / n_tot < bca->min_frac || n_alt < bca->min_support ) ? 0 : 1;
187 if ( n_types == 1 || !indel_support_ok ) { // then skip
188 free(aux); return -1;
192 if (bam_verbose >= 2)
193 fprintf(stderr, "[%s] excessive INDEL alleles at position %d. Skip the position.\n", __func__, pos + 1);
196 types = (int*)calloc(n_types, sizeof(int));
198 types[t++] = aux[0] - MINUS_CONST;
199 for (i = 1; i < m; ++i)
200 if (aux[i] != aux[i-1])
201 types[t++] = aux[i] - MINUS_CONST;
203 for (t = 0; t < n_types; ++t)
204 if (types[t] == 0) break;
205 ref_type = t; // the index of the reference type (0)
207 { // calculate left and right boundary
208 left = pos > INDEL_WINDOW_SIZE? pos - INDEL_WINDOW_SIZE : 0;
209 right = pos + INDEL_WINDOW_SIZE;
210 if (types[0] < 0) right -= types[0];
211 // in case the alignments stand out the reference
212 for (i = pos; i < right; ++i)
213 if (ref[i] == 0) break;
216 /* The following block fixes a long-existing flaw in the INDEL
217 * calling model: the interference of nearby SNPs. However, it also
218 * reduces the power because sometimes, substitutions caused by
219 * indels are not distinguishable from true mutations. Multiple
220 * sequence realignment helps to increase the power.
222 * Masks mismatches present in at least 70% of the reads with 'N'.
224 { // construct per-sample consensus
225 int L = right - left + 1, max_i, max2_i;
226 uint32_t *cns, max, max2;
228 ref_sample = calloc(n, sizeof(void*));
231 for (i = 0; i < right - left; ++i)
232 ref0[i] = bam_nt16_table[(int)ref[i+left]];
233 for (s = 0; s < n; ++s) {
234 r = ref_sample[s] = calloc(L, 1);
235 memset(cns, 0, sizeof(int) * L);
236 // collect ref and non-ref counts
237 for (i = 0; i < n_plp[s]; ++i) {
238 bam_pileup1_t *p = plp[s] + i;
240 uint32_t *cigar = bam1_cigar(b);
241 uint8_t *seq = bam1_seq(b);
242 int x = b->core.pos, y = 0;
243 for (k = 0; k < b->core.n_cigar; ++k) {
244 int op = cigar[k]&0xf;
245 int j, l = cigar[k]>>4;
246 if (op == BAM_CMATCH || op == BAM_CEQUAL || op == BAM_CDIFF) {
247 for (j = 0; j < l; ++j)
248 if (x + j >= left && x + j < right)
249 cns[x+j-left] += (bam1_seqi(seq, y+j) == ref0[x+j-left])? 1 : 0x10000;
251 } else if (op == BAM_CDEL || op == BAM_CREF_SKIP) x += l;
252 else if (op == BAM_CINS || op == BAM_CSOFT_CLIP) y += l;
255 // determine the consensus
256 for (i = 0; i < right - left; ++i) r[i] = ref0[i];
257 max = max2 = 0; max_i = max2_i = -1;
258 for (i = 0; i < right - left; ++i) {
259 if (cns[i]>>16 >= max>>16) max2 = max, max2_i = max_i, max = cns[i], max_i = i;
260 else if (cns[i]>>16 >= max2>>16) max2 = cns[i], max2_i = i;
262 if ((double)(max&0xffff) / ((max&0xffff) + (max>>16)) >= 0.7) max_i = -1;
263 if ((double)(max2&0xffff) / ((max2&0xffff) + (max2>>16)) >= 0.7) max2_i = -1;
264 if (max_i >= 0) r[max_i] = 15;
265 if (max2_i >= 0) r[max2_i] = 15;
266 //for (i = 0; i < right - left; ++i) fputc("=ACMGRSVTWYHKDBN"[(int)r[i]], stderr); fputc('\n', stderr);
268 free(ref0); free(cns);
270 { // the length of the homopolymer run around the current position
271 int c = bam_nt16_table[(int)ref[pos + 1]];
272 if (c == 15) l_run = 1;
274 for (i = pos + 2; ref[i]; ++i)
275 if (bam_nt16_table[(int)ref[i]] != c) break;
277 for (i = pos; i >= 0; --i)
278 if (bam_nt16_table[(int)ref[i]] != c) break;
282 // construct the consensus sequence
283 max_ins = types[n_types - 1]; // max_ins is at least 0
285 int *inscns_aux = calloc(5 * n_types * max_ins, sizeof(int));
286 // count the number of occurrences of each base at each position for each type of insertion
287 for (t = 0; t < n_types; ++t) {
289 for (s = 0; s < n; ++s) {
290 for (i = 0; i < n_plp[s]; ++i) {
291 bam_pileup1_t *p = plp[s] + i;
292 if (p->indel == types[t]) {
293 uint8_t *seq = bam1_seq(p->b);
294 for (k = 1; k <= p->indel; ++k) {
295 int c = bam_nt16_nt4_table[bam1_seqi(seq, p->qpos + k)];
297 ++inscns_aux[(t*max_ins+(k-1))*5 + c];
304 // use the majority rule to construct the consensus
305 inscns = calloc(n_types * max_ins, 1);
306 for (t = 0; t < n_types; ++t) {
307 for (j = 0; j < types[t]; ++j) {
308 int max = 0, max_k = -1, *ia = &inscns_aux[(t*max_ins+j)*5];
309 for (k = 0; k < 5; ++k)
311 max = ia[k], max_k = k;
312 inscns[t*max_ins + j] = max? max_k : 4;
313 if ( max_k==4 ) { types[t] = 0; break; } // discard insertions which contain N's
318 // compute the likelihood given each type of indel for each read
319 max_ref2 = right - left + 2 + 2 * (max_ins > -types[0]? max_ins : -types[0]);
320 ref2 = calloc(max_ref2, 1);
321 query = calloc(right - left + max_rd_len + max_ins + 2, 1);
322 score1 = calloc(N * n_types, sizeof(int));
323 score2 = calloc(N * n_types, sizeof(int));
325 for (t = 0; t < n_types; ++t) {
327 kpa_par_t apf1 = { 1e-4, 1e-2, 10 }, apf2 = { 1e-6, 1e-3, 10 };
328 apf1.bw = apf2.bw = abs(types[t]) + 3;
330 if (types[t] == 0) ir = 0;
331 else if (types[t] > 0) ir = est_indelreg(pos, ref, types[t], &inscns[t*max_ins]);
332 else ir = est_indelreg(pos, ref, -types[t], 0);
333 if (ir > bca->indelreg) bca->indelreg = ir;
334 // fprintf(stderr, "%d, %d, %d\n", pos, types[t], ir);
336 for (s = K = 0; s < n; ++s) {
338 for (k = 0, j = left; j <= pos; ++j)
339 ref2[k++] = bam_nt16_nt4_table[(int)ref_sample[s][j-left]];
340 if (types[t] <= 0) j += -types[t];
341 else for (l = 0; l < types[t]; ++l)
342 ref2[k++] = inscns[t*max_ins + l];
343 for (; j < right && ref[j]; ++j)
344 ref2[k++] = bam_nt16_nt4_table[(int)ref_sample[s][j-left]];
345 for (; k < max_ref2; ++k) ref2[k] = 4;
346 if (j < right) right = j;
347 // align each read to ref2
348 for (i = 0; i < n_plp[s]; ++i, ++K) {
349 bam_pileup1_t *p = plp[s] + i;
350 int qbeg, qend, tbeg, tend, sc, kk;
351 uint8_t *seq = bam1_seq(p->b);
352 uint32_t *cigar = bam1_cigar(p->b);
353 if (p->b->core.flag&4) continue; // unmapped reads
354 // FIXME: the following loop should be better moved outside; nonetheless, realignment should be much slower anyway.
355 for (kk = 0; kk < p->b->core.n_cigar; ++kk)
356 if ((cigar[kk]&BAM_CIGAR_MASK) == BAM_CREF_SKIP) break;
357 if (kk < p->b->core.n_cigar) continue;
358 // FIXME: the following skips soft clips, but using them may be more sensitive.
359 // determine the start and end of sequences for alignment
360 qbeg = tpos2qpos(&p->b->core, bam1_cigar(p->b), left, 0, &tbeg);
361 qend = tpos2qpos(&p->b->core, bam1_cigar(p->b), right, 1, &tend);
364 tbeg = tbeg - l > left? tbeg - l : left;
366 // write the query sequence
367 for (l = qbeg; l < qend; ++l)
368 query[l - qbeg] = bam_nt16_nt4_table[bam1_seqi(seq, l)];
369 { // do realignment; this is the bottleneck
370 const uint8_t *qual = bam1_qual(p->b), *bq;
372 qq = calloc(qend - qbeg, 1);
373 bq = (uint8_t*)bam_aux_get(p->b, "ZQ");
374 if (bq) ++bq; // skip type
375 for (l = qbeg; l < qend; ++l) {
376 qq[l - qbeg] = bq? qual[l] + (bq[l] - 64) : qual[l];
377 if (qq[l - qbeg] > 30) qq[l - qbeg] = 30;
378 if (qq[l - qbeg] < 7) qq[l - qbeg] = 7;
380 sc = kpa_glocal((uint8_t*)ref2 + tbeg - left, tend - tbeg + abs(types[t]),
381 (uint8_t*)query, qend - qbeg, qq, &apf1, 0, 0);
382 l = (int)(100. * sc / (qend - qbeg) + .499); // used for adjusting indelQ below
383 if (l > 255) l = 255;
384 score1[K*n_types + t] = score2[K*n_types + t] = sc<<8 | l;
386 sc = kpa_glocal((uint8_t*)ref2 + tbeg - left, tend - tbeg + abs(types[t]),
387 (uint8_t*)query, qend - qbeg, qq, &apf2, 0, 0);
388 l = (int)(100. * sc / (qend - qbeg) + .499);
389 if (l > 255) l = 255;
390 score2[K*n_types + t] = sc<<8 | l;
395 for (l = 0; l < tend - tbeg + abs(types[t]); ++l)
396 fputc("ACGTN"[(int)ref2[tbeg-left+l]], stderr);
398 for (l = 0; l < qend - qbeg; ++l) fputc("ACGTN"[(int)query[l]], stderr);
400 fprintf(stderr, "pos=%d type=%d read=%d:%d name=%s qbeg=%d tbeg=%d score=%d\n", pos, types[t], s, i, bam1_qname(p->b), qbeg, tbeg, sc);
405 free(ref2); free(query);
408 sc = alloca(n_types * sizeof(int));
409 sumq = alloca(n_types * sizeof(int));
410 memset(sumq, 0, sizeof(int) * n_types);
411 for (s = K = 0; s < n; ++s) {
412 for (i = 0; i < n_plp[s]; ++i, ++K) {
413 bam_pileup1_t *p = plp[s] + i;
414 int *sct = &score1[K*n_types], indelQ1, indelQ2, seqQ, indelQ;
415 for (t = 0; t < n_types; ++t) sc[t] = sct[t]<<6 | t;
416 for (t = 1; t < n_types; ++t) // insertion sort
417 for (j = t; j > 0 && sc[j] < sc[j-1]; --j)
418 tmp = sc[j], sc[j] = sc[j-1], sc[j-1] = tmp;
419 /* errmod_cal() assumes that if the call is wrong, the
420 * likelihoods of other events are equal. This is about
421 * right for substitutions, but is not desired for
422 * indels. To reuse errmod_cal(), I have to make
423 * compromise for multi-allelic indels.
425 if ((sc[0]&0x3f) == ref_type) {
426 indelQ1 = (sc[1]>>14) - (sc[0]>>14);
427 seqQ = est_seqQ(bca, types[sc[1]&0x3f], l_run);
429 for (t = 0; t < n_types; ++t) // look for the reference type
430 if ((sc[t]&0x3f) == ref_type) break;
431 indelQ1 = (sc[t]>>14) - (sc[0]>>14);
432 seqQ = est_seqQ(bca, types[sc[0]&0x3f], l_run);
434 tmp = sc[0]>>6 & 0xff;
435 indelQ1 = tmp > 111? 0 : (int)((1. - tmp/111.) * indelQ1 + .499); // reduce indelQ
436 sct = &score2[K*n_types];
437 for (t = 0; t < n_types; ++t) sc[t] = sct[t]<<6 | t;
438 for (t = 1; t < n_types; ++t) // insertion sort
439 for (j = t; j > 0 && sc[j] < sc[j-1]; --j)
440 tmp = sc[j], sc[j] = sc[j-1], sc[j-1] = tmp;
441 if ((sc[0]&0x3f) == ref_type) {
442 indelQ2 = (sc[1]>>14) - (sc[0]>>14);
444 for (t = 0; t < n_types; ++t) // look for the reference type
445 if ((sc[t]&0x3f) == ref_type) break;
446 indelQ2 = (sc[t]>>14) - (sc[0]>>14);
448 tmp = sc[0]>>6 & 0xff;
449 indelQ2 = tmp > 111? 0 : (int)((1. - tmp/111.) * indelQ2 + .499);
450 // pick the smaller between indelQ1 and indelQ2
451 indelQ = indelQ1 < indelQ2? indelQ1 : indelQ2;
452 if (indelQ > 255) indelQ = 255;
453 if (seqQ > 255) seqQ = 255;
454 p->aux = (sc[0]&0x3f)<<16 | seqQ<<8 | indelQ; // use 22 bits in total
455 sumq[sc[0]&0x3f] += indelQ < seqQ? indelQ : seqQ;
456 // fprintf(stderr, "pos=%d read=%d:%d name=%s call=%d indelQ=%d seqQ=%d\n", pos, s, i, bam1_qname(p->b), types[sc[0]&0x3f], indelQ, seqQ);
459 // determine bca->indel_types[] and bca->inscns
460 bca->maxins = max_ins;
461 bca->inscns = realloc(bca->inscns, bca->maxins * 4);
462 for (t = 0; t < n_types; ++t)
463 sumq[t] = sumq[t]<<6 | t;
464 for (t = 1; t < n_types; ++t) // insertion sort
465 for (j = t; j > 0 && sumq[j] > sumq[j-1]; --j)
466 tmp = sumq[j], sumq[j] = sumq[j-1], sumq[j-1] = tmp;
467 for (t = 0; t < n_types; ++t) // look for the reference type
468 if ((sumq[t]&0x3f) == ref_type) break;
469 if (t) { // then move the reference type to the first
471 for (; t > 0; --t) sumq[t] = sumq[t-1];
474 for (t = 0; t < 4; ++t) bca->indel_types[t] = B2B_INDEL_NULL;
475 for (t = 0; t < 4 && t < n_types; ++t) {
476 bca->indel_types[t] = types[sumq[t]&0x3f];
477 memcpy(&bca->inscns[t * bca->maxins], &inscns[(sumq[t]&0x3f) * max_ins], bca->maxins);
480 for (s = n_alt = 0; s < n; ++s) {
481 for (i = 0; i < n_plp[s]; ++i) {
482 bam_pileup1_t *p = plp[s] + i;
483 int x = types[p->aux>>16&0x3f];
484 for (j = 0; j < 4; ++j)
485 if (x == bca->indel_types[j]) break;
486 p->aux = j<<16 | (j == 4? 0 : (p->aux&0xffff));
487 if ((p->aux>>16&0x3f) > 0) ++n_alt;
488 // fprintf(stderr, "X pos=%d read=%d:%d name=%s call=%d type=%d q=%d seqQ=%d\n", pos, s, i, bam1_qname(p->b), p->aux>>16&63, bca->indel_types[p->aux>>16&63], p->aux&0xff, p->aux>>8&0xff);
492 free(score1); free(score2);
494 for (i = 0; i < n; ++i) free(ref_sample[i]);
496 free(types); free(inscns);
497 return n_alt > 0? 0 : -1;