R/slimr_scripting.R
slim_function.Rd
Specify an Eidos function to be included in the SLiM script
slim_function(..., name, return_type = "f$", body)
List of arguments specified using Eidos's argument syntax (which includes type specification; see details)
Name of function being created.
Type of the functions return, using Eidos' type syntax (see details)
SLiM / Eidos code to be executed in the body of the function. Can also be an R function, in which case the body of the R function is used.
A slimr_block
object (only useful with the context of a slim_script
) call.
test_sim <- slim_script(
slim_function("o<Subpopulation>$ subpop1", "o<Subpopulation>$ subpop2",
name = "calculateFST",
return_type = "f$", body = function(subpop1, subpop2) {
p1_p = sim.mutationFrequencies(subpop1);
p2_p = sim.mutationFrequencies(subpop2);
mean_p = (p1_p + p2_p) / 2.0;
H_t = 2.0 * mean_p * (1.0 - mean_p);
H_s = p1_p * (1.0 - p1_p) + p2_p * (1.0 - p2_p);
fst = 1.0 - H_s/H_t;
fst = fst[isFinite(fst)]; ## exclude muts where mean_p is 0.0 or 1.0
return(mean(fst));
}),
slim_block_init_minimal(mutation_rate = 1e-6),
slim_block_add_subpops(2, 100),
slim_block(1, 20, late(), {
r_output(calculateFST(p1, p2), "out", do_every = 10)
})
)
test_sim
#> <slimr_script[4]>
#> block_1:function (f$)calculateFST(o<Subpopulation>$ subpop1, o<Subpopulation>$ subpop2) {
#> p1_p = sim.mutationFrequencies(subpop1);
#> p2_p = sim.mutationFrequencies(subpop2);
#> mean_p = (p1_p + p2_p)/2;
#> H_t = 2 * mean_p * (1 - mean_p);
#> H_s = p1_p * (1 - p1_p) + p2_p * (1 - p2_p);
#> fst = 1 - H_s/H_t;
#> fst = fst[isFinite(fst)];
#> return(mean(fst));
#> }
#>
#> block_init:initialize() {
#> initializeMutationRate(1e-06);
#> initializeMutationType("m1", 0.5, "f", 0);
#> initializeGenomicElementType("g1", m1, 1);
#> initializeGenomicElement(g1, 0, 1e+05 - 1);
#> initializeRecombinationRate(1e-08);
#> }
#>
#> block_3:1 early() {
#> {
#> sim.addSubpop(p1, 100);
#> sim.addSubpop(p2, 100);
#> }
#> }
#>
#> block_4:1:20 late() {
#> {calculateFST(p1, p2) -> out}
#> }