-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.R
323 lines (245 loc) · 9.08 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
############################################################################
# iSeq: A Web-based server for RNA-seq Data Analysis and Visualization
# Copyright (C) 2016 Chao Zhang
#
# Any bugs or suggestion please report to Czh3([email protected])
############################################################################
options(shiny.maxRequestSize=40*1024^2)
options(Expressions=500000)
library(ggplot2)
library(reshape2)
library(shinyBS)
library(zeroclipr)
library(plotly)
source("ReadFile.R", local=TRUE)
shinyServer(func = function(input, output, session) {
######### upload file #############
#source("upload.R", local=TRUE)
# upload file
EXP <- reactive({
if(is.null(input$file1)){
if(input$ExampleData == 0){
return(NULL)
} else {
return(loadExample())
}
} else {
return(uploadData())
}
})
uploadData <- reactive({
inFile1 <- input$file1
if (is.null(inFile1)){
return(NULL)
}
Expression <- csvRead(inFile1$datapath)
inFile2 = input$file2
if (is.null(inFile2)){
condition <- data.frame(Condition = colnames(Expression))
condition <- t(condition)
colnames(condition) = colnames(Expression)
condition <- as.data.frame(condition)
} else {
condition <- read.csv(inFile2$datapath, header= T, sep=',', row.names = 1, stringsAsFactors = F)
rownames(condition) = c("Condition")
if (nrow(condition) != 1 ){
createAlert(session, "alert", "exampleAlert", title = "Oops",
content = "The condition file MUST have 2 lines", append = FALSE, dismiss = FALSE)
}
if(is.null(Expression)){
createAlert(session, "alert1", "exampleAlert", title = "Upload Expression data",
content = "", append = FALSE)
} else if (ncol(condition) != ncol(Expression)){
createAlert(session, "alert2", "exampleAlert", title = "Oops",
content = "The columns of condition file MUST be the same as Expression file", append = FALSE, dismiss = FALSE)
} else if (length(unique(t(condition)[,1])) <= 1){
createAlert(session, "alert3", "exampleAlert", title = "Just one condition ?", append = FALSE, dismiss = FALSE)
}
}
Expression = NORM()
return(list('Expression' = Expression, 'condition' = condition))
})
loadExample <- reactive({
tmp = Example()
tmp$Expression = NORM()
return(tmp)
})
# load example
Example <- eventReactive(input$ExampleData,{
Expression = csvRead("data/Test.Expression.csv")
condition = read.csv("data/Test.Condition.csv", header= T, sep=',', row.names = 1, stringsAsFactors = F)
Expression = NORM()
return(list('Expression' = Expression, 'condition' = condition))
})
output$fileUploaded <- reactive({
return(!is.null(EXP()))
})
outputOptions(output, 'fileUploaded', suspendWhenHidden=FALSE)
output$fileUploaded1 <- reactive({
return(is.null(EXP()))
})
outputOptions(output, 'fileUploaded1', suspendWhenHidden=FALSE)
########## Upload page #############
# Expression file
output$contents <- DT::renderDataTable({
DT::datatable(EXP()$Expression)
})
# condition file
output$condition <- renderTable({
return(EXP()$condition)
})
######### down load Expression ####
output$downloadExpression <- downloadHandler(
filename = function() {
paste(input$file1, '.iSeq.csv', sep='')
},
content = function(file) {
write.csv(EXP()$Expression, file, quote = F)
}
)
########## main plots ##############
#source("main_plot.R", local=TRUE)
# main plots
plots <- function(Expression){
withProgress(message = 'QC plot:', value = 0, {
incProgress(1/5, detail = "Summary")
incProgress(2/5, detail = "Density")
#distribution
output$distribution <- renderPlotly({
if (ncol(Expression) > 20){
return(NULL)
}
Expression.melt = melt(log10(Expression + 1))
colnames(Expression.melt) = c("Samples", "value")
p = qplot(value, ..density.., data=Expression.melt, geom="density", fill=Samples, alpha=I(.5), ylab = "Density", xlab = "log10(Expression + 1)") +
theme_bw(base_size = 30) +
theme(panel.border = element_rect(colour = "black", size = 2))
ggplotly(p)
})
incProgress(3/5, detail = "MA plot")
# MA plot
output$MA_plot <- renderPlot({
library(affy)
par(cex = 2)
exp = log2(Expression[,1:2] + 1)
ma.plot( rowMeans(exp), (exp[, 1] - exp[, 2]), cex = 1.5, lwd = 2)
title(paste("MA plot:", colnames(exp)[1], "vs.", colnames(exp)[2]))
})
incProgress(4/5, detail = "Heatmap")
# correlation heatmap
output$clustHeatmap <- renderPlot({
d = 1 - cor(Expression)
library(RColorBrewer)
library(gplots)
hmcol = colorRampPalette(brewer.pal(9, "GnBu"))(100)
heatmap.2(d, trace="none", col = rev(hmcol), main="Correlation Heatmap")
})
incProgress(5/5, detail = "hclust")
# hclust
output$hclust <- renderPlot({
par(cex = 2)
#d = dist(t(log2(Expression+1)))
d = dist(t(Expression))
h = hclust(d)
plot(h, xlab = "Samples")
})
})
}
######### normalization ############
#source("normalization.R", local=TRUE)
# normalization Expression level
NORM <- reactive({
normalizatedMethod <- input$Normalization
inFile <- input$file1
if (is.null(inFile)){
if(input$ExampleData == 0){
return(NULL)
} else {
exp_path = "data/Test.Expression.csv"
}
} else {
exp_path = inFile$datapath
}
if(normalizatedMethod == 1){
# None
Expression <- csvRead(exp_path)
} else if(normalizatedMethod == 2){
# Quantiles
Expression <- csvRead(exp_path)
library(preprocessCore)
Expression.matrix = as.matrix(Expression)
Expression.matrix = normalize.quantiles(Expression.matrix)
colnames(Expression.matrix) = colnames(Expression)
rownames(Expression.matrix) = rownames(Expression)
Expression <- as.data.frame(Expression.matrix)
} else if(normalizatedMethod == 3){
# size factors
Expression <- csvRead(exp_path)
library( "DESeq" )
sizeF = estimateSizeFactorsForMatrix(Expression)
for (i in 1:length(sizeF)){
Expression[,i] <- Expression[,i] / sizeF[i]
}
}
return(Expression)
})
output$box_plot <- renderPlotly({
withProgress(message = 'Normalization:', value = 0, {
incProgress(1/3, detail = "read data")
Expression <- NORM()
incProgress(2/3, detail = "normalize")
p <- plots(Expression)
Expression.melt = melt(log10(Expression + 1))
colnames(Expression.melt) = c("Samples", "value")
p = qplot(Samples, value, data=Expression.melt, geom="boxplot", fill=Samples, alpha=I(.5), xlab = " ", ylab = "log10(Expression + 1)") +
theme_bw(base_size = 30) +
theme(panel.border = element_rect(colour = "black", size = 2),
legend.key.size = unit(1.2, "cm"),
axis.text.x = element_text(angle = 45, hjust = 1))
incProgress(3/3, detail = "plots")
ggplotly(p)
})
})
########### DEGs calling ###########
### DESeq
source("DESeq.R", local=TRUE)
### Fold Change
source("FoldChange.R", local=TRUE)
## setup the copy button.
COPY <- function(DEGs){
output$copyAll <- renderUI({
str <- textConnection("copy_DEGs", open = "w")
write(rownames(DEGs), str)
close(str)
zeroclipButton("clipbtn", "Copy All Differentially Expressioned Genes to Clipboard", paste(copy_DEGs, collapse= "\n"), icon("clipboard"))
})
output$copyUp <- renderUI({
str <- textConnection("copy_DEGs", open = "w")
write(rownames(DEGs[DEGs$log2FoldChange > 0,]), str)
close(str)
zeroclipButton("clipbtn1", "Copy Up-regulated Gene List to Clipboard", paste(copy_DEGs, collapse= "\n"), icon("clipboard"))
})
output$copyDown <- renderUI({
str <- textConnection("copy_DEGs", open = "w")
write(rownames(DEGs[DEGs$log2FoldChange < 0,]), str)
close(str)
zeroclipButton("clipbtn2", "Copy Down-regulated Gene List to Clipboard", paste(copy_DEGs, collapse= "\n"), icon("clipboard"))
})
}
########## Gene Ontology Enrichment ####
### David ###
source("David.R", local=TRUE)
### GOseq ###
source("GOseq.R", local=TRUE)
##### #####
############ PLOTs ##############
##### #####
########## bar plot #############
source("Barplot.R", local=TRUE)
########## gene heatmap ############
source("heatmap.R", local=TRUE)
############# PCA ##################
source("PCA.R", local=TRUE)
############# t-SNE ##################
source("tSNE.R", local=TRUE)
})