forked from samtools/bcftools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvcfview.unused.c
70 lines (61 loc) · 2.49 KB
/
vcfview.unused.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
62
63
64
65
66
67
68
69
#include <stdio.h>
#include <unistd.h>
#include <htslib/vcf.h>
#include <htslib/synced_bcf_reader.h>
#include "bcftools.h"
static void usage(void)
{
fprintf(stderr, "About: Convert between VCF/BCF.\n"); // Most of the original functionality was moved into vcfsubset
fprintf(stderr, "Usage: bcftools view [options] <in.bcf>|<in.vcf>|<in.vcf.gz>|-\n");
fprintf(stderr, "Options:\n");
fprintf(stderr, " -l INT compression level [%d]\n", -1);
fprintf(stderr, " -n FILE output file name [stdout]\n");
fprintf(stderr, " -O TYPE output type: 'b' compressed BCF; 'u' uncompressed BCF; 'z' compressed VCF; 'v' uncompressed VCF [v]\n");
fprintf(stderr, "\n");
}
int main_vcfview(int argc, char *argv[])
{
int c, clevel = -1, out_type = FT_VCF;
char *fname_out = NULL, modew[8];
while ((c = getopt(argc, argv, "l:bO:n:z?hu")) >= 0) {
switch (c) {
case 'O':
switch (optarg[0]) {
case 'b': out_type = FT_BCF_GZ; break;
case 'u': out_type = FT_BCF; break;
case 'z': out_type = FT_VCF_GZ; break;
case 'v': out_type = FT_VCF; break;
default: error("The output type \"%s\" not recognised\n", optarg);
}
break;
case 'l': clevel = atoi(optarg); out_type |= FT_GZ; break;
case 'b': out_type = FT_BCF_GZ; break;
case 'u': out_type = FT_BCF; break;
case 'z': out_type = FT_VCF_GZ; break;
case 'n': fname_out = optarg; break;
case '?':
case 'h': usage(); return 1; break;
}
}
if (argc!=optind+1) { usage(); return 1; }
// Init reader
htsFile *fp_in = hts_open(argv[optind], "r");
if ( !fp_in ) error("Fail to open: %s\n", argv[optind]);
bcf_hdr_t *hdr = bcf_hdr_read(fp_in);
if ( !hdr ) error("Fail to read VCF/BCF header: %s\n", argv[optind]);
bcf1_t *rec = bcf_init1();
// Init writer
strcpy(modew, "w");
if (clevel >= 0 && clevel <= 9) sprintf(modew + 1, "%d", clevel);
if (out_type & FT_GZ) strcat(modew,"z");
if (out_type & FT_BCF) strcat(modew, "b");
if (out_type == FT_BCF) strcat(modew, "u"); // uncompressed BCF output
htsFile *fp_out = hts_open(fname_out ? fname_out : "-", modew);
bcf_hdr_write(fp_out, hdr);
while ( bcf_read1(fp_in, hdr, rec) >= 0) bcf_write1(fp_out, hdr, rec);
bcf_destroy1(rec);
bcf_hdr_destroy(hdr);
hts_close(fp_in);
hts_close(fp_out);
return 0;
}