Switch to unified view

a b/c_VisualizationScript/Visulz_bulkATAC_motifEnrich.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
21
# graphics
22
library(ggplot2)
23
library(ggrepel)
24
library(scales)
25
library(latex2exp)
26
library(patchwork)
27
28
# * 2. Load data ----------------------------------------------------------
29
30
motifFile <- list.files("motif", "known", full.names = T)
31
motifGroup <- str_remove_all(motifFile, ".*/|_.*")
32
motifData <- map(motifFile, fread) %>% set_names(motifGroup)
33
34
motifMeta <- fread("../../data/motifTable.txt", sep = "\t")
35
motifMeta[Symbol == "", Symbol := `Factor Name`][]
36
37
name2symbol <- structure(
38
  motifMeta$Symbol,
39
  names = motifMeta$Name
40
)
41
42
motifData <- map(motifData, ~ {.x[, symbol := name2symbol[`Motif Name`]]})
43
motifData <- map(motifData, ~ {.x[is.na(symbol), symbol := str_remove(`Motif Name`, "/.*") %>% str_remove("\\(.*")]})
44
motifData <- map(motifData, ~ {.x[, symbol := toupper(symbol)]})
45
motifData <- map(motifData, ~ {.x[, p := -log10(`P-value`)]})
46
47
saveRDS(motifData$g1[, .(`Motif Name`, symbol)], "motifAnno.rds")
48
49
# * 3. Plot ---------------------------------------------------------------
50
51
plotList <- imap(motifData, ~ (
52
  ggplot(.x, aes(x = seq_along(p), y = p)) +
53
    geom_point(aes(size = p), show.legend = F) +
54
    geom_text_repel(
55
      data = .x[1:15], aes(label = symbol),
56
      nudge_x = 10, size = 3, fontface = 3, segment.size = .3, segment.alpha = .5) +
57
    scale_radius(range = c(.1, 2)) +
58
    labs(x = "order", y = TeX("$-log_{10}(\\textit{P}\\,value)$"), title = .y) +
59
    theme(
60
      aspect.ratio = 1,
61
      panel.background = element_blank(),
62
      panel.grid = element_blank(),
63
      axis.line = element_line()
64
    )
65
))
66
67
reduce(plotList, `+`) + plot_layout(ncol = 1)
68
69
ggsave("graphics/motifEnrich.png", width = 4, height = length(motifData) * 4)
70