[314984]: / b_DownstreamAnalysisScript / bulkATACana_3_annotatePeak.R

Download this file

55 lines (38 with data), 1.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
# MESSAGE -----------------------------------------------------------------
#
# author: Yulin Lyu
# email: lvyulin@pku.edu.cn
#
# require: R whatever
#
# ---
# * 1. Load packages ------------------------------------------------------
setwd("exampleData/ATAC")
# grammar
library(tidyverse)
library(magrittr)
library(glue)
library(data.table)
# analysis
library(ChIPseeker)
library(GenomicFeatures)
# * 2. Load data ----------------------------------------------------------
txdb <- makeTxDbFromGFF("../../data/hg19genes.gtf")
peakMeta <- readRDS("peakMeta.rds")
peakMeta[, id := paste(Chr, Start, End, sep = "_")][]
peakGroup <- readRDS("peakGroup.rds")
peakIndex <- map(peakGroup, ~ {match(.x, peakMeta$id)})
# * 3. Analyze ------------------------------------------------------------
homerPeak <- peakMeta
homerPeak[, Chr := str_c("chr", Chr)][]
fwrite(homerPeak, "allPeak.homer", sep = "\t", col.names = F)
peakGRlist <- map(peakIndex, ~ {
GRanges(
seqnames = peakMeta[.x, Chr],
ranges = IRanges(
start = peakMeta[.x, Start],
end = peakMeta[.x, End]))})
peakAnnoList <- map(peakGRlist, annotatePeak, TxDb = txdb, tssRegion = c(-2000, 2000))
map(peakGRlist, ~ {as.data.table(.x)[, c("width", "strand") := NULL][, id := 1:.N][, seqnames := str_c("chr", seqnames)]}) %>%
iwalk(~ fwrite(.x, str_c("peakGroup_", .y, ".bed"), sep = "\t", col.names = F))
saveRDS(peakAnnoList, "peakAnnoList.rds")