X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=bam2bcf.c;h=c2d994479a9094ea330291db34290e15faca8850;hb=0917f66462dc2a0e7be6a33cb4f3e969e3bb1c9b;hp=7fe178a4dd66a780c2f5385620c669b49bcb177d;hpb=6dc53c5ab2964e68e8936850d2339d047d2433ef;p=samtools.git diff --git a/bam2bcf.c b/bam2bcf.c index 7fe178a..c2d9944 100644 --- a/bam2bcf.c +++ b/bam2bcf.c @@ -6,19 +6,13 @@ #include "errmod.h" #include "bcftools/bcf.h" -#define END_DIST_THRES 11 - extern void ks_introsort_uint32_t(size_t n, uint32_t a[]); #define CALL_ETA 0.03f #define CALL_MAX 256 #define CALL_DEFTHETA 0.83f -struct __bcf_callaux_t { - int max_bases, capQ, min_baseQ; - uint16_t *bases; - errmod_t *e; -}; +#define CAP_DIST 25 bcf_callaux_t *bcf_call_init(double theta, int min_baseQ) { @@ -26,6 +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 = 80; bca->min_baseQ = min_baseQ; bca->e = errmod_init(1. - theta); return bca; @@ -35,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, k, 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; @@ -52,33 +50,39 @@ int bcf_call_glfgen(int _n, const bam_pileup1_t *pl, int ref_base /*4-bit*/, bcf bca->bases = (uint16_t*)realloc(bca->bases, 2 * bca->max_bases); } // fill the bases array - memset(r->qsum, 0, 4 * sizeof(float)); - for (i = n = 0, r->sum_Q2 = 0; i < _n; ++i) { + memset(r, 0, sizeof(bcf_callret1_t)); + for (i = n = 0; i < _n; ++i) { const bam_pileup1_t *p = pl + i; - int q, b, mapQ; - int min_dist; + int q, b, mapQ, baseQ, is_diff, min_dist; // set base - if (p->is_del || (p->b->core.flag&BAM_FUNMAP)) continue; // skip unmapped reads and deleted bases - q = (int)bam1_qual(p->b)[p->qpos]; // base quality + if (p->is_del || p->is_refskip || (p->b->core.flag&BAM_FUNMAP)) continue; + 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; - r->sum_Q2 += mapQ * mapQ; 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 other information + // collect annotations r->qsum[b] += q; - k = (ref4 < 4 && b == ref4)? 0 : 1; - k = k<<1 | bam1_strand(p->b); - ++r->d[k]; - // calculate min_dist + ++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; - k = (k&2) | (min_dist <= END_DIST_THRES); - ++r->ed[k]; + if (min_dist > CAP_DIST) min_dist = CAP_DIST; + r->anno[1<<2|is_diff<<1|0] += baseQ; + r->anno[1<<2|is_diff<<1|1] += baseQ * baseQ; + 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 @@ -90,9 +94,11 @@ int bcf_call_combine(int n, const bcf_callret1_t *calls, int ref_base /*4-bit*/, { int ref4, i, j, qsum[4]; int64_t tmp; - call->ori_ref = ref4 = bam_nt16_nt4_table[ref_base]; - if (ref4 > 4) ref4 = 4; - // calculate esum + 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) for (j = 0; j < 4; ++j) @@ -112,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; @@ -142,52 +150,98 @@ 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 - memset(call->d, 0, 4 * sizeof(int)); - memset(call->ed, 0, 4 * sizeof(int)); + memset(call->anno, 0, 16 * sizeof(int)); for (i = call->depth = 0, tmp = 0; i < n; ++i) { call->depth += calls[i].depth; - for (j = 0; j < 4; ++j) call->d[j] += calls[i].d[j], call->ed[j] += calls[i].ed[j]; - tmp += calls[i].sum_Q2; + for (j = 0; j < 16; ++j) call->anno[j] += calls[i].anno[j]; } - call->rmsQ = (int)(sqrt((double)tmp / call->depth) + .499); return 0; } -int bcf_call2bcf(int tid, int pos, bcf_call_t *bc, bcf1_t *b) +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; ++j) + 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 - kputs("MQ=", &s); kputw(bc->rmsQ, &s); - kputs(";DP4=", &s); - for (i = 0; i < 4; ++i) { - if (i) kputc(',', &s); - kputw(bc->d[i], &s); - } - kputs(";ED4=", &s); - for (i = 0; i < 4; ++i) { + if (bc->ori_ref < 0) kputs("INDEL;", &s); + kputs("I16=", &s); + for (i = 0; i < 16; ++i) { if (i) kputc(',', &s); - kputw(bc->ed[i], &s); + kputw(bc->anno[i], &s); } kputc('\0', &s); // FMT - kputs("PL", &s); kputc('\0', &s); + kputs("PL", &s); + if (bcr) { + kputs(":DP", &s); + if (is_SP) kputs(":SP", &s); + } + kputc('\0', &s); b->m_str = s.m; b->str = s.s; b->l_str = s.l; - bcf_sync(bc->n, b); + bcf_sync(b); memcpy(b->gi[0].data, bc->PL, b->gi[0].len * bc->n); + if (bcr) { + uint16_t *dp = (uint16_t*)b->gi[1].data; + uint8_t *sp = is_SP? b->gi[2].data : 0; + for (i = 0; i < bc->n; ++i) { + bcf_callret1_t *p = bcr + i; + dp[i] = p->depth < 0xffff? p->depth : 0xffff; + if (is_SP) { + if (p->anno[0] + p->anno[1] < 2 || p->anno[2] + p->anno[3] < 2 + || p->anno[0] + p->anno[2] < 2 || p->anno[1] + p->anno[3] < 2) + { + sp[i] = 0; + } else { + double left, right, two; + int x; + kt_fisher_exact(p->anno[0], p->anno[1], p->anno[2], p->anno[3], &left, &right, &two); + x = (int)(-4.343 * log(two) + .499); + if (x > 255) x = 255; + sp[i] = x; + } + } + } + } return 0; }