so.astro <- readRDS(file.path(dir.objects, "so.astro.rds"))
#[email protected]$ident <- as.factor([email protected]$integrated_snn_res.0.2)
Idents(so.astro) <- "integrated_snn_res.0.2"
# Prepare a vector of colors for the clusters
clusters <- levels(Idents(so.astro)) %>% as.numeric()
cluster.cols <- muscat:::.cluster_colors[seq_along(clusters)]
names(cluster.cols) <- clusters
clusters
[1] 0 1 2 3 4 5 6 7 8 9
astro.markers <- readRDS(file.path(dir.objects, "astro.markers.v1.rds")) %>%
remove_rownames() %>%
mutate(gene.name = word(gene, 2, sep = "\\."))
head(astro.markers)
astro.markers.subset <- astro.markers %>%
group_by(cluster) %>%
slice_max(n = 15, order_by = avg_log2FC)
astro.markers.subset
# Expression matrix
cells_to_clusters <- data.frame(cluster = so.astro$integrated_snn_res.0.2) %>%
rownames_to_column("cell")
geneset <- unique(astro.markers.subset$gene)
mat.scale.ours_data <-
so.astro@assays$integrated@scale.data[geneset, cells_to_clusters$cell]
cluster_indices <-
split(cells_to_clusters$cell, cells_to_clusters$cluster)
cluster_means <- sapply(cluster_indices, function(indices) {
Matrix::rowMeans(mat.scale.ours_data[, indices, drop = FALSE])
})
rownames(cluster_means) <-
word(rownames(cluster_means), 2, sep = "\\.")
# Gene cluster color bar annotation
# by cluster order
genes.clusters.cols <- astro.markers.subset %>%
select(gene, cluster) %>%
mutate(gene.full = gene,
gene = word(gene.full, 2, sep = "\\.")) %>%
group_by(gene) %>%
slice_min(n = 1, order_by = cluster) %>%
ungroup() %>%
arrange(cluster) %>%
left_join(., enframe(cluster.cols, name = "cluster", value = "colors"))
rownames(genes.clusters.cols) <- genes.clusters.cols$gene
genes.clusters.cols <-
genes.clusters.cols[rownames(cluster_means),]
row.anno.genes <-
rowAnnotation(
"Cluster" = genes.clusters.cols$cluster,
col = list("Cluster" = cluster.cols),
show_annotation_name = FALSE
)
# Define the range and breaks
breaks <-
seq(0, 1, length.out = 11) # This creates 11 points between 0 and 1
colors <- plasma(11)
# Create a color function that maps the range [0, 1] to the viridis palette
color_mapping <- colorRamp2(breaks, colors)
# Use the adjusted col argument
Heatmap(
cluster_means,
name = "Normal. expression",
col = color_mapping,
cluster_rows = FALSE,
show_row_dend = FALSE,
cluster_columns = FALSE,
column_names_side = "bottom",
row_names_side = "left",
column_names_rot = 0,
column_names_centered = TRUE,
row_names_gp = gpar(fontsize = 8),
left_annotation = row.anno.genes
)
# Specify the directory containing the H5 file and the image data
dir.Spatial_H5 <- file.path(wd, "GSM5026144")
# Load the data into a Seurat object
GSM5026144_S7_Seurat <- Load10X_Spatial(
data.dir = dir.Spatial_H5,
filename = "GSM5026144_S7_filtered_feature_bc_matrix.h5",
assay = "Spatial",
slice = "slice1",
filter.matrix = TRUE,
to.upper = FALSE,
image = NULL
)
GSM5026144_S7_Seurat <-
SCTransform(
object = GSM5026144_S7_Seurat,
assay = 'Spatial',
return.only.var.genes = F,
verbose = FALSE
)
GSM5026144_S7_Seurat
An object of class Seurat
50573 features across 2511 samples within 2 assays
Active assay: SCT (18288 features, 3000 variable features)
3 layers present: counts, data, scale.data
1 other assay present: Spatial
1 image present: slice1
astrocyte_markers <- c("Aldh1l1", "Slc1a2", "S100b", "Gfap", "Myoc")
cluster_0.module <- list(c("Gm42418", "Arhgap31", "Kcnq1ot1", "Rmst", "Cit", "Gli1", "Ttll3", "Neat1", "Gm26917"))
cluster_1.module <- list(c("Mfge8", "Igfbp2", "Btbd17"))
cluster_2.module <- list(c("Dbi", "Agt", "Nnat", "Ppia", "Tmsb4x", "Scrg1", "Mt3", "Gstm1", "Nkain4", "S100a1"))
cluster_3.module <- list(c("Thrsp", "Actb", "Chchd10", "Hint1", "Mrpl41", "Mpc1", "Camk2n1", "Ndufc2", "Nupr1", "Igfbp2"))
cluster_4.module <- list(c("Lcn2", "Ifitm3", "Timp1", "B2m", "Vim", "Gfap", "Psmb8", "Bst2"))
cluster_5.module <- list(c("Crym", "Prss56", "Slc43a3", "Mgst1", "Slc1a5", "Tuba1a", "Itm2b", "Chil1", "Ptn"))
cluster_6.module <- list(c("Thbs4", "Igfbp5", "Fxyd6", "Gsn", "Fxyd1", "Cd9", "Meg3"))
cluster_7.module <- list(c("Itih3", "Sparc", "Nkx6-2", "Etnppl", "Gria1", "Spon1", "5031439G07Rik"))
cluster_8.module <- list(c("Igtp", "Gm4951", "Ifit3", "Iigp1", "Irgm1", "Tap1", "Gbp2", "Ifit1", "Isg15", "Cxcl10"))
cluster_9.module <- list(c("Zic1", "Slc38a1", "Col1a2", "Kcnk2", "Luzp2", "Mgll", "Zic4", "Hopx"))
cluster_modules.all <- c("cluster_0.module", "cluster_1.module", "cluster_2.module", "cluster_3.module", "cluster_4.module", "cluster_5.module", "cluster_6.module", "cluster_7.module", "cluster_8.module", "cluster_9.module")
for (gene in astrocyte_markers) {
GSM5026144_S7_Seurat <-
AddModuleScore(object = GSM5026144_S7_Seurat,
features = list(gene),
name = gene)
}
for (cluster_module_name in cluster_modules.all) {
# Retrieve the actual module list using get()
cluster_module <- get(cluster_module_name)
# Add the module score to the Seurat object
GSM5026144_S7_Seurat <- AddModuleScore(object = GSM5026144_S7_Seurat, features = cluster_module, name = cluster_module_name)
}
Warning: The following features are not present in the object: Gm4951, not searching for symbol synonyms
# GSM5026144_S7_Seurat <- AddModuleScore(object = GSM5026144_S7_Seurat, features = cluster_0.module, name = "Cluster0_ModuleScore")
# GSM5026144_S7_Seurat <- AddModuleScore(object = GSM5026144_S7_Seurat, features = cluster_1.module, name = "Cluster1_ModuleScore")
# GSM5026144_S7_Seurat <- AddModuleScore(object = GSM5026144_S7_Seurat, features = cluster_2.module, name = "Cluster2_ModuleScore")
# GSM5026144_S7_Seurat <- AddModuleScore(object = GSM5026144_S7_Seurat, features = cluster_3.module, name = "Cluster3_ModuleScore")
# GSM5026144_S7_Seurat <- AddModuleScore(object = GSM5026144_S7_Seurat, features = cluster_4.module, name = "Cluster4_ModuleScore")
# GSM5026144_S7_Seurat <- AddModuleScore(object = GSM5026144_S7_Seurat, features = cluster_5.module, name = "Cluster5_ModuleScore")
# GSM5026144_S7_Seurat <- AddModuleScore(object = GSM5026144_S7_Seurat, features = cluster_6.module, name = "Cluster6_ModuleScore")
# GSM5026144_S7_Seurat <- AddModuleScore(object = GSM5026144_S7_Seurat, features = cluster_7.module, name = "Cluster7_ModuleScore")
# GSM5026144_S7_Seurat <- AddModuleScore(object = GSM5026144_S7_Seurat, features = cluster_8.module, name = "Cluster8_ModuleScore")
# GSM5026144_S7_Seurat <- AddModuleScore(object = GSM5026144_S7_Seurat, features = cluster_9.module, name = "Cluster9_ModuleScore")
# List to store individual plots
plots <- list()
features_list <- list(
"Aldh1l11" = "Aldh1l1",
"Slc1a21" = "Slc1a2",
"S100b1" = "S100b",
"Gfap1" = "Gfap",
"Myoc1" = "Myoc",
"cluster_0.module1" = "0",
"cluster_1.module1" = "1",
"cluster_2.module1" = "2",
"cluster_3.module1" = "3",
"cluster_4.module1" = "4",
"cluster_5.module1" = "5",
"cluster_6.module1" = "6",
"cluster_7.module1" = "7",
"cluster_8.module1" = "8",
"cluster_9.module1" = "9"
)
legend_only <-
SpatialPlot(
GSM5026144_S7_Seurat,
features = "cluster_9.module1",
pt.size.factor = 0,
image.alpha = 0
) + theme(legend.text = element_blank(), legend.title = element_blank())
# Create an image-only ggplot
image_only <-
SpatialPlot(GSM5026144_S7_Seurat,
features = "cluster_9.module1",
pt.size.factor = 0) + theme(legend.position = "none") +
annotate(
"text",
x = Inf,
y = Inf,
label = "H&E",
hjust = 1.1,
vjust = 1.1,
fontface = "italic",
color = "black",
size = 2
)
# Add the image-only plot to the the plots list
plots[[length(plots) + 1]] <- image_only
# Generate the plots
for (feature in names(features_list)) {
# Create a spatial feature plot for each feature
p <- SpatialFeaturePlot(GSM5026144_S7_Seurat, features = feature) +
theme(legend.position = "none") +
annotate("text", x = Inf, y = Inf, label = features_list[[feature]],
hjust = 1.1, vjust = 1.1, fontface = "italic", color = "black", size = 2)
# Store the plot in the list
plots[[feature]] <- p
}
With our analysis markers at 0.35 LogFC Cutoff
# Load the data into a Seurat object
ours_GSM5026144_S7_Seurat <- Load10X_Spatial(
data.dir = dir.Spatial_H5,
filename = "GSM5026144_S7_filtered_feature_bc_matrix.h5",
assay = "Spatial",
slice = "slice1",
filter.matrix = TRUE,
to.upper = FALSE,
image = NULL
)
ours_GSM5026144_S7_Seurat <-
SCTransform(
ours_GSM5026144_S7_Seurat,
assay = 'Spatial',
return.only.var.genes = F,
verbose = FALSE
)
# Subset data for avg_logFC > 0.35
astro.cluster_markers.subset.0.35 <- astro.markers[astro.markers$avg_log2FC > 0.35, ]
# Remove ensemble ID from gene names
astro.cluster_markers.subset.0.35$gene <- sub(".*\\.", "", astro.cluster_markers.subset.0.35$gene)
# Specify the list of clusters
specified_clusters <- c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
# Loop through each specified cluster
for (cluster in specified_clusters) {
# Subset data for the current cluster
cluster_data <- astro.cluster_markers.subset.0.35[astro.cluster_markers.subset.0.35$cluster == cluster, ]
# Create a comma-separated gene list with each gene in quotes
gene_list <- sprintf('"%s"', cluster_data$gene)
gene_list_string <- paste(gene_list, collapse = ", ")
# Create a list with the gene names and assign it to a named variable
variable_name <- paste0("cluster_", cluster, "_ours_module")
assign(variable_name, list(eval(parse(text = paste("c(", gene_list_string, ")")))), envir = .GlobalEnv)
}
# Initialize an empty list to store the cluster variables
cluster_vars <- list()
# Loop through each specified cluster
for (cluster in specified_clusters) {
# Subset data for the current cluster
cluster_data <- astro.cluster_markers.subset.0.35[astro.cluster_markers.subset.0.35$cluster == cluster, ]
# Create a comma-separated gene list with each gene in quotes
gene_list <- sprintf('"%s"', cluster_data$gene)
gene_list_string <- paste(gene_list, collapse = ", ")
# Create a list with the gene names and assign it to a named variable
variable_name <- paste0("cluster_", cluster, "_ours_module")
variable_value <- list(eval(parse(text = paste("c(", gene_list_string, ")"))))
assign(variable_name, variable_value, envir = .GlobalEnv)
# Add the variable name to the list of cluster variables
cluster_vars <- append(cluster_vars, variable_name)
}
cluster_vars
[[1]]
[1] "cluster_0_ours_module"
[[2]]
[1] "cluster_1_ours_module"
[[3]]
[1] "cluster_2_ours_module"
[[4]]
[1] "cluster_3_ours_module"
[[5]]
[1] "cluster_4_ours_module"
[[6]]
[1] "cluster_5_ours_module"
[[7]]
[1] "cluster_6_ours_module"
[[8]]
[1] "cluster_7_ours_module"
[[9]]
[1] "cluster_8_ours_module"
[[10]]
[1] "cluster_9_ours_module"
for (gene in astrocyte_markers) {
ours_GSM5026144_S7_Seurat <-
AddModuleScore(object = ours_GSM5026144_S7_Seurat, features = list(gene), name = gene)
}
# Loop through each cluster variable
for (cluster_var in cluster_vars) {
# Extract the cluster number from the variable name
cluster_number <- gsub("\\D", "", cluster_var)
# Create the name for the module score
module_score_name <- paste("cluster_", cluster_number, ".ours_module", sep = "")
# Add the module score
ours_GSM5026144_S7_Seurat <- AddModuleScore(
object = ours_GSM5026144_S7_Seurat,
features = get(cluster_var),
name = module_score_name
)
}
Warning: The following features are not present in the object: Fam212b, not searching for symbol synonymsWarning: The following features are not present in the object: 1110008F13Rik, Usmg5, 2410015M20Rik, Pyurf, 2010107E04Rik, 1500011K16Rik, Minos1, Fam213a, Tmem5, Aes, Pla2g16, D10Jhu81e, 1110004E09Rik, Fam96a, Lao1, 5930412G12Rik, 1, not searching for symbol synonymsWarning: The following features are not present in the object: Gm26772, Gm26870, Cd27, Gm10600, C130073E24Rik, Gm11713, Gm26909, 1810041L15Rik, B430010I23Rik, A330069E16Rik, not searching for symbol synonymsWarning: The following features are not present in the object: Fam213a, Pyurf, Pla2g16, 1, 1110004E09Rik, 5930412G12Rik, not searching for symbol synonymsWarning: The following features are not present in the object: Cxcl9, Mx1, Gm4951, 2410015M20Rik, Tmem5, Fam213a, F830016B08Rik, Pla2g16, 1500015O10Rik, 1, Fam19a5, Gm8186, 2900011O08Rik, Slc10a6, not searching for symbol synonymsWarning: The following features are not present in the object: Slc10a6, 1110004E09Rik, Cxcl9, Gm10600, 1, 1500015O10Rik, 2900011O08Rik, Cyr61, Gm8186, Fam19a5, not searching for symbol synonymsWarning: The following features are not present in the object: 1500015O10Rik, C130073E24Rik, Mx1, Gm26772, Gm26909, Cxcl9, Fam19a5, 1, Fam212b, 1500011K16Rik, 2900011O08Rik, 2410015M20Rik, Gm8186, Aes, Pyurf, Minos1, Usmg5, Pla2g16, Gm26870, D10Jhu81e, 2010107E04Rik, 1110004E09Rik, not searching for symbol synonymsWarning: The following features are not present in the object: Fam19a1, 1810041L15Rik, Cd27, not searching for symbol synonymsWarning: The following features are not present in the object: Gm4951, F830016B08Rik, Slc10a6, Cxcl9, Mx1, Slco1b2, not searching for symbol synonymsWarning: The following features are not present in the object: 2010107E04Rik, Fam213a, Usmg5, 2410015M20Rik, Minos1, Cxcl9, 1500011K16Rik, 1500015O10Rik, Aes, Gm8186, 2900011O08Rik, 1110008F13Rik, Lao1, Pla2g16, not searching for symbol synonyms
# List to store individual plots
our_plots <- list()
features_list <- list(
"Aldh1l11" = "Aldh1l1",
"Slc1a21" = "Slc1a2",
"S100b1" = "S100b",
"Gfap1" = "Gfap",
"Myoc1" = "Myoc",
"cluster_0.ours_module1" = "0",
"cluster_1.ours_module1" = "1",
"cluster_2.ours_module1" = "2",
"cluster_3.ours_module1" = "3",
"cluster_4.ours_module1" = "4",
"cluster_5.ours_module1" = "5",
"cluster_6.ours_module1" = "6",
"cluster_7.ours_module1" = "7",
"cluster_8.ours_module1" = "8",
"cluster_9.ours_module1" = "9"
)
ours_legend_only <- SpatialPlot(ours_GSM5026144_S7_Seurat, features = "cluster_9.ours_module1", pt.size.factor = 0, image.alpha = 0) + theme(legend.text = element_blank(), legend.title = element_blank())
# Add the legend_only plot to the the plots list
our_plots[[length(our_plots) + 1]] <- ours_legend_only
# Create an image-only ggplot
ours_image_only <- SpatialPlot(ours_GSM5026144_S7_Seurat, features = "cluster_9.ours_module1", pt.size.factor = 0) + theme(legend.position = "none")
# Add the image-only plot to the the plots list
our_plots[[length(our_plots) + 1]] <- ours_image_only
# Generate the plots
for (feature in names(features_list)) {
# Create a spatial feature plot for each feature
p <- SpatialFeaturePlot(ours_GSM5026144_S7_Seurat, features = feature) +
theme(legend.position = "none") +
annotate("text", x = Inf, y = Inf, label = features_list[[feature]],
hjust = 1.1, vjust = 1.1, fontface = "italic", color = "black", size = 2)
# Store the plot in the list
our_plots[[feature]] <- p
}