]> git.donarmstrong.com Git - samtools.git/blob - bam2bcf_indel.c
* samtools-0.1.10-6 (r839)
[samtools.git] / bam2bcf_indel.c
1 #include <assert.h>
2 #include <ctype.h>
3 #include <string.h>
4 #include "bam.h"
5 #include "bam2bcf.h"
6 #include "ksort.h"
7 #include "kaln.h"
8 #include "kprobaln.h"
9 #include "khash.h"
10 KHASH_SET_INIT_STR(rg)
11
12 #define MINUS_CONST 0x10000000
13 #define INDEL_WINDOW_SIZE 50
14 #define MIN_SUPPORT_COEF 500
15
16 void *bcf_call_add_rg(void *_hash, const char *hdtext, const char *list)
17 {
18         const char *s, *p, *q, *r, *t;
19         khash_t(rg) *hash;
20         if (list == 0 || hdtext == 0) return _hash;
21         if (_hash == 0) _hash = kh_init(rg);
22         hash = (khash_t(rg)*)_hash;
23         if ((s = strstr(hdtext, "@RG\t")) == 0) return hash;
24         do {
25                 t = strstr(s + 4, "@RG\t"); // the next @RG
26                 if ((p = strstr(s, "\tID:")) != 0) p += 4;
27                 if ((q = strstr(s, "\tPL:")) != 0) q += 4;
28                 if (p && q && (t == 0 || (p < t && q < t))) { // ID and PL are both present
29                         int lp, lq;
30                         char *x;
31                         for (r = p; *r && *r != '\t' && *r != '\n'; ++r); lp = r - p;
32                         for (r = q; *r && *r != '\t' && *r != '\n'; ++r); lq = r - q;
33                         x = calloc((lp > lq? lp : lq) + 1, 1);
34                         for (r = q; *r && *r != '\t' && *r != '\n'; ++r) x[r-q] = *r;
35                         if (strstr(list, x)) { // insert ID to the hash table
36                                 khint_t k;
37                                 int ret;
38                                 for (r = p; *r && *r != '\t' && *r != '\n'; ++r) x[r-p] = *r;
39                                 x[r-p] = 0;
40                                 k = kh_get(rg, hash, x);
41                                 if (k == kh_end(hash)) k = kh_put(rg, hash, x, &ret);
42                                 else free(x);
43                         } else free(x);
44                 }
45                 s = t;
46         } while (s);
47         return hash;
48 }
49
50 void bcf_call_del_rghash(void *_hash)
51 {
52         khint_t k;
53         khash_t(rg) *hash = (khash_t(rg)*)_hash;
54         if (hash == 0) return;
55         for (k = kh_begin(hash); k < kh_end(hash); ++k)
56                 if (kh_exist(hash, k))
57                         free((char*)kh_key(hash, k));
58         kh_destroy(rg, hash);
59 }
60
61 static int tpos2qpos(const bam1_core_t *c, const uint32_t *cigar, int32_t tpos, int is_left, int32_t *_tpos)
62 {
63         int k, x = c->pos, y = 0, last_y = 0;
64         *_tpos = c->pos;
65         for (k = 0; k < c->n_cigar; ++k) {
66                 int op = cigar[k] & BAM_CIGAR_MASK;
67                 int l = cigar[k] >> BAM_CIGAR_SHIFT;
68                 if (op == BAM_CMATCH) {
69                         if (c->pos > tpos) return y;
70                         if (x + l > tpos) {
71                                 *_tpos = tpos;
72                                 return y + (tpos - x);
73                         }
74                         x += l; y += l;
75                         last_y = y;
76                 } else if (op == BAM_CINS || op == BAM_CSOFT_CLIP) y += l;
77                 else if (op == BAM_CDEL || op == BAM_CREF_SKIP) {
78                         if (x + l > tpos) {
79                                 *_tpos = is_left? x : x + l;
80                                 return y;
81                         }
82                         x += l;
83                 }
84         }
85         *_tpos = x;
86         return last_y;
87 }
88 // FIXME: check if the inserted sequence is consistent with the homopolymer run
89 // l is the relative gap length and l_run is the length of the homopolymer on the reference
90 static inline int est_seqQ(const bcf_callaux_t *bca, int l, int l_run)
91 {
92         int q, qh;
93         q = bca->openQ + bca->extQ * (abs(l) - 1);
94         qh = l_run >= 3? (int)(bca->tandemQ * (double)abs(l) / l_run + .499) : 1000;
95         return q < qh? q : qh;
96 }
97
98 static inline int est_indelreg(int pos, const char *ref, int l, char *ins4)
99 {
100         int i, j, max = 0, max_i = pos, score = 0;
101         l = abs(l);
102         for (i = pos + 1, j = 0; ref[i]; ++i, ++j) {
103                 if (ins4) score += (toupper(ref[i]) != "ACGTN"[(int)ins4[j%l]])? -10 : 1;
104                 else score += (toupper(ref[i]) != toupper(ref[pos+1+j%l]))? -10 : 1;
105                 if (score < 0) break;
106                 if (max < score) max = score, max_i = i;
107         }
108         return max_i - pos;
109 }
110
111 int bcf_call_gap_prep(int n, int *n_plp, bam_pileup1_t **plp, int pos, bcf_callaux_t *bca, const char *ref,
112                                           const void *rghash)
113 {
114         extern void ks_introsort_uint32_t(int, uint32_t*);
115         int i, s, j, k, t, n_types, *types, max_rd_len, left, right, max_ins, *score1, *score2;
116         int N, K, l_run, ref_type, n_alt;
117         char *inscns = 0, *ref2, *query;
118         khash_t(rg) *hash = (khash_t(rg)*)rghash;
119         if (ref == 0 || bca == 0) return -1;
120         // mark filtered reads
121         if (rghash) {
122                 N = 0;
123                 for (s = N = 0; s < n; ++s) {
124                         for (i = 0; i < n_plp[s]; ++i) {
125                                 bam_pileup1_t *p = plp[s] + i;
126                                 const uint8_t *rg = bam_aux_get(p->b, "RG");
127                                 p->aux = 1; // filtered by default
128                                 if (rg) {
129                                         khint_t k = kh_get(rg, hash, (const char*)(rg + 1));
130                                         if (k != kh_end(hash)) p->aux = 0, ++N; // not filtered
131                                 }
132                         }
133                 }
134                 if (N == 0) return -1; // no reads left
135         }
136         // determine if there is a gap
137         for (s = N = 0; s < n; ++s) {
138                 for (i = 0; i < n_plp[s]; ++i)
139                         if (plp[s][i].indel != 0) break;
140                 if (i < n_plp[s]) break;
141         }
142         if (s == n) return -1; // there is no indel at this position.
143         for (s = N = 0; s < n; ++s) N += n_plp[s]; // N is the total number of reads
144         { // find out how many types of indels are present
145                 int m, n_alt = 0, n_tot = 0;
146                 uint32_t *aux;
147                 aux = calloc(N + 1, 4);
148                 m = max_rd_len = 0;
149                 aux[m++] = MINUS_CONST; // zero indel is always a type
150                 for (s = 0; s < n; ++s) {
151                         for (i = 0; i < n_plp[s]; ++i) {
152                                 const bam_pileup1_t *p = plp[s] + i;
153                                 if (rghash == 0 || p->aux == 0) {
154                                         ++n_tot;
155                                         if (p->indel != 0) {
156                                                 ++n_alt;
157                                                 aux[m++] = MINUS_CONST + p->indel;
158                                         }
159                                 }
160                                 j = bam_cigar2qlen(&p->b->core, bam1_cigar(p->b));
161                                 if (j > max_rd_len) max_rd_len = j;
162                         }
163                 }
164                 ks_introsort(uint32_t, m, aux);
165                 // squeeze out identical types
166                 for (i = 1, n_types = 1; i < m; ++i)
167                         if (aux[i] != aux[i-1]) ++n_types;
168                 if (n_types == 1 || n_alt * MIN_SUPPORT_COEF < n_tot) { // no indels or too few supporting reads
169                         free(aux); return -1;
170                 }
171                 types = (int*)calloc(n_types, sizeof(int));
172                 t = 0;
173                 types[t++] = aux[0] - MINUS_CONST; 
174                 for (i = 1; i < m; ++i)
175                         if (aux[i] != aux[i-1])
176                                 types[t++] = aux[i] - MINUS_CONST;
177                 free(aux);
178                 for (t = 0; t < n_types; ++t)
179                         if (types[t] == 0) break;
180                 ref_type = t; // the index of the reference type (0)
181                 assert(n_types < 64);
182         }
183         { // calculate left and right boundary
184                 left = pos > INDEL_WINDOW_SIZE? pos - INDEL_WINDOW_SIZE : 0;
185                 right = pos + INDEL_WINDOW_SIZE;
186                 if (types[0] < 0) right -= types[0];
187                 // in case the alignments stand out the reference
188                 for (i = pos; i < right; ++i)
189                         if (ref[i] == 0) break;
190                 right = i;
191         }
192         { // the length of the homopolymer run around the current position
193                 int c = bam_nt16_table[(int)ref[pos + 1]];
194                 if (c == 15) l_run = 1;
195                 else {
196                         for (i = pos + 2; ref[i]; ++i)
197                                 if (bam_nt16_table[(int)ref[i]] != c) break;
198                         l_run = i;
199                         for (i = pos; i >= 0; --i)
200                                 if (bam_nt16_table[(int)ref[i]] != c) break;
201                         l_run -= i + 1;
202                 }
203         }
204         // construct the consensus sequence
205         max_ins = types[n_types - 1]; // max_ins is at least 0
206         if (max_ins > 0) {
207                 int *inscns_aux = calloc(4 * n_types * max_ins, sizeof(int));
208                 // count the number of occurrences of each base at each position for each type of insertion
209                 for (t = 0; t < n_types; ++t) {
210                         if (types[t] > 0) {
211                                 for (s = 0; s < n; ++s) {
212                                         for (i = 0; i < n_plp[s]; ++i) {
213                                                 bam_pileup1_t *p = plp[s] + i;
214                                                 if (p->indel == types[t]) {
215                                                         uint8_t *seq = bam1_seq(p->b);
216                                                         for (k = 1; k <= p->indel; ++k) {
217                                                                 int c = bam_nt16_nt4_table[bam1_seqi(seq, p->qpos + k)];
218                                                                 if (c < 4) ++inscns_aux[(t*max_ins+(k-1))*4 + c];
219                                                         }
220                                                 }
221                                         }
222                                 }
223                         }
224                 }
225                 // use the majority rule to construct the consensus
226                 inscns = calloc(n_types * max_ins, 1);
227                 for (t = 0; t < n_types; ++t) {
228                         for (j = 0; j < types[t]; ++j) {
229                                 int max = 0, max_k = -1, *ia = &inscns_aux[(t*max_ins+j)*4];
230                                 for (k = 0; k < 4; ++k)
231                                         if (ia[k] > max)
232                                                 max = ia[k], max_k = k;
233                                 inscns[t*max_ins + j] = max? max_k : 4;
234                         }
235                 }
236                 free(inscns_aux);
237         }
238         // compute the likelihood given each type of indel for each read
239         ref2  = calloc(right - left + max_ins + 2, 1);
240         query = calloc(right - left + max_rd_len + max_ins + 2, 1);
241         score1 = calloc(N * n_types, sizeof(int));
242         score2 = calloc(N * n_types, sizeof(int));
243         bca->indelreg = 0;
244         for (t = 0; t < n_types; ++t) {
245                 int l, ir;
246                 kpa_par_t apf1 = { 1e-4, 1e-2, 10 }, apf2 = { 1e-6, 1e-3, 10 };
247                 apf1.bw = apf2.bw = abs(types[t]) + 3;
248                 // compute indelreg
249                 if (types[t] == 0) ir = 0;
250                 else if (types[t] > 0) ir = est_indelreg(pos, ref, types[t], &inscns[t*max_ins]);
251                 else ir = est_indelreg(pos, ref, -types[t], 0);
252                 if (ir > bca->indelreg) bca->indelreg = ir;
253 //              fprintf(stderr, "%d, %d, %d\n", pos, types[t], ir);
254                 // write ref2
255                 for (k = 0, j = left; j <= pos; ++j)
256                         ref2[k++] = bam_nt16_nt4_table[bam_nt16_table[(int)ref[j]]];
257                 if (types[t] <= 0) j += -types[t];
258                 else for (l = 0; l < types[t]; ++l)
259                                  ref2[k++] = inscns[t*max_ins + l];
260                 if (types[0] < 0) { // mask deleted sequences to avoid a particular error in the model.
261                         int jj, tmp = types[t] >= 0? -types[0] : -types[0] + types[t];
262                         for (jj = 0; jj < tmp && j < right && ref[j]; ++jj, ++j)
263                                 ref2[k++] = 4;
264                 }
265                 for (; j < right && ref[j]; ++j)
266                         ref2[k++] = bam_nt16_nt4_table[bam_nt16_table[(int)ref[j]]];
267                 if (j < right) right = j;
268                 // align each read to ref2
269                 for (s = K = 0; s < n; ++s) {
270                         for (i = 0; i < n_plp[s]; ++i, ++K) {
271                                 bam_pileup1_t *p = plp[s] + i;
272                                 int qbeg, qend, tbeg, tend, sc;
273                                 uint8_t *seq = bam1_seq(p->b);
274                                 // FIXME: the following skips soft clips, but using them may be more sensitive.
275                                 // determine the start and end of sequences for alignment
276                                 qbeg = tpos2qpos(&p->b->core, bam1_cigar(p->b), left,  0, &tbeg);
277                                 qend = tpos2qpos(&p->b->core, bam1_cigar(p->b), right, 1, &tend);
278                                 if (types[t] < 0) {
279                                         int l = -types[t];
280                                         tbeg = tbeg - l > left?  tbeg - l : left;
281                                 }
282                                 // write the query sequence
283                                 for (l = qbeg; l < qend; ++l)
284                                         query[l - qbeg] = bam_nt16_nt4_table[bam1_seqi(seq, l)];
285                                 { // do realignment; this is the bottleneck
286                                         const uint8_t *qual = bam1_qual(p->b), *bq;
287                                         uint8_t *qq;
288                                         qq = calloc(qend - qbeg, 1);
289                                         bq = (uint8_t*)bam_aux_get(p->b, "ZQ");
290                                         if (bq) ++bq; // skip type
291                                         for (l = qbeg; l < qend; ++l) {
292                                                 qq[l - qbeg] = bq? qual[l] + (bq[l] - 64) : qual[l];
293                                                 if (qq[l - qbeg] > 30) qq[l - qbeg] = 30;
294                                                 if (qq[l - qbeg] < 7) qq[l - qbeg] = 7;
295                                         }
296                                         sc = kpa_glocal((uint8_t*)ref2 + tbeg - left, tend - tbeg + abs(types[t]),
297                                                                         (uint8_t*)query, qend - qbeg, qq, &apf1, 0, 0);
298                                         l = (int)(100. * sc / (qend - qbeg) + .499); // used for adjusting indelQ below
299                                         if (l > 255) l = 255;
300                                         score1[K*n_types + t] = score2[K*n_types + t] = sc<<8 | l;
301                                         if (sc > 5) {
302                                                 sc = kpa_glocal((uint8_t*)ref2 + tbeg - left, tend - tbeg + abs(types[t]),
303                                                                                 (uint8_t*)query, qend - qbeg, qq, &apf2, 0, 0);
304                                                 l = (int)(100. * sc / (qend - qbeg) + .499);
305                                                 if (l > 255) l = 255;
306                                                 score2[K*n_types + t] = sc<<8 | l;
307                                         }
308                                         free(qq);
309                                 }
310 /*
311                                 for (l = 0; l < tend - tbeg + abs(types[t]); ++l)
312                                         fputc("ACGTN"[(int)ref2[tbeg-left+l]], stderr);
313                                 fputc('\n', stderr);
314                                 for (l = 0; l < qend - qbeg; ++l) fputc("ACGTN"[(int)query[l]], stderr);
315                                 fputc('\n', stderr);
316                                 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);
317 */
318                         }
319                 }
320         }
321         free(ref2); free(query);
322         { // compute indelQ
323                 int *sc, tmp, *sumq;
324                 sc   = alloca(n_types * sizeof(int));
325                 sumq = alloca(n_types * sizeof(int));
326                 memset(sumq, 0, sizeof(int) * n_types);
327                 for (s = K = 0; s < n; ++s) {
328                         for (i = 0; i < n_plp[s]; ++i, ++K) {
329                                 bam_pileup1_t *p = plp[s] + i;
330                                 int *sct = &score1[K*n_types], indelQ1, indelQ2, seqQ, indelQ;
331                                 for (t = 0; t < n_types; ++t) sc[t] = sct[t]<<6 | t;
332                                 for (t = 1; t < n_types; ++t) // insertion sort
333                                         for (j = t; j > 0 && sc[j] < sc[j-1]; --j)
334                                                 tmp = sc[j], sc[j] = sc[j-1], sc[j-1] = tmp;
335                                 /* errmod_cal() assumes that if the call is wrong, the
336                                  * likelihoods of other events are equal. This is about
337                                  * right for substitutions, but is not desired for
338                                  * indels. To reuse errmod_cal(), I have to make
339                                  * compromise for multi-allelic indels.
340                                  */
341                                 if ((sc[0]&0x3f) == ref_type) {
342                                         indelQ1 = (sc[1]>>14) - (sc[0]>>14);
343                                         seqQ = est_seqQ(bca, types[sc[1]&0x3f], l_run);
344                                 } else {
345                                         for (t = 0; t < n_types; ++t) // look for the reference type
346                                                 if ((sc[t]&0x3f) == ref_type) break;
347                                         indelQ1 = (sc[t]>>14) - (sc[0]>>14);
348                                         seqQ = est_seqQ(bca, types[sc[0]&0x3f], l_run);
349                                 }
350                                 tmp = sc[0]>>6 & 0xff;
351                                 indelQ1 = tmp > 111? 0 : (int)((1. - tmp/111.) * indelQ1 + .499); // reduce indelQ
352                                 sct = &score2[K*n_types];
353                                 for (t = 0; t < n_types; ++t) sc[t] = sct[t]<<6 | t;
354                                 for (t = 1; t < n_types; ++t) // insertion sort
355                                         for (j = t; j > 0 && sc[j] < sc[j-1]; --j)
356                                                 tmp = sc[j], sc[j] = sc[j-1], sc[j-1] = tmp;
357                                 if ((sc[0]&0x3f) == ref_type) {
358                                         indelQ2 = (sc[1]>>14) - (sc[0]>>14);
359                                 } else {
360                                         for (t = 0; t < n_types; ++t) // look for the reference type
361                                                 if ((sc[t]&0x3f) == ref_type) break;
362                                         indelQ2 = (sc[t]>>14) - (sc[0]>>14);
363                                 }
364                                 tmp = sc[0]>>6 & 0xff;
365                                 indelQ2 = tmp > 111? 0 : (int)((1. - tmp/111.) * indelQ2 + .499);
366                                 // pick the smaller between indelQ1 and indelQ2
367                                 indelQ = indelQ1 < indelQ2? indelQ1 : indelQ2;
368                                 p->aux = (sc[0]&0x3f)<<16 | seqQ<<8 | indelQ;
369                                 sumq[sc[0]&0x3f] += indelQ < seqQ? indelQ : seqQ;
370 //                              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);
371                         }
372                 }
373                 // determine bca->indel_types[] and bca->inscns
374                 bca->maxins = max_ins;
375                 bca->inscns = realloc(bca->inscns, bca->maxins * 4);
376                 for (t = 0; t < n_types; ++t)
377                         sumq[t] = sumq[t]<<6 | t;
378                 for (t = 1; t < n_types; ++t) // insertion sort
379                         for (j = t; j > 0 && sumq[j] > sumq[j-1]; --j)
380                                 tmp = sumq[j], sumq[j] = sumq[j-1], sumq[j-1] = tmp;
381                 for (t = 0; t < n_types; ++t) // look for the reference type
382                         if ((sumq[t]&0x3f) == ref_type) break;
383                 if (t) { // then move the reference type to the first
384                         tmp = sumq[t];
385                         for (; t > 0; --t) sumq[t] = sumq[t-1];
386                         sumq[0] = tmp;
387                 }
388                 for (t = 0; t < 4; ++t) bca->indel_types[t] = B2B_INDEL_NULL;
389                 for (t = 0; t < 4 && t < n_types; ++t) {
390                         bca->indel_types[t] = types[sumq[t]&0x3f];
391                         memcpy(&bca->inscns[t * bca->maxins], &inscns[(sumq[t]&0x3f) * max_ins], bca->maxins);
392                 }
393                 // update p->aux
394                 for (s = n_alt = 0; s < n; ++s) {
395                         for (i = 0; i < n_plp[s]; ++i) {
396                                 bam_pileup1_t *p = plp[s] + i;
397                                 int x = types[p->aux>>16&0x3f];
398                                 for (j = 0; j < 4; ++j)
399                                         if (x == bca->indel_types[j]) break;
400                                 p->aux = j<<16 | (j == 4? 0 : (p->aux&0xffff));
401                                 if ((p->aux>>16&0x3f) > 0) ++n_alt;
402 //                              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);
403                         }
404                 }               
405         }
406         free(score1); free(score2);
407         // free
408         free(types); free(inscns);
409         return n_alt > 0? 0 : -1;
410 }
411
412 #define END_SKIP 4 // must be larger than B2B_MAX_MNP
413 #define MIN_MNP_FLANK_BAQ 20
414
415 int bcf_call_mnp_prep(int n, int *n_plp, bam_pileup1_t **plp, int pos, bcf_callaux_t *bca, const char *ref)
416 {
417         extern void ks_introsort_uint32_t(int, uint32_t*);
418         int i, s, j, k, t, n_types, *types;
419         int N, ref_seq, ref_type;
420         if (ref == 0 || bca == 0) return -1;
421         if (pos < bca->last_mnp_pos + B2B_MNP_WIN) return -2; // to avoid calling a TNP multiple times
422         if (pos < END_SKIP || ref[pos] == 0 || ref[pos+1] == 0) return -2; // end of the reference
423         { // determine if there is an MNP
424                 int r[2];
425                 uint8_t *tt;
426                 r[0] = bam_nt16_table[(int)ref[pos]];
427                 r[1] = bam_nt16_table[(int)ref[pos+1]];
428                 for (s = 0; s < n; ++s) {
429                         for (i = 0; i < n_plp[s]; ++i) {
430                                 bam_pileup1_t *p = plp[s] + i;
431                                 int left, rght;
432                                 if (p->qpos < END_SKIP || p->qpos >= p->b->core.l_qseq - END_SKIP) continue;
433                                 tt = bam1_seq(p->b);
434                                 if (bam1_seqi(tt, p->qpos) == r[0] || bam1_seqi(tt, p->qpos+1) == r[1]) continue; // no MNP
435                                 tt = bam1_qual(p->b);
436                                 for (left = 0, k = p->qpos - END_SKIP; k <= p->qpos; ++k)
437                                         left = left > tt[k]? left : tt[k];
438                                 for (rght = 0, k = p->qpos; k < p->b->core.l_qseq - END_SKIP; ++k)
439                                         rght = rght > tt[k]? left : tt[k];
440                                 if (left >= MIN_MNP_FLANK_BAQ && rght >= MIN_MNP_FLANK_BAQ) break; // bracketed by good bases
441                         }
442                         if (i != n_plp[s]) break;
443                 }
444                 if (s == n) return -1; // there is no MNP at this position.
445         }
446         for (s = N = 0; s < n; ++s) N += n_plp[s]; // N is the total number of reads
447         { // construct the MNP consensus
448                 uint8_t *tt;
449                 uint32_t *aux, x, *cnt;
450                 int m = 0;
451                 aux = calloc(N + 1, 4);
452                 for (i = 0, x = 0; i < B2B_MAX_MNP; ++i)
453                         x |= bam_nt16_nt4_table[bam_nt16_table[(int)ref[pos+i]]] << 2*i;
454                 ref_seq = x;
455                 bca->indelreg = 0;
456                 for (s = 0; s < n; ++s) {
457                         for (i = 0; i < n_plp[s]; ++i) {
458                                 bam_pileup1_t *p = plp[s] + i;
459                                 int stop;
460                                 if (p->qpos < END_SKIP || p->qpos >= p->b->core.l_qseq - END_SKIP) continue;
461                                 tt = bam1_seq(p->b);
462                                 for (k = j = stop = 0, x = 0; k < B2B_MAX_MNP; ++k) {
463                                         int c = bam_nt16_nt4_table[bam1_seqi(tt, p->qpos + k)];
464                                         if (c > 3) break;
465                                         if (c != (ref_seq>>k*2&3) && !stop) ++j;
466                                         else stop = 1;
467                                         x |= c << 2*k;
468                                 }
469                                 if (k == B2B_MAX_MNP && j >= 2) aux[m++] = x;
470                         }
471                 }
472                 if (m == 0) {
473                         free(aux); return -1;
474                 }
475                 ks_introsort(uint32_t, m, aux);
476                 // squeeze out indentical types
477                 for (i = 1, n_types = 1; i < m; ++i)
478                         if (aux[i] != aux[i-1]) ++n_types;
479                 // count reads for each type
480                 cnt = alloca(n_types * 4);
481                 cnt[0] = 1<<8 | aux[0];
482                 for (i = 1, t = 0; i < m; ++i) {
483                         if (aux[i] != aux[i-1]) {
484                                 ++t;
485                                 cnt[t] = 1<<8 | aux[i];
486                         } else cnt[t] += 1<<8;
487                 }
488                 free(aux);
489                 // collect types; NOTE: we only collect ONE alternative type
490                 ks_introsort(uint32_t, n_types, cnt);
491                 if ((cnt[n_types-1]>>8) * MIN_SUPPORT_COEF < N) // no MNPs or too few supporting reads
492                         return -1;
493                 types = (int*)calloc(2, sizeof(int));
494                 bca->indel_types[0] = types[0] = ref_seq;
495                 bca->indel_types[1] = types[1] = cnt[n_types-1]&0xff;
496                 bca->indel_types[2] = bca->indel_types[3] = B2B_INDEL_NULL;
497                 ref_type = 0; n_types = 2;
498                 // calculate MNP length
499                 for (i = 0; i < B2B_MAX_MNP; ++i) {
500                         int c = types[0] >> 2*i & 3;
501                         for (t = 1; t < n_types; ++t)
502                                 if ((types[t] >> 2*i & 3) != c) break;
503                         if (t == n_types) break;
504                 }
505                 bca->indelreg = i;
506                 if (bca->indelreg < 2) return -1; // should not happen in principle
507                 x = (1<<2*bca->indelreg) - 1;
508                 for (t = 0; t < n_types; ++t) types[t] &= x;
509                 ref_seq &= x;
510         }
511         { // calling
512                 for (s = 0; s < n; ++s) {
513                         for (i = 0; i < n_plp[s]; ++i) {
514                                 bam_pileup1_t *p = plp[s] + i;
515                                 uint8_t *qq, *bq, *seq = bam1_seq(p->b);
516                                 uint32_t x, *cigar = bam1_cigar(p->b);
517                                 int y, skip = 0, baseQ = 0, baq = 0;
518                                 // try to get the original base quality
519                                 qq = bam1_qual(p->b);
520                                 bq = (uint8_t*)bam_aux_get(p->b, "ZQ");
521                                 if (bq) ++bq;
522                                 // get the max original base quality
523                                 for (j = p->qpos, x = 0; j < p->b->core.l_qseq && j < p->qpos + bca->indelreg; ++j) {
524                                         int q = bq? qq[j] + (bq[j] - 64) : qq[j];
525                                         int c = bam_nt16_nt4_table[bam1_seqi(seq, j)];
526                                         baseQ = baseQ > q? baseQ : q;
527                                         if (c > 3) skip = 1;
528                                         x |= c << 2 * (j - p->qpos);
529                                 }
530                                 for (t = 0; t < n_types; ++t)
531                                         if (types[t] == x) break;
532                                 if (t == n_types) skip = 1;
533                                 else { // reapply BAQ
534                                         for (k = y = 0; k < p->b->core.n_cigar; ++k) {
535                                                 int op = cigar[k]&0xf;
536                                                 int len = cigar[k]>>4;
537                                                 if (op == BAM_CMATCH) {
538                                                         if (p->qpos >= y && p->qpos + bca->indelreg <= y + len) {
539                                                                 int left, rght;
540                                                                 for (j = y, left = 0; j <= p->qpos; ++j)
541                                                                         left = left > qq[j]? left : qq[j];
542                                                                 for (j = p->qpos + bca->indelreg - 1, rght = 0; j < y + len; ++j)
543                                                                         rght = rght > qq[j]? rght : qq[j];
544                                                                 baq = left < rght? left : rght;
545                                                                 break;
546                                                         }
547                                                         y += len;
548                                                 } else if (op == BAM_CSOFT_CLIP || op == BAM_CINS) y += len;
549                                         }
550                                 }
551 //                              fprintf(stderr, "pos=%d read=%d:%d name=%s skip=%d reg=%d call=%d baseQ=%d baq=%d\n", pos+1, s, i, bam1_qname(p->b), skip, bca->indelreg, t, baseQ, baq);
552                                 baseQ = baseQ < baq? baseQ : baq;
553                                 p->aux = skip? 0 : (t<<16|baseQ<<8|baseQ);
554                         }
555                 }
556         }
557         // free
558         free(types);
559         return 0;
560 }