Using the epinetr package

Dion Detterer, Paul Kwan, Cedric Gondro

2021-01-10

Introduction

epinetr is a forward-time genetic simulation package that includes the ability to construct epistatic networks of arbitrary complexity. Applications for epinetr include the testing of methods for the detection and selection of epistasis, as well as assessing the impact of epistasis on prediction and the extent to which epistasis is captured by additive models, all under varying degrees of epistatic complexity.

There are four broad steps in the workflow:

  1. Construct the initial population with necessary parameters
  2. Attach additive effects to the population
  3. Attach an epistatic network to the population and visualise the network
  4. Run a forward-time simulation of the population and plot the simulation run

In Section 1, we look at the various ways of constructing a population in epinetr. In Section 2, we briefly discuss how to attach additive effects to a population before diving into the options for constructing epistatic networks. Section 3 shows how epinetr calculates the components of each individual’s phenotype, and how to recreate that calculation. Finally, in section 5, we demonstrate how to run the simulation.

As a preview, here’s an example workflow:

library(epinetr)

# Build a population of size 1000, with 50 QTLs, broad-sense heritability of 0.6,
# narrow-sense heritability of 0.4 and overall trait variance of 50.
pop <- Population(popSize = 1000, map = map100snp, alleleFrequencies = runif(100),
                  QTL = 50, broadH2 = 0.6, narrowh2 = 0.4, traitVar = 50)

# Attach additive effects
pop <- addEffects(pop)

# Attach an epistatic network
pop <- attachEpiNet(pop)

# Plot the network
plot(getEpiNet(pop))
An epistatic network generated between 50 QTLs.

An epistatic network generated between 50 QTLs.

# Inspect initial phenotypic components
head(getComponents(pop))
##   ID Sire Dam   Additive  Epistatic Environmental   Phenotype Sex
## 1  1    0   0  1.9403146 -0.6210688     2.5990840  3.91832983   M
## 2  2    0   0  5.2526915  2.6956568    -6.3873173  1.56103097   F
## 3  3    0   0 -2.0182882 -3.6757927    -0.6135545 -6.30763550   M
## 4  4    0   0  0.2815276  1.7682109     5.4816386  7.53137717   M
## 5  5    0   0 -1.2549731  0.5572027     3.6193921  2.92162171   F
## 6  6    0   0  0.9267308 -1.6300837     0.8021027  0.09874985   M
# Run a simulation across 250 generations
pop <- runSim(pop, generations = 250, selection = "ranking")

# Plot the simulation run
plot(pop)
A graphical representation of a simulation run across 250 generations.

A graphical representation of a simulation run across 250 generations.

# Get the allele frequencies
af <- getAlleleFreqRun(pop)

# Get the phased genotypes of the resulting population
geno <- getPhased(pop)

# Get a subset of the resulting population
ID <- getComponents(pop)$ID
ID <- sample(ID, 50)
pop2 <- getSubPop(pop, ID)

Constructing the initial population

Constructing the initial population is done via the Population function. The only data you will absolutely need is a map, either via a variant call format (VCF) file or directly via a data frame.

If you are directly supplying a map data frame, the first column should list the single nucleotide polymorphism (SNP) IDs, the second column should list the chromosome IDs for each SNP and the third column should list the position of each SNP on its chromosome in base pairs.

For example:

head(map100snp)
##   V1 V2        V3
## 1  1  1  31433512
## 2  2  1  61930663
## 3  3  1  95513586
## 4  4  1 130263477
## 5  5  1 166021598
## 6  6  1 201083562
nrow(map100snp)
## [1] 100
length(unique(map100snp[, 2]))
## [1] 22

There are 100 SNPs across 22 chromosomes in this map data frame.

Given this map, we can now construct a population. We’ll generate 200 individuals using allele frequencies selected from a uniform distribution, we’ll select 20 QTLs at random and we’ll give the phenotypic trait under examination a variance of 40, a broad-sense heritability of 0.9 and a narrow-sense heritability of 0.6.

pop <- Population(popSize = 200, map = map100snp, QTL = 20,
                  alleleFrequencies = runif(100),
                  broadH2 = 0.9, narrowh2 = 0.6, traitVar = 40)
pop
## Population of size 200 
## Specified initial trait variance: 40 
## Initial broad-sense heritability: 0.9 
## Initial narrow-sense heritability: 0.6 
## Using 100 SNPs with 20 QTLs:
## 7  21  33  34  40  46  49  62  63  68  69  74  75  80  83  87  88  92  93  95

We can fall back on the built-in defaults for broadH2, narrowh2 and traitVar of 0.5, 0.3 and 1, respectively:

pop <- Population(popSize = 200, map = map100snp, QTL = 20,
                  alleleFrequencies = runif(100))
pop
## Population of size 200 
## Specified initial trait variance: 1 
## Initial broad-sense heritability: 0.5 
## Initial narrow-sense heritability: 0.3 
## Using 100 SNPs with 20 QTLs:
## 4  8  14  18  20  49  52  53  57  58  59  68  71  72  82  86  87  95  97  100

We can also specify the QTLs by listing their SNP IDs:

pop <- Population(popSize = 200, map = map100snp,
                  QTL = c(62, 55, 92, 74, 11, 38),
                  alleleFrequencies = runif(100),
                  broadH2 = 0.9, narrowh2 = 0.6, traitVar = 40)
pop
## Population of size 200 
## Specified initial trait variance: 40 
## Initial broad-sense heritability: 0.9 
## Initial narrow-sense heritability: 0.6 
## Using 100 SNPs with 6 QTLs:
## 11  38  55  62  74  92

