[d06c2b]: / c_VisualizationScript / Visulz_bulkRNA_heatmap.R

Download this file

127 lines (97 with data), 3.4 kB

  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
# MESSAGE -----------------------------------------------------------------
#
# author: Yulin Lyu
# email: lvyulin@pku.edu.cn
#
# require: R whatever
#
# ---
# 1. Load packages --------------------------------------------------------
setwd("exampleData/RNA")
# grammar
library(tidyverse)
library(magrittr)
library(glue)
library(data.table)
# analysis
library(DESeq2)
# graphics
library(ComplexHeatmap)
library(circlize)
library(RColorBrewer)
library(ggsci)
library(scales)
# 2. Load data ------------------------------------------------------------
vsd <- readRDS("mid/vsd.rds")
vsdMtx <- assay(vsd)
diffData <- fread("mid/ES_vs_Fib.DEG.csv")
colnames(diffData)[1] <- "gene"
diffData[is.na(padj), padj := 1][]
diffData[, p := -log10(padj)][]
# 3. Plot -----------------------------------------------------------------
diffData[, type := "ns"][]
diffData[log2FoldChange > 3 & padj < 0.05, type := "up"][log2FoldChange < -3 & padj < 0.05, type := "down"][]
upGene <- diffData[order(p, decreasing = T)][type == "up"][1:10, gene]
downGene <- diffData[order(p, decreasing = T)][type == "down"][1:10, gene]
# heatmap with many genes
diffGene <- diffData[type != "ns", gene]
geneType <- diffData[type != "ns", type]
markGene <- c(upGene, downGene)
heatData <- vsdMtx[diffGene, ]
heatData %<>% apply(1, scale) %>% t() %>% set_colnames(colnames(vsdMtx))
colorFun <- colorRamp2(seq(1.5, -1.5, len = 9), brewer.pal(9, "RdBu"))
pdf("plot/heatmapMany.pdf", width = 6, height = 8)
Heatmap(
heatData, col = colorFun, border = T,
cluster_rows = T, cluster_columns = T,
show_row_names = F, show_column_names = T,
show_row_dend = F, show_column_dend = T,
column_names_rot = 45,
row_split = geneType,
column_split = rep(c("Fib", "CiPS", "ES"), each = 2),
left_annotation = rowAnnotation(
type = anno_block(
width = unit(.1, "in"), gp = gpar(fill = pal_nejm()(2)[2:1]))),
right_annotation = rowAnnotation(
gene = anno_mark(
match(markGene, diffGene), markGene,
labels_gp = gpar(fontface = "italic", fontsize = 10))),
heatmap_legend_param = list(
title = "Scaled expression",
title_position = "lefttop-rot",
legend_height = unit(1.5, "in")),
width = unit(3, "in"),
height = unit(6, "in")
)
dev.off()
# heatmap with few genes
setkey(diffData, gene)
usedGene <- c(upGene, downGene)
geneType <- diffData[usedGene, type]
heatData <- vsdMtx[usedGene, ]
heatData %<>% apply(1, scale) %>% t() %>% set_colnames(colnames(vsdMtx))
colorFun <- colorRamp2(seq(1.5, -1.5, len = 9), brewer.pal(9, "RdBu"))
pdf("plot/heatmapFew.pdf", width = 6, height = 6)
Heatmap(
heatData, col = colorFun, border = F,
cluster_rows = T, cluster_columns = T,
show_row_names = T, show_column_names = T,
show_row_dend = F, show_column_dend = T,
column_names_rot = 45,
row_names_gp = gpar(fontface = "italic"),
row_split = geneType,
column_split = rep(c("Fib", "CiPS", "ES"), each = 2),
cell_fun = function(j, i, x, y, width, height, fill){
grid.rect(x = x, y = y, width = width, height = height,
gp = gpar(col = "white", fill = NA))},
left_annotation = rowAnnotation(
type = anno_block(
width = unit(.1, "in"), gp = gpar(fill = pal_nejm()(2)[2:1]))),
heatmap_legend_param = list(
title = "Scaled expression",
title_position = "lefttop-rot",
legend_height = unit(1.5, "in")),
width = unit(3, "in"),
height = unit(4, "in")
)
dev.off()