int main_pad2unpad(int argc, char *argv[]) { bamFile in, out; if (argc == 1) { fprintf(stderr, "Usage: samtools depad <in.bam>\n"); return 1; } in = strcmp(argv[1], "-")? bam_open(argv[1], "r") : bam_dopen(fileno(stdin), "r"); out = bam_dopen(fileno(stdout), "w"); bam_pad2unpad(in, out); bam_close(in); bam_close(out); return 0; }
int main_pad2unpad(int argc, char *argv[]) { samFile *in = 0, *out = 0; bam_hdr_t *h = 0, *h_fix = 0; faidx_t *fai = 0; int c, compress_level = -1, is_long_help = 0; char in_mode[5], out_mode[6], *fn_out = 0, *fn_list = 0; int ret=0; sam_global_args ga = SAM_GLOBAL_ARGS_INIT; static const struct option lopts[] = { SAM_OPT_GLOBAL_OPTIONS('-', 0, 0, 0, 'T'), { NULL, 0, NULL, 0 } }; /* parse command-line options */ strcpy(in_mode, "r"); strcpy(out_mode, "w"); while ((c = getopt_long(argc, argv, "SCso:u1T:?", lopts, NULL)) >= 0) { switch (c) { case 'S': break; case 'C': hts_parse_format(&ga.out, "cram"); break; case 's': assert(compress_level == -1); hts_parse_format(&ga.out, "sam"); break; case 'o': fn_out = strdup(optarg); break; case 'u': compress_level = 0; if (ga.out.format == unknown_format) hts_parse_format(&ga.out, "bam"); break; case '1': compress_level = 1; if (ga.out.format == unknown_format) hts_parse_format(&ga.out, "bam"); break; case '?': is_long_help = 1; break; default: if (parse_sam_global_opt(c, optarg, lopts, &ga) == 0) break; fprintf(stderr, "[bam_fillmd] unrecognized option '-%c'\n\n", c); return usage(is_long_help); } } if (argc == optind) return usage(is_long_help); 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 (ga.reference) { fn_list = samfaipath(ga.reference); fai = fai_load(ga.reference); } // open file handlers if ((in = sam_open_format(argv[optind], in_mode, &ga.in)) == 0) { fprintf(stderr, "[depad] failed to open \"%s\" for reading.\n", argv[optind]); ret = 1; goto depad_end; } if (fn_list && hts_set_fai_filename(in, fn_list) != 0) { fprintf(stderr, "[depad] failed to load reference file \"%s\".\n", fn_list); ret = 1; goto depad_end; } if ((h = sam_hdr_read(in)) == 0) { fprintf(stderr, "[depad] failed to read the header from \"%s\".\n", argv[optind]); ret = 1; goto depad_end; } if (fai) { h_fix = fix_header(h, fai); } else { fprintf(stderr, "[depad] Warning - reference lengths will not be corrected without FASTA reference\n"); h_fix = h; } char wmode[2]; strcat(out_mode, sam_open_mode(wmode, fn_out, NULL)==0 ? wmode : "b"); if ((out = sam_open_format(fn_out? fn_out : "-", out_mode, &ga.out)) == 0) { fprintf(stderr, "[depad] failed to open \"%s\" for writing.\n", fn_out? fn_out : "standard output"); ret = 1; goto depad_end; } // Reference-based CRAM won't work unless we also create a new reference. // We could embed this, but for now we take the easy option. if (ga.out.format == cram) hts_set_opt(out, CRAM_OPT_NO_REF, 1); if (sam_hdr_write(out, h_fix) != 0) { fprintf(stderr, "[depad] failed to write header.\n"); ret = 1; goto depad_end; } // Do the depad ret = bam_pad2unpad(in, out, h, fai); depad_end: // close files, free and return if (fai) fai_destroy(fai); if (h) bam_hdr_destroy(h); sam_close(in); sam_close(out); free(fn_list); free(fn_out); return ret; }