(Note that the QTLs will be displayed only if there are no more than 100 QTLs, so as not to flood the screen.)

A full list of QTLs can also be displayed like so:

getQTL(pop)
##   ID Index
## 1 11    11
## 2 38    38
## 3 55    55
## 4 62    62
## 5 74    74
## 6 92    92

Note that any map supplied to the constructor will be sorted, first by chromosome and then by base pair position.

Constructing a population using a genotype matrix

For greater control, we can supply a matrix of phased biallelic genotypes to the constructor, either directly or via a VCF file using the vcf parameter. (Using a VCF file will also supply a map.)

If given directly, the matrix should be in individual-major format, with each allele coded with either a 0 or a 1 and no unknown values. For example, geno100snp is a genotype matrix of 100 SNPs across 500 individuals. It’s already in the necessary format, which uses one individual per row and two columns per SNP.

dim(geno100snp)
## [1] 500 200

Examining the first 5 SNPs for the first individual we find the following:

geno100snp[1, 1:10]
##  V1  V2  V3  V4  V5  V6  V7  V8  V9 V10 
##   1   0   1   1   1   1   1   1   0   0

That is, SNP 1 is heterozygous with genotype 1|0, SNPs 2-4 are homozygous with genotype 1|1 and SNP 5 is homozygous with genotype 0|0.

We can supply a phased genotype matrix to the constructor like so:

pop <- Population(popSize = nrow(geno100snp), map = map100snp, QTL = 20,
                  genotypes = geno100snp,
                  broadH2 = 0.9, narrowh2 = 0.6, traitVar = 40)

Supplying a phased genotype matrix allows us to directly specify the initial genotypes in the population. (The assumption is that the first allele for each SNP is inherited from the sire and the second allele for each SNP is inherited from the dam.) If we supply a population size to the constructor that does not match the number of rows in the genotype matrix, the genotypes will be used only to suggest allele frequencies for newly generated genotypes.

If we wish to use the genotypes to suggest allele frequencies while still maintaining the population size, we can set the literal flag to FALSE.

pop <- Population(popSize = nrow(geno100snp), map = map100snp, QTL = 20,
                  genotypes = geno100snp, literal = FALSE,
                  broadH2 = 0.9, narrowh2 = 0.6, traitVar = 40)

Modifying an existing population

The Population constructor can also be used to modify an existing population. We can, for example, adjust the heritability:

pop <- Population(pop, broadH2 = 0.7, traitVar = 30)
pop
## Population of size 500 
## Specified initial trait variance: 30 
## Initial broad-sense heritability: 0.7 
## Initial narrow-sense heritability: 0.6 
## Using 100 SNPs with 20 QTLs:
## 4  7  22  23  24  39  41  45  46  49  52  57  67  72  86  88  91  93  94  97

We can also adjust the population size; this will necessarily generate a new set of genotypes based on the same allele frequencies:

pop <- Population(pop, popSize = 800)
pop
## Population of size 800 
## Specified initial trait variance: 30 
## Initial broad-sense heritability: 0.7 
## Initial narrow-sense heritability: 0.6 
## Using 100 SNPs with 20 QTLs:
## 4  7  22  23  24  39  41  45  46  49  52  57  67  72  86  88  91  93  94  97

Similarly, we can adjust the allele frequencies, which will necessarily also generate a new set of genotypes:

pop <- Population(pop, alleleFrequencies = runif(100))
pop
## Population of size 800 
## Specified initial trait variance: 30 
## Initial broad-sense heritability: 0.7 
## Initial narrow-sense heritability: 0.6 
## Using 100 SNPs with 20 QTLs:
## 4  7  22  23  24  39  41  45  46  49  52  57  67  72  86  88  91  93  94  97

Where possible, the population features are preserved while adjusting only the parameters specified.

Attaching effects to the population

Because we have specified a non-zero narrow-sense heritability for our population, we now need to attach additive effects. This is done using the addEffects function.

pop <- addEffects(pop)
pop
## Population of size 800 
## Specified initial trait variance: 30 
## Initial broad-sense heritability: 0.7 
## Initial narrow-sense heritability: 0.6 
## Additive variance in population: 18 
## Using 100 SNPs with 20 QTLs:
## 4  7  22  23  24  39  41  45  46  49  52  57  67  72  86  88  91  93  94  97

As expected, 60% of the phenotypic variance is attributable to additive effects.

By default, effects are selected from a normal distribution; we can, however, supply a different distribution function.

pop <- addEffects(pop, distrib = runif)

Alternatively, we can supply our own additive effects for the QTLs.

effects <- c( 1.2,  1.5, -0.3, -1.4,  0.8,
              2.4,  0.2, -0.8, -0.4,  0.8,
             -0.2, -1.4,  1.4,  0.2, -0.9,
              0.4, -0.8,  0.0, -1.1, -1.3)
pop <- addEffects(pop, effects = effects)
getAddCoefs(pop)
##  [1]  2.2959752  2.8699691 -0.5739938 -2.6786378  1.5306502  4.5919505
##  [7]  0.3826625 -1.5306502 -0.7653251  1.5306502 -0.3826625 -2.6786378
## [13]  2.6786378  0.3826625 -1.7219814  0.7653251 -1.5306502  0.0000000
## [19] -2.1046440 -2.4873065

Note that the additive effects are scaled so as to guarantee the initial narrow-sense heritability. This is evident by adjusting the narrow-sense heritability within the population:

