Using the epinetr package

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 QTL, broad-sense heritability of 0.4,
# narrow-sense heritability of 0.2 and overall trait variance of 40.
pop <- Population(popSize = 1000, map = map100snp, alleleFrequencies = runif(100),
                  QTL = 50, broadH2 = 0.4, narrowh2 = 0.2, traitVar = 40)

# 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 QTL.

An epistatic network generated between 50 QTL.

# Inspect initial phenotypic components
head(getComponents(pop))
##   ID Sire Dam   Additive Epistatic Environmental Phenotype       EBV Sex
## 1  1    0   0  0.2916712 -4.657412     0.1569184 -4.208823 -1.614194   M
## 2  2    0   0  2.5999582  1.984639     0.9202915  5.504889 -3.305245   M
## 3  3    0   0  0.4613690  1.903639    -0.1376572  2.227351 -5.342153   F
## 4  4    0   0  2.6191325  2.268482     2.8089547  7.696569  5.835230   F
## 5  5    0   0  3.5942195 -2.894027     1.8049773  2.505170 -4.108830   M
## 6  6    0   0 -1.2369572  1.847769    -0.2923687  0.318443 -6.096500   M
# Run a simulation across 250 generations
pop <- runSim(pop, generations = 250, truncSire = 0.1, truncDam = 0.5)

# 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 QTL 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 QTL:
## 6  11  12  15  18  19  27  30  39  44  46  52  53  65  70  82  84  94  96  98

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 QTL:
## 6  14  19  22  24  30  37  41  48  59  60  72  73  75  76  82  90  92  96  99

epinetr estimates a breeding value for each individual in addition to supplying a true genetic value (TGV). The estimated breeding value (EBV) is calculated by first estimating the heritability using genomic relationship matrix (GRM)1 restricted maximum likelihood (GREML)2, then using the heritability estimate to in turn estimate additive SNP effects via gBLUP3,4. The EBVs are thus a window into how the model generated by epinetr appears under an assumption of additivity.

We can bypass GREML by supplying our own heritability estimate using h2est:

pop <- Population(popSize = 200, map = map100snp, QTL = 20,
                  alleleFrequencies = runif(100), h2est = 0.6)

We can also specify the QTL 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 QTL:
## 11  38  55  62  74  92

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

A full list of QTL 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 QTL:
## 13  14  20  25  28  29  42  46  48  52  53  60  62  66  68  73  80  86  91  95

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 QTL:
## 13  14  20  25  28  29  42  46  48  52  53  60  62  66  68  73  80  86  91  95

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 QTL:
## 13  14  20  25  28  29  42  46  48  52  53  60  62  66  68  73  80  86  91  95

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 QTL:
## 13  14  20  25  28  29  42  46  48  52  53  60  62  66  68  73  80  86  91  95

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

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]  1.9736717  2.4670896 -0.4934179 -2.3026170  1.3157811  3.9473434
##  [7]  0.3289453 -1.3157811 -0.6578906  1.3157811 -0.3289453 -2.3026170
## [13]  2.3026170  0.3289453 -1.4802538  0.6578906 -1.3157811  0.0000000
## [19] -1.8091991 -2.1381444

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.6114962  2.0143703 -0.4028741 -1.8800789  1.0743308  3.2229924
##  [7]  0.2685827 -1.0743308 -0.5371654  1.0743308 -0.2685827 -1.8800789
## [13]  1.8800789  0.2685827 -1.2086222  0.5371654 -1.0743308  0.0000000
## [19] -1.4772049 -1.7457876

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 QTL 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 QTL:
## 13  14  20  25  28  29  42  46  48  52  53  60  62  66  68  73  80  86  91  95

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

A random epistatic network generated between 20 QTL.

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

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

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

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 di 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 QTL to only have additive effects applied, we can use the additive argument, giving the number of QTL not to be included in the epistatic network.

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

An epistatic network where 7 QTL 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 QTL 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 QTL 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 ≥ 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,]    0    0    0    0    0
##  [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    0
##  [9,]    0    0    0    0    0
## [10,]    0    0    0    0    0
## [11,]    0    0    0    0    0
## [12,]    0    1    0    0    0
## [13,]    1    0    0    1    0
## [14,]    0    0    1    1    1
## [15,]    0    0    0    0    0
## [16,]    0    0    0    0    1
## [17,]    0    0    0    0    0
## [18,]    0    0    0    0    0
## [19,]    1    1    1    0    0
## [20,]    0    0    0    0    0

