X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=padding.c;h=9f8bca7e0fe13992aba557b176090936a34cb34a;hb=dc39f05a341901dc9dd1ad0a8b6dad8b48c12967;hp=6e40833c9a7b20b24029b75f87b504f3503330d2;hpb=d2496f2aaae8022e04d10b9fd09eb305026a77df;p=samtools.git diff --git a/padding.c b/padding.c index 6e40833..9f8bca7 100644 --- a/padding.c +++ b/padding.c @@ -1,7 +1,11 @@ #include #include +#include #include "kstring.h" +#include "sam_header.h" +#include "sam.h" #include "bam.h" +#include "faidx.h" static void replace_cigar(bam1_t *b, int n, uint32_t *cigar) { @@ -30,9 +34,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,30 +59,30 @@ 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) +int bam_pad2unpad(samfile_t *in, samfile_t *out, faidx_t *fai) { bam_header_t *h; bam1_t *b; kstring_t r, q; int r_tid = -1; uint32_t *cigar2 = 0; - int n2 = 0, m2 = 0, *posmap = 0; + int ret = 0, n2 = 0, m2 = 0, *posmap = 0; - h = bam_header_read(in); - /* TODO - The reference sequence lengths in the BAM + SAM headers should be updated */ - bam_header_write(out, h); b = bam_init1(); r.l = r.m = q.l = q.m = 0; r.s = q.s = 0; - while (bam_read1(in, b) >= 0) { + int read_ret; + h = in->header; + while ((read_ret = samread(in, b)) >= 0) { // read one alignment from `in' uint32_t *cigar = bam1_cigar(b); n2 = 0; if (b->core.pos == 0 && b->core.tid >= 0 && strcmp(bam1_qname(b), h->target_name[b->core.tid]) == 0) { @@ -136,7 +152,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 */ @@ -154,25 +170,106 @@ int bam_pad2unpad(bamFile in, bamFile out) replace_cigar(b, n2, cigar2); b->core.pos = posmap[b->core.pos]; } - bam_write1(out, b); + samwrite(out, b); + } + if (read_ret < -1) { + fprintf(stderr, "[depad] truncated file.\n"); + ret = 1; } free(r.s); free(q.s); free(posmap); bam_destroy1(b); - bam_header_destroy(h); - return 0; + return ret; } +static int usage(int is_long_help); + int main_pad2unpad(int argc, char *argv[]) { - bamFile in, out; - int result=0; - if (argc == 1) { - fprintf(stderr, "Usage: samtools depad \n"); - return 1; + samfile_t *in = 0, *out = 0; + faidx_t *fai; + int c, is_bamin = 1, compress_level = -1, is_bamout = 1, is_long_help = 0; + char in_mode[5], out_mode[5], *fn_out = 0, *fn_list = 0, *fn_ref = 0; + int ret=0; + + /* parse command-line options */ + strcpy(in_mode, "r"); strcpy(out_mode, "w"); + while ((c = getopt(argc, argv, "Sso:u1T:?")) >= 0) { + switch (c) { + case 'S': is_bamin = 0; break; + case 's': assert(compress_level == -1); is_bamout = 0; break; + case 'o': fn_out = strdup(optarg); break; + case 'u': assert(is_bamout == 1); compress_level = 0; break; + case '1': assert(is_bamout == 1); compress_level = 1; break; + case 'T': fn_ref = strdup(optarg); is_bamin = 0; break; + case '?': is_long_help = 1; break; + default: return usage(is_long_help); + } + } + if (argc == optind) return usage(is_long_help); + + if (is_bamin) strcat(in_mode, "b"); + if (is_bamout) strcat(out_mode, "b"); + strcat(out_mode, "h"); + if (compress_level >= 0) { + char tmp[2]; + tmp[0] = compress_level + '0'; tmp[1] = '\0'; + strcat(out_mode, tmp); + } + + // Load FASTA reference (also needed for SAM -> BAM if missing header) + if (fn_ref) { + fn_list = samfaipath(fn_ref); + fai = fai_load(fn_ref); + } + // open file handlers + if ((in = samopen(argv[optind], in_mode, fn_list)) == 0) { + fprintf(stderr, "[depad] fail to open \"%s\" for reading.\n", argv[optind]); + ret = 1; + goto depad_end; + } + if (in->header == 0) { + fprintf(stderr, "[depad] fail to read the header from \"%s\".\n", argv[optind]); + ret = 1; + goto depad_end; + } + /* TODO - The reference sequence lengths in the BAM + SAM headers should be updated */ + if ((out = samopen(fn_out? fn_out : "-", out_mode, in->header)) == 0) { + fprintf(stderr, "[depad] fail to open \"%s\" for writing.\n", fn_out? fn_out : "standard output"); + ret = 1; + goto depad_end; } - in = strcmp(argv[1], "-")? bam_open(argv[1], "r") : bam_dopen(fileno(stdin), "r"); - out = bam_dopen(fileno(stdout), "w"); - result = bam_pad2unpad(in, out); - bam_close(in); bam_close(out); - return result; + + // Do the depad + ret = bam_pad2unpad(in, out, fai); + +depad_end: + // close files, free and return + free(fn_list); free(fn_out); + samclose(in); + samclose(out); + fai_destroy(fai); + return ret; +} + +static int usage(int is_long_help) +{ + fprintf(stderr, "\n"); + fprintf(stderr, "Usage: samtools depad \n\n"); + fprintf(stderr, "Options: -s output is SAM (default is BAM)\n"); + fprintf(stderr, " -S input is SAM (default is BAM)\n"); + fprintf(stderr, " -u uncompressed BAM output (can't use with -s)\n"); + fprintf(stderr, " -1 fast compression BAM output (can't use with -s)\n"); + fprintf(stderr, " -T FILE reference sequence file [null]\n"); + fprintf(stderr, " -o FILE output file name [stdout]\n"); + fprintf(stderr, " -? longer help\n"); + fprintf(stderr, "\n"); + if (is_long_help) + fprintf(stderr, "Notes:\n\ +\n\ + 1. Requires embedded reference sequences (before the reads for that reference),\n\ + with the future aim to also support a FASTA padded reference sequence file.\n\ +\n\ + 2. The input padded alignment read's CIGAR strings must not use P or I operators.\n\ +\n"); + return 1; }