Skip to content

Commit

Permalink
Improve robot_query()
Browse files Browse the repository at this point in the history
  • Loading branch information
allenbaron committed Feb 13, 2024
1 parent c9454d2 commit 2ac4cb3
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 60 deletions.
39 changes: 0 additions & 39 deletions R/robot.R
Original file line number Diff line number Diff line change
Expand Up @@ -70,45 +70,6 @@ robot <- function(..., .robot_path = NULL) {
}


#' Execute Robot Query
#'
#' Wrapper for system ROBOT program (OBO Foundry) that executes the query
#' subcommand. See [ROBOT documentation](http://robot.obolibrary.org/) for
#' details.
#'
#' @section NOTE:
#' Requires installation of the OBO Foundry ROBOT tool in the system path. This
#' can be achieved with [install_robot()].
#'
#' @section Citation:
#' R.C. Jackson, J.P. Balhoff, E. Douglass, N.L. Harris, C.J. Mungall, and
#' J.A. Overton. ROBOT: A tool for automating ontology workflows.
#' BMC Bioinformatics, vol. 20, July 2019.
#'
#' @param input path to owl file to be queried
#' @param rq file with SPARQL query to execute
#' @param save path to save result to
#'
#' @export
robot_query <- function(input, rq, save) {
system2(
# use system robot command
"robot",
# args - subcommand, input, query (rq & save)
c(
"query",
paste0("--input ", input),
paste(
"--query",
rq,
save,
sep = " "
)
)
)
}


#' Install OBO Foundry ROBOT Tool (Mac/Linux ONLY)
#'
#' Installs latest ROBOT and robot.jar files to default system path
Expand Down
47 changes: 47 additions & 0 deletions R/robot_wrappers.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,50 @@ convert_to_ofn <- function(path, out_path = NULL, gzip = FALSE,
robot("convert", i = path, o = out_path, format = "ofn", .path = .robot_path)
invisible(out_path)
}


#' Execute a SPARQL Query with ROBOT
#'
#' Wrapper for `robot("query", ...)` that accepts a file _or_ text query, and
#' has more convenient arguments.
#'
#' @param input The path to an RDF/OWL file recognized by ROBOT, as a string.
#' @param query The text for or path to a valid SPARQL query (`ASK`, `SELECT`,
#' `CONSTRUCT`, or `UPDATE`) as a string.
#' @param output The path where output will be written, as a string.
#' @param ... Additional arguments to
#' [ROBOT query](http://robot.obolibrary.org/query) formatted as described in
#' [DO.utils::robot()].
#'
#' @returns `output` invisibly.
#'
#' @seealso [robot()] for underlying implementation; [tidy_sparql()] for tidying
#' tabular output data read into R.
#'
#' @export
robot_query <- function(input, query, output, ...) {
query_is_file <- file.exists(query)
if (query_is_file) {
q <- readr::read_lines(query)
} else {
q <- query
query <- tempfile(fileext = ".sparql")
readr::write_lines(q, query)
on.exit(file.remove(query))
}

# update query defined by the presence of INSERT/DELETE statements
update <- any(
stringr::str_detect(
q,
stringr::regex("insert|delete", ignore_case = TRUE)
)
)

if (isTRUE(update)) {
robot("query", i = input, update = query, o = output, ...)
} else {
robot("query", i = input, query = query, output, ...)
}
invisible(output)
}
40 changes: 19 additions & 21 deletions man/robot_query.Rd

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

0 comments on commit 2ac4cb3

Please sign in to comment.