-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.R
226 lines (196 loc) · 7.72 KB
/
server.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
library(shiny)
library(shinyjs)
function(input, output, session) {
# store selected cellchat in single sample view
selected_cellchat <- reactive({
req(input$sample_name)
cellchat.list[[input$sample_name]]})
# Initialize choices for comparison
observe({
updateSelectInput(session, "sample1", choices = names(cellchat.list),selected=names(cellchat.list)[1])
})
# Update the choices for sample2 when sample1 is selected
observeEvent(input$sample1, {
remaining_choices <- setdiff(names(cellchat.list), input$sample1)
updateSelectInput(session, "sample2", choices = remaining_choices)
})
# create a merged cellchat for comparison
# compare_cellchat <- reactive({
# req(input$sample1, input$sample2)
# if(input$sample1!=input$sample2){
# cc.list <- cellchat.list[c(input$sample1, input$sample2)]
# cc <- mergeCellChat(cc.list, add.names = names(cc.list))
# } else {
# message("Error: please select 2 different samples.")
# cc <- mergeCellChat(cellchat.list, add.names=names(cellchat.list))
# }
# return(cc)
# })
# Update the pathway choices based on the selected cellchat object
observe({
cellchat_obj <- selected_cellchat()
choices <- cellchat_obj@netP$pathways
cell_types <- sort(unique(cellchat_obj@idents)) # Extract available cell types
# if(!is.null(compare_cellchat())){
# cellchat_obj <- selected_cellchat()
# choices <- intersect(cellchat.list[[input$sample1]]@netP$pathways, cellchat.list[[input$sample2]]@netP$pathways)
# cell_types <- sort(intersect(unique(cellchat.list[[input$sample1]]@idents), unique(cellchat.list[[input$sample2]]@idents)))
# }
updateSelectizeInput(session, 'pathway_name', choices = choices, server = TRUE)
# Update sender and receiver cell choices
updatePickerInput(session, 'sender_cells',
choices = cell_types,
selected = cell_types) # Default to all cell types selected
updatePickerInput(session, 'receiver_cells',
choices = cell_types,
selected = cell_types)
})
# Get indices of selected cell types
get_index <- reactive({
# Filter the communication network based on selected sender and receiver cells
cellchat_obj <- selected_cellchat()
sources <- which(levels(cellchat_obj@idents)%in%input$sender_cells)
targets <- which(levels(cellchat_obj@idents)%in%input$receiver_cells)
list(sources = sources, targets = targets)
})
# Reactive value to store the selected pathway
select_pathway <- reactive({
req(input$pathway_name)
input$pathway_name
})
# Reactive value to store plot dimensions
plot_dimensions <- reactive({
list(height=input$plot_height, width=input$plot_width, cex=input$plot_cex)
})
plot_type <- reactive({
input$plot_type
})
# General function to generate plots, reducing redundancy
generate_plot <- function(plot_func) {
cellchat_obj <- req(selected_cellchat())
pathway.show <- req(select_pathway())
cell_type <- req(get_index())
dim <- plot_dimensions()
type <- plot_type()
plot_func(cellchat_obj,
signaling = pathway.show,
sources = cell_type$sources,
targets = cell_type$targets,
cex = dim$cex,
type = type)
}
# aggregated plot
agPlot <- eventReactive(input$update_plot, {
generate_plot(function(cellchat_obj, signaling, sources, targets, cex, type) {
groupSize <- as.numeric(table(cellchat_obj@idents))
if(type=="count"){
netVisual_circle(cellchat_obj@net$count,
vertex.weight=groupSize,
sources.use=sources,
targets.use=targets,
edge.weight.max = max(cellchat_obj@net$count),
weight.scale = T, label.edge= F,
vertex.label.cex = cex)
#title(main = "Number of interactions")
} else {
netVisual_circle(cellchat_obj@net$count,
vertex.weight=groupSize,
sources.use=sources,
targets.use=targets,
edge.weight.max = max(cellchat_obj@net$count),
weight.scale = T, label.edge= F,
vertex.label.cex = cex)
#title(main = "Interaction strength")
}
})
})
# circle plot for specific pathway
cPlot <- eventReactive(input$update_plot, {
generate_plot(function(cellchat_obj, signaling, sources, targets, cex, type) {
netVisual_aggregate(cellchat_obj, signaling = signaling,
sources.use=sources,
targets.use=targets,
vertex.weight = NULL,
weight.scale = (type=="weight"),
layout = "circle",
vertex.label.cex = cex)
#title(main = paste0("Circle plot of ", signaling, " pathway"))
})
})
# bubble plot for specific pathway
bPlot <- eventReactive(input$update_plot, {
generate_plot(function(cellchat_obj, signaling, sources, targets, cex, type) {
netVisual_bubble(cellchat_obj,
signaling = signaling,
sources.use=sources,
targets.use=targets,
angle.x=45,
font.size = cex*10)
#title(main = paste0("Bubble plot of ", signaling, " pathway"))
})
})
# heatmap for specific pathway
hPlot <- eventReactive(input$update_plot, {
generate_plot(function(cellchat_obj, signaling, sources, targets, cex, type) {
netVisual_heatmap(cellchat_obj,
signaling = signaling,
measure=type,
sources.use=sources,
targets.use=targets,
font.size=cex*10)
#title(main = paste0("Heatmap of ", signaling, " pathway"))
})
})
chPlot <- eventReactive(input$update_plot, {
generate_plot(function(cellchat_obj, signaling, sources, targets, cex, type) {
netVisual_aggregate(cellchat_obj, signaling = signaling,
sources.use=sources,
targets.use=targets,
layout = "chord",
weight.scale = (type=="weight"),
vertex.label.cex = cex)
#title(main = paste0("Chord plot of ", signaling, " pathway"))
})
})
output$aggr_plot_UI <- renderUI({
dim <- plot_dimensions()
plotOutput("agPlot", height=dim$height, width=dim$width)
})
output$agPlot <- renderPlot({
agPlot()
})
# Render the circle plot UI and plot with consistent dimensions
output$circle_plot_UI <- renderUI({
dim <- plot_dimensions()
plotOutput("circle_plot", height=dim$height, width=dim$width)
})
output$circle_plot <- renderPlot({
cPlot()
})
# Render the bubble plot UI and plot with consistent dimensions
output$bubble_plot_UI <- renderUI({
dim <- plot_dimensions()
plotOutput("bubble_plot", height=dim$height, width=dim$width)
})
output$bubble_plot <- renderPlot({
req(hPlot())
bPlot()
})
# Render the role plot UI and plot with consistent dimensions
output$heatmap_UI <- renderUI({
dim <- plot_dimensions()
plotOutput("heatmap", height=dim$height, width=dim$width)
})
output$heatmap <- renderPlot({
req(hPlot())
hPlot()
})
# Render the chord plot UI and plot with consistent dimensions
output$chord_plot_UI <- renderUI({
dim <- plot_dimensions()
plotOutput("chord_plot", height=dim$height, width=dim$width)
})
output$chord_plot <- renderPlot({
chPlot()
})
}