Creating rmetasim landscapes

rmetasim landscapes

The main data structure in rmetasim is the 'landscape'. It contains all of the information necessary to specify a simulation rep and contains the exact state of the simulation at any point in time. The implication is that, once built, a landscape can undergo simulation for a period of time and then be:

before moving forward with additional simulation of that landscape.

Components of a landscape

A landscape is just a 'list()' construct in R. Most of the objects contained in the list are themselves list objects.

It is possible to build one 'by hand' but rmetasim implements a set of convenience functions that make this process much less complicated.

The six main sections of the landscape:

The last three are values can change through the course of the simulation. Especially the last two.

intparam

library(rmetasim)
rland <- landscape.new.empty() #creates a skeleton of a landscape
rland <- landscape.new.intparam(rland, h=2, s=2) 

names(rland$intparam)

These values represent:

switchparam

rland <- landscape.new.switchparam(rland)
names(rland$switchparam)

These values represent:

floatparam

rland <- landscape.new.floatparam(rland)
names(rland$floatparam)

demography

S <- matrix(c(0.1, 0, 0.5, 0.3), nrow = 2)
R <- matrix(c(0, 1.1, 0, 0), nrow = 2)
M <- matrix(c(0, 0, 0, 1), nrow = 2)
rland <- landscape.new.local.demo(rland,S,R,M)

S <- matrix(c(rep(0,4),
              rep(0,4),
              rep(0,4),
              rep(0,4)), nrow = 4)

R <- matrix(c(0,0,0,1,
              0,0,0,0,
              0,1,0,0,
              0,0,0,0), byrow=T, nrow = 4)

M <- matrix(c(0,0,0,0,
              0,0,0,.1,
              0,0,0,0,
              0,.1,0,0), nrow = 4)

rland <- landscape.new.epoch(rland,S=S,R=R,M=M,extinct=c(.01,0),carry=c(100,200))

names(rland$demography)
names(rland$demography$localdem[[1]])
names(rland$demography$localdemK[[1]])
names(rland$demography$epochs[[1]])

These are the local S, R, and M matricies for each subpopulation (localdem) and the S, R, and M matrices for the interpopulation movement of individuals (epochs). Notice the subscripts [[1]] after the localdem and epochs. Both localdem and epochs are lists of individual local demographies and epochs respectively. Since both localdem and epochs are lists the subscripts are necessary to view the contents of a single local demography or epoch.

localdem

LocalS, LocalR, LocalM: the S, R, and M matricies for a particular local demography during exponential growth. This is the only local demography used if density dependence is turned off.

localdemK

LocalS, LocalR, LocalM: the S, R, and M matricies for a particular local demography when at the carrying capacity. Individal demgraphic rates are linearly interpolated between localdem and localdemK based on how close the population size is to carrying capacity.

epochs

loci

rland <- landscape.new.locus(rland,type=0,ploidy=2,mutationrate=0.001,
                   transmission=0,numalleles=5)
rland <- landscape.new.locus(rland,type=1,ploidy=1,mutationrate=0.005,
                   numalleles=3,frequencies=c(.2,.2,.6))
rland <- landscape.new.locus(rland,type=2,ploidy=2,mutationrate=0.007,
                   transmission=0,numalleles=6,allelesize=75)


names(rland$loci[[1]])
names(rland$loci[[1]]$alleles[[1]])

Both loci and alleles are lists with elements of the structure shown above.

individuals

The last major section is individuals. The structure is merely a large matrix with one individual per row. An example row may look like:

rland <- landscape.new.individuals(rland,c(10,15,20,8))
print(rland$individuals[1,])

A (static) example of an individual record with 3 loci (last one haploid) looks like this:.

stage notused birthdate ID matID patID loc1a1 loc1a2 loc2a1 loc2a2 loc3a1

The first six columns are always present and the last five shown here are a product of the choice of number of loci. The first column contains the individuals subpopulation and lifecycle stage (subpopulation = floor(x/rland\$intparam\$stages), lifecycle stage = x mod rland\$intparam\$stages). The second column is currently unused, always 0. The third column contains the generation in which the individual was born or created. The next three contain numerical ids for the individual, its mother and its father. After the first six columns the indivduals genetic code begins. The loci are shown in order with 2 columns for diploid loci and 1 column for haploid loci. The value of these columns represent the allele index of the allele the individual carries.

