-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11.4.2.tumor_stroma_gsea_cell-level_primaryIO_sarcomatoid.R
144 lines (127 loc) · 6.6 KB
/
11.4.2.tumor_stroma_gsea_cell-level_primaryIO_sarcomatoid.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
rm(list=ls())
# libraries ---------------------------------------------------------------
library(pbmcapply)
library(msigdbr)
library(GSVA)
library(ComplexHeatmap)
library(tidyverse)
#data
metadata = readRDS("Manley_SMI/data/final_dataframes/metadata_clinical_spatial.rds") %>%
filter(fov <= 20, unique_fov != "RCC5_13") %>%
mutate(Site = gsub(" $", "", Site)) %>%
filter(Site == "Tumor") %>%
mutate(Pretreatment.IO = ifelse(IT.Treatment.before.collection == "None", "Treatment Naive", "Received IO"),
Pretreatment.IO = factor(Pretreatment.IO, levels = c("Treatment Naive", "Received IO")),
Sarcomatoid = gsub(" ", "", Sarcomatoid),
Sarcomatoid.Group = case_when(IT.Treatment.before.collection == "None" & Sarcomatoid == "No" ~ "Treatment Naive Non-Sarcomatoid",
IT.Treatment.before.collection != "None" & Sarcomatoid == "No" ~ "Post IO Non-Sarcomatoid",
IT.Treatment.before.collection != "None" & Sarcomatoid == "Yes" ~ "Post IO Sarcomatoid",
T ~ "Pre IO Sarcomatoid"),
Sarcomatoid.Group = factor(Sarcomatoid.Group, levels = c("Treatment Naive Non-Sarcomatoid", "Post IO Non-Sarcomatoid",
"Post IO Sarcomatoid", "Pre IO Sarcomatoid")))
#cell types
phenotypes = metadata$lasso_final %>% unique()
#get the stroma and tumor groups
tumor = metadata %>% filter(Source == "Tumor", Sarcomatoid.Group %in% c("Treatment Naive Non-Sarcomatoid", "Pre IO Sarcomatoid"))
stroma = metadata %>% filter(Source == "Stroma", Sarcomatoid.Group %in% c("Treatment Naive Non-Sarcomatoid", "Pre IO Sarcomatoid"))
# Gene Sets ---------------------------------------------------------------
all_gene_sets = msigdbr(species = "Homo sapiens") %>%
filter(gs_cat == "H") %>% dplyr::distinct(gs_name, gene_symbol) %>% as.data.frame()
gene_set_list = split(all_gene_sets$gene_symbol, all_gene_sets$gs_name)
fovs = metadata$unique_fov %>% unique()
all_fov = readRDS("Manley_SMI/results/GEx/hallmark_pathway_list.rds")
#primary tumor (no sarcomatoid) treatment classes
naive_fovs = tumor %>% filter(Sarcomatoid.Group == "Treatment Naive Non-Sarcomatoid") %>% pull(unique_fov) %>% unique()
sarc_fovs = tumor %>% filter(Sarcomatoid.Group == "Pre IO Sarcomatoid") %>% pull(unique_fov) %>% unique()
naive_phenotype_scores = lapply(setNames(phenotypes, phenotypes), function(cell){
d = lapply(naive_fovs, function(fov){
all_fov[[fov]]$Phenotype_sets[[cell]]
}) %>%
do.call(bind_rows, .) %>%
colMeans(na.rm = T)
if(length(d) == 0){
d = rep(0, 50)
names(d) = names(gene_set_list)
return(d)
}
return(d)
}) %>% do.call(bind_rows, .)
rownames(naive_phenotype_scores) = paste0(phenotypes, " - Treatment Naive Non-Sarcomatoid")
sarc_phenotype_scores = lapply(setNames(phenotypes, phenotypes), function(cell){
d = lapply(sarc_fovs, function(fov){
all_fov[[fov]]$Phenotype_sets[[cell]]
}) %>%
do.call(bind_rows, .) %>%
colMeans(na.rm = T)
if(length(d) == 0){
d = rep(0, 50)
names(d) = names(gene_set_list)
return(d)
}
return(d)
}) %>% do.call(bind_rows, .)
rownames(sarc_phenotype_scores) = paste0(phenotypes, " - Pre IO Sarcomatoid")
#heatmap
top_annon = HeatmapAnnotation(df = data.frame(Sample = c(rep("Treatment Naive Non-Sarcomatoid", length(phenotypes)),
rep("Pre IO Sarcomatoid", length(phenotypes)))),
col = list(Sample = c("Treatment Naive Non-Sarcomatoid" = "black", "Pre IO Sarcomatoid" = "darkgray")))
mat = rbind(naive_phenotype_scores,
sarc_phenotype_scores) %>% t()
rownames(mat) = rownames(mat) %>%
gsub("HALLMARK_", "", .) %>%
gsub("_", " ", .)
ht = Heatmap(mat, top_annotation = top_annon, name = "GSVA Hallmark\nScore",
row_names_max_width = max_text_width(rownames(mat)), column_names_max_height = max_text_width(colnames(mat)),
column_labels = colnames(mat) %>% gsub("\\ - .*", "", .), column_title = "Cell Level Hallmark GSEA (Averaged) - Sarcomatoid Tumor",
col = circlize::colorRamp2(c(min(mat), 0, max(mat)), c("blue", "white", "red")))
pdf("Manley_SMI/results/GEx/primary_io/sarcomatoid_tumor/sarcomatoid_tumor_primary_pre-non-sarc-postIO_cell-level-GSEA-averaged.pdf", height = 10, width = 15)
ht
dev.off()
#primary stroma (no sarcomatoid) treatment classes
naive_fovs = stroma %>% filter(Sarcomatoid.Group == "Treatment Naive Non-Sarcomatoid") %>% pull(unique_fov) %>% unique()
sarc_fovs = stroma %>% filter(Sarcomatoid.Group == "Pre IO Sarcomatoid") %>% pull(unique_fov) %>% unique()
naive_phenotype_scores = lapply(setNames(phenotypes, phenotypes), function(cell){
d = lapply(naive_fovs, function(fov){
all_fov[[fov]]$Phenotype_sets[[cell]]
}) %>%
do.call(bind_rows, .) %>%
colMeans(na.rm = T)
if(length(d) == 0){
d = rep(0, 50)
names(d) = names(gene_set_list)
return(d)
}
return(d)
}) %>% do.call(bind_rows, .)
rownames(naive_phenotype_scores) = paste0(phenotypes, " - Treatment Naive Non-Sarcomatoid")
sarc_phenotype_scores = lapply(setNames(phenotypes, phenotypes), function(cell){
d = lapply(sarc_fovs, function(fov){
all_fov[[fov]]$Phenotype_sets[[cell]]
}) %>%
do.call(bind_rows, .) %>%
colMeans(na.rm = T)
if(length(d) == 0){
d = rep(0, 50)
names(d) = names(gene_set_list)
return(d)
}
return(d)
}) %>% do.call(bind_rows, .)
rownames(sarc_phenotype_scores) = paste0(phenotypes, " - Pre IO Sarcomatoid")
#heatmap
top_annon = HeatmapAnnotation(df = data.frame(Sample = c(rep("Treatment Naive Non-Sarcomatoid", length(phenotypes)),
rep("Pre IO Sarcomatoid", length(phenotypes)))),
col = list(Sample = c("Treatment Naive Non-Sarcomatoid" = "black", "Pre IO Sarcomatoid" = "darkgray")))
mat = rbind(naive_phenotype_scores,
sarc_phenotype_scores) %>% t()
rownames(mat) = rownames(mat) %>%
gsub("HALLMARK_", "", .) %>%
gsub("_", " ", .)
ht = Heatmap(mat, top_annotation = top_annon, name = "GSVA Hallmark\nScore",
row_names_max_width = max_text_width(rownames(mat)), column_names_max_height = max_text_width(colnames(mat)),
column_labels = colnames(mat) %>% gsub("\\ - .*", "", .), column_title = "Cell Level Hallmark GSEA (Averaged) - Sarcomatoid Stroma",
col = circlize::colorRamp2(c(min(mat), 0, max(mat)), c("blue", "white", "red")))
ht
pdf("Manley_SMI/results/GEx/primary_io/sarcomatoid_stroma/sarcomatoid_stroma_primary_pre-non-sarc-postIO_cell-level-GSEA-averaged.pdf", height = 10, width = 15)
ht
dev.off()