pop <- Population(pop, narrowh2 = 0.4)
getAddCoefs(pop)
##  [1]  1.8746559  2.3433199 -0.4686640 -2.1870986  1.2497706  3.7493119
##  [7]  0.3124427 -1.2497706 -0.6248853  1.2497706 -0.3124427 -2.1870986
## [13]  2.1870986  0.3124427 -1.4059920  0.6248853 -1.2497706  0.0000000
## [19] -1.7184346 -2.0308773

Attaching an epistatic network to the population

If broad-sense heritability is higher than narrow-sense heritability in the population, you will need to attach epistatic effects. The simplest way to do this is to use the attachEpiNet function with the default arguments, supplying only the population. This will generate a random epistatic network with the QTLs as nodes.

pop <- attachEpiNet(pop)
pop
## Population of size 800 
## Specified initial trait variance: 30 
## Initial broad-sense heritability: 0.7 
## Initial narrow-sense heritability: 0.4 
## Additive variance in population: 12 
## Epistatic variance in population: 9 
## Using 100 SNPs with 20 QTLs:
## 4  7  22  23  24  39  41  45  46  49  52  57  67  72  86  88  91  93  94  97

Note that the epistatic variance is as expected and the additive variance has been preserved.

We can visualise the network that was generated by using the getEpiNet function to retrieve the network before plotting it.

epinet <- getEpiNet(pop)
plot(epinet)
A random epistatic network generated between 20 QTLs.

A random epistatic network generated between 20 QTLs.

We can use the scaleFree flag to generate a network using the Barabasi-Albert model1.

pop <- attachEpiNet(pop, scaleFree = TRUE)
plot(getEpiNet(pop))
An epistatic network generated using the Barabasi-Albert model between 20 QTLs.

An epistatic network generated using the Barabasi-Albert model between 20 QTLs.

The Barabasi-Albert model for constructing networks assumes a connected graph that then adds a node at a time, with the probability that an existing node is connected to the new node given by \(\frac{d_i}{\sum_{j} d_j}\), where \(d_i\) is the degree of node \(i\). In epinetr, nodes are added in a random order (by shuffling the initial list) so as to ensure that no bias is introduced due to the initial ordering of nodes.

The “random” network model in epinetr acts as a control case: it uses the same algorithm as the Barabasi-Albert model but gives a uniform probability of connection to all existing nodes. This ensures that there are the same number of interactions in both cases.

If we want a number of QTLs to only have additive effects applied, we can use the additive argument, giving the number of QTLs not to be included in the epistatic network.

pop <- attachEpiNet(pop, scaleFree = TRUE, additive = 7)
plot(getEpiNet(pop))
An epistatic network where 7 QTLs have no epistatic effects.

An epistatic network where 7 QTLs have no epistatic effects.

The minimum number of interactions per QTL can be given with the m argument.

pop <- attachEpiNet(pop, scaleFree = TRUE, additive = 7, m = 2)
plot(getEpiNet(pop))
An epistatic network with a minimum of 2 interactions per epistatic QTL.

An epistatic network with a minimum of 2 interactions per epistatic QTL.

There are three points to note with the m argument. The first point is that m specifically refers to the number of interactions and not the order of the interactions (which can be set with the k parameter; see below). The second point is that it has no impact on QTLs designated as additive-only. The third point is that a minimal connected graph is initially constructed prior to m being strictly applied. (This means that you may still see some QTLs with fewer than m interactions due to the value of k.)

We can also include higher-order interactions using the k argument, which accepts a vector specifying the orders of interaction to include:

pop <- attachEpiNet(pop, scaleFree = TRUE, additive = 7, m = 2, k=2:7)
plot(getEpiNet(pop))
An epistatic network featuring 2-way to 7-way interactions

An epistatic network featuring 2-way to 7-way interactions

In cases where \(m > 1\), any initial minimal connected graph is created by selecting \(n\) nodes such that \(n\) is the smallest integer satisfying the equation \(\binom{n - 1}{k - 1} \ge m\). (In the default case of pairwise interactions, this reduces to \(n \ge m + 1\).)

Where \(k > 2\) we have extended the Barabasi-Albert model such that the attachEpiNet function adds interactions in order of increasing complexity: first, the function adds all 2-way interactions, then all 3-way interactions, etcetera. The important point to note is that, for networks generated using the Barabasi-Albert model, probabilities of connectedness are based on all previous interactions; for example, 3-way interactions are based on all previously established 2- and 3-way interactions. In this way, networks generated using the Barabasi-Albert model with multiple orders of interaction “layer up”, such that the degrees from lower-order interactions contribute to the preferential attachment for higher-order interactions.

All auto-generated networks are highly connected. More diffuse networks can, however, be manually generated, as detailed in the next section.

Supplying a user-defined network

Internally, the network is stored as an incidence matrix, where the ith row corresponds to the ith QTL and the jth column corresponds to the jth interaction.

We can inspect the complete set of interactions using the getIncMatrix function.

inc <- getIncMatrix(pop)
dim(inc)
## [1]  20 102

There are currently 102 interactions in the population. Let’s examine the first five.

inc[, 1:5]
##       [,1] [,2] [,3] [,4] [,5]
##  [1,]    0    0    0    0    0
##  [2,]    0    0    0    0    0
##  [3,]    1    1    0    1    1
##  [4,]    0    0    0    0    0
##  [5,]    0    0    0    0    0
##  [6,]    0    0    0    0    0
##  [7,]    0    0    0    0    0
##  [8,]    0    0    0    0    1
##  [9,]    0    0    0    0    0
## [10,]    0    1    0    0    0
## [11,]    0    0    0    0    0
## [12,]    1    0    1    0    0
## [13,]    0    0    0    0    0
## [14,]    0    0    1    1    0
## [15,]    0    0    0    0    0
## [16,]    0    0    0    0    0
## [17,]    0    0    0    0    0
## [18,]    0    0    0    0    0
## [19,]    0    0    0    0    0
## [20,]    0    0    0    0    0

