SFS simulation#

class Simulation(params: dict = None, model: Parametrization | str = 'GammaExpParametrization', sfs_neut: Spectrum = None, eps: float = 0, intervals_del: Tuple[float, float, int] = (-100000000.0, -1e-05, 1000), intervals_ben: Tuple[float, float, int] = (1e-05, 10000.0, 1000), integration_mode: Literal['midpoint', 'quad'] = 'midpoint', linearized: bool = True, discretization: Discretization = None, parallelize: bool = True)[source]#

Bases: object

Simulate 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
sfs_sel = sim.run()

# plot SFS
sfs_sel.plot()
__init__(params: dict = None, model: Parametrization | str = 'GammaExpParametrization', sfs_neut: Spectrum = None, eps: float = 0, intervals_del: Tuple[float, float, int] = (-100000000.0, -1e-05, 1000), intervals_ben: Tuple[float, float, int] = (1e-05, 10000.0, 1000), integration_mode: Literal['midpoint', 'quad'] = 'midpoint', linearized: bool = True, discretization: Discretization = None, parallelize: bool = True)[source]#

Create a simulation object.

Parameters:
  • params (dict) – Parameters for the DFE parametrization (see model). By default, the default parameters of model will be used.

  • model (Parametrization | str) – DFE parametrization model

  • 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.

  • eps (float) – Ancestral misidentification error

  • intervals_del (Tuple[float, float, int]) – (start, stop, n_interval) for deleterious population-scaled selection coefficients. The intervals will be log10-spaced.

  • intervals_ben (Tuple[float, float, int]) – Same as intervals_del but for positive selection coefficients

  • integration_mode (Literal['midpoint', 'quad']) – Integration mode for the DFE, quad not recommended

  • linearized (bool) – Whether to use the linearized DFE, False not recommended

  • parallelize (bool) – Whether to parallelize computations

model: Parametrization#

The DFE parametrization

params: dict#

Parameters for the DFE parametrization

sfs_neut: Spectrum#

Neutral SFS

n: int#

SFS sample size

theta: float#

Population mutation rate

n_sites: float#

Number of sites

eps: float#

Ancestral misidentification error

discretization: Discretization#

Discretization instance

run()[source]#

Simulate an SFS given a DFE.

Return type:

Spectrum

Returns:

Simulated SFS

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 rate

  • n_sites (float) – Number of sites in the simulated SFS

  • n (int) – Number of frequency classes in the simulated SFS

  • r (Sequence[float]) – Nuisance parameters that account for demography. An array of length n-1 whose elements are multiplied element-wise with the polymorphic counts of the Kingman SFS. By default, no demography effects are considered which is equivalent to r = [1] * (n-1). Note that non-default values of r will also affect estimates of the population mutation rate.

Return type:

Spectrum

Returns:

Neutral SFS

get_wright_fisher(pop_size: int, generations: int)[source]#

Get a Wright-Fisher simulation object.

Parameters:
  • pop_size (int) – Effective population size

  • generations (int) – Number of generations to simulate

Return type:

WrightFisherSimulation

Returns:

Wright-Fisher simulation object