-
Notifications
You must be signed in to change notification settings - Fork 1
/
HiCExperiment-methods.R
589 lines (492 loc) · 18.9 KB
/
HiCExperiment-methods.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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
#' @title `HiCExperiment` methods
#'
#' @name HiCExperiment
#' @rdname HiCExperiment
#' @aliases resolutions,HiCExperiment-method
#' @aliases resolution,HiCExperiment-method
#' @aliases focus,HiCExperiment-method
#' @aliases focus<-,HiCExperiment-method
#' @aliases focus<-,HiCExperiment,character-method
#' @aliases zoom,HiCExperiment,numeric-method
#' @aliases refocus,HiCExperiment,character-method
#' @aliases scores,HiCExperiment-method
#' @aliases scores,HiCExperiment,missing-method
#' @aliases scores,HiCExperiment,character-method
#' @aliases scores,HiCExperiment,numeric-method
#' @aliases scores<-,HiCExperiment-method
#' @aliases scores<-,HiCExperiment,character,numeric-method
#' @aliases topologicalFeatures,HiCExperiment-method
#' @aliases topologicalFeatures,HiCExperiment,missing-method
#' @aliases topologicalFeatures,HiCExperiment,character-method
#' @aliases topologicalFeatures,HiCExperiment,numeric-method
#' @aliases topologicalFeatures<-,HiCExperiment-method
#' @aliases topologicalFeatures<-,HiCExperiment,character,GRangesOrGInteractions-method
#' @aliases pairsFile,HiCExperiment-method
#' @aliases pairsFile<-,HiCExperiment-method
#' @aliases pairsFile<-,HiCExperiment,character-method
#' @aliases metadata<-,HiCExperiment-method
#' @aliases metadata<-,HiCExperiment,list-method
#' @aliases fileName,HiCExperiment-method
#' @aliases interactions,HiCExperiment-method
#' @aliases interactions<-,HiCExperiment-method
#' @aliases length,HiCExperiment-method
#' @aliases [,HiCExperiment-method
#' @aliases [,HiCExperiment,numeric,ANY,ANY-method
#' @aliases subsetByOverlaps,HiCExperiment,GRanges-method
#' @aliases [,HiCExperiment,logical,ANY,ANY-method
#' @aliases [,HiCExperiment,character,ANY,ANY-method
#' @aliases [,HiCExperiment,GInteractions,ANY,ANY-method
#' @aliases [,HiCExperiment,Pairs,ANY,ANY-method
#' @aliases seqinfo,HiCExperiment-method
#' @aliases bins,HiCExperiment-method
#' @aliases anchors,HiCExperiment-method
#' @aliases regions,HiCExperiment-method
#' @aliases cis,HiCExperiment-method
#' @aliases trans,HiCExperiment-method
#' @aliases show,HiCExperiment-method
#'
#' @param x A \code{HiCExperiment} object.
#' @param object A \code{HiCExperiment} object.
#' @param name Name of the element to access in topologicalFeatures or scores SimpleLists.
#' @param value Value to add to topologicalFeatures, scores, pairsFile or metadata slots.
#' @param i,ranges a GRanges, coordinates in character, or boolean vector to subset a HiCExperiment
#' @param type any of `within` or `any`, to subset interactions by overlap with a provided GRanges.
#' @param fillout.regions Whehter to add missing regions to GInteractions' regions?
#'
#' @importMethodsFrom BiocGenerics fileName
#' @importFrom GenomeInfoDb seqinfo
#'
#' @include AllGenerics.R
NULL
################################################################################
################################################################################
############### ###############
############### METHODS FOR NEW GENERICS ###############
############### ###############
################################################################################
################################################################################
#' @export
#' @rdname HiCExperiment
setMethod("resolutions", "HiCExperiment", function(x) x@resolutions)
#' @export
#' @rdname HiCExperiment
setMethod("resolution", "HiCExperiment", function(x) x@resolution)
#' @export
#' @rdname HiCExperiment
setMethod("focus", "HiCExperiment", function(x) x@focus)
#' @export
#' @rdname HiCExperiment
setMethod("focus<-", signature(x = "HiCExperiment", value = "character"), function(x, value) {
x@focus <- value
x
})
#' @export
#' @rdname HiCExperiment
setMethod("zoom", c("HiCExperiment", "numeric"), function(x, resolution) {
HiCExperiment(
fileName(x),
resolution = as.integer(resolution),
focus = focus(x),
metadata = S4Vectors::metadata(x),
topologicalFeatures = topologicalFeatures(x),
pairsFile = pairsFile(x)
)
})
#' @export
#' @rdname HiCExperiment
setMethod("refocus", c("HiCExperiment", "character"), function(x, focus) {
if (.is_cool(fileName(x))) {
res <- NULL
} else {
res <- resolution(x)
}
HiCExperiment(
fileName(x),
resolution = res,
focus = focus,
metadata = S4Vectors::metadata(x),
topologicalFeatures = topologicalFeatures(x),
pairsFile = pairsFile(x)
)
})
#' @export
#' @rdname HiCExperiment
setMethod("scores", signature(x = "HiCExperiment", name = "missing"), function(x) x@scores)
#' @export
#' @rdname HiCExperiment
setMethod("scores", signature(x = "HiCExperiment", name = "character"), function(x, name) {
.check_scores(x, name)
return(x@scores[[name]])
})
#' @export
#' @rdname HiCExperiment
setMethod("scores", signature(x = "HiCExperiment", name = "numeric"), function(x, name) {
if (name > length(scores(x))) {
stop(paste0('Only ', length(scores(x)), ' scores in x.'))
}
return(x@scores[[name]])
})
#' @export
#' @rdname HiCExperiment
setMethod("scores<-", c(x = "HiCExperiment", name = "character", value = "numeric"), function(x, name, value) {
x@scores[[name]] <- value
return(x)
})
#' @export
#' @rdname HiCExperiment
setMethod("topologicalFeatures", signature(x = "HiCExperiment", name = "missing"), function(x) {
S4Vectors::SimpleList(as.list(x@topologicalFeatures))
})
#' @export
#' @rdname HiCExperiment
setMethod("topologicalFeatures", signature(x = "HiCExperiment", name = "character"), function(x, name) {
if (!name %in% names(topologicalFeatures(x))) {
stop(paste0(name, ' not in topologicalFeatures.'))
}
x@topologicalFeatures[[name]]
})
#' @export
#' @rdname HiCExperiment
setMethod("topologicalFeatures", signature(x = "HiCExperiment", name = "numeric"), function(x, name) {
if (name > length(topologicalFeatures(x))) {
stop(paste0('Only ', length(topologicalFeatures(x)), ' topologicalFeatures in x.'))
}
x@topologicalFeatures[[name]]
})
#' @export
#' @rdname HiCExperiment
setMethod("topologicalFeatures<-", signature(x = "HiCExperiment", name = "character", value = "GRangesOrGInteractions"), function(x, name, value) {
x@topologicalFeatures[[name]] <- value
return(x)
})
#' @export
#' @rdname HiCExperiment
setMethod("pairsFile", "HiCExperiment", function(x) {
x@pairsFile
})
#' @export
#' @rdname HiCExperiment
setMethod("pairsFile<-", signature(x = "HiCExperiment", value = "character"), function(x, value) {
if (!file.exists(value)) {
stop("Provided pairsFile does not exist. Aborting now.")
}
x@pairsFile <- value
x
})
#' @export
#' @rdname HiCExperiment
setMethod("metadata<-", signature(x = "HiCExperiment", value = "list"), function(x, value) {
x@metadata <- value
x
})
################################################################################
################################################################################
############### ###############
############### METHODS FOR SUBSET ###############
############### ###############
################################################################################
################################################################################
#' @export
#' @rdname HiCExperiment
setMethod("subsetByOverlaps", signature = c(x="HiCExperiment", ranges="numeric"), function(x, ranges) {
subints <- interactions(x)[ranges]
subre <- IRanges::subsetByOverlaps(regions(x), subints)
InteractionSet::replaceRegions(subints) <- subre
interactions(x) <- subints
for (n in names(scores(x))) {
scores(x, n) <- scores(x, n)[ranges]
}
return(x)
})
#' @export
#' @rdname HiCExperiment
setMethod("subsetByOverlaps", signature = c(x="HiCExperiment", ranges="logical"), function(x, ranges) {
# Redirect to subsetByOverlaps,HiCExperiment,numeric-method
x[which(ranges)]
})
#' @export
#' @rdname HiCExperiment
setMethod("subsetByOverlaps", signature = c(x="HiCExperiment", ranges="GRanges"), function(x, ranges, type = c('within', 'any')) {
type <- match.arg(type)
if (type == "any") { ## Anchors not constrained within segment
i <- IRanges::overlapsAny(
interactions(x), ranges, type = 'within'
)
# Redirect to subsetByOverlaps,HiCExperiment,numeric-method
return(x[i])
}
else if (type == "within") { ## Anchors constrained within segment, for each range
# Redirect to subsetByOverlaps,HiCExperiment,GInteractions-method
subsetByOverlaps(x, InteractionSet::GInteractions(ranges, ranges))
}
else {
stop("'type' should be one of \"any\", \"both\"")
}
})
#' @export
#' @rdname HiCExperiment
setMethod("subsetByOverlaps", signature = c(x="HiCExperiment", ranges="GInteractions"), function(x, ranges) {
ranges <- swapAnchors(ranges)
ints <- interactions(x)
an <- anchors(ints)
i <- lapply(seq_along(ranges), function(k) {
which(IRanges::overlapsAny(
an[["first"]], anchors(ranges[k], "first"), type = 'within'
) & IRanges::overlapsAny(
an[["second"]], anchors(ranges[k], "second"), type = 'within'
))
}) |> unlist() |> unique()
# Redirect to subsetByOverlaps,HiCExperiment,numeric-method
return(x[i])
})
#' @export
#' @rdname HiCExperiment
setMethod("subsetByOverlaps", signature = c(x="HiCExperiment", ranges="Pairs"), function(x, ranges) {
gi <- GInteractions(S4Vectors::first(ranges), S4Vectors::second(ranges))
# Redirect to subsetByOverlaps,HiCExperiment,GInteractions-method
return(x[gi])
})
#' @export
#' @rdname HiCExperiment
setMethod("[", signature("HiCExperiment", "numeric"), function(x, i) {
# Redirect to subsetByOverlaps,HiCExperiment,numeric-method
subsetByOverlaps(x, i)
})
#' @export
#' @rdname HiCExperiment
setMethod("[", signature("HiCExperiment", "GRanges"), function(x, i) {
# Redirect to subsetByOverlaps,HiCExperiment,GRanges-method
subsetByOverlaps(x, i)
})
#' @export
#' @rdname HiCExperiment
setMethod("[", signature("HiCExperiment", "logical"), function(x, i) {
# Redirect to subsetByOverlaps,HiCExperiment,logical-method
subsetByOverlaps(x, i)
})
#' @export
#' @rdname HiCExperiment
setMethod("[", signature("HiCExperiment", "GInteractions"), function(x, i) {
# Redirect to subsetByOverlaps,HiCExperiment,GInteractions-method
subsetByOverlaps(x, i)
})
#' @export
#' @rdname HiCExperiment
setMethod("[", signature("HiCExperiment", "Pairs"), function(x, i) {
# Redirect to subsetByOverlaps,HiCExperiment,Pairs-method
subsetByOverlaps(x, i)
})
#' @export
#' @rdname HiCExperiment
setMethod("[", signature("HiCExperiment", "character"), function(x, i) {
re_ <- regions(x)
ints_ <- interactions(x)
# Different possible situations:
# 'II:10001-20000' --> Redirects to GRanges method
# 'II' --> Redirects to GRanges method
# 'II:10001-20000|III:50001-90000' --> Redirects to GInteractions method
# 'II|III' --> Redirects to GInteractions method
# c('II', 'III') --> Redirects to GRanges method
if (length(i) == 1) { # 'II:10001-20000', 'II:10001-20000|III:50000-90000', 'II' or 'II|III'
if (
grepl(
'.*:[0-9]*-[0-9]*\\|[A-Za-z0-9]*:[0-9]*-[0-9]*$', i
) | grepl(
'.*:[0-9]*-[0-9]*$', i
)
) { # e.g. 'II:10001-20000' or 'II:10001-20000|III:50000-90000'
i_ <- char2coords(i)
valid_regions_first <- subsetByOverlaps(
re_, S4Vectors::first(i_), type = 'within'
)$bin_id
valid_regions_second <- subsetByOverlaps(
re_, S4Vectors::second(i_), type = 'within'
)$bin_id
}
else if (
grepl(
'[^:]*\\|[^:]*$', i
)
) { # e.g. 'II|III'
chr1 <- strsplit(i, '\\|')[[1]][1]
chr2 <- strsplit(i, '\\|')[[1]][2]
if (!all(c(chr1, chr2) %in% seqnames(GenomeInfoDb::seqinfo(x)))) {
stop("One or all of the provided seqnames is not found.")
}
si <- GenomeInfoDb::seqinfo(x)
gr1 <- as(si[chr1], 'GRanges')
gr2 <- as(si[chr2], 'GRanges')
i_ <- paste(as.character(gr1), as.character(gr2), sep = '|')
# Redirect to character (with extended i_ being like "II:xxx-xxx|III:xxx-xxx")
return(x[i_])
}
else if (
all(i %in% seqnames(GenomeInfoDb::seqinfo(x)))
) { # e.g. 'II'
valid_regions_first <- re_$bin_id[as.vector(seqnames(re_)) %in% i]
valid_regions_second <- valid_regions_first
}
else {
stop("Failed to coerce i into a GRanges or GInteractions")
}
focus(x) <- i
}
else { # c('II', 'III')
if (
all(i %in% seqnames(GenomeInfoDb::seqinfo(x)))
) {
valid_regions_first <- re_$bin_id[as.vector(seqnames(re_)) %in% i]
valid_regions_second <- valid_regions_first
}
else {
stop("Failed to coerce i into a GRanges or GInteractions")
}
focus(x) <- paste(i, collapse = ', ')
}
sub <- ints_$bin_id1 %in% valid_regions_first & ints_$bin_id2 %in% valid_regions_second
x[sub]
})
################################################################################
################################################################################
############### ###############
############### METHODS FOR EXISTING GENERICS ###############
############### ###############
################################################################################
################################################################################
#' @export
#' @rdname HiCExperiment
setMethod("fileName", "HiCExperiment", function(object) object@fileName)
#' @export
#' @rdname HiCExperiment
setMethod("interactions", signature(x = "HiCExperiment"), function(x, fillout.regions = FALSE) {
gi <- x@interactions
n <- names(scores(x))
for (N in n) {
S4Vectors::mcols(gi)[[N]] <- scores(x, N)
}
if (fillout.regions) {
re <- bins(x)
subre <- re[re$bin_id >= min(regions(gi)$bin_id) & re$bin_id <= max(regions(gi)$bin_id)]
replaceRegions(gi) <- subre
}
return(gi)
})
#' @export
#' @rdname HiCExperiment
setMethod("interactions<-", signature(x = "HiCExperiment", value = "GInteractions"), function(x, value) {
x@interactions <- value
x
})
#' @export
#' @rdname HiCExperiment
setMethod("length", "HiCExperiment", function(x) length(interactions(x)))
#' @export
#' @rdname HiCExperiment
setReplaceMethod("$", "HiCExperiment", function(x, name, value) {
S4Vectors::mcols(regions(interactions(x)))[, name] <- value
return(x)
})
#' @export
#' @rdname HiCExperiment
setMethod("$", "HiCExperiment", function(x, name) {
S4Vectors::mcols(regions(interactions(x)))[, name]
})
#' @export
#' @rdname HiCExperiment
setMethod("seqinfo", "HiCExperiment", function(x) {
if (.is_mcool(fileName(x))) {
si <- .cool2seqinfo(fileName(x), resolution(x))
}
else if (.is_cool(fileName(x))) {
si <- .cool2seqinfo(fileName(x))
}
else if (.is_hic(fileName(x))) {
si <- .hic2seqinfo(fileName(x))
}
else if (.is_hicpro_matrix(fileName(x)) & .is_hicpro_regions(metadata(x)$regions)) {
si <- .hicpro2seqinfo(metadata(x)$regions)
}
return(si)
})
#' @export
#' @rdname HiCExperiment
setMethod("bins", "HiCExperiment", function(x) {
if (.is_cool(fileName(x))) {
bins <- .getCoolAnchors(
fileName(x), resolution = NULL, balanced = TRUE
)
GenomeInfoDb::seqinfo(bins) <- GenomeInfoDb::seqinfo(x)
}
else if (.is_mcool(fileName(x))) {
bins <- .getCoolAnchors(
fileName(x), resolution = resolution(x), balanced = TRUE
)
GenomeInfoDb::seqinfo(bins) <- GenomeInfoDb::seqinfo(x)
}
else if (.is_hic(fileName(x))) {
bins <- .getHicAnchors(
fileName(x), resolution = resolution(x)
)
GenomeInfoDb::seqinfo(bins) <- GenomeInfoDb::seqinfo(x)
}
else if (.is_hicpro_matrix(fileName(x)) & .is_hicpro_regions(metadata(x)$regions)) {
bins <- .getHicproAnchors(metadata(x)$regions)
GenomeInfoDb::seqinfo(bins) <- GenomeInfoDb::seqinfo(x)
}
else {
stop("Input format not supported")
}
return(bins)
})
#' @export
#' @rdname HiCExperiment
setMethod("anchors", "HiCExperiment", function(x) anchors(interactions(x)))
#' @export
#' @rdname HiCExperiment
setMethod("regions", "HiCExperiment", function(x) regions(interactions(x)))
#' @export
#' @rdname HiCExperiment
setMethod("cis", "HiCExperiment", function(x) {
an <- anchors(x)
sub <- GenomicRanges::seqnames(an[[1]]) == GenomicRanges::seqnames(an[[2]])
x[which(as.vector(sub))]
})
#' @export
#' @rdname HiCExperiment
setMethod("trans", "HiCExperiment", function(x) {
an <- anchors(x)
sub <- GenomicRanges::seqnames(an[[1]]) != GenomicRanges::seqnames(an[[2]])
x[which(as.vector(sub))]
})
setMethod("show", signature("HiCExperiment"), function(object) {
if (is.null(focus(object))) {
focus_str <- "whole genome"
}
else {
focus_str <- coords2char(focus(object))
}
gi <- interactions(object)
cat(paste0(
"`HiCExperiment` object with ",
format(sum(S4Vectors::mcols(gi)[,names(scores(object))[1]], na.rm = TRUE), big.mark = ","),
" contacts over ",
format(length(regions(object)), big.mark = ","),
" regions"
), '\n')
cat('-------\n')
cat(paste0('fileName: ', ifelse(nchar(fileName(object)) >= 1, paste0('\"', fileName(object), '\"'), "N/A")), '\n')
cat(paste0('focus: ', ifelse(nchar(focus_str) >= 1, paste0('\"', focus_str, '\"'), "N/A")), '\n')
## Resolutions
S4Vectors::coolcat("resolutions(%d): %s\n", resolutions(object))
cat(paste0('active resolution: ', resolution(object)), '\n')
## Interactions
cat(paste0('interactions: ', length(gi)), '\n')
## Scores
cat(paste0('scores(', length(scores(object)), '): ', paste(names(scores(object)), collapse = " ")), '\n')
## topologicalFeatures
cat(paste0('topologicalFeatures: ', paste(paste0(names(topologicalFeatures(object)), "(", lengths(topologicalFeatures(object)), ")"), collapse = " ")), '\n')
## Pairs
cat(paste0('pairsFile: ', ifelse(is.null(pairsFile(object)), "N/A", pairsFile(object))), '\n')
## Metadata
S4Vectors::coolcat("metadata(%d): %s\n", names(S4Vectors::metadata(object)))
})