Skip to content

Commit

Permalink
Modify bb_save to save bedsets
Browse files Browse the repository at this point in the history
  • Loading branch information
jwokaty committed Oct 15, 2024
1 parent 4b26d7a commit 200ffcc
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 21 deletions.
25 changes: 16 additions & 9 deletions R/bedbaser.R
Original file line number Diff line number Diff line change
Expand Up @@ -447,30 +447,37 @@ bb_to_grangeslist <- function(api, bedset_id, quietly = TRUE) {
GRangesList(gros)
}

#' Save a BED file to a path given an id
#' Save a BED or BEDset files to a path given an id
#'
#' @rdname bb_save
#'
#' @param api API object of BEDbase created from BEDbase()
#' @param bed_id integer() BED record identifier
#' @param path character() to save file
#' @param bed_or_bedset_id integer() BED or BEDset record identifier
#' @param path character() directory to save file
#' @param file_type character() (default bed) bed, bigbed, etc.
#' @param access_type character() (default http) s3 or http
#' @param quietly logical() (default TRUE) display messages
#'
#' @return character() file path
#'
#' @examples
#' api <- BEDbase()
#' ex <- bb_example(api, "bed")
#' bb_save(api, ex$id, tempdir())
#'
#' @export
bb_save <- function(
api, bed_id, path, file_type = "bed", access_type = "http",
api, bed_or_bedset_id, path, file_type = "bed", access_type = "http",
quietly = TRUE)
{
metadata <- bb_metadata(api, bed_id, TRUE)
save_path <- .get_file(metadata, file_type, access_type, path, quietly)

if (!dir.exists(path))
rlang::abort(paste(path, "doesn't exist.", sep = " "))
metadata <- bb_metadata(api, bed_or_bedset_id, TRUE)
if ("bedsets" %in% names(metadata)) {
ids <- list(metadata$id)
} else {
ids <- metadata$bed_ids
}
for (id in ids) {
metadata <- bb_metadata(api, id, TRUE)
.get_file(metadata, file_type, access_type, path, quietly)
}
}
13 changes: 5 additions & 8 deletions man/bb_save.Rd

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

13 changes: 9 additions & 4 deletions tests/testthat/test-bedbaser.R
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,15 @@ test_that("bb_to_grangeslist creates a GRangesList", {
expect_equal(10, length(grl))
})

test_that("bb_save saves a bed file to a path", {
test_that("bb_save saves bed files to a path", {
api <- BEDbase()
path <- tempdir()
id <- bb_example(api, "bed")$id
saved_file <- bb_save(api, id, path, quietly = TRUE)
expect_true(file.exists(saved_file))
dir.create(path)
bed <- bb_example(api, "bed")
bb_save(api, bed$id, path, quietly = TRUE)
expect_true(file.exists(file.path(path, paste0(bed$id, ".bed.gz"))))
bedset <- bb_example(api, "bedset")
bb_save(api, bedset$id, path, quietly = TRUE)
for (id in bedset$bed_ids)
expect_true(file.exists(file.path(path, paste0(id, ".bed.gz"))))
})

0 comments on commit 200ffcc

Please sign in to comment.