-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathvenn.R
207 lines (179 loc) · 6.8 KB
/
venn.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
###########################################################################################################
## Proteomics Visualization R Shiny App
##
##This software belongs to Biogen Inc. All right reserved.
##
##@file: venn.R
##@Developer : Benbo Gao (benbo.gao@Biogen.com)
##@Date : 5/16/2018
##@version 1.0
###########################################################################################################
observe({
DataIn = DataReactive()
tmptests = DataIn$tests
ntest <- length(tmptests)
if (ntest >= 5) {
ntest = 5
tmptests = c(tmptests, "Empty List")
}
if (ntest < 5) {
emptylist = 5- ntest
tmptests = c(tmptests, rep("Empty List", emptylist))
}
for (i in 1:5){
venn_test <- paste("venn_test",i,sep="")
updateSelectizeInput(session, venn_test, choices=tmptests, selected=tmptests[i])
}
})
DataVennReactive <- reactive({
DataIn = DataReactive()
results_long = DataIn$results_long
venn_fccut = log2(as.numeric(input$venn_fccut))
venn_pvalcut = as.numeric(input$venn_pvalcut)
if (input$venn_psel == "Padj") {
results_long <- results_long %>% dplyr::filter(abs(logFC) > venn_fccut & Adj.P.Value < venn_pvalcut)
} else{
results_long <- results_long %>% dplyr::filter(abs(logFC) > venn_fccut & P.Value < venn_pvalcut)
}
if (input$venn_updown == "Up") {
results_long <- results_long %>% dplyr::filter(logFC > 0)
}
if (input$venn_updown == "Down") {
results_long <- results_long %>% dplyr::filter(logFC < 0)
}
vennlist <- list()
fill <- list()
if (input$venn_test1 != "Empty List") {
list1 = results_long %>%
filter(test == input$venn_test1) %>%
dplyr::select(UniqueID) %>% collect %>% .[["UniqueID"]] %>% as.character()
fill[[input$venn_test1]] <- input$col1
vennlist[[input$venn_test1]] <- list1
}
if (input$venn_test2 != "Empty List") {
list2 = results_long %>%
filter(test == input$venn_test2) %>%
dplyr::select(UniqueID) %>% collect %>% .[["UniqueID"]] %>% as.character()
fill[[input$venn_test2]] <-input$col2
vennlist[[input$venn_test2]] <- list2
}
if (input$venn_test3 != "Empty List") {
list3 = results_long %>%
filter(test == input$venn_test3) %>%
dplyr::select(UniqueID) %>% collect %>% .[["UniqueID"]] %>% as.character()
fill[[input$venn_test3]] <-input$col3
vennlist[[input$venn_test3]] <- list3
}
if (input$venn_test4 != "Empty List") {
list4 = results_long %>%
filter(test == input$venn_test4) %>%
dplyr::select(UniqueID) %>% collect %>% .[["UniqueID"]] %>% as.character()
fill[[input$venn_test4]] <-input$col4
vennlist[[input$venn_test4]] <- list4
}
if (input$venn_test5 != "Empty List") {
list5 = results_long %>%
filter(test == input$venn_test5) %>%
dplyr::select(UniqueID) %>% collect %>% .[["UniqueID"]] %>% as.character()
fill[[input$venn_test5]] <-input$col5
vennlist[[input$venn_test5]] <- list5
}
return(venndata = list("vennlist"=vennlist, "fillcor"=fill) )
})
vennDiagram_out <- reactive({
print("drawing Venn diagram")
venndata <- DataVennReactive()
vennlist <- venndata$vennlist
fillcor <- unlist(venndata$fillcor)
SetNum = length(vennlist)
futile.logger::flog.threshold(futile.logger::ERROR, name = "VennDiagramLogger")
venn.plot <- venn.diagram(x = vennlist,
fill=fillcor, margin=input$margin,
lty=input$lty, lwd=input$lwd, alpha=input$alpha,
cex=input$cex, cat.cex=input$catcex,
fontface = input$fontface, cat.fontface=input$catfontface,
main = input$title, main.cex = input$maincex, main.pos = c(0.5, 1.1), main.fontface = "bold",
filename = NULL)
return(venn.plot)
})
output$vennDiagram <- renderPlot({
grid.draw(vennDiagram_out())
})
#show all DEGs from selected comparisons
output$venn_DEG_Data <- DT::renderDataTable({
venndata <- DataVennReactive()
vennlist <- venndata$vennlist
allIDs=unique(unlist(vennlist))
dataIn=DataReactive()
data_results=dataIn$data_results
all_names=names(data_results)
tests=names(vennlist)
selCol=NULL
for (i in 1:length(tests)) {
sel_i=which(str_detect(all_names, regex(str_c("^", tests[i]), ignore_case=T)))
if (length(sel_i)>0) {selCol=c(selCol, sel_i)}
}
name_col=which(all_names %in% c("UniqueID", "Gene.Name") )
sel_row=which(data_results$UniqueID %in% allIDs)
#browser()#debug
DEG_outdata=data_results[sel_row, c(name_col, selCol)]
DEG_outdata[,sapply(DEG_outdata,is.numeric)] <- signif(DEG_outdata[,sapply(DEG_outdata,is.numeric)],3)
DT::datatable(DEG_outdata,extensions = 'Buttons', options = list(
dom = 'lBfrtip', buttons = c('csv', 'excel', 'print'), pageLength = 20), rownames= FALSE)
})
observeEvent(input$vennDiagram, {
saved.num <- length(saved_plots$vennDiagram) + 1
saved_plots$vennDiagram[[saved.num]] <- vennDiagram_out()
})
observeEvent(input$venn_DEG_data, {
venndata <- DataVennReactive()
vennlist <- venndata$vennlist
allIDs=unique(unlist(vennlist))
dataIn=DataReactive()
data_results=dataIn$data_results
all_names=names(data_results)
tests=names(vennlist)
selCol=NULL
for (i in 1:length(tests)) {
sel_i=which(str_detect(all_names, regex(str_c("^", tests[i]), ignore_case=T)))
if (length(sel_i)>0) {selCol=c(selCol, sel_i)}
}
name_col=which(all_names %in% c("UniqueID", "Gene.Name") )
sel_row=which(data_results$UniqueID %in% allIDs)
#browser()#debug
DEG_outdata=data_results[sel_row, c(name_col, selCol)]
saved_table$DEG_outdata_Venn <- DEG_outdata
})
output$SvennDiagram <- renderPlot({
print("drawing Venn diagram 2")
venndata <- DataVennReactive()
vennlist <- venndata$vennlist
venn(vennlist, show.plot = TRUE, intersections = FALSE)
})
output$vennHTML <- renderText({
DataIn = DataReactive()
ProteinGeneName = DataIn$ProteinGeneName
venndata <- DataVennReactive()
vennlist <- venndata$vennlist
v.table <- venn(vennlist, show.plot = FALSE, intersections = TRUE)
intersect <- attr(v.table,"intersections")
venndata <- DataVennReactive()
vennlist <- venndata$vennlist
v.table <- venn(vennlist,show.plot = FALSE, intersections = TRUE)
intersect <- attr(v.table,"intersections")
htmlstr <- " <br>"
#browser() #debug
for (i in 1:length(intersect)) {
if(input$vennlistname == "Gene.Name"){
genes<-ProteinGeneName%>%dplyr::filter(UniqueID %in% intersect[[i]])%>%dplyr::select(Gene.Name)%>%unlist()
intersectlist <- toString(genes)
#intersectlist <- toString(sapply(strsplit(intersect[[i]],split= "\\_"),'[',1))
} else if (input$vennlistname == "AC") {
intersectlist <- toString(sapply(strsplit(intersect[[i]],split= "\\_"),'[',2))
} else if (input$vennlistname == "UniqueID") {
intersectlist <- toString(intersect[[i]])
}
htmlstr <- paste(htmlstr,"<p><b><font color=red>", names(intersect[i]),"</font></b>:",intersectlist, sep="")
}
htmlstr
})