#define BAM_PLF_GLF 0x08
#define BAM_PLF_VAR_ONLY 0x10
#define BAM_PLF_2ND 0x20
+#define BAM_PLF_RANBASE 0x40
+#define BAM_PLF_1STBASE 0x80
+#define BAM_PLF_ALLBASE 0x100
typedef struct {
bam_header_t *h;
g3->offset = pos - d->last_pos;
d->last_pos = pos;
glf3_write1(d->fp_glf, g3);
- if (proposed_indels)
- r = bam_maqindel(n, pos, d->ido, pu, d->ref, proposed_indels[0], proposed_indels+1);
- else r = bam_maqindel(n, pos, d->ido, pu, d->ref, 0, 0);
+ if (pos < d->len) {
+ if (proposed_indels)
+ r = bam_maqindel(n, pos, d->ido, pu, d->ref, proposed_indels[0], proposed_indels+1);
+ else r = bam_maqindel(n, pos, d->ido, pu, d->ref, 0, 0);
+ }
if (r) { // then write indel line
int het = 3 * n, min;
min = het;
// update d->ref if necessary
if (d->fai && (int)tid != d->tid) {
free(d->ref);
- d->ref = fai_fetch(d->fai, d->h->target_name[tid], &d->len);
+ 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';
if (i == n) return 0;
}
// call the consensus and indel
- if (d->format & BAM_PLF_CNS) // call consensus
- cns = bam_maqcns_call(n, pu, d->c);
+ 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
if (proposed_indels) // the first element gives the size of the array
r = bam_maqindel(n, pos, d->ido, pu, d->ref, proposed_indels[0], proposed_indels+1);
d->tid = -1; d->mask = BAM_DEF_MASK;
d->c = bam_maqcns_init();
d->ido = bam_maqindel_opt_init();
- while ((c = getopt(argc, argv, "st:f:cT:N:r:l:im:gI:G:vM:S2a")) >= 0) {
+ while ((c = getopt(argc, argv, "st:f:cT:N:r:l:im:gI:G:vM:S2aR:")) >= 0) {
switch (c) {
case 'a': d->c->is_soap = 1; break;
case 's': d->format |= BAM_PLF_SIMPLE; break;
case 'I': d->ido->q_indel = atoi(optarg); break;
case 'G': d->ido->r_indel = atof(optarg); break;
case 'S': is_SAM = 1; break;
+ case 'R':
+ if (strcmp(optarg, "random") == 0) d->format |= BAM_PLF_RANBASE;
+ else if (strcmp(optarg, "first") == 0) d->format |= BAM_PLF_1STBASE;
+ else if (strcmp(optarg, "all") == 0) d->format |= BAM_PLF_ALLBASE;
+ else fprintf(stderr, "[bam_pileup] unrecognized -R\n");
+ break;
default: fprintf(stderr, "Unrecognizd option '-%c'.\n", c); return 1;
}
}
free(fn_list); free(fn_fa); free(d);
return 1;
}
+ if (d->format & (BAM_PLF_RANBASE|BAM_PLF_1STBASE|BAM_PLF_ALLBASE)) d->format |= BAM_PLF_CNS;
if (fn_fa) d->fai = fai_load(fn_fa);
if (d->format & (BAM_PLF_CNS|BAM_PLF_GLF)) bam_maqcns_prepare(d->c); // consensus calling
if (d->format & BAM_PLF_GLF) { // for glf output