|
1 | 1 | /* vcfhead.c -- view VCF/BCF file headers. |
2 | 2 |
|
3 | 3 | Copyright (C) 2021 University of Glasgow. |
| 4 | + Copyright (C) 2023 Genome Research Ltd. |
4 | 5 |
|
5 | 6 | Author: John Marshall <[email protected]> |
6 | 7 |
|
@@ -41,30 +42,36 @@ int main_vcfhead(int argc, char *argv[]) |
41 | 42 | "Usage: bcftools head [OPTION]... [FILE]\n" |
42 | 43 | "\n" |
43 | 44 | "Options:\n" |
44 | | -" -h, --headers INT Display INT header lines [all]\n" |
45 | | -" -n, --records INT Display INT variant record lines [none]\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" |
46 | 48 | "\n"; |
47 | 49 |
|
48 | 50 | static const struct option loptions[] = { |
49 | 51 | { "headers", required_argument, NULL, 'h' }, |
50 | 52 | { "records", required_argument, NULL, 'n' }, |
| 53 | + { "samples", required_argument, NULL, 's' }, |
51 | 54 | { NULL, 0, NULL, 0 } |
52 | 55 | }; |
53 | 56 |
|
54 | 57 | int all_headers = 1; |
| 58 | + int samples = 0; |
55 | 59 | uint64_t nheaders = 0; |
56 | 60 | uint64_t nrecords = 0; |
57 | 61 |
|
58 | 62 | int c, nargs; |
59 | | - while ((c = getopt_long(argc, argv, "h:n:", loptions, NULL)) >= 0) |
| 63 | + while ((c = getopt_long(argc, argv, "h:n:s:", loptions, NULL)) >= 0) |
60 | 64 | switch (c) { |
61 | 65 | case 'h': all_headers = 0; nheaders = strtoull(optarg, NULL, 0); break; |
62 | 66 | case 'n': nrecords = strtoull(optarg, NULL, 0); break; |
| 67 | + case 's': nrecords = strtoull(optarg, NULL, 0); samples = 1; break; |
63 | 68 | default: |
64 | 69 | fputs(usage, stderr); |
65 | 70 | return EXIT_FAILURE; |
66 | 71 | } |
67 | 72 |
|
| 73 | + if ( samples && all_headers ) all_headers = 0; |
| 74 | + |
68 | 75 | nargs = argc - optind; |
69 | 76 | if (nargs == 0 && isatty(STDIN_FILENO)) { |
70 | 77 | fputs(usage, stdout); |
@@ -99,17 +106,34 @@ int main_vcfhead(int argc, char *argv[]) |
99 | 106 | bcf_hdr_format(hdr, 0, &str); |
100 | 107 | fputs(ks_str(&str), stdout); |
101 | 108 | } |
102 | | - else if (nheaders > 0) { |
| 109 | + else if (nheaders > 0 || samples ) { |
103 | 110 | bcf_hdr_format(hdr, 0, &str); |
104 | 111 | char *lim = str.s; |
105 | 112 | uint64_t n; |
| 113 | + int samples_printed = 0; |
106 | 114 | for (n = 0; n < nheaders; n++) { |
| 115 | + if ( samples && !strncmp(lim,"#CHROM\t",7) ) samples_printed = 1; |
107 | 116 | lim = strchr(lim, '\n'); |
108 | 117 | if (lim) lim++; |
109 | 118 | else break; |
110 | 119 | } |
111 | | - if (lim) *lim = '\0'; |
112 | | - fputs(ks_str(&str), stdout); |
| 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 | + } |
113 | 137 | } |
114 | 138 |
|
115 | 139 | if (nrecords > 0) { |
|
0 commit comments