-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselect_diff_regions.Rmd
324 lines (237 loc) · 11.1 KB
/
select_diff_regions.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
---
title: "select_diff_regions"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
dyn.load("/home/rstudio/libs/libxml2.so.2")
library(dplyr)
library(BSgenome)
library(BSgenome.Mmusculus.UCSC.mm10)
library(GenomicRanges)
dyn.load("/home/rstudio/libs/libgsl.so.25")
dyn.load("/home/rstudio/libs/libgslcblas.so.0")
library(TFBSTools)
library(tibble)
library(Biostrings)
library(rtracklayer)
```
Set significance parameters:
```{r, include=T}
fdr.matches = 0.05
# Joaquina's parameters for choosing significantly regulated regions
# fdr = 0.01
# min.l2fc = 2 # Min log2(fold change)
# min.baseMean = 100
# fdr = 0.01
# min.l2fc = 2 # Min log2(fold change)
# min.baseMean = 80
# fdr = 0.01
# min.l2fc = 2 # Min log2(fold change)
# min.baseMean = 60
# fdr = 0.01
# min.l2fc = 2 # Min log2(fold change)
# min.baseMean = 40
# fdr = 0.01
# min.l2fc = 2 # Min log2(fold change)
# min.baseMean = 20
# fdr = 0.01
# min.l2fc = 1.5 # Min log2(fold change)
# min.baseMean = 20
# fdr = 0.01
# min.l2fc = 1 # Min log2(fold change)
# min.baseMean = 20
# fdr = 0.01
# min.l2fc = 0.5 # Min log2(fold change)
# min.baseMean = 20
# fdr = 0.02
# min.l2fc = 0.5 # Min log2(fold change)
# min.baseMean = 20
# fdr = 0.03
# min.l2fc = 0.5 # Min log2(fold change)
# min.baseMean = 20
# fdr = 0.04
# min.l2fc = 0.5 # Min log2(fold change)
# min.baseMean = 20
fdr = 0.05
min.l2fc = 0.5 # Min log2(fold change)
min.baseMean = 20
# ---
# fdr = 0.01
# min.l2fc = 2 # Min log2(fold change)
# min.baseMean = 0
# fdr = 0.01
# min.l2fc = 0 # Min log2(fold change)
# min.baseMean = 0
# fdr = 0.05
# min.l2fc = 0 # Min log2(fold change)
# min.baseMean = 0
```
Define a general function for generating ranges of NFIA-dependent regions:
```{r, include=T}
select.dep.regions = function(p.11.7.dep, count.table) {
dep.annot = count.table %>%
dplyr::select(Geneid,
Chr,
Start,
End,
Strand) %>%
filter(Geneid %in% p.11.7.dep)
return(GRanges(seqnames = dep.annot$Chr,
ranges = IRanges(start = dep.annot$Start,
end = dep.annot$End,
names = dep.annot$Geneid),
strand = strand(dep.annot$Strand)))
}
```
Define a general function for scanning NFIA-dependent regions:
```{r, include=T}
scan.tf.motif = function(domain.name, p.nfia.hm.pwm, p.da.seq, strand, min.score, sites.filename) {
p.nfia.hm.sites = searchSeq(p.nfia.hm.pwm,
p.da.seq,
strand = strand,
min.score = min.score)
p.nfia.hm.sites.df = writeGFF3(p.nfia.hm.sites) %>%
dplyr::select(-source,
-feature,
-frame,
-attributes) %>%
mutate(pvalue = unlist(pvalues(p.nfia.hm.sites, type = "sampling"))) %>%
mutate(padj = p.adjust(pvalue, method = "BH")) %>%
filter(padj < fdr.matches)
saveRDS(p.nfia.hm.sites.df, file = sites.filename)
p.matched.regions = unique(p.nfia.hm.sites.df$seqname)
cat("\n")
cat(paste0(domain.name, ":\n"))
cat(paste0("Number of all NFIA-dependent regions : ", length(p.da.seq), "\n"))
cat(paste0("Number of matched NFIA-dependent regions: ", length(p.matched.regions),
" (", round(length(p.matched.regions) / length(p.da.seq), 2) * 100, "%)", "\n"))
cat(paste0("Number of NFIA motif matches : ", nrow(p.nfia.hm.sites.df)))
return(p.matched.regions)
}
```
## Select NFIA-dependent regions to scan
Select NFIA-dependent regions in each domain:
```{r, include=T}
p1.11.7.dep = readRDS(paste0("../r_results/diff_accessibility/tables/p1_D11_vs_D7_dep",
"_fdr", fdr, "_min-l2fc", min.l2fc, "_min-baseMean", min.baseMean, ".rds"))
p2.11.7.dep = readRDS(paste0("../r_results/diff_accessibility/tables/p2_D11_vs_D7_dep",
"_fdr", fdr, "_min-l2fc", min.l2fc, "_min-baseMean", min.baseMean, ".rds"))
pM.11.7.dep = readRDS(paste0("../r_results/diff_accessibility/tables/pM_D11_vs_D7_dep",
"_fdr", fdr, "_min-l2fc", min.l2fc, "_min-baseMean", min.baseMean, ".rds"))
p1 = read.delim("../results/select_samples/featureCounts_WT_D7_p1_vs_WT_D11_p1.tsv") %>%
dplyr::select(-X)
names(p1) = gsub(pattern = ".mLb.clN.bam", replacement = "", x = names(p1))
p2 = read.delim("../results/select_samples/featureCounts_WT_D7_p2_vs_WT_D11_p2.tsv") %>%
dplyr::select(-X)
names(p2) = gsub(pattern = ".mLb.clN.bam", replacement = "", x = names(p2))
pM = read.delim("../results/select_samples/featureCounts_WT_D7_pM_vs_WT_D11_pM.tsv") %>%
dplyr::select(-X)
names(pM) = gsub(pattern = ".mLb.clN.bam", replacement = "", x = names(pM))
p1.dep.ranges = select.dep.regions(p1.11.7.dep, p1)
export(p1.dep.ranges,
paste0("../r_results/select_diff_regions/p1_dep_ranges",
"_fdr", fdr, "_min-l2fc", min.l2fc, "_min-baseMean", min.baseMean,
".bed"))
p2.dep.ranges = select.dep.regions(p2.11.7.dep, p2)
export(p2.dep.ranges,
paste0("../r_results/select_diff_regions/p2_dep_ranges",
"_fdr", fdr, "_min-l2fc", min.l2fc, "_min-baseMean", min.baseMean,
".bed"))
pM.dep.ranges = select.dep.regions(pM.11.7.dep, pM)
export(pM.dep.ranges,
paste0("../r_results/select_diff_regions/pM_dep_ranges",
"_fdr", fdr, "_min-l2fc", min.l2fc, "_min-baseMean", min.baseMean,
".bed"))
```
## Scan NFIA-dependent regions for the NFIA motif
Obtain the sequences of NFIA-dependent regions per domain:
```{r, include=T}
mm10.genome = BSgenome.Mmusculus.UCSC.mm10
p1.da.seq = getSeq(mm10.genome, p1.dep.ranges)
p2.da.seq = getSeq(mm10.genome, p2.dep.ranges)
pM.da.seq = getSeq(mm10.genome, pM.dep.ranges)
```
Prepare background nucleotide frequencies in NFIA-dependent regions in each domain:
```{r, include=T}
p1.nc.freq = letterFrequency(p1.da.seq, letters = c("A", "C", "G", "T"))
p1.background = colSums(p1.nc.freq) / sum(p1.nc.freq)
p2.nc.freq = letterFrequency(p2.da.seq, letters = c("A", "C", "G", "T"))
p2.background = colSums(p2.nc.freq) / sum(p2.nc.freq)
pM.nc.freq = letterFrequency(pM.da.seq, letters = c("A", "C", "G", "T"))
pM.background = colSums(pM.nc.freq) / sum(pM.nc.freq)
```
Prepare NFIA motifs to scan:
```{r, include=T}
nfia.hm.pwm.raw = read.delim("../input/NFIA_HUMAN.H11MO.0.C.pwm",
header = F,
skip = 1)
names(nfia.hm.pwm.raw) = c("A", "C", "G", "T")
nfia.profile.matrix = t(nfia.hm.pwm.raw)
p1.nfia.hm.pwm = PWMatrix(ID = "NFIA",
name = "NFIA",
profileMatrix = nfia.profile.matrix,
bg = p1.background)
p2.nfia.hm.pwm = PWMatrix(ID = "NFIA",
name = "NFIA",
profileMatrix = nfia.profile.matrix,
bg = p2.background)
pM.nfia.hm.pwm = PWMatrix(ID = "NFIA",
name = "NFIA",
profileMatrix = nfia.profile.matrix,
bg = pM.background)
```
Scan NFIA-dependent regions in each domain for the NFIA binding motif from HOCOMOCO. Scan only one strand, as the NFIA motif from HOCOMOCO is palindromic, and we do not expect a strand bias in its matches. Indeed, for p1, I scanned both the "+" and the "-" strand, and the numbers of matched NFIA-dependent regions were almost identical.
```{r, include=T}
p1.matched.regions = scan.tf.motif(domain.name = "p1",
p.nfia.hm.pwm = p1.nfia.hm.pwm,
p.da.seq = p1.da.seq,
strand = "+",
min.score = "80%",
sites.filename = paste0("../r_results/select_diff_regions/",
"p1_nfia_hm_sites_plus-strand_80pc_match-fdr_", fdr.matches,
"_in-regions-of_fdr", fdr, "_min-l2fc", min.l2fc, "_min-baseMean", min.baseMean, ".rds"))
p2.matched.regions = scan.tf.motif(domain.name = "p2",
p.nfia.hm.pwm = p2.nfia.hm.pwm,
p.da.seq = p2.da.seq,
strand = "+",
min.score = "80%",
sites.filename = paste0("../r_results/select_diff_regions/",
"p2_nfia_hm_sites_plus-strand_80pc_match-fdr_", fdr.matches,
"_in-regions-of_fdr", fdr, "_min-l2fc", min.l2fc, "_min-baseMean", min.baseMean, ".rds"))
pM.matched.regions = scan.tf.motif(domain.name = "pM",
p.nfia.hm.pwm = pM.nfia.hm.pwm,
p.da.seq = pM.da.seq,
strand = "+",
min.score = "80%",
sites.filename = paste0("../r_results/select_diff_regions/",
"pM_nfia_hm_sites_plus-strand_80pc_match-fdr_", fdr.matches,
"_in-regions-of_fdr", fdr, "_min-l2fc", min.l2fc, "_min-baseMean", min.baseMean, ".rds"))
```
Select NFIA-dependent regions with NFIA matches:
```{r, include=T}
p1.dep.nfia.ranges = p1.dep.ranges[p1.dep.ranges@ranges@NAMES %in% p1.matched.regions]
p1.dep.nfia.ranges.filename = paste0("../r_results/select_diff_regions/",
"p1_dep_nfia_ranges_with_sites_plus-strand_80pc_match-fdr_", fdr.matches,
"_in-regions-of_fdr", fdr, "_min-l2fc", min.l2fc, "_min-baseMean", min.baseMean)
saveRDS(p1.dep.nfia.ranges,
file = paste0(p1.dep.nfia.ranges.filename, ".rds"))
export(p1.dep.nfia.ranges,
paste0(p1.dep.nfia.ranges.filename, ".bed"))
p2.dep.nfia.ranges = p2.dep.ranges[p2.dep.ranges@ranges@NAMES %in% p2.matched.regions]
p2.dep.nfia.ranges.filename = paste0("../r_results/select_diff_regions/",
"p2_dep_nfia_ranges_with_sites_plus-strand_80pc_match-fdr_", fdr.matches,
"_in-regions-of_fdr", fdr, "_min-l2fc", min.l2fc, "_min-baseMean", min.baseMean)
saveRDS(p2.dep.nfia.ranges,
file = paste0(p2.dep.nfia.ranges.filename, ".rds"))
export(p2.dep.nfia.ranges,
paste0(p2.dep.nfia.ranges.filename, ".bed"))
pM.dep.nfia.ranges = pM.dep.ranges[pM.dep.ranges@ranges@NAMES %in% pM.matched.regions]
pM.dep.nfia.ranges.filename = paste0("../r_results/select_diff_regions/",
"pM_dep_nfia_ranges_with_sites_plus-strand_80pc_match-fdr_", fdr.matches,
"_in-regions-of_fdr", fdr, "_min-l2fc", min.l2fc, "_min-baseMean", min.baseMean)
saveRDS(pM.dep.nfia.ranges,
file = paste0(pM.dep.nfia.ranges.filename, ".rds"))
export(pM.dep.nfia.ranges,
paste0(pM.dep.nfia.ranges.filename, ".bed"))
```