Here we can see that the first interaction is between QTL 13 and QTL 19, and that QTL 14 is included in 3 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 QTL:

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 QTL 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 QTL

As already shown, it is possible to create purely additive QTL; similarly, it is also possible to create purely epistatic QTL. 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.73341130  0.16411765 -0.31306031 -0.82462680 -0.38279958 -4.02139150
##  [7]  0.00000000  0.00000000 -2.00339921  0.00000000  0.47649277 -1.29060122
## [13]  2.83484957 -2.47264580 -2.00710266 -0.30950995 -0.22913474  0.02552006
## [19]  0.54002653  0.32993703

We can see here that QTL 7, 8 and 10 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 QTL in our model, we can explicitly give the SNP IDs of the QTL we want to be purely additive to attachEpiNet.

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

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

Let’s make the first 5 QTL 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 QTL 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,]    0    0    0    0    1    0    0    0    0
##  [7,]    0    0    0    0    0    0    0    1    0
##  [8,]    0    0    0    0    0    0    0    0    1
##  [9,]    1    0    1    1    1    0    0    0    0
## [10,]    0    0    1    0    0    0    0    0    1
## [11,]    0    1    0    0    0    0    0    0    0
## [12,]    0    0    0    1    0    1    0    0    0
## [13,]    0    0    0    0    0    1    1    1    0
## [14,]    1    1    0    0    0    0    0    0    0
## [15,]    0    0    0    0    0    0    1    0    0

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 QTL being purely additive.

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

Let’s make QTL 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]  3.619945218 -2.448614256 -0.070008217 -0.005266226  1.439284168
##  [6]  0.000000000  0.000000000  0.000000000  0.000000000  0.000000000
## [11]  0.363966832  1.325726288  1.386487822 -1.010255263  2.782432153

We now have a population where 5 of the QTL 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 QTL, this corresponds to 3k possible genotypes within the interaction overall, and for this reason, epinetr assigns a set of 3k potential epistatic effects drawn from a normal distribution to each interaction.

Let’s create a new population with 20 QTL, 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 QTL are part of the first interaction:

qtls <- which(getIncMatrix(pop)[, 1] > 0)
qtls
## [1] 12 16 17

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.4783411  0.3842714  0.5961326
## [2,] -0.5380122 -1.0974086 -0.6016741
## [3,]  0.2412810 -0.3549186 -1.8052902
## 
## , , 2
## 
##            [,1]      [,2]       [,3]
## [1,] -0.2551964 -1.068372 -0.1659587
## [2,]  0.5374920 -1.060170  1.3187866
## [3,]  1.2809140 -1.430196  0.5993717
## 
## , , 3
## 
##            [,1]       [,2]       [,3]
## [1,] -0.4309781 -1.3082630 -0.3019815
## [2,] -2.8077323  1.2868283  0.7986960
## [3,] -0.3769380 -0.6044085 -0.1081703

As expected, this is a 33 array of possible effects: the first dimension maps to QTL 12, the second dimension maps to QTL 16 and the third dimension maps to QTL 17. 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 12 has the heterozygous genotype. The possible effects for the interaction are thus given by the following:

interaction1[2, , ]
##            [,1]      [,2]      [,3]
## [1,] -0.5380122  0.537492 -2.807732
## [2,] -1.0974086 -1.060170  1.286828
## [3,] -0.6016741  1.318787  0.798696

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

interaction1[2, 1, ]
## [1] -0.5380122  0.5374920 -2.8077323

Finally, suppose that QTL 17 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] -2.807732

