Debian Codegraph @

bcftools/vcfhead.c

raw symbols SCIP
1 /* vcfhead.c -- view VCF/BCF file headers.
2
3 Copyright (C) 2021 University of Glasgow.
4 Copyright (C) 2023 Genome Research Ltd.
5
6 Author: John Marshall <[email protected]>
7
8 Permission is hereby granted, free of charge, to any person obtaining a copy
9 of this software and associated documentation files (the "Software"), to deal
10 in the Software without restriction, including without limitation the rights
11 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 copies of the Software, and to permit persons to whom the Software is
13 furnished to do so, subject to the following conditions:
14
15 The above copyright notice and this permission notice shall be included in
16 all copies or substantial portions of the Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 THE SOFTWARE. */
25
26 #include <getopt.h>
27 #include <inttypes.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <unistd.h>
31
32 #include <htslib/kstring.h>
33 #include <htslib/vcf.h>
34
35 #include "bcftools.h"
36
37 int main_vcfhead(int argc, char *argv[])
38 {
39 static const char usage[] =
40 "\n"
41 "About: Displays VCF/BCF headers and optionally the first few variant records\n"
42 "Usage: bcftools head [OPTION]... [FILE]\n"
43 "\n"
44 "Options:\n"
45 " -h, --headers INT Display INT header lines [all]\n"
46 " -n, --records INT Display INT variant record lines [none]\n"
47 " -s, --samples INT Display INT records starting with the #CHROM header line [none]\n"
48 "\n";
49
50 static const struct option loptions[] = {
51 { "headers", required_argument, NULL, 'h' },
52 { "records", required_argument, NULL, 'n' },
53 { "samples", required_argument, NULL, 's' },
54 { NULL, 0, NULL, 0 }
55 };
56
57 int all_headers = 1;
58 int samples = 0;
59 uint64_t nheaders = 0;
60 uint64_t nrecords = 0;
61
62 int c, nargs;
63 while ((c = getopt_long(argc, argv, "h:n:s:", loptions, NULL)) >= 0)
64 switch (c) {
65 case 'h': all_headers = 0; nheaders = strtoull(optarg, NULL, 0); break;
66 case 'n': nrecords = strtoull(optarg, NULL, 0); break;
67 case 's': nrecords = strtoull(optarg, NULL, 0); samples = 1; break;
68 default:
69 fputs(usage, stderr);
70 return EXIT_FAILURE;
71 }
72
73 if ( samples && all_headers ) all_headers = 0;
74
75 nargs = argc - optind;
76 if (nargs == 0 && isatty(STDIN_FILENO)) {
77 fputs(usage, stdout);
78 return EXIT_SUCCESS;
79 }
80 else if (nargs > 1) {
81 fputs(usage, stderr);
82 return EXIT_FAILURE;
83 }
84
85 const char *fname = (nargs == 1)? argv[optind] : "-";
86 vcfFile *fp = bcf_open(fname, "r");
87 if (fp == NULL) {
88 if (strcmp(fname, "-") != 0)
89 error_errno("[%s] Can't open \"%s\"", __func__, fname);
90 else
91 error_errno("[%s] Can't open standard input", __func__);
92 }
93
94 bcf_hdr_t *hdr = bcf_hdr_read(fp);
95 if (hdr == NULL) {
96 bcf_close(fp);
97 if (strcmp(fname, "-") != 0)
98 error("[%s] Can't read headers from \"%s\"\n", __func__, fname);
99 else
100 error("[%s] Can't read headers\n", __func__);
101 }
102
103 kstring_t str = KS_INITIALIZE;
104
105 if (all_headers) {
106 bcf_hdr_format(hdr, 0, &str);
107 fputs(ks_str(&str), stdout);
108 }
109 else if (nheaders > 0 || samples ) {
110 bcf_hdr_format(hdr, 0, &str);
111 char *lim = str.s;
112 uint64_t n;
113 int samples_printed = 0;
114 for (n = 0; n < nheaders; n++) {
115 if ( samples && !strncmp(lim,"#CHROM\t",7) ) samples_printed = 1;
116 lim = strchr(lim, '\n');
117 if (lim) lim++;
118 else break;
119 }
120 if ( nheaders )
121 {
122 char tmp;
123 if (lim) { tmp = *lim; *lim = '\0'; }
124 fputs(ks_str(&str), stdout);
125 if (lim) *lim = tmp;
126 }
127 if ( lim && samples && !samples_printed )
128 {
129 while ( lim && *lim )
130 {
131 if ( !strncmp(lim,"#CHROM\t",7) ) { fputs(lim, stdout); break; }
132 lim = strchr(lim, '\n');
133 if (lim) lim++;
134 else break;
135 }
136 }
137 }
138
139 if (nrecords > 0) {
140 bcf1_t *rec = bcf_init();
141 uint64_t n;
142 for (n = 0; n < nrecords && bcf_read(fp, hdr, rec) >= 0; n++) {
143 ks_clear(&str);
144 if (vcf_format(hdr, rec, &str) >= 0)
145 fputs(ks_str(&str), stdout);
146 else
147 fprintf(stderr, "[%s] Record #%"PRIu64 " is invalid\n", __func__, n+1);
148 }
149 bcf_destroy(rec);
150 }
151
152 ks_free(&str);
153 bcf_hdr_destroy(hdr);
154 bcf_close(fp);
155
156 return EXIT_SUCCESS;
157 }