#include <string.h>
#include <assert.h>
+#include <unistd.h>
#include "kstring.h"
+#include "sam_header.h"
+#include "sam.h"
#include "bam.h"
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]);
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)
{
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) {
if (b->core.n_cigar > 2 && bam_cigar_op(cigar[1]) == BAM_CSOFT_CLIP) {
write_cigar(cigar2, n2, m2, cigar[1]);
}
- } else {
- /* Insert a dummy 0M entry to let use remove superfluous leading P ops */
- write_cigar(cigar2, n2, m2, 0);
}
- /* Include any pads if starts with an insert */
- for (k = 0; k+1 < b->core.pos && !r.s[b->core.pos - k - 1]; ++k);
- if (k) write_cigar(cigar2, n2, m2, bam_cigar_gen(k, BAM_CPAD));
/* Determine CIGAR operator for each base in the aligned read */
for (i = 0, k = b->core.pos; i < q.l; ++i, ++k)
q.s[i] = q.s[i]? (r.s[k]? BAM_CMATCH : BAM_CINS) : (r.s[k]? BAM_CDEL : BAM_CPAD);
+ /* Include any pads if starts with an insert */
+ if (q.s[0] == BAM_CINS) {
+ for (k = 0; k+1 < b->core.pos && !r.s[b->core.pos - k - 1]; ++k);
+ if (k) write_cigar(cigar2, n2, m2, bam_cigar_gen(k, BAM_CPAD));
+ }
/* Count consecutive CIGAR operators to turn into a CIGAR string */
for (i = k = 1, op = q.s[0]; i < q.l; ++i) {
if (op != q.s[i]) {
write_cigar(cigar2, n2, m2, cigar[b->core.n_cigar-1]);
}
/* Remove redundant P operators between M/X/=/D operators, e.g. 5M2P10M -> 15M */
- /* or right at the start before M/X/=/D */
int pre_op, post_op;
for (i = 2; i < n2; ++i)
if (bam_cigar_op(cigar2[i-1]) == BAM_CPAD) {
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 || pre_op == BAM_CSOFT_CLIP || pre_op == BAM_CHARD_CLIP) || (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 */
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 <in.bam>\n");
- return 1;
+ samfile_t *in = 0, *out = 0;
+ 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);
}
- 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;
+
+ // Load FASTA reference (also needed for SAM -> BAM if missing header)
+ if (fn_ref) fn_list = samfaipath(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;
+ }
+
+ // Do the depad
+ ret = bam_pad2unpad(in, out);
+
+depad_end:
+ // close files, free and return
+ free(fn_list); free(fn_out);
+ samclose(in);
+ samclose(out);
+ return ret;
+}
+
+static int usage(int is_long_help)
+{
+ fprintf(stderr, "\n");
+ fprintf(stderr, "Usage: samtools depad <in.bam>\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;
}