SFS simulation#
Simulate a site-frequency spectrum from a given demography and distribution of fitness effects.
Classes:
Simulate an SFS under selection given a DFE and a neutral SFS. |
- class Simulation(sfs_neut: Spectrum, params: dict = None, model: Parametrization | str = 'GammaExpParametrization', intervals_del: Tuple[float, float, int] = (-100000000.0, -1e-05, 1000), intervals_ben: Tuple[float, float, int] = (1e-05, 10000.0, 1000), h_callback: Callable[[float, ndarray], ndarray] = None, integration_mode: Literal['midpoint', 'quad'] = 'midpoint', linearized: bool = True, n_sites_div: float = None, discretization: Discretization = None, parallelize: bool = True)[source]#
Bases:
objectSimulate an SFS under selection given a DFE and a neutral SFS.
Example usage:
import fastdfe as fd # create simulation object by specifying neutral SFS and DFE sim = fd.Simulation( sfs_neut=fd.Simulation.get_neutral_sfs(n=20, n_sites=1e8, theta=1e-4), params=dict(S_d=-300, b=0.3, p_b=0.1, S_b=0.1), model=fd.GammaExpParametrization() ) # perform the simulation sim.run() # plot spectra sim.get_spectra().plot()
Methods:
Create a simulation object.
Simulate an SFS given a DFE.
Get the spectra used in the simulation.
Get the proportion of adaptive substitutions (alpha) under the specified DFE and parameters.
Get omega, the rate of non-synonymous over synonymous substitutions (dN/dS), under the specified DFE and parameters.
Get omega_a, the rate of adaptive non-synonymous over synonymous substitutions (adaptive dN/dS), under the specified DFE and parameters.
Obtain a standard neutral SFS for a given theta and number of sites.
- __init__(sfs_neut: Spectrum, params: dict = None, model: Parametrization | str = 'GammaExpParametrization', intervals_del: Tuple[float, float, int] = (-100000000.0, -1e-05, 1000), intervals_ben: Tuple[float, float, int] = (1e-05, 10000.0, 1000), h_callback: Callable[[float, ndarray], ndarray] = None, integration_mode: Literal['midpoint', 'quad'] = 'midpoint', linearized: bool = True, n_sites_div: float = None, discretization: Discretization = None, parallelize: bool = True)[source]#
Create a simulation object.
- Parameters:
sfs_neut¶ (
Spectrum) – Neutral SFS. This sfs is informative on the population sample size, population mutation rate, the number of sites, and demography.Simulation.get_neutral_sfs()can be used to obtain a neutral SFS.params¶ (
dict) – Parameters of the DFE parametrization (see model) in addition toepsand dominanceh. By default, eps=0, h=0.5, the default parameters ofmodelwill be used.epsis the ancestral-misidentification parameter of the inference model, which enters through the nuisance parameters of the given neutral SFS. The simulated selected SFS thus differs from the one simulated foreps=0withSpectrum.misidentify()applied.model¶ (
Parametrization|str) – DFE parametrization modelintervals_del¶ (
Tuple[float,float,int]) –(start, stop, n_interval)for deleterious population-scaled selection coefficients. The intervals will be log10-spaced. Decreasing the number of intervals to100provides nearly identical results while increasing speed, especially when precomputing across dominance coefficients.intervals_ben¶ (
Tuple[float,float,int]) – Same asintervals_delbut for positive selection coefficients. Decreasing the number of intervals to100provides nearly identical results while increasing speed, especially when precomputing across dominance coefficients.h_callback¶ (
Callable[[float,ndarray],ndarray]) – A function mapping the scalar parameterhand the array of selection coefficientsSto dominance coefficients of the same shape, allowing models wherehdepends onS. The default islambda h, S: np.full_like(S, h), keepinghconstant. The parameterhis taken fromparams. The fitness of heterozygotes and mutation homozygotes is defined as1 + 2hsand1 + 2s, respectively.integration_mode¶ (
Literal['midpoint','quad']) – Integration mode when computing expected SFS under semidominance.quadis not recommended.linearized¶ (
bool) – Whether to discretize and cache the linearized integral mapping DFE to SFS or usescipy.integrate.quad()in each call.Falseis slow, intended for validation only and deprecated.n_sites_div¶ (
float) – Number of mutational target sites over which divergence is counted. When given, the simulated SFS carry an expected fixed-derived (divergence) class – the expected number of substitutionstheta * n_sites_div * flux(flux = 1for neutral, the selected-relative substitution rate otherwise). WhenNone(the default) the fixed-derived class is zero. Note that this is the true (population-level) divergence; it does not model finite-sample misattributed polymorphism.discretization¶ (
Discretization) – PrecomputedDiscretizationinstance. When given,intervals_del,intervals_ben,h_callback,integration_mode,linearizedandparallelizeare ignored.
-
model:
Parametrization# The DFE parametrization
-
h_callback:
Optional[Callable[[float,ndarray],ndarray]]# Function mapping the parameter
hand the selection coefficients to dominance coefficients
-
n_sites_div:
Optional[float]# Number of mutational target sites for divergence (
None-> no divergence class)
-
discretization:
Discretization# Discretization instance
- get_spectra() Spectra[source]#
Get the spectra used in the simulation.
- Returns:
Spectra with the neutral and selected SFS. When
n_sites_divwas given, both carry their expected divergence (fixed-derived) class, which is part of the total number of sites.
- get_alpha() float[source]#
Get the proportion of adaptive substitutions (alpha) under the specified DFE and parameters.
- Returns:
Proportion of adaptive substitutions
- get_omega() float[source]#
Get omega, the rate of non-synonymous over synonymous substitutions (dN/dS), under the specified DFE and parameters.
- Returns:
Rate of non-synonymous over synonymous substitutions (dN/dS)
- get_omega_a() float[source]#
Get omega_a, the rate of adaptive non-synonymous over synonymous substitutions (adaptive dN/dS), under the specified DFE and parameters.
- Returns:
Rate of adaptive non-synonymous over synonymous substitutions (adaptive dN/dS)
- static get_neutral_sfs(theta: float, n_sites: float, n: int, r: Sequence[float] = None) Spectrum[source]#
Obtain a standard neutral SFS for a given theta and number of sites.
- Parameters:
r¶ (
Sequence[float]) – Nuisance parameters that account for demography. An array of lengthn-1whose elements are multiplied element-wise with the polymorphic counts of the Kingman SFS. By default, no demography effects are considered which is equivalent tor = [1] * (n-1). Note that non-default values ofrwill also affect estimates of the population mutation rate.
- Returns:
Neutral SFS