Here we can see that the first interaction is between QTL 3 and QTL 12, and that QTL 3 is included in 4 of the first 5 interactions.

We can define our own network in the same way. rincmat100snp is an example of a user-defined incidence matrix, giving 19 interactions across 20 QTLs:

rincmat100snp
##       V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 V13 V14 V15 V16 V17 V18 V19
##  [1,]  0  1  0  0  0  0  0  0  0   0   0   0   1   0   0   0   0   0   0
##  [2,]  0  0  0  0  0  0  1  0  0   0   0   0   0   0   0   0   0   0   0
##  [3,]  0  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0   0   0   0
##  [4,]  0  0  0  0  1  0  0  0  0   0   0   0   0   0   0   0   0   0   0
##  [5,]  0  0  0  0  0  0  0  0  0   0   1   0   0   0   0   0   1   0   0
##  [6,]  0  0  0  0  0  0  0  0  1   0   0   1   0   0   1   0   1   1   0
##  [7,]  0  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0   0   0   0
##  [8,]  0  0  0  0  0  0  0  0  0   0   1   0   0   0   0   0   0   0   0
##  [9,]  1  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0   0   0   0
## [10,]  0  0  0  0  1  0  0  0  0   0   0   0   1   1   0   1   0   0   1
## [11,]  0  0  0  1  0  0  0  0  0   1   0   0   0   0   0   0   0   0   0
## [12,]  0  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0   0   0   0
## [13,]  0  0  0  0  0  0  0  0  0   1   0   0   0   0   1   0   0   0   1
## [14,]  0  1  0  0  0  0  0  0  0   0   0   0   0   0   0   0   0   0   0
## [15,]  1  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0   0   0   0
## [16,]  0  0  0  0  0  1  1  1  0   0   0   1   0   0   0   0   0   0   0
## [17,]  0  0  0  0  0  0  0  0  1   0   0   0   0   0   0   1   0   0   0
## [18,]  0  0  1  1  0  1  0  0  0   0   0   0   0   1   0   0   0   1   0
## [19,]  0  0  1  0  0  0  0  1  0   0   0   0   0   0   0   0   0   0   0
## [20,]  0  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0   0   0   0

We can attach an epistatic network based on this incidence matrix to our population using the incmat argument:

pop <- attachEpiNet(pop, incmat = rincmat100snp)

Let’s visualise the subsequent network:

plot(getEpiNet(pop))
An epistatic network derived from a user-defined incidence matrix.

An epistatic network derived from a user-defined incidence matrix.

As per the interaction matrix, 4 of the 20 QTLs are not part of any interactions. (Rows 3, 7, 12 and 20 only contain 0s.)

We can create a 3-way interaction by modifying the matrix such that QTL 20 is included in the first interaction:

# Include the 20th QTL in the first interaction
mm <- rincmat100snp
mm[20, 1] <- 1
pop <- attachEpiNet(pop, incmat = mm)

Again, let’s visualise the subsequent network.

plot(getEpiNet(pop))
A user-defined epistatic network featuring a single 3-way interaction.

A user-defined epistatic network featuring a single 3-way interaction.

The network now includes a single 3-way interaction.

Creating purely epistatic QTLs

As already shown, it is possible to create purely additive QTLs; similarly, it is also possible to create purely epistatic QTLs. This involves specifying the additive coefficients explicitly, where at least one coefficient is 0; for example:

# 20 coefficients, 3 of which are 0
coefs <- sample(c(rep(0, 3), rnorm(17)), 20)
pop <- addEffects(pop, effects = coefs)
getAddCoefs(pop)
##  [1]  0.000000000 -1.081088264 -0.067340894  0.000000000  1.278133553
##  [6] -1.158879032 -1.548010944 -2.962041035  0.710351099  1.328594303
## [11] -0.121150291 -0.099486644  2.302960182  0.007473146  0.050323141
## [16] -0.094074709  0.000000000  1.225245630  3.077296290 -0.424590497

We can see here that QTLs 1, 4 and 17 all have additive coefficients of 0, making them purely epistatic (since the population includes epistatic interactions).

If we wish to have both purely additive and purely epistatic QTLs in our model, we can explicitly give the SNP IDs of the QTLs we want to be purely additive to attachEpiNet.

Suppose we have 15 QTLs overall. For the sake of simplicity, we’ll make the 15 QTLs the first 15 SNPs in the map:

pop <- Population(pop, QTL = 1:15)

Let’s make the first 5 QTLs additive-only by giving their SNP IDs to the additive parameter of attachEpiNet:

pop <- attachEpiNet(pop, additive = 1:5)

As we can see in the incidence matrix, the first 5 of the 15 QTLs have no epistatic effects:

getIncMatrix(pop)
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
##  [1,]    0    0    0    0    0    0    0    0    0
##  [2,]    0    0    0    0    0    0    0    0    0
##  [3,]    0    0    0    0    0    0    0    0    0
##  [4,]    0    0    0    0    0    0    0    0    0
##  [5,]    0    0    0    0    0    0    0    0    0
##  [6,]    1    0    0    0    1    0    1    0    0
##  [7,]    0    0    0    0    0    0    0    0    1
##  [8,]    0    0    0    1    0    1    0    0    0
##  [9,]    0    0    0    0    0    0    0    1    0
## [10,]    1    1    1    0    0    0    0    1    0
## [11,]    0    0    0    0    0    1    0    0    0
## [12,]    0    0    0    0    0    0    1    0    0
## [13,]    0    0    0    0    1    0    0    0    0
## [14,]    0    0    1    0    0    0    0    0    0
## [15,]    0    1    0    1    0    0    0    0    1

