-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDESeq.R
153 lines (111 loc) · 4.55 KB
/
DESeq.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
# call differentially expresed genes using "DESeq" r package
values <- reactiveValues()
call_DEG = function(Expression, condition, condition1, condition2){
withProgress(message = 'Calling Differentially Expressed Genes ', value = 0, {
library( "DESeq" )
incProgress(1/4, detail = "loading data")
tmp = EXP()
Expression = tmp$Expression
condition = tmp$condition
condition.vector = as.vector(as.matrix(condition)[1,colnames(Expression)])
Expression.int = sapply(Expression, as.integer)
rownames(Expression.int) = rownames(Expression)
incProgress(2/4, detail = "estimateDispersions")
cds = newCountDataSet( Expression.int, condition.vector )
#cds = estimateSizeFactors( cds )
sizeFactors(cds) = seq(1, 1, length.out = length(condition.vector))
# No replications
if(length(colnames(condition)[condition[1,] == input$Condition1]) == 1 & length(colnames(condition)[condition[1,] == input$Condition2]) == 1 ){
cds = estimateDispersions( cds, method = "blind", sharingMode="fit-only" )
} else {
cds = estimateDispersions( cds, method = "pooled" )
}
incProgress(3/4, detail = "Testing")
res = nbinomTest( cds, condition1, condition2 )
#DESeq::plotMA(res)
#resSig = res[ res$padj < 0.01 & (res$log2FoldChange > 1 | res$log2FoldChange < -1) & (res$baseMeanA > 1 | res$baseMeanB > 1), ]
incProgress(4/4, detail = "Done")
resSig = na.omit(res)
resSig <- resSig[ order(resSig$pval), ]
return(resSig)
})
}
output$conditionSelecter1 <- renderUI({
selectInput("Condition1", "Choose condition1:", unique(t(EXP()$condition)[,1]))
})
output$conditionSelecter2 <- renderUI({
condition = EXP()$condition
selectInput("Condition2", "Choose condition2:", unique(t(condition)[,1]), selected = unique(t(condition)[,1])[2])
})
DEseqButtonAct <- eventReactive(input$DEseqButton, {
tmp = EXP()
Expression = tmp$Expression
condition = tmp$condition
DESeqOut <- call_DEG(Expression, condition, input$Condition1, input$Condition2)
padj = filterDEseqP()
foldChange = filterDEseqF()
baseMean = filterDEseqB()
DEGs <-DESeqOut[DESeqOut$baseMean >= baseMean & (DESeqOut$foldChange >= foldChange | DESeqOut$foldChange <= 1/foldChange) & DESeqOut$padj <= padj,]
rownames(DEGs) = DEGs$id
DEGs <- DEGs[,2:ncol(DEGs)]
values$DEGs = DEGs
DESeq_plot(Expression, condition, DESeqOut, DEGs)
COPY(DEGs)
DT::datatable(DEGs)
})
output$DESeqOutput <- DT::renderDataTable({
DEseqButtonAct()
})
DESeq_plot <- function(Expression, condition, DESeqOut, DEGs){
id = rownames(DEGs)
DEGs = cbind(id, DEGs)
output$DESeq_valcano <- renderPlotly({
if (exists("DESeqOut")){
p = ggplot() + geom_point(data = DESeqOut, aes(x = log2FoldChange, y = -log10(pval), color = log10(baseMean), text = paste0("Gene: ", id)), alpha = 0.3) +
scale_colour_gradient(low="green", high="red") +
geom_point(data = DEGs, aes(x = log2FoldChange, y = -log10(pval), color = log10(baseMean), text = paste0("Gene: ", id)), alpha = 0.8) +
ggtitle("Volcano plot") +
theme_bw(base_size = 30) +
theme(panel.border = element_rect(colour = "black", size = 2))
g = ggplotly(p)
} else {
return(NULL)
}
})
}
## filter
filterDEseqP = reactive({
padj = switch(input$DESeqPadjCutoff,
"0.01" = 0.01,
"0.05" = 0.05,
"0.1" = 0.1,
"none" = 1
)
})
filterDEseqF = reactive({
foldChange = switch(input$DESeqfoldChangeCutoff,
"less than 2/3 or greater than 1.5" = 1.5, # 2/3;1.5 FC
"less than 0.5 or greater than 2" = 2, # 0.5;2 FC
"less than 1/3 or greater than 3" = 3, # 1/3;3
"less than 0.25 or greater than 4" = 4, # 0.25;4
"none" = 1 # all
)
})
filterDEseqB = reactive({
baseMean = switch(input$DESeqbaseMeanCutoff,
"1" = 1,
"10" = 10,
"20" = 20,
"30" = 30,
"none" = 0
)
})
# download data
output$downloadDESeqOutput <- downloadHandler(
filename = function() {
paste(input$file1, 'DESeq.iSeq.csv', sep='')
},
content = function(file) {
write.csv(values$DEGs, file, quote = F)
}
)