0
|
1 #include <stdio.h>
|
|
2 #include <stdlib.h>
|
|
3 #include "knetfile.h"
|
|
4 #include "bgzf.h"
|
|
5 #include "bam.h"
|
|
6
|
|
7 #define BUF_SIZE 0x10000
|
|
8
|
|
9 int bam_reheader(BGZF *in, const bam_header_t *h, int fd)
|
|
10 {
|
|
11 BGZF *fp;
|
|
12 bam_header_t *old;
|
|
13 int len;
|
|
14 uint8_t *buf;
|
|
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);
|
|
22 bgzf_flush(fp);
|
|
23 }
|
|
24 #ifdef _USE_KNETFILE
|
|
25 while ((len = knet_read(in->fp, buf, BUF_SIZE)) > 0)
|
|
26 fwrite(buf, 1, len, fp->fp);
|
|
27 #else
|
|
28 while (!feof(in->file) && (len = fread(buf, 1, BUF_SIZE, in->file)) > 0)
|
|
29 fwrite(buf, 1, len, fp->file);
|
|
30 #endif
|
|
31 free(buf);
|
|
32 fp->block_offset = in->block_offset = 0;
|
|
33 bgzf_close(fp);
|
|
34 return 0;
|
|
35 }
|
|
36
|
|
37 int main_reheader(int argc, char *argv[])
|
|
38 {
|
|
39 bam_header_t *h;
|
|
40 BGZF *in;
|
|
41 if (argc != 3) {
|
|
42 fprintf(stderr, "Usage: samtools reheader <in.header.sam> <in.bam>\n");
|
|
43 return 1;
|
|
44 }
|
|
45 { // read the header
|
|
46 tamFile fph = sam_open(argv[1]);
|
|
47 if (fph == 0) {
|
|
48 fprintf(stderr, "[%s] fail to read the header from %s.\n", __func__, argv[1]);
|
|
49 return 1;
|
|
50 }
|
|
51 h = sam_header_read(fph);
|
|
52 sam_close(fph);
|
|
53 }
|
|
54 in = strcmp(argv[2], "-")? bam_open(argv[2], "r") : bam_dopen(fileno(stdin), "r");
|
|
55 if (in == 0) {
|
|
56 fprintf(stderr, "[%s] fail to open file %s.\n", __func__, argv[2]);
|
|
57 return 1;
|
|
58 }
|
|
59 bam_reheader(in, h, fileno(stdout));
|
|
60 bgzf_close(in);
|
|
61 return 0;
|
|
62 }
|