diff --git a/R/unichem.R b/R/unichem.R index 921ce4d..82e64a6 100644 --- a/R/unichem.R +++ b/R/unichem.R @@ -24,16 +24,18 @@ #' #' @export getUnichemSources <- function() { + funContext <- .funContext("AnnotationGx::getUnichemSources") + response <- .build_unichem_query("sources") |> .build_request() |> .perform_request() |> .parse_resp_json() if(response$response != "Success"){ - .err("Unichem API request failed.") + .err(funContext, "Unichem API request failed.") } - .debug(sprintf("Unichem sourceCount: %s", response$totalSources)) + .debug(funContext, sprintf("Unichem sourceCount: %s", response$totalSources)) sources_dt <- .asDT(response$sources) @@ -65,7 +67,7 @@ getUnichemSources <- function() { #' This function queries the UniChem API for a compound based on the provided parameters. #' #' @param type `character` The type of compound identifier to search for. Valid types are "uci", "inchi", "inchikey", and "sourceID". -#' @param compound `character` The compound identifier to search for. +#' @param compound `character` or `integer` The compound identifier to search for. #' @param sourceID `integer` The source ID to search for if the type is "sourceID". Defaults to NULL. #' @param request_only `boolean` Whether to return the request only. Defaults to FALSE. #' @param raw `boolean` Whether to return the raw response. Defaults to FALSE. @@ -81,7 +83,7 @@ queryUnichem <- function( type, compound, sourceID = NA_integer_, request_only = FALSE, raw = FALSE, ... ){ checkmate::assert_string(type) - checkmate::assert_string(compound) + checkmate::assert_atomic(compound) checkmate::assert_integerish(sourceID) checkmate::assertLogical(request_only) checkmate::assertLogical(raw) diff --git a/R/unichem_helpers.R b/R/unichem_helpers.R index c6cdf7c..a27398f 100644 --- a/R/unichem_helpers.R +++ b/R/unichem_helpers.R @@ -16,6 +16,7 @@ .build_unichem_query <- function( endpoint, query_only = FALSE ) { + funContext <- .funContext("AnnotationGx:::.build_unichem_query") valid_endpoints <- c("compounds", "connectivity", "images", "sources") checkmate::assert_subset(endpoint, valid_endpoints) @@ -24,6 +25,8 @@ url <- httr2::url_parse(unichem_api) url$path <- .buildURL(url$path, endpoint) + .debug(funContext, "URL: ", capture.output(show(url))) + if (query_only) return(url) return(httr2::url_build(url)) @@ -50,11 +53,15 @@ .build_unichem_compound_req <- function( type, compound, sourceID = NULL, ... ){ + funContext <- .funContext("AnnotationGx:::.build_unichem_compound_req") + valid_types <- c("uci", "inchi", "inchikey", "sourceID") checkmate::assert_subset(type, valid_types) base_url <- .build_unichem_query("compounds") + .debug(funContext, "Base URL: ", capture.output(show(base_url))) + body <- list( type = type, compound = compound @@ -75,6 +82,6 @@ .build_request() |> httr2::req_body_json(body) + .debug(funContext, "Request: ", capture.output(show(request))) return(request) - } diff --git a/man/queryUnichem.Rd b/man/queryUnichem.Rd index 64114d1..2e7b1e9 100644 --- a/man/queryUnichem.Rd +++ b/man/queryUnichem.Rd @@ -16,7 +16,7 @@ queryUnichem( \arguments{ \item{type}{\code{character} The type of compound identifier to search for. Valid types are "uci", "inchi", "inchikey", and "sourceID".} -\item{compound}{\code{character} The compound identifier to search for.} +\item{compound}{\code{character} or \code{integer} The compound identifier to search for.} \item{sourceID}{\code{integer} The source ID to search for if the type is "sourceID". Defaults to NULL.} diff --git a/vignettes/treatment_pipeline.Rmd b/vignettes/treatment_pipeline.Rmd index 7319544..47bd9c4 100644 --- a/vignettes/treatment_pipeline.Rmd +++ b/vignettes/treatment_pipeline.Rmd @@ -34,13 +34,12 @@ names_to_cids <- AnnotationGx::mapCompound2CID(treatmentMetadata$CTRP.treatmenti ```{r use CID in unichem} sources <- getUnichemSources() -response <- .build_compound_query( +response <- queryUnichem( type = "sourceID", - names_to_cids[1, cids], + compound = names_to_cids[1, cids], sourceID = sources[Name == "pubchem", SourceID] ) -response |> asdt() - +response ```