-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path2016-05-10_DESeq2_4.R
275 lines (236 loc) · 11.2 KB
/
2016-05-10_DESeq2_4.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
## Load required libraries
library("BiocParallel")
library("DESeq2")
library("RColorBrewer")
library("calibrate")
library("genefilter")
library("ggplot2")
library("gplots")
library("pheatmap")
library("Rtsne")
## SETUP ## -------------------
register(MulticoreParam(4))
dir <- setwd("/Volumes/IBD/Yudanin/RNAseq/2-2B-1 RNAseq/2-2B-1 DESeq2")
date <- paste0(Sys.Date())
expNum <- paste0("2-2B-1")
# Import data from featureCounts
countdata <- read.table("2-2B-1 Raw Counts.txt", header=TRUE, row.names=1)
# Convert to matrix
countdata <- as.matrix(countdata)
head(countdata)
# Assign condition
names <- colnames(countdata)
getdonor <- function (colname){
strsplit(colname,"_")[[1]][2]
}
donor <- unlist(lapply(names, getdonor))
gettissue <- function (colname){
strsplit(colname,"_")[[1]][1]
}
tissue <- unlist(lapply(names, gettissue))
getsubset <- function (colname){
strsplit(colname,"_")[[1]][3]
}
subset <- unlist(lapply(names, getsubset))
rm(names)
# Analysis with DESeq2 ----------------------------------------------------
# Create a coldata frame and instantiate the DESeqDataSet. See ?DESeqDataSetFromMatrix
(coldata <- data.frame(row.names=colnames(countdata), tissue, donor, subset))
dds <- DESeqDataSetFromMatrix(countData=countdata, colData=coldata, design=~tissue+donor+subset)
colorder <- c(grep("NK", colnames(dds)), grep("ILC1", colnames(dds)), grep("ILC2", colnames(dds)), grep("ILC3", colnames(dds)))
## Run DESeq normalization
dds<-DESeq(dds)
write.csv(counts(dds, normalized=TRUE)[,colorder],file= paste0(date," ",expNum," ", "Normalized Counts.csv"))
save.image(paste0(dir,"/", date," ",expNum, " DESeq2.RData"))
## Variance transformation for clustering/heatmaps, etc
vsd <- varianceStabilizingTransformation(dds)
save.image(paste0(dir,"/", date," ",expNum, " DESeq2.RData"))
## PCA ----------------------------------------------------
pca <- plotPCA(vsd, intgroup=c("tissue","donor", "subset"), returnData=TRUE)
percentVar <- round(100 * attr(pca, "percentVar"))
subsetfill <- c( ILC1="#C7302A", ILC2="#4266F6", ILC3="#269040", NK="#E7A626" )[pca$subset]
donorshapes <- c("178"=21, "201"=22, "206"=23, "217"=24, "223"=25) [pca$donor]
tissuecolor <- c(Jejunum="#212121", Colon="#000000", Lung="#717171") [pca$tissue]
cairo_pdf(paste0(date," ",expNum," Tissue, Subset & Donor PCA.pdf"), w=6, h=4)
ggplot(pca, aes(PC1, PC2, shape=donor, color=tissue)) +
geom_point(size=4, stroke = 0.5, aes(shape=donor, color=tissue, fill=subset)) +
scale_shape_manual(values = donorshapes) +
scale_color_manual(values = tissuecolor) +
scale_fill_manual(values = subsetfill) +
theme(legend.title = element_blank()) +
xlab(paste0("PC1: ",percentVar[1],"% variance")) +
ylab(paste0("PC2: ",percentVar[2],"% variance")) +
ggtitle(paste0(expNum," Tissue, Donor & Subset PCA.pdf"))
dev.off()
## Heatmap Functions and colors ----------------------------------------------------
normheatmap <- function(mtx, cluster_cols=TRUE, title=title, cex=1, h=1, w=1, ...){
pheatmap(mtx,
cex = cex,
cluster_rows=TRUE,
scale="row",
breaks = c(seq(-1.5, 1.5, length.out = 256)),
border_color = NA,
drop_levels=TRUE,
color = my_palette,
show_rownames=TRUE,
cluster_cols=cluster_cols,
annotation_col=coldata[c(1,3)],
annotation_colors = ann_colors,
annotation_legend=FALSE,
main= paste0(expNum, " ",title),
legend=FALSE,
fontsize= 10,
treeheight_col = 20,
treeheight_row = 20,
height=h*6,
width=w*6,
filename= paste0(date," ",expNum," ",title,".pdf"))
}
resheatmap <- function(vsd, genes, samples, cluster_cols=TRUE, title=title, cex, h=1, w=1, ...){
filtered <- assay(vsd) [genes, samples]
filtered <- filtered[rowVars(filtered)>0,]
with(vsd,
pheatmap(filtered[order( rowVars( assay(vsd)[genes,]), decreasing=TRUE),],
cex = cex,
cluster_rows=TRUE,
scale="row",
breaks = c(seq(-2, 2, length.out = 256)),
border_color = NA,
drop_levels=TRUE,
color = my_palette,
show_rownames=TRUE,
cluster_cols=cluster_cols,
annotation_col=coldata[c(1,3)],
annotation_colors = ann_colors,
annotation_legend=FALSE,
main= paste0(expNum, " ",title),
legend=FALSE,
fontsize= 10,
treeheight_col = 10,
treeheight_row = 20,
height=15*h,
width=6*w,
filename= paste0(date," ",expNum," ",title,".pdf"))
)
}
my_palette <- colorRampPalette(brewer.pal(11, "RdBu")) (255)
my_palette <- rev(my_palette)
ann_colors = list(
subset = c( ILC1="#C7302A", ILC2="#4266F6", ILC3="#269040", NK="#E7A626" )[colData(vsd)$subset],
tissue = c( Spleen="black", Lung="light gray", Jejunum="#707070" )[colData(vsd)$tissue])
## Sample distance heatmap ---------------------------------------------------------
sampleDists <- as.matrix(dist(t(assay(vsd))))
normheatmap(sampleDists,
cex=0.5,
title= "Sample Distance Heatmap")
## tSNE ----------------------------------------------------
rtsnesample <- Rtsne(sampleDists, is_distance = TRUE, perplexity = 5)
subsetfill <- c( ILC1="#C7302A", ILC2="#4266F6", ILC3="#269040", NK="#E7A626" )[colData(vsd)$subset]
donorshapes <- c("178"=21, "201"=22, "206"=23, "217"=24, "223"=25) [colData(vsd)$donor]
tissuecolor <- c(Jejunum="#212121", Colon="#000000", Lung="#717171") [colData(vsd)$tissue]
cairo_pdf(paste0(date," ",expNum," Tissue, Donor & Subset tSNE.pdf"), w=6, h=4)
plot(rtsnesample$Y, col=tissuecolor, bg=subsetfill, pch=donorshapes, cex=1, xlab="tSNE 1", ylab="tSNE 2", main="2-1B-1 Tissue, Donor & Subset tSNE")
dev.off()
## Top Variable Genes Heatmap ----------------------------------------------------
minNOTzero <- rownames(assay(vsd)[which(rowMin(counts(dds))>0),])
minNOTzero <- minNOTzero[which(rowVars(assay(vsd)[minNOTzero,])>1)]
topVarGenes <- head( minNOTzero[order( rowVars( assay(vsd)[minNOTzero,]), decreasing=TRUE)], 100)
ilc1VarGenes <- head( minNOTzero[order( rowVars( assay(vsd)[minNOTzero, grep("_ILC1",colnames(vsd))]), decreasing=TRUE)], 100)
ilc2VarGenes <- head( minNOTzero[order( rowVars( assay(vsd)[minNOTzero, grep("_ILC2",colnames(vsd))]), decreasing=TRUE)], 100)
ilc3VarGenes <- head( minNOTzero[order( rowVars( assay(vsd)[minNOTzero, grep("_ILC3",colnames(vsd))]), decreasing=TRUE)], 100)
nkVarGenes <- head( minNOTzero[order( rowVars( assay(vsd)[minNOTzero, grep("_NK",colnames(vsd))]), decreasing=TRUE)], 100)
allVarGenes <- unique(c(topVarGenes,
ilc3VarGenes,
ilc2VarGenes,
ilc1VarGenes,
nkVarGenes))
write.csv(counts(dds, normalized=TRUE)[topVarGenes,colorder],file= paste0(date," ",expNum," ", "Top Variable Genes.csv"))
write.csv(counts(dds, normalized=TRUE)[allVarGenes,colorder],file= paste0(date," ",expNum," ", "All Variable Genes.csv"))
resheatmap(vsd,
genes = topVarGenes,
samples= colnames(vsd),
cex= 0.7,
w=1.2,
title= "Top Variable Genes Heatmap")
dev.off()
resheatmap(vsd,
genes = allVarGenes,
samples= colnames(vsd),
cex= 0.7,
w=1.2,
title= "All Variable Genes Heatmap")
dev.off()
### Differential Expression Functions---------------------------------------------------------------------
deresults <- function (dds, fcThresh=2, baseMeanThresh=100, padjThresh=0.1){
reslist <- list()
resnames <- resultsNames(dds)[-1]
for (num in 2:length(resnames)){
for (den in 1:(num-1)){
print(paste0(resnames[num],".", resnames[den]))
res <- results(dds, contrast=list(resnames[num], resnames[den]))
res <- subset(res, abs(log2FoldChange) > fcThresh & baseMean > baseMeanThresh & padj < padjThresh)
reslist[[paste0(resnames[num],".", resnames[den])]] <- res
}
}
return(reslist)
}
### Differential Expression---------------------------------------------------------------------
sdg <- deresults(dds)
## Comparison to 3-1A-3 ---------------------
signaturegenes <- read.table("/Volumes/IBD/Yudanin/RNAseq/3-1A-3 RNAseq/3-1A-3 DAVID/3-1A-3 signaturegenes.txt",
quote="\"", comment.char="", na.strings="", stringsAsFactors=FALSE)
signaturegenes <- toupper(signaturegenes[[1]])
signaturegenes <- signaturegenes[which(signaturegenes %in% rownames(counts(dds)))]
signaturegenes <- signaturegenes[order( rowVars( assay(vsd)[signaturegenes,]), decreasing=TRUE)]
signaturegenes <- subset(signaturegenes,
rowMin(counts(dds)[signaturegenes,])>0)
signaturegenes <- signaturegenes[order(rowVars(assay(vsd)[signaturegenes, ]), decreasing=TRUE)]
resheatmap(vsd,
genes = signaturegenes,
samples= colnames(vsd),
cex= 0.9,
h=0.7,
w=1.2,
title= "3-1A-3 Comparison Heatmap")
dev.off()
## Comparison to 2-1A-4 ---------------------
tissuesignaturegenes <- read.csv("/Volumes/IBD/Yudanin/RNAseq/2-1A-4 RNAseq/2-1A-4 DESeq2/2-1A-4 Tissue Signature Genes.csv",
header=FALSE, stringsAsFactors=FALSE)
tissuesignaturegenes <- toupper(tissuesignaturegenes[[1]])
tissuesignaturegenes <- tissuesignaturegenes[which(tissuesignaturegenes %in% rownames(counts(dds)))]
tissuesignaturegenes <- subset(tissuesignaturegenes,
rowVars(counts(dds)[tissuesignaturegenes,])>1)
tissuesignaturegenes <- tissuesignaturegenes[order( rowVars( assay(vsd)[tissuesignaturegenes,]), decreasing=TRUE)]
resheatmap(vsd,
genes = tissuesignaturegenes,
samples= colnames(vsd),
cex= 0.9,
h=0.7,
w=1.2,
title= "2-1A-4 Comparison Heatmap")
dev.off()
## Tissue & Subset Signature Genes ---------------------
tissueGenes <- read.csv("/Volumes/IBD/Yudanin/RNAseq/2-2B-1 RNAseq/2-2B-1 DESeq2/All Samples/2-2B-1 Tissue Signature Genes.csv",
header=FALSE, stringsAsFactors=FALSE)
tissueGenes <- toupper(tissueGenes[[1]])
tissueGenes <- tissueGenes[which(tissueGenes %in% rownames(counts(dds)))]
tissueGenes <- subset(tissueGenes,
rowVars(counts(dds)[tissueGenes,])>1)
tissueGenes <- tissueGenes[order( rowVars( assay(vsd)[tissueGenes,]), decreasing=TRUE)]
subsetGenes <- read.csv("/Volumes/IBD/Yudanin/RNAseq/2-2B-1 RNAseq/2-2B-1 DESeq2/All Samples/2-2B-1 Subset Signature Genes.csv",
header=FALSE, stringsAsFactors=FALSE)
subsetGenes <- toupper(subsetGenes[[1]])
subsetGenes <- subsetGenes[which(subsetGenes %in% rownames(counts(dds)))]
subsetGenes <- subset(subsetGenes,
rowMin(counts(dds)[subsetGenes,])>0)
subsetGenes <- subsetGenes[order( rowVars( assay(vsd)[subsetGenes,]), decreasing=TRUE)]
subsettissueGenes <- unique(c(tissueGenes, subsetGenes))
subsettissueGenes <- subsettissueGenes[order( rowVars( assay(vsd)[subsettissueGenes,]), decreasing=TRUE)]
resheatmap(vsd,
genes = subsettissueGenes,
samples= colnames(vsd),
cex= 0.8,
h=0.6,
w=1.2,
title= "Tissue & Subset Signature Genes Heatmap")
dev.off()