Introduction#
sfsutils is a Python toolkit for deriving site-frequency spectra (SFS) from raw variant data. It provides a configurable variant-to-SFS parser together with the annotations and filters commonly required to prepare population-genetic data for downstream analysis. The package is object-oriented and thoroughly documented, and because it applies the same polarisation, filtering, and projection to a common sample size across datasets, the resulting spectra are directly comparable.
Motivation#
The SFS condenses population genetic variation by quantifying the number of alleles observed at each frequency, and is the input to a wide range of population-genetic methods. Obtaining a spectrum from real data is rarely a single step: sites must be polarised against an ancestral state, stratified into meaningful categories (for example by degeneracy or synonymy), and filtered to remove sites that violate modelling assumptions. Because comparisons across species, populations, or genomic regions are only meaningful when the spectra are derived in a consistent manner, sfsutils collects these operations behind a single, reproducible parsing interface.
How it works#
sfsutils reads variants from VCF files, VCF-Zarr stores, or tskit tree sequences (ARGs) through a single streamed site interface, and counts derived-allele frequencies into one or more spectra. Rather than depending on pre-annotated input, it can derive the required site-level information itself. Ancestral alleles may be taken from an existing AA info tag, or inferred from one or more outgroups either by maximum parsimony or under a maximum-likelihood substitution model; site degeneracy and synonymy can be computed directly from FASTA and GFF references. The same annotations let the parser stratify sites as it counts them, producing a separate spectrum per category, for example a synonymous and a non-synonymous spectrum, or one spectrum per degeneracy class. A collection of filters excludes sites that violate downstream modelling assumptions, including non-biallelic and non-SNV sites, sites outside coding sequences, CpG sites, and sites affected by GC-biased gene conversion or by deviant outgroups. Beyond the ordinary one-dimensional spectrum, the same parser can also derive the joint SFS across several populations and the two-site SFS of linked pairs of sites. All of these spectra support the operations needed to inspect and reshape them, including folding, polarising, marginalising, resampling, and visualisation. Parsing, filtering, and annotating are also available from the command line (see the CLI reference).
Features#
sfsutils offers a range of features for preparing site-frequency spectra.
Parsing: streamlining the extraction of spectra from raw variant data:
Built-in variant-to-SFS parser, with support for versatile stratification
Joint site-frequency spectra across several populations and two-site spectra of linked pairs of sites
Stratification by degeneracy, synonymy, base transition or transversion type, ancestral base, genomic context, contig, and more
Utilities to determine the number of mutational target sites when monomorphic sites are not present in the provided input
Serialization of spectra and parser configurations
A command-line interface for parsing, filtering, and annotating
Object-oriented and customizable design
Annotation: adding the site-level information required to build meaningful spectra:
Site-degeneracy annotation from FASTA and GFF references
Ancestral-allele annotation, either by maximum parsimony or by a maximum-likelihood model using one or more outgroups
Synonymy annotation of coding variants
Filtration: removing sites that violate downstream modelling assumptions:
Filtering of non-biallelic and non-SNV sites
Restriction to coding sequences
Removal of sites affected by GC-biased gene conversion or by deviant outgroups
Visualization: plotting utilities for inspecting spectra:
Visualization of individual and stratified spectra
Contents#
Python Reference
R Reference
CLI Reference
API Reference
Miscellaneous
References#
Matthias Birkner, Jochen Blath, and Bjarki Eldon. Statistical properties of the site-frequency spectrum associated with Λ-coalescents. Genetics, 195(3):1037–1053, 2013. doi:10.1534/genetics.113.156612.
Eliot F. Fenton, Daniel P. Rice, John Novembre, and Michael M. Desai. Detecting deviations from Kingman coalescence using 2-site frequency spectra. Genetics, 229(4):iyaf023, 2025. doi:10.1093/genetics/iyaf023.
Yun-Xin Fu. Statistical properties of segregating sites. Theoretical Population Biology, 48(2):172–197, 1995. doi:10.1006/tpbi.1995.1025.