
Filter monomorphic SNPs in a VCF using bcftools (AC/AN-based)
Source:R/filter_monomorphic_vcf.R
filter_monomorphic_vcf.RdWrapper around bcftools view and bcftools query to:
keep only polymorphic markers in a VCF, and
generate a blacklist of monomorphic sites.
A site is considered polymorphic in the sample if at least one ALT allele has
an allele count strictly between 0 and the total allele count
(INFO/AN). In bcftools expression syntax:
INFO/AC>0 && INFO/AC<INFO/ANThis works for multi-ALT sites: as soon as one ALT allele is segregating (some REF and some ALT\(_i\)), the variant is kept. Sites fixed for REF or fixed for a single ALT allele are dropped as monomorphic.
The function is intended for use on freshly created VCFs where INFO/AC
and INFO/AN are trustworthy (e.g. direct output from FreeBayes or
bcftools). It does not inspect genotypes or GDS, only the VCF INFO fields.
Filter target: VCF variants.
Usage
filter_monomorphic_vcf(
vcf,
out.vcf = NULL,
blacklist.file = NULL,
bcftools.path = "bcftools",
compress = TRUE,
index = TRUE,
verbose = TRUE
)Arguments
- vcf
(character) Path to the input VCF (
.vcfor.vcf.gz).- out.vcf
(optional, character) Output VCF filename for polymorphic markers. If
NULL, the function generates a filename from the input VCF root by appending".polymorphic.vcf"and".gz"whencompress = TRUE. Default:out.vcf = NULL.- blacklist.file
(optional, character) Path to a TSV file storing monomorphic markers (
CHROM,POS,ID). IfNULL, the function generates a filename from the VCF root by appending".monomorphic_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.- verbose
(logical) Show progress messages and bcftools command lines. Default:
verbose = TRUE.
Value
Invisibly returns a list with:
polymorphic.vcf– path to the filtered VCF;blacklist– path to the monomorphic marker list;log– path to the bcftools log file.
Details
Important distinction — allele-level vs genotype-level polymorphism
filter_monomorphic_vcf identifies polymorphic sites solely from VCF
INFO fields (AC and AN), using the bcftools expression:
INFO/AC>0 && INFO/AC<INFO/ANA variant is kept if at least one ALT allele has a non-zero allele count and is not fixed in the sample. This is an allele-level definition of polymorphism:
If some chromosomes carry REF and some carry ALT\(_i\), the variant is considered polymorphic—even if all individuals share the same genotype (e.g., all REF/ALT heterozygotes).
This pre-filtering step is intentionally lightweight and designed for VCF-level slimming before merging for example scaffolds. It does not inspect genotypes and does not attempt to detect genotype-level monomorphism.
As a result, the set of monomorphic markers detected here may differ from
those detected later inside a GDS by filter_monomorphic.
Note
Why results differ between filter_monomorphic_vcf and
filter_monomorphic
These functions intentionally implement two different biological definitions:
filter_monomorphic_vcfremoves sites that are allele-monomorphic, based on INFO/AC and INFO/AN.filter_monomorphicremoves sites that are genotype-monomorphic, based on the distribution of genotype phenotypes in the GDS.
A variant can contain both REF and ALT alleles (allele-level polymorphism)
but still have only one genotype phenotype across all individuals.
Therefore, it is expected and correct that
filter_monomorphic may remove additional markers.
Author
Thierry Gosselin thierrygosselin@icloud.com