X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=bam_md.c;h=20b1913efa09ce796392885b0f4b831c8c759f4a;hb=7251efa37992752a8cf62ff363da0ad5099937bd;hp=d2c84dcccd084c3399bf94ac6085af8902eb8532;hpb=6cda28b3a4c6afcf8c739a478b6bcf1e514f96e9;p=samtools.git diff --git a/bam_md.c b/bam_md.c index d2c84dc..20b1913 100644 --- a/bam_md.c +++ b/bam_md.c @@ -7,6 +7,9 @@ #include "sam.h" #include "kstring.h" #include "kaln.h" +#include "kprobaln.h" + +char bam_nt16_nt4_table[] = { 4, 0, 1, 4, 2, 4, 4, 4, 3, 4, 4, 4, 4, 4, 4, 4 }; void bam_fillmd1_core(bam1_t *b, char *ref, int is_equal, int max_nm) { @@ -161,118 +164,132 @@ int bam_cap_mapQ(bam1_t *b, char *ref, int thres) return (int)(t + .499); } -// local realignment - -#define MIN_REF_LEN 10 -#define MIN_BAND_WIDTH 11 - -int bam_realn(bam1_t *b, const char *ref) +int bam_prob_realn_core(bam1_t *b, const char *ref, int flag) { - int k, score, q[2], r[2], kk[2], kl[2], x, y, max, j, n_cigar, endx = -1; + int k, i, bw, x, y, yb, ye, xb, xe, apply_baq = flag&1, extend_baq = flag>>1&1; uint32_t *cigar = bam1_cigar(b); - uint8_t *seq = bam1_seq(b); bam1_core_t *c = &b->core; - q[0] = q[1] = r[0] = r[1] = kk[0] = kk[1] = kl[0] = kl[1] = -1; - // find the right boundary - for (k = 0, score = max = 0, x = c->pos, y = 0; k < c->n_cigar; ++k) { - int op = cigar[k]&0xf; - int ol = cigar[k]>>4; - if (op == BAM_CMATCH) { - for (j = 0; j < ol; ++j) { - int c1, c2, z = y + j; - c1 = bam_nt16_nt4_table[bam1_seqi(seq, z)]; - if (ref[x+j] == 0) return -1; - c2 = bam_nt16_nt4_table[(int)bam_nt16_table[(int)ref[x+j]]]; - if (c1 < 3 && c2 < 3) - score += c1 == c2? 5 : -4; - if (score < 0) score = 0; - if (score > max) max = score, q[1] = z, r[1] = x+j, kk[1] = k, kl[1] = j + 1; - } - x += ol; y += ol; - } else if (op == BAM_CINS) { - score -= 4 - ol * 3; - y += ol; - if (score < 0) score = 0; - } else if (op == BAM_CDEL) { - score -= 4 - ol * 3; - x += ol; - if (score < 0) score = 0; - } else if (op == BAM_CSOFT_CLIP) y += ol; - else if (op == BAM_CREF_SKIP) x += ol; - } - if (score < 0) return -1; // no high scoring segments - endx = x - 1; - // find the left boundary - for (k = c->n_cigar - 1, score = max = 0, x = x-1, y = y-1; k >= 0; --k) { - int op = cigar[k]&0xf; - int ol = cigar[k]>>4; - if (op == BAM_CMATCH) { - for (j = 0; j < ol; ++j) { - int c1, c2, z = y - j; - c1 = bam_nt16_nt4_table[bam1_seqi(seq, z)]; - if (ref[x+j] == 0) return -1; - c2 = bam_nt16_nt4_table[(int)bam_nt16_table[(int)ref[x-j]]]; - if (c1 < 3 && c2 < 3) - score += c1 == c2? 5 : -4; - if (score < 0) score = 0; - if (score > max) max = score, q[0] = z, r[0] = x-j, kk[0] = k, kl[0] = j + 1; - } - x -= ol; y -= ol; - } else if (op == BAM_CINS) { - score -= 4 - ol * 3; - y -= ol; - if (score < 0) score = 0; - } else if (op == BAM_CDEL) { - score -= 4 - ol * 3; - x -= ol; - if (score < 0) score = 0; - } else if (op == BAM_CSOFT_CLIP) y -= ol; - else if (op == BAM_CREF_SKIP) x -= ol; + kpa_par_t conf = kpa_par_def; + uint8_t *bq = 0, *zq = 0, *qual = bam1_qual(b); + if ((c->flag & BAM_FUNMAP) || b->core.l_qseq == 0) return -1; // do nothing + // test if BQ or ZQ is present + if ((bq = bam_aux_get(b, "BQ")) != 0) ++bq; + if ((zq = bam_aux_get(b, "ZQ")) != 0 && *zq == 'Z') ++zq; + if (bq && zq) { // remove the ZQ tag + bam_aux_del(b, zq-1); + zq = 0; } - if (q[1] - q[0] < 15) return -1; // the high-scoring segment is too short - // modify CIGAR - n_cigar = 0; - cigar = calloc(c->n_cigar + 4, 4); - if (q[0] != 0) cigar[n_cigar++] = (uint32_t)q[0]<<4 | BAM_CSOFT_CLIP; - if (r[0] != c->pos) cigar[n_cigar++] = (uint32_t)(r[0] - c->pos)<<4 | BAM_CREF_SKIP; - if (kk[0] == kk[1]) { - cigar[n_cigar++] = (uint32_t)(kl[0] + kl[1] - (bam1_cigar(b)[kk[0]]>>4))<<4 | BAM_CMATCH; - } else { - cigar[n_cigar++] = (uint32_t)kl[0]<<4 | BAM_CMATCH; - for (k = kk[0] + 1; k < kk[1]; ++k) - cigar[n_cigar++] = bam1_cigar(b)[k]; - cigar[n_cigar++] = (uint32_t)kl[1]<<4 | BAM_CMATCH; // FIXME: add ref_skip after this line + if (bq || zq) { + if ((apply_baq && zq) || (!apply_baq && bq)) return -3; // in both cases, do nothing + if (bq && apply_baq) { // then convert BQ to ZQ + for (i = 0; i < c->l_qseq; ++i) + qual[i] = qual[i] + 64 < bq[i]? 0 : qual[i] - ((int)bq[i] - 64); + *(bq - 3) = 'Z'; + } else if (zq && !apply_baq) { // then convert ZQ to BQ + for (i = 0; i < c->l_qseq; ++i) + qual[i] += (int)zq[i] - 64; + *(zq - 3) = 'B'; + } + return 0; } - if (q[1] + 1 != c->l_qseq) - cigar[n_cigar++] = (uint32_t)(c->l_qseq - q[1] - 1)<<4 | BAM_CSOFT_CLIP; - // copy over the alignment - if (4 * (n_cigar - (int)c->n_cigar) + b->data_len > b->m_data) { // enlarge b->data - b->m_data = 4 * (n_cigar - (int)c->n_cigar) + b->data_len; - kroundup32(b->m_data); - b->data = realloc(b->data, b->m_data); + // find the start and end of the alignment + x = c->pos, y = 0, yb = ye = xb = xe = -1; + for (k = 0; k < c->n_cigar; ++k) { + int op, l; + op = cigar[k]&0xf; l = cigar[k]>>4; + if (op == BAM_CMATCH) { + if (yb < 0) yb = y; + if (xb < 0) xb = x; + ye = y + l; xe = x + l; + x += l; y += l; + } else if (op == BAM_CSOFT_CLIP || op == BAM_CINS) y += l; + else if (op == BAM_CDEL) x += l; + else if (op == BAM_CREF_SKIP) return -1; // do nothing if there is a reference skip } - if (n_cigar != (int)c->n_cigar) { // move data - memmove(b->data + c->l_qname + 4 * n_cigar, bam1_seq(b), b->data_len - c->l_qname - 4 * c->n_cigar); - b->data_len += 4 * (n_cigar - (int)c->n_cigar); + // set bandwidth and the start and the end + bw = 7; + if (abs((xe - xb) - (ye - yb)) > bw) + bw = abs((xe - xb) - (ye - yb)) + 3; + conf.bw = bw; + xb -= yb + bw/2; if (xb < 0) xb = 0; + xe += c->l_qseq - ye + bw/2; + if (xe - xb - c->l_qseq > bw) + xb += (xe - xb - c->l_qseq - bw) / 2, xe -= (xe - xb - c->l_qseq - bw) / 2; + { // glocal + uint8_t *s, *r, *q, *seq = bam1_seq(b), *bq; + int *state; + bq = calloc(c->l_qseq + 1, 1); + memcpy(bq, qual, c->l_qseq); + s = calloc(c->l_qseq, 1); + for (i = 0; i < c->l_qseq; ++i) s[i] = bam_nt16_nt4_table[bam1_seqi(seq, i)]; + r = calloc(xe - xb, 1); + for (i = xb; i < xe; ++i) { + if (ref[i] == 0) { xe = i; break; } + r[i-xb] = bam_nt16_nt4_table[bam_nt16_table[(int)ref[i]]]; + } + state = calloc(c->l_qseq, sizeof(int)); + q = calloc(c->l_qseq, 1); + kpa_glocal(r, xe-xb, s, c->l_qseq, qual, &conf, state, q); + if (!extend_baq) { // in this block, bq[] is capped by base quality qual[] + for (k = 0, x = c->pos, y = 0; k < c->n_cigar; ++k) { + int op = cigar[k]&0xf, l = cigar[k]>>4; + if (op == BAM_CMATCH) { + for (i = y; i < y + l; ++i) { + if ((state[i]&3) != 0 || state[i]>>2 != x - xb + (i - y)) bq[i] = 0; + else bq[i] = bq[i] < q[i]? bq[i] : q[i]; + } + x += l; y += l; + } else if (op == BAM_CSOFT_CLIP || op == BAM_CINS) y += l; + else if (op == BAM_CDEL) x += l; + } + for (i = 0; i < c->l_qseq; ++i) bq[i] = qual[i] - bq[i] + 64; // finalize BQ + } else { // in this block, bq[] is BAQ that can be larger than qual[] (different from the above!) + uint8_t *left, *rght; + left = calloc(c->l_qseq, 1); rght = calloc(c->l_qseq, 1); + for (k = 0, x = c->pos, y = 0; k < c->n_cigar; ++k) { + int op = cigar[k]&0xf, l = cigar[k]>>4; + if (op == BAM_CMATCH) { + for (i = y; i < y + l; ++i) + bq[i] = ((state[i]&3) != 0 || state[i]>>2 != x - xb + (i - y))? 0 : q[i]; + for (left[y] = bq[y], i = y + 1; i < y + l; ++i) + left[i] = bq[i] > left[i-1]? bq[i] : left[i-1]; + for (rght[y+l-1] = bq[y+l-1], i = y + l - 2; i >= y; --i) + rght[i] = bq[i] > rght[i+1]? bq[i] : rght[i+1]; + for (i = y; i < y + l; ++i) + bq[i] = left[i] < rght[i]? left[i] : rght[i]; + x += l; y += l; + } else if (op == BAM_CSOFT_CLIP || op == BAM_CINS) y += l; + else if (op == BAM_CDEL) x += l; + } + for (i = 0; i < c->l_qseq; ++i) bq[i] = 64 + (qual[i] <= bq[i]? 0 : qual[i] - bq[i]); // finalize BQ + free(left); free(rght); + } + if (apply_baq) { + for (i = 0; i < c->l_qseq; ++i) qual[i] -= bq[i] - 64; // modify qual + bam_aux_append(b, "ZQ", 'Z', c->l_qseq + 1, bq); + } else bam_aux_append(b, "BQ", 'Z', c->l_qseq + 1, bq); + free(bq); free(s); free(r); free(q); free(state); } - memcpy(bam1_cigar(b), cigar, n_cigar * 4); - c->n_cigar = n_cigar; - free(cigar); return 0; } +int bam_prob_realn(bam1_t *b, const char *ref) +{ + return bam_prob_realn_core(b, ref, 1); +} + int bam_fillmd(int argc, char *argv[]) { - int c, is_equal = 0, tid = -2, ret, len, is_bam_out, is_sam_in, is_uncompressed, max_nm = 0, is_realn, is_capQ; + int c, is_equal, tid = -2, ret, len, is_bam_out, is_sam_in, is_uncompressed, max_nm, is_realn, capQ, baq_flag; samfile_t *fp, *fpout = 0; faidx_t *fai; char *ref = 0, mode_w[8], mode_r[8]; bam1_t *b; - is_bam_out = is_sam_in = is_uncompressed = is_realn = is_capQ = 0; + is_equal = is_bam_out = is_sam_in = is_uncompressed = is_realn = max_nm = capQ = baq_flag = 0; mode_w[0] = mode_r[0] = 0; strcpy(mode_r, "r"); strcpy(mode_w, "w"); - while ((c = getopt(argc, argv, "reubSCn:")) >= 0) { + while ((c = getopt(argc, argv, "EreubSC:n:A")) >= 0) { switch (c) { case 'r': is_realn = 1; break; case 'e': is_equal = 1; break; @@ -280,7 +297,9 @@ int bam_fillmd(int argc, char *argv[]) case 'u': is_uncompressed = is_bam_out = 1; break; case 'S': is_sam_in = 1; break; case 'n': max_nm = atoi(optarg); break; - case 'C': is_capQ = 1; break; + case 'C': capQ = atoi(optarg); break; + case 'A': baq_flag |= 1; break; + case 'E': baq_flag |= 2; break; default: fprintf(stderr, "[bam_fillmd] unrecognized option '-%c'\n", c); return 1; } } @@ -295,7 +314,9 @@ int bam_fillmd(int argc, char *argv[]) fprintf(stderr, " -u uncompressed BAM output (for piping)\n"); fprintf(stderr, " -b compressed BAM output\n"); fprintf(stderr, " -S the input is SAM with header\n"); - fprintf(stderr, " -r read-independent local realignment\n\n"); + fprintf(stderr, " -A modify the quality string\n"); + fprintf(stderr, " -r compute the BQ tag (without -A) or cap baseQ by BAQ (with -A)\n"); + fprintf(stderr, " -E extended BAQ for better sensitivity but lower specificity\n\n"); return 1; } fp = samopen(argv[optind], mode_r, 0); @@ -318,9 +339,9 @@ int bam_fillmd(int argc, char *argv[]) fprintf(stderr, "[bam_fillmd] fail to find sequence '%s' in the reference.\n", fp->header->target_name[tid]); } - if (is_realn) bam_realn(b, ref); - if (is_capQ) { - int q = bam_cap_mapQ(b, ref, 40); + if (is_realn) bam_prob_realn_core(b, ref, baq_flag); + if (capQ > 10) { + int q = bam_cap_mapQ(b, ref, capQ); if (b->core.qual > q) b->core.qual = q; } if (ref) bam_fillmd1_core(b, ref, is_equal, max_nm);