-
Notifications
You must be signed in to change notification settings - Fork 1
/
checks.R
257 lines (225 loc) · 7.35 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
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
#' @title Checks functions
#'
#' @name checks
#' @rdname checks
#'
#' @description
#'
#' Internal functions to validate the nature/structure of (m)cool files or
#' `HiCExperiment` objects.
#' All these check functions should return a logical.
#'
#' @param path Path of a (m)cool file
#' @param contacts A `HiCExperiment` object
#' @param use.scores Name of scores to check
#' @param resolution Resolution
#' @param pair Pairs object with length of 1
#' @param bed Path to regions file generated by HiC-Pro
#' @param ... `HiCExperiment` object, arguments passed on by other functions
#' @return Logical
#' @keywords internal
NULL
################################################################################
######################### CHECKS FOR COOL-BASED HICEXPERIMENTS #################
################################################################################
#' @rdname checks
.check_cool_file <- function(path) {
if (!file.exists(path) | {
isTRUE(nzchar(Sys.readlink(path), keepNA = TRUE)) &
!file.exists(Sys.readlink(path))
}) {
stop('File not found. Aborting now')
}
if (!{.is_cool(path) | .is_mcool(path)}) {
stop('Provided file is not a .cool/.mcool file.\n Aborting now.')
}
TRUE
}
#' @rdname checks
.check_cool_format <- function(path, resolution, ...) {
if (.is_mcool(path)) {
res <- .lsCoolResolutions(path)
if (is.null(resolution)) {
stop("File is in .mcool format, a resolution must be provided.\n", paste0(' Available resolutions: ', paste0(res, collapse = ', '), '.'))
}
if (!resolution %in% res) {
stop("Resolution not stored in cool file.\n", paste0(' Available resolutions: ', paste0(res, collapse = ', '), '.'))
}
}
if (.is_cool(path)) {
correctRes <- .lsCoolResolutions(path)
if (!is.null(resolution)) {
if (resolution != correctRes) {
stop("File is in .cool format, please do not specify any resolution. Aborting now.")
}
}
}
TRUE
}
#' @rdname checks
.is_mcool <- function(path) {
if (!.is_hdf5(path)) return(FALSE)
x <- .lsCoolFiles(path)
all(grepl('^/resolutions', x))
}
#' @rdname checks
.is_cool <- function(path) {
if (!.is_hdf5(path)) return(FALSE)
x <- .lsCoolFiles(path)
all(!grepl('^/resolutions', x))
}
.is_hdf5 <- function(path) {
.Call("_H5Fis_hdf5", path, PACKAGE = 'rhdf5')
}
################################################################################
######################### CHECKS FOR HIC-BASED HICEXPERIMENTS ##################
################################################################################
#' @rdname checks
.check_hic_file <- function(path) {
if (!file.exists(path) | {
isTRUE(nzchar(Sys.readlink(path), keepNA = TRUE)) &
!file.exists(Sys.readlink(path))
}) {
stop('File not found. Aborting now')
}
if (!{.is_hic(path)}) {
stop('Provided file is not a .hic file.\n Aborting now.')
}
TRUE
}
#' @rdname checks
.check_hic_format <- function(path, resolution, ...) {
res <- .lsHicResolutions(path)
if (is.null(resolution)) {
stop("File is in .hic format, a resolution must be provided.\n", paste0(' Available resolutions: ', paste0(res, collapse = ', '), '.'))
}
if (!resolution %in% res) {
stop("Resolution not stored in .hic file.\n", paste0(' Available resolutions: ', paste0(res, collapse = ', '), '.'))
}
TRUE
}
#' @rdname checks
.is_hic <- function(path) {
path <- gsub('~', Sys.getenv('HOME'), path)
tryCatch(
expr = {strawr::readHicChroms(path); TRUE},
error = function(e) {
FALSE
},
warning = function(e) {
FALSE
}
)
}
################################################################################
######################### CHECKS FOR HICPRO-BASED HICEXPERIMENTS ###############
################################################################################
#' @rdname checks
.check_hicpro_files <- function(path, bed) {
if (!file.exists(path) | {
isTRUE(nzchar(Sys.readlink(path), keepNA = TRUE)) &
!file.exists(Sys.readlink(path))
}) {
stop('Matrix file not found. Aborting now')
}
if (!{.is_hicpro_matrix(path)}) {
stop('Provided matrix is not an HiC-Pro file.\n Aborting now.')
}
if (!is.null(bed)) {
if (!file.exists(bed) | {
isTRUE(nzchar(Sys.readlink(bed), keepNA = TRUE)) &
!file.exists(Sys.readlink(bed))
}) {
stop('Regions file not found. Aborting now')
}
if (!{.is_hicpro_regions(bed)}) {
stop('Provided regions are not an HiC-Pro file.\n Aborting now.')
}
}
TRUE
}
#' @rdname checks
.is_hicpro_matrix <- function(path) {
tryCatch(
expr = {
x <- vroom::vroom(
file = path,
col_names = FALSE,
n_max = 1000,
show_col_types = FALSE,
progress = FALSE
)
{
is.numeric(x[[1]]) &
is.numeric(x[[2]]) &
is.numeric(x[[3]]) &
ncol(x) == 3 &
all(x >= 0)
}
},
error = function(e) {
FALSE
},
warning = function(e) {
FALSE
}
)
}
#' @rdname checks
.is_hicpro_regions <- function(bed) {
tryCatch(
expr = {
x <- vroom::vroom(
file = bed,
col_names = FALSE,
n_max = 1000,
show_col_types = FALSE,
progress = FALSE
)
{
{is.numeric(x[[1]]) | is.character(x[[1]])} &
is.numeric(x[[2]]) &
is.numeric(x[[3]]) &
{ncol(x) == 3 | ncol(x) == 4} &
all(x[, c(2, 3)] >= 0) &
all(x[,3] > x[,2]) &
length(unique(x[,3] - x[,2]))
}
},
error = function(e) {
FALSE
},
warning = function(e) {
FALSE
}
)
}
################################################################################
######################### CHECKS FOR HICEXPERIMENT OBJECTS #####################
################################################################################
#' @rdname checks
.check_resolution <- function(contacts, resolution) {
availableRes <- resolutions(contacts)
if (!resolution %in% availableRes)
stop(paste0("Resolution not stored in the matrix file.\n Available resolutions: ", paste0(availableRes, collapse = ', '), '.'))
TRUE
}
#' @rdname checks
.check_scores <- function(contacts, use.scores) {
nscores <- names(scores(contacts))
if (!use.scores %in% nscores)
stop(paste0("Queried scores not found."))
TRUE
}
################################################################################
######################### OTHER CHECKS #########################################
################################################################################
#' @rdname checks
.is_square <- function(pair) {
w1 <- GenomicRanges::width(S4Vectors::first(pair))
w2 <- GenomicRanges::width(S4Vectors::second(pair))
if (w1 != w2) {
stop("Provided pair is not square.")
}
TRUE
}