+ g = bam_maqcns_glfgen(n, pu, bam_nt16_table[rb], d->c);
+ memcpy(g3, g, sizeof(glf1_t));
+ g3->rtype = GLF3_RTYPE_SUB;
+ g3->offset = pos - d->last_pos;
+ d->last_pos = pos;
+ glf3_write1(d->fp_glf, g3);
+ if (pos < d->len) {
+ int m = (!d->max_depth || d->max_depth>n) ? n : d->max_depth;
+ if (proposed_indels)
+ r = bam_maqindel(m, pos, d->ido, pu, d->ref, proposed_indels[0], proposed_indels+1);
+ else r = bam_maqindel(m, pos, d->ido, pu, d->ref, 0, 0);
+ }
+ if (r) { // then write indel line
+ int het = 3 * n, min;
+ min = het;
+ if (min > r->gl[0]) min = r->gl[0];
+ if (min > r->gl[1]) min = r->gl[1];
+ g3->ref_base = 0;
+ g3->rtype = GLF3_RTYPE_INDEL;
+ memset(g3->lk, 0, 10);
+ g3->lk[0] = r->gl[0] - min < 255? r->gl[0] - min : 255;
+ g3->lk[1] = r->gl[1] - min < 255? r->gl[1] - min : 255;
+ g3->lk[2] = het - min < 255? het - min : 255;
+ g3->offset = 0;
+ g3->indel_len[0] = r->indel1;
+ g3->indel_len[1] = r->indel2;
+ g3->min_lk = min < 255? min : 255;
+ g3->max_len = (abs(r->indel1) > abs(r->indel2)? abs(r->indel1) : abs(r->indel2)) + 1;
+ g3->indel_seq[0] = strdup(r->s[0]+1);
+ g3->indel_seq[1] = strdup(r->s[1]+1);
+ glf3_write1(d->fp_glf, g3);
+ bam_maqindel_ret_destroy(r);
+ }
+ free(g);
+ glf3_destroy1(g3);
+ return 0;
+}
+
+static void pileup_seq(const bam_pileup1_t *p, int pos, int ref_len, const char *ref)
+{
+ if (p->is_head) printf("^%c", p->b->core.qual > 93? 126 : p->b->core.qual + 33);
+ if (!p->is_del) {
+ int j, rb, c = bam_nt16_rev_table[bam1_seqi(bam1_seq(p->b), p->qpos)];
+ rb = (ref && pos < ref_len)? ref[pos] : 'N';
+ if (c == '=' || toupper(c) == toupper(rb)) c = bam1_strand(p->b)? ',' : '.';
+ else c = bam1_strand(p->b)? tolower(c) : toupper(c);
+ putchar(c);
+ if (p->indel > 0) {
+ printf("+%d", p->indel);
+ for (j = 1; j <= p->indel; ++j) {
+ c = bam_nt16_rev_table[bam1_seqi(bam1_seq(p->b), p->qpos + j)];
+ putchar(bam1_strand(p->b)? tolower(c) : toupper(c));
+ }
+ } else if (p->indel < 0) {
+ printf("%d", p->indel);
+ for (j = 1; j <= -p->indel; ++j) {
+ c = (ref && (int)pos+j < ref_len)? ref[pos+j] : 'N';
+ putchar(bam1_strand(p->b)? tolower(c) : toupper(c));
+ }
+ }
+ } else putchar('*');
+ if (p->is_tail) putchar('$');
+}
+
+static int pileup_func(uint32_t tid, uint32_t pos, int n, const bam_pileup1_t *pu, void *data)
+{
+ pu_data_t *d = (pu_data_t*)data;
+ bam_maqindel_ret_t *r = 0;
+ int i, rb, rms_mapq = -1, *proposed_indels = 0;
+ uint64_t rms_aux;
+ uint32_t cns = 0;
+
+ // if GLF is required, suppress -c completely
+ if (d->format & BAM_PLF_GLF) return glt3_func(tid, pos, n, pu, data);
+ // if d->hash is initialized, only output the sites in the hash table
+ if (d->hash) {
+ khint_t k = kh_get(64, d->hash, (uint64_t)tid<<32|pos);
+ if (k == kh_end(d->hash)) return 0;
+ proposed_indels = kh_val(d->hash, k);
+ }
+ // update d->ref if necessary
+ if (d->fai && (int)tid != d->tid) {
+ free(d->ref);
+ d->ref = faidx_fetch_seq(d->fai, d->h->target_name[tid], 0, 0x7fffffff, &d->len);
+ d->tid = tid;
+ }
+ rb = (d->ref && (int)pos < d->len)? d->ref[pos] : 'N';
+ // when the indel-only mode is asked for, return if no reads mapped with indels
+ if (d->format & BAM_PLF_INDEL_ONLY) {
+ for (i = 0; i < n; ++i)
+ if (pu[i].indel != 0) break;
+ if (i == n) return 0;
+ }
+ // call the consensus and indel
+ if (d->format & BAM_PLF_CNS) { // call consensus
+ if (d->format & (BAM_PLF_RANBASE|BAM_PLF_1STBASE)) { // use a random base or the 1st base as the consensus call
+ const bam_pileup1_t *p = (d->format & BAM_PLF_1STBASE)? pu : pu + (int)(drand48() * n);
+ int q = bam1_qual(p->b)[p->qpos];
+ int mapQ = p->b->core.qual < d->c->cap_mapQ? p->b->core.qual : d->c->cap_mapQ;
+ uint32_t b = bam1_seqi(bam1_seq(p->b), p->qpos);
+ cns = b<<28 | 0xf<<24 | mapQ<<16 | q<<8;
+ } else if (d->format & BAM_PLF_ALLBASE) { // collapse all bases
+ uint64_t rmsQ = 0;
+ uint32_t b = 0;
+ for (i = 0; i < n; ++i) {
+ const bam_pileup1_t *p = pu + i;
+ int q = p->b->core.qual < d->c->cap_mapQ? p->b->core.qual : d->c->cap_mapQ;
+ b |= bam1_seqi(bam1_seq(p->b), p->qpos);
+ rmsQ += q * q;
+ }
+ rmsQ = (uint64_t)(sqrt((double)rmsQ / n) + .499);
+ cns = b<<28 | 0xf<<24 | rmsQ<<16 | 60<<8;
+ } else cns = bam_maqcns_call(n, pu, d->c);
+ }
+ if ((d->format & (BAM_PLF_CNS|BAM_PLF_INDEL_ONLY)) && d->ref && pos < d->len) { // call indels
+ int m = (!d->max_depth || d->max_depth>n) ? n : d->max_depth;
+ if (proposed_indels) // the first element gives the size of the array
+ r = bam_maqindel(m, pos, d->ido, pu, d->ref, proposed_indels[0], proposed_indels+1);
+ else r = bam_maqindel(m, pos, d->ido, pu, d->ref, 0, 0);
+ }
+ // when only variant sites are asked for, test if the site is a variant
+ if ((d->format & BAM_PLF_CNS) && (d->format & BAM_PLF_VAR_ONLY)) {
+ if (!(bam_nt16_table[rb] != 15 && cns>>28 != bam_nt16_table[rb])) { // not a SNP
+ if (!(r && (r->gt == 2 || strcmp(r->s[r->gt], "*")))) { // not an indel
+ if (r) bam_maqindel_ret_destroy(r);
+ return 0;
+ }
+ }
+ }
+ // print the first 3 columns