-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot_heatmap_deseq.R
54 lines (51 loc) · 1.91 KB
/
plot_heatmap_deseq.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
library(tidyverse)
library(DESeq2)
# plotting function
plot_deseq2_hm <- function(res, dds, lfc=NULL, n=100, row.names=T,
colors=c("steelblue4","white","firebrick4")) {
resdf <- as.data.frame(res) %>%
mutate(gene = rownames(res))
if (is.null(n) | is.na(n)) stop("n required - plotting all genes is not recommnended")
if (!is.null(lfc)) {
resdf <- resdf %>% dplyr::filter(abs(log2FoldChange) > lfc)
}
resdf <- resdf %>%
mutate(dir=ifelse(log2FoldChange < 0, "neg", "pos")) %>%
dplyr::filter(!is.na(dir) & !is.na(padj)) %>%
group_by(dir) %>%
arrange(-abs(log2FoldChange)) %>%
slice_head(n=n) %>%
ungroup() %>%
arrange(log2FoldChange)
rlogdds <- rlog(dds)
plotdat <- assay(rlogdds)[match(resdf$gene, rownames(rlogdds)),] %>%
as.data.frame() %>%
cbind(resdf, .) %>%
mutate(gene=factor(gene, levels=unique(gene))) %>%
gather("sample", "expr", -c(1:8)) %>%
group_by(gene) %>%
mutate(expr = scale(as.numeric(expr)))
message(paste0("Plotting ", length(unique(plotdat$gene)), " genes"))
p <- ggplot(plotdat, aes(x=sample, y=gene, fill=expr)) +
geom_tile() +
scale_x_discrete(expand=c(0,0)) +
scale_y_discrete(expand=c(0,0)) +
scale_fill_gradient2(low = colors[[1]], mid = colors[[2]], high = colors[[3]],
name = "Expression level\n[z-score]") +
theme(panel.background = element_blank(),
panel.border = element_rect(color="black", fill=NA),
panel.grid = element_blank(),
axis.title = element_blank(),
axis.ticks = element_blank())
if (row.names == F) {
p <- p + theme(axis.text.y = element_blank())
}
return(p)
}
# load example data
dds <- makeExampleDESeqDataSet(m=4)
dds <- DESeq(dds)
res <- results(dds, contrast=c("condition","B","A"))
# plot
plot_deseq2_hm(res=res, dds=dds, n=250, row.names=F)
plot_deseq2_hm(res=res, dds=dds, n=250, lfc=4, row.names=T)