forked from szenitha/Shiny-Seq
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathANOVA_module.R
More file actions
275 lines (246 loc) · 8.73 KB
/
Copy pathANOVA_module.R
File metadata and controls
275 lines (246 loc) · 8.73 KB
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
source("gene_count_module.R")
ANOVA_module_UI<-function(id)
{
ns<-NS(id)
tagList(
br(),
radioButtons(ns("lfc"), "Show log2 foldchange?:", choices = c("Yes", "No"),selected = "No"),
br(),
br(),
fluidRow(
column(1,
selectInput(ns("datachoice6") ,label = h5("Select Data Type"),
choices = list("Excel" = 1, "CSV" = 2),
selected = 1)),
column(1,
br(),
br(),
downloadButton(ns('download_ANOVA_Table'), 'Download ANOVA Table'))),
DT::dataTableOutput(ns("ANOVA")),
gene_count_module_UI(ns("module5"))
)
}
ANOVA_module<-function(input,output,session,
batch_choice,dds,design,combination,
conchoice,DE_genes,normal,batch_corrected)
{
if(!is.null(DE_genes()))
{
print("inside anova module")
#Perform ANOVA
anova<-reactive({
res<-NULL
# Create a Progress object
progress <- shiny::Progress$new()
# Make sure it closes when we exit this reactive, even if there's an error
on.exit(progress$close())
progress$set(message = "Preparing ANOVA table", value = 0)
# Number of times we'll go through the loop
n <- 2
# Increment the progress bar, and update the detail text.
progress$inc(1/n, detail = paste("Doing part", 1,"/",n))
# Pause for 0.1 seconds to simulate a long computation.
Sys.sleep(0.1)
if(as.numeric(batch_choice())==2)
{
res<-DESeq(dds(),test = "LRT",reduced = design())
# Increment the progress bar, and update the detail text.
progress$inc(1/n, detail = paste("Doing part", 2,"/",n))
# Pause for 0.1 seconds to simulate a long computation.
Sys.sleep(0.1)
}
else if(as.numeric(batch_choice())==3)
{
print(design())
res<-DESeq(dds(),test = "LRT",reduced = design())
# Increment the progress bar, and update the detail text.
progress$inc(1/n, detail = paste("Doing part", 2,"/",n))
# Pause for 0.1 seconds to simulate a long computation.
Sys.sleep(0.1)
}
else
{
res<-DESeq(dds(),test = "LRT",reduced = design())#,parallel = TRUE)
# Increment the progress bar, and update the detail text.
progress$inc(1/n, detail = paste("Doing part", 2,"/",n))
# Pause for 0.1 seconds to simulate a long computation.
Sys.sleep(0.1)
}
res
})
#prepare ANOVA table for display(This reactive merges the anova table with DE analysis results)
anova_table<-reactive({
dds_anova<-anova()
res<-results(dds_anova)
#Get base mean of each condition
base_mean<-sapply( levels(dds_anova$condition), function(lvl) rowMeans( counts(dds_anova,normalized=TRUE)[,dds_anova$condition == lvl] ) )
#Merge the basemeans with the anova table
anova<-merge(res,base_mean,by=0,all=TRUE)
for(i in 8:length(colnames(anova)))
{
colnames(anova)[i]<-paste0(colnames(anova)[i],'_mean')
}
print(head(anova))
genes<-anova[,1]
anova<-anova[-1]
rownames(anova)<-genes
colnames(anova)[1]<-"Overall mean"
result<-DE_genes()
combo<-combination()
num <-length(combo())
l<-list()
l[[1]]<-anova[,-3]
for(i in 1:num)
{
print(nrow(as.data.frame(result()[[i]][4])))
res<-as.data.frame(result()[[i]][4])
print(head(res))
######change####
a_tab<-res[,-4]
print(colnames(a_tab))
c<-colnames(a_tab)
print(length(c))
print('howdy')
print(c)
res <- a_tab[,c(c[2],c[3],c[5],c[6],c[1],c[4])]
####################
colnames(res)[5]<-"Overall mean"
for (j in 1:length(colnames(res)))
{
colnames(res)[j]<-paste0(combo()[[i]],' ',colnames(res)[j])
}
l[[length(l)+1]]<-res
}
print(head(l[[1]]))
#Condense results of ANOVA and DE analysis into one data
anova<-transform(Reduce(merge, lapply(l, function(x) data.frame(rn = rownames(x),x))), row.names=rn, rn=NULL)
})
#Display ANOVA table
output$ANOVA <- DT::renderDataTable({
a_tab<-anova_table()[,-c(2,3)]
########change#######
cond<-unique(colData(dds())[,as.numeric(conchoice())])
print(cond)
c<-colnames(a_tab)
print(length(c))
temp<-as.vector(c[4:(3+length(cond))])
temp2<-as.vector(c[(length(cond)+4):length(c)])
print(temp2)
print(c(temp,c[2],c[3],temp2,c[1]))
print('howdy')
print(head(a_tab[,c(temp,c[2],c[3],temp2,c[1])]))
######################
a_tab=a_tab[,c(temp,c[2],c[3],temp2,c[1])]
anova<-NULL
if(input$lfc =="No")
{
names<-colnames(a_tab)
idx<-grep("log2FoldChange",names)
anova=a_tab[,-idx]
}
else anova=a_tab
DT::datatable(anova,class = 'cell-border stripe',
selection = list(mode='single',target = 'row'),
extensions = list('Scroller'=NULL,'Buttons'=NULL),
options = list(deferRender = TRUE,scrollX = TRUE,scrollY = 300,scroller = TRUE,dom = 'Bfrtip',
buttons = list('copy')
)
)
})
#display boxplot when row(gene) in ANOVA table is clicked
observeEvent(input$ANOVA_rows_selected,{
print('hey')
print(input$ANOVA_rows_selected)
selected <- input$ANOVA_rows_selected
print(selected)
full_data<-normal()
print(head(full_data))
#get the gene
print("howdy")
print(head(anova_table()))
print(head(rownames(anova_table())),1)
print(typeof(rownames(anova_table())))
temp<-strsplit(rownames(anova_table())," ")
print(head(temp))
print(temp[[1]])
an_gene<-temp[[selected]]
print("hey")
print(an_gene)
library('data.table')
print(an_gene)
counts<-as.vector(full_data[which(an_gene %in% rownames(full_data)),])
cond<-as.vector(colData(dds())[,as.numeric(conchoice())])
print(length(counts))
print(length(cond))
df<-data.frame(counts,cond)
colnames(df)<-c('count','condition')
print(head(df))
print(batch_choice())
print(as.numeric(batch_choice()))
batch<-batch_choice()
if(as.numeric(batch)==1) callModule(gene_count_module,"module5",NULL,reactive({dds()}),reactive({an_gene}))
else
{
idx<-which(rownames(batch_corrected()) %in% an_gene)
print(idx)
callModule(gene_count_module,"module5",batch_corrected()[idx,],reactive({dds()}),reactive({an_gene}))
}
})
#Download ANOVA table
output$download_ANOVA_Table <- downloadHandler(
filename = function() {
if(as.numeric(input$datachoice6==1)){paste('ANOVA_table.xlsx')}
else if(as.numeric(input$datachoice6==2)){paste('ANOVA_table.csv')}
},
content = function(file) {
if(as.numeric(input$datachoice6==1)){
anova<-anova_table()
a_tab<-anova_table()[,-c(2,3)]
########reordering columns in anova table for display#######
cond<-unique(colData(dds())[,as.numeric(conchoice())])
print(cond)
c<-colnames(a_tab)
print(length(c))
temp<-as.vector(c[4:(3+length(cond))])
temp2<-as.vector(c[(length(cond)+4):length(c)])
print(temp2)
print(c(temp,c[2],c[3],temp2,c[1]))
print('howdy')
# anova <-
print(head(a_tab[,c(temp,c[2],c[3],temp2,c[1])]))
######################
anova=a_tab[,c(temp,c[2],c[3],temp2,c[1])]
M <- as.matrix(anova)
wb <- createWorkbook()
addWorksheet(wb, sheetName = "ANOVA")
writeData(wb = wb, sheet = 1, x = M, colNames = T, rowNames = T)
saveWorkbook(wb, file)
}
else {
anova<-anova_table()
a_tab<-anova_table()[,-c(2,3)]
########reordering columns in anova table for display#######
cond<-unique(colData(dds())[,as.numeric(conchoice())])
print(cond)
c<-colnames(a_tab)
print(length(c))
temp<-as.vector(c[4:(3+length(cond))])
temp2<-as.vector(c[(length(cond)+4):length(c)])
print(temp2)
print(c(temp,c[2],c[3],temp2,c[1]))
print('howdy')
# anova <-
print(head(a_tab[,c(temp,c[2],c[3],temp2,c[1])]))
######################
anova=a_tab[,c(temp,c[2],c[3],temp2,c[1])]
write.csv(anova, file)
}
}
)
}
return(list(
anova_table=reactive({anova_table()}),
dds=reactive({anova()})
)
)
}