-
Notifications
You must be signed in to change notification settings - Fork 0
/
3.monocle.rmd
304 lines (254 loc) · 12.2 KB
/
3.monocle.rmd
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
---
title: "Lineage Trajectory w. Monocle"
subtitle: "After removing low-quality nuclei detected with DropletQC"
author: "Marcos Nascimento"
date: "`r Sys.Date()`"
output: html_notebook
---
# Setup
```{r Setup}
library(tidyverse)
library(Seurat)
library(ggplot2)
library(viridis)
library(patchwork)
library(DropletUtils)
library(MetBrewer)
library(future)
library(SeuratWrappers)
library(monocle3)
library(ggtrace)
library(progress)
plan("multicore", workers = 80)
options(future.globals.maxSize= 5*1024*1024^2)
mytheme <- theme_minimal() +
theme(axis.line = element_line(),
axis.ticks = element_line(),)
simple <- NoAxes() + NoLegend()
mysc <- scale_color_viridis(option = "A")
region.pal = c("#5EBFA2", "#F69663", "#731DD8", "#FB7C7E")
inter_type.pal <- c("#191970", "#708090", "#F4A460", "#6B8E23",
"#F08080", "#87CEEB", "#4169E1", "#90EE90",
"#CD5C5C", "#FFD700", "#008B8B", "#FF6347",
"#800080", "#008080", "#FF8C00", "#FF69B4")
```
# Inter.exp
## Loading Data
```{r}
inter.exp_step2<- readRDS("../3.label_transfer/inter.exp_step2.rds")
[email protected] = "RNA"
Idents(inter.exp_step2) = "RNA_snn_res.1"
(DimPlot(inter.exp_step2, label = T, group.by = "RNA_snn_res.0.8", shuffle = T) +
coord_fixed() +
simple +
labs(title = "Clusters")) +
(DimPlot(inter.exp_step2, label = F, group.by = "age", shuffle = T) +
coord_fixed() +
NoAxes() +
labs(title = "Age") +
scale_color_viridis_d(option = "H")) +
(DimPlot(inter.exp_step2, label = T, group.by = "inter_type", shuffle = T) +
coord_fixed() +
simple +
labs(title = "Cell Identity") +
scale_color_manual(values = inter_type.pal))
```
# Converting the seurat object to a CDS object
```{r}
inter.exp_cds = SeuratWrappers::as.cell_data_set(inter.exp_step2)
inter.exp_cds = cluster_cells(reduction_method = "UMAP", cluster_method = "louvain", inter.exp_cds, k = 50)
plot_cells(inter.exp_cds, color_cells_by = "cluster")
plot_cells(inter.exp_cds, color_cells_by = "partition")
inter.exp_cds = learn_graph(inter.exp_cds, close_loop = T,
learn_graph_control = list(nn.k = 10,
minimal_branch_len = 30,
euclidean_distance_ratio = 3,
geodesic_distance_ratio = 0.1))
plot_cells(inter.exp_cds,
color_cells_by = "cluster",
label_groups_by_cluster=FALSE,
label_leaves=F,
label_branch_points=F,
trajectory_graph_color = "cyan") +
coord_fixed()
#Calculating pseudotime
inter.exp_cds <- order_cells(inter.exp_cds)
plot_cells(inter.exp_cds,
color_cells_by = "pseudotime",
label_cell_groups=FALSE,
label_leaves=FALSE,
label_branch_points=FALSE,
label_roots = F,
trajectory_graph_segment_size = 0.7,
trajectory_graph_color = "cyan",
graph_label_size=1.5) +
scale_color_viridis(name = "Pseudotime")+
coord_fixed() +
NoAxes() +
theme(legend.position = "bottom")
ggsave("inter.exp_pseudotime.png", width = 5, height = 4, dpi = 600)
saveRDS(inter.exp_cds, file = "inter.exp_cds.rds", compress = F)
```
## Updating Seurat object
Adding pseudotime to the original seurat object as a metadata column
```{r}
#Identifying distinct lineages for each neuronal subtype in the dataset by selecting distinct branches in the trajectory:
#This is how I manually selected each lineage, connecting the most undifferentiated cells in the immature mix cluster to the cells at the "tips" of each cluster. For reproducibility purposes, I am supplying a .Rdata file (cells_in_each_lineage.Rdata)with the selected cells for each lineage:
#cge.vip.sncg.pax_lineage_cells = choose_graph_segments(inter.exp_cds, return_list = T)
# cge.lamp_lineage_cells = choose_graph_segments(inter.exp_cds, return_list = T)
# mge.chand_lineage_cells = choose_graph_segments(inter.exp_cds, return_list = T)
# mge.lamp_lineage_cells = choose_graph_segments(inter.exp_cds, return_list = T)
# mge.sst_lineage_cells = choose_graph_segments(inter.exp_cds, return_list = T)
# mge.pvalb_lineage_cells = choose_graph_segments(inter.exp_cds, return_list = T)
[email protected][cells_in_each_lineage$cge.vip.sncg.pax_lineage, "CGE.VIP_lin"] = "CGE-VIP/SNCG/PAX6"
[email protected][cells_in_each_lineage$cge.lamp_lineage, "CGE.LAMP5_lin"] = "CGE-LAMP5"
[email protected][cells_in_each_lineage$mge.lamp_lineage, "MGE.LAMP5_lin"] = "MGE-LAMP5"
[email protected][cells_in_each_lineage$mge.chand_lineage, "MGE.CHAND_lin"] = "MGE-Chandelier"
[email protected][cells_in_each_lineage$mge.sst_lineage, "MGE.SST_lin"] = "MGE-SST"
[email protected][cells_in_each_lineage$mge.pvalb_lineage, "MGE.PVALB_lin"] = "MGE-PVALB"
inter.exp_step3<- AddMetaData(
object = inter.exp_step2,
metadata = inter.exp_cds@principal_graph_aux@listData$UMAP$pseudotime,
col.name = "pseudotime"
)
[email protected]$pseudotime = as.numeric(gsub("Inf", NA, [email protected]$pseudotime))
saveRDS(inter.exp_step3, file = "inter.exp_step3.rds")
```
# Plots
## Lineages Pseudotime
```{r}
umap.coords = inter.exp_step3@[email protected]
cells.meta = [email protected]
data = cbind(umap.coords, cells.meta)
ptsize = 0.2
a = ggplot(data, aes(UMAP_1, UMAP_2)) +
geom_point(color = "grey90", size = ptsize) +
geom_point(data = data %>% filter(!is.na(CGE.VIP_lin)), aes(col = pseudotime), size = ptsize) +
scale_color_viridis() +
theme_void() +
coord_fixed() +
labs(title = "CGE-VIP Lineage") +
NoLegend()
b = ggplot(data, aes(UMAP_1, UMAP_2)) +
geom_point(color = "grey90", size = ptsize) +
geom_point(data = data %>% filter(!is.na(CGE.LAMP5_lin)), aes(color = pseudotime), size = ptsize) +
scale_color_viridis() +
theme_void() +
coord_fixed() +
labs(title = "CGE-LAMP5 Lineage") +
NoLegend()
c = ggplot(data, aes(UMAP_1, UMAP_2)) +
geom_point(color = "grey90", size = ptsize) +
geom_point(data = data %>% filter(!is.na(`MGE.LAMP5_lin`)), aes(color = pseudotime), size = ptsize) +
scale_color_viridis() +
theme_void() +
coord_fixed() +
labs(title = "MGE-LAMP5 Lineage") +
NoLegend()
d = ggplot(data, aes(UMAP_1, UMAP_2)) +
geom_point(color = "grey90", size = ptsize) +
geom_point(data = data %>% filter(!is.na(`MGE.CHAND_lin`)), aes(color = pseudotime), size = ptsize) +
scale_color_viridis() +
theme_void() +
coord_fixed() +
labs(title = "MGE-Chandelier Lineage") +
NoLegend()
e = ggplot(data, aes(UMAP_1, UMAP_2)) +
geom_point(color = "grey90", size = ptsize) +
geom_point(data = data %>% filter(!is.na(`MGE.PVALB_lin`)), aes(color = pseudotime), size = ptsize) +
scale_color_viridis() +
theme_void() +
coord_fixed() +
labs(title = "MGE-SST Lineage") +
NoLegend()
f = ggplot(data, aes(UMAP_1, UMAP_2)) +
geom_point(color = "grey90", size = ptsize) +
geom_point(data = data %>% filter(!is.na(`MGE.SST_lin`)), aes(color = pseudotime), size = ptsize) +
scale_color_viridis() +
theme_void() +
coord_fixed() +
labs(title = "MGE-SST Lineage") +
NoLegend()
(a+b+c+d+e+f & theme(plot.title = element_text(family = "Arial", face = "bold", hjust = 0.5))) + plot_layout(nrow = 1)
ggsave("inter.exp_step3_pseudotime_lineages_geompoint.png", width = 16, height = 3, dpi = 600)
```
## Region composition along pseudotime
```{r}
n.bin = 12
g = ggplot([email protected] %>% filter(CGE.VIP_lin == "CGE-VIP/SNCG/PAX6"), aes(pseudotime, fill = fct_rev(region))) +
geom_histogram(bins = n.bin, position = "fill") +
scale_fill_manual(values = rev(region.pal)) +
scale_y_continuous(label = scales::percent, expand = c(0,0), name = "Percentage of cells") +
scale_x_continuous(expand = c(0,0), name = "Pseudotime") +
labs(title = "CGE-VIP/RELN Lineage") +
theme(plot.title = element_text(family = "Arial", face = "bold", hjust = 0.5), legend.title = element_blank())
h = ggplot([email protected] %>% filter(CGE.LAMP5_lin == "CGE-LAMP5"), aes(pseudotime, fill = fct_rev(region))) +
geom_histogram(bins = n.bin, position = "fill") +
scale_fill_manual(values = rev(region.pal)) +
scale_y_continuous(label = scales::percent, expand = c(0,0), name = "Percentage of cells") +
scale_x_continuous(expand = c(0,0), name = "Pseudotime") +
labs(title = "CGE-LAMP5 Lineage") +
theme(plot.title = element_text(family = "Arial", face = "bold", hjust = 0.5), legend.title = element_blank())
i = ggplot([email protected] %>% filter(MGE.LAMP5_lin == "MGE-LAMP5"), aes(pseudotime, fill = fct_rev(region))) +
geom_histogram(bins = n.bin, position = "fill") +
scale_fill_manual(values = rev(region.pal)) +
scale_y_continuous(label = scales::percent, expand = c(0,0), name = "Percentage of cells") +
scale_x_continuous(expand = c(0,0), name = "Pseudotime") +
labs(title = "MGE-LAMP5 Lineage") +
theme(plot.title = element_text(family = "Arial", face = "bold", hjust = 0.5), legend.title = element_blank())
j = ggplot([email protected] %>% filter(MGE.CHAND_lin == "MGE-Chandelier"), aes(pseudotime, fill = fct_rev(region))) +
geom_histogram(bins = n.bin, position = "fill") +
scale_fill_manual(values = rev(region.pal)) +
scale_y_continuous(label = scales::percent, expand = c(0,0), name = "Percentage of cells") +
scale_x_continuous(expand = c(0,0), name = "Pseudotime") +
labs(title = "MGE-Chandelier Lineage") +
theme(plot.title = element_text(family = "Arial", face = "bold", hjust = 0.5), legend.title = element_blank())
k = ggplot([email protected] %>% filter(MGE.SST_lin == "MGE-SST"), aes(pseudotime, fill = fct_rev(region)))+
geom_histogram(bins = n.bin, position = "fill") +
scale_fill_manual(values = rev(region.pal)) +
scale_y_continuous(label = scales::percent, expand = c(0,0), name = "Percentage of cells") +
scale_x_continuous(expand = c(0,0), name = "Pseudotime") +
labs(title = "MGE-SST/PVALB Lineage") +
theme(plot.title = element_text(family = "Arial", face = "bold", hjust = 0.5), legend.title = element_blank())
l = ggplot([email protected] %>% filter(MGE.PVALB_lin == "MGE-PVALB"), aes(pseudotime, fill = fct_rev(region)))+
geom_histogram(bins = n.bin, position = "fill") +
scale_fill_manual(values = rev(region.pal)) +
scale_y_continuous(label = scales::percent, expand = c(0,0), name = "Percentage of cells") +
scale_x_continuous(expand = c(0,0), name = "Pseudotime") +
labs(title = "MGE-SST/PVALB Lineage") +
theme(plot.title = element_text(family = "Arial", face = "bold", hjust = 0.5), legend.title = element_blank())
(g+h+i+j+k+l & theme(plot.title = element_text(family = "Arial", hjust = 0.5), legend.position = "none")) + plot_layout(nrow = 1)
ggsave("inter.exp_pseudotime_lineagecomposition.png", width = 16, height = 2.5, dpi = 600)
```
#Breakdown of lineages for cells in the EC stream
```{r}
ec.stream.lineage.data = data %>% filter(sample == "EC Stream") %>% select(ends_with("_lin"))
extract_single_non_na <- function(row) {
non_na_values <- row[!is.na(row)]
if (length(non_na_values) == 1) {
return(non_na_values)
} else {
return(NA)
}
}
# Apply the function to each row
ec.stream.lineage.data$lineage <- apply(ec.stream.lineage.data, 1, extract_single_non_na)
lineage.order = ec.stream.lineage.data%>% group_by(lineage) %>% summarize(n = n()) %>% drop_na() %>% arrange(-n) %>% pull(lineage)
ec.stream.lineage.data$lineage[is.na(ec.stream.lineage.data$lineage)] = "Overlapping Trajectories"
ec.stream.lineage.data$lineage = factor(ec.stream.lineage.data$lineage, levels = c(lineage.order, "Overlapping Trajectories"))
ec.stream.lineage.data %>%
ggplot(aes(x = "a", fill = lineage)) +
geom_bar() +
scale_y_continuous(expand = c(0,0)) +
scale_fill_manual(name = "Lineage",
values = c("#9370DB", "#556B2F", "#48D1CC", "#4169E1", "#32CD32", "#FF8C00","grey85")) +
labs(y = "Count") +
theme_classic() +
theme(axis.title.x = element_blank(),
axis.text.x = element_blank(),
axis.ticks.x = element_blank())
ggsave("ecstream_lineage_composition.png", width = 4, height = 4.5)
```
```{r}
sessionInfo()
```