-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5acdf98
commit f1eb5b4
Showing
1 changed file
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#' Match bed-like entries to Hi-C bin indices | ||
#' | ||
#' @inheritParams PESCAn | ||
#' @param mode A \code{character} of length 1 indicating what position of the | ||
#' \code{bed} argument to match with the indices. Possible values: | ||
#' \code{"center"}, \code{"start"} or \code{"end"}. | ||
#' | ||
#' @return An \code{integer} vector of length \code{nrow(bed)} and parallel to | ||
#' \code{bed} with indices to the Hi-C matrix. | ||
#' | ||
#' @details Out of bounds values are matched to nearest bin. | ||
#' @export | ||
bed2idx <- function(ABS, bed, mode = c("centre", "start", "end")) { | ||
# American/British spelling | ||
mode <- gsub("center", "centre", mode) | ||
mode <- match.arg(mode, c("centre", "start", "end")) | ||
|
||
# Reformat bed depending on mode | ||
bed <- cbind.data.frame( | ||
V1 = bed[, 1], | ||
V2 = switch(mode, | ||
"centre" = (bed[, 2] + bed[, 3]) / 2, | ||
"start" = bed[, 2], | ||
"end" = bed[, 3] | ||
) | ||
) | ||
|
||
# Assign entries to shared chromosomes | ||
chroms <- intersect(ABS[, 1], bed[, 1]) | ||
bed_group <- match(bed[, 1], chroms) | ||
ABS_group <- match(ABS[, 1], chroms) | ||
|
||
# Split by chromosome | ||
bed_chrom <- split(bed[, 2], bed_group) | ||
ABS_chrom <- split(ABS[, c(2, 4)], ABS_group) | ||
|
||
# Match bed entry to idx | ||
out <- mapply(function(i, j) { | ||
j[pmax(findInterval(i, j[, 1]), 1), 2] | ||
}, i = bed_chrom, j = ABS_chrom) | ||
unsplit(out, bed_group) | ||
} | ||
|
||
|
||
# taken from ggplot | ||
try_require <- function(package, fun, source = NULL) { | ||
if (requireNamespace(package, quietly = TRUE)) { | ||
return(invisible()) | ||
} | ||
|
||
if(source == 'BIOC'){ | ||
stop("Package `", package, "` required for `", fun , "`.\n", | ||
"Please install from Bioconductor and try again.", call. = FALSE) | ||
} else if(source == 'github'){ | ||
stop("Package `", package, "` required for `", fun , "`.\n", | ||
"Please install from github and try again.", call. = FALSE) | ||
} else { | ||
stop("Package `", package, "` required for `", fun , "`.\n", | ||
"Please install and try again.", call. = FALSE) | ||
} | ||
|
||
} |