msprime: Run a slendr model in msprime

View source: R/engines.R

msprimeR Documentation

Run a slendr model in msprime

Description

This function will execute a built-in msprime script and run a compiled slendr demographic model.

Usage

msprime(
  model,
  sequence_length,
  recombination_rate,
  samples = NULL,
  output = NULL,
  random_seed = NULL,
  load = TRUE,
  verbose = FALSE,
  debug = FALSE,
  run = TRUE
)

Arguments

model

Model object created by the compile function

sequence_length

Total length of the simulated sequence (in base-pairs)

recombination_rate

Recombination rate of the simulated sequence (in recombinations per basepair per generation)

samples

A data frame of times at which a given number of individuals should be remembered in the tree-sequence (see schedule_sampling for a function that can generate the sampling schedule in the correct format). If missing, only individuals present at the end of the simulation will be recorded in the tree-sequence output file.

output

Path to the output tree-sequence file. If NULL (the default), tree sequence will be saved to a temporary file.

random_seed

Random seed (if NULL, a seed will be generated between 0 and the maximum integer number available)

load

Should the final tree sequence be immediately loaded and returned? Default is TRUE. The alternative (FALSE) is useful when a tree-sequence file is written to a custom location to be loaded at a later point.

verbose

Write the output log to the console (default FALSE)?

debug

Write msprime's debug log to the console (default FALSE)?

run

Should the msprime engine be run? If FALSE, the command line msprime command will be printed (and returned invisibly as a character vector) but not executed.

Value

A tree-sequence object loaded via Python-R reticulate interface function ts_load (internally represented by the Python object tskit.trees.TreeSequence). Optionally, depending on the value of the arguments load = or run =, nothing or a character vector, respectively.

Examples


init_env()

# load an example model
model <- read_model(path = system.file("extdata/models/introgression", package = "slendr"))

# afr and eur objects would normally be created before slendr model compilation,
# but here we take them out of the model object already compiled for this
# example (in a standard slendr simulation pipeline, this wouldn't be necessary)
afr <- model$populations[["AFR"]]
eur <- model$populations[["EUR"]]
chimp <- model$populations[["CH"]]

# schedule the sampling of a couple of ancient and present-day individuals
# given model at 20 ky, 10 ky, 5ky ago and at present-day (time 0)
modern_samples <- schedule_sampling(model, times = 0, list(afr, 10), list(eur, 100), list(chimp, 1))
ancient_samples <- schedule_sampling(model, times = c(40000, 30000, 20000, 10000), list(eur, 1))

# sampling schedules are just data frames and can be merged easily
samples <- rbind(modern_samples, ancient_samples)

# run a simulation using the msprime back end from a compiled slendr model object
ts <- msprime(model, sequence_length = 1e5, recombination_rate = 0, samples = samples)

# automatic loading of a simulated output can be prevented by `load = FALSE`, which can be
# useful when a custom path to a tree-sequence output is given for later downstream analyses
output_file <- tempfile(fileext = ".trees")
msprime(model, sequence_length = 1e5, recombination_rate = 0, samples = samples,
        output = output_file, load = FALSE, random_seed = 42)
# ... at a later stage:
ts <- ts_load(output_file, model)

summary(ts)

slendr documentation built on June 22, 2024, 6:56 p.m.