Skip to content

Commit

Permalink
Before v1
Browse files Browse the repository at this point in the history
  • Loading branch information
jwokaty committed Apr 9, 2024
1 parent 9a5fae4 commit 07b13c9
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 122 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: bedbaser
Title: A BEDbase client
Version: 0.0.0.9000
Version: 0.0.9000
Authors@R:
person("Jennifer", "Wokaty", , "[email protected]",
role = c("aut", "cre"))
Expand All @@ -12,13 +12,13 @@ Imports:
AnVIL,
BiocFileCache,
dplyr,
glue,
httr,
purrr,
rtracklayer,
R.utils,
stringr,
tibble,
tools,
utils
Suggests:
BiocStyle,
Expand Down
5 changes: 2 additions & 3 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@

export(.BEDbase)
export(BEDbase)
export(bb_search)
export(download_file)
export(get_rec_type)
export(search_bedbase)
exportClasses(BEDbase)
exportMethods(bb_beds_in_bedset)
exportMethods(bb_count)
exportMethods(bb_genomes)
exportMethods(bb_metadata)
exportMethods(bb_records)
exportMethods(get_bb_service_info)
importFrom(AnVIL,Service)
importFrom(BiocFileCache,BiocFileCache)
importFrom(BiocFileCache,bfcadd)
Expand All @@ -20,7 +19,6 @@ importFrom(BiocFileCache,bfcneedsupdate)
importFrom(BiocFileCache,bfcquery)
importFrom(BiocFileCache,bfcrpath)
importFrom(dplyr,bind_rows)
importFrom(glue,glue)
importFrom(httr,content)
importFrom(httr2,req_perform)
importFrom(httr2,request)
Expand All @@ -34,6 +32,7 @@ importFrom(stringr,str_detect)
importFrom(stringr,str_split_1)
importFrom(tibble,as_tibble)
importFrom(tibble,tibble)
importFrom(tools,R_user_dir)
importFrom(utils,URLencode)
importFrom(utils,download.file)
importFrom(utils,tail)
59 changes: 34 additions & 25 deletions R/bedbaser.R
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ setMethod(
function(x, rec_type = c("bed", "bedset")) {
rec_type <- match.arg(rec_type)
if (rec_type == "bed")
response <- x$get_bed_genome_assemblies_bed_genomes_get()
resp <- x$get_bed_genome_assemblies_bed_genomes_get()
else
response <- x$get_bedset_genome_assemblies_bedset_genomes_get()
genome_list <- content(response)
resp <- x$get_bedset_genome_assemblies_bedset_genomes_get()
genome_list <- content(resp)
genome_tibble <- tibble()
if (length(genome_list)) {
cnames <- names(genome_list[[1]])
Expand Down Expand Up @@ -120,10 +120,10 @@ setMethod(
function(x, id, rec_type = c("bed", "bedset")) {
rec_type <- match.arg(rec_type)
if (rec_type == "bed")
response <- x$get_bed_metadata_bed__bed_id__metadata_get(id)
resp <- x$get_bed_metadata_bed__bed_id__metadata_get(id)
else
response <- x$get_bedset_metadata_bedset__bedset_id__metadata_get(id)
record <- content(response)
resp <- x$get_bedset_metadata_bedset__bedset_id__metadata_get(id)
record <- content(resp)
record$metadata
}
)
Expand All @@ -138,9 +138,10 @@ setGeneric(name = "bb_records",
#' Note: how to get next page token
#'
#' @param rec_type character() bed or bedset
#' @param limit integer() maximum records
#' @param token integer() next page of records
#' @param limit integer() (defaults to NULL) maximum records
#' @param token integer() (defaults to NULL) page token of records
#'
#' @importFrom httr content
#' @importFrom purrr map_dfr set_names
#' @importFrom tibble tibble as_tibble
#'
Expand All @@ -156,11 +157,11 @@ setMethod(
function(x, rec_type = c("bed", "bedset"), limit = NULL, token = NULL) {
rec_type <- match.arg(rec_type)
if (rec_type == "bed")
response <- x$list_beds_bed_list_get(limit=limit, token=token)
resp <- x$list_beds_bed_list_get(limit=limit, token=token)
else
response <- x$list_bedsets_bedset_list_get(limit=limit, token=token)
resp <- x$list_bedsets_bedset_list_get(limit=limit, token=token)
records_tibble <- tibble()
records_list <- content(response)
records_list <- content(resp)
if (length(records_list)) {
cnames <- names(records_list$records[[1]])
records_tibble <- records_list$records |>
Expand All @@ -178,6 +179,8 @@ setGeneric(name = "bb_beds_in_bedset",
#'
#' @param rec_id integer() BEDset record identifier
#'
#' @importFrom httr content
#'
#' @return list() BED record identifiers
#'
#' @examples
Expand All @@ -190,33 +193,40 @@ setMethod(
function(x, rec_id) {
response <-
x$get_bedfiles_in_bedset_bedset__bedset_id__bedfiles_get(rec_id)
records <- content(response)
records <- content(resp)
unlist(records$bedfile_metadata, use.names = FALSE)
}
)

setGeneric(name = "bb_search",
def = function(x, query, limit = NULL, offset = NULL) {
standardGeneric("bb_search")
})

#' Search BEDbase
#'
#' @param keywords character() keyword to search for
#' @param limit integer() maximum number of results to include
#' @param quiet logical() (default FALSE) display message
#' @param query character() keywords to search
#' @param limit integer() (defaults to NULL) maximum number of results
#' @param offset integer() (defaults to NULL) page offset of results
#'
#' @importFrom dplyr bind_rows
#' @importFrom glue glue
#' @importFrom httr2 req_perform request resp_body_json
#' @importFrom httr content
#' @importFrom tibble as_tibble
#' @importFrom utils URLencode
#'
#' @return tibble()
#'
#' @examples
#' search_bedbase("excluderanges")
#' client <- BEDbase()
#' bb_search(client, "excluderanges")
#'
#' @export
search_bedbase <- function(keywords, limit, quiet = FALSE) {
encoded_keywords <- URLencode(keywords, reserved = TRUE)
url <- glue("search/bed/{encoded_keywords}?limit={limit}")
results <- query_bedbase(url, quiet)
bb_search <- function(x, query, limit = NULL, offset = NULL) {
encoded_query <- URLencode(query, reserved = TRUE)
resp <- x$text_to_bed_search_search_bed__query__get(encoded_query,
limit = limit,
offset = offset)
results <- content(resp)
if (!length(results))
return(results)
metadata <- tibble()
Expand Down Expand Up @@ -249,7 +259,6 @@ search_bedbase <- function(keywords, limit, quiet = FALSE) {
#' @param quiet logical() (default FALSE) display message
#'
#' @importFrom BiocFileCache bfcrpath
#' @importFrom glue glue
#' @importFrom httr2 req_perform request resp_body_json
#' @importFrom stringr str_split_1
#' @importFrom utils download.file tail
Expand All @@ -265,9 +274,9 @@ download_file <- function(rec_id, file_type = c("bytes", "thumbnail"),
obj_id <- make_obj_id(rec_id)
stopifnot(acc_id %in% get_access_ids(obj_id, TRUE))
file_type <- match.arg(file_type)
url <- glue("objects/{obj_id}/access/{acc_id}")
url <- paste0("objects/", obj_id, "/access/", acc_id)
if (file_type == "thumbnail")
url <- glue("{url}/{file_type}")
url <- paste(url, file_type, sep="/")
download_url <- query_bedbase(url, quiet)
.download_and_cache(download_url, quiet)
}
Expand Down
10 changes: 5 additions & 5 deletions R/cache.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#' Get the cache
#'
#' Note: This function is described in BiocFileCache
#' This function is described in the BiocFileCache vignette.
#'
#' @importFrom BiocFileCache BiocFileCache
#' @importFrom tools R_user_dir
#'
#' @return BiocFileCache object
#'
Expand All @@ -11,20 +12,19 @@
#'
#' @noRd
.get_cache <- function() {
bfc <- tools::R_user_dir("bedbaser", which="cache")
bfc <- R_user_dir("bedbaser", which="cache")
BiocFileCache(bfc)
}

#' Retrieve path from cache or download file and cache
#'
#' Note: This function is described in BiocFileCache
#' This function is described in the BiocFileCache vignette.
#'
#' @param url character() remote resource
#' @param quiet logical() display message
#'
#' @importFrom BiocFileCache BiocFileCache bfcadd bfcdownload bfcneedsupdate
#' bfcquery bfcrpath
#' @importFrom glue glue
#' @importFrom rlang inform
#'
#' @return filepath character()
Expand All @@ -37,7 +37,7 @@
rid <- bfcquery(bfc, url)$rid
if (!length(rid)) {
if (!quiet)
inform(glue("Downloading {url} ..."))
inform(paste("Downloading", url, "..."))
rid <- names(bfcadd(bfc, url))
}
if (!isFALSE(bfcneedsupdate(bfc, rid)))
Expand Down
28 changes: 2 additions & 26 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#'
#' @param id character() BEDbase identifier
#'
#' @importFrom glue glue
#' @importFrom rlang abort
#' @importFrom stringr regex str_detect
#'
Expand All @@ -20,7 +19,7 @@ get_rec_type <- function(id) {
else if (str_detect(id, regex("^[:alnum:]+$")))
rec_type <- "object"
else
abort(glue("Unknown record identifier: {id}"))
abort(paste("Unknown record identifier:", id))
rec_type
}

Expand All @@ -32,15 +31,13 @@ get_rec_type <- function(id) {
#' @param rec_type character() (default bed) BEDbase record type
#' @param result_id character() (default bedfile) BEDbase result identifier
#'
#' @importFrom glue glue
#'
#' @return String
#'
#' @examples
#' obj_id <- make_obj_id("eaf9ee97241f300f1c7e76e1f945141f")
make_obj_id <- function(rec_id, rec_type = "bed", result_id = "bedfile")
{
glue("{rec_type}.{rec_id}.{result_id}")
paste(rec_type, rec_id, result_id, sep =".")
}

#' Get valid access identifiers
Expand All @@ -63,24 +60,3 @@ get_access_ids <- function(obj_id, quiet = FALSE) {
}
access_methods
}

setGeneric(name = "get_bb_service_info",
def = function(x) { standardGeneric("get_bb_service_info") })

#' Get service information
#'
#' @importFrom httr content
#'
#' @return list() service info, such as version
#'
#' @examples
#' bb <- BEDbase()
#' get_bb_service_info(bb)
#'
#' @export
setMethod(
"get_bb_service_info", "BEDbase",
function(x) {
content(x$service_info_service_info_get())
}
)
4 changes: 2 additions & 2 deletions man/bb_records-BEDbase-method.Rd

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

25 changes: 25 additions & 0 deletions man/bb_search.Rd

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

2 changes: 1 addition & 1 deletion man/dot-download_and_cache.Rd

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

19 changes: 0 additions & 19 deletions man/get_bb_service_info-BEDbase-method.Rd

This file was deleted.

25 changes: 0 additions & 25 deletions man/search_bedbase.Rd

This file was deleted.

Loading

0 comments on commit 07b13c9

Please sign in to comment.