-
Notifications
You must be signed in to change notification settings - Fork 0
/
LongitudinalProjectories.R
297 lines (210 loc) · 11.1 KB
/
LongitudinalProjectories.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
286
287
288
289
290
291
292
293
294
295
296
297
#Filters on the visit numbers you selected. If you choose within subject ANOVA, it also only keeps participants that has all the selected visits.
dataset_longitudinal <- reactive({
#initial Copy of Data
dataset <- selectedDataset_Clinical()
dataset_eventFilter <- dataset %>% filter(event_type %in% input$LongFilter)
#if within we only want to keep participants we have data for across the selected visits.
if (input$AnovaType == 2){
dataset_partFilter <- dataset_eventFilter[dataset_eventFilter$participant_id %in% names(which(table(dataset_eventFilter$participant_id) == length(unique(dataset_eventFilter$event_type)))), ]
}
else if (input$AnovaType == 1){
dataset_partFilter <- dataset_eventFilter
}
return(dataset_partFilter)
})
StatResults <- reactive({
#grab the dataset
dataset <- dataset_longitudinal()
#Set Protein Names
Proteinnames_anova <- colnames(dataset)[22:ncol(dataset)]
#LongFormat the data
dataset_long <- dataset %>% dplyr::select(event_type, participant_id, Proteinnames_anova) %>%
gather(Proteinnames_anova, key = "Protein", value = "Intensity")
#We do a split of the dataframe based on unique ProteinID. Then we run the Anova.
#Based on some input values the formulas change slightly
#Normal ANOVA, if parametric is choosen
if (input$AnovaType == 1 && input$AnovaParametric == 1){
dataset_anovaResults <- lapply(split(dataset_long, dataset_long$Protein), aov, formula = Intensity ~ event_type)
anovaResults <- cbind(
data.frame(sapply(dataset_anovaResults, function(x) summary(x)[[1]][["F value"]][[1]])),
data.frame(sapply(dataset_anovaResults, function(x) summary(x)[[1]][["Pr(>F)"]][[1]]))) %>%
rownames_to_column()
}
#Within Subject, if parametric is choosen
else if (input$AnovaType == 2 && input$AnovaParametric == 1){
dataset_anovaResults <- lapply(split(dataset_long, dataset_long$Protein), aov, formula = Intensity ~ event_type + Error(participant_id/event_type))
anovaResults <- cbind(
data.frame(sapply(dataset_anovaResults, function(x) summary(x)$`Error: participant_id:event_type`[[1]]$'F value'[1])),
data.frame(sapply(dataset_anovaResults, function(x) summary(x)$`Error: participant_id:event_type`[[1]]$'Pr(>F)'[1]))) %>%
rownames_to_column()
}
#Kruskal Wallis (Non Para ANOVA)
else if (input$AnovaType == 1 && input$AnovaParametric == 2){
dataset_anovaResults <- lapply(split(dataset_long, dataset_long$Protein), function(x){kruskal.test(Intensity ~ event_type, data = x)})
anovaResults <- cbind(
data.frame(sapply(dataset_anovaResults, function(x) x$statistic )),
data.frame(sapply(dataset_anovaResults, function(x) x$p.value))) %>%
rownames_to_column()
anovaResults$rowname <- substr(anovaResults$rowname, 1, nchar(anovaResults$rowname)-27)
}
#return empty data
else if (input$AnovaType == 2 && input$AnovaParametric == 2){
anovaResults <- data.frame(matrix(ncol=3,nrow=0, dimnames=list(NULL, c("Dingetjes",
"Dangetjes", "Dingen"))))
}
colnames(anovaResults) <- c("Protein", "Fvalue", "Pvalue")
try(anovaResults$Padjusted <- p.adjust(anovaResults$Pvalue, method = "BH"))
return(anovaResults)
})
############ OUTPUT ###############
output$Longit_SamplePerVisit <- renderPlot({
dataset_longitudinal() %>% group_by(event_type) %>% tally() %>%
ggplot(aes(x = event_type, y = n, fill = event_type)) +
geom_bar(sta = "identity") +
theme_light() +
geom_text(aes(label= n), position=position_dodge(width=0.9), vjust=-0.25) +
theme(legend.position = "none") +
ylab("Number of Samples") +
xlab("")
})
output$Longit_volcano <- renderPlotly({
#Datasetcopy
dataset <- StatResults()
#SignificantTrueNo
dataset$significant <- dataset$Padjusted < 0.05
#Change Column Names. Plot in both cases.
if (input$AnovaParametric == 1){
colnames(dataset) <- c("Protein", "Fvalue", "Pvalue", "Padjusted", "significant")
ggplotly(
dataset %>%
ggplot(aes(x = Fvalue, y = -log10(Padjusted), label = Protein)) +
geom_point(aes(fill = significant), size = 2.3, alpha = 0.6) +
scale_fill_manual(values=c("Grey", "orange")) +
theme_light() +
xlab("Anova F value") + ylab("-log10(adjusted p-value")
)
}
else if (input$AnovaParametric == 2){
colnames(dataset) <- c("Protein", "ChiSquared", "Pvalue", "Padjusted", "significant")
ggplotly(
dataset %>%
ggplot(aes(x = ChiSquared, y = -log10(Padjusted), label = Protein)) +
geom_point(aes(fill = significant), size = 2.3, alpha = 0.6) +
scale_fill_manual(values=c("Grey", "orange")) +
theme_light() +
xlab("Kruskal Wallis Chi Squared") + ylab("-log10(adjusted p-value")
)
}
})
output$Longitudinal_Table <- DT::renderDataTable({
StatResults()
})
output$SpaghettiLong_all <- renderPlotly({
#Copy
dataset_scaled <- dataset_longitudinal()
dataset_scaled[,22:ncol(dataset_scaled)] <- scale(dataset_scaled[,22:ncol(dataset_scaled)])
SignificantProteins <- (StatResults() %>% filter(StatResults()$Padjusted < 0.05))$Protein
#Only keep columns that were significant
significant <- dataset_scaled %>%
dplyr::select(colnames(dataset_scaled)[1:21], SignificantProteins) %>% #filter for significant
group_by(event_type) %>%
summarise(across(where(is.numeric), ~ mean(.x, na.rm = TRUE))) %>%
dplyr::select(event_type, SignificantProteins) %>%
gather(key = "ProteinID", value = "Average", SignificantProteins)
ggplotly(significant %>% ggplot(aes(x = event_type, y = Average, group = ProteinID, color = "red")) +
geom_line(alpha = 0.5) +
theme_light() +
theme(legend.position = "none") +
xlab(""))
})
output$SpaghettiLong_Facet <- renderPlotly({
#Copy
dataset_scaled <- dataset_longitudinal()
dataset_scaled[,22:ncol(dataset_scaled)] <- scale(dataset_scaled[,22:ncol(dataset_scaled)])
SignificantProteins <- (StatResults() %>% filter(StatResults()$Padjusted < 0.05))$Protein
#Only keep columns that were significant
significant <- dataset_scaled %>% dplyr::select(event_type, !!!input$SpaghettiFacet, SignificantProteins) %>%
group_by_("event_type", input$SpaghettiFacet) %>%
summarise_at(SignificantProteins, mean, na.rm = T) %>%
gather(key = "ProteinID", value = "Average", SignificantProteins)
plotly::ggplotly(significant %>% ggplot(aes(x = event_type, y = Average, group = ProteinID, colour = input$SpaghettiFacet)) +
geom_line(alpha = 0.5) +
theme_light() +
theme(legend.position = "none") +
xlab("") +
facet_grid(input$SpaghettiFacet))
})
observeEvent(StatResults(), #Observe changes in the reactive DF.
updatePickerInput(session = session, inputId = "BoxPlotProtein",
choices = (StatResults() %>% filter(StatResults()$Padjusted < 0.05))$Protein)
)
output$BoxplotComparisons <- renderPlot({
Comparisons <- data.frame(t(data.frame(combn(input$LongFilter, 2)))) %>% rownames_to_column()
Comparisons <- by(Comparisons, Comparisons$rowname, function(y) unlist(y[y != ''][-1]))
#Based on the type of ANOVA you choose we have to do t.test, paired t.test or Wilcoxon Rank Sum.
#Normal Anova, Parametric assumptions --> t.test
if (input$AnovaType == 1 && input$AnovaParametric == 1){
dataset_longitudinal() %>%
ggplot(aes(x = event_type, y = !!as.symbol(input$BoxPlotProtein), fill = event_type)) +
geom_boxplot() +
stat_compare_means(comparisons = Comparisons, method = "t.test") + # Add pairwise comparisons p-value
theme(legend.position="right", axis.text.x = element_blank(), axis.ticks.x = element_blank(), axis.title.x = element_blank()) +
theme_light() +
theme(axis.text.x = element_text(angle = 45, vjust = 0.999, hjust=1)) +
xlab("")
}
#Paired Anova, Parametric assumptions --> paired t.test
else if (input$AnovaType == 2 && input$AnovaParametric == 1){
dataset_longitudinal() %>%
ggplot(aes(x = event_type, y = !!as.symbol(input$BoxPlotProtein), fill = event_type)) +
geom_boxplot() +
stat_compare_means(comparisons = Comparisons, paired = T, method = "t.test") + # Add pairwise comparisons p-value
theme(legend.position="right", axis.text.x = element_blank(), axis.ticks.x = element_blank(), axis.title.x = element_blank()) +
theme_light() +
theme(axis.text.x = element_text(angle = 45, vjust = 0.999, hjust=1)) +
xlab("")
}
#Kruskal, non-Parametric assumptions --> Wilcoxon.
else if (input$AnovaType == 1 && input$AnovaParametric == 2){
dataset_longitudinal() %>%
ggplot(aes(x = event_type, y = !!as.symbol(input$BoxPlotProtein), fill = event_type)) +
geom_boxplot() +
stat_compare_means(comparisons = Comparisons, method = "wilcox.test") + # Add pairwise comparisons p-value
theme(legend.position="right", axis.text.x = element_blank(), axis.ticks.x = element_blank(), axis.title.x = element_blank()) +
theme_light() +
theme(axis.text.x = element_text(angle = 45, vjust = 0.999, hjust=1)) +
xlab("")
}
})
output$EnrichmentPlot <- renderPlot({
#Map Significant Proteins
Significant <- StatResults()[StatResults()$Padjusted < 0.05, 1]
#run our proteins through the DB. Make STRING thingy
string_db$plot_network(Significant)
})
output$EnrichmentLongitudinal <- DT::renderDataTable({
#Map Significant Proteins
Significant <- StatResults()[StatResults()$Padjusted < 0.05, 1]
#run our proteins through the DB
enrichmentResults_Long <- string_db$get_enrichment(Significant)
#filter only GO process, KEGG and RCTM
enrichmentResults_Long <- enrichmentResults_Long %>%
filter(category == "Process" | category == "RCTM" | category == "KEGG") %>%
dplyr::select(-ncbiTaxonId, -number_of_genes, -number_of_genes_in_background, -preferredNames) %>%
relocate(inputGenes, .after = description)
DT::datatable(enrichmentResults_Long) %>% DT::formatStyle(columns = c(0:4), fontSize = '80%')
})
output$GoPlotEnrich <- renderPlot({
#Map Significant Proteins
Significant <- StatResults()[StatResults()$Padjusted < 0.05, 1]
#run our proteins through the DB
enrichmentResults_Long <- string_db$get_enrichment(Significant)
#filter only GO process, KEGG and RCTM
enrichmentResults_Long <- enrichmentResults_Long %>%
filter(category == "Process")
enrichmentResults_Long$term <- gsub("\\.", ':', enrichmentResults_Long$term)
mat <- GO_similarity(enrichmentResults_Long$term)
cl <- cluster_terms(mat)
plotSimi <- ht_clusters(mat, cl, fontsize = runif(30, min = 15, max = 25))
draw(plotSimi)
})