-
Notifications
You must be signed in to change notification settings - Fork 1
/
pikas.slim
158 lines (129 loc) · 6.19 KB
/
pikas.slim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
initialize() {
initializeSLiMModelType("nonWF");
initializeSLiMOptions(keepPedigrees=T, dimensionality="xy");
initializeTreeSeq();
defaults = Dictionary(
"SEED", getSeed(),
"SD", 93.20466, // sigma_D, dispersal distance
"SX", 93.20466, // sigma_X, interaction distance for measuring local density
"SM", 93.20466, // sigma_M, mate choice distance
"K", 2.5e-4, // carrying capacity per unit area
"LIFETIME", 3.25, // average life span
"WIDTH", 16299, // width of the simulated area
"HEIGHT", 16299, // height of the simulated area
"BURNIN", 0, // number of ticks before recording
"RUNTIME", 1400, // number of ticks to run the simulation for after burn-in
"L", 2e9, // genome length
"R", 1e-8, // recombination rate
"MU", 0, // mutation rate
"MAP_FILE", "./elevation_map.png",
"MIN_T", -5, // minimum pika survivable temperature
"MAX_T", 28, // maximum pika survivable temperature
"SEASONAL_VAR", 11.35, // seasonal variation (Table 4 from Collados-Lara et al: Jan. 17 = -1.0 C, July 19 = 21.7 C; 1/2 range = 11.35)
"SHOCK", 0.1, // annual temperature fluctuation shock parameter
"PERSISTENCE", 0.98, // annual temperature fluctuation persistence parameter
"ELEVATION_RANGE", c(7539, 13507) * 3.048e-4 // elevation range in ft from map legend -> converting to km
);
// Set up parameters with a user-defined function
setupParams(defaults);
defineConstant("FECUN", 1 / LIFETIME);
defineConstant("RHO", FECUN / ((1 + FECUN) * K));
defineGlobal("PARAMS", defaults);
setSeed(SEED);
initializeMutationRate(MU);
initializeMutationType("m1", 0.5, "f", 0.0);
initializeGenomicElementType("g1", m1, 1.0);
initializeGenomicElement(g1, 0, L-1);
initializeRecombinationRate(R);
// elevation params
defineConstant("ELEVATION_MAP", Image(MAP_FILE));
defineGlobal("ELEVATION", ELEVATION_MAP.floatK * (ELEVATION_RANGE[1]-ELEVATION_RANGE[0]) + ELEVATION_RANGE[0]);
defineGlobal("TEMPERATURE", -10 * ELEVATION + 37); // estimate temperature from elevation (Collados-Lara AJ et al., 2020)
// spatial interaction for local density measurement (competition)
initializeInteractionType(1, "xy", reciprocal=T, maxDistance=3 * SX);
i1.setInteractionFunction("n", 1, SX);
// spatial interaction for mate choice
initializeInteractionType(2, "xy", reciprocal=T, maxDistance=3 * SM);
i2.setInteractionFunction("n", 1, SM);
// vector of temperature fluctuations
fluc = rep(0.0, RUNTIME);
for (i in 1:(RUNTIME-1))
fluc[i] = fluc[i-1]*PERSISTENCE + rnorm(1, 0, SHOCK);
defineConstant("FLUCTUATIONS", fluc);
}
1 first() {
sim.addSubpop("p1", asInteger(K * WIDTH * HEIGHT));
p1.setSpatialBounds(c(0, 0, WIDTH, HEIGHT));
// this map is (only) for visualizing elevation in the GUI
p1.defineSpatialMap("elevation", "xy", ELEVATION_MAP.floatK, interpolate=T, valueRange=c(0,1), colors=c("#0000FF", "#FFFFFF"));
// this map is actually used by the model
spatmap = p1.defineSpatialMap("temperature", "xy", TEMPERATURE, interpolate=T, valueRange=c(0,40), colors=c("#0000FF", "#FFFFFF", "#FF0000"));
defineGlobal("TEMPMAP", spatmap);
p1.individuals.setSpatialPosition(p1.pointUniform(p1.individualCount));
}
first() {
// preparation for the reproduction() callback
i2.evaluate(p1);
}
reproduction() {
// choose our nearest neighbor as a mate, within the max distance
mate = i2.drawByStrength(individual, 1);
if (mate.size())
subpop.addCrossed(individual, mate, count=rpois(1, FECUN));
}
early() {
// disperse offspring
offspring = p1.subsetIndividuals(maxAge=0);
p1.deviatePositions(offspring, "reprising", INF, "n", SD);
// update temperature (0.016 / year; and assuming ticks are years, here)
TEMPMAP.add(0.016); // increase global temperature
catn("sim.cycle: " + sim.cycle + ", temperature range: " + paste(TEMPMAP.range() + FLUCTUATIONS[sim.cycle]));
// calculate % habitable space,
temps = TEMPMAP.gridValues();
mydata = c(p1.individualCount, mean(temps > (MIN_T + SEASONAL_VAR) & temps < (MAX_T - SEASONAL_VAR)));
writeFile(OUTDIR + "/pika_simdata.txt", paste(mydata, sep='\t'), append=T);
// calculate fitness
i1.evaluate(p1);
inds = p1.individuals;
competition = i1.localPopulationDensity(inds);
fitness = 1 / (1 + RHO * competition);
temps = TEMPMAP.mapValue(inds.spatialPosition) + FLUCTUATIONS[sim.cycle];
fitness[temps < (MIN_T + SEASONAL_VAR) | temps > (MAX_T - SEASONAL_VAR)] = 0.0;
inds.fitnessScaling = fitness;
}
2: late() {
// color individuals in GUI:
// darker = happier (closer to mean of habitable temperature range)
// greener = stressed (further from optimal temperature)
optimum = (MAX_T - MIN_T)/2 + MIN_T;
for (ind in p1.individuals) {
ind_score = abs(TEMPMAP.mapValue(ind.spatialPosition) - optimum) / ((MAX_T - MIN_T)/10);
ind.color = rgb2color(c(0, ind_score, 0));
}
}
BURNIN + RUNTIME late() {
sim.treeSeqOutput(OUTPATH, simplify=F);
sim.simulationFinished();
}
function (void)setupParams(object<Dictionary>$ defaults)
{
if (!exists("PARAMFILE")) defineConstant("PARAMFILE", "./params.json");
if (!exists("OUTDIR")) defineConstant("OUTDIR", ".");
defaults.addKeysAndValuesFrom(Dictionary("PARAMFILE", PARAMFILE, "OUTDIR", OUTDIR));
if (fileExists(PARAMFILE)) {
defaults.addKeysAndValuesFrom(Dictionary(readFile(PARAMFILE)));
defaults.setValue("READ_FROM_PARAMFILE", PARAMFILE);
}
defaults.setValue("OUTBASE", OUTDIR + "/out_" + defaults.getValue("SEED"));
defaults.setValue("OUTPATH", defaults.getValue("OUTBASE") + ".trees");
for (k in defaults.allKeys) {
if (!exists(k))
defineConstant(k, defaults.getValue(k));
else
defaults.setValue(k, executeLambda(k + ";"));
}
// print out default values
catn("===========================");
catn("Model constants: " + defaults.serialize("pretty"));
catn("===========================");
}