-
Notifications
You must be signed in to change notification settings - Fork 0
/
de-analysis.R
319 lines (257 loc) · 8.89 KB
/
de-analysis.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
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#######################
## Intstallation ##
#######################
BiocManager::install("TCGAbiolinks")
BiocManager::install("msigdbr")
BiocManager::install("RNAseqQC")
BiocManager::install("DESeq2")
BiocManager::install("ensembldb")
install.packages("dplyr")
install.packages("ggplot2")
install.packages("purrr")
install.packages("tidyr")
install.packages("tibble")
install.packages("magrittr")
install.packages("stringr")
install.packages("tidyverse")
install.packages("vsn")
###################
## Libraries ##
###################
library(TCGAbiolinks)
library(msigdbr)
library(RNAseqQC)
library(DESeq2)
library(ensembldb)
library(dplyr)
library(ggplot2)
library(purrr)
library(tidyr)
library(tibble)
library(magrittr)
library(stringr)
library(tidyverse)
library(vsn)
################################
## Downloading TCGA-COAD Data ##
################################
# get a list of projects
# gdcprojects <- getGDCprojects()
# Querying All Colorectal Cancer Data (Tumor)
query_tumor <- GDCquery(
project = "TCGA-COAD",
data.category = "Transcriptome Profiling",
data.type = "Gene Expression Quantification",
experimental.strategy = "RNA-Seq",
workflow.type = "STAR - Counts",
access = "open",
sample.type = "Primary Tumor"
)
tumor <- getResults(query_tumor)
# Querying All Colorectal Cancer Data (Normal)
query_normal <- GDCquery(
project = "TCGA-COAD",
data.category = "Transcriptome Profiling",
data.type = "Gene Expression Quantification",
experimental.strategy = "RNA-Seq",
workflow.type = "STAR - Counts",
access = "open",
sample.type = "Solid Tissue Normal"
)
normal <- getResults(query_normal)
# Filtering samples with both tumor and normal tissues
submitter_ids <- inner_join(tumor, normal, by = "cases.submitter_id") %>%
select(cases.submitter_id)
tumor <- tumor %>%
filter(cases.submitter_id %in% submitter_ids$cases.submitter_id)
normal <- normal %>%
filter(cases.submitter_id %in% submitter_ids$cases.submitter_id)
# Merging to a single data.frame
samples <- rbind(tumor, normal)
unique(samples$sample_type)
samples$sample.submitter_id
# Overwrite barcode of query with the filtered sample ids
query_coad <- GDCquery(
project = "TCGA-COAD",
data.category = "Transcriptome Profiling",
data.type = "Gene Expression Quantification",
experimental.strategy = "RNA-Seq",
workflow.type = "STAR - Counts",
access = "open",
sample.type = c("Solid Tissue Normal", "Primary Tumor"),
barcode = as.list(samples$sample.submitter_id)
)
getResults(query_coad)
# Downloading TCGA data
GDCdownload(query_coad)
#####################################
## Downloading RCD signatures data ##
#####################################
# Necroptosis
necroptosis_geneset <- msigdbr(species = "human", category = "C5", subcategory = "GO:BP") %>%
filter(gs_name == "GOBP_NECROPTOTIC_SIGNALING_PATHWAY")
head(necroptosis_geneset)
# Ferroptosis
ferroptosis_geneset <- msigdbr(species = "human", category = "C2", subcategory = "CP:WIKIPATHWAYS") %>%
filter(gs_name == "WP_FERROPTOSIS")
head(ferroptosis_geneset)
# Ferroptosis
pyroptosis_geneset <- msigdbr(species = "human", category = "C2", subcategory = "CP:REACTOME") %>%
filter(gs_name == "REACTOME_PYROPTOSIS")
head(pyroptosis_geneset)
#####################################
## Data wranggling for DE Analysis ##
#####################################
# prepare count data
tcga_coad_data <- GDCprepare(query_coad, summarizedExperiment = TRUE)
head(tcga_coad_data)
#tcga_coad_data <- tcga_coad_data %>%
# filter()
# unstanded, stranded_first, stranded_second, tpm_unstrand, fpkm_unstrand, fpkm_uq_unstrand
coad_matrix <- assay(tcga_coad_data, 'unstranded')
coad_matrix
# make dataframe of sample names, cell Lines, and tissue type(normal or tumor)
rownames(samples) <- samples$cases
View(samples)
samples <- samples %>%
select(case="cases.submitter_id", type="sample_type")
samples$type <- str_replace(samples$type, "Solid Tissue Normal", "normal")
samples$type <- str_replace(samples$type, "Primary Tumor", "tumor")
coad_matrix <- coad_matrix[, rownames(samples)]
# Check if all samples in the counts dataframe are in the samples dataframe
all(colnames(coad_matrix) %in% rownames((samples)))
all(colnames(coad_matrix) == rownames(samples))
a <- str_split_fixed(rownames(coad_matrix), "[.]", 2)
counts_matrix <- coad_matrix
rownames(counts_matrix) <- a[, 1]
counts_matrix
#####################
## Quality Control ##
#####################
dds <- DESeqDataSetFromMatrix(countData = counts_matrix,
colData = samples,
design = ~ type)
# QC plots on raw count matrix
plot_total_counts(dds)
plot_library_complexity(dds)
plot_gene_detection(dds)
# Gene biotypes
plot_biotypes(dds)
# Gene filtering
# filter to keep only rows that have 10 reads total. before: 45194 x 87 after: 53996 x 519
keep <- rowSums(counts(dds)) >= 10
dds <- dds[keep,]
# dds2 <- filter_genes(dds, min_count = 10, min_rep = 41)
# Variance stabilization
vsd <-vst(dds)
sdplot <-meanSdPlot(assay(vsd))
sdplot$gg <- sdplot$gg +
ggtitle(label="Mean SD of transformed read counts") +
ylab("standard deviation")
print(sdplot$gg)
# Chromosomal expression
map(c("1", "5", "14"), ~plot_chromosome(vsd, .x))
# Replicate variability
# define new grouping variable
colData(vsd)$trt_mut <- paste0(colData(vsd)$type, "_", colData(vsd)$case)
ma_plots <- plot_sample_MAs(vsd, group = "trt_mut")
cowplot::plot_grid(plotlist = ma_plots[17:24], ncol = 2)
# Clustering
# set seed to control random annotation colors
set.seed(1)
plot_sample_clustering(vsd, anno_vars = c("type"), distance = "euclidean")
# Principal component analysis (PCA) plot (from Tutorial)
plot_pca(vsd, PC_x = 1, PC_y = 2, color_by = "type")
# PCA plot (from Ms Jenny)
pcaData<-plotPCA(vsd, intgroup=c("type"), returnData=TRUE)
percentVar <- round(100 * attr(pcaData, "percentVar"))
ggplot(pcaData, aes(PC1, PC2, color=type, shape=type)) +
geom_point(size=3) +
xlab(paste0("PC1: ",percentVar[1],"% variance")) +
ylab(paste0("PC2: ",percentVar[2],"% variance")) +
coord_fixed()
#to further check outliers
boxplot(log10(assays(dds$type=="normal")[["cooks"]]), range=0, las=2)
##################################################
## Differential Expression Analysis (All Genes) ##
##################################################
library(EnsDb.Hsapiens.v79)
# Plot a gene
dds <- estimateSizeFactors(dds)
# dds1 <- dds
# geneIDs <- ensembldb::select(EnsDb.Hsapiens.v79, keys= rownames(dds), keytype = "GENEID", columns = c("SYMBOL", "GENEID"))
# geneIDs
# rownames(dds1)
# rownames(dds1) <- geneIDs$SYMBOL[rownames(dds1) == geneIDs$GENEID]
# "TLR3" %in% rownames(dds1)
# dds
# plot_gene("RIPK3", dds1, x_var = "case", color_by = "type")
# Differential expression testing
dds$type <- factor(dds$type, levels = c("normal","tumor"))
dds <- DESeq(dds)
resultsNames(dds)
res <- results(dds)
res
write.csv(res, file = "DE_Results.csv")
plotDispEsts(dds)
# Plot a testing result
de_res <- lfcShrink(dds, coef="type_tumor_vs_normal", lfcThreshold = log2(1.5), type = "normal", parallel = TRUE)
# https://rstudio-pubs-static.s3.amazonaws.com/329027_593046fb6d7a427da6b2c538caf601e1.html
res <- results(dds, contrast=c('type', 'tumor', 'normal'))
res <- res[order(res$padj),]
library(knitr)
kable(res[1:5,-(3:4)])
res <- results(dds, contrast=c("type","normal","tumor"))
ix = which.min(res$padj)
res <- res[order(res$padj),]
kable(res[1:5,-(3:4)])
barplot(assay(dds)[ix,],las=2, main=rownames(dds)[ ix ] )
#
dds <- DESeq(dds)
res <- results(dds)
res
summary(res)
res0.05 <- results(dds, alpha = 0.05)
summary(res0.05)
resultsNames(dds)
# Visualization MA plot
plotMA(res)
############################################
# Filtering Necroptosis-related regulators #
############################################
a <- str_split_fixed(rownames(coad_matrix), "[.]", 2)
counts_matrix <- coad_matrix
rownames(counts_matrix) <- a[, 1]
counts_matrix
coad_necroptosis <- counts_matrix[rownames(counts_matrix) %in% necroptosis_geneset$ensembl_gene, ]
coad_necroptosis <- coad_necroptosis[, rownames(samples)]
# Check if all samples in the counts dataframe are in the samples dataframe
all(colnames(coad_necroptosis) %in% rownames((samples)))
all(colnames(coad_necroptosis) == rownames(samples))
###############################
## DE Analysis (Necroptosis) ##
###############################
#
dds <- DESeqDataSetFromMatrix(countData = coad_necroptosis,
colData = samples,
design = ~ type)
dds
keep <- rowSums(counts(dds)) >= 10
dds <- dds[keep,]
dds
#
dds$type <- relevel(dds$type, ref = "normal")
#
dds <- DESeq(dds)
res <- results(dds)
res
summary(res)
res0.05 <- results(dds, alpha = 0.05)
summary(res0.05)
resultsNames(dds)
# Code below allows for comparing across more than 1 levels
# results(dds, contrast = c("dexamethasone", "treated_4hrs", "untreated"))
res_multiomics <- results(dds, lfcThreshold= 2, alpha=0.05)
summary(res_multiomics)
# Visualization MA plot
plotMA(res)