-
Notifications
You must be signed in to change notification settings - Fork 0
/
CAGEfightR_to_ANANSE.Rmd
352 lines (276 loc) · 11.9 KB
/
CAGEfightR_to_ANANSE.Rmd
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
---
## R Markdown CAGEfightR to ANANSE workflow
Here CAGE-seq data is prepared for the ANANSE pipeline.
Pre-processing: BED files containing CTSSs data can be converted to bigwig
format.
Input: CTSS files in bigwig format
Outputs:
- Enhancer regions with (average) score (TPM)
- Enhancer region file in .bed format
- Gene expression files per sample(TPM)
- Differential gene expression
Genome = hg19
Required layout:
work_dir/
data/
source_type_1.CTSS.raw.minus.bw
source_type_1.CTSS.raw.plus.bw
target_type_1.CTSS.raw.minus.bw
target_type_1.CTSS.raw.plus.bw
out_dir/
target_type/
enhancer_source.tsv
enhancer_target.tsv
TPM_source_1.txt
TPM_target_1.txt
DE_genes.txt
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(rtracklayer)
library(CAGEfightR)
library(InteractionSet)
library(stringr)
library(dplyr)
library(tidyr)
library(DESeq2)
library(TxDb.Hsapiens.UCSC.hg19.knownGene) # Change accordingly!
library(BSgenome.Hsapiens.UCSC.hg19) # Change accordingly!
library(org.Hs.eg.db) # If fails, run: options(connectionObserver = NULL)
# Genome info - Change accordingly!
UCSCGenome <- "hg19"
BS_genome <- "BSgenome.Hsapiens.UCSC.hg19"
txdb <- TxDb.Hsapiens.UCSC.hg19.knownGene
odb <- org.Hs.eg.db
# Data
setwd("PATH_TO_YOUR_WORK_DIRECTORY")
work_dir <- getwd()
data = "PATH_TO_YOUR_DATA_DIRECTORY"
print(list.files(path = data))
# Pre-processing (TRUE or FALSE)
# To convert ctss files to bigwig
PREPROCESSING = FALSE
# Select samples - Change accordingly!
source_type <- "CD14_Monocytes"
target_types <- "MLLAF9"
# Or for multiple cell types
# source_type <- "Fibroblast_Skin"
# target_types <- c("Hepatocyte",
# "hIPS",
# "Keratinocyte_epidermal",
# "Macrophage_monocyte_derived",
# "Astrocyte_Cerebral_Cortex")
# Select CAGEfightR stringency
UNEXPRESSED <- 1 # minimal TPM
```
```{r CTSSs to BigWig pre-processing}
# Download FANTOM5 data from https://fantom.gsc.riken.jp/5/sstar/Browse_samples
# Use "wget" to download the CTSS.bed.gz files and "gunzip" to uncompress files
# Change names accordingly. For example, Fibroblast_1.bed
if (PREPROCESSING == TRUE){
hg19 <- SeqinfoForUCSCGenome("hg19")
bedfiles <- list.files(path = data, pattern = "*.bed", full.names = TRUE)
if (length(bedfiles) == 0){print("No BED files in data directory. Pre-processing failed.")}
for (i in bedfiles) {
print(i)
name_plus <- str_replace(i, ".bed", ".CTSS.raw.plus.bw")
name_minus <- str_replace(i, ".bed", ".CTSS.raw.minus.bw")
convertBED2BigWig(input=i,
outputPlus=name_plus,
outputMinus=name_minus,
genome=hg19)
}
} else {
print("Pre-processing skipped.")
}
```
```{r CAGEfightR to ANANSE}
for (type in target_types){
out_dir <- paste(source_type, "_to_", type, sep = "")
dir.create(file.path(work_dir, out_dir)) # Create output dir
target_type = type
genomeInfo <- seqinfo(txdb)
# Create two named BigWigFileList-objects
pattern_plus <- paste("(", source_type, "|", target_type, ").*plus",
sep = "")
pattern_minus <- paste("(", source_type, "|", target_type, ").*minus",
sep = "")
bw_plus <- list.files(path = data, pattern = pattern_plus, full.names = T)
bw_minus <- list.files(path = data, pattern = pattern_minus, full.names = T)
# reorder files to source_type first and target_type second
if (grepl(source_type, basename(bw_plus[1])) == F){
print("WARNING: Reorder file order.")
bw_plus <- sort(bw_plus, decreasing = T)}
if (grepl(source_type, basename(bw_minus[1])) == F){
bw_minus <- sort(bw_minus, decreasing = T)}
bw_plus <- BigWigFileList(bw_plus)
bw_minus <- BigWigFileList(bw_minus)
names(bw_plus) <- sub(".CTSS.raw.plus.bw", "", basename(bw_plus))
names(bw_minus) <- sub(".CTSS.raw.minus.bw", "", basename(bw_minus))
source_number <- length(grep(source_type, names(bw_plus), value = F))
target_number <- length(grep(target_type, names(bw_plus), value = F))
# Set minimum samples cutoff
if (source_number > 2 && target_number > 2){
minimum_samples <- 2
}
else if (source_number == 2 && target_number == 2) {
minimum_samples <- 1
}
else{
minimum_samples <- 0
}
# Set minimum samples manually
# minimum_samples <- 3
# Sanity check
print(names(bw_plus))
print(names(bw_minus))
# Quantify CTSS, normalize, and pool CTSSs
CTSSs <- quantifyCTSSs(plusStrand = bw_plus,
minusStrand = bw_minus,
genome = genomeInfo)
CTSSs <- calcTPM(CTSSs, inputAssay="counts", outputAssay="TPM",
outputColumn = "totalTags")
CTSSs <- calcPooled(CTSSs, inputAssay = "TPM")
# Remove excess noise
# (Default) Count number of samples with MORE ( > ) than 0 counts:
CTSSs <- calcSupport(CTSSs,
inputAssay="counts",
outputColumn="support",
unexpressed=0)
# Adjust "support" to discard CTSSs only in 'n' samples
# Default = 1
# table(rowRanges(CTSSs)$support)
supportedCTSSs <- subset(CTSSs, support > 1)
supportedCTSSs <- calcTPM(supportedCTSSs, totalTags="totalTags")
supportedCTSSs <- calcPooled(supportedCTSSs)
# Unidirectional clustering
prefiltered_TCs <- clusterUnidirectionally(supportedCTSSs,
pooledCutoff=0,
mergeDist=20)
Unidirectional <- quantifyClusters(CTSSs,
clusters=prefiltered_TCs,
inputAssay="counts")
Unidirectional <- calcTPM(Unidirectional,
totalTags = "totalTags")
# Only TSSs expressed at more than 1 TPM in more than 'n' samples
# Default: unexpressed = 0, minSamples = 1
Unidirectional <- subsetBySupport(Unidirectional,
inputAssay="TPM",
unexpressed=UNEXPRESSED,
minSamples=minimum_samples)
# Annotation (Transcript Models)
Unidirectional <- assignTxID(Unidirectional,
txModels=txdb,
outputColumn="txID")
Unidirectional <- assignTxType(Unidirectional,
txModels=txdb,
outputColumn="txTyp")
Unidirectional <- assignTxType(Unidirectional,
txModels=txdb,
outputColumn="peakTxType",
swap="thick")
Unidirectional <- assignGeneID(Unidirectional,
geneModels=txdb,
outputColumn="geneID")
# Match IDs to symbols
symbols_uni <- mapIds(odb,
keys=rowRanges(Unidirectional)$geneID,
keytype="ENTREZID",
column="SYMBOL")
# Add to object
rowRanges(Unidirectional)$symbol <- as.character(symbols_uni)
Unidirectional <- assignMissingID(Unidirectional,
outputColumn="symbol")
Unidirectional <- assignGeneID(Unidirectional,
geneModels=txdb,
outputColumn="geneID")
# Bidirectional clustering
# Default: balanceThreshold=0.95
BCs <- clusterBidirectionally(CTSSs, balanceThreshold=0.95)
BCs <- calcBidirectionality(BCs, samples=CTSSs)
# Remove (excess noise) BCs that are not observed in > 'n' samples
# Default = 0
# table(BCs$bidirectionality)
BCs <- subset(BCs, bidirectionality > 0)
Bidirectional <- quantifyClusters(CTSSs,
clusters=BCs,
inputAssay="counts")
Bidirectional <- calcTPM(Bidirectional,
totalTags = "totalTags")
# Remove excess noise: Only keep BCs expressed in more than 'n' samples
# Default minSamples = 1, unexpressed = 0
Bidirectional <- subsetBySupport(Bidirectional,
inputAssay="counts",
unexpressed=UNEXPRESSED,
minSamples=minimum_samples)
# Genelevel
genelevel <- quantifyGenes(Unidirectional,
genes="symbol",
inputAssay="counts")
genelevel$group <- factor(c(rep(source_type, source_number),
rep(target_type, target_number)))
# genelevel$group <- relevel(genelevel$group, target_type)
print(genelevel$group)
# --------------------------------- DEseq2 --------------------------------- #
dds_genelevel <- DESeqDataSet(genelevel, ~group)
dds <- DESeq(dds_genelevel)
res <- results(dds, contrast = c("group", target_type, source_type))
# --------------------------------- EXPORT --------------------------------- #
# Enhancers & Regions
enhancers <- assays(Bidirectional)$TPM
print(colnames(enhancers))
# Enhancers source
average_tpm_source_enhancer <- rowMeans(enhancers[,c(1:source_number)])
source_enhancers <- data.frame(rownames(enhancers),
Means = average_tpm_source_enhancer)
rownames(source_enhancers) <- NULL
colnames(source_enhancers) <- c("pos", "CAGE")
source_enhancers_name <- paste(out_dir, "enhancers_source.tsv", sep = "/")
write.table(source_enhancers,
source_enhancers_name,
row.names = F,
quote = FALSE,
sep = "\t")
# Enhancers target
average_tpm_target_enhancer <- rowMeans(enhancers[,c((source_number + 1):(source_number + target_number))])
target_enhancers <- data.frame(rownames(enhancers),
Means = average_tpm_target_enhancer)
rownames(target_enhancers) <- NULL
colnames(target_enhancers) <- c("pos", "CAGE")
target_enhancers_names <- paste(out_dir, "enhancers_target.tsv", sep = "/")
write.table(target_enhancers,
target_enhancers_names,
row.names = F,
quote = FALSE,
sep = "\t")
# Gene expression (TPM)
genes_tpm <- calcTPM(genelevel)
genes_tpm <- assays(genes_tpm)$TPM
row.names.remove <- c("NULL")
genes_tpm <- genes_tpm[!(row.names(genes_tpm) %in% row.names.remove),]
for (i in colnames(genes_tpm)) {
print(i)
type_tpm <- data.frame(genes_tpm[,i])
type_tpm <- tibble::rownames_to_column(type_tpm, "resid")
colnames(type_tpm) <- c("resid", "tpm")
if (grepl(source_type, i) == T){
file_name <- str_replace(i, source_type, "")
file_name <- paste(out_dir, "/TPM_source", file_name, ".txt", sep="")
write.table(type_tpm, file_name, row.names = F, quote = FALSE, sep = "\t")
} else {
file_name <- str_replace(i, target_type, "")
file_name <- paste(out_dir, "/TPM_target", file_name, ".txt", sep="")
write.table(type_tpm, file_name, row.names = F, quote = FALSE, sep = "\t")
}
}
# Differentially expressed genes
de_genes <- res[,c(2,6)]
row.names.remove <- c("NULL")
de_genes <- de_genes[!(row.names(de_genes) %in% row.names.remove),]
de_genes <- as.data.frame(de_genes)
de_genes <- tibble::rownames_to_column(de_genes, "resid")
de_genes <- drop_na(de_genes)
de_genes_name <- paste(out_dir, "DE_genes.txt", sep = "/")
write.table(de_genes, de_genes_name, row.names = F, quote = FALSE, sep = "\t")
}
```