-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.R
191 lines (150 loc) · 5.22 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
library(shiny)
library(shinydashboard)
# library(caret)
library(tidyverse)
# library(TCseq)
library(ggplot2)
library(plotly)
library(ggfortify)
library(gridExtra)
library(umap)
library(gplots)
library(scales)
library(Rtsne)
library(reshape)
library(reshape2)
library(reticulate)
# library(data.table)
library(rlang)
server <- function(input, output,session) {
output$expression.txt <- downloadHandler(
filename = "expression.txt",
content = function(file) {
file.copy("www/data/expression.txt", file)
}
)
df = reactive({
req(input$file1)
dat <- read.table(input$file1$datapath,
header = input$header,
sep = input$sep,
quote = input$quote,
row.names = 1)
return(dat)
})
# df = df[,is.na(colSums(df=='Error'))] %>% na.omit(df) #remove columns with 'Error' and rows with NA
output$contents <- renderTable({
dat = cbind(row.names(df()),df()) %>% `colnames<-`(c('genes',colnames(df())))
if(input$disp == "head") {
return(head(dat))
}
else {
return(dat)
}
})
# df2 = reactive({
# req(input$file1)
# dat <- read.table(input$file1$datapath,
# header = input$header,
# sep = input$sep,
# quote = input$quote)
# return(dat)
# })
#heatmap
# ord <- reactive({hclust( dist(scale(df()), method = "euclidean"), method = "ward.D" )$order})
dat_heatmap = reactive({
dat = cbind(df(),kmeans(df(),input$k)$cluster)
# row.names(dat) = dat[,1]
# dat = dat[-1]
# dat = melt(read.table(input$file1$datapath,header = 1)[ord(),])
colnames(dat)[dim(dat)[2]] = 'Cluster'
dat = dat[order(dat$'Cluster'),][-dim(dat)[2]]
# dat = gather(df2(),experiments,expression,2:length(df2()[1,]))
dat = reshape2::melt(as.matrix(dat))
# dat = melt(setDT(dat, keep.rownames = TRUE), "rn")
# dat$rn = factor(dat$rn,levels = dat$rn)
return(dat)
# return(melt(as.matrix(dat)))
# [,c('X2','X1','value')]
})
output$heatmap = renderPlot({
# if(is.null(input$file1)){return()}
ggplot( dat_heatmap(), aes(x = Var2,y = factor(Var1,levels = unique(Var1))) )+
geom_tile(aes(fill = value))+
scale_fill_gradient(low="grey90", high="red") +
labs(x= 'exp',y = 'gene')+
theme(axis.text.y = element_text(size = 6))
})
#
# output$heatmap = renderPlot({
# if(is.null(input$file1)){return()}
#
# ggplot( dat_heatmap(), aes(x = variable,y = factor(rn,levels = unique(rn))) )+
# geom_tile(aes(fill = value))+
# scale_fill_gradient(low="grey90", high="red") +
# labs(x= 'exp',y = 'gene')+
# theme(axis.text.y = element_text(size = 6))
# })
#
# dat = reactive({
# dat = timeclust(as.matrix(df()),
# algo = input$algo,
# k = input$k,
# standardize = TRUE)
# # iter.max=input$iter.max)
# return(dat)
# })
# output$clustering = renderPlot({
# if(is.null(input$file1)){return()}
# timeclustplot(dat(),categories = "time points",col =1,axis.text.size = 11)[0]
# })
dat_pca_result = reactive({
dat = merge(as.data.frame(prcomp(df())$x),as.data.frame(kmeans(df(),input$k)$cluster),by ='row.names',all.x=T)
colnames(dat)[dim(dat)[2]] = 'Cluster'
colnames(dat)[1] = 'genes'
dat = dat[order(dat$Cluster),]
return(dat)
})
# output$pca <- renderPlotly({
# ggplotly(ggplot(dat_pca(),aes(x=PC1,y=PC2,color= Cluster))+
# geom_point())
#
# })
output$threed_clustering <- renderPlotly({
plot_ly(dat_pca_result(), x = ~PC1, y = ~PC2, z = ~PC3, color = ~Cluster,type ='scatter3d',mode = 'markers', text = ~genes, hoverinfo='text')
# text = ~genes
# %>% add_markers() %>%
# layout(scene = list(xaxis = list(title = 'PC1'),
# yaxis = list(title = 'PC2'),
# zaxis = list(title = 'PC3')))
})
# compare
# beginning <- reactive({Sys.time()})
pca = reactive({prcomp(t(df()))})
# end <- reactive({Sys.time()})
# t_prcomp <- reactive({as.numeric(beginning() - end())})
# beginning <- reactive({Sys.time()})
tsne = reactive({Rtsne(df())})
# end <- reactive({Sys.time()})
# t_tsne <- reactive({as.numeric(beginning() - end())})
# beginning <- reactive({Sys.time()})
Umap = reactive({umap(as.matrix(df()))})
# end <- reactive({Sys.time()})
# t_umap <- reactive({as.numeric(beginning() - end())})
output$cluster_compare = renderPlot({grid.arrange(
ggplot(as.data.frame(pca()$rotation),aes(x=PC1,y=PC2))+
geom_point()+labs(title = 'pca'),
ggplot(as.data.frame(tsne()$Y),aes(x=V1,y=V2))+
geom_point()+labs(x= 'tsne 1', y = 'tsne 2', title = 'tsne'),
ggplot(as.data.frame(Umap()$layout),aes(x = V1,y=V2))+
geom_point()+labs(x= 'umap 1', y = 'umap 2', title = 'umap'),
# ggplot( dat_heatmap(), aes(variable, name) ) +
# geom_tile(aes(fill = value)),
# scale_fill_gradient2(low=muted("blue"), high=muted("red")),
# ggplot(as.data.frame(rbind(t_prcomp(),t_tsne(),t_umap())),aes(x = c('PCA','tsne','Umap'), y = V1)) +
# geom_col() + labs(x = 'Algorithms', y = 'Running time'),
ncol = 2,
nrow = 2
)
})
}