| 1 |
#include "bcftools.pysam.h" |
| 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 |
#include <getopt.h> |
| 29 |
#include <inttypes.h> |
| 30 |
#include <stdio.h> |
| 31 |
#include <stdlib.h> |
| 32 |
#include <unistd.h> |
| 33 |
|
| 34 |
#include <htslib/kstring.h> |
| 35 |
#include <htslib/vcf.h> |
| 36 |
|
| 37 |
#include "bcftools.h" |
| 38 |
|
| 39 |
int main_vcfhead(int argc, char *argv[]) |
| 40 |
{ |
| 41 |
static const char usage[] = |
| 42 |
"\n" |
| 43 |
"About: Displays VCF/BCF headers and optionally the first few variant records\n" |
| 44 |
"Usage: bcftools head [OPTION]... [FILE]\n" |
| 45 |
"\n" |
| 46 |
"Options:\n" |
| 47 |
" -h, --headers INT Display INT header lines [all]\n" |
| 48 |
" -n, --records INT Display INT variant record lines [none]\n" |
| 49 |
" -s, --samples INT Display INT records starting with the #CHROM header line [none]\n" |
| 50 |
"\n"; |
| 51 |
|
| 52 |
static const struct option loptions[] = { |
| 53 |
{ "headers", required_argument, NULL, 'h' }, |
| 54 |
{ "records", required_argument, NULL, 'n' }, |
| 55 |
{ "samples", required_argument, NULL, 's' }, |
| 56 |
{ NULL, 0, NULL, 0 } |
| 57 |
}; |
| 58 |
|
| 59 |
int all_headers = 1; |
| 60 |
int samples = 0; |
| 61 |
uint64_t nheaders = 0; |
| 62 |
uint64_t nrecords = 0; |
| 63 |
|
| 64 |
int c, nargs; |
| 65 |
while ((c = getopt_long(argc, argv, "h:n:s:", loptions, NULL)) >= 0) |
| 66 |
switch (c) { |
| 67 |
case 'h': all_headers = 0; nheaders = strtoull(optarg, NULL, 0); break; |
| 68 |
case 'n': nrecords = strtoull(optarg, NULL, 0); break; |
| 69 |
case 's': nrecords = strtoull(optarg, NULL, 0); samples = 1; break; |
| 70 |
default: |
| 71 |
fputs(usage, bcftools_stderr); |
| 72 |
return EXIT_FAILURE; |
| 73 |
} |
| 74 |
|
| 75 |
if ( samples && all_headers ) all_headers = 0; |
| 76 |
|
| 77 |
nargs = argc - optind; |
| 78 |
if (nargs == 0 && isatty(STDIN_FILENO)) { |
| 79 |
fputs(usage, bcftools_stdout); |
| 80 |
return EXIT_SUCCESS; |
| 81 |
} |
| 82 |
else if (nargs > 1) { |
| 83 |
fputs(usage, bcftools_stderr); |
| 84 |
return EXIT_FAILURE; |
| 85 |
} |
| 86 |
|
| 87 |
const char *fname = (nargs == 1)? argv[optind] : "-"; |
| 88 |
vcfFile *fp = bcf_open(fname, "r"); |
| 89 |
if (fp == NULL) { |
| 90 |
if (strcmp(fname, "-") != 0) |
| 91 |
error_errno("[%s] Can't open \"%s\"", __func__, fname); |
| 92 |
else |
| 93 |
error_errno("[%s] Can't open standard input", __func__); |
| 94 |
} |
| 95 |
|
| 96 |
bcf_hdr_t *hdr = bcf_hdr_read(fp); |
| 97 |
if (hdr == NULL) { |
| 98 |
bcf_close(fp); |
| 99 |
if (strcmp(fname, "-") != 0) |
| 100 |
error("[%s] Can't read headers from \"%s\"\n", __func__, fname); |
| 101 |
else |
| 102 |
error("[%s] Can't read headers\n", __func__); |
| 103 |
} |
| 104 |
|
| 105 |
kstring_t str = KS_INITIALIZE; |
| 106 |
|
| 107 |
if (all_headers) { |
| 108 |
bcf_hdr_format(hdr, 0, &str); |
| 109 |
fputs(ks_str(&str), bcftools_stdout); |
| 110 |
} |
| 111 |
else if (nheaders > 0 || samples ) { |
| 112 |
bcf_hdr_format(hdr, 0, &str); |
| 113 |
char *lim = str.s; |
| 114 |
uint64_t n; |
| 115 |
int samples_printed = 0; |
| 116 |
for (n = 0; n < nheaders; n++) { |
| 117 |
if ( samples && !strncmp(lim,"#CHROM\t",7) ) samples_printed = 1; |
| 118 |
lim = strchr(lim, '\n'); |
| 119 |
if (lim) lim++; |
| 120 |
else break; |
| 121 |
} |
| 122 |
if ( nheaders ) |
| 123 |
{ |
| 124 |
char tmp; |
| 125 |
if (lim) { tmp = *lim; *lim = '\0'; } |
| 126 |
fputs(ks_str(&str), bcftools_stdout); |
| 127 |
if (lim) *lim = tmp; |
| 128 |
} |
| 129 |
if ( lim && samples && !samples_printed ) |
| 130 |
{ |
| 131 |
while ( lim && *lim ) |
| 132 |
{ |
| 133 |
if ( !strncmp(lim,"#CHROM\t",7) ) { fputs(lim, bcftools_stdout); break; } |
| 134 |
lim = strchr(lim, '\n'); |
| 135 |
if (lim) lim++; |
| 136 |
else break; |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
if (nrecords > 0) { |
| 142 |
bcf1_t *rec = bcf_init(); |
| 143 |
uint64_t n; |
| 144 |
for (n = 0; n < nrecords && bcf_read(fp, hdr, rec) >= 0; n++) { |
| 145 |
ks_clear(&str); |
| 146 |
if (vcf_format(hdr, rec, &str) >= 0) |
| 147 |
fputs(ks_str(&str), bcftools_stdout); |
| 148 |
else |
| 149 |
fprintf(bcftools_stderr, "[%s] Record #%"PRIu64 " is invalid\n", __func__, n+1); |
| 150 |
} |
| 151 |
bcf_destroy(rec); |
| 152 |
} |
| 153 |
|
| 154 |
ks_free(&str); |
| 155 |
bcf_hdr_destroy(hdr); |
| 156 |
bcf_close(fp); |
| 157 |
|
| 158 |
return EXIT_SUCCESS; |
| 159 |
} |