-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDESeq2.R
executable file
·222 lines (130 loc) · 5 KB
/
DESeq2.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
library(DESeq2)
library(tidyverse)
# Load the txi object
txi <- readRDS("RObjects/txi.rds")
# Load sample info table
sampleinfo <- read_tsv("data/samplesheet_corrected.tsv",
col_types = "cccc")
all(colnames(txi$counts) == sampleinfo$SampleName)
# The simple model
simple.model <- as.formula(~ TimePoint)
# The model matrix
model.matrix(simple.model, data = sampleinfo)
# Exercise 1
simple.model <- as.formula(~ Status)
model.matrix(simple.model, data = sampleinfo)
# Switching "uninfected" to be the intercept
sampleinfo <- mutate(sampleinfo,
Status = fct_relevel(Status, "Uninfected"))
model.matrix(simple.model, data = sampleinfo)
# Building a DESeq2DataSet
simple.model <- as.formula(~ Status)
sampleinfo <- mutate(sampleinfo,
Status = fct_relevel(Status, "Uninfected"))
ddsObj.raw <- DESeqDataSetFromTximport(txi = txi,
colData = sampleinfo,
design = simple.model)
# Filter out the unexpressed
keep <- rowSums(counts(ddsObj.raw)) > 5
ddsObj.filt <- ddsObj.raw[keep, ]
# Differential expression analysis
## Deseq2 workflow
### Estimate Size Factors
ddsObj <- estimateSizeFactors(ddsObj.filt)
normalizationFactors(ddsObj.filt)
logcounts <- log2(counts(ddsObj, normalized = FALSE) + 1)
limma::plotMA(logcounts, array = 5, ylim = c(-5, 5))
abline(h = 0, col = "red")
logNormalizedCounts <- log2(counts(ddsObj,
normalized = TRUE) + 1)
limma::plotMA(logNormalizedCounts,
array = 5,
ylim = c(-5, 5))
abline(h = 0, col = "red")
### Estimate Dispersions
ddsObj <- estimateDispersions(ddsObj)
plotDispEsts(ddsObj)
### Modelling and Wald test
ddsObj <- nbinomWaldTest(ddsObj)
# The DESeq command
ddsObj <- DESeq(ddsObj.filt)
# Generate a table of differential expression results
results.simple <- results(ddsObj, alpha = 0.05)
results.simple
# Exercise 2
## a) Upregulation
sum(results.simple$log2FoldChange > 0 & results.simple$padj < 0.05,
na.rm = TRUE)
## b) Downregulation
sum(results.simple$log2FoldChange < 0 & results.simple$padj < 0.05,
na.rm = TRUE)
# The Additive model
additive.model <- as.formula(~ TimePoint + Status)
# Exercise 3
ddsObj.raw <- DESeqDataSetFromTximport(txi = txi,
colData = sampleinfo,
design = additive.model)
ddsObj.filt <- ddsObj.raw[keep, ]
design(ddsObj.filt) <- additive.model
## 1. Run the DESeq2 workflow
ddsObj <- DESeq(ddsObj.filt)
## 2. extract results
results.additive <- results(ddsObj, alpha = 0.05)
### a) How many coefficients
model.matrix(additive.model, data = sampleinfo)
### d) What contrast does `results.additive`
results.additive
### e) How many genes with padj < 0.05
sum(results.additive$padj < 0.05, na.rm = TRUE)
# How `results` picks default contrast
resultsNames(ddsObj)
# Rename results
results.InfectedvUninfected <- results.additive
rm(results.additive)
# Getting top 100 genes
topGenesIvU <- as.data.frame(results.InfectedvUninfected) %>%
rownames_to_column("GeneID") %>%
top_n(100, wt = -padj)
# Exercise 5
## 1. d33 v d11
results.d33vd11 <- results(ddsObj,
name = "TimePoint_d33_vs_d11",
alpha = 0.05)
## 2. How many sig genes
sum(results.d33vd11$padj < 0.05, na.rm = TRUE)
# Look at exploratory data analysis to determine if we want
# an interaction
vstcounts <- vst(ddsObj.raw, blind = TRUE)
plotPCA(vstcounts, intgroup = c("Status", "TimePoint"))
# Exercise 3
## 1 . Build model
interaction.model <- ~ TimePoint + Status + TimePoint:Status
## 2. Run DESeq2
design(ddsObj.filt) <- interaction.model
ddsObj.interaction <- DESeq(ddsObj.filt)
## 3. Extract results
results.interaction <- results(ddsObj.interaction, alpha = 0.05)
sum(results.interaction$padj < 0.05, na.rm = TRUE)
# Extract a specific contrast from an interaction model
resultsNames(ddsObj.interaction)
results.interaction.d11 <- results(ddsObj.interaction,
name = "Status_Infected_vs_Uninfected",
alpha = 0.05)
results.interaction.d33 <- results(ddsObj.interaction,
contrast = list(c("Status_Infected_vs_Uninfected",
"TimePointd33.StatusInfected")),
alpha = 0.05)
sum(results.interaction.d11$padj < 0.05, na.rm = TRUE)
sum(results.interaction.d33$padj < 0.05, na.rm = TRUE)
# Exercise 7
## 1. d33 v d11 - Infected
results.interaction.Inf <- results(ddsObj.interaction,
contrast = list(c("TimePoint_d33_vs_d11",
"TimePointd33.StatusInfected")),
alpha = 0.05)
sum(results.interaction.Inf$padj < 0.05, na.rm = TRUE)
## 2. d33 v d11 - Uninfected
results.interaction.Uninf <- results(ddsObj.interaction,
name = "TimePoint_d33_vs_d11",
alpha = 0.05)
sum(results.interaction.Uninf$padj < 0.05, na.rm = TRUE)