-
Notifications
You must be signed in to change notification settings - Fork 7
/
server-svaseq.R
185 lines (125 loc) · 4.94 KB
/
server-svaseq.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
observe({
svaReactive()
})
svaReactive <- eventReactive(input$runSVA, {
validate(need(as.formula(input$designFormulaSva) != as.formula("~1"), "Need biological factors to estimate SVs!"))
isolate({
dds = ddsInitReactive()
})
withProgress(message = "Running SVA , please wait",{
js$addStatusIcon("svaseqTab","loading")
removeNotification("errorNotify")
removeNotification("errorNotify1")
validate(need(
tryCatch({
dds <- estimateSizeFactors(dds)
norm.cts <- counts(dds, normalized=TRUE)
### SVA
isolate({
mm <- model.matrix(as.formula(input$designFormulaSva), colData(dds))
mm0 <- model.matrix(~ 1, colData(dds))
norm.cts <- norm.cts[rowSums(norm.cts) > 0,]
svafit <- svaseq(norm.cts, mod=mm, mod0=mm0, n.sv=input$numSVA)
svNames = paste0("SV", 1:ncol(svafit$sv))
for(i in 1:length(svNames))
colData(dds)[,svNames[i]] <- svafit$sv[,i]
varNames = colnames(colData(dds))
varNames = varNames[varNames != "sizeFactor"]
svaFormula = paste("~", paste( rev(varNames), collapse="+"))
myValues$ddsSva = dds
updateTextInput(session, "newFormulaSva", value = svaFormula)
updateSelectInput(session, "xaxisSva", choices = colnames(colData(dds)), selected = "SV1")
updateSelectInput(session, "yaxisSva", choices = colnames(colData(dds)), selected = "SV2")
updateSelectInput(session, "colorBy", choices = colnames(colData(dds)), selected = colnames(colData(dds))[1])
updateSelectizeInput(session, "varsToRegress", choices = colnames(colData(dds)), selected = c("SV1","SV2"))
updateSelectizeInput(session, "factorNameInputSva", choices = colnames(colData(dds)), selected = colnames(colData(dds))[1])
js$addStatusIcon("svaseqTab","done")
return(list('svafit'=svafit,'ddsSva'=dds))
})
},
error = function(e) {
myValues$status = paste("SVA Error: ",e$message)
showNotification(id="errorNotify", myValues$status, type = "error", duration = NULL)
#showNotification(id="errorNotify1", "If this is intended, please select 'No Replicates' in Input Data step. OR use ~ 1 as the design formula", type = "error", duration = NULL)
js$addStatusIcon("svaseqTab","fail")
return(NULL)
})
,
"Error"
))
})
})
output$svaText = renderText({
validate(need(as.formula(input$designFormulaSva) != as.formula("~1"), "Cannot use ~ 1 to estimate SVs. Biological factors are required!"))
return(paste("Using biological factors:", input$designFormulaSva,"to estimate Surrogate Variables (SVs)"))
})
output$svaPlot <- renderPlotly({
dds = svaReactive()$ddsSva
if(!is.null(dds))
{
df = as.data.frame(colData(dds))
xaxis = input$xaxisSva
yaxis = input$yaxisSva
colorBy = input$colorBy
if(xaxis == "" && yaxis == "" && colorBy == "")
{
xaxis = colnames(df)[1]
yaxis = colnames(df)[1]
colorBy = colnames(df)[1]
}
ggplot(df, aes_string(xaxis,yaxis, col = colorBy)) +
geom_point() +
geom_text(aes(label = rownames(df)),hjust=0, vjust=0)
}
})
observeEvent(input$regressVarsBatch,ignoreInit = TRUE,{
withProgress(message = "Removing batch effect, this may take a long time.",{
dds = myValues$ddsSva
svaFormula = as.formula(input$newFormulaSva)
design(dds) <- svaFormula
BiocParallel::register(MulticoreParam(3))
shiny::setProgress(value = 0.3, detail = "Running DESeq ...")
dds <- DESeq(dds, parallel = T)
shiny::setProgress(value = 0.6, detail = "Computing VST matrix ...")
vsd <- varianceStabilizingTransformation(dds)
shiny::setProgress(value = 0.8, detail = "limma::removeBatchEffect ...")
assay(vsd) <- limma::removeBatchEffect(assay(vsd), covariates = colData(dds)[,input$varsToRegress])
myValues$vsdSva = vsd
myValues$ddsAddSV = dds
})
})
output$pcaSvaPlot = renderPlotly({
validate(need(length(input$factorNameInputSva) > 0 ,"Need at least one condition!"))
vsd = myValues$vsdSva
if(!is.null(vsd))
{
DESeq2::plotPCA(vsd, intgroup = input$factorNameInputSva)
}
})
output$pcaSvaAvailable <- reactive({
return(!is.null(myValues$vsdSva))
})
outputOptions(output, 'pcaSvaAvailable', suspendWhenHidden=FALSE)
output$ddsSvaAvailable <- reactive({
return(!is.null(svaReactive()$ddsSva))
})
outputOptions(output, 'ddsSvaAvailable', suspendWhenHidden=FALSE)
output$varsToIncludeInDeseq = renderText({
return("")
})
observe({
if(input$runDeseqWithSVs > 0 )
{
#
#myValues$DF = colData(myValues$ddsAddSV)
myValues$dds = myValues$ddsAddSV
GotoTab("deseqTab")
}
})
observe({
if(input$runDeseqWithoutSVs > 0 )
{
myValues$dds = ddsInitReactive()
GotoTab("deseqTab")
}
})