haploid_lowd Class Reference

class haploid_lowd(L=1, rng_seed=0)

Class for low-dimensional population genetics (short genomes ~20 loci).

The class offers a number of functions, but an example will explain the basic idea:

#####################################
#   EXAMPLE SCRIPT                  #
#####################################
import numpy as np
import matplotlib.pyplot as plt
import FFPopSim as h

c = h.haploid_lowd(5)               # 5 loci

# initialize with 300 individuals with genotype 00000,
# and 700 with genotype 00010
c.set_genotypes([0, 2], [300, 700])

# set an additive fitness landscape with these coefficients
c.set_fitness_additive([0.02,0.03,0.04,0.02, -0.03])
# Note: we are in the -/+ basis, so
#        F[10000] - F[00000] = 2 * 0.02 
# Hence the coefficients are half of the effect of mutation on fitness 

c.evolve(100)                       # evolve for 100 generations
c.plot_diversity_histogram()
plt.show()
#####################################
__init__(L=1, rng_seed=0)

Construct a low-dimensional population with certain parameters.

Parameters:
  • L : number of loci (at least 1)
  • rng_seed : seed for the random number generator
__str__() <==> str(x)
__repr__() <==> repr(x)
copy(rng_seed=0)

Copy population into new instance.

Parameters:
  • rng_seed: random number to initialize the new population
status()

Print a status list of the population parameters

L

Number of loci (read-only)

number_of_loci

Number of loci (read-only)

N

Population size (read-only)

population_size

Population size (read-only)

generation

Current generation (read-only)

circular

is the genome circular?

carrying_capacity

current carrying capacity of the environment

recombination_model

Model of recombination to use

Available values:

  • FFPopSim.FREE_RECOMBINATION: free shuffling between parents
  • FFPopSim.CROSSOVERS: block recombination with crossover probability
  • FFPopSim.SINGLE_CROSSOVER: block recombination with crossover probability
outcrossing_rate

outcrossing rate (probability of sexual reproduction per generation)

set_allele_frequencies(frequencies, N)

Initialize the population in linkage equilibrium with specified allele frequencies.

Parameters:
  • frequencies: an array of length L with all allele frequencies
  • N: set the population size and, if still unset, the carrying capacity to this value

Note

the population size is only used for resampling and has therefore no effect on the speed of the simulation.

set_genotypes(genotypes, counts)

Initialize population with fixed counts for specific genotypes.

Parameters:
  • genotypes: list of genotypes to set. Genotypes are specified as integers,

    from 00...0 that is 0, up to 11...1 that is 2^L-1.

  • counts: list of counts for those genotypes

Note

the population size and, if unset, the carrying capacity will be set as the sum of the counts.

Note

you can use Python binary notation for the indices, e.g. 0b0110 is 6.

set_wildtype(N)

Initialize population of N individuals with the - allele at all loci (wildtype)

Parameters:
  • N: the number of individuals

Note

the carrying capacity is set to the same value if still unset.

set_recombination_rates(rates, model=None)

Set the recombination rate(s).

Parameters:
  • rates: if a double, the recombination rate at between any two loci; if an array, the locus-specific recombination rates
  • model: the recombination model to use (CROSSOVERS or, for linear genomes, SINGLE_CROSSOVER)

Note

if locus-specific rates are specified, the array must have length (L-1) for linear chromosomes and length L for circular ones. The i-th element is the crossover rate between the i-th site and the (i+1)-th site.

Note

if the recombination model is not specified, the current model will be kept or, if the current model is FREE_RECOMBINATION, then CROSSOVERS will be set.

get_recombination_rates()

Get recombination rates.

Returns:
  • the rates between neighboring loci, a list of float of length L-1

Note

if the recombination model if FREE_RECOMBINATION, an error is raised.

set_mutation_rates(rates, rates_back=None)

Set the mutation rate(s).