Now, we can plot the network in order to visualise the incidence matrix:

plot(getEpiNet(pop))
The epistatic network generated with the first 5 of the 15 QTLs being purely additive.

The epistatic network generated with the first 5 of the 15 QTLs being purely additive.

Let’s make QTLs 6-10 epistatic-only by explicitly giving their coefficients as 0 to addEffects:

coefs <- rnorm(15)
coefs[6:10] <- 0
pop <- addEffects(pop, effects = coefs)
getAddCoefs(pop)
##  [1]  0.7180830 -1.8539361 -2.9016609  1.0446160  2.6898943  0.0000000
##  [7]  0.0000000  0.0000000  0.0000000  0.0000000  1.0660867  2.2179282
## [13] -0.5569307  2.8534020  0.7195520

We now have a population where 5 of the QTLs are purely additive, 5 are purely epistatic and 5 are both additive and epistatic.

Calculating effects

Now that we’ve seen how epinetr builds epistatic networks, it’s time to turn our attention to the effects generated by these networks.

Each QTL can, of course, only have one of three genotypes per individual: the homozygous genotype coded 0|0, the heterozygous genotype and the homozygous genotype coded 1|1. For an interaction consisting of \(k\) QTLs, this corresponds to \(3^k\) possible genotypes within the interaction overall, and for this reason, epinetr assigns a set of \(3^k\) potential epistatic effects drawn from a normal distribution to each interaction.

Let’s create a new population with 20 QTLs, additive effects and an epistatic network consisting solely of 3-way interactions:

pop <- Population(popSize = 200, map = map100snp, QTL = 20,
                  alleleFrequencies = runif(100),
                  broadH2 = 0.9, narrowh2 = 0.6, traitVar = 40)
pop <- addEffects(pop)
pop <- attachEpiNet(pop, k = 3)

We can plot the network in order to visualise these 3-way interactions:

plot(getEpiNet(pop))
An epistatic network consisting of 3-way interactions.

An epistatic network consisting of 3-way interactions.

Let’s determine which QTLs are part of the first interaction:

qtls <- which(getIncMatrix(pop)[, 1] > 0)
qtls
## [1]  4 10 14

The possible effects for this interaction can be found using getInteraction:

interaction1 <- getInteraction(pop, 1) # Return first interaction array
interaction1
## , , 1
## 
##            [,1]      [,2]        [,3]
## [1,]  0.4941777 -2.884275 -0.98792571
## [2,] -1.8301219  1.159978  0.23586876
## [3,]  1.0945040  1.173695  0.09223239
## 
## , , 2
## 
##             [,1]       [,2]      [,3]
## [1,] -0.97753073 -0.5438338  1.688157
## [2,]  0.85533476  0.7177372 -1.157436
## [3,]  0.08373268  1.2584567  1.088320
## 
## , , 3
## 
##            [,1]      [,2]       [,3]
## [1,]  0.9240990  1.688806 -1.0702468
## [2,]  1.5488021 -2.128255 -1.5853468
## [3,] -0.2755026 -1.605615 -0.4119969

As expected, this is a \(3^3\) array of possible effects: the first dimension maps to QTL 4, the second dimension maps to QTL 10 and the third dimension maps to QTL 14. Along each dimension, the first index maps to the homozygous genotype coded 0|0, the second index maps to the heterozygous genotype and the third index maps to the homozygous genotype coded 1|1.

Suppose for a particular individual QTL 4 has the heterozygous genotype. The possible effects for the interaction are thus given by the following:

interaction1[2, , ]
##            [,1]       [,2]      [,3]
## [1,] -1.8301219  0.8553348  1.548802
## [2,]  1.1599782  0.7177372 -2.128255
## [3,]  0.2358688 -1.1574359 -1.585347

Furthermore, suppose that QTL 10 has the homozygous genotype coded 0|0. The possible effects for the interaction are now further constrained to the following:

interaction1[2, 1, ]
## [1] -1.8301219  0.8553348  1.5488021

Finally, suppose that QTL 14 has the homozygous genotype coded 1|1. We now have all we need in order to know the effect of this interaction on the phenotype:

interaction1[2, 1, 3]
## [1] 1.548802

Thus the contribution of this particular interaction to this individual’s overall phenotype is 1.5488021. (An offset, however, has yet to be applied: see the next section for details.)

Inspecting additive, epistatic and environmental components

Once any necessary additive and epistatic effects are attached to our population, we can inspect the phenotypic components of each individual, using the getComponents function:

components <- getComponents(pop)
head(components)
##   ID Sire Dam  Additive  Epistatic Environmental Phenotype Sex
## 1  1    0   0  2.781772 -5.1664274     0.1975568 -2.187099   F
## 2  2    0   0  1.175381  3.7513827     0.8256997  5.752463   F
## 3  3    0   0  3.110365  0.3337473     0.8420019  4.286115   F
## 4  4    0   0  7.288447 -0.3440490    -2.0732957  4.871102   M
## 5  5    0   0  9.648976  0.4039665    -1.2675971  8.785346   F
## 6  6    0   0 -3.653999 -1.7371449     3.1369587 -2.254185   F

Inspecting the additive component, we can see its mean and variance are as expected:

mean(components$Additive)
## [1] -8.4815e-16
var(components$Additive)
## [1] 24

Similarly for the epistatic and environmental components:

