annotate PsiCLASS-1.0.2/samtools-0.1.19/bam_reheader.c @ 0:903fc43d6227 draft default tip

Uploaded
author lsong10
date Fri, 26 Mar 2021 16:52:45 +0000
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1 #include <stdio.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
2 #include <stdlib.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
3 #include "knetfile.h"
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
4 #include "bgzf.h"
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
5 #include "bam.h"
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
6
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
7 #define BUF_SIZE 0x10000
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
8
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
9 int bam_reheader(BGZF *in, const bam_header_t *h, int fd)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
10 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
11 BGZF *fp;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
12 bam_header_t *old;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
13 int len;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
14 uint8_t *buf;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
15 if (in->is_write) return -1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
16 buf = malloc(BUF_SIZE);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
17 old = bam_header_read(in);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
18 fp = bgzf_fdopen(fd, "w");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
19 bam_header_write(fp, h);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
20 if (in->block_offset < in->block_length) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
21 bgzf_write(fp, in->uncompressed_block + in->block_offset, in->block_length - in->block_offset);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
22 bgzf_flush(fp);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
23 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
24 #ifdef _USE_KNETFILE
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
25 while ((len = knet_read(in->fp, buf, BUF_SIZE)) > 0)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
26 fwrite(buf, 1, len, fp->fp);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
27 #else
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
28 while (!feof(in->file) && (len = fread(buf, 1, BUF_SIZE, in->file)) > 0)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
29 fwrite(buf, 1, len, fp->file);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
30 #endif
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
31 free(buf);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
32 fp->block_offset = in->block_offset = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
33 bgzf_close(fp);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
34 return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
35 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
36
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
37 int main_reheader(int argc, char *argv[])
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
38 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
39 bam_header_t *h;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
40 BGZF *in;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
41 if (argc != 3) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
42 fprintf(stderr, "Usage: samtools reheader <in.header.sam> <in.bam>\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
43 return 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
44 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
45 { // read the header
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
46 tamFile fph = sam_open(argv[1]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
47 if (fph == 0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
48 fprintf(stderr, "[%s] fail to read the header from %s.\n", __func__, argv[1]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
49 return 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
50 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
51 h = sam_header_read(fph);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
52 sam_close(fph);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
53 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
54 in = strcmp(argv[2], "-")? bam_open(argv[2], "r") : bam_dopen(fileno(stdin), "r");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
55 if (in == 0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
56 fprintf(stderr, "[%s] fail to open file %s.\n", __func__, argv[2]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
57 return 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
58 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
59 bam_reheader(in, h, fileno(stdout));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
60 bgzf_close(in);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
61 return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
62 }