Parameters:
  • rates:if a double, the mutation rate at any locus in both directions or, if rates_back is not None, only in the forward direction

    if a vector, the mutation rate is specified for each locus, the same in both directions or, if rates_back is not None, only in the forward direction

  • rates_back: mutation rate in the backward direction (global or locus-specific)

get_mutation_rates(locus=None, direction=None)

Get one or several mutation rates.

Parameters:
  • locus: get only the mutation rate(s) of this locus

  • direction: get only the forward or backward mutation rate(s). This argument

    is a Boolean, 0/False for forward rates, 1/True for backward rates.

Returns:
  • the mutation rate(s) requested

Note: if the mutation rates for all loci and/or directions are the same, this function will try to be smart and give you the answer you are looking for. In case of doubt, you will get a matrix (L x 2) with the full mutation rate landscape.

evolve(gen=1)

Evolve for some generations

Parameters:
  • gen: number of generations to evolve the population, defaults to one
evolve_deterministic(gen=1)

Evolve for some generations deterministically (skips the resampling)

Parameters:
  • gen: number of generations to evolve the population
evolve_norec(gen=1)

Evolve for some generations without recombination

Parameters:
  • gen: number of generations to evolve the population
get_genotype_frequency(genotype)

Get the frequency of a genotype

Parameters:
  • genotype: genotype, whose the frequency is to be returned
Returns:
  • the frequency of the genotype
get_genotype_frequencies()

Get the frequency of each genotype.

get_allele_frequency(locus)

Get the frequency of the + allele

Parameters:
  • locus: locus, at which the frequency of the + allele is to be computed
Returns:
  • the frequency of the + allele, :math:`

u_i := rac{1 + left<s_i ight>}{2}`, where \(s_i \in \{-1, 1\}\).

get_allele_frequencies()

Get the frequencies of all + alleles

get_pair_frequency(locus1, locus2)

Get the frequency of genotypes with the + allele at both loci.

Parameters:
  • locus1: first locus
  • locus2: second locus
Returns:
  • the joint frequency of the + alleles
get_chi(locus)

Get chi of an allele in the -/+ basis

Parameters:
  • locus: locus whose chi is to be computed
Returns:
  • the chi of that allele, :math:`chi_i := left<s_i

ight>`, where \(s_i \in \{-1, 1\}\).

get_chi2(locus1, locus2)

Get \(\chi_{ij}\)

Parameters:
  • locus1: first locus
  • locus2: second locus
Returns:
  • the linkage disequilibiurm between them, i.e. :math:`chi_{ij} := left<s_i s_j

ight> - chi_i cdot chi_j`.

get_LD(locus1, locus2)

Get linkage disequilibrium

Parameters:
  • locus1: first locus
  • locus2: second locus
Returns:
  • the linkage disequilibiurm between them, i.e. :math:`D_{ij} := 1 / 4 left[left<s_i s_j

ight> - chi_i cdot chi_j ight]`.

get_moment(locus1, locus2)

Get moment of two alleles in the -/+ basis

Parameters:
  • locus1: first locus
  • locus2: second locus
Returns:
  • the second moment, i.e. :math:`left<s_i s_j

ight>`, where \(s_i, s_j \in \{-1, 1\}\).

random_genomes(n_sample)

Get random genomes according sampled from the population.

Parameters:
  • n_sample: number of random genomes to sample
Returns:
  • integers corresponding to random genomes in the population.
get_fitness(genotype)

Get fitness values of a genotype

Parameters:
  • genotype: genotype whose fitness is to be calculated. This can either be an integer or in binary format, e.g. 5 = 0b101
Returns:
  • the fitness of that genotype.
get_fitnesses()

Get the fitness of all possible genotypes.

get_fitness_histogram(n_sample=1000, **kwargs)

Get the histogram of the fitness of a sample from the population.

Parameters:
  • n_sample: number of individual to sample at random from the population. defaults to 1000
Returns:
  • h: numpy.histogram of fitness in the population
