-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathanalyse_myeloid.R
453 lines (384 loc) · 11.3 KB
/
analyse_myeloid.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
# @DEPI rna_decontaminated.rds
# @DEPI metadata.rds
# @DEPO rna_myeloid.rds
# @DEPO metadata_myeloid.rds
# @DEPO dge_results_myeloid.rds
library(monocle3)
library(muscat)
library(scuttle)
library(scater)
library(nebula)
library(fgsea)
library(msigdbr)
library(tidyverse)
library(scico)
library(patchwork)
library(ComplexHeatmap)
library(RColorBrewer)
source("common_functions.R")
source("styling.R")
# Load data ---------------------------------------------------------------
nb_metadata <- readRDS("data_generated/metadata.rds")
myeloid_barcodes <-
nb_metadata %>%
filter(cellont_abbr == "M") %>%
pull(cell)
nb <-
readRDS("data_generated/rna_decontaminated.rds") %>%
magrittr::extract(, myeloid_barcodes) %>%
logNormCounts(assay.type = "soupx_counts")
# Alignment and clustering ------------------------------------------------
set.seed(42)
cds_my <-
nb %>%
preprocess_cds(verbose = TRUE) %>%
reduce_dimension(preprocess_method = "PCA", verbose = TRUE) %>%
align_cds(alignment_group = "sample", verbose = TRUE) %>%
reduce_dimension(
reduction_method = "UMAP",
preprocess_method = "Aligned",
verbose = TRUE
) %>%
cluster_cells(k = 20, random_seed = 42, verbose = TRUE)
my_metadata <-
list(
tibble(cell = rownames(colData(cds_my))),
nb_metadata %>%
select(cell, sample, group) %>%
mutate(group = rename_groups(group), sample = rename_patients(sample)),
reducedDim(cds_my, "UMAP") %>%
magrittr::set_colnames(c("UMAP1", "UMAP2")) %>%
as_tibble(rownames = "cell"),
clusters(cds_my) %>%
enframe(name = "cell", value = "subcluster")
) %>%
reduce(left_join, by = "cell") %>%
mutate(
collcluster =
fct_collapse(
subcluster,
"classical mono" = c("2", "4", "5"),
"mDCs" = "7",
"nonclassical mono" = "8",
other_level = "other"
) %>%
fct_relevel("classical mono", "nonclassical mono", "mDCs")
)
# DGE ---------------------------------------------------------------------
## Prepare data ----
nb <- cds_my
tif <-
readRDS("data_generated/metadata.rds") %>%
group_by(sample) %>%
summarise(tif = sum(cellont_abbr == "NB") / n()) %>%
mutate(sample = rename_patients(sample))
nb@colData <-
my_metadata %>%
mutate(
Size_Factor = colData(nb)$Size_Factor
) %>%
left_join(tif, by = "sample") %>%
column_to_rownames("cell") %>%
as("DataFrame")
rowData(nb)[["gene_short_name"]] <- rownames(nb)
## Find genes ----
analyse_dge <- function(cell_type,
ref_group,
other_groups,
collapse_groups = NULL) {
info("Analysing cell type {cell_type}, ",
"{str_c(other_groups, collapse = '/')} vs {ref_group}")
col_metadata <-
colData(nb) %>%
as_tibble(rownames = "cell")
# optionally, collapse group levels
if (!is.null(collapse))
col_metadata <-
col_metadata %>%
mutate(group = fct_collapse(group, !!!collapse_groups))
# subset column metadata, set correct factor levels (reference must be first)
col_metadata <-
col_metadata %>%
filter(
collcluster == cell_type,
group %in% c(ref_group, other_groups)
) %>%
mutate(
group =
group %>%
fct_relevel(ref_group, other_groups) %>%
fct_drop()
)
# subset data
nb_sub <- nb[, col_metadata$cell]
colData(nb_sub) <-
col_metadata %>%
column_to_rownames("cell") %>%
as("DataFrame")
# reorder count matrix as required by nebula
data_grouped <- group_cell(
counts(nb_sub),
id = colData(nb_sub)$sample,
pred = model.matrix(~group + tif, data = colData(nb_sub)),
offset = colData(nb_sub)$Size_Factor
)
# run analysis
res <- nebula(
data_grouped$count,
id = data_grouped$id,
pred = data_grouped$pred,
offset = data_grouped$offset,
verbose = TRUE
)
# format results
res$summary %>%
as_tibble() %>%
mutate(
algorithm = res$algorithm,
convergence = res$convergence,
overdispersion_subject = res$overdispersion$Subject,
overdispersion_cell = res$overdispersion$cell
)
}
set.seed(1)
dge_results_vs_C <- map_dfr(
my_metadata$collcluster %>%
levels() %>%
set_names(),
analyse_dge,
ref_group = "C",
other_groups = c("M", "A", "S"),
.id = "cell_type"
)
dge_results_MNA_vs_other <- map_dfr(
my_metadata$collcluster %>%
levels() %>%
set_names(),
analyse_dge,
ref_group = "other",
other_groups = "M",
collapse_groups = list(other = c("A", "S")),
.id = "cell_type"
)
## Filter genes ----
calc_expression_frequency <- function(data, collapse_groups = NULL) {
# required for calcExprFreqs()
cds <- prepSCE(
nb,
kid = "collcluster",
gid = "group",
sid = "sample",
drop = TRUE
)
# generate table with frequencies on sample level
exp_frq <-
cds %>%
calcExprFreqs() %>%
assays() %>%
as.list() %>%
map_dfr(as_tibble, rownames = "gene", .id = "cell_type") %>%
select(!C:S)
# summarise at group level
exp_frq_groups <-
exp_frq %>%
pivot_longer(C1:S5, names_to = "sample", values_to = "frq") %>%
left_join(
my_metadata %>% distinct(sample, group),
by = "sample"
) %>%
group_by(cell_type, gene, group) %>%
summarise(frq = min(frq))
# optionally, add frequencies in collapsed groups
if (!is.null(collapse_groups))
exp_frq_groups <- bind_rows(
exp_frq_groups,
exp_frq_groups %>%
ungroup() %>%
mutate(
group = fct_collapse(group, !!!collapse_groups, other_level = "unused")
) %>%
filter(group != "unused") %>%
group_by(cell_type, gene, group) %>%
summarise(frq = min(frq))
)
# add columns to input
data %>%
extract(
comparison,
into = c("group", "group_ref"),
regex = "(.)(.+)",
remove = FALSE
) %>%
left_join(
exp_frq_groups,
by = c("cell_type", "gene", "group")
) %>%
left_join(
exp_frq_groups,
by = c("cell_type", "gene", group_ref = "group")
) %>%
select(!c(group, group_ref)) %>%
rename(frq = frq.x, frq_ref = frq.y)
}
gather_dge_results <- function(df, suffix) {
df %>%
select(cell_type, gene, starts_with("logFC_g"), starts_with("p_g")) %>%
pivot_longer(
!c(cell_type, gene),
names_to = c(".value", "comparison"),
names_pattern = "(.+)_group(.+)"
) %>%
mutate(comparison = str_c(comparison, suffix))
}
dge_results_wide <-
bind_rows(
gather_dge_results(dge_results_vs_C, "c"),
gather_dge_results(dge_results_MNA_vs_other, "as")
) %>%
arrange(cell_type, gene) %>%
group_by(comparison, cell_type) %>%
mutate(p_adj = p.adjust(p, method = "fdr")) %>%
ungroup() %>%
calc_expression_frequency(
collapse_groups = list(as = c("A", "S"), c = "C")
)
# identical to the function in analyse_dge.mm
filter_dge_results <- function(data,
max_p = Inf,
max_p_adj = 0.05,
min_abs_log_fc = 1,
min_freq = 0.05,
remove_ribosomal = TRUE) {
res <-
data %>%
filter(
abs(logFC) >= min_abs_log_fc,
p <= max_p,
p_adj <= max_p_adj,
frq >= min_freq | frq_ref >= min_freq
) %>%
mutate(direction = if_else(logFC > 0, "up", "down"))
if (remove_ribosomal) {
ribo_proteins <-
msigdbr(species = "Homo sapiens", category = "C2") %>%
filter(gs_name == "KEGG_RIBOSOME") %>%
pull(human_gene_symbol)
res <-
res %>%
filter(!gene %in% ribo_proteins)
}
res
}
dge_results_wide_filtered <-
dge_results_wide %>%
filter_dge_results(max_p_adj = Inf, min_abs_log_fc = 0)
dge_results_wide_filtered %>%
filter(abs(logFC) < 10) %>%
ggplot(aes(logFC / log(2), -log10(p_adj))) +
geom_point(alpha = .1)
# GSEA --------------------------------------------------------------------
# section copied from analyse_dge.R
#' Download enrichr databases in a format that can be used by fgsea.
#'
#' @param dbs Databases to download.
#'
#' @return A named list with names deriving from values in `dbs`. Each element
#' is a named list. Names correspond to enrichr terms, values are character
#' vectors that comprise all genes associated with the respective term.
get_enrichr_genesets <- function(dbs) {
dbs %>%
map(
function(db) {
info("Downloading {db}")
url <- paste0(
"https://maayanlab.cloud/Enrichr/geneSetLibrary",
"?mode=text&libraryName=",
db
)
read_lines(url)
}
) %>%
set_names(dbs) %>%
map(
function(db) {
m <- str_match(db, "(.+?)\\t\\t(.+)")
terms <- m[, 2]
genes <- m[, 3] %>% str_split("\\t")
genes %>%
map(stringi::stri_remove_empty) %>%
set_names(terms)
}
)
}
#' Perform gene set enrichment analysis.
#'
#' @param data DGE data as returned by `filter_dge_results()`.
#' @param gene_sets Gene set as returned by `get_enrichr_genesets()`.
#'
#' @return A dataframe, comprising columns "db", "comparison", "cell_type", as
#' well as all columns in the result of `fgseaMultilevel()`.
perform_gsea <- function(data, gene_sets) {
data %>%
distinct(comparison, cell_type) %>%
mutate(db = list(names(gene_sets))) %>%
unnest_longer(db) %>%
pmap_dfr(
function(comparison, cell_type, db) {
ranked_genes <-
data %>%
filter(comparison == {{comparison}}, cell_type == {{cell_type}}) %>%
select(gene, logFC) %>%
deframe()
ranked_genes <- ranked_genes[!is.na(ranked_genes)]
info("GSEA of comparison {comparison}, cell type {cell_type}, ",
"db {db} ({length(ranked_genes)} genes)")
fgseaMultilevel(
gene_sets[[db]],
ranked_genes,
eps = 0,
nPermSimple = 10000
) %>%
as_tibble() %>%
mutate(
db = {{db}},
comparison = {{comparison}},
cell_type = {{cell_type}},
.before = 1
)
}
)
}
# already defined in secion 'enrichment analysis',
# but we repeat it here for convenience
enrichr_dbs <- c(
"GO_Biological_Process_2018",
"GO_Cellular_Component_2018",
"GO_Molecular_Function_2018",
"KEGG_2019_Human",
"WikiPathways_2019_Human",
"MSigDB_Hallmark_2020",
"TRRUST_Transcription_Factors_2019"
)
enrichr_genesets <- get_enrichr_genesets(enrichr_dbs)
# remove mouse genes from TTRUST database
enrichr_genesets$TRRUST_Transcription_Factors_2019 <-
enrichr_genesets$TRRUST_Transcription_Factors_2019 %>%
magrittr::extract(imap_lgl(., ~str_detect(.y, "human"))) %>%
set_names(str_extract, "\\w+")
gsea_results <-
dge_results_wide_filtered %>%
perform_gsea(enrichr_genesets)
# Save data ---------------------------------------------------------------
cds_my %>% saveRDS("data_generated/rna_myeloid.rds")
my_metadata %>% saveRDS("data_generated/metadata_myeloid.rds")
list(
cds = nb,
metadata = my_metadata,
results_vs_C = dge_results_vs_C,
results_MNA_vs_other = dge_results_MNA_vs_other,
results_wide = dge_results_wide,
results_wide_filtered = dge_results_wide_filtered,
gsea = gsea_results,
gene_sets = enrichr_genesets
) %>%
saveRDS("data_generated/dge_results_myeloid.rds")