-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsynthetic_generation.R
514 lines (409 loc) · 15.1 KB
/
synthetic_generation.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
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
### Validation of JUNIPER's ability to reconstruct epidemics
library(juniper0)
library(simulatR)
library(ape)
library(reshape2)
library(outbreaker2)
library(lubridate)
library(TransPhylo)
args <- as.numeric(commandArgs(TRUE))
# Default params
defaults <- list(
mu_g = 5,
var_g = 5,
mu_s = 5,
var_s = 5,
mu = 2e-5,
N_eff = log(100),
p_sample = 0.5,
trans_sample = NA,
R = 2,
psi = 0.5,
coverage = 1
)
params <- names(defaults)
# Alternative params
alternatives <- list(
mu_g = c(2.5, 10),
var_g = c(2.5, 10),
mu_s = c(2.5, 10),
var_s = c(2.5, 10),
mu = c(1e-5, 4e-5),
N_eff = c(log(20), log(500)),
p_sample = c(0.25, 0.75),
trans_sample = c(0.25, 0.75),
R = c(1.5, 2.5),
psi = c(0.25, 0.75),
coverage = c(0.8, 0.9)
)
# Matrix of parameter inputs per experiment
combos <- as.data.frame(defaults)
for (p in params) {
for (j in 1:length(alternatives[[p]])) {
newrow <- combos[1, ]
newrow[[p]] <- alternatives[[p]][j]
combos <- rbind(combos, newrow)
}
}
compare_outbreaker <- function(i, n_global){
fasta <- read.FASTA(paste0(
"synthetic-outbreaks/experiment_", i, "/input_data/aligned.fasta"
))
dates <- as.Date(gsub(".*\\|", "", names(fasta)))
dates <- as.numeric(difftime(dates, as.Date("2000-01-01"), "days"))
names(fasta) <- NULL
dat <- list(
dna = fasta,
dates = dates,
w_dens = dgamma(1:20, shape = combos$mu_g[i]^2 / combos$var_g[i], rate = combos$mu_g[i] / combos$var_g[i]) # Needs to change for different generation ints...
)
start <- Sys.time()
out <- outbreaker(
data = dat,
config = list(n_iter = n_global, sample_every = 100, pb = T)
)
end <- Sys.time()
runtime <- as.numeric(difftime(end, start, units = "hours"))
save(runtime, file = paste0("synthetic-outbreaks/experiment_", i, "/runtime/o2.RData"))
save(out, file = paste0("synthetic-outbreaks/experiment_", i, "/outbreaker.RData"))
}
### Comparison to BadTrIP
compare_badtrip <- function(i, n_global){
## Write input
fasta <- read.FASTA(paste0("synthetic-outbreaks/experiment_", i, "/input_data/aligned.fasta"))
names <- gsub("\\|.*", "", names(fasta))
# Composition of nucleotides by site
comp <- list()
# Length of genome
N_bases <- length(fasta[[1]])
# Loop over hosts
for (j in 1:length(fasta)) {
comp[[j]] <- matrix(0, nrow = N_bases, ncol = 4)
vcf <- read.delim(
paste0("./synthetic-outbreaks/experiment_", i, "/input_data/vcf/", names[j], ".vcf"),
colClasses = c("character", "integer", "character", "character", "character", "character", "character", "character")
)
for(k in 1:N_bases){
if(fasta[[j]][k] %in% c(
as.raw(136),
as.raw(40),
as.raw(72),
as.raw(24)
)){
if(k %in% vcf$POS){
ind <- match(k, vcf$POS)
alleles <- c(vcf$REF[ind], vcf$ALT[ind])
alleles <- match(alleles, c("A", "C", "G", "T"))
af <- vcf$INFO[ind]
af <- gsub(".*;AF=", "", af)
af <- gsub(";SB=.*", "", af)
af <- as.numeric(af)
comp[[j]][k, alleles] <- round(c(1-af, af) * 10000)
}else{
allele <- match(fasta[[j]][k], c(
as.raw(136),
as.raw(40),
as.raw(72),
as.raw(24)
))
comp[[j]][k, allele] <- 10000
}
}
}
}
# For each position, map A, C, G, T to a random order of the those four nucleotides,
# since BadTRiP behaves differently under different nucleotide prevalences
# and our simulations treat the initial genome as all A's.
shuffles <- list()
for (k in 1:N_bases) {
shuffles[[k]] <- sample(1:4, 4, replace = F)
}
for (j in 1:length(fasta)) {
for (k in 1:N_bases) {
comp[[j]][k, ] <- comp[[j]][k, shuffles[[k]]]
}
comp[[j]] <- paste(
comp[[j]][, 1],
comp[[j]][, 2],
comp[[j]][, 3],
comp[[j]][, 4],
sep = "-"
)
}
alignment <- as.data.frame(comp, col.names = paste0("S", 1:length(fasta)))
if(!dir.exists(
paste0("synthetic-outbreaks/experiment_", i, "/badtrip")
)){
dir.create(paste0("synthetic-outbreaks/experiment_", i, "/badtrip"))
}
write.table(alignment, row.names = F, quote = F, file = paste0("synthetic-outbreaks/experiment_", i, "/badtrip/inputAlignment.txt"), sep = "\t")
# Write dates
dates <- as.Date(gsub(".*\\|", "", names(fasta)))
dates <- as.numeric(difftime(dates, as.Date("2000-01-01"), "days"))
df <- data.frame(x = paste0("H", 1:length(fasta)), y = paste0("S", 1:length(fasta)), z = dates)
write.table(df, file = paste0("synthetic-outbreaks/experiment_", i, "/badtrip/inputSamples.txt"), quote= F, col.names = F, row.names = F, sep = "\t")
epi <- data.frame(x = paste0("H", 1:length(fasta)), y = dates - combos$mu_s[i] - 2 * combos$mu_g[i], z = dates - combos$mu_s[i] + 2 * combos$mu_g[i])
write.table(epi, file = paste0("synthetic-outbreaks/experiment_", i, "/badtrip/inputEpiData.txt"), quote= F, col.names = F, row.names = F, sep = "\t")
system(paste0(
"python3 badtrip_scripts/create_BADTRIP_xml.py -a synthetic-outbreaks/experiment_",
i,
"/badtrip/inputAlignment.txt -e synthetic-outbreaks/experiment_",
i,
"/badtrip/inputEpiData.txt -s synthetic-outbreaks/experiment_",
i,
"/badtrip/inputSamples.txt -m ",
as.integer(n_global),
" -o synthetic-outbreaks/experiment_",
i,
"/badtrip/BADTRIP_setup"
))
start <- Sys.time()
# Run badtrip
system(
paste0(
"/Applications/BEAST_2.7.5/bin/beast -threads 8 synthetic-outbreaks/experiment_", i, "/badtrip/BADTRIP_setup.xml"
)
)
end <- Sys.time()
runtime <- as.numeric(difftime(end, start, units = "hours"))
save(runtime, file = paste0("synthetic-outbreaks/experiment_", i, "/runtime/bt.RData"))
# Extract summary tree
system(
paste0(
"python3 badtrip_scripts/Make_transmission_tree_alternative.py -i synthetic-outbreaks/experiment_", i, "/badtrip/BADTRIP_setup.trees -b 20 -o synthetic-outbreaks/experiment_", i, "/badtrip/summary"
)
)
}
### Comparison to IQTree + TransPhylo
compare_transphylo <- function(i, n_global){
# Directory for iqtree files
dir.create(
paste0(
"synthetic-outbreaks/experiment_", i, "/iqtree"
)
)
# Run IQTree
system(
paste0(
"iqtree2 -s synthetic-outbreaks/experiment_", i, "/input_data/aligned.fasta -m JC --prefix synthetic-outbreaks/experiment_", i, "/iqtree/iqtree-results"
)
)
# Prepare date csv
fasta <- read.FASTA(paste0("synthetic-outbreaks/experiment_", i, "/input_data/aligned.fasta"))
names <- names(fasta)
dates <- as.Date(gsub(".*\\|", "", names))
# Convert dates to years, transphylo doesn't like small date spans in years
dates <- as.numeric(difftime(dates, as.Date("2000-01-01")))
max_year <- 2000 + max(dates)
dates <- as.Date("2000-01-01") + years(dates)
date_csv <- data.frame(name = names, date = dates)
write.csv(date_csv, file = paste0("synthetic-outbreaks/experiment_", i, "/iqtree/date.csv"), row.names = F, quote = F)
# Run TreeTime
system(
paste0(
"treetime --tree synthetic-outbreaks/experiment_",
i,
"/iqtree/iqtree-results.treefile --dates synthetic-outbreaks/experiment_",
i,
"/iqtree/date.csv --aln synthetic-outbreaks/experiment_",
i,
"/input_data/aligned.fasta --outdir synthetic-outbreaks/experiment_",
i,
"/treetime --coalescent skyline --n-skyline 2 --greedy-resolve"
)
)
# Load timed tree
phy <- read.nexus(paste0("synthetic-outbreaks/experiment_", i, "/treetime/timetree.nexus"))
# Convert to binary tree
phy <- multi2di(phy)
# Make branch lengths nonzero
phy$edge.length <- phy$edge.length + 0.01
# Add max date
ptree<-ptreeFromPhylo(phy,dateLastSample= max_year)
#plot(ptree)
start <- Sys.time()
res_TransPhylo <- inferTTree(
ptree,
mcmcIterations=n_global,
w.mean = combos$mu_g[i],
w.std = sqrt(combos$var_g[i]),
ws.mean = combos$mu_s[i],
ws.std = sqrt(combos$var_s[i]),
startOff.r = combos$R[i] * combos$psi[i] / (1 - combos$psi[i]),
startOff.p = 1 - combos$psi[i],
startPi = combos$p_sample[i],
updateOff.r = T,
updateOff.p = F,
updatePi = T,
dateT=max_year + 0.01
)
end <- Sys.time()
runtime <- as.numeric(difftime(end, start, units = "hours"))
save(runtime, file = paste0("synthetic-outbreaks/experiment_", i, "/runtime/tp.RData"))
# We typically run TransPhylo at a very high number of iterations yet achieve relatively low ESSs. Here slim down the posterior samples (without typically altering ESS)
if(n_global > 1e5){
res_TransPhylo <- res_TransPhylo[seq(1000, n_global, 1000)]
}
save(res_TransPhylo, file = paste0("synthetic-outbreaks/experiment_", i, "/res_TransPhylo_slim.RData"))
}
# Generate a simulated epidemic using parameters in combos[i, ] and reconstruct it using each method
validate <- function(
i, # Which experiment number, corresponding to a row in the combos data frame
n_global = 10000, # Number of global iterations for each Juniper method
n_global_outbreaker = 10000, # Number of iterations for outbresker2
n_global_transphylo = 1e6, # Number of iterations for TransPhylo (very slow convergence observed in practice)
n_global_badtrip = 1e5 # Number of iterations for BadTrIP
){
# Create new directory for synthetic outbreaks
if(!dir.exists("synthetic-outbreaks")){
dir.create("synthetic-outbreaks")
}
# Create a new directory, if needed, for experiment i
dname <- paste0("synthetic-outbreaks/experiment_", i)
if(!dir.exists(dname)){
dir.create(dname)
}else{
stop(paste0("A directory called ", dname, " already exists. Please delete it and try again."))
}
dir.create(paste0(dname, "/runtime"))
# For reproducible results that vary by experiment
set.seed(i)
# Simulate epidemic by rejection sampling
done <- F
while(!done){
done <- epi_sim(
a_g = combos$mu_g[i]^2 / combos$var_g[i],
lambda_g = combos$mu_g[i] / combos$var_g[i],
a_s = combos$mu_s[i]^2 / combos$var_s[i],
lambda_s = combos$mu_s[i] / combos$var_s[i],
R = combos$R[i],
psi = combos$psi[i],
mu = combos$mu[i],
N_eff = combos$N_eff[i],
init_genome = rep("A", 10000), # Initialize to genome of all A's
p_samp = combos$p_sample[i],
trans_samp = combos$trans_sample[i],
coverage = combos$coverage[i],
n_obs = 50,
include_root = F,
outdir = paste0("./", dname, "/input_data")
)
}
## Write metadata
fasta <- ape::read.FASTA(paste0("./", dname, "/input_data/aligned.fasta"))
samples <- gsub("\\|.*", "", names(fasta))
dates <- gsub(".*\\|", "", names(fasta))
meta <- data.frame(sample = samples, date = dates)
big_meta <- data.frame(sample = c(samples, "ref_genome"), date = c(dates, "2000-01-01"))
write.csv(meta, file = paste0("./", dname, "/input_data/metadata.csv"), row.names = F, quote = F)
# Run badtrip
set.seed(1)
compare_badtrip(i, n_global_badtrip)
# Run outbreaker
set.seed(1)
compare_outbreaker(i, n_global_outbreaker)
# Run TransPhylo
set.seed(1)
compare_transphylo(i, n_global_transphylo)
# Reconstruct with Juniper, first with perfectly-specified inputs
set.seed(1)
init <- initialize(
n_subtrees = 1, # We will parallelize over simulations, not within each one
n_global = n_global,
indir = paste0(dname, "/input_data"),
a_g = combos$mu_g[i]^2 / combos$var_g[i],
lambda_g = combos$mu_g[i] / combos$var_g[i],
a_s = combos$mu_s[i]^2 / combos$var_s[i],
lambda_s = combos$mu_s[i] / combos$var_s[i],
psi = combos$psi[i],
init_mu = combos$mu[i],
init_N_eff = combos$N_eff[i]
)
start <- Sys.time()
res <- run_mcmc(init)
end <- Sys.time()
runtime <- as.numeric(difftime(end, start, units = "hours"))
save(runtime, file = paste0("synthetic-outbreaks/experiment_", i, "/runtime/juniper_ideal.RData"))
out <- summarize(res)
# Save results for correctly-specified model
save(out, file = paste0("./", dname, "/output_ideal.RData"))
## Reconstruct, with misspecified (i.e. default inputs)
set.seed(1)
init <- initialize(
n_subtrees = 1,
n_global = n_global,
indir = paste0(dname, "/input_data")
)
start <- Sys.time()
res <- run_mcmc(init)
end <- Sys.time()
runtime <- as.numeric(difftime(end, start, units = "hours"))
#save(runtime, file = paste0("synthetic-outbreaks/experiment_", i, "/runtime/juniper_default.RData"))
out <- summarize(res)
# Save results for missipecified model
save(out, file = paste0("./", dname, "/output_misspecified.RData"))
## Reconstruct, next assuming all cases are sampled
## In this case we have to supply the root of the cluster
combined_fasta <- c(
read.FASTA(paste0(dname, "/input_data/ref.fasta")),
read.FASTA(paste0(dname, "/input_data/aligned.fasta"))
)
old_fasta <- read.FASTA(paste0(dname, "/input_data/aligned.fasta"))
# Overwrite the old fasta
write.FASTA(combined_fasta, file = paste0(dname, "/input_data/aligned.fasta"))
# Overwrite metadata
write.csv(big_meta, file = paste0("./", dname, "/input_data/metadata.csv"), row.names = F, quote = F)
set.seed(1)
init <- initialize(
n_subtrees = 1, # We will parallelize over simulations, not within each one
n_global = n_global,
indir = paste0(dname, "/input_data"),
a_g = combos$mu_g[i]^2 / combos$var_g[i],
lambda_g = combos$mu_g[i] / combos$var_g[i],
a_s = combos$mu_s[i]^2 / combos$var_s[i],
lambda_s = combos$mu_s[i] / combos$var_s[i],
psi = combos$psi[i],
init_mu = combos$mu[i],
init_N_eff = combos$N_eff[i],
init_pi = 1,
ongoing = F,
fixed_pi = T,
root = "ref_genome"
)
start <- Sys.time()
res <- run_mcmc(init)
end <- Sys.time()
runtime <- as.numeric(difftime(end, start, units = "hours"))
#save(runtime, file = paste0("synthetic-outbreaks/experiment_", i, "/runtime/juniper_fully_sampled.RData"))
out <- summarize(res)
# Save results for model assuming perfect sequencing
save(out, file = paste0("./", dname, "/output_fully_sampled.RData"))
## Reconstruct using ONLY consensus genomes
unlink(paste0("./", dname, "/input_data/vcf"), recursive=TRUE)
# Overwrite the combined fasta to revert to what we had before
write.FASTA(old_fasta, file = paste0(dname, "/input_data/aligned.fasta"))
# Overwrite metadata
write.csv(meta, file = paste0("./", dname, "/input_data/metadata.csv"), row.names = F, quote = F)
set.seed(1)
init <- initialize(
n_subtrees = 1, # We will parallelize over simulations, not within each one
n_global = n_global,
indir = paste0(dname, "/input_data"),
a_g = combos$mu_g[i]^2 / combos$var_g[i],
lambda_g = combos$mu_g[i] / combos$var_g[i],
a_s = combos$mu_s[i]^2 / combos$var_s[i],
lambda_s = combos$mu_s[i] / combos$var_s[i],
psi = combos$psi[i],
init_mu = combos$mu[i],
init_N_eff = combos$N_eff[i]
)
start <- Sys.time()
res <- run_mcmc(init)
end <- Sys.time()
runtime <- as.numeric(difftime(end, start, units = "hours"))
#save(runtime, file = paste0("synthetic-outbreaks/experiment_", i, "/runtime/juniper_consensus.RData"))
out <- summarize(res)
# Save results for consensus JUNIPER
save(out, file = paste0("./", dname, "/output_consensus.RData"))
}
validate(args[1], args[2], args[3], args[4], args[5])