mean(components$Epistatic)
## [1] -2.625938e-18
var(components$Epistatic)
## [1] 12
mean(components$Environmental)
## [1] 2.748405e-17
var(components$Environmental)
## [1] 4

The means are (effectively) 0 because the components are zero-centred using fixed offsets applied to the additive and epistatic components that are preserved across generations. (Only the initial generation’s environmental component is zero-centred.) We can retrieve these offsets with the getAddOffset and getEpiOffset functions, respectively:

getAddOffset(pop)
## [1] 13.52826
getEpiOffset(pop)
## [1] 0.5256278

For the overall phenotypic value, we find the following:

mean(components$Phenotype)
## [1] -8.284855e-16
var(components$Phenotype)
## [1] 39.99597

This approximation of the specified variance is due to a small amount of co-variance between components:

cov(components$Additive, components$Epistatic)
## [1] 0.002129933
cov(components$Additive, components$Environmental)
## [1] -0.0846687
cov(components$Environmental, components$Epistatic)
## [1] 0.08052375

However:

cor(components$Additive, components$Epistatic)
## [1] 0.0001255075

epinetr attempts to minimise these co-variances by selecting from within the random distributions for the environmental and epistatic components such that co-variances are minimal, given computational constraints. In particular, the epistatic component is optimised using a genetic algorithm.

Deriving additive and epistatic components

We are now in a position to derive the additive component for the population. First, we’ll retrieve the population’s unphased genotypes using the getGeno function:

geno <- getGeno(pop)

Alternatively, we could use the getHaplo function, which returns a list of the two haplotype matrices within the population; we would then need to sum the two haplotypes together.

Next, we’ll select only the QTLs within the genotypes:

geno <- geno[, getQTL(pop)$Index]

Finally, we’ll multiply the genotypes by the additive coefficients and apply the offset:

additive <- geno %*% getAddCoefs(pop) + getAddOffset(pop)
additive[1:5]
## [1] 2.781772 1.175381 3.110365 7.288447 9.648976

Compare with the additive component for the first five individuals given by getComponents:

getComponents(pop)$Additive[1:5]
## [1] 2.781772 1.175381 3.110365 7.288447 9.648976

The function getEpistasis returns a matrix:

head(getEpistasis(pop))
##            [,1]      [,2]        [,3]       [,4]       [,5]       [,6]
## [1,]  0.8553348 -1.816332 -1.01715111 -1.1092825  0.2942212 -0.4858312
## [2,] -0.2755026  1.160872 -0.95639563  1.9766330 -0.4096100 -0.4858312
## [3,] -1.8301219  0.439532 -0.21109694 -0.5035787  0.9606001  0.2129665
## [4,] -0.2755026  1.160872 -0.95639563  1.9766330 -0.4096100 -0.5633134
## [5,]  1.0945040 -0.912906  0.07263521  0.6669743 -0.0944977 -0.3541265
## [6,] -1.6056155 -0.578183 -0.95639563  0.5544912 -0.5063063 -0.5633134
##            [,7]        [,8]       [,9]       [,10]      [,11]      [,12]
## [1,]  1.1413977  0.47924036  0.4067707 -0.30821989 -1.3984891 -1.2634749
## [2,] -0.1620100 -1.10007378 -0.4267641 -0.08389557 -1.3984891  0.4468833
## [3,]  0.3190867 -0.07650839 -0.4346787 -0.30821989 -0.4658299  0.6318572
## [4,] -0.6592138  0.38088234 -0.4267641 -0.08389557 -1.3984891  0.4468833
## [5,] -0.4529702 -0.62532834 -0.4267641 -0.08389557 -1.3984891  0.4468833
## [6,] -0.6592138  0.38088234 -0.4267641 -0.08389557  1.4884951 -0.6570730
##           [,13]       [,14]     [,15]      [,16]       [,17]       [,18]
## [1,]  0.9403157  0.23780027 -0.331380 -0.8697625 -0.08313758 -1.36407548
## [2,]  2.4644270  0.87027403 -0.331380  1.4367200  0.46359948  0.03629806
## [3,] -0.1226447 -0.07772658  1.177807  0.3302283 -0.08313758 -0.15041481
## [4,]  1.3519757 -0.98402442 -0.331380 -0.1199799  0.46359948 -0.44195411
## [5,]  0.7100724  0.04510164 -0.331380  1.4367200  0.46359948 -0.37779421
## [6,] -2.4075977  0.35951762  1.497382  1.4367200  1.17612349 -0.71202626

The rows in this matrix are the individuals; the columns are the contributions of each interaction to the overall epistatic component. By summing the rows and applying the epistatic offset to each value, we can derive the contribution of epistasis to each individual’s phenotype:

epistatic <- rowSums(getEpistasis(pop)) + getEpiOffset(pop)
epistatic[1:5]
## [1] -5.1664274  3.7513827  0.3337473 -0.3440490  0.4039665

We can similarly compare this result with the epistatic component for the first five individuals given by getComponents:

getComponents(pop)$Epistatic[1:5]
## [1] -5.1664274  3.7513827  0.3337473 -0.3440490  0.4039665

We can thus easily derive both the additive and epistatic components for each individual. This can be further replicated for individuals not in the population.

Suppose you have a matrix of genotypes (geno2) for five individuals not in the population:

