SFS simulation#
- 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[[ndarray], ndarray] = None, integration_mode: Literal['midpoint', 'quad'] = 'midpoint', linearized: bool = True, 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()
- __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[[ndarray], ndarray] = None, integration_mode: Literal['midpoint', 'quad'] = 'midpoint', linearized: bool = True, 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.get_neutral_sfs()can be used to obtain a neutral SFS.params (
dict) – Parameters of the DFE parametrization (see model) in addition to ancestral misidentification paramsepsand dominanceh. By default, eps=0, h=0.5, the default parameters ofmodelwill be used.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[[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. Expected allele counts for a given dominance value are obtained by linear interpolation between precomputed values inintervals_h. The inferred parameter is still namedh, even if transformed byh_callback, and its bounds, scales, and initial values can be set viabounds,scales, andx0. 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.quadin each call.Falsenot recommended.parallelize (
bool) – Whether to parallelize computations
-
model:
Parametrization# The DFE parametrization
-
params:
dict# Parameters for the DFE parametrization
-
n:
int# SFS sample size
-
theta:
float# Population mutation rate
-
n_sites:
float# Number of sites
-
discretization:
Discretization# Discretization instance
- get_spectra()[source]#
Get the spectra used in the simulation.
- Return type:
- Returns:
Tuple of neutral SFS and selected SFS
- get_alpha()[source]#
Get the proportion of adaptive substitutions (alpha) under the specified DFE and parameters.
- Return type:
float- Returns:
Proportion of adaptive substitutions
- static get_neutral_sfs(theta: float, n_sites: float, n: int, r: Sequence[float] = None)[source]#
Obtain a standard neutral SFS for a given theta and number of sites.
- Parameters:
theta (
float) – Population mutation raten_sites (
float) – Number of total sitesn (
int) – Number of frequency classesr (
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.
- Return type:
- Returns:
Neutral SFS