-
Notifications
You must be signed in to change notification settings - Fork 41
/
server-filterdata.R
286 lines (235 loc) · 10.5 KB
/
server-filterdata.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
## ==================================================================================== ##
# START Shiny App for analysis and visualization of transcriptome data.
# Copyright (C) 2016 Jessica Minnier
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# You may contact the author of this code, Jessica Minnier, at <[email protected]>
## ==================================================================================== ##
##
observe({
print("server-datafilter-update-filters")
data_analyzed = analyzeDataReactive()
tmpdatlong = data_analyzed$data_long
tmpynames = tmpdatlong%>%select(-unique_id,-sampleid,-group,-one_of("rep"))%>%colnames()
tmpgroups = data_analyzed$group_names
tmpsamples = as.character(data_analyzed$sampledata$sampleid)
tmpgeneids = data_analyzed$geneids
data_analyzedgenes = as.character(unlist(tmpgeneids))
tmptests = unique(as.character(data_analyzed$results$test))
updateSelectizeInput(session,"datafilter_groups",
choices=tmpgroups,selected=tmpgroups)
updateSelectizeInput(session,"datafilter_samples",
choices=tmpsamples,selected=tmpsamples)
updateSelectizeInput(session,"datafilter_gene_select",
choices=data_analyzedgenes,server=TRUE)
updateSelectizeInput(session,"datafilter_selecttest",
choices=tmptests,
selected = tmptests[1]
)
updateRadioButtons(session,'datafilter_selectexpr',
choices=sort(tmpynames,decreasing = TRUE),
selected= sort(tmpynames,decreasing = TRUE)[1])
}, priority=1)
# after selecting test
observe({
print("server-datafilter-update-tests")
data_analyzed = analyzeDataReactive()
tmptests = unique(as.character(data_analyzed$results$test))
if(input$datafilter_selecttest%in%tmptests) {
tmptest = input$datafilter_selecttest
# get max abs fold change for this test
tmpdat = data_analyzed$results
tmpdat = tmpdat%>%filter(test==tmptest)
tmpfc = abs(tmpdat$logFC)
tmpfc = tmpfc[tmpfc<Inf]
tmpmax = max(tmpfc,na.rm=T)
if(tmpmax==Inf)
updateNumericInput(session,"datafilter_fccut",
min=0,max= ceiling(tmpmax),value=0)
}
}, priority = 2)
# after selecting expression value
observe({
print("server-datafilter-update-expr")
data_analyzed = analyzeDataReactive()
tmpynames = data_analyzed$data_long%>%select(-unique_id,-sampleid,-group,-one_of("rep"))%>%colnames()
if(input$datafilter_selectexpr%in%tmpynames) {
exprname = input$datafilter_selectexpr
#calculate miin and max
tmpdat = data_analyzed$data_long
tmpmin = min(tmpdat[,colnames(tmpdat)==exprname],na.rm=T)
tmpmax = max(tmpdat[,colnames(tmpdat)==exprname],na.rm=T)
updateNumericInput(session,"datafilter_exprmin",
min=floor(tmpmin),max= ceiling(tmpmax),value=floor(tmpmin))
updateNumericInput(session,"datafilter_exprmax",
min=floor(tmpmin),max= ceiling(tmpmax),value=ceiling(tmpmax))
}
}, priority = 2)
# after selecting group
observe({
print("server-datafilter-update-samples")
data_analyzed = analyzeDataReactive()
tmpselected = input$datafilter_groups
print(tmpselected)
if(!is.null(tmpselected)) {
if(!(tmpselected[1]=="")) {
tmpselected = input$datafilter_groups
tmpsampledata = data_analyzed$sampledata
tmpsampledata = tmpsampledata %>% filter(group%in%tmpselected)
tmpsamples = as.character(tmpsampledata$sampleid)
print(tmpsamples)
updateSelectizeInput(session,"datafilter_samples",
choices=tmpsamples,
selected=tmpsamples)
}
}
}, priority = 2)
# after selecting test
# observe({
# print("server-datafilter-update-tests")
# data_analyzed = analyzeDataReactive()
# if(!(input$datafilter_selecttest=="")) {
# tmptest = input$datafilter_selecttest
# # get max abs fold change for this test
# tmpdat = data_analyzed$results
# tmpdat = tmpdat%>%filter(test==tmptest)
# tmpfc = abs(tmpdat$logFC)
# tmpfc = tmpfc[tmpfc<Inf]
# tmpmax = max(tmpfc,na.rm=T)
# if(tmpmax==Inf)
#
# updateNumericInput(session,"datafilter_fccut",
# min=0,max= ceiling(tmpmax),value=0)
# }
# })
# after selecting expression value
# observe({
# print("server-datafilter-update-expr")
# data_analyzed = analyzeDataReactive()
# if(!(input$datafilter_selectexpr=="")) {
# exprname = input$datafilter_selectexpr
# #calculate miin and max
# tmpdat = data_analyzed$data_long # add filter by group and sample id
# tmpmin = min(tmpdat[,colnames(tmpdat)==exprname],na.rm=T)
# tmpmax = max(tmpdat[,colnames(tmpdat)==exprname],na.rm=T)
#
# updateNumericInput(session,"datafilter_exprmin",
# min=floor(tmpmin),max= ceiling(tmpmax),value=floor(tmpmin))
# updateNumericInput(session,"datafilter_exprmax",
# min=floor(tmpmin),max= ceiling(tmpmax),value=ceiling(tmpmax))
# }
# })
filterDataReactive <- reactive({
print("filterDataReactive")
data_analyzed = analyzeDataReactive()
tmpsampledata = data_analyzed$sampledata
tmpgeneids = data_analyzed$geneids
tmpres = data_analyzed$results
tmpgroups = data_analyzed$group_names
tmpdatlong = data_analyzed$data_long
# tmpdatlong = data_analyzed$data_long
# tmpynames = tmpdatlong%>%select(-unique_id,-sampleid,-group)%>%colnames()
#
# tmptests = unique(as.character(tmpdat$test))
mydata <- data_analyzed$data_results_table
mydata_genes = left_join(mydata,tmpgeneids) # need also to have unique id
groupids = lapply(tmpgroups,function(k) grep(k,colnames(mydata)))
# filter by group
if(!(input$datafilter_groups[1]=="")) {
tmpselected = input$datafilter_groups
tmprem = match(as.character(tmpsampledata$sampleid[which(!(tmpsampledata$group%in%tmpselected))]),colnames(mydata))
tmpkeep = setdiff(1:ncol(mydata),tmprem)
mydata = mydata[,tmpkeep]
}
# filter by sampleid
if(!(input$datafilter_samples[1]=="")) {
tmpselected = input$datafilter_samples
tmpsamplesrem = setdiff(as.character(tmpsampledata$sampleid),tmpselected) # leftover samples
tmprem = match(tmpsamplesrem,colnames(mydata))
tmpkeep = setdiff(1:ncol(mydata),tmprem)
mydata = mydata[,tmpkeep]
}
# filter by geneid or name
if((input$datafilter_genelist)&(length(input$datafilter_gene_select)>0)) {
tmpselected = input$datafilter_gene_select
# find the columns with gene identifiers
tmpmydata_genes = mydata_genes[,match(colnames(tmpgeneids),colnames(mydata_genes),nomatch=0)]
# try to match gene names to each column, then take the union of all the indx
tmpind = unique(na.omit(c(apply(tmpmydata_genes,2,function(k) match(tmpselected,k)))))
mydata = mydata[tmpind,]
mydata_genes = mydata_genes[tmpind,]
}
#add filter by gene name file like in heatmap
if(input$datafilter_signif) {
tmpres_filter = tmpres%>%filter(test==input$datafilter_selecttest)
tmpres_filter = tmpres_filter%>%filter(P.Value<=input$datafilter_pvaluecut,
adj.P.Val<=input$datafilter_qvaluecut)
tmpres_up = tmpres_filter%>%filter(logFC>=input$datafilter_fccut)
tmpres_down = tmpres_filter%>%filter(logFC<=input$datafilter_fccut)
if(input$datafilter_logfc_dir=="up") {
tmpgenes=as.character(tmpres_up$unique_id)
}else if(input$datafilter_logfc_dir=="down"){
tmpgenes=as.character(tmpres_down$unique_id)
}else{
tmpgenes=c(as.character(tmpres_up$unique_id),as.character(tmpres_down$unique_id))
}
tmpind = match(tmpgenes,mydata_genes$unique_id,nomatch=0)
mydata = mydata[tmpind,]
mydata_genes = mydata_genes[tmpind,]
}
if(input$datafilter_expr) {
tmpdatlong_filter = data.table::data.table(tmpdatlong)[unique_id%in%mydata_genes$unique_id,]
data.table::setnames(tmpdatlong_filter,input$datafilter_selectexpr, "mycol")
tmpdatlong_filter = tmpdatlong_filter[,.(min=min(mycol,na.rm=T),max=max(mycol,na.rm=T)),by=unique_id]
tmpdatlong_filter = tmpdatlong_filter[(min>=input$datafilter_exprmin)&(max<=input$datafilter_exprmax),]
tmpgenes = as.character(tmpdatlong_filter$unique_id)
tmpind = match(tmpgenes,mydata_genes$unique_id,nomatch=0)
mydata = mydata[tmpind,]
mydata_genes = mydata_genes[tmpind,]
}
shiny::validate(need(nrow(mydata)>0,message="No features pass this filter."))
mydata
# need to add:
# filter only within some groups, should filter be based on above group selections? no because test is not
# save data as file with filter settings concatinated?
# show number of genes that pass filter like in heatmap
# data frame display too wide, truncate columns?
# output data should have counts too? which is shown, log2cpm? log2cpm_voom?
#download record of filters buttons
#DF display, make prettier?
#data summary?
})
output$filterdataoutput <- renderDataTable({
print("output$filterdataoutput")
res <- filterDataReactive()
res[,sapply(res,is.numeric)] <- signif(res[,sapply(res,is.numeric)],3)
datatable(res, class = 'cell-border stripe', rownames = FALSE)
})
output$nrow_filterdata <- renderText({
res <- filterDataReactive()
tmpnum = ifelse(is.null(res),0,nrow(res))
paste("Chosen filters result in ",tmpnum, " genes.")
})
# if datafilter_fold_change_groups selected
# observe({
# group1 = input$datafilter_fold_change_groups[1]
# group2 = input$datafilter_fold_change_groups[2]
# tmpdatlong%>%filter(group==group1)
# tmpdatlong%>%filter(group==group2)
# updateNumericInput(session,"datafilter_log2fc_cut",min=0,max=max(abs))
# })
output$download_filtered_data_csv <- downloadHandler(filename = paste0("START_results_filtered_",Sys.Date(),".csv"),
content = function(file) {
write_csv(filterDataReactive(), file)})