-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiloDE_tutorial.Rmd
More file actions
206 lines (159 loc) · 7.25 KB
/
miloDE_tutorial.Rmd
File metadata and controls
206 lines (159 loc) · 7.25 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
### Setup
```{r}
library(tidyverse)
library(miloDE)
suppressMessages(library(MouseGastrulationData))
library(scuttle)
suppressMessages(library(miloR))
suppressMessages(library(uwot))
library(scran)
library(reshape2)
#library(scWGCNA)
suppressMessages(library(Seurat))
library(viridis)
library(ggpubr)
library(BiocParallel)
ncores = 4
mcparam = MulticoreParam(workers = ncores)
register(mcparam)
# If matrix packge goes crazy again:
# remotes::install_version("Matrix", version ="1.5.4.1")
```
###miloDE tutorial
###1. Load data
```{r}
sce = suppressMessages(MouseGastrulationData::Tal1ChimeraData())
# downsample to few selected cell types
cts = c("Spinal cord" , "Haematoendothelial progenitors", "Endothelium" , "Blood progenitors 1" , "Blood progenitors 2")
sce = sce[, sce$celltype.mapped %in% cts]
sce$celltype.mapped[sce$celltype.mapped == "Haematoendothelial progenitors"] = "Haem. prog-s."
sce = sce[!rownames(sce) == "tomato-td" , ]
sce = logNormCounts(sce)
sce$tomato = sapply(sce$tomato , function(x) ifelse(isTRUE(x) , "Tal1_KO" , "WT"))
# for this exercise, we focus on 3000 highly variable genes (for computational efficiency)
dec.sce = modelGeneVar(sce)
hvg.genes = getTopHVGs(dec.sce, n = 3000)
sce = sce[hvg.genes , ]
rowdata = as.data.frame(rowData(sce))
rownames(sce) = rowdata$SYMBOL
set.seed(32)
umaps = as.data.frame(uwot::umap(reducedDim(sce , "pca.corrected")))
reducedDim(sce , "UMAP") = umaps
umaps = cbind(as.data.frame(colData(sce)) , reducedDim(sce , "UMAP"))
names(EmbryoCelltypeColours)[names(EmbryoCelltypeColours) == "Haematoendothelial progenitors"] = "Haem. prog-s."
cols_ct = EmbryoCelltypeColours[names(EmbryoCelltypeColours) %in% unique(umaps$celltype.mapped)]
p = ggplot(umaps , aes(x = V1 , y = V2 , col = celltype.mapped)) +
geom_point() +
scale_color_manual(values = cols_ct) +
facet_wrap(~tomato) +
theme_bw() +
labs(x = "UMAP-1", y = "UMAP-2")
p
```
### 2. Assign neighbourhoods
```{r}
# 2.1 Estimate k -> neighbourhood size
stat_k = estimate_neighbourhood_sizes(sce, k_grid = seq(10,40,5) , order = 2, prop = 0.1 , filtering = TRUE, reducedDim_name = "pca.corrected" , plot_stat = TRUE)
stat_k
# We will use k=20, order=2 –> that returns an average neighbourhood size ~400 cells.
kable(stat_k , caption = "Neighbourhood size distribution ~ k")
# 2.2 Assign neighborhoods
set.seed(32)
sce_milo = assign_neighbourhoods(sce , k = 20 , order = 2, filtering = TRUE , reducedDim_name = "pca.corrected" , verbose = F)
nhoods_sce = nhoods(sce_milo)
# assign cell types for nhoods
nhood_stat_ct = data.frame(Nhood = 1:ncol(nhoods_sce) , Nhood_center = colnames(nhoods_sce))
nhood_stat_ct = miloR::annotateNhoods(sce_milo , nhood_stat_ct , coldata_col = "celltype.mapped")
p = plot_milo_by_single_metric(sce_milo, nhood_stat_ct, colour_by = "celltype.mapped" , layout = "UMAP" , size_range = c(1.5,3) , edge_width = c(0.2,0.5)) +
scale_fill_manual(values = cols_ct , name = "Cell type")
p
```
###3. DE testing
```{r}
# 1. Calculate AUC per neighbourhood
stat_auc = suppressWarnings(calc_AUC_per_neighbourhood(sce_milo , sample_id = "sample" , condition_id = "tomato", min_n_cells_per_sample = 1, BPPARAM = mcparam))
p = plot_milo_by_single_metric(sce_milo, stat_auc, colour_by = "auc" , layout = "UMAP" , size_range = c(1.5,3) , edge_width = c(0.2,0.5)) +
scale_fill_viridis(name = "AUC")
p
# 2. DE testing
de_stat = de_test_neighbourhoods(sce_milo ,
sample_id = "sample",
design = ~tomato,
covariates = c("tomato"),
subset_nhoods = stat_auc$Nhood[!is.na(stat_auc$auc)],
output_type = "SCE",
plot_summary_stat = TRUE,
layout = "UMAP", BPPARAM = mcparam ,
verbose = T)
```
###4. Take a look at the results
```{r}
# 1 Get neighbourhood ranking by the extent of DE
stat_de_magnitude = rank_neighbourhoods_by_DE_magnitude(de_stat)
p1 = plot_milo_by_single_metric(sce_milo, stat_de_magnitude, colour_by = "n_DE_genes" , layout = "UMAP" , size_range = c(1.5,3) , edge_width = c(0.2,0.5)) +
scale_fill_viridis(name = "# DE genes")
p2 = plot_milo_by_single_metric(sce_milo, stat_de_magnitude, colour_by = "n_specific_DE_genes" , layout = "UMAP" , size_range = c(1.5,3) , edge_width = c(0.2,0.5)) +
scale_fill_viridis(name = "# specific\nDE genes" , option = "inferno")
p = ggarrange(p1,p2)
p
```
###Use scWGCNA to detect gene modules
```{r}
get_wgcna_modules = function(de_stat , subset_hoods = NULL ,
n_hoods_sig.thresh = 2 ,
npcs = 5 ,
pval.thresh = 0.1 ){
require(scWGCNA)
require(Seurat)
require(dplyr)
require(reshape2)
set.seed(32)
# subset hoods
if (!is.null(subset_hoods)){
de_stat = de_stat[de_stat$Nhood %in% subset_hoods , ]
}
# focus on genes that DE in at least 2 nhoods
de_stat_per_gene = as.data.frame(de_stat %>% group_by(gene) %>% dplyr::summarise(n_hoods_sig = sum(pval_corrected_across_nhoods < pval.thresh , na.rm = TRUE)))
genes_sig = de_stat_per_gene$gene[de_stat_per_gene$n_hoods_sig >= n_hoods_sig.thresh]
de_stat = de_stat[de_stat$gene %in% genes_sig, ]
de_stat = de_stat[order(de_stat$Nhood) , ]
# discard neighbourhoods in which testing was not performed
de_stat = de_stat[de_stat$test_performed , ]
# for this analysis, set logFC to 0 and pvals to 1 if they are NaN
de_stat$logFC[is.na(de_stat$logFC)] = 0
de_stat$pval[is.na(de_stat$pval)] = 1
de_stat$pval_corrected_across_genes[is.na(de_stat$pval_corrected_across_genes)] = 1
de_stat$pval_corrected_across_nhoods[is.na(de_stat$pval_corrected_across_nhoods)] = 1
# set logFC to 0 if pval_corrected_across_nhoods > pval.thresh
de_stat$logFC[de_stat$pval_corrected_across_nhoods >= pval.thresh] = 0
# move the object to Seurat
de_stat = reshape2::dcast(data = de_stat, formula = gene~Nhood, value.var = "logFC")
rownames(de_stat) = de_stat$gene
de_stat = de_stat[,2:ncol(de_stat)]
obj.seurat <- CreateSeuratObject(counts = de_stat)
DefaultAssay(obj.seurat) <- "RNA"
obj.seurat = FindVariableFeatures(obj.seurat)
# scale
obj.seurat[["RNA"]]@scale.data = as.matrix(obj.seurat[["RNA"]]@data)
obj.seurat = RunPCA(obj.seurat , npcs = npcs)
# run scwgcna
clusters_scwgcna = run.scWGCNA(p.cells = obj.seurat,
s.cells = obj.seurat,
is.pseudocell = F,
features = rownames(obj.seurat),
less = TRUE , merging = TRUE)
# compile stat
clusters = lapply(1:length(clusters_scwgcna$module.genes) , function(i){
out = data.frame(cluster = i , gene = clusters_scwgcna$module.genes[[i]] , n_genes = length(clusters_scwgcna$module.genes[[i]]))
return(out)
})
clusters = do.call(rbind , clusters)
# add colors
genes_w_colors = clusters_scwgcna$dynamicCols
genes_w_colors = data.frame(gene = names(genes_w_colors) , cluster_color = genes_w_colors)
clusters = merge(clusters , genes_w_colors)
return(clusters)
}
# for simplicity we will focus on genes that are DE in at least 4 neighbourhoods
modules_wgcna = suppressMessages(get_wgcna_modules(convert_de_stat(de_stat) , n_hoods_sig.thresh = 4))
```