Thus the contribution of this particular interaction to this individual’s overall phenotype is -2.8077323. (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        EBV Sex
## 1  1    0   0 -1.718223  0.6819743    -1.5558726 -2.5921215  2.0301948   F
## 2  2    0   0 -5.822939 -1.1395354     0.7943877 -6.1680868 -0.6509031   M
## 3  3    0   0  9.238017 -2.8393167    -2.0796820  4.3190179  6.8081168   M
## 4  4    0   0  6.522336 -3.5963404    -3.5557232 -0.6297277 13.6755447   F
## 5  5    0   0 -5.783281 -1.2264748     0.9917477 -6.0180078  0.2498856   F
## 6  6    0   0 -2.979039 -2.0476143     0.2795292 -4.7471237 -3.4669111   M

As we can see, GBLUP-based EBVs have also been calculated for each individual.

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

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

Similarly for the epistatic and environmental components:

mean(components$Epistatic)
## [1] 6.493178e-17
var(components$Epistatic)
## [1] 12
mean(components$Environmental)
## [1] -1.663559e-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] -1.928274
getEpiOffset(pop)
## [1] 0.3196198

For the overall phenotypic value, we find the following:

mean(components$Phenotype)
## [1] 1.692635e-16
var(components$Phenotype)
## [1] 39.96434

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

cov(components$Additive, components$Epistatic)
## [1] -0.01500245
cov(components$Additive, components$Environmental)
## [1] 0.137979
cov(components$Environmental, components$Epistatic)
## [1] -0.1408073

However:

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

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 QTL 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] -1.718223 -5.822939  9.238017  6.522336 -5.783281

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

getComponents(pop)$Additive[1:5]
## [1] -1.718223 -5.822939  9.238017  6.522336 -5.783281

The function getEpistasis returns a matrix:

head(getEpistasis(pop))
##           [,1]       [,2]        [,3]         [,4]        [,5]         [,6]
## [1,]  0.537492 -0.1610767 -0.06015522 -0.001979025 -0.62242281 -0.656146838
## [2,]  0.537492 -0.1610767 -0.06015522  1.020355210  0.31634648 -0.656146838
## [3,]  0.241281  0.2505662 -1.52059657  0.500194571 -1.41059988 -0.754512915
## [4,]  0.241281  0.2505662 -1.52059657  0.500194571  1.62976736 -0.534554059
## [5,] -1.060170 -0.4520495 -0.06015522  0.623831247 -0.07831493  0.004105819
## [6,]  0.241281  0.2505662  1.03821369 -0.001979025 -0.68171010  0.140926819
##            [,7]       [,8]       [,9]       [,10]      [,11]      [,12]
## [1,]  0.7310257 -0.2225030  0.3509266 -0.01675756  0.4158013  0.4350874
## [2,] -0.4194999 -1.5838575  0.3509266 -0.01675756 -0.6120003  0.2174080
## [3,] -0.5324607 -0.4238416 -0.2364046 -0.16658294  0.3646302 -0.2107372
## [4,]  0.7310257 -0.7820658  0.3464884 -0.15091062  0.1145806 -2.0074192
## [5,]  0.6345645  0.2880140  0.3367879  2.38718712  0.1427509 -0.2223996
## [6,]  0.6345645  0.2880140 -1.3241496 -0.25855431  0.1508106 -0.7680767
##            [,13]       [,14]      [,15]       [,16]       [,17]      [,18]
## [1,]  0.09758333 -0.07183343 -0.1779259  0.14375622 -0.61650545  0.2579880
## [2,] -0.17153144 -0.17972729  0.6411426  1.30240394 -1.47780225 -0.5066751
## [3,]  0.60427115 -0.95785769  0.6411426 -0.46584320  0.09984305  0.8185719
## [4,] -0.31253747 -1.87626551  1.0094285 -0.46584320 -0.61650545 -0.4725948
## [5,] -1.82450630  0.18677114 -0.6513837 -0.80148408 -0.66714254 -0.3325013
## [6,]  0.09758333 -1.87626551 -0.1779259  0.07976315 -0.66714254  0.4668463

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]  0.6819743 -1.1395354 -2.8393167 -3.5963404 -1.2264748

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

