forked from Ulas-lab/Shiny-Seq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPCA.R
216 lines (189 loc) · 7.96 KB
/
PCA.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
source("functions.R")
#Module PCA
#UI component
#Server component: Calls Pcaplot function from fucntions.R
PCA_UI<-function(id)
{
ns<-NS(id)
tagList(
#User input: PCA type: 2D/3D
radioButtons(ns("plotType"), "PCA Type:", choices = c("2D", "3D")),
#User input: Annotation variables to color samples in PCA
uiOutput(ns("annotation_pca")),
#User input: Compute PCA for top 500 most variable genes/All genes
selectInput(ns("top"), label = h5("Should PCA be computed for:"),
choices = list("Top 500 genes" = 1, "All" = 2),
selected = 2),
uiOutput(ns("point_size")),
#Download button: PCA gets downloaded on user click
fluidRow(column(3," "),column(6,downloadButton(ns('download_pca_svg'), 'Download plot as svg'))),
#Output: Display PCA plot
fluidRow(column(1," "),
column(6,plotlyOutput(ns("pca"))))
)
}
PCA_module<-function(input,output,session,data,resize_factor=NULL)
{
output$point_size<-renderUI({
req(input$plotType)
if(input$plotType=="2D")
{
textInput(session$ns("pca_2d_plot_point_size"),label="Set point size in plot",value = "5")
}
})
#Creates a checkbox widget containing annotation variables to color samples in PCA
output$annotation_pca<-renderUI({
#if (!is.null(input$file2)) {
print("create Checkbox")
checklist = list()
#print(head(data[[3]]))
for (i in seq_along(colnames(data[[2]]))) {
checklist[[colnames(data[[2]])[[i]]]] = i
}
radioButtons(session$ns("annotation_pca"), "Choose the annotation", checklist)
#}
})
#Generate PCA plot
output$pca<-renderPlotly({
point_size=1
req(input$pca_2d_plot_point_size)
if(input$pca_2d_plot_point_size!="") point_size=as.numeric(input$pca_2d_plot_point_size)
pcaplot(data = data, annotation = input$annotation_pca, top = input$top,
plotType = input$plotType, resize_factor = resize_factor,
point_size = point_size)[[1]]
})
#Download pca in svg format
output$download_pca_svg <- downloadHandler(
filename = function(){
if (input$plotType == "2D")
{
if(input$top==2) paste('2D PCA of Raw data all genes.svg')
else paste('2D PCA of Raw data top 500 genes.svg')
}
else if(input$plotType == "3D")
{
if(input$top==2) paste('3D PCA of Raw data all genes.pdf')
else paste('3D PCA of Raw data top 500 genes.pdf')
}
},
content = function(file) {
point_size=1
req(input$pca_2d_plot_point_size)
if(input$pca_2d_plot_point_size!="") point_size=as.numeric(input$pca_2d_plot_point_size)
p<-pcaplot(data = data, annotation = input$annotation_pca, top = input$top,
plotType = input$plotType, point_size = point_size)[[2]]
ggsave(file,p, width = 22, height = 15, units = "cm")
}
)
return(reactive({input$top}))
}
PCA_module2<-function(input,output,session,data,resize_factor=NULL)
{
output$point_size<-renderUI({
req(input$plotType)
if(input$plotType=="2D")
{
textInput(session$ns("pca_2d_plot_point_size"),label="Set point size in plot",value = "5")
}
})
#Creates a checkbox widget containing annotation variables to color samples in PCA
output$annotation_pca<-renderUI({
#if (!is.null(input$file2)) {
print("create Checkbox")
checklist = list()
#print(head(data[[3]]))
for (i in seq_along(colnames(data[[2]]))) {
checklist[[colnames(data[[2]])[[i]]]] = i
}
radioButtons(session$ns("annotation_pca"), "Choose the annotation", checklist)
#}
})
#Generate PCA plot
output$pca<-renderPlotly({
point_size=1
req(input$pca_2d_plot_point_size)
if(input$pca_2d_plot_point_size!="") point_size=as.numeric(input$pca_2d_plot_point_size)
pcaplot(data = data, annotation = input$annotation_pca, top = input$top,
plotType = input$plotType, resize_factor = resize_factor,
point_size = point_size)[[1]]
})
#Download pca in svg format
output$download_pca_svg <- downloadHandler(
filename = function(){
if (input$plotType == "2D")
{
if(input$top==2) paste('2D PCA of Normalized data all genes.svg')
else paste('2D PCA of Normalized data top 500 genes.svg')
}
else if(input$plotType == "3D")
{
if(input$top==2) paste('3D PCA of Normalized data all genes.pdf')
else paste('3D PCA of Normalized data top 500 genes.pdf')
}
},
content = function(file) {
point_size=1
req(input$pca_2d_plot_point_size)
if(input$pca_2d_plot_point_size!="") point_size=as.numeric(input$pca_2d_plot_point_size)
p<-pcaplot(data = data, annotation = input$annotation_pca, top = input$top,
plotType = input$plotType, point_size = point_size)[[2]]
ggsave(file,p, width = 22, height = 15, units = "cm")
}
)
return(reactive({input$top}))
}
PCA_module3<-function(input,output,session,data,resize_factor=NULL)
{
output$point_size<-renderUI({
req(input$plotType)
if(input$plotType=="2D")
{
textInput(session$ns("pca_2d_plot_point_size"),label="Set point size in plot",value = "5")
}
})
#Creates a checkbox widget containing annotation variables to color samples in PCA
output$annotation_pca<-renderUI({
#if (!is.null(input$file2)) {
print("create Checkbox")
checklist = list()
#print(head(data[[3]]))
for (i in seq_along(colnames(data[[2]]))) {
checklist[[colnames(data[[2]])[[i]]]] = i
}
radioButtons(session$ns("annotation_pca"), "Choose the annotation", checklist)
#}
})
#Generate PCA plot
output$pca<-renderPlotly({
point_size=1
req(input$pca_2d_plot_point_size)
if(input$pca_2d_plot_point_size!="") point_size=as.numeric(input$pca_2d_plot_point_size)
pcaplot(data = data, annotation = input$annotation_pca, top = input$top,
plotType = input$plotType, resize_factor = resize_factor,
point_size = point_size)[[1]]
})
#Download pca in svg format
output$download_pca_svg <- downloadHandler(
filename = function(){
if (input$plotType == "2D")
{
if(input$top==2) paste('2D PCA of batch corrected data all genes.svg')
else paste('2D PCA of batch corrected data top 500 genes.svg')
}
else if(input$plotType == "3D")
{
if(input$top==2) paste('3D PCA of batch corrected data all genes.pdf')
else paste('3D PCA of batch corrected data top 500 genes.pdf')
}
},
content = function(file) {
point_size=1
req(input$pca_2d_plot_point_size)
if(input$pca_2d_plot_point_size!="") point_size=as.numeric(input$pca_2d_plot_point_size)
p<-pcaplot(data = data, annotation = input$annotation_pca, top = input$top,
plotType = input$plotType, point_size = point_size)[[2]]
ggsave(file,p, width = 22, height = 15, units = "cm")
}
)
return(reactive({input$top}))
}