-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFigure_5
286 lines (262 loc) · 12.5 KB
/
Figure_5
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
#The source code to reproduce Figure 5.
library(tidyverse)
library(furrr)
library(dyno)
library(dyngen)
library(SingleCellExperiment)
library(spectral)
library(Lamian)
library(tradeSeq)
library(tictoc)
theme_set(theme_bw(base_size = 20))
num.targets <- 250
num.hks <- 250
num.features <- 100
de.rate <- 0.5
ko.rate <- 0
num.cores <- 1
cell.range <- c(100, 500, 1000, 5000)
#parameters and functions
min.freq <- 0.2
max.freq <- 1
length.freq <- 101
sim.num <- 100
ko_gene <- "C1_TF1"
LS <- function(g, time1, exp1, time2 = NULL, exp2 = NULL, min.freq = 0, max.freq = 1, length.freq = 101, m = "generalized", sim.num = 100){
.quiet <- function(x){
sink(tempfile())
on.exit(sink())
invisible(force(x))
}
time1 <- time1[!is.infinite(time1)]
exp1 <- exp1[,!is.infinite(time1)]
if(is.null(time2)){
seq1 <- exp1[which(rownames(exp1) == g),]
ls1 <- spec.lomb(x = time1, y = seq1,
f = seq(min.freq, max.freq, length = length.freq), mode = m) %>% .quiet
return(tibble(gene = g, p.dynamic = min(ls1$p), p.de = NA))
}else{
time2 <- time2[!is.infinite(time2)]
exp2 <- exp2[,!is.infinite(time2)]
seq1 <- exp1[which(rownames(exp1) == g),]
seq2 <- exp2[which(rownames(exp2) == g),]
ls1 <- spec.lomb(x = time1, y = seq1,
f = seq(min.freq, max.freq, length = length.freq), mode = m) %>% .quiet
ls2 <- spec.lomb(x = time2, y = seq2,
f = seq(min.freq, max.freq, length = length.freq), mode = m) %>% .quiet
null.hypothesis <- NULL
for(i in seq(sim.num)){
sim.ls1 <- spec.lomb(x = sample(time1, length(time1)),
y = seq1,
f = seq(min.freq, max.freq, length = length.freq), mode = m) %>% .quiet
sim.ls2 <- spec.lomb(x = sample(time2, length(time2)),
y = seq2,
f = seq(min.freq, max.freq, length = length.freq), mode = m) %>% .quiet
null.hypothesis <- c(null.hypothesis, dist(rbind(sim.ls1$A, sim.ls2$A), method = "canberra"))
}
p <- sum(dist(rbind(ls1$A, ls2$A), method = "canberra") < null.hypothesis) / sim.num
return(tibble(gene = g, p.dynamic = min(c(ls1$p, ls2$p)), p.de = p))
}
}
res <- tibble(method = NA,
cell.num = NA,
test = NA,
time = NA)
model <- "bifurcating"
for(num.cells in cell.range){
set.seed(8)
#model generation
dataset <- dyntoy::generate_dataset(model = model,
num_cells = num.cells,
num_features = num.features,
differentially_expressed_rate = de.rate) %>%
add_root(root_milestone_id = .$prior_information$start_milestones) %>%
add_pseudotime()
save(dataset, file = str_c("./models/dynamicTest_", num.cells, "cells.RData"))
backbone <- backbone_bifurcating()
config <- initialise_model(backbone = backbone,
num_cells = num.cells,
num_tfs = nrow(backbone$module_info),
num_targets = num.targets,
num_hks = num.hks,
num_cores = num.cores,
simulation_params = simulation_default(census_interval = 10,
ssa_algorithm = ssa_etl(tau = 300 / 3600),
experiment_params = simulation_type_wild_type(num_simulations = 100)))
model_common <- config %>%
generate_tf_network() %>%
generate_feature_network() %>%
generate_kinetics() %>%
generate_gold_standard()
model_wt <- model_common %>% generate_cells()
model_ko <- model_common
model_ko$simulation_params$experiment_params <- simulation_type_knockdown(num_simulations = 100,
timepoint = 0,
genes = ko_gene,
num_genes = 1,
multiplier = ko.rate)
model_ko <- model_ko %>% generate_cells()
wt <- model_wt %>% generate_experiment() %>% as_dyno() %>% add_root(root_milestone_id = "sA") %>% add_pseudotime(pseudotime = NULL)
ko <- model_ko %>% generate_experiment() %>% as_dyno() %>% add_root(root_milestone_id = "sA") %>% add_pseudotime(pseudotime = NULL)
save(wt, file = str_c("./models/DEtest_", num.cells, "_wt.RData"))
save(ko, file = str_c("./models/DEtest_", num.cells, "_ko.RData"))
}
for(num.cells in cell.range){
#dynamic test
load(str_c("./models/dynamicTest_", num.cells, "cells.RData"))
counts <- t(as.matrix(dataset$counts))
expression <- t(as.matrix(dataset$expression))
sce <- SingleCellExperiment(assays = List(counts = counts, log2 = log2(counts)))
pseudotime <- dataset$pseudotime
#LS
.pseudotime <- pseudotime / max(pseudotime)
plan(sequential)
tic()
ls.res <- future_map_dfr(rownames(expression), LS, .pseudotime, expression,
.options = furrr_options(seed = 8))
time <- toc()
res <- res %>% add_row(method = "LS",
cell.num = num.cells,
test = "Dynamic test",
time = time$callback_msg %>% str_split(pattern = " ", simplify = TRUE) %>% .[,1] %>% as.numeric)
#tradeSeq
cellWeights <- rep(1, num.cells)
tic()
sce <- fitGAM(counts = counts, pseudotime = pseudotime, cellWeights = cellWeights,
nknots = 6, verbose = FALSE)
assoRes <- associationTest(sce)
time <- toc()
res <- res %>% add_row(method = "tradeSeq (association)",
cell.num = num.cells,
test = "Dynamic test",
time = time$callback_msg %>% str_split(pattern = " ", simplify = TRUE) %>% .[,1] %>% as.numeric)
tic()
sce <- fitGAM(counts = counts, pseudotime = pseudotime, cellWeights = cellWeights,
nknots = 6, verbose = FALSE)
sveRes <- startVsEndTest(sce)
time <- toc()
res <- res %>% add_row(method = "tradeSeq (startVsEnd)",
cell.num = num.cells,
test = "Dynamic test",
time = time$callback_msg %>% str_split(pattern = " ", simplify = TRUE) %>% .[,1] %>% as.numeric)
#Lamian
if(num.cells %% 2 == 0){
.cellanno <- data.frame(Cell = colnames(expression), Sample = rep(c("WT1", "WT2"), each = num.cells / 2))
}else{
.cellanno <- data.frame(Cell = colnames(expression), Sample = rep(c("WT1", "WT2"), c(round(num.cells / 2), round(num.cells / 2) - 1)))
}
.design <- matrix(c(1, 1, 0, 1), ncol = 2, dimnames = list(c("WT1", "WT2"), c("intercept", "group")))
lamian.res <- NULL
tic()
try(lamian.res <- lamian_test(expr = expression,
cellanno = .cellanno,
pseudotime = pseudotime,
design = .design,
test.type = 'time',
permuiter = 100,
ncores = num.cores))
time <- toc()
if(!is.null(lamian.res)){
res <- res %>% add_row(method = "Lamian",
cell.num = num.cells,
test = "Dynamic test",
time = time$callback_msg %>% str_split(pattern = " ", simplify = TRUE) %>% .[,1] %>% as.numeric)
}else{
res <- res %>% add_row(method = "Lamian",
cell.num = num.cells,
test = "Dynamic test",
time = NA)
}
#DE test
load(str_c("./models/DEtest_", num.cells, "_wt.RData"))
load(str_c("./models/DEtest_", num.cells, "_ko.RData"))
wt.counts <- wt$counts %>% as.matrix() %>% t()
ko.counts <- ko$counts %>% as.matrix() %>% t()
wt.expression <- wt$expression %>% as.matrix() %>% t()
wt.counts <- wt$counts %>% as.matrix() %>% t()
wt.pseudotime <- wt$pseudotime
ko.expression <- ko$expression %>% as.matrix() %>% t()
ko.counts <- ko$counts %>% as.matrix() %>% t()
ko.pseudotime <- ko$pseudotime
counts <- cbind(wt.counts, ko.counts)
#LS
plan(sequential)
tic()
res.LS <- future_map_dfr(rownames(wt.expression), LS, wt.pseudotime, wt.expression,
ko.pseudotime, ko.expression, .options = furrr_options(seed = 8))
time <- toc()
res <- res %>% add_row(method = "LS",
cell.num = num.cells,
test = "DE test",
time = time$callback_msg %>% str_split(pattern = " ", simplify = TRUE) %>% .[,1] %>% as.numeric)
#Lamian
.expression <- cbind(wt.expression, ko.expression)
colnames(.expression) <- c(str_c("WT_", colnames(wt.expression)), str_c("KO_", colnames(ko.expression)))
.pseudotime <- c(wt.pseudotime, ko.pseudotime)
names(.pseudotime) <- c(str_c("WT_", colnames(wt.expression)), str_c("KO_", colnames(ko.expression)))
.cellanno <- data.frame(Cell = colnames(.expression),
Sample = rep(c("WT", "KO"), each = num.cells / 2))
.design <- matrix(c(1, 1, 0, 1), ncol = 2, dimnames = list(c("WT", "KO"), c("intercept", "group")))
tic()
res.lamian <- lamian_test(expr = .expression,
cellanno = .cellanno,
pseudotime = .pseudotime,
design = .design,
test.type = 'variable',
overall.only = TRUE,
test.method = "chisq",
permuiter = 100,
ncores = num.cores)
time <- toc()
res <- res %>% add_row(method = "Lamian",
cell.num = num.cells,
test = "DE test",
time = time$callback_msg %>% str_split(pattern = " ", simplify = TRUE) %>% .[,1] %>% as.numeric)
#tradeSeq
colnames(counts) <- colnames(.expression)
.condition <- factor(rep(c("WT", "KO"), each = num.cells), levels = c("WT", "KO"))
.pseudotime <- matrix(c(wt.pseudotime, rep(0, num.cells), rep(0, num.cells), ko.pseudotime), ncol = 2)
.cellweights <- matrix(c(rep(1, num.cells), rep(0, num.cells), rep(0, num.cells), rep(1, num.cells)), ncol = 2)
tic()
gam <- fitGAM(counts = counts, pseudotime = .pseudotime,
cellWeights = .cellweights,
nknots = 6, parallel = FALSE)
res.tradeseq.diffEnd <- diffEndTest(gam)
time <- toc()
res <- res %>% add_row(method = "tradeSeq (diffEndTest)",
cell.num = num.cells,
test = "DE test",
time = time$callback_msg %>% str_split(pattern = " ", simplify = TRUE) %>% .[,1] %>% as.numeric)
tic()
gam <- fitGAM(counts = counts, pseudotime = .pseudotime,
cellWeights = .cellweights,
nknots = 6, parallel = FALSE)
res.tradeseq.pattern <- patternTest(gam)
time <- toc()
res <- res %>% add_row(method = "tradeSeq (patternTest)",
cell.num = num.cells,
test = "DE test",
time = time$callback_msg %>% str_split(pattern = " ", simplify = TRUE) %>% .[,1] %>% as.numeric)
tic()
res.tradeseq.earlyDE <- earlyDETest(gam, knots = c(1, 3))
gam <- fitGAM(counts = counts, pseudotime = .pseudotime,
cellWeights = .cellweights,
nknots = 6, parallel = FALSE)
time <- toc()
res <- res %>% add_row(method = "tradeSeq (earlyDETest)",
cell.num = num.cells,
test = "DE test",
time = time$callback_msg %>% str_split(pattern = " ", simplify = TRUE) %>% .[,1] %>% as.numeric)
}
res$test <- factor(res$test, levels = c("Dynamic test", "DE test"))
res <- res[-1,]
save(res, file = "./res.RData")
res$method <- factor(res$method, levels = c("LS", "tradeSeq (startVsEnd)", "tradeSeq (association)", "tradeSeq (earlyDETest)", "tradeSeq (diffEndTest)", "tradeSeq (patternTest)", "Lamian"))
g <- ggplot(res, aes(x = cell.num %>% as.factor, y = time, fill = method)) +
geom_bar(stat = "identity", position = "dodge") +
xlab("cell number") + ylab("Time (sec)") +
facet_wrap(. ~ test, scales = "free") +
theme(legend.title = element_blank(), legend.position = "bottom", legend.text = element_text(size = 20)) +
geom_text(aes(label = str_c(round(time, 1), " sec"), y = time + 3), position = position_dodge(width = 0.9))
g
ggsave(g, file = "./time_label.pdf", dpi = 300)