-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsim_wf.r
61 lines (52 loc) · 1.61 KB
/
sim_wf.r
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
pop_size = function(s,a,r,t) {
num_epoch = length(a)
if (length(r) != num_epoch | length(t) != num_epoch) {
stop('a, r, and t should be the same length')
}
max_epoch = sapply(s,function(x){max(which(t<=x))})
pop_size = a[max_epoch]*exp(r[max_epoch]*(s-t[max_epoch])) #TODO: This is bad on the boundaries
return(pop_size)
}
after_mutation = function(mu1,mu2,p) {
p*(1-mu2)+(1-p)*mu1
}
after_selection = function(s,h,p) {
p*(1 + s*h*(1 - p) + s*p)/(1 + s*p*(2*h*(1 - p) + p))
}
sim_alleles = function(p0,N,s,h,mu1,mu2,tmax,ss=NULL) {
p = p0
n = length(p0)
message("generation 1")
update_freq = floor(tmax/10)
cur_update = update_freq
for (t in 1:tmax) {
if (t == cur_update) {
message(paste0("generation ",t))
cur_update = cur_update + update_freq
}
curN = floor(N(t))
# message(paste(p,collapse=" "))
p = after_mutation(mu1,mu2,p)
# message(paste(p,collapse=" "))
p = after_selection(s,h,p)
# message(paste(p,collapse=" "))
x = rbinom(n,2*curN,p)
if (any(is.na(x))) {
message(paste(p,collapse=" "))
message(paste(x, collapse=" "))
message(curN)
message(t)
invisible(readline(prompt="Press [enter] to continue"))
}
p = x/(2*curN)
# message(paste(p,collapse=" "))
# invisible(readline(prompt="Press [enter] to continue"))
}
if (is.null(ss)) {
x
} else {
print(ss)
print(curN)
rhyper(n,x,2*curN-x,ss)
}
}