Skip to content

Commit

Permalink
update get_linkage_disequilibrium_*() functions and add test for shar…
Browse files Browse the repository at this point in the history
…ed utility function
  • Loading branch information
rmgpanw committed Jun 22, 2024
1 parent bb68f69 commit 35f47e6
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 33 deletions.
29 changes: 15 additions & 14 deletions R/get_linkage_disequilibrium_by_variant_data.R
Original file line number Diff line number Diff line change
@@ -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)
}
16 changes: 3 additions & 13 deletions R/get_linkage_disequilibrium_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
17 changes: 17 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
@@ -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"]]))
}
10 changes: 4 additions & 6 deletions man/get_linkage_disequilibrium_by_variant_data.Rd

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

39 changes: 39 additions & 0 deletions tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
@@ -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
)
)
}
)
1 change: 1 addition & 0 deletions vignettes/developer_guide.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 35f47e6

Please sign in to comment.