7 #define BUF_SIZE 0x10000
9 int bam_reheader(BGZF *in, const bam_header_t *h, int fd)
15 if (in->is_write) return -1;
16 buf = malloc(BUF_SIZE);
17 old = bam_header_read(in);
18 fp = bgzf_fdopen(fd, "w");
19 bam_header_write(fp, h);
20 if (in->block_offset < in->block_length) {
21 bgzf_write(fp, in->uncompressed_block + in->block_offset, in->block_length - in->block_offset);
25 while ((len = knet_read(in->fp, buf, BUF_SIZE)) > 0)
26 fwrite(buf, 1, len, fp->fp);
28 while (!feof(in->file) && (len = fread(buf, 1, BUF_SIZE, in->file)) > 0)
29 fwrite(buf, 1, len, fp->file);
32 fp->block_offset = in->block_offset = 0;
37 int main_reheader(int argc, char *argv[])
42 fprintf(stderr, "Usage: samtools reheader <in.header.sam> <in.bam>\n");
46 tamFile fph = sam_open(argv[1]);
48 fprintf(stderr, "[%s] fail to read the header from %s.\n", __func__, argv[1]);
51 h = sam_header_read(fph);
54 in = strcmp(argv[2], "-")? bam_open(argv[2], "r") : bam_dopen(fileno(stdin), "r");
56 fprintf(stderr, "[%s] fail to open file %s.\n", __func__, argv[2]);
59 bam_reheader(in, h, fileno(stdout));