- 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);
+ fai = fai_load(fn_ref);
+ }
+ // open file handlers
+ if ((in = samopen(argv[optind], in_mode, fn_list)) == 0) {
+ fprintf(stderr, "[depad] failed to open \"%s\" for reading.\n", argv[optind]);
+ ret = 1;
+ goto depad_end;
+ }
+ if (in->header == 0) {
+ fprintf(stderr, "[depad] failed 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] failed 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, 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 <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;