#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)
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) {
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);
+ }
+
+ // 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;
}
- 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;
+ 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;
}