1 /* test/vcf-miniview.c -- minimal BCF/VCF viewer, for use by test harness.
2
3 Copyright (C) 2014, 2016 Genome Research Ltd.
4
5 Author: John Marshall <jm18@sanger.ac.uk>
6
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13
14 The above copyright notice and this permission notice shall be included in
15 all copies or substantial portions of the Software.
16
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 DEALINGS IN THE SOFTWARE. */
24
25 #include <config.h>
26
27 #include <ctype.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 #include <htslib/vcf.h>
33
usage()34 void usage()
35 {
36 fprintf(stderr,
37 "Usage: vcf-miniview [view] [-f] [FILE]\n"
38 "Options:\n"
39 " -f Filters out ## headers and various fields, to simplify file comparison\n");
40 exit(EXIT_FAILURE);
41 }
42
fail(const char * message)43 void fail(const char *message)
44 {
45 fprintf(stderr, "vcf-miniview: %s\n", message);
46 exit(EXIT_FAILURE);
47 }
48
erase(kstring_t * str,const char * tag)49 void erase(kstring_t* str, const char *tag)
50 {
51 char *begin, *end;
52
53 if ((begin = strstr(str->s+1, tag)) == NULL) return;
54
55 end = begin;
56 while (*end && *end != '\t' && *end != ';') end++;
57 if (begin[-1] == ';') begin--;
58
59 memmove(begin, end, str->l - (end - str->s) + 1);
60 str->l -= end - begin;
61 }
62
main(int argc,char ** argv)63 int main(int argc, char **argv)
64 {
65 int optind, filter = 0;
66 htsFile *in;
67 bcf_hdr_t *hdr;
68 bcf1_t *rec;
69 kstring_t str = { 0, 0, NULL };
70
71 optind = 1;
72 if (optind < argc && strcmp(argv[optind], "view") == 0) optind++;
73 if (optind < argc && strcmp(argv[optind], "-f") == 0) filter = 1, optind++;
74 if (argc == 1 || argc - optind > 1) usage();
75
76 if ((in = hts_open((optind < argc)? argv[optind] : "-", "r")) == NULL)
77 fail("can't open input file");
78
79 if ((hdr = bcf_hdr_read(in)) == NULL)
80 fail("can't read header");
81
82 bcf_hdr_format(hdr, 0, &str);
83 if (filter) {
84 char *fixed = strstr(str.s, "\n#CHROM");
85 printf("%s", fixed? fixed+1 : str.s);
86 }
87 else printf("%s", str.s);
88
89 rec = bcf_init();
90 while (bcf_read(in, hdr, rec) >= 0) {
91 str.l = 0;
92 vcf_format(hdr, rec, &str);
93 if (filter) {
94 erase(&str, "IMF=");
95 erase(&str, "DP=");
96 erase(&str, "IDV=");
97 erase(&str, "IMP=");
98 erase(&str, "IS=");
99 erase(&str, "VDB=");
100 erase(&str, "SGB=");
101 erase(&str, "MQB=");
102 erase(&str, "BQB=");
103 erase(&str, "RPB=");
104 erase(&str, "MQ0F=");
105 erase(&str, "MQSB=");
106 }
107 printf("%s", str.s);
108 }
109
110 free(str.s);
111 bcf_destroy(rec);
112 bcf_hdr_destroy(hdr);
113 hts_close(in);
114 return EXIT_SUCCESS;
115 }
116