
Filter SNPs in a VCF based on genotyping / missing rate (bcftools)
Source:R/filter_genotyping_vcf.R
filter_genotyping_vcf.RdWrapper around bcftools +fill-tags and bcftools view/query to:
keep only markers with missing proportion below or equal to a user-defined threshold, and
generate a blacklist of markers above that threshold.
The missing proportion per site is taken from the F_MISSING INFO tag,
computed by bcftools +fill-tags. A variant is kept if:
F_MISSING <= genotyping.thresholdThis makes the function well suited as a lightweight, VCF-level pruning step (e.g. before merging scaffold-level VCFs), without opening a GDS.
Filter target: VCF variants.
Usage
filter_genotyping_vcf(
vcf,
genotyping.threshold,
out.vcf = NULL,
blacklist.file = NULL,
bcftools.path = "bcftools",
compress = TRUE,
index = TRUE,
keep.filled.vcf = FALSE,
verbose = TRUE
)Arguments
- vcf
(character) Path to the input VCF (
.vcfor.vcf.gz).- genotyping.threshold
(double) Maximum allowed missing proportion per site (i.e.
F_MISSING). For example,genotyping.threshold = 0.5keeps markers with at most 50 percent missing genotypes. Must be between 0 and 1.- out.vcf
(optional, character) Output VCF filename for filtered markers. If
NULL, the function generates a filename from the input VCF root by appending".genotyping.vcf"and".gz"whencompress = TRUE. Default:out.vcf = NULL.- blacklist.file
(optional, character) Path to a TSV file storing blacklisted markers (
CHROM,POS,ID). IfNULL, the function generates a filename from the VCF root by appending".genotyping_blacklist.tsv". Default:blacklist.file = NULL.- bcftools.path
(character) Path or name of the
bcftoolsexecutable (e.g."bcftools"or a full path inside a conda environment). Default:bcftools.path = "bcftools".- compress
(logical) Compress the output VCF using bgzip (
-Oz). Default:compress = TRUE.- index
(logical) Index the output VCF with tabix when
compress = TRUE. Default:index = TRUE.- keep.filled.vcf
(logical) Keep the intermediate VCF with
F_MISSINGfilled-in (the output ofbcftools +fill-tags)? IfFALSE, the function removes this file and its index at the end. Default:keep.filled.vcf = FALSE.- verbose
(logical) Show progress messages and bcftools command lines. Default:
verbose = TRUE.
Value
Invisibly returns a list with:
genotyping.vcf– path to the filtered VCF;blacklist– path to the blacklist of high-missing markers;log– path to the bcftools log file;filled.vcf– path to the intermediate VCF withF_MISSING(may have been deleted ifkeep.filled.vcf = FALSE).
Details
The function operates entirely at the VCF level:
bcftools +fill-tagsis used to computeF_MISSINGfor each site (if not already present);bcftools view -i 'F_MISSING<=X'keeps well-genotyped markers;bcftools query -i 'F_MISSING>X'writes a blacklist of removed markers.
This is conceptually similar to filter_genotyping, but:
filter_genotyping_vcfworks on the raw VCF usingF_MISSING(per-site missing proportion),filter_genotypingworks on a GDS and uses the full radr statistics and plotting machinery.
Author
Thierry Gosselin thierrygosselin@icloud.com