geno2 <- geno2[, getQTL(pop)$Index]
geno2
##      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14]
## [1,]    0    2    0    0    0    0    1    2    1     0     1     2     1     2
## [2,]    1    2    2    1    1    1    2    2    1     1     1     1     1     1
## [3,]    1    0    2    0    1    1    2    2    1     0     1     0     0     1
## [4,]    2    0    1    0    1    1    1    1    1     2     1     2     1     1
## [5,]    1    1    0    0    1    2    1    1    1     0     1     1     1     1
##      [,15] [,16] [,17] [,18] [,19] [,20]
## [1,]     2     0     0     0     0     1
## [2,]     0     0     0     0     1     2
## [3,]     0     1     1     2     0     0
## [4,]     1     1     0     2     1     2
## [5,]     2     1     0     1     2     2

We can find their additive components using the following code:

additive2 <- geno2 %*% getAddCoefs(pop) + getAddOffset(pop)
additive2[1:5]
## [1]  7.658521  6.561471 -6.730842 -8.678346  9.232830

Similarly, we can find their epistatic components using the following code:

epistatic2 <- rowSums(getEpistasis(pop, geno = geno2)) + getEpiOffset(pop)
epistatic2
## [1] -4.1894616  8.3153932 -0.2455596 13.7731383 -2.6643398

Note that this is achieved via the optional geno argument in the getEpistasis function.

Running the simulation

In order to run the simulation, we need to use the runSim function.

popRun <- runSim(pop, generations = 150)

The above command will iterate through 150 generations, with generation 1 being the initial generation supplied.

There are several optional arguments that can be supplied to the simulator to alter selection, recombination and mutation across generations:

Each mating pair produces a number of full-sibling offspring by sampling once from the litter-size probability mass function given by litterDist. The vector can be of arbitrary length, such that in order to specify the possibility of up to \(n\) full-sibling offspring per mating pair, a vector of length \(n + 1\) must be supplied.

Linear ranking selection is a form of weighted stochastic selection: if the individuals in a population of size \(n\) are each given a rank \(r\) based on descending order of phenotypic value (i.e. the individual with the highest phenotypic value is given the rank \(r_1 = 1\) while the individual with the lowest phenotypic value is given the rank \(r_n = n\)), the probability of an individual \(i\) being selected for mating is given by:

\[P(i \textrm{ is selected}) = \frac{2(n - r_i + 1)}{n(n + 1)}\]

For a population of 10, we have the following probabilities of selection for the highest-to-lowest ranked individuals:

n <- 10
pmf <- 2 * (n - 1:n + 1) / (n * (n + 1))
pmf
##  [1] 0.18181818 0.16363636 0.14545455 0.12727273 0.10909091 0.09090909
##  [7] 0.07272727 0.05454545 0.03636364 0.01818182
Probability mass function for linear ranking selection on a population of 10 individuals.

Probability mass function for linear ranking selection on a population of 10 individuals.

epinetr performs selection by first splitting the population into male and female sub-populations. Next, if the round is outside any initial burn-in period, each sub-population is truncated to a proportion of its original size per the values of truncSire and truncDam, respectively.

When linear ranking selection is used, females are then exhaustively sampled, without replacement, for each mating pair using their linear ranking probabilities, as given above; males are sampled for each mating pair using their linear ranking probabilities but with replacement, where they are each only replaced a maximum number of times as specified by breedSire. Random selection occurs in the same manner, but all probabilities are uniform. During any initial burn-in period, random selection is enforced.

Finally, each mating pair produces a number of full-sibling offspring by sampling once from the litter-size probability mass function given by litterDist (with the default guaranteeing two full-sibling offspring per mating pair). Half-siblings occur when sires can mate more than once per round (as given by breedSire) or sires or dams survive beyond one round (as given by roundsSire and roundsDam, respectively). Note that in order to maintain the population size, of the sires and dams earmarked as reaching the end of their breeding lifespan, the youngest may be kept in the population to the next round if there is a shortfall of offspring: this can also result in the appearance of half-siblings.

We can perform different simulation runs on the same population as follows:

popRunRank <- runSim(pop, generations = 150, selection = "ranking")
popRunBurnIn <- runSim(pop, generations = 150, burnIn = 50,
                       selection = "ranking",
                       truncSire = 0.25, truncDam = 0.25,
                       roundsSire = 5, roundsDam = 5,
                       litterDist = c(0.1, 0.3, 0.4, 0.2),
                       breedSire = 7)

To visually compare these runs, we can plot them:

plot(popRun)
A graphical representation of a simulation run using the default parameters.

A graphical representation of a simulation run using the default parameters.

plot(popRunRank)
A graphical representation of a simulation run using linear ranking selection.

A graphical representation of a simulation run using linear ranking selection.

plot(popRunBurnIn)
A graphical representation of a simulation run using ranking selection and a burn-in period of the first 50 generations.

A graphical representation of a simulation run using ranking selection and a burn-in period of the first 50 generations.

Using the allGenoFileName argument in runSim allows the simulator to write a serialised file containing all the genotypes generated during the run. To retrieve such a file, we use the loadGeno function:

popRun <- runSim(pop, generations = 150, allGenoFileName = "geno.epi")
geno <- loadGeno("geno.epi")

This object is a matrix in the same phased format as the genotype matrices supplied to the constructor.

geno[1:5, 1:8]
##      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,]    0    0    0    1    0    0    0    0
## [2,]    0    1    0    0    0    0    0    0
## [3,]    0    0    0    1    0    0    0    0
## [4,]    0    0    0    0    0    0    0    0
## [5,]    0    0    0    1    0    0    0    0

To begin retrieving data from a simulation run, we can use the getPedigree function to return the pedigree data frame for the population across the entire run. For example:

ped <- getPedigree(popRun)
ped[512:517, ]
##      ID Sire Dam    Additive   Epistatic Environmental  Phenotype Sex Round
## 512 512  285 270  0.29517031 -2.27559416    -2.9604406 -4.9408644   F     3
## 513 513  300 387  1.91647134  2.09794968    -0.1416299  3.8727911   M     3
## 514 514  300 387  6.24579155 -1.50367063     1.9541397  6.6962606   F     3
## 515 515  210 407 -0.91639351  0.02459879     1.2679332  0.3761385   F     3
## 516 516  210 407  0.04175244 -3.47847212     2.4024194 -1.0343003   F     3
## 517 517  294 242  4.95907559 -0.99817237    -2.2845855  1.6763177   F     3

Note that the originating round is included in the pedigree of each individual.

The getAlleleFreqRun function returns the allele frequencies for each SNP per generation. For example:

qtl <- getQTL(popRun)$Index
af <- getAlleleFreqRun(popRun)
af[, qtl[1]]
##   [1] 0.3650 0.3725 0.3775 0.3775 0.3925 0.3675 0.3850 0.3675 0.3850 0.3925
##  [11] 0.4050 0.4275 0.4600 0.4375 0.4275 0.4400 0.4200 0.4200 0.3625 0.3425
##  [21] 0.3825 0.4125 0.4050 0.4375 0.4625 0.4525 0.4850 0.4875 0.4575 0.4375
##  [31] 0.4525 0.4500 0.4725 0.4700 0.5100 0.5675 0.5300 0.4850 0.5325 0.5500
##  [41] 0.5600 0.5700 0.5950 0.5725 0.5725 0.6025 0.6600 0.6750 0.6675 0.6800
##  [51] 0.7300 0.7150 0.7375 0.7175 0.7100 0.7000 0.7125 0.7275 0.6950 0.6925
##  [61] 0.6600 0.6875 0.6950 0.7125 0.7325 0.7875 0.7700 0.7525 0.7775 0.7875
##  [71] 0.7375 0.7325 0.7550 0.7350 0.7825 0.7725 0.7625 0.7650 0.7725 0.7850
##  [81] 0.7800 0.7750 0.7575 0.7250 0.7450 0.7250 0.7250 0.7950 0.7900 0.7975
##  [91] 0.8275 0.7775 0.7500 0.7525 0.7250 0.7275 0.7225 0.7350 0.7050 0.6825
## [101] 0.7050 0.6725 0.6325 0.6250 0.6325 0.6575 0.6875 0.6875 0.7025 0.7125
## [111] 0.7525 0.7625 0.7425 0.7000 0.7050 0.6650 0.6725 0.6925 0.6550 0.6075
## [121] 0.6450 0.6625 0.6875 0.6400 0.6575 0.6600 0.6450 0.6475 0.6800 0.6950
## [131] 0.6750 0.6950 0.7075 0.6975 0.7075 0.7375 0.7550 0.7625 0.7200 0.7200
## [141] 0.7350 0.7475 0.7225 0.7275 0.7400 0.7125 0.6900 0.7100 0.7025 0.6925

The getPhased function returns the phased genotype matrix for the current population:

geno <- getPhased(popRun)
geno[1:6, 1:10]
##      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,]    0    0    1    1    1    1    1    1    0     0
## [2,]    0    0    0    0    1    1    0    0    1     1
## [3,]    0    0    1    0    1    0    1    1    0     1
## [4,]    0    0    1    0    1    0    1    1    0     1
## [5,]    0    0    0    0    0    0    1    1    1     1
## [6,]    0    0    0    0    0    0    1    1    1     1

Alternatively, the getGeno function returns the unphased genotype matrix for the current population:

geno <- getGeno(popRun)
geno[1:6, 1:5]
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    0    2    2    2    0
## [2,]    0    0    2    0    2
## [3,]    0    1    1    2    1
## [4,]    0    1    1    2    1
## [5,]    0    0    0    2    2
## [6,]    0    0    0    2    2

Finally, we can create a new subpopulation based on the current population by specifying the IDs to use:

ID <- getComponents(popRun)$ID
ID <- sample(ID, 50)
popRun2 <- getSubPop(popRun, ID)

Using the pedigree dropper

Finally, we can use a pedigree data frame to determine selection, with the data frame giving IDs for each individual as well as its sire and dam IDs. Such a data frame can be retrieved from a previous simulation run using the getPedigree function on the resulting population, or we can instead use a new data frame like so:

pedData[201:210, ]
##      ID Sire Dam
## 201 201  124 111
## 202 202  124 111
## 203 203   59 188
## 204 204   59 188
## 205 205  165  30
## 206 206  165  30
## 207 207  153 112
## 208 208  153 112
## 209 209  119  16
## 210 210  119  16

To use this “pedigree dropper”, we call runSim with the pedigree argument:

popRunPed <- runSim(pop, pedigree = pedData)

The pedigree dropper first sorts the pedigree into the implicit number of generations, then runs the simulation using selection according to the given data frame. (Note that if you’re using pedigree data from a previous run, the number of generations reported by the pedigree dropper may be different from the number of iterations of the simulator that produced the pedigree.)

As usual, we can plot the resulting run:

plot(popRunPed)
A graphical representation of a simulation run using a pedigree data frame.

A graphical representation of a simulation run using a pedigree data frame.

Conclusion

The epinetr package is a flexible suite of functions designed to allow for the analysis of epistasis under a multitude of conditions, with complex interactions being a core component of the simulation. This vignette is intended as an overview of the package, with as much detail as possible included. That said, the help pages for each function will provide further detail.


  1. Barabasi AL, Albert R, “Emergence of scaling in random networks,” Science 286(5439): 509-12, 15 October 1999.↩︎