Creating a landscape

The section above introduces the different helper functions that can be used to create landscapes. Help for any of them can be obtained in the classic R fashion: ?landscape.new.intparams will provide help for the intparam constructor. Typing landscape.new.example without the parentheses will show the code for producing an entire landscape.

Here's a complete example of building a landscape with two populations and two stages in each population

rland <- landscape.new.empty()
rland <- landscape.new.intparam(rland, h = 2, s = 2)
rland <- landscape.new.switchparam(rland, mp = 0)
rland <- landscape.new.floatparam(rland)

S <- matrix(c(0, 0, 1, 0), byrow = TRUE, nrow = 2)
R <- matrix(c(0, 1.1, 0, 0), byrow = TRUE, nrow = 2)
M <- matrix(c(0, 0, 0, 1), byrow = TRUE, nrow = 2)

rland <- landscape.new.local.demo(rland, S, R, M)

S <- matrix(rep(0, 16), nrow = 4)
R <- matrix(rep(0, 16), nrow = 4)
M <- matrix(rep(0, 16), nrow = 4)

rland <- landscape.new.epoch(rland, S = S, R = R, M = M, 
                             carry = c(1000, 1000))

rland <- landscape.new.locus(rland, type = 0, ploidy = 2, 
                             mutationrate = 0.001, transmission = 0, numalleles = 5)
rland <- landscape.new.locus(rland, type = 1, ploidy = 1, 
                             mutationrate = 0.005, numalleles = 3, frequencies = c(0.2, 
                                                                       0.2, 0.6))
rland <- landscape.new.locus(rland, type = 2, ploidy = 2, 
                             mutationrate = 0.007, transmission = 0, numalleles = 6, 
                             allelesize = 75)
rland <- landscape.new.individuals(rland, c(50, 0, 50, 0))

using (not) pipes with magrittr

The magrittr package implements a way to pipe results from one function to another. This is used extensively in dplyr, for example. Since v3.0.0 of rmetasim, you can use pipes to create landscapes (and act on them as well). Here is the same code above using magrittr instead

library(magrittr)
#first set up the matrices for local demographies
S <- matrix(c(0, 0, 1, 0), byrow = TRUE, nrow = 2)
R <- matrix(c(0, 1.1, 0, 0), byrow = TRUE, nrow = 2)
M <- matrix(c(0, 0, 0, 1), byrow = TRUE, nrow = 2)

#and epochs 
S.epoch <- matrix(rep(0, 16), nrow = 4)
R.epoch <- matrix(rep(0, 16), nrow = 4)
M.epoch <- matrix(rep(0, 16), nrow = 4)

##now create the landscape
rland <- landscape.new.empty() %>% 
    landscape.new.intparam(h=2,s=2) %>% 
    landscape.new.switchparam(mp=0) %>%
    landscape.new.floatparam() %>%
    landscape.new.local.demo( S, R, M) %>%
    landscape.new.epoch(S = S.epoch, R = R.epoch, M = M.epoch, 
                        carry = c(1000, 1000)) %>%
    landscape.new.locus(type = 0, ploidy = 2, 
                        mutationrate = 0.001, transmission = 0, numalleles = 5) %>%
    landscape.new.locus(type = 1, ploidy = 1, 
                        mutationrate = 0.005, numalleles = 3, frequencies = c(0.2, 
                                                                  0.2, 0.6)) %>%
    landscape.new.locus(type = 2, ploidy = 2, 
                        mutationrate = 0.007, transmission = 0, numalleles = 6, 
                        allelesize = 75) %>%
    landscape.new.individuals(c(50, 0, 50, 0))

Run a simulation

You can then use the landscape created in the previous section in a simulation. The easiest approach is to use landscape.simulate(). Here is the result of simulating for 10 time points (usually thought of as years) and then estimating Phi_ST for each locus.

rland <- landscape.simulate(rland,10)
landscape.amova(rland)

With (not) pipes...

Here is the same analysis as the previous section.

rland %>% landscape.simulate(10) %>% landscape.amova()


Try the rmetasim package in your browser

Any scripts or data that you put into this service are public.

rmetasim documentation built on Feb. 8, 2020, 1:06 a.m.