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