X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=bam2bcf.c;h=be8cf1fe529a735800947b80b2a52c7fbaadb2d4;hb=599bea10bc3487a0cbdf505e792a21a13fee2b11;hp=d5a8dd440286cc2b6d0b119df6e6940d8ea05bbf;hpb=98946f7c4842bc7c0429bb78982366ce46c70624;p=samtools.git diff --git a/bam2bcf.c b/bam2bcf.c index d5a8dd4..be8cf1f 100644 --- a/bam2bcf.c +++ b/bam2bcf.c @@ -3,6 +3,7 @@ #include "bam.h" #include "kstring.h" #include "bam2bcf.h" +#include "errmod.h" #include "bcftools/bcf.h" extern void ks_introsort_uint32_t(size_t n, uint32_t a[]); @@ -19,9 +20,7 @@ bcf_callaux_t *bcf_call_init(double theta, int min_baseQ) if (theta <= 0.) theta = CALL_DEFTHETA; bca = calloc(1, sizeof(bcf_callaux_t)); bca->capQ = 60; - bca->openQ = 40; - bca->extQ = 20; - bca->tandemQ = 100; + bca->openQ = 40; bca->extQ = 20; bca->tandemQ = 80; bca->min_baseQ = min_baseQ; bca->e = errmod_init(1. - theta); return bca; @@ -31,16 +30,19 @@ void bcf_call_destroy(bcf_callaux_t *bca) { if (bca == 0) return; errmod_destroy(bca->e); - free(bca->bases); free(bca); + free(bca->bases); free(bca->inscns); free(bca); } - -int bcf_call_glfgen(int _n, const bam_pileup1_t *pl, int ref_base /*4-bit*/, bcf_callaux_t *bca, bcf_callret1_t *r) +/* ref_base is the 4-bit representation of the reference base. It is + * negative if we are looking at an indel. */ +int bcf_call_glfgen(int _n, const bam_pileup1_t *pl, int ref_base, bcf_callaux_t *bca, bcf_callret1_t *r) { - int i, n, ref4; + int i, n, ref4, is_indel; memset(r, 0, sizeof(bcf_callret1_t)); - ref4 = bam_nt16_nt4_table[ref_base]; + if (ref_base >= 0) { + ref4 = bam_nt16_nt4_table[ref_base]; + is_indel = 0; + } else ref4 = 4, is_indel = 1; if (_n == 0) return -1; - // enlarge the bases array if necessary if (bca->max_bases < _n) { bca->max_bases = _n; @@ -54,18 +56,23 @@ int bcf_call_glfgen(int _n, const bam_pileup1_t *pl, int ref_base /*4-bit*/, bcf int q, b, mapQ, baseQ, is_diff, min_dist; // set base if (p->is_del || p->is_refskip || (p->b->core.flag&BAM_FUNMAP)) continue; - baseQ = q = (int)bam1_qual(p->b)[p->qpos]; // base quality + baseQ = q = is_indel? p->aux&0xff : (int)bam1_qual(p->b)[p->qpos]; // base/indel quality 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 = bam1_seqi(bam1_seq(p->b), p->qpos); // base - b = bam_nt16_nt4_table[b? b : ref_base]; // b is the 2-bit base + if (!is_indel) { + b = bam1_seqi(bam1_seq(p->b), p->qpos); // base + b = bam_nt16_nt4_table[b? b : ref_base]; // b is the 2-bit base + is_diff = (ref4 < 4 && b == ref4)? 0 : 1; + } else { + b = p->aux>>8&0x3f; + is_diff = (b != 0); + } bca->bases[n++] = q<<5 | (int)bam1_strand(p->b)<<4 | b; // collect annotations r->qsum[b] += q; - is_diff = (ref4 < 4 && b == ref4)? 0 : 1; ++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; @@ -83,115 +90,14 @@ int bcf_call_glfgen(int _n, const bam_pileup1_t *pl, int ref_base /*4-bit*/, bcf return r->depth; } -int bcf_call_glfgen_gap(int pos, int _n, const bam_pileup1_t *pl, bcf_callaux_t *bca, bcf_callret1_t *r) -{ - int i, n, n_ins, n_del; - memset(r, 0, sizeof(bcf_callret1_t)); - if (_n == 0) return -1; - - // 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; -} - int bcf_call_combine(int n, const bcf_callret1_t *calls, int ref_base /*4-bit*/, bcf_call_t *call) { int ref4, i, j, qsum[4]; int64_t tmp; - call->ori_ref = ref4 = bam_nt16_nt4_table[ref_base]; - if (ref4 > 4) ref4 = 4; + if (ref_base >= 0) { + call->ori_ref = ref4 = bam_nt16_nt4_table[ref_base]; + if (ref4 > 4) ref4 = 4; + } else call->ori_ref = -1, ref4 = 0; // calculate qsum memset(qsum, 0, 4 * sizeof(int)); for (i = 0; i < n; ++i) @@ -212,9 +118,11 @@ int bcf_call_combine(int n, const bcf_callret1_t *calls, int ref_base /*4-bit*/, else break; } } - if (((ref4 < 4 && j < 4) || (ref4 == 4 && j < 5)) && i >= 0) - call->unseen = j, call->a[j++] = qsum[i]&3; - call->n_alleles = j; + if (ref_base >= 0) { // for SNPs, find the "unseen" base + if (((ref4 < 4 && j < 4) || (ref4 == 4 && j < 5)) && i >= 0) + call->unseen = j, call->a[j++] = qsum[i]&3; + call->n_alleles = j; + } else call->n_alleles = j; // set the PL array if (call->n < n) { call->n = n; @@ -242,6 +150,7 @@ int bcf_call_combine(int n, const bcf_callret1_t *calls, int ref_base /*4-bit*/, PL[j] = y; } } +// if (ref_base < 0) fprintf(stderr, "%d,%d,%f,%d\n", call->n_alleles, x, sum_min, call->unseen); call->shift = (int)(sum_min + .499); } // combine annotations @@ -253,24 +162,49 @@ int bcf_call_combine(int n, const bcf_callret1_t *calls, int ref_base /*4-bit*/, return 0; } -int bcf_call2bcf(int tid, int pos, bcf_call_t *bc, bcf1_t *b, bcf_callret1_t *bcr, int is_SP) +int bcf_call2bcf(int tid, int pos, bcf_call_t *bc, bcf1_t *b, bcf_callret1_t *bcr, int is_SP, + const bcf_callaux_t *bca, const char *ref) { extern double kt_fisher_exact(int n11, int n12, int n21, int n22, double *_left, double *_right, double *two); kstring_t s; - int i; + int i, j; b->n_smpl = bc->n; b->tid = tid; b->pos = pos; b->qual = 0; s.s = b->str; s.m = b->m_str; s.l = 0; kputc('\0', &s); - kputc("ACGTN"[bc->ori_ref], &s); kputc('\0', &s); - for (i = 1; i < 5; ++i) { - if (bc->a[i] < 0) break; - if (i > 1) kputc(',', &s); - kputc(bc->unseen == i? 'X' : "ACGT"[bc->a[i]], &s); + if (bc->ori_ref < 0) { // an indel + // write REF + kputc(ref[pos], &s); + for (j = 0; j < bca->indelreg; ++j) kputc(ref[pos+1+j], &s); + kputc('\0', &s); + // write ALT + kputc(ref[pos], &s); + for (i = 1; i < 4; ++i) { + if (bc->a[i] < 0) break; + if (i > 1) kputc(',', &s); + if (bca->indel_types[bc->a[i]] < 0) { // deletion + for (j = -bca->indel_types[bc->a[i]]; j < bca->indelreg; ++i) + kputc(ref[pos+1+j], &s); + } else { // insertion; cannot be a reference unless a bug + char *inscns = &bca->inscns[bc->a[i] * bca->maxins]; + for (j = 0; j < bca->indel_types[bc->a[i]]; ++j) + kputc("ACGTN"[(int)inscns[j]], &s); + for (j = 0; j < bca->indelreg; ++j) kputc(ref[pos+1+j], &s); + } + } + kputc('\0', &s); + } else { // a SNP + kputc("ACGTN"[bc->ori_ref], &s); kputc('\0', &s); + for (i = 1; i < 5; ++i) { + if (bc->a[i] < 0) break; + if (i > 1) kputc(',', &s); + kputc(bc->unseen == i? 'X' : "ACGT"[bc->a[i]], &s); + } + kputc('\0', &s); } kputc('\0', &s); - kputc('\0', &s); // INFO + if (bc->ori_ref < 0) kputs("INDEL;", &s); kputs("I16=", &s); for (i = 0; i < 16; ++i) { if (i) kputc(',', &s);