forked from BI-STEM-Away/sMAP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.R
671 lines (581 loc) · 20 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
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
function(input,output,session){
# @roman_ramirez
# stop app on browser close
session$onSessionEnded(
function() {
stopApp()
}
)
# @samuel_bharti and @roman_ramirez
# BACK AND PROCEED BUTTONS
# INTRODUCTION BUTTONS
observeEvent(input$to_end, {
stopApp()
}
)
observeEvent(input$to_dataImport, {
updateTabItems(session, "tabs", "dataImport")
}
)
# DATA IMPORT BUTTONS
observeEvent(input$backTo_introduction, {
updateTabItems(session, "tabs", "introduction")
}
)
observeEvent(input$to_qC, {
updateTabItems(session, "tabs", "qC")
}
)
# QC BUTTONS
observeEvent(input$backTo_dataImport, {
updateTabItems(session, "tabs", "dataImport")
}
)
observeEvent(input$backTo_qC, {
updateTabItems(session, "tabs", "qC")
}
)
observeEvent(input$to_normalization, {
updateTabItems(session, "tabs", "normalization")
}
)
observeEvent(input$backTo_normalization, {
updateTabItems(session, "tabs", "normalization")
}
)
observeEvent(input$to_batchCorrection, {
updateTabItems(session, "tabs", "batchCorrection")
}
)
observeEvent(input$backTo_batchCorrection, {
updateTabItems(session, "tabs", "batchCorrection")
}
)
observeEvent(input$to_potentialOutliers, {
updateTabItems(session, "tabs", "potentialOutliers")
}
)
observeEvent(input$backTo_potentialOutliers, {
updateTabItems(session, "tabs", "potentialOutliers")
}
)
observeEvent(input$to_sampleGrouping, {
updateTabItems(session, "tabs", "sampleGrouping")
}
)
# SAMPLE GROUPING BUTTONS
observeEvent(input$backTo_sampleGrouping, {
updateTabItems(session, "tabs", "sampleGrouping")
}
)
observeEvent(input$to_volcanoPlot, {
updateTabItems(session, "tabs", "volcanoPlot")
}
)
# DEG ANALYSIS BUTTONS
observeEvent(input$backTo_volcanoPlot, {
updateTabItems(session, "tabs", "volcanoPlot")
}
)
observeEvent(input$to_topDEGs, {
updateTabItems(session, "tabs", "topDEGs")
}
)
observeEvent(input$backTo_topDEGs, {
updateTabItems(session, "tabs", "topDEGs")
}
)
observeEvent(input$to_functionalEnrichmentAnalysis, {
updateTabItems(session, "tabs", "functionalEnrichmentAnalysis")
}
)
# FUNCTIONAL ANALYSIS BUTTONS
observeEvent(input$backTo_functionalEnrichmentAnalysis, {
updateTabItems(session, "tabs", "functionalEnrichmentAnalysis")
}
)
observeEvent(input$to_geneConceptNetwork, {
updateTabItems(session, "tabs", "geneConceptNetwork")
}
)
observeEvent(input$backTo_geneConceptNetwork, {
updateTabItems(session, "tabs", "geneConceptNetwork")
}
)
observeEvent(input$to_gsea, {
updateTabItems(session, "tabs", "gsea")
}
)
observeEvent(input$backTo_gsea, {
updateTabItems(session, "tabs", "gsea")
}
)
observeEvent(input$to_transcriptionFactorAnalysis, {
updateTabItems(session, "tabs", "transcriptionFactorAnalysis")
}
)
observeEvent(input$backTo_transcriptionFactorAnalysis, {
updateTabItems(session, "tabs", "transcriptionFactorAnalysis")
}
)
observeEvent(input$to_introduction, {
updateTabItems(session, "tabs", "introduction")
}
)
output$resNumCheck <- reactive({
funcEven(input$numCheck)
})
### START: DATA IMPORT AND QC SERVER CODE ###
###DATA IMPORTATION####
#Code for using GEO accession number to import data
#nk468188
geo_data<-reactive({
if(input$geoname==""){return()}
#If user wants series matrix data, uses getGEO function
#sneha-mr
else{
return(getGEO(input$geoname))}
})
#reactive value for metadata, can be changed later when outliers removed
meta_data<-reactiveVal()
#metadata input
meet<-reactive({
if(is.null(input$metadata)){
return(NULL)
}
else{
return(read.csv(input$metadata$datapath))
}
})
#Update reactive metadata variable when "Load Data" Button is hit
observeEvent(input$loaddat,meta_data(meet()))
selections<-reactive({
if(is.null(meet())){
return(NULL)
}
selections<-colnames(meet())[-1]
})
#Reading in CEL files from tar zipped user upload
celdat<-reactive({
if(is.null(input$celzip$datapath)){
return(NULL)
}
if(input$oligo=="Gene ST Array" || input$oligo=="Exon ST Array"){
#sneha-mr
#function to be used if user has chip that requires oligo package
return(read.celfiles(untar(input$celzip$datapath,list=TRUE)))
}
#if using any other chip, read in using affy package
#nk468188 and Modupe001
else{
affy<-ReadAffy(filenames=as.character(untar(input$celzip$datapath,list=TRUE)))
return(affy)
}
})
#ShreyaVora14, referenced code from aagarw30
#reading in txt and cSV data
data<-reactive({
file1<- input$file
if(is.null(file1)){return()}
if(grepl("\\.txt$", file1)[1]){
return(read.delim(file = file1$datapath))
}
else{
return(read.csv(file = file1$datapath))
}
})
#Text outputs to confirm data and metadata are loaded
#nk468188
# observeEvent(input$loaddat,output$obj<-renderText({
# if(is.null(data()) && is.null(celdat()) && is.null(geo_data())){
# return("Please upload data.")
# }
# else{
# return("Your data has been loaded. See the summary below.")
# }
# }))
#ShreyaVora14, referenced code from aagarw30
observeEvent(input$loaddat,output$csv_summary<-renderDT({
# if(is.null(data()) && is.null(geo_data())){
# return()
# }
# else
if(is.null(geo_data()) && is.null(data())==FALSE){
# print("Printed loc1")
datatable(data(),extensions = c('Responsive'), class = 'cell-border stripe',
options = list(pageLength = 10,responsive = TRUE))
}
else if(is.null(data()) && is.null(geo_data())==FALSE){
gn<-geo_data()
#sneha-mr
as.data.frame(exprs(gn[[1]]))
print("Printed loc2")
}
}))
#nk468188, referenced code form aagarw30
observeEvent(input$loaddat,output$raw_summary<-renderDT({
data2<-celdat()
if(is.null(data2)){
return(NULL)
}
print("Printed loc3")
datatable(exprs(data2),extensions = c('Responsive'), class = 'cell-border stripe',
options = list(pageLength = 10,responsive = TRUE))
}))
####QUALITY CONTROL######
#Reactive value to store data after normalization and/or batch correction
final_qc_dat<-reactiveVal()
#Normalization of data
#disha-22
observeEvent(input$normlzdata,
{
if(is.null(celdat())){
final_qc_dat(NULL)
}
else{
if(input$normlztype=="RMA" && input$oligo=="Other"){
norm_affy<-exprs(affy::rma(celdat()))
final_qc_dat(norm_affy)
output$norm_comp<-renderText("Normalization is complete.")
}
else if(input$normlztype=="GCRMA" && input$oligo=="Other"){
norm_affy<-exprs(gcrma(celdat()))
final_qc_dat(norm_affy)
output$norm_comp<-renderText("Normalization is complete.")
}
else if(input$normlztype=="MAS5" && input$oligo=="Other"){
norm_affy<-log(exprs(mas5(celdat())),2)
final_qc_dat(norm_affy)
output$norm_comp<-renderText("Normalization is complete.")
}
else if(input$normlztype=="RMA" && (input$oligo=="Gene ST Array" || input$oligo=="Exon ST Array")){
norm_affy<-exprs(oligo::rma(celdat()))
final_qc_dat(norm_affy)
output$norm_comp<-renderText("Normalization is complete.")
}
else if(input$normlztype=="GCRMA" && is.null(geo_data()) && (input$oligo=="Gene ST Array" || input$oligo=="Exon ST Array")){
norm_affy<-NULL
final_qc_dat(norm_affy)
}
else if(input$normlztype=="MAS5" && is.null(geo_data()) && (input$oligo=="Gene ST Array" || input$oligo=="Exon ST Array")){
norm_affy<-NULL
final_qc_dat(norm_affy)
}
}
})
#Dropdown selection for user to select which metadata column assigns each sample to batches
output$batch_cat<-renderUI({
selectInput("batch_feat","If samples come from different batches, specify which metadata feature indicates the batch each sample belongs to.",choices=colnames(meet())[-1])
})
#Perform batch correction if button pressed, update reactive value
observeEvent(input$startbatch,{
batch_cor_dat<-ComBat(final_qc_dat(),batch=meet()[,input$batch_feat])
final_qc_dat(batch_cor_dat)
output$batch_com<-renderText("Batch Correction is complete.")
}
)
#Visualize data prior to normalization using RLE or NUSE
#xgeng3 and nk468188
observeEvent(input$vis_dat,output$plot_raw<-renderPlot({
if(input$qc_method=="RLE" && input$oligo=="Other" && is.null(geo_data())){
affy.data=fitPLM(celdat())
RLE(affy.data,main="RLE")
}
else if(input$qc_method=="NUSE" && input$oligo=="Other"&& is.null(geo_data())){
affy.data=fitPLM(celdat())
NUSE(affy.data,main="NUSE")
}
else if(input$qc_method=="RLE" && (input$oligo=="Gene ST Array" || input$oligo=="Exon ST Array")&& is.null(geo_data())){
oligo.data=oligo::fitProbeLevelModel(celdat())
RLE(oligo.data,main="RLE")
}
else if(input$qc_method=="NUSE"&& (input$oligo=="Gene ST Array" || input$oligo=="Exon ST Array")&& is.null(geo_data())){
oligo.data=oligo::fitProbeLevelModel(celdat())
NUSE(oligo.data,main="RLE")
}
else if(input$qc_method=="NUSE"&& is.null(celdat())){
NUSE(fitPLM(geo_data()),main="NUSE")
}
else if(input$qc_method=="RLE"&& is.null(celdat())){
RLE(fitPLM(geo_data()),main="RLE")
}
}))
#Visualize normalized data using Boxplot or PCA
#nk468188
observeEvent(input$vis_button,{
if(input$qc_method2=="Boxplot"){
output$plot_status<-renderText("Your plot is being generated.")
output$qcplot<-renderPlot({
boxplot(final_qc_dat(),xlab="Sample Number",ylab="Gene Expression Values",main="Boxplot of Gene Expression for Each Sample",cex.axis=0.1,las=3)
})
}
else if(input$qc_method2=="PCA"){
pcacomps<-prcomp(final_qc_dat(),center=FALSE,scale=FALSE)
comps<-pcacomps$rotation
output$pc_comp<-renderUI({
selectInput("comp_plot","Which components do you want to plot?",choices=colnames(comps),multiple=TRUE)
})
output$feat<-renderUI({
selectInput("feat_color","Which feature do you want to group samples by?",choices=colnames(meet()))
})
}
})
#Specify Principal Components and Colors for PCA
#nk468188
observeEvent(input$pcplot,{
if(length(input$comp_plot)>2){
output$pcwarn<-renderText("Please only select two principal components.")
}
else if (length(input$comp_plot)<2){
output$pcwarn<-renderText("Please select two principal components.")
}
if(is.null(input$feat_color)){
output$pcwarn<-renderText("Please specify a feature to group samples by.")
}
else{
output$pcwarn<-NULL
pcacomps1<-prcomp(final_qc_dat(),center=FALSE,scale=FALSE)
comps1<-pcacomps1$rotation
input_comp<-as.vector(input$comp_plot)
pcs<-comps1[,input_comp]
pc1<-pcs[,1]
pc2<-pcs[,2]
colors<-meet()[,input$feat_color]
data_to_plot<-data.frame(pc1,pc2,colors)
p<-ggplot(data_to_plot,aes(x=pc1,y=pc2,color=colors))
p<-p+geom_point()+labs(color=input$feat_color)+ggtitle("PCA Plot for Normalized Data")+xlab("PC1")+ylab("PC2")
output$qcplot<-renderPlot(p)
}
})
#Outliers
observeEvent(input$getout,{
#Find outliers
outlier_affy<-outliers(final_qc_dat(),method=as.vector(input$outmethod))
output$potout<-renderUI(names(as.vector(outlier_affy@which)))
values<-outlier_affy@statistic
dat_fram<-data.frame(colnames(final_qc_dat()),values)
#Visualize outlier statistic value for each sample
p<-ggplot(data=dat_fram,aes(x=dat_fram[,1],y=dat_fram[,2]))+geom_col()+geom_hline(yintercept=outlier_affy@threshold)+ggtitle("Potential Outliers")+labs(y="Value of Selected Statistic",x="Sample")+theme(axis.text.x=element_text(size=7))
output$outplot<-renderPlot(p)
output$remove<-renderUI(selectInput("torem","Select outlier candidates you would like to remove.",multiple=TRUE,choices=as.list(names(outlier_affy@which))))
#Remove outliers and update expression matrix
observeEvent(input$update,{
expr_mat_2<-final_qc_dat()
meta_datframe<-meta_data()
for(name in input$torem){
sample_names<-colnames(expr_mat_2)
ind_to_remove<-which(sample_names==name)
expr_mat_2<-expr_mat_2[,-ind_to_remove]
meta_sample_names=meta_datframe[,1]
meta_ind_to_remove<-which(meta_sample_names==name)
meta_matrix<-meta_datframe[,-meta_ind_to_remove]
}
#Update reactive value
final_qc_dat(expr_mat_2)
meta_data(meta_datframe)
#Table of expression data with outlier samples removed
output$newexprs<-renderDT({
datatable(expr_mat_2,extensions = c('Responsive'), class = 'cell-border stripe',
options = list(pageLength = 10,responsive = TRUE))
})
})
})
observeEvent(input$grouped, {
updateTabItems(session, "tabs", "degAnalysis")
}
)
####STATISTICAL ANALYSIS####
#Data Annotation
#marmomeni
data_for_an<-reactive({
if(is.null(data())==FALSE){
qc_dat<-as.data.frame(data())
row.names(qc_dat)<-qc_dat[,1]
qc_dat<-qc_dat[,-1]
return(qc_dat)
}
else if(is.null(geo_data())==FALSE){
#sneha-mr
gn1<-geo_data()
return(as.data.frame(exprs(gn1[[1]])))
}
else if(is.null(celdat())==FALSE){
qc_dat<-final_qc_dat()
qc_dat<-as.data.frame(qc_dat,row.names=rownames(qc_dat))
return(qc_dat)
}
else{
return(NULL)
}
})
annotated_data <- reactive({
if(is.null(data_for_an())){
return(NULL)}
else{
data_stat<-data_for_an()
#Get gene symbols from hgu133plus.db
symbols<-AnnotationDbi::select(hgu133plus2.db, keys=row.names(data_stat), columns=c("SYMBOL"))
#Remove duplicate ProbeIDs
symbols <- symbols[!duplicated(symbols$PROBEID),]
data_stat$sym<-symbols$SYMBOL
#Remove NA values
data_stat<-na.omit(data_stat)
#Remove duplicate gene symbols
new_sym<-data_stat$sym
data_stat<-data_stat[,-which(colnames(data_stat)=="sym")]
data_stat<-collapseRows(data_stat,rowGroup=new_sym,rowID=rownames(data_stat))
expr_dat_frame2<-as.data.frame(data_stat$datETcollapsed)
return(expr_dat_frame2)
}
})
filtered_dat<-NULL
#Filter genes with low levels of expression
#marmomeni
observeEvent(input$filt_gen,filtered_dat<-({
dat_for_filter<-annotated_data()
gene_means<-rowMeans(dat_for_filter)
perc<-as.numeric(quantile(gene_means, probs=(input$cutoff/100), na.rm=T))
dat_for_filter[which(gene_means >= perc),]
output$gen_filt<-renderText({"The genes have been filtered."})
}))
#Adds to UI features of samples from metadata, user can select which features they wish to compare based on
#nk468188
output$col_selection<-renderUI({
selectInput("col_int","Select the feature you wish to analyze further",choices=colnames(meet()))
})
#Find DEGs
#nk468188
desmat1<-reactiveVal()
final_result<-eventReactive(input$degs,{
#Data to use based on if user filtered data or not
if(is.null(filtered_dat)){
dat_for_stat<-annotated_data()
}
else{
dat_for_stat<-filtered_dat
}
met_dat<-meta_data()
index_col<-which(colnames(met_dat)==input$col_int)
#marmomeni
variable<-factor(met_dat[,index_col])
des_matrix<-model.matrix(~0+variable,met_dat)
desmat1(des_matrix)
colnames(des_matrix)<-c("Factor_a","Factor_b")
fitting<-lmFit(dat_for_stat,des_matrix)
fac1<-colnames(as.data.frame(des_matrix))[1]
fac2<-colnames(as.data.frame(des_matrix))[2]
phr<-c(paste(fac1,fac2,sep="-"))
con_mat<-makeContrasts(contrasts=phr,levels=colnames(des_matrix))
fit.contrast<-contrasts.fit(fitting,con_mat)
stat.con<-eBayes(fit.contrast)
result<-topTable(stat.con,sort.by="p",p.value=input$p_val,number=length(rownames(dat_for_stat)))
}
)
output$toptab<-renderDT({
datatable(final_result(),extensions = c('Responsive'), class = 'cell-border stripe',
options = list(pageLength = 10,responsive = TRUE))
})
#Reactive Volcano Plot inputs
#disha-22
volcano_p<-reactive({input$m})
volcano_fc<-reactive({as.numeric(input$n)})
observeEvent(input$degs,{
output$plot1 <- renderPlot({
if(is.null(final_result)){
NULL
}
else{
result_to_plot<-final_result()
EnhancedVolcano(data.frame(result_to_plot),lab=rownames(data.frame(result_to_plot)),
x='logFC',y='P.Value',
title='RShiny Volcano Plot Demo',
pCutoff=volcano_p(), FCcutoff=volcano_fc(),
xlab = bquote(~Log[2] ~ "fold change"),
ylab = bquote(~-Log[10] ~ italic(P)))
}})})
######FUNCTIONAL ANALYSIS######
#disha-22
genelist<-reactive({
gene_table<-final_result()
GSEA.entrez <- AnnotationDbi::select(org.Hs.eg.db, keys=rownames(data.frame(gene_table)), columns=c("ENTREZID"),keytype="SYMBOL")
GSEA.entrez <- GSEA.entrez[!duplicated(GSEA.entrez$SYMBOL),]
row.names(GSEA.entrez) <- GSEA.entrez$SYMBOL
GSEA.genes <- merge(GSEA.entrez, gene_table, by="row.names")
genlist<-GSEA.genes$logFC
names(genlist) <- GSEA.genes$ENTREZID
genlist2 <- sort(genlist, decreasing=T)
return(genlist2)
})
msig <- msigdbr(species="Homo sapiens", category="H")
h <- msig %>% select(gs_name, entrez_gene)
observeEvent(input$gsea,output$plot_gsea <- renderPlot({
gsea <- GSEA(genelist(), TERM2GENE=h, eps=0)
gseaplot2(gsea, geneSetID=1:5, pvalue_table=T)
}))
#marmomeni and disha-22
eKegg <- reactive({
gene_entrez<-genelist()
enrichKEGG(gene = names(gene_entrez), organism = "hsa")
})
observeEvent(input$kegg,output$dotplot <- renderPlot({
dotplot(eKegg(), showCategory = input$x)
}))
observeEvent(input$kegg,output$barplot <- renderPlot({
barplot(eKegg(), showCategory = input$y)
}))
ont_cat<-reactive({
if(input$type=="Cellular Components"){
return("CC")
}
else if(input$type=="Molecular Functions"){
return("MF")
}
else if(input$type=="Biological Processes"){
return("BP")
}
})
#disha-22
enrichedgo <- reactive({
entrez_gene<-genelist()
enrichGO(gene = names(entrez_gene),
OrgDb = org.Hs.eg.db,
ont = ont_cat(),
readable = T,
pAdjustMethod = "fdr")
})
enrichedforplot <- reactive({setReadable(enrichedgo(), OrgDb = org.Hs.eg.db)})
observeEvent(input$go,output$dotplot2 <- renderPlot({
dotplot(enrichedforplot(), showCategory = input$a)
}))
observeEvent(input$go,output$barplot2 <- renderPlot({
barplot(enrichedforplot(), showCategory = input$b)
}))
observeEvent(input$go,output$GOgraph <- renderPlot({
plotGOgraph(enrichedforplot())
}))
output$exportgsea <- downloadHandler(
filename=function(){
paste(input$plot_gsea,".png",sep='')
},
content=function(file){
ggsave(file,plot=plot_gsea(),device=device)
}
)
output$exportkegg <- downloadHandler(
filename=function(){
paste(input$plot_kegg,".png",sep='')
},
content=function(file){
ggsave(file,plot=plot_kegg(),device=device)
}
)
output$exportgo <- downloadHandler(
filename=function(){
paste(input$plot_go,".png",sep='')
},
content=function(file){
ggsave(file,plot=plot_go(),device=device)
}
)
### END: DATA IMPORT AND QC SERVER CODE ###
}