-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdemes.slim
309 lines (284 loc) · 11.8 KB
/
demes.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
source("demes.eidos");
function (void)print_scriptblock(object<SLiMEidosBlock>$ sb) {
cat(sb.start);
if (sb.start != sb.end) {
cat(":" + sb.end);
}
// Remove source lines containing "dbg(self)" before printing.
source = sb.source;
lines = strsplit(source, "\n");
dbg_lines = strcontains(lines, "dbg(self)");
lines = lines[which(!dbg_lines)];
source = paste(lines, sep="\n");
catn(" " + sb.type + "() " + source);
}
// Print the current scriptblock, but only in the "start" generation.
function (void)dbg(object<SLiMEidosBlock>$ self_) {
if (sim.generation == self_.start) {
print_scriptblock(self_);
}
}
function (void)register_scriptblocks(
object<Dictionary>$ model, object<Dictionary> script_blocks, integer$ verbosity
) {
nrows = nrow_dict(script_blocks);
if (nrows > 0) {
for (i in 0:(nrows-1)) {
sb = script_blocks.getRowValues(i);
start = sb.getValue("start");
end = sb.getValue("end");
type = sb.getValue("type");
lines = strsplit(sb.getValue("source"), "\n");
if (verbosity > 0) {
lines = c("dbg(self);", lines);
}
source = "{\n " + paste(lines, sep="\n ") + "\n}";
//catn("# registering " + start + ":" + end + " " + type + "()");
if (type == "early") {
sb = sim.registerEarlyEvent(NULL, source, start, end);
} else if (type == "late") {
sb = sim.registerLateEvent(NULL, source, start, end);
} else {
stop("only early and late scriptblocks are supported");
}
}
}
}
/*
Add SLiM source lines to the script_blocks between the given start and
end times.
*/
function (void)sb_add(
object<Dictionary> script_blocks, string lines, integer$ start,
[Ni$ end = NULL], [string$ type = "late"]
) {
if (isNULL(end)) {
end = start;
}
assert(type == "early" | type == "late");
if (start == 1) {
// The script blocks are registered in generation 1 early(), so we
// must schedule other generation 1 blocks as late() or they won't
// run at all.
assert(type == "late");
}
nrows = nrow_dict(script_blocks);
if (nrows > 0) {
for (i in 0:(nrows-1)) {
sb = script_blocks.getRowValues(i);
sb_start = sb.getValue("start");
sb_end = sb.getValue("end");
sb_type = sb.getValue("type");
if (sb_start == start & sb_end == end & sb_type == type) {
// Append lines to the existing script block.
//catn("# appending " + start + ":" + end + " " + type + "()");
sb_source = sb.getValue("source");
source = paste(c(sb_source, lines), sep="\n");
//catn("# appending: " + source);
sources = script_blocks.getValue("source");
sources[i] = source;
script_blocks.setValue("source", sources);
return;
}
}
}
// Create a new script block.
//catn("# adding " + start + ":" + end + " " + type + "()");
source = paste(lines, sep="\n");
sb = Dictionary("start", start, "end", end, "type", type, "source", source);
script_blocks.appendKeysAndValuesFrom(sb);
}
/*
Construct and register the events that characterise the Demes model.
*/
function (void)demes_schedule_events(object<Dictionary>$ model, [integer$ verbosity=1]) {
script_blocks = Dictionary();
if (sim.modelType != "WF") {
stop("Sorry, non-Wright-Fisher models not yet supported. Patches welcome!");
}
for (deme in model.getValue("demes")) {
name = deme.getValue("name");
id = get_deme_id(model, name);
start_time = deme.getValue("start_time");
epochs = deme.getValue("epochs");
start_size = epochs[0].getValue("start_size");
ancestors = deme.getValue("ancestors");
cloning_rate = epochs[0].getValue("cloning_rate");
selfing_rate = (1 - cloning_rate) * epochs[0].getValue("selfing_rate");
if (length(ancestors) == 0) {
lines = c(
"// " + name + ": conjured from the void",
"sim.addSubpop(" + id + ", " + start_size + ");",
"p"+id+".name = \""+name+"\";"
);
if (cloning_rate > 0) {
lines = c(lines, "p" + id + ".setCloningRate(" + cloning_rate + ");");
}
if (selfing_rate > 0) {
lines = c(lines, "p" + id + ".setSelfingRate(" + selfing_rate + ");");
}
sb_add(script_blocks, lines, start_time, type="late");
} else if (length(ancestors) == 1) {
anc_id = get_deme_id(model, ancestors[0]);
lines = c(
"// " + name + ": split from " + ancestors[0],
"sim.addSubpopSplit(" + id + ", " + start_size + ", " + anc_id + ");",
"p"+id+".name = \""+name+"\";",
"p"+id+".setValue(\"ancestor_id\", "+anc_id+");"
);
if (cloning_rate > 0) {
lines = c(lines, "p" + id + ".setCloningRate(" + cloning_rate + ");");
}
if (selfing_rate > 0) {
lines = c(lines, "p" + id + ".setSelfingRate(" + selfing_rate + ");");
}
sb_add(script_blocks, lines, start_time, type="early");
} else {
// multiple ancestors
proportions = deme.getValue("proportions");
assert(length(ancestors) == length(proportions));
anc_id = c();
for (ancestor in ancestors) {
anc_id = c(anc_id, get_deme_id(model, ancestor));
}
lines = c(
"// " + name + ": admixture of " + paste(ancestors),
"sim.addSubpop(" + id + ", " + start_size + ");",
"p"+id+".name = \""+name+"\";",
"p" + id + ".setMigrationRates(c(" + paste(anc_id, sep=", ") +
"), c(" + paste(proportions, sep=", ") + "));"
);
if (cloning_rate > 0) {
lines = c(lines, "p" + id + ".setCloningRate(" + cloning_rate + ");");
}
if (selfing_rate > 0) {
lines = c(lines, "p" + id + ".setSelfingRate(" + selfing_rate + ");");
}
sb_add(script_blocks, lines, start_time, type="early");
zeros = rep(0, length(proportions));
lines = c(
"// " + name,
"p" + id + ".setMigrationRates(c(" + paste(anc_id, sep=", ") +
"), c(" + paste(zeros, sep=", ") + "));"
);
sb_add(script_blocks, lines, start_time, type="late");
}
i = 0;
for (epoch in epochs) {
start_time = epoch.getValue("start_time");
end_time = epoch.getValue("end_time");
size_function = epoch.getValue("size_function");
start_size = epoch.getValue("start_size");
if (size_function == "constant") {
if (i > 0) {
prev_size = epochs[i-1].getValue("end_size");
if (start_size != prev_size) {
lines = c(
"// " + name + ": previous N=" + prev_size,
"p" + id + ".setSubpopulationSize(" + start_size + ");"
);
sb_add(script_blocks, lines, start_time + 1, type="early");
}
}
} else if (size_function == "exponential") {
end_size = epoch.getValue("end_size");
lines = c(
"// " + name + ": exponential "
+ ifelse(start_size < end_size, "growth", "decline"),
"start_size = " + start_size + ";",
"end_size = " + end_size + ";",
"r = log(end_size / start_size);",
"gx = (sim.generation - self.start + 1) / (self.end - self.start + 1);",
"size = asInteger(round(start_size * exp(r * gx)));",
"p" + id + ".setSubpopulationSize(size);"
);
sb_add(script_blocks, lines, start_time + 1, end_time, type="early");
} else {
stop("unexpected size_function: " + size_function);
}
cloning_rate = epoch.getValue("cloning_rate");
selfing_rate = (1 - cloning_rate) * epoch.getValue("selfing_rate");
do_selfing = F;
do_cloning = F;
if (i > 0) {
do_selfing = selfing_rate != epochs[i-1].getValue("selfing_rate");
do_cloning = cloning_rate != epochs[i-1].getValue("cloning_rate");
}
if (do_selfing) {
lines = c("p" + id + ".setSelfingRate(" + selfing_rate + ");");
sb_add(script_blocks, lines, start_time + 1, type="early");
}
if (do_cloning) {
lines = c("p" + id + ".setCloningRate(" + cloning_rate + ");");
sb_add(script_blocks, lines, start_time + 1, type="early");
}
i = i + 1;
}
}
g_end = model.getValue("end_time");
migrations = model.getValue("migrations");
if (length(migrations) > 0) {
for (migration in migrations) {
start_time = migration.getValue("start_time");
end_time = migration.getValue("end_time");
source = migration.getValue("source");
dest = migration.getValue("dest");
source_id = get_deme_id(model, source);
dest_id = get_deme_id(model, dest);
rate = migration.getValue("rate");
lines = c(
"// " + source + " -> " + dest,
"p" + dest_id + ".setMigrationRates("+source_id+", "+rate+");"
);
sb_add(script_blocks, lines, start_time + 1, type="early");
if (end_time != g_end) {
lines = c(
"// " + source + " -| " + dest,
"p" + dest_id + ".setMigrationRates("+source_id+", 0);"
);
sb_add(script_blocks, lines, end_time, type="late");
}
}
}
pulses = model.getValue("pulses");
if (length(pulses) > 0) {
for (pulse in pulses) {
sources = pulse.getValue("sources");
dest = pulse.getValue("dest");
dest_id = get_deme_id(model, dest);
time = pulse.getValue("time");
proportions = pulse.getValue("proportions");
assert(length(sources) == length(proportions));
i = 0;
for (source in sources) {
proportion = proportions[i];
source_id = get_deme_id(model, source);
lines = c(
"// " + source + " -> " + dest,
"p" + dest_id + ".setMigrationRates("+source_id+", "+proportion+");"
);
sb_add(script_blocks, lines, time, type="early");
lines = c(
"// " + source + " -| " + dest,
"p" + dest_id + ".setMigrationRates("+source_id+", 0);"
);
sb_add(script_blocks, lines, time, type="late");
i = i + 1;
}
}
}
for (deme in model.getValue("demes")) {
name = deme.getValue("name");
id = get_deme_id(model, name);
epochs = deme.getValue("epochs");
end_time = epochs[length(epochs)-1].getValue("end_time");
if (end_time != g_end) {
lines = c(
"// " + name + ": extinction",
"p" + id + ".setSubpopulationSize(0);"
);
sb_add(script_blocks, lines, end_time, type="late");
}
}
register_scriptblocks(model, script_blocks, verbosity);
}