upstream-dependencies/bcftools/vcfview.c
1/* vcfview.c -- VCF/BCF conversion, view, subset and filter VCF/BCF files.
2
3 Copyright (C) 2013-2023 Genome Research Ltd.
4
5 Author: Shane McCarthy <sm15@sanger.ac.uk>
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software and associated documentation files (the "Software"), to deal
9in the Software without restriction, including without limitation the rights
10to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11copies of the Software, and to permit persons to whom the Software is
12furnished to do so, subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in
15all copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23THE SOFTWARE. */
24
25#include <stdio.h>
26#include <strings.h>
27#include <unistd.h>
28#include <getopt.h>
29#include <ctype.h>
30#include <string.h>
31#include <errno.h>
32#include <sys/stat.h>
33#include <sys/types.h>
34#include <math.h>
35#include <inttypes.h>
36#include <htslib/vcf.h>
37#include <htslib/synced_bcf_reader.h>
38#include <htslib/vcfutils.h>
39#include <htslib/kbitset.h>
40#include "bcftools.h"
41#include "filter.h"
42#include "htslib/khash_str2int.h"
43
44#define FLT_INCLUDE 1
45#define FLT_EXCLUDE 2
46
47#define ALLELE_NONREF 1
48#define ALLELE_MINOR 2
49#define ALLELE_ALT1 3
50#define ALLELE_MAJOR 4
51#define ALLELE_NONMAJOR 5
52
53#define GT_NEED_HOM 1
54#define GT_NEED_HET 2
55#define GT_NO_HOM 3
56#define GT_NO_HET 4
57#define GT_NEED_MISSING 5
58#define GT_NO_MISSING 6
59
60typedef struct _args_t
61{
62 filter_t *filter;
63 char *filter_str;
64 int filter_logic; // one of FLT_INCLUDE/FLT_EXCLUDE (-i or -e)
65
66 bcf_srs_t *files;
67 bcf_hdr_t *hdr, *hnull, *hsub; // original header, sites-only header, subset header
68 char **argv, *format, *sample_names, *subset_fname, *targets_list, *regions_list;
69 int regions_overlap, targets_overlap;
70 int argc, clevel, n_threads, output_type, print_header, update_info, header_only, n_samples, *imap, calc_ac;
71 int trim_alts, sites_only, known, novel, min_alleles, max_alleles, private_vars, uncalled, phased;
72 int min_ac, min_ac_type, max_ac, max_ac_type, min_af_type, max_af_type, gt_type;
73 int *ac, mac;
74 float min_af, max_af;
75 char *fn_ref, *fn_out, **samples;
76 int sample_is_file, force_samples;
77 char *include_types, *exclude_types;
78 int include, exclude;
79 int record_cmd_line;
80 char *index_fn;
81 int write_index;
82 int trim_star_allele;
83 htsFile *out;
84}
85args_t;
86
87static void init_data(args_t *args)
88{
89 int i;
90 args->hdr = args->files->readers[0].header;
91
92 if (args->calc_ac && args->update_info)
93 {
94 if (bcf_hdr_append(args->hdr,"##INFO=<ID=AC,Number=A,Type=Integer,Description=\"Allele count in genotypes\">") < 0)
95 error_errno("[%s] Failed to add \"AC\" INFO header", __func__);
96 if (bcf_hdr_append(args->hdr,"##INFO=<ID=AN,Number=1,Type=Integer,Description=\"Total number of alleles in called genotypes\">") < 0)
97 error_errno("[%s] Failed to add \"AN\" INFO header", __func__);
98 }
99 if (args->record_cmd_line) bcf_hdr_append_version(args->hdr, args->argc, args->argv, "bcftools_view");
100 else if (bcf_hdr_sync(args->hdr) < 0)
101 error_errno("[%s] Failed to update header", __func__);
102
103 // setup sample data
104 if (args->sample_names)
105 {
106 void *hdr_samples = khash_str2int_init();
107 for (i=0; i<bcf_hdr_nsamples(args->hdr); i++)
108 khash_str2int_inc(hdr_samples, bcf_hdr_int2id(args->hdr,BCF_DT_SAMPLE,i));
109
110 void *exclude = (args->sample_names[0]=='^') ? khash_str2int_init() : NULL;
111 int nsmpl;
112 char **smpl = NULL;
113 args->samples = NULL; args->n_samples = 0;
114 smpl = hts_readlist(exclude ? &args->sample_names[1] : args->sample_names, args->sample_is_file, &nsmpl);
115 if ( !smpl )
116 {
117 error("Could not read the list: \"%s\"\n", exclude ? &args->sample_names[1] : args->sample_names);
118 }
119
120 if ( exclude )
121 {
122 for (i=0; i<nsmpl; i++) {
123 if (!khash_str2int_has_key(hdr_samples,smpl[i])) {
124 if (args->force_samples) {
125 fprintf(stderr, "Warn: exclude called for sample that does not exist in header: \"%s\"... skipping\n", smpl[i]);
126 } else {
127 error("Error: exclude called for sample that does not exist in header: \"%s\". Use \"--force-samples\" to ignore this error.\n", smpl[i]);
128 }
129 }
130 khash_str2int_inc(exclude, smpl[i]);
131 }
132
133 for (i=0; i<bcf_hdr_nsamples(args->hdr); i++)
134 {
135 if ( exclude && khash_str2int_has_key(exclude,bcf_hdr_int2id(args->hdr,BCF_DT_SAMPLE,i)) ) continue;
136 args->samples = (char**) realloc(args->samples, (args->n_samples+1)*sizeof(const char*));
137 args->samples[args->n_samples++] = strdup(bcf_hdr_int2id(args->hdr,BCF_DT_SAMPLE,i));
138 }
139 khash_str2int_destroy(exclude);
140 }
141 else
142 {
143 for (i=0; i<nsmpl; i++) {
144 if (!khash_str2int_has_key(hdr_samples,smpl[i])) {
145 if (args->force_samples) {
146 fprintf(stderr, "Warn: subset called for sample that does not exist in header: \"%s\"... skipping\n", smpl[i]);
147 continue;
148 } else {
149 error("Error: subset called for sample that does not exist in header: \"%s\". Use \"--force-samples\" to ignore this error.\n", smpl[i]);
150 }
151 }
152 args->samples = (char**) realloc(args->samples, (args->n_samples+1)*sizeof(const char*));
153 args->samples[args->n_samples++] = strdup(smpl[i]);
154 }
155 }
156 for (i=0; i<nsmpl; i++) free(smpl[i]);
157 free(smpl);
158 khash_str2int_destroy(hdr_samples);
159 if (args->n_samples == 0) {
160 fprintf(stderr, "Warn: subsetting has removed all samples\n");
161 args->sites_only = 1;
162 }
163 }
164
165 if (args->n_samples)
166 args->imap = (int*)malloc(args->n_samples * sizeof(int));
167
168 // determine variant types to include/exclude
169 if (args->include_types || args->exclude_types) {
170 if (args->include_types && args->exclude_types) {
171 fprintf(stderr, "Error: only supply one of --include-types, --exclude-types options\n");
172 exit(1);
173 }
174 char **type_list = 0;
175 int m = 0, n = 0;
176 const char *q, *p;
177 for (q = p = args->include_types ? args->include_types : args->exclude_types;; ++p) {
178 if (*p == ',' || *p == 0) {
179 if (m == n) {
180 m = m? m<<1 : 16;
181 type_list = (char**)realloc(type_list, m * sizeof(char*));
182 }
183 type_list[n] = (char*)calloc(p - q + 1, 1);
184 strncpy(type_list[n++], q, p - q);
185 q = p + 1;
186 if (*p == 0) break;
187 }
188 }
189 type_list = (char**)realloc(type_list, n * sizeof(char*));
190
191 if (args->include_types) {
192 args->include = 0;
193 for (i = 0; i < n; ++i) {
194 if (strcmp(type_list[i], "snps") == 0) args->include |= VCF_SNP<<1;
195 else if (strcmp(type_list[i], "indels") == 0) args->include |= VCF_INDEL<<1;
196 else if (strcmp(type_list[i], "mnps") == 0) args->include |= VCF_MNP<<1;
197 else if (strcmp(type_list[i], "other") == 0) args->include |= VCF_OTHER<<1;
198 else if (strcmp(type_list[i], "ref") == 0) args->include |= VCF_OTHER<<1;
199 else if (strcmp(type_list[i], "bnd") == 0) args->include |= VCF_BND<<1;
200 else {
201 fprintf(stderr, "[E::%s] unknown type\n", type_list[i]);
202 fprintf(stderr, "Accepted types are snps, indels, mnps, other\n");
203 exit(1);
204 }
205 }
206 }
207 if (args->exclude_types) {
208 args->exclude = 0;
209 for (i = 0; i < n; ++i) {
210 if (strcmp(type_list[i], "snps") == 0) args->exclude |= VCF_SNP<<1;
211 else if (strcmp(type_list[i], "indels") == 0) args->exclude |= VCF_INDEL<<1;
212 else if (strcmp(type_list[i], "mnps") == 0) args->exclude |= VCF_MNP<<1;
213 else if (strcmp(type_list[i], "other") == 0) args->exclude |= VCF_OTHER<<1;
214 else if (strcmp(type_list[i], "ref") == 0) args->exclude |= VCF_OTHER<<1;
215 else if (strcmp(type_list[i], "bnd") == 0) args->exclude |= VCF_BND<<1;
216 else {
217 fprintf(stderr, "[E::%s] unknown type\n", type_list[i]);
218 fprintf(stderr, "Accepted types are snps, indels, mnps, other\n");
219 exit(1);
220 }
221 }
222 }
223 for (i = 0; i < n; ++i)
224 free(type_list[i]);
225 free(type_list);
226 }
227
228 char wmode[8];
229 set_wmode(wmode,args->output_type,args->fn_out,args->clevel);
230 args->out = hts_open(args->fn_out ? args->fn_out : "-", wmode);
231 if ( !args->out ) error("%s: %s\n", args->fn_out,strerror(errno));
232 if ( args->n_threads > 0)
233 hts_set_opt(args->out, HTS_OPT_THREAD_POOL, args->files->p);
234
235 // headers: hdr=full header, hsub=subset header, hnull=sites only header
236 if (args->sites_only){
237 args->hnull = bcf_hdr_subset(args->hdr, 0, 0, 0);
238 bcf_hdr_remove(args->hnull, BCF_HL_FMT, NULL);
239 }
240 if (args->n_samples > 0)
241 {
242 args->hsub = bcf_hdr_subset(args->hdr, args->n_samples, args->samples, args->imap);
243 if ( !args->hsub ) error("Error occurred while subsetting samples\n");
244 if ( args->n_samples != bcf_hdr_nsamples(args->hsub) )
245 {
246 int i;
247 for (i=0; i<args->n_samples; i++)
248 if ( args->imap[i]<0 ) error("Error: No such sample: \"%s\"\n", args->samples[i]);
249 }
250 }
251
252 if ( args->filter_str )
253 args->filter = filter_init(args->hdr, args->filter_str);
254}
255
256static void destroy_data(args_t *args)
257{
258 int i;
259 if ( args->imap ) {
260 for (i = 0; i < args->n_samples; ++i)
261 free(args->samples[i]);
262 free(args->samples);
263 free(args->imap);
264 }
265 if (args->hnull) bcf_hdr_destroy(args->hnull);
266 if (args->hsub) bcf_hdr_destroy(args->hsub);
267 if ( args->filter )
268 filter_destroy(args->filter);
269 free(args->ac);
270}
271
272// true if all samples are phased.
273// haploid genotypes are considered phased
274// ./. => not phased, .|. => phased
275int bcf_all_phased(const bcf_hdr_t *header, bcf1_t *line)
276{
277 bcf_unpack(line, BCF_UN_FMT);
278 bcf_fmt_t *fmt_ptr = bcf_get_fmt(header, line, "GT");
279 int all_phased = 1;
280 if ( fmt_ptr )
281 {
282 int i, isample;
283 for (isample=0; isample<line->n_sample; isample++)
284 {
285 int sample_phased = 0;
286 #define BRANCH_INT(type_t,vector_end) { \
287 type_t *p = (type_t*) (fmt_ptr->p + isample*fmt_ptr->size); \
288 for (i=0; i<fmt_ptr->n; i++) \
289 { \
290 if (fmt_ptr->n == 1 || (p[i] == vector_end && i == 1)) { sample_phased = 1; break; } /* haploid phased by definition */ \
291 if ( p[i] == vector_end ) { break; }; /* smaller ploidy */ \
292 if ( bcf_gt_is_missing(p[i]) ) continue; /* missing allele */ \
293 if ((p[i])&1) { \
294 sample_phased = 1; \
295 break; \
296 } \
297 } \
298 }
299 switch (fmt_ptr->type) {
300 case BCF_BT_INT8: BRANCH_INT(int8_t, bcf_int8_vector_end); break;
301 case BCF_BT_INT16: BRANCH_INT(int16_t, bcf_int16_vector_end); break;
302 case BCF_BT_INT32: BRANCH_INT(int32_t, bcf_int32_vector_end); break;
303 default: fprintf(stderr, "[E::%s] todo: fmt_type %d\n", __func__, fmt_ptr->type); exit(1); break;
304 }
305 #undef BRANCH_INT
306 if (!sample_phased) {
307 all_phased = 0;
308 break;
309 }
310 }
311 }
312 return all_phased;
313}
314
315int subset_vcf(args_t *args, bcf1_t *line)
316{
317 if ( args->min_alleles && line->n_allele < args->min_alleles ) return 0; // min alleles
318 if ( args->max_alleles && line->n_allele > args->max_alleles ) return 0; // max alleles
319 if (args->novel || args->known)
320 {
321 if ( args->novel && (line->d.id[0]!='.' || line->d.id[1]!=0) ) return 0; // skip sites which are known, ID != '.'
322 if ( args->known && line->d.id[0]=='.' && line->d.id[1]==0 ) return 0; // skip sites which are novel, ID == '.'
323 }
324
325 if (args->include || args->exclude)
326 {
327 int line_type = bcf_get_variant_types(line);
328 if ( args->include && !((line_type<<1) & args->include) ) return 0; // include only given variant types
329 if ( args->exclude && (line_type<<1) & args->exclude ) return 0; // exclude given variant types
330 }
331
332 if ( args->filter )
333 {
334 int ret = filter_test(args->filter, line, NULL);
335 if ( args->filter_logic==FLT_INCLUDE ) { if ( !ret ) return 0; }
336 else if ( ret ) return 0;
337 }
338
339 hts_expand(int, line->n_allele, args->mac, args->ac);
340 int i, an = 0, non_ref_ac = 0;
341 if (args->calc_ac) {
342 bcf_calc_ac(args->hdr, line, args->ac, BCF_UN_INFO|BCF_UN_FMT); // get original AC and AN values from INFO field if available, otherwise calculate
343 for (i=1; i<line->n_allele; i++)
344 non_ref_ac += args->ac[i];
345 for (i=0; i<line->n_allele; i++)
346 an += args->ac[i];
347 }
348
349 int update_ac = args->calc_ac;
350 if (args->n_samples)
351 {
352 int non_ref_ac_sub = 0, *ac_sub = (int*) calloc(line->n_allele,sizeof(int));
353 bcf_subset(args->hdr, line, args->n_samples, args->imap);
354 if ( args->calc_ac && !bcf_get_fmt(args->hdr,line,"GT") ) update_ac = 0;
355 if ( update_ac )
356 {
357 bcf_calc_ac(args->hsub, line, ac_sub, BCF_UN_FMT); // recalculate AC and AN
358 an = 0;
359 for (i=0; i<line->n_allele; i++) {
360 args->ac[i] = ac_sub[i];
361 an += ac_sub[i];
362 }
363 for (i=1; i<line->n_allele; i++)
364 non_ref_ac_sub += ac_sub[i];
365 if (args->private_vars) {
366 if (args->private_vars == FLT_INCLUDE && !(non_ref_ac_sub > 0 && non_ref_ac == non_ref_ac_sub)) { free(ac_sub); return 0; } // select private sites
367 if (args->private_vars == FLT_EXCLUDE && non_ref_ac_sub > 0 && non_ref_ac == non_ref_ac_sub) { free(ac_sub); return 0; } // exclude private sites
368 }
369 non_ref_ac = non_ref_ac_sub;
370 }
371 free(ac_sub);
372 }
373
374 bcf_fmt_t *gt_fmt;
375 if ( args->gt_type && (gt_fmt=bcf_get_fmt(args->hdr,line,"GT")) )
376 {
377 int nhet = 0, nhom = 0, nmiss = 0;
378 for (i=0; i<bcf_hdr_nsamples(args->hdr); i++)
379 {
380 int type = bcf_gt_type(gt_fmt,i,NULL,NULL);
381 if ( type==GT_HET_RA || type==GT_HET_AA )
382 {
383 if ( args->gt_type==GT_NO_HET ) return 0;
384 nhet = 1;
385 }
386 else if ( type==GT_UNKN )
387 {
388 if ( args->gt_type==GT_NO_MISSING ) return 0;
389 nmiss = 1;
390 }
391 else
392 {
393 if ( args->gt_type==GT_NO_HOM ) return 0;
394 nhom = 1;
395 }
396 }
397 if ( args->gt_type==GT_NEED_HOM && !nhom ) return 0;
398 else if ( args->gt_type==GT_NEED_HET && !nhet ) return 0;
399 else if ( args->gt_type==GT_NEED_MISSING && !nmiss ) return 0;
400 }
401
402 int minor_ac = 0;
403 int major_ac = 0;
404 if ( args->calc_ac )
405 {
406 minor_ac = args->ac[0];
407 major_ac = args->ac[0];
408 for (i=1; i<line->n_allele; i++){
409 if (args->ac[i] < minor_ac) { minor_ac = args->ac[i]; }
410 if (args->ac[i] > major_ac) { major_ac = args->ac[i]; }
411 }
412 }
413
414 if (args->min_ac!=-1)
415 {
416 if (args->min_ac_type == ALLELE_NONREF && args->min_ac>non_ref_ac) return 0; // min AC
417 else if (args->min_ac_type == ALLELE_MINOR && args->min_ac>minor_ac) return 0; // min minor AC
418 else if (args->min_ac_type == ALLELE_ALT1 && args->min_ac>args->ac[1]) return 0; // min 1st alternate AC
419 else if (args->min_ac_type == ALLELE_MAJOR && args->min_ac > major_ac) return 0; // min major AC
420 else if (args->min_ac_type == ALLELE_NONMAJOR && args->min_ac > an-major_ac) return 0; // min non-major AC
421 }
422 if (args->max_ac!=-1)
423 {
424 if (args->max_ac_type == ALLELE_NONREF && args->max_ac<non_ref_ac) return 0; // max AC
425 else if (args->max_ac_type == ALLELE_MINOR && args->max_ac<minor_ac) return 0; // max minor AC
426 else if (args->max_ac_type == ALLELE_ALT1 && args->max_ac<args->ac[1]) return 0; // max 1st alternate AC
427 else if (args->max_ac_type == ALLELE_MAJOR && args->max_ac < major_ac) return 0; // max major AC
428 else if (args->max_ac_type == ALLELE_NONMAJOR && args->max_ac < an-major_ac) return 0; // max non-major AC
429 }
430 if (args->min_af!=-1)
431 {
432 if (an == 0) return 0; // freq not defined, skip site
433 if (args->min_af_type == ALLELE_NONREF && args->min_af>non_ref_ac/(double)an) return 0; // min AF
434 else if (args->min_af_type == ALLELE_MINOR && args->min_af>minor_ac/(double)an) return 0; // min minor AF
435 else if (args->min_af_type == ALLELE_ALT1 && args->min_af>args->ac[1]/(double)an) return 0; // min 1st alternate AF
436 else if (args->min_af_type == ALLELE_MAJOR && args->min_af > major_ac/(double)an) return 0; // min major AF
437 else if (args->min_af_type == ALLELE_NONMAJOR && args->min_af > (an-major_ac)/(double)an) return 0; // min non-major AF
438 }
439 if (args->max_af!=-1)
440 {
441 if (an == 0) return 0; // freq not defined, skip site
442 if (args->max_af_type == ALLELE_NONREF && args->max_af<non_ref_ac/(double)an) return 0; // max AF
443 else if (args->max_af_type == ALLELE_MINOR && args->max_af<minor_ac/(double)an) return 0; // max minor AF
444 else if (args->max_af_type == ALLELE_ALT1 && args->max_af<args->ac[1]/(double)an) return 0; // max 1st alternate AF
445 else if (args->max_af_type == ALLELE_MAJOR && args->max_af < major_ac/(double)an) return 0; // max major AF
446 else if (args->max_af_type == ALLELE_NONMAJOR && args->max_af < (an-major_ac)/(double)an) return 0; // max non-major AF
447 }
448 if (args->uncalled) {
449 if (args->uncalled == FLT_INCLUDE && an > 0) return 0; // select uncalled
450 if (args->uncalled == FLT_EXCLUDE && an == 0) return 0; // skip if uncalled
451 }
452 if (update_ac && args->update_info) {
453 bcf_update_info_int32(args->hdr, line, "AC", &args->ac[1], line->n_allele-1);
454 bcf_update_info_int32(args->hdr, line, "AN", &an, 1);
455 }
456 if (args->trim_alts)
457 {
458 int ret = bcf_trim_alleles(args->hsub ? args->hsub : args->hdr, line);
459 if ( ret<0 ) error("Error: Could not trim alleles at %s:%"PRId64"\n", bcf_seqname(args->hsub ? args->hsub : args->hdr, line), (int64_t) line->pos+1);
460 }
461 if (args->trim_star_allele)
462 {
463 int iunseen;
464 if ( args->trim_star_allele && (line->n_allele > 2 || args->trim_star_allele > 1) && (iunseen=get_unseen_allele(line)) && iunseen>0 )
465 {
466 // the unobserved star allele should be trimmed, either it is variant site or trimming of all sites was requested
467 kbitset_t *rm_set = kbs_init(line->n_allele);
468 kbs_insert(rm_set, iunseen);
469 if ( bcf_remove_allele_set(args->hdr,line,rm_set) )
470 error("[%s] Error: failed to trim the unobserved allele at %s:%"PRIhts_pos"\n",__func__,bcf_seqname(args->hdr,line),line->pos+1);
471 kbs_destroy(rm_set);
472 }
473 }
474 if (args->phased) {
475 int phased = bcf_all_phased(args->hdr, line);
476 if (args->phased == FLT_INCLUDE && !phased) { return 0; } // skip unphased
477 if (args->phased == FLT_EXCLUDE && phased) { return 0; } // skip phased
478 }
479 if (args->sites_only) bcf_subset(args->hsub ? args->hsub : args->hdr, line, 0, 0);
480 return 1;
481}
482
483void set_allele_type (int *atype, char *atype_string)
484{
485 *atype = ALLELE_NONREF;
486 if (strcmp(atype_string, "minor") == 0) {
487 *atype = ALLELE_MINOR;
488 }
489 else if (strcmp(atype_string, "alt1") == 0) {
490 *atype = ALLELE_ALT1;
491 }
492 else if (strcmp(atype_string, "nref") == 0) {
493 *atype = ALLELE_NONREF;
494 }
495 else if (strcmp(atype_string, "major") == 0) {
496 *atype = ALLELE_MAJOR;
497 }
498 else if (strcmp(atype_string, "nonmajor") == 0) {
499 *atype = ALLELE_NONMAJOR;
500 }
501 else {
502 error("Error: allele type not recognised. Expected one of nref|alt1|minor|major|nonmajor, got \"%s\".\n", atype_string);
503 }
504}
505
506static void usage(args_t *args)
507{
508 fprintf(stderr, "\n");
509 fprintf(stderr, "About: VCF/BCF conversion, view, subset and filter VCF/BCF files.\n");
510 fprintf(stderr, "Usage: bcftools view [options] <in.vcf.gz> [region1 [...]]\n");
511 fprintf(stderr, "\n");
512 fprintf(stderr, "Output options:\n");
513 fprintf(stderr, " -G, --drop-genotypes Drop individual genotype information (after subsetting if -s option set)\n");
514 fprintf(stderr, " -h, --header-only Print only the header in VCF output (equivalent to bcftools head)\n");
515 fprintf(stderr, " -H, --no-header Suppress the header in VCF output\n");
516 fprintf(stderr, " --with-header Print both header and records in VCF output [default]\n");
517 fprintf(stderr, " -l, --compression-level [0-9] Compression level: 0 uncompressed, 1 best speed, 9 best compression [%d]\n", args->clevel);
518 fprintf(stderr, " --no-version Do not append version and command line to the header\n");
519 fprintf(stderr, " -o, --output FILE Output file name [stdout]\n");
520 fprintf(stderr, " -O, --output-type u|b|v|z[0-9] u/b: un/compressed BCF, v/z: un/compressed VCF, 0-9: compression level [v]\n");
521 fprintf(stderr, " -r, --regions REGION Restrict to comma-separated list of regions\n");
522 fprintf(stderr, " -R, --regions-file FILE Restrict to regions listed in FILE\n");
523 fprintf(stderr, " --regions-overlap 0|1|2 Include if POS in the region (0), record overlaps (1), variant overlaps (2) [1]\n");
524 fprintf(stderr, " -t, --targets [^]REGION Similar to -r but streams rather than index-jumps. Exclude regions with \"^\" prefix\n");
525 fprintf(stderr, " -T, --targets-file [^]FILE Similar to -R but streams rather than index-jumps. Exclude regions with \"^\" prefix\n");
526 fprintf(stderr, " --targets-overlap 0|1|2 Include if POS in the region (0), record overlaps (1), variant overlaps (2) [0]\n");
527 fprintf(stderr, " --threads INT Use multithreading with INT worker threads [0]\n");
528 fprintf(stderr, "\n");
529 fprintf(stderr, "Subset options:\n");
530 fprintf(stderr, " -A, --trim-unseen-allele Remove '<*>' or '<NON_REF>' at variant (-A) or at all (-AA) sites\n");
531 fprintf(stderr, " -a, --trim-alt-alleles Trim ALT alleles not seen in the genotype fields (or their subset with -s/-S)\n");
532 fprintf(stderr, " -I, --no-update Do not (re)calculate INFO fields for the subset (currently INFO/AC and INFO/AN)\n");
533 fprintf(stderr, " -s, --samples [^]LIST Comma separated list of samples to include (or exclude with \"^\" prefix). Be careful\n");
534 fprintf(stderr, " when combining filtering with sample subsetting as filtering comes (usually) first.\n");
535 fprintf(stderr, " If unsure, split sample subsetting and filtering in two commands, using -Ou when piping.\n");
536 fprintf(stderr, " -S, --samples-file [^]FILE File of samples to include (or exclude with \"^\" prefix)\n");
537 fprintf(stderr, " --force-samples Only warn about unknown subset samples\n");
538 fprintf(stderr, "\n");
539 fprintf(stderr, "Filter options:\n");
540 fprintf(stderr, " -c/C, --min-ac/--max-ac INT[:TYPE] Minimum/maximum count for non-reference (nref), 1st alternate (alt1), least frequent\n");
541 fprintf(stderr, " (minor), most frequent (major) or sum of all but most frequent (nonmajor) alleles [nref]\n");
542 fprintf(stderr, " -f, --apply-filters LIST Require at least one of the listed FILTER strings (e.g. \"PASS,.\")\n");
543 fprintf(stderr, " -g, --genotype [^]hom|het|miss Require one or more hom/het/missing genotype or, if prefixed with \"^\", exclude such sites\n");
544 fprintf(stderr, " -i/e, --include/--exclude EXPR Select/exclude sites for which the expression is true (see man page for details)\n");
545 fprintf(stderr, " -k/n, --known/--novel Select known/novel sites only (ID is not/is '.')\n");
546 fprintf(stderr, " -m/M, --min-alleles/--max-alleles INT Minimum/maximum number of alleles listed in REF and ALT (e.g. -m2 -M2 for biallelic sites)\n");
547 fprintf(stderr, " -p/P, --phased/--exclude-phased Select/exclude sites where all samples are phased\n");
548 fprintf(stderr, " -q/Q, --min-af/--max-af FLOAT[:TYPE] Minimum/maximum frequency for non-reference (nref), 1st alternate (alt1), least frequent\n");
549 fprintf(stderr, " (minor), most frequent (major) or sum of all but most frequent (nonmajor) alleles [nref]\n");
550 fprintf(stderr, " -u/U, --uncalled/--exclude-uncalled Select/exclude sites without a called genotype\n");
551 fprintf(stderr, " -v/V, --types/--exclude-types LIST Select/exclude comma-separated list of variant types: snps,indels,mnps,ref,bnd,other [null]\n");
552 fprintf(stderr, " -x/X, --private/--exclude-private Select/exclude sites where the non-reference alleles are exclusive (private) to the subset samples\n");
553 fprintf(stderr, " -W, --write-index[=FMT] Automatically index the output files [off]\n");
554 fprintf(stderr, "\n");
555 exit(1);
556}
557
558int main_vcfview(int argc, char *argv[])
559{
560 int c;
561 args_t *args = (args_t*) calloc(1,sizeof(args_t));
562 args->argc = argc; args->argv = argv;
563 args->files = bcf_sr_init();
564 args->clevel = -1;
565 args->print_header = 1;
566 args->update_info = 1;
567 args->output_type = FT_VCF;
568 args->n_threads = 0;
569 args->record_cmd_line = 1;
570 args->write_index = 0;
571 args->min_ac = args->max_ac = args->min_af = args->max_af = -1;
572 args->regions_overlap = 1;
573 args->targets_overlap = 0;
574 int targets_is_file = 0, regions_is_file = 0;
575
576 static struct option loptions[] =
577 {
578 {"genotype",required_argument,NULL,'g'},
579 {"compression-level",required_argument,NULL,'l'},
580 {"threads",required_argument,NULL,9},
581 {"header-only",no_argument,NULL,'h'},
582 {"no-header",no_argument,NULL,'H'},
583 {"with-header",no_argument,NULL,4},
584 {"exclude",required_argument,NULL,'e'},
585 {"include",required_argument,NULL,'i'},
586 {"trim-alt-alleles",no_argument,NULL,'a'},
587 {"trim-unseen-allele",no_argument,NULL,'A'},
588 {"no-update",no_argument,NULL,'I'},
589 {"drop-genotypes",no_argument,NULL,'G'},
590 {"private",no_argument,NULL,'x'},
591 {"exclude-private",no_argument,NULL,'X'},
592 {"uncalled",no_argument,NULL,'u'},
593 {"exclude-uncalled",no_argument,NULL,'U'},
594 {"apply-filters",required_argument,NULL,'f'},
595 {"known",no_argument,NULL,'k'},
596 {"novel",no_argument,NULL,'n'},
597 {"min-alleles",required_argument,NULL,'m'},
598 {"max-alleles",required_argument,NULL,'M'},
599 {"samples",required_argument,NULL,'s'},
600 {"samples-file",required_argument,NULL,'S'},
601 {"force-samples",no_argument,NULL,1},
602 {"output-type",required_argument,NULL,'O'},
603 {"output-file",required_argument,NULL,'o'},
604 {"output",required_argument,NULL,'o'},
605 {"types",required_argument,NULL,'v'},
606 {"exclude-types",required_argument,NULL,'V'},
607 {"targets",required_argument,NULL,'t'},
608 {"targets-file",required_argument,NULL,'T'},
609 {"targets-overlap",required_argument,NULL,2},
610 {"regions",required_argument,NULL,'r'},
611 {"regions-file",required_argument,NULL,'R'},
612 {"regions-overlap",required_argument,NULL,3},
613 {"min-ac",required_argument,NULL,'c'},
614 {"max-ac",required_argument,NULL,'C'},
615 {"min-af",required_argument,NULL,'q'},
616 {"max-af",required_argument,NULL,'Q'},
617 {"phased",no_argument,NULL,'p'},
618 {"exclude-phased",no_argument,NULL,'P'},
619 {"no-version",no_argument,NULL,8},
620 {"write-index",optional_argument,NULL,'W'},
621 {NULL,0,NULL,0}
622 };
623 char *tmp;
624 while ((c = getopt_long(argc, argv, "l:t:T:r:R:o:O:s:S:Gf:knv:V:m:M:aAuUhHc:C:Ii:e:xXpPq:Q:g:W::",loptions,NULL)) >= 0)
625 {
626 char allele_type[9] = "nref";
627 switch (c)
628 {
629 case 'O':
630 switch (optarg[0]) {
631 case 'b': args->output_type = FT_BCF_GZ; break;
632 case 'u': args->output_type = FT_BCF; break;
633 case 'z': args->output_type = FT_VCF_GZ; break;
634 case 'v': args->output_type = FT_VCF; break;
635 default:
636 {
637 args->clevel = strtol(optarg,&tmp,10);
638 if ( *tmp || args->clevel<0 || args->clevel>9 ) error("The output type \"%s\" not recognised\n", optarg);
639 }
640 };
641 if ( optarg[1] )
642 {
643 args->clevel = strtol(optarg+1,&tmp,10);
644 if ( *tmp || args->clevel<0 || args->clevel>9 ) error("Could not parse argument: --compression-level %s\n", optarg+1);
645 }
646 break;
647 case 'l':
648 args->clevel = strtol(optarg,&tmp,10);
649 if ( *tmp ) error("Could not parse argument: --compression-level %s\n", optarg);
650 args->output_type |= FT_GZ;
651 break;
652 case 'o': args->fn_out = optarg; break;
653 case 'H': args->print_header = 0; break;
654 case 'h': args->header_only = 1; break;
655 case 4 : args->print_header = 1; args->header_only = 0; break;
656
657 case 't': args->targets_list = optarg; break;
658 case 'T': args->targets_list = optarg; targets_is_file = 1; break;
659 case 'r': args->regions_list = optarg; break;
660 case 'R': args->regions_list = optarg; regions_is_file = 1; break;
661
662 case 's': args->sample_names = optarg; break;
663 case 'S': args->sample_names = optarg; args->sample_is_file = 1; break;
664 case 1 : args->force_samples = 1; break;
665 case 'a': args->trim_alts = 1; args->calc_ac = 1; break;
666 case 'A': args->trim_star_allele++; break;
667 case 'I': args->update_info = 0; break;
668 case 'G': args->sites_only = 1; break;
669
670 case 'f': args->files->apply_filters = optarg; break;
671 case 'k': args->known = 1; break;
672 case 'n': args->novel = 1; break;
673 case 'm':
674 args->min_alleles = strtol(optarg,&tmp,10);
675 if ( *tmp ) error("Could not parse argument: --min-alleles %s\n", optarg);
676 break;
677 case 'M':
678 args->max_alleles = strtol(optarg,&tmp,10);
679 if ( *tmp ) error("Could not parse argument: --max-alleles %s\n", optarg);
680 break;
681 case 'v': args->include_types = optarg; break;
682 case 'V': args->exclude_types = optarg; break;
683 case 'e':
684 if ( args->filter_str ) error("Error: only one -i or -e expression can be given, and they cannot be combined\n");
685 args->filter_str = optarg; args->filter_logic |= FLT_EXCLUDE; break;
686 case 'i':
687 if ( args->filter_str ) error("Error: only one -i or -e expression can be given, and they cannot be combined\n");
688 args->filter_str = optarg; args->filter_logic |= FLT_INCLUDE; break;
689 case 'c':
690 {
691 args->min_ac_type = ALLELE_NONREF;
692 if ( sscanf(optarg,"%d:%8s",&args->min_ac, allele_type)!=2 && sscanf(optarg,"%d",&args->min_ac)!=1 )
693 error("Error: Could not parse --min-ac %s\n", optarg);
694 set_allele_type(&args->min_ac_type, allele_type);
695 args->calc_ac = 1;
696 break;
697 }
698 case 'C':
699 {
700 args->max_ac_type = ALLELE_NONREF;
701 if ( sscanf(optarg,"%d:%8s",&args->max_ac, allele_type)!=2 && sscanf(optarg,"%d",&args->max_ac)!=1 )
702 error("Error: Could not parse --max-ac %s\n", optarg);
703 set_allele_type(&args->max_ac_type, allele_type);
704 args->calc_ac = 1;
705 break;
706 }
707 case 'q':
708 {
709 args->min_af_type = ALLELE_NONREF;
710 if ( sscanf(optarg,"%f:%8s",&args->min_af, allele_type)!=2 && sscanf(optarg,"%f",&args->min_af)!=1 )
711 error("Error: Could not parse --min-af %s\n", optarg);
712 set_allele_type(&args->min_af_type, allele_type);
713 args->calc_ac = 1;
714 break;
715 }
716 case 'Q':
717 {
718 args->max_af_type = ALLELE_NONREF;
719 if ( sscanf(optarg,"%f:%8s",&args->max_af, allele_type)!=2 && sscanf(optarg,"%f",&args->max_af)!=1 )
720 error("Error: Could not parse --max-af %s\n", optarg);
721 set_allele_type(&args->max_af_type, allele_type);
722 args->calc_ac = 1;
723 break;
724 }
725
726 case 'x': args->private_vars |= FLT_INCLUDE; args->calc_ac = 1; break;
727 case 'X': args->private_vars |= FLT_EXCLUDE; args->calc_ac = 1; break;
728 case 'u': args->uncalled |= FLT_INCLUDE; args->calc_ac = 1; break;
729 case 'U': args->uncalled |= FLT_EXCLUDE; args->calc_ac = 1; break;
730 case 'p': args->phased |= FLT_INCLUDE; break; // phased
731 case 'P': args->phased |= FLT_EXCLUDE; break; // exclude-phased
732 case 'g':
733 {
734 if ( !strcasecmp(optarg,"hom") ) args->gt_type = GT_NEED_HOM;
735 else if ( !strcasecmp(optarg,"het") ) args->gt_type = GT_NEED_HET;
736 else if ( !strcasecmp(optarg,"miss") ) args->gt_type = GT_NEED_MISSING;
737 else if ( !strcasecmp(optarg,"^hom") ) args->gt_type = GT_NO_HOM;
738 else if ( !strcasecmp(optarg,"^het") ) args->gt_type = GT_NO_HET;
739 else if ( !strcasecmp(optarg,"^miss") ) args->gt_type = GT_NO_MISSING;
740 else error("The argument to -g not recognised. Expected one of hom/het/miss/^hom/^het/^miss, got \"%s\".\n", optarg);
741 break;
742 }
743 case 2 :
744 args->targets_overlap = parse_overlap_option(optarg);
745 if ( args->targets_overlap < 0 ) error("Could not parse: --targets-overlap %s\n",optarg);
746 break;
747 case 3 :
748 args->regions_overlap = parse_overlap_option(optarg);
749 if ( args->regions_overlap < 0 ) error("Could not parse: --regions-overlap %s\n",optarg);
750 break;
751 case 9 : args->n_threads = strtol(optarg, 0, 0); break;
752 case 8 : args->record_cmd_line = 0; break;
753 case 'W':
754 if (!(args->write_index = write_index_parse(optarg)))
755 error("Unsupported index format '%s'\n", optarg);
756 break;
757 case '?': usage(args); break;
758 default: error("Unknown argument: %s\n", optarg);
759 }
760 }
761
762 if ( args->filter_logic == (FLT_EXCLUDE|FLT_INCLUDE) ) error("Only one of -i or -e can be given.\n");
763 if ( args->private_vars > FLT_EXCLUDE ) error("Only one of -x or -X can be given.\n");
764 if ( args->uncalled > FLT_EXCLUDE ) error("Only one of -u or -U can be given.\n");
765 if ( args->phased > FLT_EXCLUDE ) error("Only one of -p or -P can be given.\n");
766
767 if ( args->sample_names && args->update_info) args->calc_ac = 1;
768
769 char *fname = NULL;
770 if ( optind>=argc )
771 {
772 if ( !isatty(fileno((FILE *)stdin)) ) fname = "-"; // reading from stdin
773 else usage(args);
774 }
775 else fname = argv[optind];
776
777 // read in the regions from the command line
778 if ( args->regions_list )
779 {
780 bcf_sr_set_opt(args->files,BCF_SR_REGIONS_OVERLAP,args->regions_overlap);
781 if ( bcf_sr_set_regions(args->files, args->regions_list, regions_is_file)<0 )
782 error("Failed to read the regions: %s\n", args->regions_list);
783 }
784 else if ( optind+1 < argc )
785 {
786 int i;
787 kstring_t tmp = {0,0,0};
788 kputs(argv[optind+1],&tmp);
789 for (i=optind+2; i<argc; i++) { kputc(',',&tmp); kputs(argv[i],&tmp); }
790 if ( bcf_sr_set_regions(args->files, tmp.s, 0)<0 )
791 error("Failed to read the regions: %s\n", tmp.s);
792 free(tmp.s);
793 }
794 if ( args->targets_list )
795 {
796 bcf_sr_set_opt(args->files,BCF_SR_TARGETS_OVERLAP,args->targets_overlap);
797 if ( bcf_sr_set_targets(args->files, args->targets_list, targets_is_file, 0)<0 )
798 error("Failed to read the targets: %s\n", args->targets_list);
799 }
800
801 if ( bcf_sr_set_threads(args->files, args->n_threads)<0 ) error("Failed to create threads\n");
802 if ( !bcf_sr_add_reader(args->files, fname) ) error("Failed to read from %s: %s\n", !strcmp("-",fname)?"standard input":fname,bcf_sr_strerror(args->files->errnum));
803
804 init_data(args);
805 bcf_hdr_t *out_hdr = args->hnull ? args->hnull : (args->hsub ? args->hsub : args->hdr);
806 if (args->print_header)
807 {
808 if ( bcf_hdr_write(args->out, out_hdr)!=0 ) error("[%s] Error: cannot write to %s\n", __func__,args->fn_out);
809 }
810 else if ( args->output_type & FT_BCF )
811 error("BCF output requires header, cannot proceed with -H\n");
812
813 if ( init_index2(args->out,out_hdr,args->fn_out, &args->index_fn,
814 args->write_index) < 0 )
815 error("Error: failed to initialise index for %s\n",args->fn_out);
816
817 int ret = 0;
818 if (!args->header_only)
819 {
820 while ( bcf_sr_next_line(args->files) )
821 {
822 bcf1_t *line = args->files->readers[0].buffer[0];
823 if ( line->errcode && out_hdr!=args->hdr ) error("Undefined tags in the header, cannot proceed in the sample subset mode.\n");
824 if ( subset_vcf(args, line) && bcf_write1(args->out, out_hdr, line)!=0 ) error("[%s] Error: cannot write to %s\n", __func__,args->fn_out);
825 }
826 ret = args->files->errnum;
827 if ( ret ) fprintf(stderr,"Error: %s\n", bcf_sr_strerror(args->files->errnum));
828 }
829
830 if (args->write_index)
831 {
832 if (bcf_idx_save(args->out) < 0)
833 {
834 if ( hts_close(args->out)!=0 ) error("Error: close failed %s\n", args->fn_out?args->fn_out:"stdout");
835 error("Error: cannot write to index %s\n", args->index_fn);
836 }
837 free(args->index_fn);
838 }
839
840 if ( hts_close(args->out)!=0 ) error("Error: close failed %s\n", args->fn_out?args->fn_out:"stdout");
841 destroy_data(args);
842 bcf_sr_destroy(args->files);
843 free(args);
844 return ret;
845}