a b/c_VisualizationScript/Visulz_bulkATAC_heatmapPeak.R
1
2
# MESSAGE -----------------------------------------------------------------
3
#
4
# author: Yulin Lyu
5
# email: lvyulin@pku.edu.cn
6
#
7
# require: R whatever
8
#
9
# ---
10
11
# * 1. Load packages ------------------------------------------------------
12
13
setwd("exampleData/ATAC")
14
15
# grammar
16
library(tidyverse)
17
library(magrittr)
18
library(glue)
19
library(data.table)
20
library(GenomicRanges)
21
22
# graphics
23
library(ComplexHeatmap)
24
library(circlize)
25
library(RColorBrewer)
26
library(ggsci)
27
library(scales)
28
29
# * 2. Load data ----------------------------------------------------------
30
31
diffList <- readRDS("diffList.rds")
32
peakMeta <- readRDS("peakMeta.rds")
33
peakMtx <- readRDS("peakMtx.rds")
34
35
diffPeak <- map(diffList, ~ {as.data.table(.x)[abs(Fold) > 1, paste(seqnames, start, end, sep = "_")]}) %>%
36
  unlist() %>% unname() %>% unique()
37
38
peakMeta[, id := paste(Chr, Start, End, sep = "_")][]
39
40
diffMtx <- as.matrix(peakMtx[peakMeta$id %in% diffPeak]) %>% set_rownames(peakMeta[id %in% diffPeak, id])
41
42
# * 3. Plot ---------------------------------------------------------------
43
44
dir.create("graphics")
45
46
heatData <- apply(diffMtx, 1, scale) %>% t() %>% set_colnames(colnames(diffMtx))
47
48
colorFun <- colorRamp2(seq(2, -2, len = 9), brewer.pal(9, "RdBu"))
49
50
p <- Heatmap(
51
  heatData[, c(3:5, 1:2, 6:8)], col = colorFun, border = F,
52
  cluster_rows = T, cluster_columns = F,
53
  show_row_names = F, show_column_names = T,
54
  show_row_dend = F, show_column_dend = F,
55
  column_names_rot = 45,
56
  row_km = 5,
57
  row_km_repeats = 100,
58
  left_annotation = rowAnnotation(
59
    type = anno_block(
60
      width = unit(.1, "in"), gp = gpar(fill = pal_nejm()(5)))),
61
  heatmap_legend_param = list(
62
    title = "Scaled expression",
63
    title_position = "lefttop-rot",
64
    legend_height = unit(2, "in")),
65
  width = unit(3, "in"),
66
  height = unit(6, "in"))
67
68
png("graphics/heatmapPeak.png", width = 4, height = 7, units = "in", res = 300)
69
p
70
dev.off()
71
72
peakOrder <- row_order(p)
73
peakGroup <- map(peakOrder, ~ {rownames(diffMtx)[.x]})
74
75
saveRDS(peakGroup, "peakGroup.rds")