forked from Ulas-lab/Shiny-Seq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Biological_Process_module.R
290 lines (245 loc) · 9.12 KB
/
Biological_Process_module.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
source("functions.R")
Biological_process_module_UI<-function(id)
{
ns<-NS(id)
tagList(
bscols(widths = 10,div(style="height: 15px;",width = '200px')),
a("Click to be directed to Gene Ontology Consortium ", href="http://www.geneontology.org/", target="_blank"),
bscols(widths = 10,div(style="height: 15px;",width = '200px')),
fluidRow(column(10,DT::dataTableOutput(ns("Enriched_bp")))),
br(),
hr(),
br(),
fluidRow(
column(1,
selectInput(ns("datachoice10") ,label = h5("Select Data Type"),
choices = list("Excel" = 1, "CSV" = 2),
selected = 1)),
column(1,
br(),
br(),
downloadButton(ns('download_Enriched_BP_Table'), 'Download Enriched BP Table'))),
DT::dataTableOutput(ns("filtered_BP")),
br(),
br(),
hr(),
br(),
uiOutput(ns("plot_option_bp")),
uiOutput(ns("plot_category_bp")),
uiOutput(ns("plot_category_go")),
downloadButton(ns('download_bp_plot'), 'Download Plot'),
plotOutput(ns("bp"))
)
}
Biological_process_module<-function(input,output,session,DE_genes,
organism,dds.fc,combination,wgcna_output)
{
#Compute the GO terms based on a list for a list of DE genes(uses function enrichGO from clusterprofiler)
#Construct a matrix where row->comparison A vs B, C vs D .etc
# columns (up regulated GO terms, down regulated GO terms)
#Loop through a similar matrix generated by reactive DE_genes)
#Filter GO terms for each element in the matrix generated by reactive DE_genes
#enrichment analysis
combo<-combination()
print(combo())
num<-length(combo())
Enriched_BP<-reactive({
print("inside biological process line 39")
print(organism())
result<-DE_genes()
if(!is.null(wgcna_output()))
{
if((length(wgcna_output()$modules())>0))
{
enrichment_main("biological process",result(),organism(),dds.fc(),
length(combo()),wgcna_output()$modules(),wgcna_output()$WGCNA_matrix(),NULL)
}
}
else
{
print("inside biological process line 53")
print(combination())
print(unlist(combination()))
combo<-combination()
print(combo())
num<-length(combo())
print(combo())
print(num)
enrichment_main("biological process",result(),organism(),dds.fc(),
num,NULL,NULL,NULL)
}
})
#Display summary of biological process
output$Enriched_bp <- DT::renderDataTable({
result<-Enriched_BP()[[1]]
print(result)
rows<-num
modules<-NULL
WGCNA_matrix<-NULL
res<-NULL
if(!is.null(wgcna_output()))
{
if((length(wgcna_output()$modules())>0))
{
modules<-as.data.frame(table(wgcna_output()$modules()))
colnames(modules)<-c("Var1","number")
entry<-c(as.vector(combo()), as.vector(modules$Var1))
print(entry)
rows<-length(entry)
res<-data.frame(matrix(NA, nrow = rows, ncol = 3))
colnames(res)<-c('Up regulated','Down regulated','Regulated')
print(modules$Var1)
rownames(res)<-lapply(1:rows, function(i) {
unlist(entry[i])
})
for(i in 1:length(combo()))
{
res[i,1]<-nrow(as.data.frame(result[[i]][[1]]))
res[i,2]<-nrow(as.data.frame(result[[i]][[2]]))
res[i,3]<-0
}
for(i in 1+length(combo()):nrow(modules))
{
print('res')
print(result[[i]][[3]])
res[i,1:2]<-0
res[i,3]<-nrow(as.data.frame(result[[i]][3]))
}
}
}
else
{
res<-data.frame(matrix(NA, nrow = length(combo()), ncol = 2))
rownames(res)<-lapply(1:length(combo()), function(i) {
print(combo())
combo()[[i]]
})
colnames(res)<-c('Enriched BP for Up-reg genes','Enriched BP for Down-reg genes')
print(res)
print(res[1,1])
for(i in 1:length(combo()))
{
print(nrow(as.data.frame(result[[i]][[1]])))
res[i,1]<-nrow(as.data.frame(result[[i]][[1]]))
res[i,2]<-nrow(as.data.frame(result[[i]][[2]]))
}
}
print(res)
DT::datatable(res,class = 'cell-border stripe',
selection = list(mode='single',target = 'cell'),
extensions = list('Scroller'=NULL,'Buttons'=NULL),
options = list(deferRender = TRUE,scrollX = TRUE,scrollY = 150,scroller = TRUE,dom = 'Bfrtip',
buttons = list('copy', list(extend = 'collection',buttons = c('csv', 'excel', 'pdf'),
text = 'Download table'))),
escape = FALSE
)
})
#Display GO terms for the selected comparison
observeEvent(input$Enriched_bp_cell_clicked,{
print('hey')
print(input$Enriched_bp_cells_selected)
print(input$Enriched_bp_cell_clicked)
selected <- input$Enriched_bp_cells_selected
row<-selected[1]
print('row')
print(row)
col<-selected[2]
print('col')
print(col)
if(length(selected)>0){
output$filtered_BP <- DT::renderDataTable({
print('hey')
result<-Enriched_BP()[[1]]
df<-as.data.frame(result[[row]][[col]])
print(head(df))
DT::datatable(df,class = 'cell-border stripe',
selection = list(target = 'column'),
extensions = list('Scroller'=NULL,'Buttons'=NULL),
options = list(deferRender = TRUE,scrollX = TRUE,scrollY = 150,scroller = TRUE,dom = 'Bfrtip',
buttons = list('copy')))
})
#download button
output$download_Enriched_BP_Table <- downloadHandler(
filename = function()
{
if(as.numeric(input$datachoice10==1)){
condition <-combo()[[row]]
if(col==1) paste('Up regulated BP for ',condition,'.xlsx')
else if(col==2) paste('Down regulated BP for ',condition,'.xlsx')
}
else {
condition <-combo()[[row]]
if(col==1) paste('Up regulated BP for ',condition,'.csv')
else if(col==2) paste('Down regulated BP for ',condition,'.csv')
}
},
content = function(file) {
#sort by adjusted p value.
print('heyho')
result<-Enriched_BP()[[1]]
df<-as.data.frame(result[[row]][[col]])
nam<-'Sheet1'
condition <-combo()[[row]]
condition<-str_replace_all(condition,"[^[:alnum:]]",".")
if(col==1) nam<-paste('Up regulated BP for ',condition)
else if(col==2) nam<-paste('Down regulated BP for ',condition)
if(as.numeric(input$datachoice10==1)){
M <- as.matrix(df)
wb <- createWorkbook()
addWorksheet(wb, sheetName = "Biological Processes")
writeData(wb = wb, sheet = 1, x = M, colNames = T, rowNames = T)
saveWorkbook(wb, file)
}
else{
write.csv(df, file)
}
}
)
output$plot_category_bp<-renderUI({
textInput(session$ns("category_bp"),label = h6("Enter number of categories to display"),
value = "10")
})
output$plot_category_go<-renderUI({
textInput(session$ns("category_go"),label = h6("Enter number of GO levels to display"),
value = "")
})
print(as.numeric(input$category_bp))
#Display boxplot of to 10 GO term of selected comparison
bpplot<-reactive(
{
result<-Enriched_BP()[[2]] #obj
res<-Enriched_BP()[[1]]
req(input$category_bp)
if(nrow(res[[row]][[col]]) == 0){
warning("No Data available for plotting")
}
else if(nrow(res[[row]][[1]]) == 0 && nrow(res[[row]][[2]] != 0)){
enrichment_plot("biological process",result,res,row,1,input$category_bp,input$category_go)
}
else{
enrichment_plot("biological process",result,res,row,col,input$category_bp,input$category_go)
}
})
output$bp<- renderPlot({
bpplot()
})
output$download_bp_plot <- downloadHandler(
filename =function()
{
condition <-combo()[[row]]
if(col==1) paste('Barplot of Up regulated BP for ',condition,'.pdf')
else if(col==2) paste(' Barplot of Down regulated BP for ',condition,'.pdf')
},
content = function(file) {
req(input$category_bp)
ggsave(file,bpplot())#,width=800, height=500)
#dev.off()
})
}
})
return(list(
Enriched_BP_table=reactive({Enriched_BP()[[1]]}),
Enriched_BP_obj=reactive({Enriched_BP()[[2]]})
)
)
}