Skip to content

Commit

Permalink
fix: Refactor UniChem API functions and add debug statements
Browse files Browse the repository at this point in the history
  • Loading branch information
jjjermiah committed Mar 11, 2024
1 parent c8d88cc commit efa3208
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 10 deletions.
10 changes: 6 additions & 4 deletions R/unichem.R
Original file line number Diff line number Diff line change
Expand Up @@ -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.")

Check warning on line 35 in R/unichem.R

View check run for this annotation

Codecov / codecov/patch

R/unichem.R#L35

Added line #L35 was not covered by tests
}

.debug(sprintf("Unichem sourceCount: %s", response$totalSources))
.debug(funContext, sprintf("Unichem sourceCount: %s", response$totalSources))

sources_dt <- .asDT(response$sources)

Expand Down Expand Up @@ -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.
Expand All @@ -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)
Expand Down
9 changes: 8 additions & 1 deletion R/unichem_helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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))
Expand All @@ -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
Expand All @@ -75,6 +82,6 @@
.build_request() |>
httr2::req_body_json(body)

.debug(funContext, "Request: ", capture.output(show(request)))
return(request)

}
2 changes: 1 addition & 1 deletion man/queryUnichem.Rd

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

7 changes: 3 additions & 4 deletions vignettes/treatment_pipeline.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

0 comments on commit efa3208

Please sign in to comment.