plot_fitness_histogram(axis=None, n_sample=1000, **kwargs)

Plot the histogram of the fitness of a sample from the population.

Parameters:
  • axis: use an already existing axis for the plot
  • n_sample: number of individual to sample at random from the population. Defaults to 1000.
  • kwargs: further optional keyword arguments to numpy.histograms
get_divergence_statistics(n_sample=1000)

Get the mean and variance of the divergence of a population sample – same as mean and variance of allele frequencies.

Parameters:
  • n_sample: number of individuals to sample at random from the population. defaults to 1000.
Returns:
  • stat: structure with mean and variance of divergence in the population
get_divergence_histogram(bins=10, n_sample=1000, **kwargs)

Get the histogram of the divergence of a population sample.

Parameters:
  • bins: number of bins or list of bin edges (passed verbatim to numpy.histogram)
  • n_sample: number of individual to sample at random from the population, defaults to 1000.
  • kwargs: further optional keyword arguments to numpy.histograms
Returns:
  • h: numpy.histogram of divergence in the population

Note: to get a normalized histogram, use the density keyword.

plot_divergence_histogram(axis=None, n_sample=1000, **kwargs)

Plot the histogram of the divergence of a population sample.

Parameters:
  • axis: use an already existing axis for the plot
  • n_sample: number of individual to sample at random from the population, defaults to 1000.
  • kwargs: further optional keyword arguments to numpy.histograms
get_diversity_statistics(n_sample=1000)

Get the mean and variance of the diversity of a population sample

Parameters:
  • n_sample: number of individual to sample at random from the population, defaults to 1000.
Returns:
  • stat: structure with mean and variance of diversity in the population
get_diversity_histogram(bins=10, n_sample=1000, **kwargs)

Get the histogram of the diversity in a sample from the population.

Parameters:
  • bins: number of bins or list of bin edges (passed verbatim to numpy.histogram)
  • n_sample: number of individual to sample at random from the population, defaults to 1000.
  • kwargs: further optional keyword arguments to numpy.histograms
Returns:
  • h: numpy.histogram of diversity in the population

Note: to get a normalized histogram, use the density keyword.

plot_diversity_histogram(axis=None, n_sample=1000, **kwargs)

Plot the histogram of the diversity of a population sample.

Parameters:
  • axis: use an already existing axis for the plot
  • n_sample: number of individual to sample at random from the population, defaults to 1000.
  • kwargs: further optional keyword arguments to numpy.histograms
set_fitness_function(genotypes, values)

Set the fitness landscape for individual genotypes.

Parameters:
  • genotypes: genotype to which the fitness values will be assigned. Genotypes are specified as integers,

    from 00...0 that is 0, up to 11...1 that is 2^L-1.

  • values: fitness values to assign

Note

you can use Python binary notation for the genotypes, e.g. 0b0110 is 6.

set_fitness_coefficients(coefficients, values)

Set the fitness landscape in Fourier space for individual Fourier coefficients.

Parameters:
  • coefficients: Fourier coefficients to which the values will be assigned. They are specified

    as integers, from 00...0 that is 0, up to 11...1 that is 2^L-1.

  • values: values to assign

Note

you can use Python binary notation for the coefficients, e.g. 0b0110 is 6.

set_fitness_additive(coefficients)

Set an additive fitness landscape. Coefficients obey +/- convention.

Parameters:
  • coefficients: array/list of additive fitness coefficients. It must have length L.
genotype_entropy()

Get the genotype entropy of the population

Note

the genotype entropy is defined as \(-\sum_{i=0}^{2^L} p_i \log p_i\).

allele_entropy()

get the allele entropy of the population

Note

the allele entropy is defined as \(-\sum_{i=0}^{L} \left[\nu_i\log \nu_i + (1-\nu_i)\log(1-\nu_i)\right]\).

Previous topic

genotype_value_pair Class Reference

Next topic

haploid_highd Class Reference

This Page