Imported Upstream version 0.5
[pysam.git] / samtools / bam2bcf_indel.c.pysam.c
1 #include "pysam.h"
2
3 #include <assert.h>
4 #include <ctype.h>
5 #include <string.h>
6 #include "bam.h"
7 #include "bam2bcf.h"
8 #include "ksort.h"
9 #include "kaln.h"
10 #include "kprobaln.h"
11 #include "khash.h"
12 KHASH_SET_INIT_STR(rg)
13
14 #define MINUS_CONST 0x10000000
15 #define INDEL_WINDOW_SIZE 50
16
17 void *bcf_call_add_rg(void *_hash, const char *hdtext, const char *list)
18 {
19         const char *s, *p, *q, *r, *t;
20         khash_t(rg) *hash;
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;
25         do {
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
30                         int lp, lq;
31                         char *x;
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
37                                 khint_t k;
38                                 int ret;
39                                 for (r = p; *r && *r != '\t' && *r != '\n'; ++r) x[r-p] = *r;
40                                 x[r-p] = 0;
41                                 k = kh_get(rg, hash, x);
42                                 if (k == kh_end(hash)) k = kh_put(rg, hash, x, &ret);
43                                 else free(x);
44                         } else free(x);
45                 }
46                 s = t;
47         } while (s);
48         return hash;
49 }
50
51 void bcf_call_del_rghash(void *_hash)
52 {
53         khint_t k;
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));
59         kh_destroy(rg, hash);
60 }
61
62 static int tpos2qpos(const bam1_core_t *c, const uint32_t *cigar, int32_t tpos, int is_left, int32_t *_tpos)
63 {
64         int k, x = c->pos, y = 0, last_y = 0;
65         *_tpos = c->pos;
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;
71                         if (x + l > tpos) {
72                                 *_tpos = tpos;
73                                 return y + (tpos - x);
74                         }
75                         x += l; y += l;
76                         last_y = y;
77                 } else if (op == BAM_CINS || op == BAM_CSOFT_CLIP) y += l;
78                 else if (op == BAM_CDEL || op == BAM_CREF_SKIP) {
79                         if (x + l > tpos) {
80                                 *_tpos = is_left? x : x + l;
81                                 return y;
82                         }
83                         x += l;
84                 }
85         }
86         *_tpos = x;
87         return last_y;
88 }
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)
92 {
93         int q, qh;
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;
97 }
98
99 static inline int est_indelreg(int pos, const char *ref, int l, char *ins4)
100 {
101         int i, j, max = 0, max_i = pos, score = 0;
102         l = abs(l);
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;
108         }
109         return max_i - pos;
110 }
111
112 int bcf_call_gap_prep(int n, int *n_plp, bam_pileup1_t **plp, int pos, bcf_callaux_t *bca, const char *ref,
113                                           const void *rghash)
114 {
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, max_ref2;
117         int N, K, l_run, ref_type, n_alt;
118         char *inscns = 0, *ref2, *query, **ref_sample;
119         khash_t(rg) *hash = (khash_t(rg)*)rghash;
120         if (ref == 0 || bca == 0) return -1;
121         // mark filtered reads
122         if (rghash) {
123                 N = 0;
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
129                                 if (rg) {
130                                         khint_t k = kh_get(rg, hash, (const char*)(rg + 1));
131                                         if (k != kh_end(hash)) p->aux = 0, ++N; // not filtered
132                                 }
133                         }
134                 }
135                 if (N == 0) return -1; // no reads left
136         }
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;
142         }
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;
147                 uint32_t *aux;
148                 aux = calloc(N + 1, 4);
149                 m = max_rd_len = 0;
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) {
155                                         ++n_tot;
156                                         if (p->indel != 0) {
157                                                 ++n_alt;
158                                                 aux[m++] = MINUS_CONST + p->indel;
159                                         }
160                                 }
161                                 j = bam_cigar2qlen(&p->b->core, bam1_cigar(p->b));
162                                 if (j > max_rd_len) max_rd_len = j;
163                         }
164                 }
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 || (double)n_alt / n_tot < bca->min_frac || n_alt < bca->min_support) { // then skip
170                         free(aux); return -1;
171                 }
172                 types = (int*)calloc(n_types, sizeof(int));
173                 t = 0;
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;
178                 free(aux);
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);
183         }
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;
191                 right = i;
192         }
193         /* The following block fixes a long-existing flaw in the INDEL
194          * calling model: the interference of nearby SNPs. However, it also
195          * reduces the power because sometimes, substitutions caused by
196          * indels are not distinguishable from true mutations. Multiple
197          * sequence realignment helps to increase the power.
198          */
199         { // construct per-sample consensus
200                 int L = right - left + 1, max_i, max2_i;
201                 uint32_t *cns, max, max2;
202                 char *ref0, *r;
203                 ref_sample = calloc(n, sizeof(void*));
204                 cns = calloc(L, 4);
205                 ref0 = calloc(L, 1);
206                 for (i = 0; i < right - left; ++i)
207                         ref0[i] = bam_nt16_table[(int)ref[i+left]];
208                 for (s = 0; s < n; ++s) {
209                         r = ref_sample[s] = calloc(L, 1);
210                         memset(cns, 0, sizeof(int) * L);
211                         // collect ref and non-ref counts
212                         for (i = 0; i < n_plp[s]; ++i) {
213                                 bam_pileup1_t *p = plp[s] + i;
214                                 bam1_t *b = p->b;
215                                 uint32_t *cigar = bam1_cigar(b);
216                                 uint8_t *seq = bam1_seq(b);
217                                 int x = b->core.pos, y = 0;
218                                 for (k = 0; k < b->core.n_cigar; ++k) {
219                                         int op = cigar[k]&0xf;
220                                         int j, l = cigar[k]>>4;
221                                         if (op == BAM_CMATCH) {
222                                                 for (j = 0; j < l; ++j)
223                                                         if (x + j >= left && x + j < right)
224                                                                 cns[x+j-left] += (bam1_seqi(seq, y+j) == ref0[x+j-left])? 1 : 0x10000;
225                                                 x += l; y += l;
226                                         } else if (op == BAM_CDEL || op == BAM_CREF_SKIP) x += l;
227                                         else if (op == BAM_CINS || op == BAM_CSOFT_CLIP) y += l;
228                                 }
229                         }
230                         // determine the consensus
231                         for (i = 0; i < right - left; ++i) r[i] = ref0[i];
232                         max = max2 = 0; max_i = max2_i = -1;
233                         for (i = 0; i < right - left; ++i) {
234                                 if (cns[i]>>16 >= max>>16) max2 = max, max2_i = max_i, max = cns[i], max_i = i;
235                                 else if (cns[i]>>16 >= max2>>16) max2 = cns[i], max2_i = i;
236                         }
237                         if ((double)(max&0xffff) / ((max&0xffff) + (max>>16)) >= 0.7) max_i = -1;
238                         if ((double)(max2&0xffff) / ((max2&0xffff) + (max2>>16)) >= 0.7) max2_i = -1;
239                         if (max_i >= 0) r[max_i] = 15;
240                         if (max2_i >= 0) r[max2_i] = 15;
241 //                      for (i = 0; i < right - left; ++i) fputc("=ACMGRSVTWYHKDBN"[(int)r[i]], pysamerr); fputc('\n', pysamerr);
242                 }
243                 free(ref0); free(cns);
244         }
245         { // the length of the homopolymer run around the current position
246                 int c = bam_nt16_table[(int)ref[pos + 1]];
247                 if (c == 15) l_run = 1;
248                 else {
249                         for (i = pos + 2; ref[i]; ++i)
250                                 if (bam_nt16_table[(int)ref[i]] != c) break;
251                         l_run = i;
252                         for (i = pos; i >= 0; --i)
253                                 if (bam_nt16_table[(int)ref[i]] != c) break;
254                         l_run -= i + 1;
255                 }
256         }
257         // construct the consensus sequence
258         max_ins = types[n_types - 1]; // max_ins is at least 0
259         if (max_ins > 0) {
260                 int *inscns_aux = calloc(4 * n_types * max_ins, sizeof(int));
261                 // count the number of occurrences of each base at each position for each type of insertion
262                 for (t = 0; t < n_types; ++t) {
263                         if (types[t] > 0) {
264                                 for (s = 0; s < n; ++s) {
265                                         for (i = 0; i < n_plp[s]; ++i) {
266                                                 bam_pileup1_t *p = plp[s] + i;
267                                                 if (p->indel == types[t]) {
268                                                         uint8_t *seq = bam1_seq(p->b);
269                                                         for (k = 1; k <= p->indel; ++k) {
270                                                                 int c = bam_nt16_nt4_table[bam1_seqi(seq, p->qpos + k)];
271                                                                 if (c < 4) ++inscns_aux[(t*max_ins+(k-1))*4 + c];
272                                                         }
273                                                 }
274                                         }
275                                 }
276                         }
277                 }
278                 // use the majority rule to construct the consensus
279                 inscns = calloc(n_types * max_ins, 1);
280                 for (t = 0; t < n_types; ++t) {
281                         for (j = 0; j < types[t]; ++j) {
282                                 int max = 0, max_k = -1, *ia = &inscns_aux[(t*max_ins+j)*4];
283                                 for (k = 0; k < 4; ++k)
284                                         if (ia[k] > max)
285                                                 max = ia[k], max_k = k;
286                                 inscns[t*max_ins + j] = max? max_k : 4;
287                         }
288                 }
289                 free(inscns_aux);
290         }
291         // compute the likelihood given each type of indel for each read
292         max_ref2 = right - left + 2 + 2 * (max_ins > -types[0]? max_ins : -types[0]);
293         ref2  = calloc(max_ref2, 1);
294         query = calloc(right - left + max_rd_len + max_ins + 2, 1);
295         score1 = calloc(N * n_types, sizeof(int));
296         score2 = calloc(N * n_types, sizeof(int));
297         bca->indelreg = 0;
298         for (t = 0; t < n_types; ++t) {
299                 int l, ir;
300                 kpa_par_t apf1 = { 1e-4, 1e-2, 10 }, apf2 = { 1e-6, 1e-3, 10 };
301                 apf1.bw = apf2.bw = abs(types[t]) + 3;
302                 // compute indelreg
303                 if (types[t] == 0) ir = 0;
304                 else if (types[t] > 0) ir = est_indelreg(pos, ref, types[t], &inscns[t*max_ins]);
305                 else ir = est_indelreg(pos, ref, -types[t], 0);
306                 if (ir > bca->indelreg) bca->indelreg = ir;
307 //              fprintf(pysamerr, "%d, %d, %d\n", pos, types[t], ir);
308                 // realignment
309                 for (s = K = 0; s < n; ++s) {
310                         // write ref2
311                         for (k = 0, j = left; j <= pos; ++j)
312                                 ref2[k++] = bam_nt16_nt4_table[(int)ref_sample[s][j-left]];
313                         if (types[t] <= 0) j += -types[t];
314                         else for (l = 0; l < types[t]; ++l)
315                                          ref2[k++] = inscns[t*max_ins + l];
316                         for (; j < right && ref[j]; ++j)
317                                 ref2[k++] = bam_nt16_nt4_table[(int)ref_sample[s][j-left]];
318                         for (; k < max_ref2; ++k) ref2[k] = 4;
319                         if (j < right) right = j;
320                         // align each read to ref2
321                         for (i = 0; i < n_plp[s]; ++i, ++K) {
322                                 bam_pileup1_t *p = plp[s] + i;
323                                 int qbeg, qend, tbeg, tend, sc, kk;
324                                 uint8_t *seq = bam1_seq(p->b);
325                                 uint32_t *cigar = bam1_cigar(p->b);
326                                 if (p->b->core.flag&4) continue; // unmapped reads
327                                 // FIXME: the following loop should be better moved outside; nonetheless, realignment should be much slower anyway.
328                                 for (kk = 0; kk < p->b->core.n_cigar; ++kk)
329                                         if ((cigar[kk]&BAM_CIGAR_MASK) == BAM_CREF_SKIP) break;
330                                 if (kk < p->b->core.n_cigar) continue;
331                                 // FIXME: the following skips soft clips, but using them may be more sensitive.
332                                 // determine the start and end of sequences for alignment
333                                 qbeg = tpos2qpos(&p->b->core, bam1_cigar(p->b), left,  0, &tbeg);
334                                 qend = tpos2qpos(&p->b->core, bam1_cigar(p->b), right, 1, &tend);
335                                 if (types[t] < 0) {
336                                         int l = -types[t];
337                                         tbeg = tbeg - l > left?  tbeg - l : left;
338                                 }
339                                 // write the query sequence
340                                 for (l = qbeg; l < qend; ++l)
341                                         query[l - qbeg] = bam_nt16_nt4_table[bam1_seqi(seq, l)];
342                                 { // do realignment; this is the bottleneck
343                                         const uint8_t *qual = bam1_qual(p->b), *bq;
344                                         uint8_t *qq;
345                                         qq = calloc(qend - qbeg, 1);
346                                         bq = (uint8_t*)bam_aux_get(p->b, "ZQ");
347                                         if (bq) ++bq; // skip type
348                                         for (l = qbeg; l < qend; ++l) {
349                                                 qq[l - qbeg] = bq? qual[l] + (bq[l] - 64) : qual[l];
350                                                 if (qq[l - qbeg] > 30) qq[l - qbeg] = 30;
351                                                 if (qq[l - qbeg] < 7) qq[l - qbeg] = 7;
352                                         }
353                                         sc = kpa_glocal((uint8_t*)ref2 + tbeg - left, tend - tbeg + abs(types[t]),
354                                                                         (uint8_t*)query, qend - qbeg, qq, &apf1, 0, 0);
355                                         l = (int)(100. * sc / (qend - qbeg) + .499); // used for adjusting indelQ below
356                                         if (l > 255) l = 255;
357                                         score1[K*n_types + t] = score2[K*n_types + t] = sc<<8 | l;
358                                         if (sc > 5) {
359                                                 sc = kpa_glocal((uint8_t*)ref2 + tbeg - left, tend - tbeg + abs(types[t]),
360                                                                                 (uint8_t*)query, qend - qbeg, qq, &apf2, 0, 0);
361                                                 l = (int)(100. * sc / (qend - qbeg) + .499);
362                                                 if (l > 255) l = 255;
363                                                 score2[K*n_types + t] = sc<<8 | l;
364                                         }
365                                         free(qq);
366                                 }
367 /*
368                                 for (l = 0; l < tend - tbeg + abs(types[t]); ++l)
369                                         fputc("ACGTN"[(int)ref2[tbeg-left+l]], pysamerr);
370                                 fputc('\n', pysamerr);
371                                 for (l = 0; l < qend - qbeg; ++l) fputc("ACGTN"[(int)query[l]], pysamerr);
372                                 fputc('\n', pysamerr);
373                                 fprintf(pysamerr, "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);
374 */
375                         }
376                 }
377         }
378         free(ref2); free(query);
379         { // compute indelQ
380                 int *sc, tmp, *sumq;
381                 sc   = alloca(n_types * sizeof(int));
382                 sumq = alloca(n_types * sizeof(int));
383                 memset(sumq, 0, sizeof(int) * n_types);
384                 for (s = K = 0; s < n; ++s) {
385                         for (i = 0; i < n_plp[s]; ++i, ++K) {
386                                 bam_pileup1_t *p = plp[s] + i;
387                                 int *sct = &score1[K*n_types], indelQ1, indelQ2, seqQ, indelQ;
388                                 for (t = 0; t < n_types; ++t) sc[t] = sct[t]<<6 | t;
389                                 for (t = 1; t < n_types; ++t) // insertion sort
390                                         for (j = t; j > 0 && sc[j] < sc[j-1]; --j)
391                                                 tmp = sc[j], sc[j] = sc[j-1], sc[j-1] = tmp;
392                                 /* errmod_cal() assumes that if the call is wrong, the
393                                  * likelihoods of other events are equal. This is about
394                                  * right for substitutions, but is not desired for
395                                  * indels. To reuse errmod_cal(), I have to make
396                                  * compromise for multi-allelic indels.
397                                  */
398                                 if ((sc[0]&0x3f) == ref_type) {
399                                         indelQ1 = (sc[1]>>14) - (sc[0]>>14);
400                                         seqQ = est_seqQ(bca, types[sc[1]&0x3f], l_run);
401                                 } else {
402                                         for (t = 0; t < n_types; ++t) // look for the reference type
403                                                 if ((sc[t]&0x3f) == ref_type) break;
404                                         indelQ1 = (sc[t]>>14) - (sc[0]>>14);
405                                         seqQ = est_seqQ(bca, types[sc[0]&0x3f], l_run);
406                                 }
407                                 tmp = sc[0]>>6 & 0xff;
408                                 indelQ1 = tmp > 111? 0 : (int)((1. - tmp/111.) * indelQ1 + .499); // reduce indelQ
409                                 sct = &score2[K*n_types];
410                                 for (t = 0; t < n_types; ++t) sc[t] = sct[t]<<6 | t;
411                                 for (t = 1; t < n_types; ++t) // insertion sort
412                                         for (j = t; j > 0 && sc[j] < sc[j-1]; --j)
413                                                 tmp = sc[j], sc[j] = sc[j-1], sc[j-1] = tmp;
414                                 if ((sc[0]&0x3f) == ref_type) {
415                                         indelQ2 = (sc[1]>>14) - (sc[0]>>14);
416                                 } else {
417                                         for (t = 0; t < n_types; ++t) // look for the reference type
418                                                 if ((sc[t]&0x3f) == ref_type) break;
419                                         indelQ2 = (sc[t]>>14) - (sc[0]>>14);
420                                 }
421                                 tmp = sc[0]>>6 & 0xff;
422                                 indelQ2 = tmp > 111? 0 : (int)((1. - tmp/111.) * indelQ2 + .499);
423                                 // pick the smaller between indelQ1 and indelQ2
424                                 indelQ = indelQ1 < indelQ2? indelQ1 : indelQ2;
425                                 if (indelQ > 255) indelQ = 255;
426                                 if (seqQ > 255) seqQ = 255;
427                                 p->aux = (sc[0]&0x3f)<<16 | seqQ<<8 | indelQ; // use 22 bits in total
428                                 sumq[sc[0]&0x3f] += indelQ < seqQ? indelQ : seqQ;
429 //                              fprintf(pysamerr, "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);
430                         }
431                 }
432                 // determine bca->indel_types[] and bca->inscns
433                 bca->maxins = max_ins;
434                 bca->inscns = realloc(bca->inscns, bca->maxins * 4);
435                 for (t = 0; t < n_types; ++t)
436                         sumq[t] = sumq[t]<<6 | t;
437                 for (t = 1; t < n_types; ++t) // insertion sort
438                         for (j = t; j > 0 && sumq[j] > sumq[j-1]; --j)
439                                 tmp = sumq[j], sumq[j] = sumq[j-1], sumq[j-1] = tmp;
440                 for (t = 0; t < n_types; ++t) // look for the reference type
441                         if ((sumq[t]&0x3f) == ref_type) break;
442                 if (t) { // then move the reference type to the first
443                         tmp = sumq[t];
444                         for (; t > 0; --t) sumq[t] = sumq[t-1];
445                         sumq[0] = tmp;
446                 }
447                 for (t = 0; t < 4; ++t) bca->indel_types[t] = B2B_INDEL_NULL;
448                 for (t = 0; t < 4 && t < n_types; ++t) {
449                         bca->indel_types[t] = types[sumq[t]&0x3f];
450                         memcpy(&bca->inscns[t * bca->maxins], &inscns[(sumq[t]&0x3f) * max_ins], bca->maxins);
451                 }
452                 // update p->aux
453                 for (s = n_alt = 0; s < n; ++s) {
454                         for (i = 0; i < n_plp[s]; ++i) {
455                                 bam_pileup1_t *p = plp[s] + i;
456                                 int x = types[p->aux>>16&0x3f];
457                                 for (j = 0; j < 4; ++j)
458                                         if (x == bca->indel_types[j]) break;
459                                 p->aux = j<<16 | (j == 4? 0 : (p->aux&0xffff));
460                                 if ((p->aux>>16&0x3f) > 0) ++n_alt;
461 //                              fprintf(pysamerr, "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);
462                         }
463                 }               
464         }
465         free(score1); free(score2);
466         // free
467         for (i = 0; i < n; ++i) free(ref_sample[i]);
468         free(ref_sample);
469         free(types); free(inscns);
470         return n_alt > 0? 0 : -1;
471 }