X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=padding.c;h=6d97b1882a6f70ac6bca1d35f7b8217b0a02c926;hb=a46b631be3830db9bf4db53aa5e04c043a3213eb;hp=6e40833c9a7b20b24029b75f87b504f3503330d2;hpb=d2496f2aaae8022e04d10b9fd09eb305026a77df;p=samtools.git diff --git a/padding.c b/padding.c index 6e40833..6d97b18 100644 --- a/padding.c +++ b/padding.c @@ -30,9 +30,21 @@ static void replace_cigar(bam1_t *b, int n, uint32_t *cigar) static void unpad_seq(bam1_t *b, kstring_t *s) { int k, j, i; + int length; uint32_t *cigar = bam1_cigar(b); uint8_t *seq = bam1_seq(b); - ks_resize(s, b->core.l_qseq); + // b->core.l_qseq gives length of the SEQ entry (including soft clips, S) + // We need the padded length after alignment from the CIGAR (excluding + // soft clips S, but including pads from CIGAR D operations) + length = 0; + for (k = 0; k < b->core.n_cigar; ++k) { + int op, ol; + op= bam_cigar_op(cigar[k]); + ol = bam_cigar_oplen(cigar[k]); + if (op == BAM_CMATCH || op == BAM_CEQUAL || op == BAM_CDIFF || op == BAM_CDEL) + length += ol; + } + ks_resize(s, length); for (k = 0, s->l = 0, j = 0; k < b->core.n_cigar; ++k) { int op, ol; op = bam_cigar_op(cigar[k]); @@ -43,13 +55,14 @@ static void unpad_seq(bam1_t *b, kstring_t *s) j += ol; } else if (op == BAM_CHARD_CLIP) { /* do nothing */ - } else if (op == BAM_CDEL || op == BAM_CPAD) { + } else if (op == BAM_CDEL) { for (i = 0; i < ol; ++i) s->s[s->l++] = 0; } else { fprintf(stderr, "[depad] ERROR: Didn't expect CIGAR op %c in read %s\n", BAM_CIGAR_STR[op], bam1_qname(b)); assert(-1); } } + assert(length == s->l); } int bam_pad2unpad(bamFile in, bamFile out) @@ -136,7 +149,7 @@ int bam_pad2unpad(bamFile in, bamFile out) pre_op = bam_cigar_op(cigar2[i-2]); post_op = bam_cigar_op(cigar2[i]); /* Note don't need to check for X/= as code above will use M only */ - if ((pre_op == BAM_CMATCH || pre_op == BAM_CDIFF) && (post_op == BAM_CMATCH || post_op == BAM_CDIFF)) { + if ((pre_op == BAM_CMATCH || pre_op == BAM_CDEL) && (post_op == BAM_CMATCH || post_op == BAM_CDEL)) { /* This is a redundant P operator */ cigar2[i-1] = 0; // i.e. 0M /* If had same operator either side, combine them in post_op */ @@ -168,6 +181,7 @@ int main_pad2unpad(int argc, char *argv[]) int result=0; if (argc == 1) { fprintf(stderr, "Usage: samtools depad \n"); + fprintf(stderr, "\nRequires embedded reference sequences (before the reads for that reference).\n"); return 1; } in = strcmp(argv[1], "-")? bam_open(argv[1], "r") : bam_dopen(fileno(stdin), "r");