- // enlarge the bases array if necessary
- if (bca->max_bases < _n) {
- bca->max_bases = _n;
- kroundup32(bca->max_bases);
- bca->bases = (uint16_t*)realloc(bca->bases, 2 * bca->max_bases);
- }
- // fill the bases array
- memset(r, 0, sizeof(bcf_callret1_t));
- r->indelreg = 10000;
- for (i = n = 0; i < _n; ++i) {
- const bam_pileup1_t *p = pl + i;
- int q, b, mapQ, indelQ, is_diff, min_dist;
- if (p->is_del || p->is_refskip || (p->b->core.flag&BAM_FUNMAP)) continue;
- { // compute indel (base) quality
- // this can be made more efficient, but realignment is the bottleneck anyway
- int j, k, x, y, op, len = 0, max_left, max_rght, seqQ, indelreg;
- bam1_core_t *c = &p->b->core;
- uint32_t *cigar = bam1_cigar(p->b);
- uint8_t *qual = bam1_qual(p->b);
- for (k = y = 0, x = c->pos; k < c->n_cigar && y <= p->qpos; ++k) {
- op = cigar[k]&0xf;
- len = cigar[k]>>4;
- if (op == BAM_CMATCH) {
- if (pos > x && pos < x + len) break;
- x += len; y += len;
- } else if (op == BAM_CINS || op == BAM_CSOFT_CLIP) y += len;
- else if (op == BAM_CDEL || op == BAM_CREF_SKIP) x += len;
- }
- if (k == c->n_cigar) continue; // this actually should not happen
- max_left = max_rght = 0; indelreg = 0;
- if (pos == x + len - 1 && k+2 < c->n_cigar && ((cigar[k+1]&0xf) == BAM_CINS || (cigar[k+1]&0xf) == BAM_CDEL)
- && (cigar[k+2]&0xf) == BAM_CMATCH)
- {
- for (j = y; j < y + len; ++j)
- if (max_left < qual[j]) max_left = qual[j];
- if ((cigar[k+1]&0xf) == BAM_CINS) y += cigar[k+1]>>4;
- else x += cigar[k+1]>>4;
- op = cigar[k+2]&0xf; len = cigar[k+2]>>4;
- for (j = y; j < y + len; ++j) {
- if (max_rght < qual[j]) max_rght = qual[j];
- if (qual[j] > BAM2BCF_INDELREG_THRES && indelreg == 0)
- indelreg = j - y + 1;
- }
- } else {
- for (j = y; j <= p->qpos; ++j)
- if (max_left < qual[j]) max_left = qual[j];
- for (j = p->qpos + 1; j < y + len; ++j)
- if (max_rght < qual[j]) max_rght = qual[j];
-
- }
- indelQ = max_left < max_rght? max_left : max_rght;
- // estimate the sequencing error rate
- seqQ = bca->openQ;
- if (p->indel != 0) seqQ += bca->extQ * (abs(p->indel) - 1); // FIXME: better to model homopolymer
- if (p->indel != 0) { // a different model for tandem repeats
- uint8_t *seq = bam1_seq(p->b);
- int tandemQ, qb = bam1_seqi(seq, p->qpos), l;
- for (j = p->qpos + 1; j < c->l_qseq; ++j)
- if (qb != bam1_seqi(seq, j)) break;
- l = j;
- for (j = (int)p->qpos - 1; j >= 0; --j)
- if (qb != bam1_seqi(seq, j)) break;
- l = l - (j + 1);
- tandemQ = (int)((double)(abs(p->indel)) / l * bca->tandemQ + .499);
- if (seqQ > tandemQ) seqQ = tandemQ;
- }
-// fprintf(stderr, "%s\t%d\t%d\t%d\t%d\t%d\t%d\n", bam1_qname(p->b), pos+1, p->indel, indelQ, seqQ, max_left, max_rght);
- if (indelQ > seqQ) indelQ = seqQ;
- q = indelQ;
- }
- if (q < bca->min_baseQ) continue;
- mapQ = p->b->core.qual < bca->capQ? p->b->core.qual : bca->capQ;
- if (q > mapQ) q = mapQ;
- if (q > 63) q = 63;
- if (q < 4) q = 4;
- b = p->indel? 1 : 0;
- bca->bases[n++] = q<<5 | (int)bam1_strand(p->b)<<4 | b;
- // collect annotations
- r->qsum[b] += q;
- is_diff = b;
- ++r->anno[0<<2|is_diff<<1|bam1_strand(p->b)];
- min_dist = p->b->core.l_qseq - 1 - p->qpos;
- if (min_dist > p->qpos) min_dist = p->qpos;
- if (min_dist > CAP_DIST) min_dist = CAP_DIST;
- r->anno[1<<2|is_diff<<1|0] += indelQ;
- r->anno[1<<2|is_diff<<1|1] += indelQ * indelQ;
- r->anno[2<<2|is_diff<<1|0] += mapQ;
- r->anno[2<<2|is_diff<<1|1] += mapQ * mapQ;
- r->anno[3<<2|is_diff<<1|0] += min_dist;
- r->anno[3<<2|is_diff<<1|1] += min_dist * min_dist;
- }
- r->depth = n;
- // glfgen
- errmod_cal(bca->e, n, 2, bca->bases, r->p);
- return r->depth;
+ float weight=0, tot_prob=0;
+
+ int i;
+ for (i=0; i<n; i++)
+ {
+ int mvd = calls[i].mvd[0];
+ int dp = calls[i].mvd[1];
+ int read_len = calls[i].mvd[2];
+
+ if ( dp<2 ) continue;
+
+ float prob = 0;
+ if ( dp==2 )
+ {
+ // Exact formula
+ prob = (mvd==0) ? 1.0/read_len : (read_len-mvd)*2.0/read_len/read_len;
+ }
+ else if ( dp==3 )
+ {
+ // Sin, quite accurate approximation
+ float mu = read_len/2.9;
+ prob = mvd>2*mu ? 0 : sin(mvd*3.14/2/mu) / (4*mu/3.14);
+ }
+ else
+ {
+ // Scaled gaussian curve, crude approximation, but behaves well. Using fixed depth for bigger depths.
+ if ( dp>5 )
+ dp = 5;
+ float sigma2 = (read_len/1.9/(dp+1)) * (read_len/1.9/(dp+1));
+ float norm = 1.125*sqrt(2*3.14*sigma2);
+ float mu = read_len/2.9;
+ if ( mvd < mu )
+ prob = exp(-(mvd-mu)*(mvd-mu)/2/sigma2)/norm;
+ else
+ prob = exp(-(mvd-mu)*(mvd-mu)/3.125/sigma2)/norm;
+ }
+
+ //fprintf(stderr,"dp=%d mvd=%d read_len=%d -> prob=%f\n", dp,mvd,read_len,prob);
+ tot_prob += prob*dp;
+ weight += dp;
+ }
+ tot_prob = weight ? tot_prob/weight : 1;
+ //fprintf(stderr,"prob=%f\n", tot_prob);
+ call->vdb = tot_prob;