Skip to content

Commit

Permalink
Rename tidy_sparql() what arg to tidy_what
Browse files Browse the repository at this point in the history
To make it easier to implement and document in other functions.
Also update robot_query() and extract_ordo_mappings() with
these changes.

Tests (robot_wrappers): PASS
  • Loading branch information
allenbaron committed Feb 14, 2024
1 parent 3758078 commit bdcf5ce
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 39 deletions.
14 changes: 10 additions & 4 deletions R/extract.R
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,8 @@ extract_as_tidygraph <- function(x, query = NULL, collapse_method = "first",
#'
#' @param output The path where output will be written, as a string, or `NULL`
#' (default) to load data directly.
#' @inheritDotParams tidy_sparql -query_res
#' @inheritParams tidy_sparql
#'
#' @returns
#' If `output` is specified, the path to the output file with the data.
#' Otherwise, the data as a [tibble](tibble::tibble).
Expand All @@ -481,15 +482,20 @@ extract_as_tidygraph <- function(x, query = NULL, collapse_method = "first",
#'
#' @export
extract_ordo_mappings <- function(ordo_path, as_skos = TRUE, output = NULL,
...) {
tidy_what = "everything") {
if (isTRUE(as_skos)) {
q_nm <- "mapping-ordo-skos.rq"
} else {
q_nm <- "mapping-ordo.rq"
}

q_file <- system.file("sparql", q_nm, package = "DO.utils", mustWork = TRUE)
out <- robot_query(input = ordo_path, query = q_file, output)
out <- tidy_sparql(out, ...)
out <- robot_query(
input = ordo_path,
query = q_file,
output = output,
tidy_what = tidy_what
)

out
}
6 changes: 2 additions & 4 deletions R/robot_wrappers.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ convert_to_ofn <- function(path, out_path = NULL, gzip = FALSE,
#' @param ... Additional arguments to
#' [ROBOT query](http://robot.obolibrary.org/query) formatted as described in
#' [DO.utils::robot()].
#' @param tidy_what The elements of a SELECT query to tidy, as a character
#' vector. Ignored when `output` is specified. See [tidy_sparql()] `what` for
#' options (default: `"nothing"`).
#' @inheritParams tidy_sparql
#'
#' @returns
#' If `output` is specified, the path to the output file with the query result.
Expand Down Expand Up @@ -125,7 +123,7 @@ robot_query <- function(input, query, output = NULL, ...,
output,
col_types = readr::cols(.default = readr::col_character())
)
out <- tidy_sparql(out, what = tidy_what)
out <- tidy_sparql(out, tidy_what)
}
out
}
28 changes: 14 additions & 14 deletions R/tidy.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ utils::globalVariables("where")

#' Tidy SPARQL Query
#'
#' Tidies SPARQL query results according to desired specifications (see `what`
#' param for details).
#' Tidies SPARQL query results according to desired specifications (see
#' `tidy_what` parameter for details).
#'
#' @param query_res The results of a SPARQL query, as a data.frame (usually
#' produced by [owl_xml()]$query() or similar from [DOrepo()], but can also
#' be used to tidy results of [robot("query", ...)][robot] loaded with
#' `readr`).
#' @param what The elements of the query to tidy, as a character vector. One or
#' more of the following:
#' @param tidy_what The elements of a SPARQL-created data.frame to tidy, as a
#' character vector. One or more of the following:
#'
#' * `"everything"` to apply all tidy operations (has precedence over
#' `"nothing"`).
Expand All @@ -27,41 +27,41 @@ utils::globalVariables("where")
#' @inheritDotParams to_curie -x
#'
#' @export
tidy_sparql <- function(query_res, what = "everything", ...) {
tidy_sparql <- function(query_res, tidy_what = "everything", ...) {
what_opts <- c("header", "unnest", "uri_to_curie", "lgl_NA_FALSE",
"as_tibble")
what <- match.arg(
what,
tidy_what <- match.arg(
tidy_what,
choices = c(what_opts, "everything", "nothing"),
several.ok = TRUE
)
if ("everything" %in% what) what <- what_opts
if ("nothing" %in% what) return(query_res)
if ("everything" %in% tidy_what) tidy_what <- what_opts
if ("nothing" %in% tidy_what) return(query_res)

res <- query_res
if ("header" %in% what) {
if ("header" %in% tidy_what) {
names(res) <- stringr::str_remove(names(res), "^\\?")
}

if ("unnest" %in% what) {
if ("unnest" %in% tidy_what) {
res <- DO.utils::unnest_cross(res, where(is.list), keep_empty = TRUE)
}

if ("uri_to_curie" %in% what) {
if ("uri_to_curie" %in% tidy_what) {
res <- dplyr::mutate(
res,
dplyr::across(dplyr::where(is.character), ~ to_curie(.x, ...))
)
}

if ("lgl_NA_FALSE" %in% what) {
if ("lgl_NA_FALSE" %in% tidy_what) {
res <- dplyr::mutate(
res,
dplyr::across(dplyr::where(is.logical), ~ replace_na(.x, FALSE))
)
}

if ("as_tibble" %in% what) {
if ("as_tibble" %in% tidy_what) {
res <- tibble::as_tibble(query_res)
}

Expand Down
28 changes: 19 additions & 9 deletions man/extract_ordo_mappings.Rd

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

16 changes: 13 additions & 3 deletions man/robot_query.Rd

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

10 changes: 5 additions & 5 deletions man/tidy_sparql.Rd

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

0 comments on commit bdcf5ce

Please sign in to comment.