-
Notifications
You must be signed in to change notification settings - Fork 4
/
simulate_data.R
85 lines (62 loc) · 2.35 KB
/
simulate_data.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
# ------------------------------------------------------------------------------
#' Script to simulate data, ground truth graphs and noisy priors for individual
#' hotspots
#'
#' @author Johann Hawe
#'
# ------------------------------------------------------------------------------
log <- file(snakemake@log[[1]], open="wt")
sink(log)
sink(log, type="message")
# ------------------------------------------------------------------------------
print("Prep libraries, scripts and params")
library(igraph)
library(graph)
library(BDgraph)
source("scripts/priors.R")
source("scripts/lib.R")
source("scripts/simulation/lib.R")
# ------------------------------------------------------------------------------
# Snakemake params and inputs
# inputs
fdata <- snakemake@input[["data"]]
franges <- snakemake@input[["ranges"]]
fpriors <- snakemake@input[["priors"]]
# outputs
fout <- snakemake@output[[1]]
# params
sentinel <- snakemake@params$sentinel
threads <- snakemake@threads
runs <- 1:as.numeric(snakemake@params$runs)
# ------------------------------------------------------------------------------
print("Loading data.")
data <- readRDS(fdata)
nodes <- colnames(data)
ranges <- readRDS(franges)
priors <- readRDS(fpriors)
# restrict to priors for which we also have data available
priors <- priors[rownames(priors) %in% nodes, colnames(priors) %in% nodes]
# ------------------------------------------------------------------------------
print(paste0("Running ", length(runs), " simulations."))
# we use bdgraph.sim internally, we need to set the number of threads which it
# uses to avoid threading issues:
RhpcBLASctl::omp_set_num_threads(1)
RhpcBLASctl::blas_set_num_threads(1)
simulations <- mclapply(runs, function(x) {
set.seed(x)
# create the hidden and observed graphs
graphs <- create_prior_graphs(priors, sentinel, threads=1)
print(paste0("Run ", x, " done."))
# simulate data for ggm
return(simulate_data(graphs, sentinel, data, nodes, threads=1))
}, mc.cores = threads)
names(simulations) <- paste0("run_", runs)
# ------------------------------------------------------------------------------
print("Saving results.")
save(file=fout, simulations, priors, ranges, nodes, data,
runs, fdata, franges, fpriors)
# ------------------------------------------------------------------------------
print("SessionInfo:")
sessionInfo()
sink()
sink(type="message")