From 35f47e6a063a7874df04f9b66fbf384d7020121b Mon Sep 17 00:00:00 2001 From: rmgpanw Date: Sat, 22 Jun 2024 13:44:53 +0100 Subject: [PATCH] update get_linkage_disequilibrium_*() functions and add test for shared utility function --- ...t_linkage_disequilibrium_by_variant_data.R | 29 +++++++------- R/get_linkage_disequilibrium_data.R | 16 ++------ R/utils.R | 17 ++++++++ ..._linkage_disequilibrium_by_variant_data.Rd | 10 ++--- tests/testthat/test-utils.R | 39 +++++++++++++++++++ vignettes/developer_guide.Rmd | 1 + 6 files changed, 79 insertions(+), 33 deletions(-) create mode 100644 R/utils.R create mode 100644 tests/testthat/test-utils.R diff --git a/R/get_linkage_disequilibrium_by_variant_data.R b/R/get_linkage_disequilibrium_by_variant_data.R index 9ad4d71..ce92d59 100644 --- a/R/get_linkage_disequilibrium_by_variant_data.R +++ b/R/get_linkage_disequilibrium_by_variant_data.R @@ -1,20 +1,21 @@ -#'Get Linkage Disequilibrium By Variant Data +#' Get Linkage Disequilibrium By Variant Data #' -#'@description -#' - Find linkage disequilibrium (LD) data for a given variant +#' @description Find linkage disequilibrium (LD) data for a given variant #' -#'[GTEx Portal API -#'documentation](https://gtexportal.org/api/v2/redoc#tag/Datasets-Endpoints/operation/get_linkage_disequilibrium_by_variant_data_api_v2_dataset_ldByVariant_get) +#' [GTEx Portal API +#' documentation](https://gtexportal.org/api/v2/redoc#tag/Datasets-Endpoints/operation/get_linkage_disequilibrium_by_variant_data_api_v2_dataset_ldByVariant_get) #' -#'@inheritParams gtexr_arguments -#'@return A Tibble -#'@export -#'@family Datasets Endpoints +#' @inheritParams gtexr_arguments +#' @return A tibble. +#' @export +#' @family Datasets Endpoints #' #' @examples -#' get_linkage_disequilibrium_by_variant_data() -get_linkage_disequilibrium_by_variant_data <- function(variantId = NULL, - page = 0, - itemsPerPage = 250){ - gtex_query(endpoint = "dataset/ldByVariant") +#' get_linkage_disequilibrium_by_variant_data("chr1_159245536_C_T_b38") +get_linkage_disequilibrium_by_variant_data <- function(variantId, + page = 0, + itemsPerPage = 250) { + resp_body <- gtex_query(endpoint = "dataset/ldByVariant", return_raw = TRUE) + + process_resp_body_linkage_disequilibrium(resp_body) } diff --git a/R/get_linkage_disequilibrium_data.R b/R/get_linkage_disequilibrium_data.R index 0ebba0e..52ca9b9 100644 --- a/R/get_linkage_disequilibrium_data.R +++ b/R/get_linkage_disequilibrium_data.R @@ -20,18 +20,8 @@ get_linkage_disequilibrium_data <- function(gencodeId, datasetId = "gtex_v8", page = 0, - itemsPerPage = 250){ - result <- gtex_query(endpoint = "dataset/ld", - return_raw = TRUE) + itemsPerPage = 250) { + resp_body <- gtex_query(endpoint = "dataset/ld", return_raw = TRUE) - paging_info_messages(result) - - result$data |> - purrr::map(\(x) tibble::tibble(variants = x[[1]], ld = x[[2]])) |> - dplyr::bind_rows() |> - tidyr::separate_wider_delim( - cols = "variants", - delim = ",", - names = c("variantId_1", "variantId_2") - ) + process_resp_body_linkage_disequilibrium(resp_body) } diff --git a/R/utils.R b/R/utils.R new file mode 100644 index 0000000..e382b50 --- /dev/null +++ b/R/utils.R @@ -0,0 +1,17 @@ +process_resp_body_linkage_disequilibrium <- function(resp_body) { + paging_info_messages(resp_body) + + if (rlang::is_empty(resp_body$data)) { + return(tibble::tibble()) + } + + resp_body$data |> + purrr::map(\(x) tibble::tibble(variants = x[[1]], ld = x[[2]])) |> + dplyr::bind_rows() |> + tidyr::separate_wider_delim( + cols = "variants", + delim = ",", + names = c("variantId_1", "variantId_2") + ) |> + dplyr::mutate("ld" = as.numeric(.data[["ld"]])) +} diff --git a/man/get_linkage_disequilibrium_by_variant_data.Rd b/man/get_linkage_disequilibrium_by_variant_data.Rd index 22fb9f7..066942a 100644 --- a/man/get_linkage_disequilibrium_by_variant_data.Rd +++ b/man/get_linkage_disequilibrium_by_variant_data.Rd @@ -5,7 +5,7 @@ \title{Get Linkage Disequilibrium By Variant Data} \usage{ get_linkage_disequilibrium_by_variant_data( - variantId = NULL, + variantId, page = 0, itemsPerPage = 250 ) @@ -18,17 +18,15 @@ get_linkage_disequilibrium_by_variant_data( \item{itemsPerPage}{Integer (default = 250).} } \value{ -A Tibble +A tibble. } \description{ -\itemize{ -\item Find linkage disequilibrium (LD) data for a given variant -} +Find linkage disequilibrium (LD) data for a given variant \href{https://gtexportal.org/api/v2/redoc#tag/Datasets-Endpoints/operation/get_linkage_disequilibrium_by_variant_data_api_v2_dataset_ldByVariant_get}{GTEx Portal API documentation} } \examples{ -get_linkage_disequilibrium_by_variant_data() +get_linkage_disequilibrium_by_variant_data("chr1_159245536_C_T_b38") } \seealso{ Other Datasets Endpoints: diff --git a/tests/testthat/test-utils.R b/tests/testthat/test-utils.R new file mode 100644 index 0000000..e370c7e --- /dev/null +++ b/tests/testthat/test-utils.R @@ -0,0 +1,39 @@ +test_that( + "`process_resp_body_linkage_disequilibrium()` handles both valid and empty responses", + { + # empty response + expect_equal(process_resp_body_linkage_disequilibrium(list( + data = list(), + paging_info = list( + numberOfPages = 0, + page = 0, + maxItemsPerPage = 250, + totalNumberOfItems = 0 + ) + )), + tibble::tibble()) + + # valid response (`get_linkage_disequilibrium_by_variant_data("chr1_153209639_T_C_b38")`) + expect_equal( + process_resp_body_linkage_disequilibrium(list( + data = list( + list( + "chr1_153202482_C_T_b38,chr1_153209639_T_C_b38", + "0.28083400000000003" + ) + ), + paging_info = list( + numberOfPages = 1, + page = 0, + maxItemsPerPage = 250, + totalNumberOfItems = 162 + ) + )), + tibble::tibble( + variantId_1 = "chr1_153202482_C_T_b38", + variantId_2 = "chr1_153209639_T_C_b38", + ld = 0.28083400000000003 # should be numeric + ) + ) + } +) diff --git a/vignettes/developer_guide.Rmd b/vignettes/developer_guide.Rmd index 537d96a..dc41956 100644 --- a/vignettes/developer_guide.Rmd +++ b/vignettes/developer_guide.Rmd @@ -30,6 +30,7 @@ library(gtexr) - [ ] Names match those for API endpoint, except where the argument accepts an array (vector in R) of values, in which case it is pluralised - [ ] Note that the [Get Genomic Features] endpoint takes a path parameter "featureId". The corresponding gtexr argument is prefixed with "." (`.featureId`). - [ ] Default values also match those for API endpoint + - [ ] Query parameters flagged as "required" in the GTEx Portal API documentation do not have default values. Note however that while `variantId` is not flagged as "required" for [Get Linkage Disequilibrium By Variant Data](https://gtexportal.org/api/v2/redoc#tag/Datasets-Endpoints/operation/get_linkage_disequilibrium_by_variant_data_api_v2_dataset_ldByVariant_get), no default value is provided in `get_linkage_disequilibrium_by_variant_data()` as providing `NULL` or an unrecognised variant ID returns an empty response. - [ ] Are all documented in `gtexr_arguments.R`. Rules...see also link to separate section(?) (**TO DO**) - [ ] All functions therefore use `@inheritParams gtexr_arguments` for documentation