Skip to content

Commit

Permalink
Normalise all input paths before passing to rmarkdown::render()
Browse files Browse the repository at this point in the history
  • Loading branch information
HDash committed Jul 10, 2024
1 parent 30d520b commit a7954dd
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 19 deletions.
15 changes: 12 additions & 3 deletions R/MotifPeeker.R
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,15 @@ MotifPeeker <- function(
force(genome_build)

### Check argument lengths ###
if (!is.null(alignment_files) &&
length(peak_files) != length(alignment_files)) {
if (!is.null(alignment_files)) {
stp_msg <- paste("Length of", shQuote("peak_files"), "and",
shQuote("alignment_files"), "must be equal.")
stopper(stp_msg)
len_peak_files <- if (is.list(peak_files)) length(peak_files) else 1
len_alignment_files <- if (is.list(alignment_files))
length(alignment_files) else 1
if (len_peak_files != len_alignment_files) {
stopper(stp_msg)
}
}
if (!is.null(cell_counts) && length(cell_counts) != length(peak_files)) {
stp_msg <- paste0("Length of ", shQuote("cell_counts"), " must be ",
Expand Down Expand Up @@ -212,7 +216,12 @@ MotifPeeker <- function(
paste0("MotifPeeker_", format(Sys.time(), "%Y%m%d_%H%M%S"))
)
dir.create(out_dir, showWarnings = debug)

### Normalise input paths ###
out_dir <- normalizePath(out_dir)
peak_files <- normalise_paths(peak_files)
alignment_files <- normalise_paths(alignment_files)
motif_files <- normalise_paths(motif_files)

### Store arguments in a list ###
args_list <- list(
Expand Down
1 change: 1 addition & 0 deletions R/check_ENCODE.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#'
#' @export
check_ENCODE <- function(encode_id, expect_format, verbose = FALSE) {
if (!all(is.character(encode_id))) return(encode_id)
### Validate ENCODE ID ###
id_pattern <- "^ENC(SR|BS|DO|GM|AB|LB|FF|PL)\\d{3}[A-Z]{3}$"
if (!all(grepl(id_pattern, encode_id))) return(encode_id)
Expand Down
23 changes: 23 additions & 0 deletions R/normalise_paths.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#' Apply \code{\link[base]{normalizePath}} to a list of paths
#'
#' @param path_list A list of paths.
#'
#' @return A list of normalised paths or the input as is if contents are not
#' a character.
#'
#' @keywords internal
normalise_paths <- function(path_list) {
if (all(is.null(path_list))) return(path_list)

## Convert objects to list if not already
if (!is.list(path_list) &&(!is.vector(path_list) || length(path_list) == 1))
path_list <- list(path_list)

## Return input as is if not character
if (!all(sapply(path_list, is.character))) return(path_list)

## Normalise paths
lapply(path_list, function(path) {
normalizePath(path)
})
}
17 changes: 3 additions & 14 deletions inst/markdown/MotifPeeker.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -80,20 +80,11 @@ result <- list(
peaks = Vectorize(read_peak_file, "peak_file")(peak_files_encode),
alignments = lapply(alignment_files_encode, Rsamtools::BamFile),
exp_labels = params$exp_labels,
exp_type = unname(Vectorize(format_exptype,
"exp_type")(exp_types))
exp_type = unname(Vectorize(format_exptype, "exp_type")(exp_types))
)
motif_files_jaspar <- if (motif_len > 1) {
Vectorize(check_JASPAR, "motif_id")(params$motif_files)
} else {
check_JASPAR(params$motif_files)
}
motif_files_jaspar <- Vectorize(check_JASPAR, "motif_id")(params$motif_files)
user_motifs <- list(
motifs = if (motif_len > 1) {
Vectorize(read_motif_file, "motif_file")(motif_files_jaspar)
} else if (motif_len == 1) {
read_motif_file(motif_files_jaspar)
},
motifs = Vectorize(read_motif_file, "motif_file")(motif_files_jaspar),
motif_labels = params$motif_labels
)
result$alignments <- unname(result$alignments)
Expand Down Expand Up @@ -152,8 +143,6 @@ peak_width_df <-
peak_width = unlist(result$peak_widths))
## Motif-Summit Distances
if (length(user_motifs$motifs) == 1) user_motifs$motifs <-
list(user_motifs$motifs)
motif_summit_dist_df <- get_df_distances(
result, user_motifs, genome_build, out_dir_extra, params$workers,
params$meme_path, params$debug
Expand Down
19 changes: 19 additions & 0 deletions man/normalise_paths.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions tests/testthat/test-MotifPeeker.R
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,17 @@ test_that("MotifPeeker produces output files", {
expect_true(file.exists(file.path(out_dir, "MotifPeeker.html")))

## Single set of files
## Also, try with built-in data
data("CTCF_ChIP_peaks", package = "MotifPeeker")
data("motif_MA1930.2", package = "MotifPeeker")
out_dir <- MotifPeeker(
peak_files = peaks[1],
peak_files = CTCF_ChIP_peaks,
reference_index = 1,
alignment_files = alignments[1],
exp_labels = c("ChIP"),
exp_type = c("chipseq"),
genome_build = "hg38",
motif_files = motifs[[1]],
motif_files = motif_MA1930.2,
motif_labels = NULL,
cell_counts = NULL,
denovo_motif_discovery = TRUE,
Expand Down
12 changes: 12 additions & 0 deletions tests/testthat/test-normalise_paths.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
test_that("normalise_paths works", {
expect_null(normalise_paths(NULL))

## Return non-character items as-is
data("CTCF_ChIP_peaks", package = "MotifPeeker")
expect_equal(normalise_paths(CTCF_ChIP_peaks)[[1]], CTCF_ChIP_peaks)

## Return full paths from relative paths
file <- system.file("extdata", "CTCF_ChIP_alignment.bam",
package = "MotifPeeker")
expect_equal(normalise_paths(file)[[1]], file)
})

0 comments on commit a7954dd

Please sign in to comment.