-
Notifications
You must be signed in to change notification settings - Fork 1
/
checks.R
110 lines (95 loc) · 2.39 KB
/
checks.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
#' @title Checks functions
#'
#' @name checks
#' @rdname checks
#'
#' @description
#'
#' Useful functions to validate the nature/structure of (m)cool files or
#' `HiCExperiment` objects.
#' All these check functions should return a logical.
#'
#' @param x A `HiCExperiment` object
#' @param ... `HiCExperiment` objects
#' @return Logical
#' @keywords internal
NULL
#' @rdname checks
.is_symmetrical <- function(x) {
if (is.null(focus(x))) {
return(TRUE)
}
if (grepl('\\|', focus(x))) {
return(FALSE)
}
else {
return(TRUE)
}
}
#' @rdname checks
.is_comparable <- function(...) {
.are_HiCExperiment(...)
err <- c()
if (!.is_same_seqinfo(...)) {
err <- c(err, "seqinfos")
}
if (!.is_same_resolution(...)) {
err <- c(err, "resolutions")
}
if (!.is_same_bins(...)) {
err <- c(err, "bins")
}
if (!.is_same_regions(...)) {
err <- c(err, "regions")
}
if (length(err) > 0) {
mess <- paste0("Provided `HiCExperiment` have different ", paste(err, collapse = ' & '), '.')
stop(mess)
}
TRUE
}
#' @rdname checks
.are_HiCExperiment <- function(...) {
args <- list(...)
if (!all(unlist(lapply(args, is, 'HiCExperiment')))) {
stop("Provided arguments are not all `HiCExperiment` objects.
Please only use `HiCExperiment` objects with this function.")
}
TRUE
}
#' @rdname checks
.is_same_seqinfo <- function(...) {
contacts_list <- list(...)
all(unlist(lapply(contacts_list, function(x) {
identical(seqinfo(contacts_list[[1]]), seqinfo(x))
})))
}
#' @rdname checks
.is_same_resolution <- function(...) {
contacts_list <- list(...)
all(unlist(lapply(contacts_list, function(x) {
identical(resolution(contacts_list[[1]]), resolution(x))
})))
}
#' @rdname checks
.is_same_bins <- function(...) {
contacts_list <- list(...)
all(unlist(lapply(contacts_list, function(x) {
b1 <- bins(contacts_list[[1]])
b1$weight <- NULL
b2 <- bins(x)
b2$weight <- NULL
identical(b1, b2)
})))
}
#' @rdname checks
.is_same_regions <- function(...) {
contacts_list <- list(...)
all(unlist(lapply(contacts_list, function(x) {
re1 <- regions(contacts_list[[1]])
re1$weight <- NULL
re2 <- regions(x)
re2$weight <- NULL
identical(re1, re2)
})))
}