6 #define BUF_SIZE 0x10000
8 int bam_reheader(BGZF *in, const bam_header_t *h, int fd)
14 if (in->open_mode != 'r') return -1;
15 buf = malloc(BUF_SIZE);
16 old = bam_header_read(in);
17 fp = bgzf_fdopen(fd, "w");
18 bam_header_write(fp, h);
19 if (in->block_offset < in->block_length) {
20 bgzf_write(fp, in->uncompressed_block + in->block_offset, in->block_length - in->block_offset);
24 while ((len = knet_read(in->x.fpr, buf, BUF_SIZE)) > 0)
26 while (!feof(in->file) && (len = fread(buf, 1, BUF_SIZE, in->file)) > 0)
28 fwrite(buf, 1, len, fp->x.fpw);
30 fp->block_offset = in->block_offset = 0;
35 int main_reheader(int argc, char *argv[])
40 fprintf(stderr, "Usage: samtools reheader <in.header.sam> <in.bam>\n");
44 tamFile fph = sam_open(argv[1]);
46 fprintf(stderr, "[%s] fail to read the header from %s.\n", __func__, argv[1]);
49 h = sam_header_read(fph);
52 in = strcmp(argv[2], "-")? bam_open(argv[2], "r") : bam_dopen(fileno(stdin), "r");
54 fprintf(stderr, "[%s] fail to open file %s.\n", __func__, argv[2]);
57 bam_reheader(in, h, fileno(stdout));