-
Notifications
You must be signed in to change notification settings - Fork 132
/
tutorial_complex_Heatmap.Rmd
255 lines (165 loc) · 3.97 KB
/
tutorial_complex_Heatmap.Rmd
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
---
title: "R Notebook"
output: html_notebook
---
```{r}
if (!requireNamespace("BiocManager", quietly = TRUE))
install.packages("BiocManager")
BiocManager::install("DESeq2")
```
```{r}
library(DESeq2)
library(ggplot2)
```
```{bash}
ls
```
```{r}
Counts <- read.delim("count_table.csv", header = TRUE, row.names = 1, sep = ",")
```
```{r}
Counts
```
```{r}
Counts <- Counts[which(rowSums(Counts) > 0),]
```
```{r}
Counts
```
```{r}
condition <- factor(c("C","C","C","C", "S","S","S","S"))
```
```{r}
coldata <- data.frame(row.names = colnames(Counts), condition)
```
```{r}
coldata
```
```{r}
dds <- DESeqDataSetFromMatrix(countData = Counts, colData = coldata, design = ~condition)
```
```{r}
dds <- DESeq(dds)
```
```{r}
vsdata <- vst(dds, blind=FALSE)
```
```{r}
plotPCA(vsdata, intgroup = "condition")
```
```{r}
plotDispEsts(dds)
```
```{r}
res <- results(dds, contrast = c("condition", "S", "C"))
```
```{r}
res
```
```{r}
sigs <- na.omit(res)
```
```{r}
sigs <- sigs[sigs$padj < 0.05,]
```
```{r}
sigs
```
```{r}
write.csv(sigs, file = "deseq_results.csv")
```
```{bash}
ls
```
```{r}
sigs
```
```{r}
df <- as.data.frame(sigs)
df
```
```{r}
ensembl_map <- read.csv('ensemble_key_mapper.csv', header = FALSE)
keys <- ensembl_map$V1
values <- ensembl_map$V2
l <- list()
for (i in 1:length(keys)){
l[keys[i]] <- values[i]
}
```
```{r}
#for non-mapped labels
no_values <- setdiff(rownames(df), keys)
for (i in 1:length(no_values)){
l[no_values[i]] <- 'NA'
}
```
```{r}
df$symbol <- unlist(l[rownames(df)], use.names = FALSE)
```
```{r}
df.top <- df[ (df$baseMean > 50) & (abs(df$log2FoldChange) > 2),]
df.top
```
```{r}
df.top <- df.top[order(df.top$log2FoldChange, decreasing = TRUE),]
```
```{r}
rlog_out <- rlog(dds, blind=FALSE) #get normalized count data from dds object
mat<-assay(rlog_out)[rownames(df.top), rownames(coldata)] #sig genes x samples
colnames(mat) <- rownames(coldata)
base_mean <- rowMeans(mat)
mat.scaled <- t(apply(mat, 1, scale)) #center and scale each column (Z-score) then transpose
colnames(mat.scaled)<-colnames(mat)
```
```{r}
num_keep <- 25
#1 to num_keep len-num_keep to len
rows_keep <- c(seq(1:num_keep), seq((nrow(mat.scaled)-num_keep), nrow(mat.scaled)) )
```
```{r}
l2_val <- as.matrix(df.top[rows_keep,]$log2FoldChange) #getting log2 value for each gene we are keeping
colnames(l2_val)<-"logFC"
mean <- as.matrix(df.top[rows_keep,]$baseMean) #getting mean value for each gene we are keeping
colnames(mean)<-"AveExpr"
```
```{r}
if (!requireNamespace("BiocManager", quietly = TRUE))
install.packages("BiocManager")
BiocManager::install("ComplexHeatmap")
```
```{r}
library(ComplexHeatmap)
library(RColorBrewer)
library(circlize)
```
```{r}
#maps values between b/w/r for min and max l2 values
col_logFC <- colorRamp2(c(min(l2_val),0, max(l2_val)), c("blue", "white", "red"))
#maps between 0% quantile, and 75% quantile of mean values --- 0, 25, 50, 75, 100
col_AveExpr <- colorRamp2(c(quantile(mean)[1], quantile(mean)[4]), c("white", "red"))
```
```{r}
ha <- HeatmapAnnotation(summary = anno_summary(gp = gpar(fill = 2),
height = unit(2, "cm")))
h1 <- Heatmap(mat.scaled[rows_keep,], cluster_rows = F,
column_labels = colnames(mat.scaled), name="Z-score",
cluster_columns = T)
h2 <- Heatmap(l2_val, row_labels = df.top$symbol[rows_keep],
cluster_rows = F, name="logFC", top_annotation = ha, col = col_logFC,
cell_fun = function(j, i, x, y, w, h, col) { # add text to each grid
grid.text(round(l2_val[i, j],2), x, y)
})
h3 <- Heatmap(mean, row_labels = df.top$symbol[rows_keep],
cluster_rows = F, name = "AveExpr", col=col_AveExpr,
cell_fun = function(j, i, x, y, w, h, col) { # add text to each grid
grid.text(round(mean[i, j],2), x, y)
})
h<-h1+h2+h3
h
```
```{r}
png("./heatmap_v1.png", res = 300, width = 3000, height = 5500)
print(h)
dev.off()
```