-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.R
285 lines (237 loc) · 9.61 KB
/
app.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
285
library(shiny)
library(bslib)
library(ggplot2)
library(ggpubr)
library(dplyr)
library(tidyr)
library(DT)
library(pins)
## Feb 12, 2025
#---------------------
# READ DATA FROM PINS
#---------------------
# Read the server URL and API keys
readRenviron(".Renviron")
server_url <- Sys.getenv("POSIT_SERVER_URL")
api_key <- Sys.getenv("POSIT_API_KEY")
# Read the pinned data from Posit Connect
board <- board_connect(
server = Sys.getenv("POSIT_SERVER_URL"),
key = Sys.getenv("POSIT_API_KEY")
)
prot_df <- pin_read(board,
name = "[email protected]/protein_abundance_data")
ptm_df <- pin_read(board,
name = "[email protected]/ptm_exp_data")
#---------------------
# FORMAT SIDEBAR PANEL
#---------------------
ui <- page_sidebar(
title = "Differential protein abundance and PTM explorer",
## Sidebar selection options
sidebar = sidebar(
selectizeInput("protein_choice",
"Select Protein:",
choices = sort(unique(prot_df$Protein)),
options = list(maxOptions = NULL)),
selectInput("PTM_type",
"Select PTM type:",
choices = c("Acetylation" = "acetylation",
"Phosphorylation" = "phosphorylation")),
selectInput("analysis_type",
"Select analysis:",
choices = c("ANOVA" = "anova",
"Kruskal-Wallis Rank Sum" = "kruskal.test")),
selectInput("ref_group", "Select Reference Cell Line:",
choices = c("HEK_293T", "HCT116", "U2OS", "RPE1", "HeLa")),
),
## Format layout
div(
style = "height: calc(100vh - 60px);",
layout_columns(
col_widths = c(6, 6),
# PROTEIN BOXPLOT
card(
card_header(paste("Protein abundance boxplot")),
plotOutput("protein_boxplot", height = "300px"),
textOutput("stats_result")
),
# PTM BOXPLOT
card(
card_header(paste("PTM boxplot")),
plotOutput("ptm_boxplot", height = "300px"),
textOutput("ptm_selection")
)
),
# PROTEIN TABLE
card(
card_header("Selected protein abundance"),
tableOutput("protein_table")
),
# PTM TABLE
card(
card_header("Selected ptm abundance"),
DT::dataTableOutput("ptm_table"), rownames = FALSE)
)
)
# -----------------------------
# ANALYSIS AND PLOT RENDERING
# -----------------------------
server <- function(input, output) {
#------------------------
# SELECTED PROTEIN TABLE
#------------------------
output$protein_table <- renderTable({
# Filter the dataframe to show only the selected protein
prot_df[prot_df$Protein == input$protein_choice, ]
})
#---------------------------
# PROTEIN ABUNDANCE BOXPLOT
#---------------------------
output$protein_boxplot <- renderPlot({
# Filter for selected protein and reshape data for plotting
df_filtered <- prot_df[prot_df$Protein == input$protein_choice, ]
df_filtered <- df_filtered |>
pivot_longer(
cols = -Protein,
names_to = c("cell_line", "replicate"),
names_pattern = "(.*)_([0-9])$",
values_to = "relative expression"
) |>
mutate("cell line" = gsub("_$", "", cell_line)) ##|>
##mutate(relative expression = log2(relative expression))
# Change stats based on analysis type
if (input$analysis_type == "anova") {
# Perform ANOVA
ggboxplot(df_filtered, x = "cell line", y = "relative expression",
color = "cell line", palette = "jco",
add = "jitter") +
theme(legend.position = "") +
ggtitle(paste("Protein:", input$protein_choice)) +
stat_compare_means(method = "anova",
vjust = 15) +
stat_compare_means(label = "p.signif", method = "t.test",
ref.group = input$ref_group)
} else if (input$analysis_type == "kruskal.test") {
# Perform kruskal.test t-test
ggboxplot(df_filtered, x = "cell line", y = "relative expression",
color = "cell line", palette = "jco",
add = "jitter") +
theme(legend.position = "") +
ggtitle(paste("Protein:", input$protein_choice)) +
stat_compare_means(method = "kruskal.test",
vjust = 15) +
stat_compare_means(label = "p.signif", method = "t.test",
ref.group = input$ref_group)
}
})
#---------------------------
# CALCULATE PAIRWISE T-TEST
#---------------------------
## Not rendered currently
output$stats <- renderTable({
df_filtered <- prot_df[prot_df$Protein == input$protein_choice, ]
df_filtered <- df_filtered |>
pivot_longer(
cols = -Protein,
names_to = c("cell_line", "replicate"),
names_pattern = "(.*)_([0-9])$",
values_to = "relative expression"
) |>
mutate("cell line" = gsub("_$", "", cell_line))
# Convert 'Group' to a factor (if it's not already)
df_filtered$cell_line <- factor(df_filtered$cell_line,
levels = unique(df_filtered$cell_line))
# Set reference group
df_filtered$cell_line <- relevel(df_filtered$cell_line,
ref = input$ref_group)
# Now perform the pairwise t-test
t_result <- pairwise.t.test(df_filtered$relative_expression,
df_filtered$cell_line, p.adjust.method = "bonferroni")
t_matrix <- t_result$p.value
p_df <- as.data.frame(as.table(t_matrix)) |>
mutate(pval = as.numeric(Freq)) |>
rename(Group1 = Var1, Group2 = Var2, P_Value = pval) |>
select(Group1, Group2, P_Value)
p_df$P_Value <- formatC(p_df$P_Value, digits = 3) # no rounding
p_df
}, digits = 3)
#------------
# PTM TABLE
#------------
selected_row <- reactiveVal()
# Modify the table output to store the selected row
output$ptm_table <- renderDataTable({
cdf <- ptm_df[ptm_df$Protein == input$protein_choice, ]
cdf <- cdf |> mutate(across(where(is.numeric),
~format(round(., 2), nsmall = 2)))
if (input$PTM_type == "acetylation") {
cdf <- cdf[cdf$PTM == "Acetylation", ]
} else if (input$PTM_type == "phosphorylation") {
cdf <- cdf[cdf$PTM == "Phosphorylation", ]
}
DT::datatable(cdf,
selection = 'single',
options = list(pageLength = 5), rownames = FALSE)
})
# Update the selected row when user clicks
observeEvent(input$ptm_table_rows_selected, {
cdf <- ptm_df[ptm_df$Protein == input$protein_choice, ]
if (input$PTM_type == "acetylation") {
cdf <- cdf[cdf$PTM == "Acetylation", ]
} else if (input$PTM_type == "phosphorylation") {
cdf <- cdf[cdf$PTM == "Phosphorylation", ]
}
# Store the selected row data
selected_row(cdf[input$ptm_table_rows_selected, ])
})
#-----------------------
# REACTIVE PTM BOXPLOT
#-----------------------
# Modify the boxplot to use the selected row data
output$ptm_boxplot <- renderPlot({
req(selected_row()) # Ensure we have a selected row
# Get the selected row data
selected_data <- selected_row()
# Create long format data from the selected row
ptm_long <- selected_data |>
pivot_longer(
cols = -c(Protein, Site, PTM),
names_to = c("cell_line", "replicate"),
names_pattern = "(.*)_([0-9])$",
values_to = "relative abundance"
) |>
mutate("cell line" = gsub("_$", "", cell_line))
# Change stats based on analysis type
if (input$analysis_type == "anova") {
# Perform ANOVA
ggboxplot(ptm_long,
x = "cell line",
y = "relative abundance",
color = "cell line",
palette = "jco",
add = "jitter") +
theme(legend.position = "") +
ggtitle(paste("Site:", selected_data$Site)) +
stat_compare_means(method = "anova",
vjust = 15) +
stat_compare_means(label = "p.signif", method = "t.test",
ref.group = input$ref_group)
} else if (input$analysis_type == "kruskal.test") {
# Perform kruskal.test t-test
ggboxplot(ptm_long,
x = "cell line",
y = "relative abundance",
color = "cell line",
palette = "jco",
add = "jitter") +
theme(legend.position = "") +
ggtitle(paste("Site:", selected_data$Site)) +
stat_compare_means(method = "kruskal.test",
vjust = 15) +
stat_compare_means(label = "p.signif", method = "t.test",
ref.group = input$ref_group)
}
})
}
shinyApp(ui, server)