-
Notifications
You must be signed in to change notification settings - Fork 39
/
bam_reheader.c
61 lines (58 loc) · 1.46 KB
/
bam_reheader.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <stdio.h>
#include <stdlib.h>
#include "bgzf.h"
#include "bam.h"
#define BUF_SIZE 0x10000
int bam_reheader(BGZF *in, const bam_header_t *h, int fd)
{
BGZF *fp;
bam_header_t *old;
int len;
uint8_t *buf;
if (in->open_mode != 'r') return -1;
buf = malloc(BUF_SIZE);
old = bam_header_read(in);
fp = bgzf_fdopen(fd, "w");
bam_header_write(fp, h);
if (in->block_offset < in->block_length) {
bgzf_write(fp, in->uncompressed_block + in->block_offset, in->block_length - in->block_offset);
bgzf_flush(fp);
}
#ifdef _USE_KNETFILE
while ((len = knet_read(in->x.fpr, buf, BUF_SIZE)) > 0)
fwrite(buf, 1, len, fp->x.fpw);
#else
while (!feof(in->file) && (len = fread(buf, 1, BUF_SIZE, in->file)) > 0)
fwrite(buf, 1, len, fp->file);
#endif
free(buf);
fp->block_offset = in->block_offset = 0;
bgzf_close(fp);
return 0;
}
int main_reheader(int argc, char *argv[])
{
bam_header_t *h;
BGZF *in;
if (argc != 3) {
fprintf(stderr, "Usage: samtools reheader <in.header.sam> <in.bam>\n");
return 1;
}
{ // read the header
tamFile fph = sam_open(argv[1]);
if (fph == 0) {
fprintf(stderr, "[%s] fail to read the header from %s.\n", __func__, argv[1]);
return 1;
}
h = sam_header_read(fph);
sam_close(fph);
}
in = strcmp(argv[2], "-")? bam_open(argv[2], "r") : bam_dopen(fileno(stdin), "r");
if (in == 0) {
fprintf(stderr, "[%s] fail to open file %s.\n", __func__, argv[2]);
return 1;
}
bam_reheader(in, h, fileno(stdout));
bgzf_close(in);
return 0;
}