-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdef_enhancers.R
executable file
·434 lines (311 loc) · 22 KB
/
def_enhancers.R
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
### def_enhancers() DOES NOT WORK
### ===============================================================================================
#' @title def_enhancers
#' @author amitjavilaventura
#'
#' @usage def_enhancers(k4me1, k27ac, k27me3 = NULL, conditions)
#'
#' @returns A list of dataframes with the active, primed and poised enhancers
#'
#' @param k4me1 List of dataframes with the H3K4me1 distal peaks in different conditions. Dataframes must contain the columns seqnames, start, end and condition. Cannot be NULL.
#' @param k27ac List of dataframes with the H3K27ac distal peaks in different conditions. Dataframes must contain the columns seqnames, start, end and condition. Cannot be NULL.
#' @param k27me3 List of dataframes with the H3K27me3 distal peaks in different conditions. Dataframes must contain the columns seqnames, start, end and condition. Can be NULL.
#' @param conditions Character vector with the names of the conditions present in the dataframes present in k4me1, k27ac and k27me3 arguments
def_enhancers <- function(k4me1, k27ac, k27me3 = NULL, conditions){
# Load packages
library(dplyr)
library(plyranges)
if(!is.null(k27me3)){
# define empty lists
active_enhancers <- list()
poised_enhancers <- list()
primed_enhancers <- list()
for(i in 1:length(conditions)){
# name of the conditions
name <- conditions[i]
# active enhancers
active <- plyranges::filter_by_overlaps(x = k4me1 %>% bind_rows() %>%
dplyr::filter(condition == name) %>% as_granges(),
y = k27ac %>% bind_rows() %>%
dplyr::filter(condition == name) %>% as_granges()) %>%
plyranges::filter_by_non_overlaps(y = k27me3 %>% bind_rows() %>%
dplyr::filter(condition == name) %>% as_granges())
# primed enhancers
primed <- plyranges::filter_by_non_overlaps(x = k4me1 %>% bind_rows() %>%
dplyr::filter(condition == name) %>% as_granges(),
y = k27ac %>% bind_rows() %>%
dplyr::filter(condition == name) %>% as_granges()) %>%
plyranges::filter_by_non_overlaps(y = k27me3 %>% bind_rows() %>%
dplyr::filter(condition == name) %>% as_granges())
# poised enhancers
poised <- plyranges::filter_by_overlaps(x = k4me1 %>% bind_rows() %>%
dplyr::filter(condition == name) %>% as_granges(),
y = k27me3 %>% bind_rows() %>%
dplyr::filter(condition == name) %>% as_granges()) %>%
plyranges::filter_by_non_overlaps(y = k27ac %>% bind_rows() %>%
dplyr::filter(condition == name) %>% as_granges())
# fill lists
active_enhancers[[paste(name, "active", sep = "-")]] <- active %>% as.data.frame() %>% dplyr::mutate(enhancer = "Active")
primed_enhancers[[paste(name, "primed", sep = "-")]] <- primed %>% as.data.frame() %>% dplyr::mutate(enhancer = "Primed")
poised_enhancers[[paste(name, "poised", sep = "-")]] <- poised %>% as.data.frame() %>% dplyr::mutate(enhancer = "Poised")
}
# merge active, primed and poised
all_enhancers <- c(active_enhancers, primed_enhancers, poised_enhancers)
# return
return(all_enhancers)
} else {
# define empty lists
active_enhancers <- list()
poised_enhancers <- list()
primed_enhancers <- list()
for(i in 1:length(conditions)){
# name of the conditions
name <- conditions[i]
# active enhancers
active <- plyranges::filter_by_overlaps(x = k4me1 %>% bind_rows() %>%
dplyr::filter(condition == name) %>% as_granges(),
y = k27ac %>% bind_rows() %>%
dplyr::filter(condition == name) %>% as_granges())
# primed enhancers
primed <- plyranges::filter_by_non_overlaps(x = k4me1 %>% bind_rows() %>%
dplyr::filter(condition == name) %>% as_granges(),
y = k27ac %>% bind_rows() %>%
dplyr::filter(condition == name) %>% as_granges())
# fill lists
active_enhancers[[paste(name, "active", sep = "-")]] <- active %>% as.data.frame() %>% dplyr::mutate(enhancer = "Active")
primed_enhancers[[paste(name, "primed", sep = "-")]] <- primed %>% as.data.frame() %>% dplyr::mutate(enhancer = "Primed")
}
# merge active, primed and poised
all_enhancers <- c(active_enhancers, primed_enhancers)
# return
return(all_enhancers)
}
}
### def_enhancers_no_k4me3()
### ===============================================================================================
#' @title def_enhancers_no_k4me3
#' @author amitjavilaventura
#'
#' @usage def_enhancers_no_k4me3(k4me1, k27ac, k4me3, k27me3 = NULL, conditions)
#'
#' @returns A list of dataframes with the active, primed and poised enhancers
#'
#' @param k4me1 List of dataframes with the H3K4me1 distal peaks in different conditions. Dataframes must contain the columns seqnames, start, end and condition. Cannot be NULL.
#' @param k27ac List of dataframes with the H3K27ac distal peaks in different conditions. Dataframes must contain the columns seqnames, start, end and condition. Cannot be NULL.
#' @param k4me3 List of dataframes with the H3K27me3 distal peaks in different conditions. Dataframes must contain the columns seqnames, start, end and condition. Cannot be NULL.
#' @param k27me3 List of dataframes with the H3K27me3 distal peaks in different conditions. Dataframes must contain the columns seqnames, start, end and condition. Can be NULL.
#' @param conditions Character vector with the names of the conditions present in the dataframes present in k4me1, k27ac and k27me3 arguments
def_enhancers_no_k4me3 <- function(k4me1, k27ac, k4me3, k27me3 = NULL, conditions){
# Load packages
library(dplyr)
library(plyranges)
if(!is.null(k27me3)){
# define empty lists
active_enhancers <- list()
poised_enhancers <- list()
primed_enhancers <- list()
for(i in 1:length(conditions)){
# name of the conditions
name <- conditions[i]
# active enhancers
active <- plyranges::filter_by_overlaps(x = k4me1 %>% bind_rows() %>%
dplyr::filter(condition == name) %>% as_granges(),
y = k27ac %>% bind_rows() %>%
dplyr::filter(condition == name) %>% as_granges()) %>%
plyranges::filter_by_non_overlaps(y = k27me3 %>% bind_rows() %>%
dplyr::filter(condition == name) %>% as_granges()) %>%
plyranges::filter_by_non_overlaps(y = k4me3 %>% bind_rows() %>%
dplyr::filter(condition == name) %>% as_granges())
# primed enhancers
primed <- plyranges::filter_by_non_overlaps(x = k4me1 %>% bind_rows() %>%
dplyr::filter(condition == name) %>% as_granges(),
y = k27ac %>% bind_rows() %>%
dplyr::filter(condition == name) %>% as_granges()) %>%
plyranges::filter_by_non_overlaps(y = k27me3 %>% bind_rows() %>%
dplyr::filter(condition == name) %>% as_granges()) %>%
plyranges::filter_by_non_overlaps(y = k4me3 %>% bind_rows() %>%
dplyr::filter(condition == name) %>% as_granges())
# poised enhancers
poised <- plyranges::filter_by_overlaps(x = k4me1 %>% bind_rows() %>%
dplyr::filter(condition == name) %>% as_granges(),
y = k27me3 %>% bind_rows() %>%
dplyr::filter(condition == name) %>% as_granges()) %>%
plyranges::filter_by_non_overlaps(y = k27ac %>% bind_rows() %>%
dplyr::filter(condition == name) %>% as_granges()) %>%
plyranges::filter_by_non_overlaps(y = k4me3 %>% bind_rows() %>%
dplyr::filter(condition == name) %>% as_granges())
# fill lists
active_enhancers[[paste(name, "active", sep = "-")]] <- active %>% as.data.frame() %>% dplyr::mutate(enhancer = "Active")
primed_enhancers[[paste(name, "primed", sep = "-")]] <- primed %>% as.data.frame() %>% dplyr::mutate(enhancer = "Primed")
poised_enhancers[[paste(name, "poised", sep = "-")]] <- poised %>% as.data.frame() %>% dplyr::mutate(enhancer = "Poised")
}
# merge active, primed and poised
all_enhancers <- c(active_enhancers, primed_enhancers, poised_enhancers)
# return
return(all_enhancers)
} else {
# define empty lists
active_enhancers <- list()
primed_enhancers <- list()
for(i in 1:length(conditions)){
# name of the conditions
name <- conditions[i]
# active enhancers
active <- plyranges::filter_by_overlaps(x = k4me1 %>% bind_rows() %>%
dplyr::filter(condition == name) %>% as_granges(),
y = k27ac %>% bind_rows() %>%
dplyr::filter(condition == name) %>% as_granges()) %>%
plyranges::filter_by_non_overlaps(y = k4me3 %>% bind_rows() %>%
dplyr::filter(condition == name) %>% as_granges())
# primed enhancers
primed <- plyranges::filter_by_non_overlaps(x = k4me1 %>% bind_rows() %>%
dplyr::filter(condition == name) %>% as_granges(),
y = k27ac %>% bind_rows() %>%
dplyr::filter(condition == name) %>% as_granges()) %>%
plyranges::filter_by_non_overlaps(y = k4me3 %>% bind_rows() %>%
dplyr::filter(condition == name) %>% as_granges())
# fill lists
active_enhancers[[paste(name, "active", sep = "-")]] <- active %>% as.data.frame() %>% dplyr::mutate(enhancer = "Active")
primed_enhancers[[paste(name, "primed", sep = "-")]] <- primed %>% as.data.frame() %>% dplyr::mutate(enhancer = "Primed")
}
# merge active, primed and poised
all_enhancers <- c(active_enhancers, primed_enhancers)
# return
return(all_enhancers)
}
}
### def_enhancers_k4me3_signal() DOES NOT WORK
### ===============================================================================================
#' @title def_enhancers_no_k4me3
#' @author amitjavilaventura
#'
#' It takes the k4me3 signal from bigwig files, then it computes the coverage score in the k4me3_promo and k4me3_distal peaks.
#' The k4me3_distal peaks that have a score higher than a certain score in k4me3_promo peaks (i.e. mean, median, q1, max...) are saved in a k4me3_distal_out list.
#' The k4me1_distal peaks are filtered and only those that do not overlap with the k4me3_distal_out peaks are retained.
#' Then k4me1_distal_filt peaks are overlapped (or non overlapped) with other mods to define active, primed and poised (poised only if k4me3_distal is not null) enhancers.
#'
#' @usage def_enhancers_k4me3_signal(k4me1_distal, k27ac_distal, k27me3_distal = NULL, conditions, k4me3_beds_promo, k4me3_beds_distal, k4me3_bws, coverage_op = "mean", filter_by_promo_score = "median")
#'
#' @returns A list of dataframes with the active, primed and poised enhancers
#'
#' @param k4me1_distal List of dataframes with the H3K4me1 distal peaks in different conditions. Dataframes must contain the columns seqnames, start, end and condition. Cannot be NULL.
#' @param k27ac_distal List of dataframes with the H3K27ac distal peaks in different conditions. Dataframes must contain the columns seqnames, start, end and condition. The order of the dataframes must be the same as in 'k4me1_distal'. Cannot be NULL.
#' @param k27me3_distal List of dataframes with the H3K27me3 distal peaks in different conditions. Dataframes must contain the columns seqnames, start, end and condition. The order of the dataframes must be the same as in 'k4me1_distal'. Can be NULL.
#' @param conditions Character vector with the names of the conditions present in the dataframes present in k4me1, k27ac and k27me3 arguments. Order of the conditions must be the same as in 'k4me1_distal'.
#' @param k4me3_beds_promo Character vector with the paths to the BED files of the H3K4me3 promoter peaks in each condition. Length must be equal to 'k4me1_distal'. Order of the conditions must be the same as in 'k4me1_distal'. The path must have the name of the conditions.
#' @param k4me3_beds_distal Character vector with the paths to the BED files of the H3K4me3 distal peaks in each condition. Length must be equal to 'k4me1_distal'. Order of the conditions must be the same as in 'k4me1_distal'. The path must have the name of the conditions.
#' @param k4me3_bws Character vector with the paths to the BW files of H3K4me3 in each condition. Order of the conditions must be the same as in 'k4me1_distal'. The path must have the name of the conditions.
#' @param coverage_op Character of length 1 indicating which operation should be used to compute the score from the BW file in each region. Passed through 'megadepth::get_coverage()'. One of c("mean", "sum", "min", "max"). Default: "mean"
#' @param filter_by_promo_score Either a character of lenght 1 with the measure of promoter scores or a numeric of lenght 1 indicating the percentile of the promoter scores to use to filter the distal peaks. If character, one of c("mean", "min", "q0", "q1, "median", "q2", "q3", "max", "q4"). If numeric, a value between 0 and 1. Default: "median"
def_enhancers_k4me3_signal <- function(k4me1_distal, k27ac_distal, k27me3_distal = NULL, conditions,
k4me3_beds_promo, k4me3_beds_distal, k4me3_bws, coverage_op = "mean",
filter_by_promo_score = "median" ) {
# load packages
require(dplyr)
require(megadepth)
# FILTER THE K4ME3 DISTAL PEAKS USING THE COVERAGE OF THE K4ME3 PROMO PEAKS. Retain those peaks that have to be removed from k4me1 distal peaks
# =============================================================================================================================================
# define an empty list for k4me3_distal_out peaks
k4me3_distal_out_list <- list()
# for each condition filter the k4me3 distal peaks
for (i in 1:length(conditions)) {
# take the paths to the bed and bw files
k4me3_distal <- k4me3_beds_distal[str_detect(k4me3_beds_distal, conditions[i])]
k4me3_promo <- k4me3_beds_promo[str_detect(k4me3_beds_promo, conditions[i])]
k4me3_bw <- k4me3_bws[str_detect(k4me3_bws, conditions[i])]
# compute coverage in distal and promo peaks
k4me3_coverage_distal <- get_coverage(bigwig_file = k4me3_bw, annotation = k4me3_distal, op = coverage_op)
k4me3_coverage_promo <- get_coverage(bigwig_file = k4me3_bw, annotation = k4me3_promo, op = coverage_op)
# filter distal peaks and retain those
# distal peaks with higher score than median score in promo peaks
if(filter_by_promo_score == "mean"){
mean_promo <- k4me3_coverage_promo$score %>% mean
k4me3_distal_out <- k4me3_coverage_distal %>% as_tibble() %>% dplyr::filter(score >= mean_promo)
}
# distal peaks with higher score than quantile x in promo peaks
else if(filter_by_promo_score %in% c("mean", "min", "q0", "q1", "median", "q2", "q3", "max", "q4")){
quantiles_promo <- k4me3_coverage_promo$score %>% quantile()
quantiles_distal <- k4me3_coverage_distal$score %>% quantile()
if(filter_by_promo_score %in% c("min", "q0")) { k4me3_distal_out <- k4me3_coverage_distal %>% as_tibble() %>% dplyr::filter(score >= quantiles_promo[1]) }
else if(filter_by_promo_score == "q1") { k4me3_distal_filt <- k4me3_distal_out %>% as_tibble() %>% dplyr::filter(score >= quantiles_promo[2]) }
else if(filter_by_promo_score %in% c("median", "q2")) { k4me3_distal_out <- k4me3_coverage_distal %>% as_tibble() %>% dplyr::filter(score >= quantiles_promo[3]) }
else if(filter_by_promo_score == "q3") { k4me3_distal_filt <- k4me3_distal_out %>% as_tibble() %>% dplyr::filter(score >= quantiles_promo[4]) }
else if(filter_by_promo_score %in% c("max", "q4")) { k4me3_distal_out <- k4me3_coverage_distal %>% as_tibble() %>% dplyr::filter(score >= quantiles_promo[5]) }
}
# distal peaks with higher score than percentile x in promo peaks
else if(is.numeric(filter_by_promo_score)){
percentil_promo <- k4me3_coverage_promo$score %>% quantile(probs = c(filter_by_promo_score))
k4me3_distal_out <- k4me3_coverage_distal %>% as_tibble() %>% dplyr::filter(score >= percentil_promo) %>%
dplyr::mutate(condition = conditions[i])
} # end of if/else (k4me3 filtering out)
# append each k4me3_distal_filt to the list
k4me3_distal_out_list[[conditions[i]]] <- k4me3_distal_out %>% mutate(condition = conditions[i])
} # end of loop for
# FILTER THE K4me1 DISTAL PEAKS USING filter_by_non_overlaps() WITH THE K4me3_distal_out peaks.
# =============================================================================================
# define an empty list for k4me3_distal_out peaks
k4me1_distal_filt_list <- list()
# for each condition filter_by_non_overlaps() with the k4me3_distal_out_peaks
for(i in 1:length(conditions)){
name <- conditions[i]
k4me1_distal_filt <- plyranges::filter_by_non_overlaps(x = k4me1_distal %>% bind_rows() %>% dplyr::filter(condition == name) %>% as_granges(),
y = k4me3_distal_out_list %>% bind_rows() %>% dplyr::filter(condition == name) %>% as_granges())
k4me1_distal_filt_list[[name]] <- k4me1_distal_filt %>% as_tibble()
}
# OVERLAP THE MODS IN EACH CONDITION TO DEFINE ACTIVE, PRIMED AND POISED ENHANCERS (k27me3_distal not null)
# =========================================================================================================
# overlap the mods of each condition to define active, primed, and poised enhancers
if(!is.null(k27me3_distal)){
# define empty lists
active_enhancers <- list()
primed_enhancers <- list()
poised_enhancers <- list()
# for each condition
for(i in 1:length(conditions)){
# name of the conditions
name <- conditions[i]
# active enhancers
active <- plyranges::filter_by_overlaps(x = k4me1_distal_filt_list %>% bind_rows() %>% dplyr::filter(condition == name) %>% as_granges(),
y = k27ac_distal %>% bind_rows() %>% dplyr::filter(condition == name) %>% as_granges()) %>%
plyranges::filter_by_non_overlaps(y = k27me3_distal %>% bind_rows() %>% dplyr::filter(condition == name) %>% as_granges())
# primed enhancers
primed <- plyranges::filter_by_non_overlaps(x = k4me1_distal_filt_list %>% bind_rows() %>% dplyr::filter(condition == name) %>% as_granges(),
y = k27ac_distal %>% bind_rows() %>% dplyr::filter(condition == name) %>% as_granges()) %>%
plyranges::filter_by_non_overlaps(y = k27me3_distal %>% bind_rows() %>% dplyr::filter(condition == name) %>% as_granges())
# poised enhancers
poised <- plyranges::filter_by_overlaps(x = k4me1_distal_filt_list %>% bind_rows() %>% dplyr::filter(condition == name) %>% as_granges(),
y = k27me3_distal %>% bind_rows() %>% dplyr::filter(condition == name) %>% as_granges()) %>%
plyranges::filter_by_non_overlaps(y = k27ac_distal %>% bind_rows() %>% dplyr::filter(condition == name) %>% as_granges())
# fill lists
active_enhancers[[paste(name, "active", sep = "-")]] <- active %>% as.data.frame() %>% dplyr::mutate(enhancer = "Active")
primed_enhancers[[paste(name, "primed", sep = "-")]] <- primed %>% as.data.frame() %>% dplyr::mutate(enhancer = "Primed")
poised_enhancers[[paste(name, "poised", sep = "-")]] <- poised %>% as.data.frame() %>% dplyr::mutate(enhancer = "Poised")
}
# merge active, primed and poised
all_enhancers <- c(active_enhancers, primed_enhancers, poised_enhancers)
}
# OVERLAP THE MODS IN EACH CONDITION TO DEFINE ACTIVE AND PRIMED ENHANCERS (k27me3_distal IS null)
# ================================================================================================
else {
# define empty lists
active_enhancers <- list()
primed_enhancers <- list()
for(i in 1:length(conditions)){
# name of the conditions
name <- conditions[i]
# active enhancers
active <- plyranges::filter_by_overlaps(x = k4me1_distal_filt_list %>% bind_rows() %>% dplyr::filter(condition == name) %>% as_granges(),
y = k27ac_distal %>% bind_rows() %>% dplyr::filter(condition == name) %>% as_granges())
# primed enhancers
primed <- plyranges::filter_by_non_overlaps(x = k4me1_distal_filt_list %>% bind_rows() %>% dplyr::filter(condition == name) %>% as_granges(),
y = k27ac_distal %>% bind_rows() %>% dplyr::filter(condition == name) %>% as_granges())
# fill lists
active_enhancers[[paste(name, "active", sep = "-")]] <- active %>% as.data.frame() %>% dplyr::mutate(enhancer = "Active")
primed_enhancers[[paste(name, "primed", sep = "-")]] <- primed %>% as.data.frame() %>% dplyr::mutate(enhancer = "Primed")
}
# merge active, primed and poised
all_enhancers <- c(active_enhancers, primed_enhancers)
}
# return
return(all_enhancers)
}