getComponents(pop)$Epistatic[1:5]
## [1]  0.6819743 -1.1395354 -2.8393167 -3.5963404 -1.2264748

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    1    2    1    1    1    2    1    1     1     0     2     1     1
## [2,]    1    0    0    1    2    1    0    1    2     0     1     1     0     1
## [3,]    0    0    1    0    0    1    1    1    1     2     1     0     1     0
## [4,]    1    2    1    1    0    1    1    1    1     2     1     2     2     1
## [5,]    1    1    1    1    2    1    2    0    0     0     0     0     1     1
##      [,15] [,16] [,17] [,18] [,19] [,20]
## [1,]     2     1     1     0     0     2
## [2,]     2     1     2     1     1     1
## [3,]     1     2     0     0     1     0
## [4,]     2     1     1     0     0     1
## [5,]     1     1     0     1     1     1

We can find their additive components using the following code:

additive2 <- geno2 %*% getAddCoefs(pop) + getAddOffset(pop)
additive2[1:5]
## [1]  2.734356 -4.996938 -5.944271  8.505971 -1.005717

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

epistatic2 <- rowSums(getEpistasis(pop, geno = geno2)) + getEpiOffset(pop)
epistatic2
## [1] 6.28821801 5.36757043 3.36476045 2.85431356 0.07591666

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:

  • selection determines whether random selection (the default) is employed or linear ranking selection6 is used (by supplying the string “ranking”);
  • fitness determines whether selection occurs based on phenotypic value (the default), true genetic value (by supplying the string “TGV”) or estimated breeding value (by supplying the string “EBV”);
  • truncSire and truncDam give the proportion of sires and dams, respectively, to include in selection, when sorted by descending phenotype (defaulting to 1);
  • burnIn determines how many initial generations will use random selection with no truncation (defaulting to 0);
  • roundsSire and roundsDam give the maximum number of generations for sires and dams, respectively, to survive within the population, assuming there are enough offspring generated to fill the population (defaulting to 1);
  • litterDist is a vector of probabilities for the size of each litter, starting with a litter size of 0 (defaulting to c(0, 0, 1), i.e. each litter will always contain two offspring);
  • breedSire is the maximum number of times a sire can breed within a single generation (defaulting to 10);
  • mutation is the rate of mutation for each SNP;
  • recombination is a vector of probabilities specifying the rate of recombination between consecutive SNPs in the map (obviously excepting consecutive SNPs on different chromosomes);
  • allGenoFileName is a string giving the file name to optionally output the genotypes from all 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 fitness (i.e. the individual with the highest fitness is given the rank r1 = 1 while the individual with the lowest fitness is given the rank rn = 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.

Fitness itself can be based on the phenotypic value, the true genetic value or the estimated breeding value, with the true genetic value simply being the sum of any additive and epistatic components of the phenotypic value; as stated previously, the estimated breeding value is calculated using gBLUP.

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.

As stated above, selection occurs based on fitness: this can be based on the phenotypic value, the true genetic value or the estimated breeding value

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, the youngest sires and dams earmarked as reaching the end of their breeding lifespan 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,
                       truncSire = 0.1, truncDam = 0.5,
                       roundsSire = 5, roundsDam = 5,
                       litterDist = c(0.1, 0.3, 0.4, 0.2),
                       breedSire = 7)
popRunTGV <- runSim(pop, generations = 150,
                    truncSire = 0.1, truncDam = 0.5,
                    fitness = "TGV")
popRunEBV <- runSim(pop, generations = 150,
                    truncSire = 0.1, truncDam = 0.5,
                    fitness = "EBV")

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 truncation selection and a burn-in period of the first 50 generations.

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

plot(popRunTGV)
A graphical representation of a simulation run using truncation selection based on true genetic values.

A graphical representation of a simulation run using truncation selection based on true genetic values.

plot(popRunEBV)
A graphical representation of a simulation run using truncation selection based on estimated breeding values.

