10 KHASH_SET_INIT_STR(rg)
12 #define MINUS_CONST 0x10000000
13 #define INDEL_WINDOW_SIZE 50
15 #define MIN_SUPPORT_COEF 500
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) {
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;
112 int bcf_call_gap_prep(int n, int *n_plp, bam_pileup1_t **plp, int pos, bcf_callaux_t *bca, const char *ref,
115 extern void ks_introsort_uint32_t(int, uint32_t*);
116 int i, s, j, k, t, n_types, *types, max_rd_len, left, right, max_ins, *score1, *score2;
117 int N, K, l_run, ref_type, n_alt;
118 char *inscns = 0, *ref2, *query;
119 khash_t(rg) *hash = (khash_t(rg)*)rghash;
120 if (ref == 0 || bca == 0) return -1;
121 // mark filtered reads
124 for (s = N = 0; s < n; ++s) {
125 for (i = 0; i < n_plp[s]; ++i) {
126 bam_pileup1_t *p = plp[s] + i;
127 const uint8_t *rg = bam_aux_get(p->b, "RG");
128 p->aux = 1; // filtered by default
130 khint_t k = kh_get(rg, hash, (const char*)(rg + 1));
131 if (k != kh_end(hash)) p->aux = 0, ++N; // not filtered
135 if (N == 0) return -1; // no reads left
137 // determine if there is a gap
138 for (s = N = 0; s < n; ++s) {
139 for (i = 0; i < n_plp[s]; ++i)
140 if (plp[s][i].indel != 0) break;
141 if (i < n_plp[s]) break;
143 if (s == n) return -1; // there is no indel at this position.
144 for (s = N = 0; s < n; ++s) N += n_plp[s]; // N is the total number of reads
145 { // find out how many types of indels are present
146 int m, n_alt = 0, n_tot = 0;
148 aux = calloc(N + 1, 4);
150 aux[m++] = MINUS_CONST; // zero indel is always a type
151 for (s = 0; s < n; ++s) {
152 for (i = 0; i < n_plp[s]; ++i) {
153 const bam_pileup1_t *p = plp[s] + i;
154 if (rghash == 0 || p->aux == 0) {
158 aux[m++] = MINUS_CONST + p->indel;
161 j = bam_cigar2qlen(&p->b->core, bam1_cigar(p->b));
162 if (j > max_rd_len) max_rd_len = j;
165 ks_introsort(uint32_t, m, aux);
166 // squeeze out identical types
167 for (i = 1, n_types = 1; i < m; ++i)
168 if (aux[i] != aux[i-1]) ++n_types;
169 if (n_types == 1 || n_alt * MIN_SUPPORT_COEF < n_tot) { // no indels or too few supporting reads
170 free(aux); return -1;
172 types = (int*)calloc(n_types, sizeof(int));
174 types[t++] = aux[0] - MINUS_CONST;
175 for (i = 1; i < m; ++i)
176 if (aux[i] != aux[i-1])
177 types[t++] = aux[i] - MINUS_CONST;
179 for (t = 0; t < n_types; ++t)
180 if (types[t] == 0) break;
181 ref_type = t; // the index of the reference type (0)
182 assert(n_types < 64);
184 { // calculate left and right boundary
185 left = pos > INDEL_WINDOW_SIZE? pos - INDEL_WINDOW_SIZE : 0;
186 right = pos + INDEL_WINDOW_SIZE;
187 if (types[0] < 0) right -= types[0];
188 // in case the alignments stand out the reference
189 for (i = pos; i < right; ++i)
190 if (ref[i] == 0) break;
193 { // the length of the homopolymer run around the current position
194 int c = bam_nt16_table[(int)ref[pos + 1]];
195 if (c == 15) l_run = 1;
197 for (i = pos + 2; ref[i]; ++i)
198 if (bam_nt16_table[(int)ref[i]] != c) break;
200 for (i = pos; i >= 0; --i)
201 if (bam_nt16_table[(int)ref[i]] != c) break;
205 // construct the consensus sequence
206 max_ins = types[n_types - 1]; // max_ins is at least 0
208 int *inscns_aux = calloc(4 * n_types * max_ins, sizeof(int));
209 // count the number of occurrences of each base at each position for each type of insertion
210 for (t = 0; t < n_types; ++t) {
212 for (s = 0; s < n; ++s) {
213 for (i = 0; i < n_plp[s]; ++i) {
214 bam_pileup1_t *p = plp[s] + i;
215 if (p->indel == types[t]) {
216 uint8_t *seq = bam1_seq(p->b);
217 for (k = 1; k <= p->indel; ++k) {
218 int c = bam_nt16_nt4_table[bam1_seqi(seq, p->qpos + k)];
219 if (c < 4) ++inscns_aux[(t*max_ins+(k-1))*4 + c];
226 // use the majority rule to construct the consensus
227 inscns = calloc(n_types * max_ins, 1);
228 for (t = 0; t < n_types; ++t) {
229 for (j = 0; j < types[t]; ++j) {
230 int max = 0, max_k = -1, *ia = &inscns_aux[(t*max_ins+j)*4];
231 for (k = 0; k < 4; ++k)
233 max = ia[k], max_k = k;
234 inscns[t*max_ins + j] = max? max_k : 4;
239 // compute the likelihood given each type of indel for each read
240 ref2 = calloc(right - left + max_ins + 2, 1);
241 query = calloc(right - left + max_rd_len + max_ins + 2, 1);
242 score1 = calloc(N * n_types, sizeof(int));
243 score2 = calloc(N * n_types, sizeof(int));
245 for (t = 0; t < n_types; ++t) {
247 kpa_par_t apf1 = { 1e-4, 1e-2, 10 }, apf2 = { 1e-6, 1e-3, 10 };
248 apf1.bw = apf2.bw = abs(types[t]) + 3;
250 if (types[t] == 0) ir = 0;
251 else if (types[t] > 0) ir = est_indelreg(pos, ref, types[t], &inscns[t*max_ins]);
252 else ir = est_indelreg(pos, ref, -types[t], 0);
253 if (ir > bca->indelreg) bca->indelreg = ir;
254 // fprintf(stderr, "%d, %d, %d\n", pos, types[t], ir);
256 for (k = 0, j = left; j <= pos; ++j)
257 ref2[k++] = bam_nt16_nt4_table[bam_nt16_table[(int)ref[j]]];
258 if (types[t] <= 0) j += -types[t];
259 else for (l = 0; l < types[t]; ++l)
260 ref2[k++] = inscns[t*max_ins + l];
261 if (types[0] < 0) { // mask deleted sequences to avoid a particular error in the model.
262 int jj, tmp = types[t] >= 0? -types[0] : -types[0] + types[t];
263 for (jj = 0; jj < tmp && j < right && ref[j]; ++jj, ++j)
266 for (; j < right && ref[j]; ++j)
267 ref2[k++] = bam_nt16_nt4_table[bam_nt16_table[(int)ref[j]]];
268 if (j < right) right = j;
269 // align each read to ref2
270 for (s = K = 0; s < n; ++s) {
271 for (i = 0; i < n_plp[s]; ++i, ++K) {
272 bam_pileup1_t *p = plp[s] + i;
273 int qbeg, qend, tbeg, tend, sc;
274 uint8_t *seq = bam1_seq(p->b);
275 // FIXME: the following skips soft clips, but using them may be more sensitive.
276 // determine the start and end of sequences for alignment
277 qbeg = tpos2qpos(&p->b->core, bam1_cigar(p->b), left, 0, &tbeg);
278 qend = tpos2qpos(&p->b->core, bam1_cigar(p->b), right, 1, &tend);
281 tbeg = tbeg - l > left? tbeg - l : left;
283 // write the query sequence
284 for (l = qbeg; l < qend; ++l)
285 query[l - qbeg] = bam_nt16_nt4_table[bam1_seqi(seq, l)];
286 { // do realignment; this is the bottleneck
287 const uint8_t *qual = bam1_qual(p->b), *bq;
289 qq = calloc(qend - qbeg, 1);
290 bq = (uint8_t*)bam_aux_get(p->b, "BQ");
291 if (bq) ++bq; // skip type
292 for (l = qbeg; l < qend; ++l) {
293 qq[l - qbeg] = bq? qual[l] + (bq[l] - 33) : qual[l];
294 if (qq[l - qbeg] > 30) qq[l - qbeg] = 30;
295 if (qq[l - qbeg] < 7) qq[l - qbeg] = 7;
297 sc = kpa_glocal((uint8_t*)ref2 + tbeg - left, tend - tbeg + abs(types[t]),
298 (uint8_t*)query, qend - qbeg, qq, &apf1, 0, 0);
299 score1[K*n_types + t] = score2[K*n_types + t] = sc;
300 if (sc > 5) { // I do not know why, but a long exact match always has sc==5
301 sc = kpa_glocal((uint8_t*)ref2 + tbeg - left, tend - tbeg + abs(types[t]),
302 (uint8_t*)query, qend - qbeg, qq, &apf2, 0, 0);
303 score2[K*n_types + t] = sc;
308 for (l = 0; l < tend - tbeg + abs(types[t]); ++l)
309 fputc("ACGTN"[(int)ref2[tbeg-left+l]], stderr);
311 for (l = 0; l < qend - qbeg; ++l) fputc("ACGTN"[(int)query[l]], stderr);
313 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);
318 free(ref2); free(query);
321 sc = alloca(n_types * sizeof(int));
322 sumq = alloca(n_types * sizeof(int));
323 memset(sumq, 0, sizeof(int) * n_types);
324 for (s = K = 0; s < n; ++s) {
325 for (i = 0; i < n_plp[s]; ++i, ++K) {
326 bam_pileup1_t *p = plp[s] + i;
327 int *sct = &score1[K*n_types], indelQ1, indelQ2, seqQ, indelQ;
328 for (t = 0; t < n_types; ++t) sc[t] = sct[t]<<6 | t;
329 for (t = 1; t < n_types; ++t) // insertion sort
330 for (j = t; j > 0 && sc[j] < sc[j-1]; --j)
331 tmp = sc[j], sc[j] = sc[j-1], sc[j-1] = tmp;
332 /* errmod_cal() assumes that if the call is wrong, the
333 * likelihoods of other events are equal. This is about
334 * right for substitutions, but is not desired for
335 * indels. To reuse errmod_cal(), I have to make
336 * compromise for multi-allelic indels.
338 if ((sc[0]&0x3f) == ref_type) {
339 indelQ1 = (sc[1]>>6) - (sc[0]>>6);
340 seqQ = est_seqQ(bca, types[sc[1]&0x3f], l_run);
342 for (t = 0; t < n_types; ++t) // look for the reference type
343 if ((sc[t]&0x3f) == ref_type) break;
344 indelQ1 = (sc[t]>>6) - (sc[0]>>6);
345 seqQ = est_seqQ(bca, types[sc[0]&0x3f], l_run);
347 sct = &score2[K*n_types];
348 for (t = 0; t < n_types; ++t) sc[t] = sct[t]<<6 | t;
349 for (t = 1; t < n_types; ++t) // insertion sort
350 for (j = t; j > 0 && sc[j] < sc[j-1]; --j)
351 tmp = sc[j], sc[j] = sc[j-1], sc[j-1] = tmp;
352 if ((sc[0]&0x3f) == ref_type) {
353 indelQ2 = (sc[1]>>6) - (sc[0]>>6);
355 for (t = 0; t < n_types; ++t) // look for the reference type
356 if ((sc[t]&0x3f) == ref_type) break;
357 indelQ2 = (sc[t]>>6) - (sc[0]>>6);
359 indelQ = indelQ1 < indelQ2? indelQ1 : indelQ2;
360 if (sc[0]>>6 > MAX_SCORE) indelQ = 0; // too many mismatches; something bad possibly happened
361 p->aux = (sc[0]&0x3f)<<16 | seqQ<<8 | indelQ;
362 sumq[sc[0]&0x3f] += indelQ < seqQ? indelQ : seqQ;
363 // fprintf(stderr, "pos=%d read=%d:%d name=%s call=%d q=%d\n", pos, s, i, bam1_qname(p->b), types[sc[0]&0x3f], indelQ);
366 // determine bca->indel_types[] and bca->inscns
367 bca->maxins = max_ins;
368 bca->inscns = realloc(bca->inscns, bca->maxins * 4);
369 for (t = 0; t < n_types; ++t)
370 sumq[t] = sumq[t]<<6 | t;
371 for (t = 1; t < n_types; ++t) // insertion sort
372 for (j = t; j > 0 && sumq[j] > sumq[j-1]; --j)
373 tmp = sumq[j], sumq[j] = sumq[j-1], sumq[j-1] = tmp;
374 for (t = 0; t < n_types; ++t) // look for the reference type
375 if ((sumq[t]&0x3f) == ref_type) break;
376 if (t) { // then move the reference type to the first
378 for (; t > 0; --t) sumq[t] = sumq[t-1];
381 for (t = 0; t < 4; ++t) bca->indel_types[t] = B2B_INDEL_NULL;
382 for (t = 0; t < 4 && t < n_types; ++t) {
383 bca->indel_types[t] = types[sumq[t]&0x3f];
384 memcpy(&bca->inscns[t * bca->maxins], &inscns[(sumq[t]&0x3f) * max_ins], bca->maxins);
387 for (s = n_alt = 0; s < n; ++s) {
388 for (i = 0; i < n_plp[s]; ++i) {
389 bam_pileup1_t *p = plp[s] + i;
390 int x = types[p->aux>>16&0x3f];
391 for (j = 0; j < 4; ++j)
392 if (x == bca->indel_types[j]) break;
393 p->aux = j<<16 | (j == 4? 0 : (p->aux&0xffff));
394 if ((p->aux>>16&0x3f) > 0) ++n_alt;
395 // 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);
399 free(score1); free(score2);
401 free(types); free(inscns);
402 return n_alt > 0? 0 : -1;