Skip to content

Commit

Permalink
update get_downloads_page_data() and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
rmgpanw committed Jun 21, 2024
1 parent e654db7 commit 93b3159
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 18 deletions.
70 changes: 56 additions & 14 deletions R/get_downloads_page_data.R
Original file line number Diff line number Diff line change
@@ -1,22 +1,64 @@
#'Get Downloads Page Data
#' Get Downloads Page Data
#'
#'@description
#' - Retrieves all the files belonging to the given project_id for display on the Downloads Page
#' @description Retrieves all the files belonging to the given `project_id` for
#' display on the `Downloads Page`
#'
#'[GTEx Portal API
#'documentation](https://gtexportal.org/api/v2/redoc#tag/Datasets-Endpoints/operation/get_downloads_page_data_api_v2_dataset_openAccessFilesMetadata_get)
#' [GTEx Portal API
#' documentation](https://gtexportal.org/api/v2/redoc#tag/Datasets-Endpoints/operation/get_downloads_page_data_api_v2_dataset_openAccessFilesMetadata_get)
#'
#'@inheritParams gtexr_arguments
#' @details Note: The GTEx Portal API documentation states "GTEx currently has
#' one project available: gtex". However, `project_id` values "adult-gtex" and
#' "egtex" both return results, whereas "gtex" does not (see examples).
#'
#'@return A Tibble
#'@export
#'@family Datasets Endpoints
#' @inheritParams gtexr_arguments
#'
#' @return A Tibble
#' @export
#' @family Datasets Endpoints
#'
#' @examples
#' get_downloads_page_data()
#' \dontrun{
#' # "adult-gtex" (default `project_id` value) and "egtex" both return results
#' get_downloads_page_data()
#' egtex <- get_downloads_page_data("egtex")
#' egtex
#'
#' # ..."gtex" does not
#' get_downloads_page_data("gtex")
#'
#' # get details for whole blood methylation data, including download URL
#' purrr::pluck(
#' egtex$children,
#' 1,
#' "folders",
#' "Methylation - EPIC Array",
#' "children",
#' "folders",
#' "mQTLs",
#' "children",
#' "files",
#' "WholeBlood.mQTLs.regular.txt.gz"
#' )
#' }
get_downloads_page_data <- function(project_id = "adult-gtex") {
gtex_query(endpoint = "dataset/openAccessFilesMetadata",
return_raw = TRUE)$data |>
purrr::map(tibble::as_tibble) |>
dplyr::bind_rows()
result <- gtex_query(endpoint = "dataset/openAccessFilesMetadata",
return_raw = TRUE)

result |>
purrr::map(name_unnamed_items, name_item = "displayName") |>
tibble::as_tibble() |>
tidyr::unnest_wider("data") |>
dplyr::arrange(dplyr::pick(dplyr::any_of("order")))
}

name_unnamed_items <- function(x, name_item) {

if (inherits(x, "list")) {
if (is.null(names(x))) {
names(x) <- purrr::map_chr(x, \(item) item[[name_item]])
}
x <- purrr::map(x, name_unnamed_items, name_item = name_item)
}

return(x)
}
34 changes: 30 additions & 4 deletions man/get_downloads_page_data.Rd

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

13 changes: 13 additions & 0 deletions tests/testthat/test-get_downloads_page_data.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
test_that(
"`get_downloads_page_data()` runs without error for `project_id` = 'gtex' (which returns no results)",
{
expect_equal(
with_mocked_bindings(
code = get_downloads_page_data("gtex"),
gtex_query = function(...)
list(data = list())
),
tibble::tibble()
)
}
)

0 comments on commit 93b3159

Please sign in to comment.