Skip to content

Commit

Permalink
Removes terminfo withClassification param
Browse files Browse the repository at this point in the history
  • Loading branch information
johnbradley committed Sep 8, 2021
1 parent 96a8efd commit f45e325
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 100 deletions.
30 changes: 5 additions & 25 deletions R/terms.R
Original file line number Diff line number Diff line change
Expand Up @@ -179,15 +179,9 @@ get_term_label <- function(term_iris, preserveOrder = FALSE, verbose = FALSE, .t
#' `as.terminfo` creates an object (or a list of objects) of type "terminfo".
#' The object to be coerced can be a character vector (of term IRIs), or a data.frame.
#' In the latter case, there must be a column "id" with term IRIs.
#' If the object is already of type "terminfo", a new terminfo object will be created
#' so options such as withClassification will be applied.
#'
#' @param x an object of type "terminfo" or coercible to it, or to be tested
#' for being of type "terminfo"
#' @param withClassification logical. If TRUE classification data will be available
#' through the terminfo object at key "classification". Default is FALSE, because
#' obtaining taxa requires an additional query per object. The default can be
#' customized by setting the "rphenoscape.fetch.classification" option.
#' @param ... additional parameters where applicable
#'
#' @return
Expand All @@ -196,8 +190,8 @@ get_term_label <- function(term_iris, preserveOrder = FALSE, verbose = FALSE, .t
#' data.frame). A terminfo object has properties "id" (ID, i.e., IRI of the
#' term), "label" (label of the term if one exists). If the term is a taxon
#' additional properties will be populated: "extinct", "rank", "common_name".
#' If `withClassification` is TRUE,there will also be a key "classification"
#' (a list with properties "subClassOf", "equivalentTo" and "superClassOf").
#' There will also be a key "classification" (a list with properties "subClassOf",
#' "equivalentTo" and "superClassOf").
#'
#' @examples
#' # find a term iri
Expand All @@ -207,12 +201,6 @@ get_term_label <- function(term_iris, preserveOrder = FALSE, verbose = FALSE, .t
#' class(obj)
#' obj
#'
#' # classification details can be requested:
#' term_iri <- find_term('maxilla', matchType='exact')
#' # turn it into a terminfo object
#' obj <- as.terminfo(term_iri, withClassification=TRUE)
#' class(obj)
#' obj
#'
#' # taxon terms have additional properties:
#' term_iri <- find_term('Coralliozetus angelicus', matchType='exact')
Expand All @@ -224,7 +212,7 @@ get_term_label <- function(term_iris, preserveOrder = FALSE, verbose = FALSE, .t
#' @name terminfo
#' @rdname terminfo
#' @export
as.terminfo <- function(x, withClassification = getOption("rphenoscape.fetch.classification", default = FALSE), ...) {
as.terminfo <- function(x, ...) {
UseMethod("as.terminfo", x)
}

Expand Down Expand Up @@ -270,7 +258,7 @@ as.terminfo.data.frame <- function(x, ...) {

#' @export
as.terminfo.terminfo <- function(x, ...) {
# recreate to allow fetching additional data with ... parameters like withClassification
# recreate to allow fetching additional data with ... parameters
as.terminfo(x$id, ...)
}

Expand Down Expand Up @@ -319,21 +307,13 @@ is_known_term_response <- function(x) {
}

#' @export
terminfo <- function(iri, withClassification = getOption("rphenoscape.fetch.classification", default = FALSE)) {
terminfo <- function(iri) {
stopifnot(is.character(iri))
res <- get_json_data(pkb_api("/term"),
query = list(iri = iri),
forceGET = TRUE,
cleanNames = TRUE)
if (is_known_term_response(res)) {
if (withClassification) {
classification <- term_classification(iri)
# remove redundant information
classification$id <- NULL
classification$label <- NULL
# add to resulting list
res$classification <- classification
}
if (identical(obo_ont_type(res$isDefinedBy), "taxon")) {
taxon <- get_json_data(pkb_api("/taxon"),
list(iri = iri),
Expand Down
23 changes: 3 additions & 20 deletions man/terminfo.Rd

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

57 changes: 2 additions & 55 deletions tests/testthat/test-pk.R
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,7 @@ test_that("creating terminfo objects and extracting properties", {
testthat::expect_is(obj, "terminfo")
testthat::expect_true(is.terminfo(obj))
testthat::expect_true(is_valid_terminfo(obj))

# optionally include classification
testthat::expect_false("classification" %in% names(obj))
obj <- as.terminfo(terms[1, "id"], withClassification = TRUE)
testthat::expect_true(is.terminfo(obj))
testthat::expect_true(is_valid_terminfo(obj))
# check that the server returned classification details
testthat::expect_true("classification" %in% names(obj))
testthat::expect_equal(length(obj$classification), 3)

Expand All @@ -218,13 +213,6 @@ test_that("creating terminfo objects and extracting properties", {
testthat::expect_true(is.terminfo(obj))
testthat::expect_false(is_valid_terminfo(obj))

# robust to unresolving IRIs including classification
testthat::expect_warning(obj <- as.terminfo("foo", withClassification = TRUE))
testthat::expect_is(obj, "terminfo")
testthat::expect_true(is.terminfo(obj))
testthat::expect_false(is_valid_terminfo(obj))
testthat::expect_null(obj$classification)

# also works with data.frame as input
obj <- as.terminfo(terms[1,])
testthat::expect_is(obj, "terminfo")
Expand Down Expand Up @@ -253,10 +241,6 @@ test_that("pretty-printing terminfo objects", {
# create terminfo object
ti <- as.terminfo(term_iri)
# check print output
expect_output(print(ti), "terminfo 'basihyal bone' http.*Definition:.*Synonyms:.*Relationships:.*")
# create terminfo object with classification
ti <- as.terminfo(term_iri, withClassification = TRUE)
# check print output including classification sections
expect_output(print(ti), "terminfo 'basihyal bone' http.*Definition:.*Synonyms:.*Relationships:.*Subclass of:.*Superclass of::*")

# find taxon term iri
Expand All @@ -265,41 +249,4 @@ test_that("pretty-printing terminfo objects", {
ti <- as.terminfo(term_iri)
# check print output including taxon specific sections
expect_output(print(ti), "terminfo 'Coralliozetus angelicus' http.*Synonyms:.*Extinct:.*Rank:.*Common Name:.*")
})

test_that("as.terminfo withClassification can be controlled via an option", {
# find term iri
term_iri <- find_term("basihyal bone", limit = 1)
# create terminfo object
ti <- as.terminfo(term_iri)
# classification should not be filled in
testthat::expect_false("classification" %in% names(ti))

# turn on the option to default withClassification to TRUE
options(rphenoscape.fetch.classification = TRUE)
# create terminfo object
ti <- as.terminfo(term_iri)
# classification should be filled in
testthat::expect_true("classification" %in% names(ti))
testthat::expect_equal(length(ti$classification), 3)

# setting the option to FALSE should return to the default behavior
options(rphenoscape.fetch.classification = FALSE)
# create terminfo object
ti <- as.terminfo(term_iri)
# classification should not be filled in
testthat::expect_false("classification" %in% names(ti))
})

test_that("as.terminfo can add classification to terminfo objects", {
# find term iri
term_iri <- find_term("basihyal bone", limit = 1)
# create terminfo object
ti <- as.terminfo(term_iri)
# classification should not be filled in
testthat::expect_false("classification" %in% names(ti))
# run as.terminfo on a terminfo object requesting classification data
ti <- as.terminfo(ti, withClassification = TRUE)
# classification should be filled in
testthat::expect_true("classification" %in% names(ti))
})
})

0 comments on commit f45e325

Please sign in to comment.