1 | 1 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,64 @@ |
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 |
+#ifdef _MAIN /* Rsamtools */ |
|
38 |
+int main_reheader(int argc, char *argv[]) |
|
39 |
+{ |
|
40 |
+ bam_header_t *h; |
|
41 |
+ BGZF *in; |
|
42 |
+ if (argc != 3) { |
|
43 |
+ fprintf(stderr, "Usage: samtools reheader <in.header.sam> <in.bam>\n"); |
|
44 |
+ return 1; |
|
45 |
+ } |
|
46 |
+ { // read the header |
|
47 |
+ tamFile fph = sam_open(argv[1]); |
|
48 |
+ if (fph == 0) { |
|
49 |
+ fprintf(stderr, "[%s] fail to read the header from %s.\n", __func__, argv[1]); |
|
50 |
+ return 1; |
|
51 |
+ } |
|
52 |
+ h = sam_header_read(fph); |
|
53 |
+ sam_close(fph); |
|
54 |
+ } |
|
55 |
+ in = strcmp(argv[2], "-")? bam_open(argv[2], "r") : bam_dopen(fileno(stdin), "r"); |
|
56 |
+ if (in == 0) { |
|
57 |
+ fprintf(stderr, "[%s] fail to open file %s.\n", __func__, argv[2]); |
|
58 |
+ return 1; |
|
59 |
+ } |
|
60 |
+ bam_reheader(in, h, fileno(stdout)); |
|
61 |
+ bgzf_close(in); |
|
62 |
+ return 0; |
|
63 |
+} |
|
64 |
+#endif /* Rsamtools */ |