A graphical representation of a simulation run using truncation selection based on estimated breeding values.

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        EBV Sex
## 512 512  207 323  2.561868 -1.025932    -0.6741995   0.86173736  8.0590727   M
## 513 513  239 362  3.047986 -2.156720    -0.8376095   0.05365639 10.9189746   M
## 514 514  239 362  3.207147 -1.102844    -4.8755743  -2.77127130  9.9452528   F
## 515 515  326 298 -2.724836  1.380128     1.8562297   0.51152164  5.9776081   M
## 516 516  326 298 -9.477621  1.694027     1.2928194  -6.49077434 -0.3218079   F
## 517 517  237 405 -6.946605 -6.064435     0.8774355 -12.13360413 -2.3030897   M
##     Round
## 512     3
## 513     3
## 514     3
## 515     3
## 516     3
## 517     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.2625 0.2825 0.2975 0.2825 0.2625 0.2500 0.2375 0.2325 0.2050 0.2300
##  [11] 0.2675 0.2675 0.2825 0.3300 0.3425 0.3500 0.3125 0.3250 0.3150 0.3025
##  [21] 0.2875 0.2775 0.2650 0.3250 0.3525 0.3350 0.3325 0.3375 0.3200 0.3500
##  [31] 0.3425 0.3800 0.3775 0.4025 0.3875 0.3475 0.3700 0.3475 0.3550 0.3650
##  [41] 0.3325 0.3150 0.3000 0.3000 0.2975 0.2700 0.2850 0.3200 0.2825 0.3100
##  [51] 0.3150 0.3125 0.3175 0.2825 0.2975 0.3400 0.3200 0.3325 0.3625 0.4100
##  [61] 0.3825 0.3975 0.4375 0.4775 0.4875 0.4800 0.4675 0.4700 0.4500 0.4500
##  [71] 0.4425 0.4350 0.4325 0.4650 0.4300 0.4725 0.4950 0.5000 0.4850 0.4925
##  [81] 0.4650 0.4500 0.4150 0.3575 0.3700 0.3550 0.3525 0.2875 0.2925 0.2675
##  [91] 0.2525 0.2375 0.2025 0.1775 0.1925 0.2075 0.1875 0.1725 0.1950 0.2250
## [101] 0.2200 0.2425 0.2100 0.2075 0.2000 0.2200 0.2100 0.2075 0.2075 0.1950
## [111] 0.2025 0.1825 0.1800 0.1525 0.1200 0.1025 0.1500 0.1225 0.1400 0.1200
## [121] 0.1100 0.1150 0.0650 0.0550 0.0550 0.0650 0.0625 0.0600 0.0450 0.0275
## [131] 0.0350 0.0375 0.0325 0.0125 0.0125 0.0150 0.0175 0.0075 0.0075 0.0100
## [141] 0.0100 0.0050 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000

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    0    0    0    0    0    0    0     0
## [2,]    0    1    0    0    1    0    1    1    1     0
## [3,]    1    0    0    0    0    0    1    0    0     0
## [4,]    0    0    0    0    0    0    0    0    0     0
## [5,]    1    1    0    0    0    0    1    1    0     0
## [6,]    1    1    0    0    0    0    1    1    0     0

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    0    0    0    0
## [2,]    1    0    1    2    1
## [3,]    1    0    0    1    0
## [4,]    0    0    0    0    0
## [5,]    2    0    0    2    0
## [6,]    2    0    0    2    0

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  163 150
## 202 202  163 150
## 203 203   40 189
## 204 204   40 189
## 205 205  180 104
## 206 206  180 104
## 207 207  134  22
## 208 208  134  22
## 209 209  108 116
## 210 210  108 116

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.

References

1.
VanRaden, P. M. Efficient methods to compute genomic predictions. Journal of dairy science 91, 4414–4423 (2008).
2.
Yang, J., Zeng, J., Goddard, M. E., Wray, N. R. & Visscher, P. M. Concepts, estimation and interpretation of SNP-based heritability. Nature genetics 49, 1304–1310 (2017).
3.
Clark, S. A. & Werf, J. van der. Genomic best linear unbiased prediction (gBLUP) for the estimation of genomic breeding values. in Genome-wide association studies and genomic prediction 321–330 (Springer, 2013).
4.
Habier, D., Fernando, R. L. & Garrick, D. J. Genomic BLUP decoded: A look into the black box of genomic prediction. Genetics 194, 597–607 (2013).
5.
Barabási, A.-L. & Albert, R. Emergence of scaling in random networks. science 286, 509–512 (1999).
6.
Goldberg, D. E. & Deb, K. A comparative analysis of selection schemes used in genetic algorithms. in Foundations of genetic algorithms vol. 1 69–93 (Elsevier, 1991).