-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgsva_pathway_server.R
More file actions
195 lines (173 loc) · 6.84 KB
/
Copy pathgsva_pathway_server.R
File metadata and controls
195 lines (173 loc) · 6.84 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
gsva_pathway_server <- function(id) {
moduleServer(id, function(input, output, session) {
ns <- session$ns
uploaded_lncrna <- reactiveVal(NULL)
gsva_scores <- reactiveVal(NULL)
plot_ready <- reactiveVal(FALSE)
# Reset plot when dataset changes
observeEvent(input$gsva_dataset, {
plot_ready(FALSE)
gsva_scores(NULL)
})
# Load uploaded lncRNA CSV
observeEvent(input$gsva_file, {
req(input$gsva_file)
df <- readr::read_csv(input$gsva_file$datapath, show_col_types = FALSE)
uploaded_lncrna(df)
output$gsva_table <- DT::renderDataTable({
DT::datatable(df, options = list(pageLength = 10))
})
})
# Run GSVA enrichment
observeEvent(input$run_gsva, {
req(uploaded_lncrna())
tryCatch({
selected_file <- input$gsva_dataset
gsva_file_path <- file.path("E:/New_Tool_Split_App/prepare_data", selected_file)
gsva_data <- readRDS(gsva_file_path)
# Only for NPINTER, extract using Biobase
if (selected_file == "gsva_scores_npinter.rds" && inherits(gsva_data, "ExpressionSet")) {
gsva_data <- Biobase::exprs(gsva_data)
}
if (!is.null(rownames(gsva_data))) {
pathway_names <- sapply(strsplit(rownames(gsva_data), ";"), `[`, 1)
rownames(gsva_data) <- pathway_names
}
df_gsva <- data.frame(
Pathway = rownames(gsva_data),
gsva_data,
check.names = FALSE
)
gsva_scores(df_gsva)
plot_ready(FALSE)
showNotification("✅ GSVA enrichment completed.", type = "message")
}, error = function(e) {
showNotification(paste("❌ GSVA error:", e$message), type = "error")
})
})
# Enable plot generation explicitly
observeEvent(input$plot_gsva, {
req(gsva_scores())
plot_ready(TRUE)
})
# Download CSV
output$download_gsva_csv <- downloadHandler(
filename = function() {
paste0("GSVA_Pathway_Enrichment_", Sys.Date(), ".csv")
},
content = function(file) {
req(gsva_scores())
write.csv(gsva_scores(), file, row.names = FALSE)
}
)
# Reactive plot object
gsva_plot_obj <- reactive({
req(plot_ready())
req(gsva_scores())
selected_file <- input$gsva_dataset
df <- gsva_scores()
df_long <- tidyr::pivot_longer(
df,
cols = -Pathway,
names_to = "Cluster",
values_to = "EnrichmentScore"
)
df_long$Cluster <- factor(df_long$Cluster, levels = gtools::mixedsort(unique(df_long$Cluster)))
# Apply NPINTER-specific top 20 bar logic ONLY for NPINTER
if (selected_file == "gsva_scores_npinter.rds") {
pathway_vars <- df_long %>%
group_by(Pathway) %>%
summarise(Var = var(EnrichmentScore, na.rm = TRUE)) %>%
arrange(desc(Var)) %>%
slice_head(n = 20)
selected_paths <- pathway_vars$Pathway
df_plot <- df_long %>% filter(Pathway %in% selected_paths)
plot_type <- "bar"
} else {
# Your original logic untouched for all other files
total_paths <- length(unique(df_long$Pathway))
if (total_paths <= 30) {
selected_paths <- unique(df_long$Pathway)
} else {
pathway_vars <- df_long %>%
group_by(Pathway) %>%
summarise(Var = var(EnrichmentScore, na.rm = TRUE)) %>%
arrange(desc(Var)) %>%
slice_head(n = 30)
selected_paths <- pathway_vars$Pathway
}
df_plot <- df_long %>% filter(Pathway %in% selected_paths)
plot_type <- switch(
selected_file,
"GSVA_Cancer_Hallmark_ssGSEA_scores.rds" = "boxplot",
"GSVA_Disease_Type_scores.rds" = "violin",
"gsva_scores_drug.rds" = "bar",
"gsva_scores_survival.rds" = "dot",
"gsva_lncrnadisease_scores.rds" = "bar_horizontal",
"boxplot"
)
}
# Use your exact plotting logic unchanged
if (plot_type == "boxplot") {
p <- ggplot(df_plot, aes(x = Cluster, y = EnrichmentScore, fill = Pathway)) +
geom_boxplot(outlier.size = 0.4, alpha = 0.8, position = position_dodge(width = 0.8))
} else if (plot_type == "violin") {
p <- ggplot(df_plot, aes(x = Cluster, y = EnrichmentScore, fill = Pathway)) +
geom_violin(alpha = 0.7, scale = "width", trim = TRUE)
} else if (plot_type == "bar") {
p <- ggplot(df_plot, aes(x = Cluster, y = EnrichmentScore, fill = Pathway)) +
geom_bar(stat = "identity", position = position_dodge(width = 0.8))
} else if (plot_type == "dot") {
p <- ggplot(df_plot, aes(x = EnrichmentScore, y = reorder(Pathway, EnrichmentScore), color = Cluster)) +
geom_point(size = 3, alpha = 0.8) +
facet_wrap(~Cluster, scales = "free_x")
} else if (plot_type == "bar_horizontal") {
p <- ggplot(df_plot, aes(x = EnrichmentScore, y = reorder(Pathway, EnrichmentScore), fill = Cluster)) +
geom_bar(stat = "identity", position = position_dodge(width = 0.8), alpha = 0.8) +
coord_flip()
}
p <- p +
labs(
title = paste0("GSVA Enrichment Scores (", tools::toTitleCase(gsub("_", " ", plot_type)), ")"),
x = ifelse(plot_type %in% c("dot", "bar_horizontal"), "Enrichment Score", "Cluster"),
y = ifelse(plot_type %in% c("dot", "bar_horizontal"), "Pathway", "Enrichment Score")
) +
theme_classic(base_size = 16) +
theme(
plot.title = element_text(hjust = 0.5, face = "bold", size = 18),
axis.text.x = element_text(angle = 45, hjust = 1, size = 14),
axis.text.y = element_text(size = 14),
axis.title = element_text(size = 16),
legend.title = element_blank(),
legend.position = "bottom",
panel.grid = element_blank(),
panel.border = element_blank()
) +
scale_fill_viridis_d(option = "plasma") +
scale_color_viridis_d(option = "plasma")
return(p)
})
# Render plot
output$gsva_plot <- renderPlot({
gsva_plot_obj()
})
# Download high-res PNG
output$download_gsva_plot <- downloadHandler(
filename = function() {
paste0("GSVA_Pathway_Enrichment_", Sys.Date(), ".png")
},
content = function(file) {
req(plot_ready())
ggsave(
filename = file,
plot = gsva_plot_obj(),
device = "png",
dpi = 1000,
width = 12,
height = 8,
units = "in"
)
}
)
})
}