-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFigure S10.R
284 lines (214 loc) · 12.5 KB
/
Figure S10.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
#Cell Stem Cell . 2022 Apr 7;29(4):593-609.e7. doi: 10.1016/j.stem.2022.03.002.
library(monocle3)
library(ggplot2)
library(dplyr)
#font
simple_theme <- theme(text = element_blank(),
panel.grid = element_blank(),
axis.title = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank(),
axis.line.x = element_blank(),
axis.line.y = element_blank(), legend.position = "none") ### theme to remove legends and axis/font
simple_themeL <- theme(text = element_text(size=14),
panel.grid = element_blank(),
axis.title = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank(),
axis.line.x = element_blank(),
axis.line.y = element_blank()) #### theme show the legend with large font
#Load FL-EC
cds<-load_cellranger_data("/Users")
#Remove low UMI
colData(cds)$n.umis <- Matrix::colSums(counts(cds))
summary(colData(cds)$n.umis)
qplot(colData(cds)$n.umis, geom="density")
qplot(log10(colData(cds)$n.umis), geom="density")
cds <- cds[,Matrix::colSums(counts(cds)) > 3000]
cds
summary(colData(cds)$n.umis)
cds <- detect_genes(cds, min_expr=0.1)
cds <- cds[,colData(cds)$num_genes_expressed > 1000]
summary(colData(cds)$n.umis)
summary(pData(cds)$num_genes_expressed)
#Preprocess
cds <- preprocess_cds(cds,num_dim = 10)
plot_pc_variance_explained(cds)
cds <- reduce_dimension(cds)
cds <- cluster_cells(cds)
cluster <- clusters(cds)
plot_cells(cds)
#List of all mitochondrial genes by Ensembl ID
mito_genes_updated <- c("ENSMUSG00000064351", "ENSMUSG00000064354", "ENSMUSG00000064370", "ENSMUSG00000064357")
num_mito <- Matrix::colSums(counts(cds[mito_genes_updated]))
cds$n.mito <- num_mito
cds
colData(cds)$n.umis <- Matrix::colSums(counts(cds))
perc_mito <- 100*(cds$n.mito / cds$n.umis)
cds$perc_mito_umis <- perc_mito
cds_mito_filter <- cds[,pData(cds)$perc_mito_umis < 10]
cds_mito_filter
cds<- cds_mito_filter
set.seed(1)
cds <- cluster_cells(cds, resolution=0.0001, random_seed = 1)
cluster <- clusters(cds)
plot_cells(cds, color_cells_by="cluster", group_cells_by="cluster", cell_size = 0.5, group_label_size=5)
cds
plot_cells(cds, genes=c("Cdh5","Ptprc","Pf4","Pecam1"), cell_size = 1, label_cell_groups = F)+ simple_themeL
#Extract EC
cds<-choose_cells(cds)
summary(colData(cds)$n.umis)
summary(pData(cds)$num_genes_expressed)
cds
#Figure S10A-C
#Nichenetr
#Receptor-Ligand interaction analysis by Nichenetr
simple_theme <- theme(text = element_blank(),
panel.grid = element_blank(),
axis.title = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank(),
axis.line.x = element_blank(),
axis.line.y = element_blank(), legend.position = "none") ### theme to remove legends and axis/font
simple_themeL <- theme(text = element_text(size=14),
panel.grid = element_blank(),
axis.title = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank(),
axis.line.x = element_blank(),
axis.line.y = element_blank()) #### theme show the legend with large font
library(nichenetr)
library(tidyverse)
library(patchwork)
#ligand_target_matrix = readRDS(url("https://zenodo.org/record/3260758/files/ligand_target_matrix.rds"))
RES_DIR <- file.path("/Users")
ligand_target_matrix <- readRDS(file.path(RES_DIR, "ligand_target_matrix.rds"))
ligand_target_matrix[1:5,1:5] #target genes in row(yoko), ligand in column(tate)
dim(ligand_target_matrix)
#Convert the ligand-target model from human to mouse symbols.
colnames(ligand_target_matrix) = ligand_target_matrix %>% colnames() %>% convert_human_to_mouse_symbols()
rownames(ligand_target_matrix) = ligand_target_matrix %>% rownames() %>% convert_human_to_mouse_symbols()
ligand_target_matrix = ligand_target_matrix %>% .[!is.na(rownames(ligand_target_matrix)), !is.na(colnames(ligand_target_matrix))]
dim(ligand_target_matrix)
#Perform the NicheNet analysis
#1.Define expressed genes in sender and receiver cell populations
#receiver/target
#location
RES_DIR <- file.path(RES_DIR <- file.path("/Users"))
#Read
cds <- readRDS(file.path(RES_DIR, "Figure4.RDS"))
colData(cds)
#Hash-HSC: engraft vs non-engraft
cds2 <- cds[,(colData(cds)$identifier == "Engraft"),]
colData(cds2)
#For single-cell data generated by the 10x platform in our lab, we don’t use the definition used here, but we consider genes to be expressed in a cell type when they have non-zero values in at least 10% of the cells from that cell type.
cds2 <- detect_genes(cds2, min_expr = 0.1)
rowData(cds2)$use_for_rec_list <- rowData(cds2)$num_cells_expressed > (0.10 * ncol(cds2)) ### detect genes expressed in >10% of cells
cds2_exp <- (subset(rowData(cds2), rowData(cds2)$use_for_rec_list == TRUE))$gene_short_name ###create list of expressed genes
length(cds2_exp)
#1-2 sender/niche
#E14-EC
#location
RES_DIR <- file.path(RES_DIR <- file.path("/Users"))
#Read
cds3 <- readRDS(file.path(RES_DIR, "Figure S10.RDS"))
cds3 <- detect_genes(cds3, min_expr = 0.1)
rowData(cds3)$use_for_rec_list <- rowData(cds3)$num_cells_expressed > (0.10 * ncol(cds3)) ### detect genes expressed in >10% of cells
cds3_exp <- (subset(rowData(cds3), rowData(cds3)$use_for_rec_list == TRUE))$gene_short_name ###create list of genes
length(cds3_exp)
#2: Define the gene set of interest and a background of gene
RES_DIR <- file.path("/Users")
geneset_oi = read.csv(file.path(RES_DIR, "Table.csv"))%>% pull(gene_short_name)
geneset_oi =geneset_oi %>% .[. %in% rownames(ligand_target_matrix)]
head(geneset_oi)
length(geneset_oi)
#background of genes = all the genes expressed in 6-cultured HSC colonies
cds <- detect_genes(cds, min_expr = 0.1)
rowData(cds)$use_for_rec_list <- rowData(cds)$num_cells_expressed > (0.10 * ncol(cds))
cds_exp <- (subset(rowData(cds), rowData(cds)$use_for_rec_list == TRUE))$gene_short_name
length(cds_exp)
background_expressed_genes = cds_exp %>% .[. %in% rownames(ligand_target_matrix)]
head(background_expressed_genes)
#3: Define a set of potential ligands
#Putative ligand-receptor links were gathered from NicheNet’s ligand-receptor data sources.
lr_network = readRDS(url("https://zenodo.org/record/3260758/files/lr_network.rds"))
lr_network = lr_network %>% mutate(from = convert_human_to_mouse_symbols(from), to = convert_human_to_mouse_symbols(to)) %>% drop_na()
lr_network
#Ligand
ligands = lr_network %>% pull(from) %>% unique()
expressed_ligands = intersect(ligands,cds3_exp)
length(expressed_ligands)
#Receptor
receptors = lr_network %>% pull(to) %>% unique()
#Expressed Ligands in Engraft colony
expressed_receptors = intersect(receptors,cds2_exp)
length(expressed_receptors)
#Engraft-EC
lr_network_expressed = lr_network %>% filter(from %in% expressed_ligands & to %in% expressed_receptors)
head(lr_network_expressed)
# 4: Perform NicheNet’s ligand activity analysis on the gene set of interest
potential_ligands = lr_network_expressed %>% pull(from) %>% unique()
head(potential_ligands)
ligand_activities = predict_ligand_activities(geneset = geneset_oi, background_expressed_genes = background_expressed_genes, ligand_target_matrix = ligand_target_matrix, potential_ligands = potential_ligands)
#Ranking of ligand activities by pearson
ligand_activities %>% arrange(-pearson)
write.csv(ligand_activities, "/Users")
best_upstream_ligands = ligand_activities %>% top_n(20, pearson) %>% arrange(-pearson) %>% pull(test_ligand)
head(best_upstream_ligands)
p_hist_lig_activity = ggplot(ligand_activities, aes(x=pearson)) +
geom_histogram(color="black", fill="skyblue") +
geom_vline(aes(xintercept=min(ligand_activities %>% top_n(20, pearson) %>% pull(pearson))), color="red", linetype="dashed", size=1) +
labs(x="ligand activity2 (Engraft-colony)", y = "# ligands") +
theme_classic()
p_hist_lig_activity
best_upstream_ligands%>% intersect(expressed_ligands)
#5: Infer target genes of top-ranked ligands and visualize in a heatmap
active_ligand_target_links_df = best_upstream_ligands %>% lapply(get_weighted_ligand_target_links,geneset = geneset_oi, ligand_target_matrix = ligand_target_matrix, n = 250) %>% bind_rows()
nrow(active_ligand_target_links_df)
head(active_ligand_target_links_df)
active_ligand_target_links = prepare_ligand_target_visualization(ligand_target_df = active_ligand_target_links_df, ligand_target_matrix = ligand_target_matrix, cutoff = 0.01)
nrow(active_ligand_target_links_df)
head(active_ligand_target_links_df)
order_ligands = intersect(best_upstream_ligands, colnames(active_ligand_target_links)) %>% rev()%>% make.names()
order_targets = active_ligand_target_links_df$target %>% unique() %>% intersect(rownames(active_ligand_target_links)) %>% make.names()
rownames(active_ligand_target_links) = rownames(active_ligand_target_links) %>% make.names() # make.names() for heatmap visualization of genes like H2-T23
colnames(active_ligand_target_links) = colnames(active_ligand_target_links) %>% make.names() # make.names() for heatmap visualization of genes like H2-T23
vis_ligand_target = active_ligand_target_links[order_targets,order_ligands] %>% t()
p_ligand_target_network = vis_ligand_target %>% make_heatmap_ggplot("Prioritized ligands","DE genes highly expressed in Engraft-colony", color = "#0457d2",legend_position = "top", x_axis_position = "top",legend_title = "Regulatory potential") + scale_fill_gradient2(low = "whitesmoke", high = "#0457d2", breaks = c(0,0.005,0.01)) + theme(axis.text.x = element_text(face = "italic"))
p_ligand_target_network
p_ligand_target_network + simple_theme
#Ligand-receptor network inference for top-ranked ligands
lr_network_top = lr_network %>% filter(from %in% best_upstream_ligands & to %in% expressed_receptors) %>% distinct(from,to)
best_upstream_receptors = lr_network_top %>% pull(to) %>% unique()
#get the weights of the ligand-receptor interactions as used in the NicheNet model
#weighted_networks = readRDS(url("https://zenodo.org/record/3260758/files/weighted_networks.rds"))
RES_DIR <- file.path("/Users")
weighted_networks <- readRDS(file.path(RES_DIR, "weighted_networks.rds"))
#change to mouse
weighted_networks$lr_sig = weighted_networks$lr_sig %>% mutate(from = convert_human_to_mouse_symbols(from), to = convert_human_to_mouse_symbols(to)) %>% drop_na()
weighted_networks$gr = weighted_networks$gr %>% mutate(from = convert_human_to_mouse_symbols(from), to = convert_human_to_mouse_symbols(to)) %>% drop_na()
head(weighted_networks$lr_sig)
head(weighted_networks$gr)
lr_network_top_df = weighted_networks$lr_sig %>% filter(from %in% best_upstream_ligands & to %in% best_upstream_receptors)
# convert to a matrix
lr_network_top_df = lr_network_top_df %>% spread("from","weight",fill = 0)
lr_network_top_matrix = lr_network_top_df %>% dplyr::select(-to) %>% as.matrix() %>% magrittr::set_rownames(lr_network_top_df$to)
# perform hierarchical clustering to order the ligands and receptors
dist_receptors = dist(lr_network_top_matrix, method = "binary")
hclust_receptors = hclust(dist_receptors, method = "ward.D2")
order_receptors = hclust_receptors$labels[hclust_receptors$order]
dist_ligands = dist(lr_network_top_matrix %>% t(), method = "binary")
hclust_ligands = hclust(dist_ligands, method = "ward.D2")
order_ligands_receptor = hclust_ligands$labels[hclust_ligands$order]
vis_ligand_receptor_network = lr_network_top_matrix[order_receptors, order_ligands_receptor]
p_ligand_receptor_network = vis_ligand_receptor_network %>% t() %>% make_heatmap_ggplot("Prioritized ligands","Receptors expressed in Engraft-colonies", color = "mediumvioletred", x_axis_position = "top",legend_title = "Prior interaction potential")
p_ligand_receptor_network
p_ligand_receptor_network+simple_theme
#Follow-up analysis 2: Visualize expression of top-predicted ligands and their target genes in a combined heatmap
library(RColorBrewer)
library(cowplot)
library(ggpubr)
ligand_pearson_matrix = ligand_activities %>% dplyr::select(pearson) %>% as.matrix() %>% magrittr::set_rownames(ligand_activities$test_ligand)
vis_ligand_pearson = ligand_pearson_matrix[order_ligands, ] %>% as.matrix(ncol = 1) %>% magrittr::set_colnames("Ligands")
p_ligand_pearson = vis_ligand_pearson %>% make_heatmap_ggplot("Prioritized ligands","Ligand activity", color = "darkred",x_axis = FALSE, y_axis = TRUE, legend_position = "left", legend_title = "Pearson")
p_ligand_pearson+simple_themeL