-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmutation_models_functions.R
More file actions
381 lines (269 loc) · 11.3 KB
/
Copy pathmutation_models_functions.R
File metadata and controls
381 lines (269 loc) · 11.3 KB
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
# ESTIMATORS OF POPULATION SIZE WITH BOUNDARY AND RECURRENT MUTATION MODELS
# Author: Rui Borges
# Sampled frequency spectrum of the recurrent mutation model
# mu : mutation rate
# N : effective population size
# M : number of sampled individuals
sampled_recurrent_sfs <- function(mu,N,M){
# creates an empty vector
ssfs <- rep(NA,M+1)
# calculates the frequencies of the monomorphic counts
# this step guarantees that we do not encounter underflow errors
if (abs(N*mu-1) < 0.00001){
ssfs[1] <- (N-M)/(M+1)
ssfs[M+1] <- ssfs[1]
} else {
ssfs[1] <- exp(lgamma(1-N-N*mu)+lgamma(1-2*N*mu-M)-lgamma(1-N-2*N*mu)-lgamma(1-N*mu-M))
ssfs[M+1] <- ssfs[1]
}
# calculates the frequencies of the polymorphic counts
m <- 1:(M-1)
ssfs[m+1] <- exp(log(N) + log(mu) + lchoose(M,m)+lgamma(N*mu+m)+lgamma(2*N*mu+N)+lgamma(N*mu-m+M)-lgamma(N*mu+1)-lgamma(2*N*mu+M)-lgamma(N*mu+N))
# normalizes and returns the sampled SFS
return(ssfs/sum(ssfs))
}
# Sampled frequency spectrum of the boundary mutation model
# mu : mutation rate
# N : effective population size
# M : number of sampled individuals
sampled_boundary_sfs <- function(mu,N,M){
# creates an empty vector
ssfs <- rep(NA,M+1)
# calculates some harmonic numbers
hn <- digamma(N)-digamma(1)
hm <- digamma(M)-digamma(1)
# calculates the frequencies of the monomorphic counts
ssfs[1] <- 1+mu*N*(hn-hm)
ssfs[M+1] <- 1+mu*N*(hn-hm)
# calculates the frequencies of the polymorphic counts
m <- 1:(M-1)
ssfs[m+1] <- mu*N*M/(m*(M-m))
# normalizes and returns the sampled SFS
return(ssfs/sum(ssfs))
}
# Likelihood of the boundary mutation model
# counts : the empirical sampled site frequency spectrum
# lN : log of the effective population size
# mu : mutation rate
llik_sampled_boundary_sfs <- function(counts,lN,mu){
# calculates the sampled frequency spectrum of the boundary mutation model assuming that
# a sample of M individuals was taken from a population of size N
M <- length(counts)-1
N <- round(exp(lN))
expected_counts <- sampled_boundary_sfs(mu,N,M)
# calculates the log ikelihood
llik <- sum(counts*log(expected_counts))
# returns the likelihood
return(llik)
}
# Likelihood of the recurrent mutation model
# counts : the empirical sampled site frequency spectrum
# lN : log of the effective population size
# mu : mutation rate
llik_sampled_recurrent_sfs <- function(counts,lN,mu){
# calculates the sampled frequency spectrum of the recurrent mutation model assuming that
# a sample of M individuals was taken from a population of size N
M <- length(counts)-1
N <- round(exp(lN))
expected_counts <- sampled_recurrent_sfs(mu,N,M)
# calculates the log ikelihood
llik <- sum(counts*log(expected_counts))
# returns the likelihood
return(llik)
}
# Metropolis-Hastings on the effective population size under the boundary mutation model
# mcmc : list of objects (this list is automatically created by the mcmc functions)
mh_boundary_N <- function(mcmc){
# samples from a normal proposal
# while avoiding too small or big population sizes
lN1 <- rnorm(1,mcmc$lN,mcmc$tunning)
if (lN1<2 || lN1>120){
return(mcmc)
}
# calculates the likelihood under the boundary mutation model
lk1 <- llik_sampled_boundary_sfs(mcmc$counts,lN1,mcmc$mu)
# Accept-reject step
# in case of acceptance the newly proposed population size and the likelihood are updated
alpha <- exp(lk1-mcmc$lk)
if (alpha > runif(1,0,1)){
mcmc$lN <- lN1
mcmc$lk <- lk1
mcmc$ap <- mcmc$ap +1
}
# returns the updated mcmc list
return(mcmc)
}
# Metropolis-Hastings on the effective population size under the boundary mutation model
# mcmc : list of objects (this list is automatically created by the mcmc functions)
mh_recurrent_N <- function(mcmc){
# samples from a normal proposal
# while avoiding too small or big population sizes
lN1 <- rnorm(1,mcmc$lN,mcmc$tunning)
if (lN1<2 || lN1>100){
return(mcmc)
}
# calculates the likelihood under the recurrent mutation model
lk1 <- llik_sampled_recurrent_sfs(mcmc$counts,lN1,mcmc$mu)
# Accept-reject step
# in case of acceptance the newly proposed population size and the likelihood are updated
alpha <- exp(lk1-mcmc$lk)
if (alpha > runif(1,0,1)){
mcmc$lN <- lN1
mcmc$lk <- lk1
mcmc$ap <- mcmc$ap +1
}
# returns the updated mcmc list
return(mcmc)
}
# Markov Chain Monte Carlo simulations to estimate the population size under the recurrent mutation model
# I : number of MCMC iterates (should be a multiple of 100 and higher than 1000; only every 100th iterate are kept!)
# counts : the empirical sampled site frequency spectrum
# mu : mutation rate
mcmc_recurrent <- function(I,counts,mu){
# sets the acceptance probability to 0 and calculates the likelihood for lN
lN <- -log(mu)
ap <- 0
lk <- llik_sampled_recurrent_sfs(counts,lN,mu)
# creates a list that includes the mutation rate, the current population size, likelihood
# proposal tunning and acceptance probability
mcmc <- list(lN=lN,mu=mu,counts=counts,lk=lk,tunning=1,ap=ap)
# vector with the sampled values of N
mcmclN <- rep(NA,(I-1000)/100)
# runs the MCMC
p <- 1
for (i in 1:I){
# Metropolis-Hastings step with the recurrent mutation model
mcmc <- mh_recurrent_N(mcmc)
# saves each 100th iterate
# forces a burning phase of 1000 iterates
if ((i%%100 ==0) && i>1000 ){
# saves the effective population size
mcmclN[p] <- mcmc$lN
p <- p+1
# calculates the acceptance probability
aprob <- mcmc$ap/i
# retunes the proposal variance according to the acceptance probability so that
# the acceptance probability revolves around 0.345
mcmc$tunning <- mcmc$tunning*(-0.8312691*aprob*aprob+2.331269*aprob+0.5)
}
}
# returns the vector of sampled population sizes
return(mcmclN/log(10))
}
# Markov Chain Monte Carlo simulations to estimate the population size under the boundary mutation model
# I : number of MCMC iterates (should be a multiple of 100 and higher than 1000; only every 100th iterate are kept!)
# counts : the empirical sampled site frequency spectrum
# mu : mutation rate
mcmc_boundary <- function(I,counts,mu){
# sets the acceptance probability to 0 and calculates the likelihood for lN
lN <- -log(mu)
ap <- 0
lk <- llik_sampled_boundary_sfs(counts,lN,mu)
# creates a list that includes the mutation rate, the current population size, likelihood
# proposal tunning and acceptance probability
mcmc <- list(lN=lN,mu=mu,counts=counts,lk=lk,tunning=1,ap=ap)
# vector with the sampled values of N
mcmclN <- rep(NA,(I-1000)/100)
# runs the MCMC
p <- 1
for (i in 1:I){
# Metropolis-Hastings step with the boundary mutation model
mcmc <- mh_boundary_N(mcmc)
# saves each 100th iterate
# forces a burning phase of 1000 iterates
if ((i%%100 ==0) && i>1000 ){
# saves the effective population size
mcmclN[p] <- mcmc$lN
p <- p+1
# calculates the acceptance probability
aprob <- mcmc$ap/i
# returns the proposal variance according to the acceptance probability so that
# the acceptance probability revolves around 0.345
mcmc$tunning <- mcmc$tunning*(-0.8312691*aprob*aprob+2.331269*aprob+0.5)
}
}
# returns the vector of sampled population sizes
return(mcmclN/log(10))
}
# Markov Chain Monte Carlo simulations to estimate the population size under the recurrent mutation model and uncertain mu
# I : number of MCMC iterates (should be a multiple of 100 and higher than 1000; only every 100th iterate are kept!)
# counts : the empirical sampled site frequency spectrum
# mu : mutation rate
# sd : standard deviation of the mutation rate: samples are taken from a Normal(mu,sd)
mcmc_recurrent_umu <- function(I,counts,mu,sd){
# sets the acceptance probability to 0 and calculates the likelihood for lN
lN <- -log(mu)
ap <- 0
lk <- llik_sampled_recurrent_sfs(counts,lN,mu)
# creates a list that includes the mutation rate, the current population size, likelihood
# proposal tunning and acceptance probability
mcmc <- list(lN=lN,mu=mu,sd=sd,counts=counts,lk=lk,tunning=0.1,ap=ap)
# vector with the sampled values of N
mcmclN <- rep(NA,(I-1000)/100)
# runs the MCMC
p <- 1
for (i in 1:I){
# Metropolis-Hastings step with the recurrent mutation model
mcmc <- mh_recurrent_N(mcmc)
# saves each 100th iterate
# forces a burning phase of 1000 iterates
if ((i%%100 ==0) && i>1000 ){
# saves the effective population size
mcmclN[p] <- mcmc$lN
p <- p+1
# calculates the acceptance probability
aprob <- mcmc$ap/i
# returns the proposal variance according to the acceptance probability so that
# the acceptance probability revolves around 0.345
mcmc$tunning <- mcmc$tunning*(-0.8312691*aprob*aprob+2.331269*aprob+0.5)
# samples a new mutation rate
# updates the mutation rate and the likelihood
mcmc$mu <- rnorm(1,mu,sd) # other distribution could be used
mcmc$lk <- llik_sampled_recurrent_sfs(mcmc$counts,mcmc$lN,mcmc$mu)
mcmc <- mh_recurrent_N(mcmc)
}
}
# returns the vector of sampled population sizes
return(mcmclN/log(10))
}
# Markov Chain Monte Carlo simulations to estimate the population size under the boundary mutation model and uncertain mu
# I : number of MCMC iterates (should be a multiple of 100 and higher than 1000; only every 100th iterate are kept!)
# counts : the empirical sampled site frequency spectrum
# mu : mutation rate
# sd : standard deviation of the mutation rate: samples are taken from a Normal(mu,sd)
mcmc_boundary_umu <- function(I,counts,mu,sd){
# sets the acceptance probability to 0 and calculates the likelihood for lN
lN <- -log(mu)
ap <- 0
lk <- llik_sampled_boundary_sfs(counts,lN,mu)
# creates a list that includes the mutation rate, the current population size, likelihood
# proposal tuning and acceptance probability
mcmc <- list(lN=lN,mu=mu,sd=sd,counts=counts,lk=lk,tunning=0.1,ap=ap)
# vector with the sampled values of N
mcmclN <- rep(NA,(I-1000)/100)
# runs the MCMC
p <- 1
for (i in 1:I){
# Metropolis-Hastings step with the boundary mutation model
mcmc <- mh_boundary_N(mcmc)
# saves each 100th iterate
# forces a burning phase of 1000 iterates
if ((i%%100 ==0) && i>1000 ){
# saves the effective population size
mcmclN[p] <- mcmc$lN
p <- p+1
# calculates the acceptance probability
aprob <- mcmc$ap/i
# returns the proposal variance according to the acceptance probability so that
# the acceptance probability revolves around 0.345
mcmc$tunning <- mcmc$tunning*(-0.8312691*aprob*aprob+2.331269*aprob+0.5)
# samples a new mutation rate
# updates the mutation rate and the likelihood
mcmc$mu <- rnorm(1,mu,sd) # other distribution could be used
mcmc$lk <- llik_sampled_boundary_sfs(mcmc$counts,mcmc$lN,mcmc$mu)
mcmc <- mh_boundary_N(mcmc)
}
}
# returns the vector of sampled population sizes
return(mcmclN/log(10))
}