-
Notifications
You must be signed in to change notification settings - Fork 0
/
RNASeq_fibroblasts_05_DESeq2.R
210 lines (186 loc) · 8.51 KB
/
RNASeq_fibroblasts_05_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
library("DESeq2")
library("vsn")
library("dplyr")
library("ggplot2")
library("pheatmap")
library("BiocParallel")
register(SnowParam(8))
library("tximport")
library("readr")
library(openxlsx)
library("rjson")
library(doParallel)
cl <- makeCluster(detectCores(), type='PSOCK')
registerDoParallel(cl)
setwd("D:/Work/! projects/MTM/fibroblasts/RNAseq/09 - DESeq2")
sessionInfo = paste("session-info ",Sys.time(),".txt", sep="")
sessionInfo = gsub(":", "-", sessionInfo)
sink(sessionInfo)
sessionInfo()
sink()
lnames = load(file = "txi.RData")
lnames
names(samples)
# subset for columns of interest
samples1 = samples[ , c("fibro_RNAseqID","Site","Sex","Group","HD","mean_CAG_small", "mean_CAG_large","MTM_DBS","MTM_DBS_zero" ,"MTM_weight", "MTM_height", "MTM_BMI" , "MTM_age_months", "MTM_tfcscore", "MTM_motscore", "MTM_fiscore", "MTM_indepscl", "rel_mtDNA")]
head(samples1)
# factoring for continuous variables
cutBMI = cut(samples1$MTM_BMI, 5)
cutAGE = cut(samples1$MTM_age_months, 5)
factoring = cbind(as.character(cutBMI), as.character(cutAGE))
rownames(factoring) = rownames(samples1)
colnames(factoring) = c("MTM_BMI", "MTM_age_months")
write.csv(factoring, file="factoring_for_continuous_variables.csv")
cutBMI = cut(samples1$MTM_BMI, 5, c(paste("BMI",1:5,sep="")))
cutAGE = cut(samples1$MTM_age_months, 5, c(paste("AGE",1:5,sep="")))
samples2 = cbind(cutBMI, cutAGE, samples1)
head(samples2)
# design matrix with new factors
design = ~ Site + Sex + cutAGE + cutBMI + Group
design
ddsTxi <- DESeqDataSetFromTximport(txi, colData = samples2, design = design)
ddsTxi
#outlier removal
outliers = list()
outliers = c("813896", "813913", "813890", "813910", "813912", "813877", "813887", "813892", "813919", "813903")
dds <- ddsTxi[,!colnames(ddsTxi) %in% outliers]
dds
eval.dds <- DESeq(dds, parallel=TRUE)
eval.dds
# coefficients:
resultsNames(eval.dds)
# get normalization and size factors
# normalization factor for each gene
normFactors = normalizationFactors(eval.dds)
# size factor for library used for the gene norm factors
nm <- assays(eval.dds)[["avgTxLength"]]
sf <- estimateSizeFactorsForMatrix(counts(eval.dds) / nm)
sf1 = data.frame(sf)
sf2 = cbind(rownames(sf1),sf1)
colnames(sf2) = c("sample.ID","size.factors")
write.csv(sf2, file="size_factors_used_to_correct_libraries.csv", row.names = F)
# specify contrast here, c('factorName','numeratorLevel','denominatorLevel')
# need to do for every pair-wise comparison
traitcolumn = "Group"
trait1 = "pre"
trait2 = "control"
contrast = vector()
contrast[1]=paste(traitcolumn)
contrast[2]=paste(trait1)
contrast[3]=paste(trait2)
contrast
# run with shrunken LFCs to be able to cross-compare
res <- results(eval.dds, contrast=contrast, parallel = TRUE)
res
resShrunk <- lfcShrink(eval.dds, contrast=contrast, parallel = TRUE)
resShrunk
resShrunk2 <- lfcShrink(eval.dds, contrast = contrast, type="ashr", parallel = TRUE)
resShrunk2
#resShrunk2 <- lfcShrink(eval.dds, coef = 2, type="apeglm", parallel = TRUE)
resDat <- cbind(rownames(res), res)
colnames(resDat)[1] = "geneid"
write.table(resDat, file = paste(trait1,"_vs_", trait2,"_DESeq2_results.csv", sep=""), sep=",", row.names=F)
resDat2 <- cbind(rownames(resShrunk), resShrunk)
colnames(resDat2)[1] = "geneid"
write.table(resDat2, file = paste(trait1,"_vs_", trait2,"_DESeq2_results_shrunken_LFCs_normal.csv", sep=""), sep=",", row.names=F)
resDat3 <- cbind(rownames(resShrunk2), resShrunk2)
colnames(resDat3)[1] = "geneid"
write.table(resDat3, file = paste(trait1,"_vs_", trait2,"_DESeq2_results_shrunken_LFCs_ashr.csv", sep=""), sep=",", row.names=F)
# LFCs
pdf(file = paste(trait1,"_vs_", trait2,"_DEseq_results_LFCs.pdf", sep=""), w=30, h=10);
maxRes = max(res$log2FoldChange, na.rm=T) + 0.1
maxResShrunk = max(resShrunk$log2FoldChange, na.rm=T) + 0.1
maxResShrunk2 = max(resShrunk2$log2FoldChange, na.rm=T) + 0.1
minRes = min(res$log2FoldChange, na.rm=T) - 0.1
minResShrunk = min(resShrunk$log2FoldChange, na.rm=T) - 0.1
minResShrunk2 = min(resShrunk2$log2FoldChange, na.rm=T) - 0.1
par(mfrow=c(1,3))
plotMA(res, main="DESeq2 – unshrunken LFCs", ylim=c(minRes, maxRes))
plotMA(resShrunk, main="DESeq2 – shrunken LFCs (normal)", ylim=c(minResShrunk, maxResShrunk))
plotMA(resShrunk2, main="DESeq2 – shrunken LFCs (ashr)", ylim=c(minResShrunk2, maxResShrunk2))
dev.off()
# ------------------------------------------
# ------------------------------------------
# specify contrast here, c('factorName','numeratorLevel','denominatorLevel')
# need to do for every pair-wise comparison
traitcolumn = "Group"
trait1 = "early"
trait2 = "control"
contrast = vector()
contrast[1]=paste(traitcolumn)
contrast[2]=paste(trait1)
contrast[3]=paste(trait2)
contrast
# run with shrunken LFCs to be able to cross-compare
res <- results(eval.dds, contrast=contrast, parallel = TRUE)
res
resShrunk <- lfcShrink(eval.dds, contrast=contrast, parallel = TRUE)
resShrunk
resShrunk2 <- lfcShrink(eval.dds, contrast = contrast, type="ashr", parallel = TRUE)
resShrunk2
#resShrunk2 <- lfcShrink(eval.dds, coef = 2, type="apeglm", parallel = TRUE)
resDat <- cbind(rownames(res), res)
colnames(resDat)[1] = "geneid"
write.table(resDat, file = paste(trait1,"_vs_", trait2,"_DESeq2_results.csv", sep=""), sep=",", row.names=F)
resDat2 <- cbind(rownames(resShrunk), resShrunk)
colnames(resDat2)[1] = "geneid"
write.table(resDat2, file = paste(trait1,"_vs_", trait2,"_DESeq2_results_shrunken_LFCs_normal.csv", sep=""), sep=",", row.names=F)
resDat3 <- cbind(rownames(resShrunk2), resShrunk2)
colnames(resDat3)[1] = "geneid"
write.table(resDat3, file = paste(trait1,"_vs_", trait2,"_DESeq2_results_shrunken_LFCs_ashr.csv", sep=""), sep=",", row.names=F)
# LFCs
pdf(file = paste(trait1,"_vs_", trait2,"_DEseq_results_LFCs.pdf", sep=""), w=30, h=10);
maxRes = max(res$log2FoldChange, na.rm=T) + 0.1
maxResShrunk = max(resShrunk$log2FoldChange, na.rm=T) + 0.1
maxResShrunk2 = max(resShrunk2$log2FoldChange, na.rm=T) + 0.1
minRes = min(res$log2FoldChange, na.rm=T) - 0.1
minResShrunk = min(resShrunk$log2FoldChange, na.rm=T) - 0.1
minResShrunk2 = min(resShrunk2$log2FoldChange, na.rm=T) - 0.1
par(mfrow=c(1,3))
plotMA(res, main="DESeq2 – unshrunken LFCs", ylim=c(minRes, maxRes))
plotMA(resShrunk, main="DESeq2 – shrunken LFCs (normal)", ylim=c(minResShrunk, maxResShrunk))
plotMA(resShrunk2, main="DESeq2 – shrunken LFCs (ashr)", ylim=c(minResShrunk2, maxResShrunk2))
dev.off()
# ------------------------------------------
# ------------------------------------------
# specify contrast here, c('factorName','numeratorLevel','denominatorLevel')
# need to do for every pair-wise comparison
traitcolumn = "Group"
trait1 = "early"
trait2 = "pre"
contrast = vector()
contrast[1]=paste(traitcolumn)
contrast[2]=paste(trait1)
contrast[3]=paste(trait2)
contrast
# run with shrunken LFCs to be able to cross-compare
res <- results(eval.dds, contrast=contrast, parallel = TRUE)
res
resShrunk <- lfcShrink(eval.dds, contrast=contrast, parallel = TRUE)
resShrunk
resShrunk2 <- lfcShrink(eval.dds, contrast = contrast, type="ashr", parallel = TRUE)
resShrunk2
#resShrunk2 <- lfcShrink(eval.dds, coef = 2, type="apeglm", parallel = TRUE)
resDat <- cbind(rownames(res), res)
colnames(resDat)[1] = "geneid"
write.table(resDat, file = paste(trait1,"_vs_", trait2,"_DESeq2_results.csv", sep=""), sep=",", row.names=F)
resDat2 <- cbind(rownames(resShrunk), resShrunk)
colnames(resDat2)[1] = "geneid"
write.table(resDat2, file = paste(trait1,"_vs_", trait2,"_DESeq2_results_shrunken_LFCs_normal.csv", sep=""), sep=",", row.names=F)
resDat3 <- cbind(rownames(resShrunk2), resShrunk2)
colnames(resDat3)[1] = "geneid"
write.table(resDat3, file = paste(trait1,"_vs_", trait2,"_DESeq2_results_shrunken_LFCs_ashr.csv", sep=""), sep=",", row.names=F)
# LFCs
pdf(file = paste(trait1,"_vs_", trait2,"_DEseq_results_LFCs.pdf", sep=""), w=30, h=10);
maxRes = max(res$log2FoldChange, na.rm=T) + 0.1
maxResShrunk = max(resShrunk$log2FoldChange, na.rm=T) + 0.1
maxResShrunk2 = max(resShrunk2$log2FoldChange, na.rm=T) + 0.1
minRes = min(res$log2FoldChange, na.rm=T) - 0.1
minResShrunk = min(resShrunk$log2FoldChange, na.rm=T) - 0.1
minResShrunk2 = min(resShrunk2$log2FoldChange, na.rm=T) - 0.1
par(mfrow=c(1,3))
plotMA(res, main="DESeq2 – unshrunken LFCs", ylim=c(minRes, maxRes))
plotMA(resShrunk, main="DESeq2 – shrunken LFCs (normal)", ylim=c(minResShrunk, maxResShrunk))
plotMA(resShrunk2, main="DESeq2 – shrunken LFCs (ashr)", ylim=c(minResShrunk2, maxResShrunk2))
dev.off()
# ------------------------------------------