Skip to content

Commit

Permalink
Add smiles retrival and fix some typos
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobias Schulze committed Aug 22, 2023
1 parent 35b8013 commit e715196
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 8 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export(getCtsKey)
export(getCtsRecord)
export(getDTXCID)
export(getDTXSID)
export(getDTXSMILES)
export(getField)
export(getMolecule)
export(getPcId)
Expand Down
13 changes: 11 additions & 2 deletions R/createMassBank.R
Original file line number Diff line number Diff line change
Expand Up @@ -536,13 +536,14 @@ gatherPubChem <- function(key){
#' substance ID, the CAS-RN, the DTX preferred name, and the DTXCID (chemical ID).
#'
#' @usage gatherCCTE(key, api_key)
#' @param key An Inchi-Key
#' @param key An Inchi-Key or other chemical identifier (e.g. Chemical name, DTXSID, CASRN, InChIKey, DTXCID)
#' @param api_key An US EPA CCTE API key (personal or application)
#' @return Returns a list with 4 slots:
#' @return Returns a list with 5 slots:
#' \code{dtxsid} The US EPA chemical dashboard substance id
#' \code{dtxcid} The US EPA chemical dashboard chemical id
#' \code{preferredName} The US EPA chemical dashboard preferred name
#' \code{casrn} The latest CAS registration number
#' \code{smiles} The SMILES annotation of the structure
#' @author Tobias Schulze
#' @seealso \code{\link{mbWorkflow}}
#' @references CCTE REST:
Expand All @@ -562,6 +563,7 @@ gatherCCTE <- function(key, api_key = NA) {
CTTE_data$dtxcid <- NA
CTTE_data$preferredname <- NA
CTTE_data$casrn <- NA
CTTE_data$smiles <- NA
return(CCTE_data)
}

Expand Down Expand Up @@ -598,6 +600,13 @@ gatherCCTE <- function(key, api_key = NA) {
CCTE_data$casrn <<- NA
})

##Retrieve latest CAS RN
tryCatch(
CCTE_data$smiles <- getDTXSMILES(key, api_key),
error=function(e){
CCTE_data$smiles <<- NA
})

return(CCTE_data)
}

Expand Down
53 changes: 51 additions & 2 deletions R/webAccess.R
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ getPcId <- function(query, from = "inchikey")
#' CCTE REST:
#' \url{https://api-ccte.epa.gov/docs}
#' @examples
#' getPcId("MKXZASYAUGDDCJ-NJAFHUGGSA-N")
#' getDTXSID("MKXZASYAUGDDCJ-NJAFHUGGSA-N")
#'
#' @export
getDTXSID <- function(key, api_key)
Expand Down Expand Up @@ -226,7 +226,6 @@ getDTXCID <- function(key, api_key)

},
error=function(e){
currEnvir$errorvar <- 1
})

if(errorvar){
Expand Down Expand Up @@ -345,6 +344,56 @@ getCASRN <- function(key, api_key)
}
}

#' Search CCTE SMILES
#'
#' Retrieves CCTE SMILES from US EPA for a search term.
#'
#' Only the first result is returned currently. \bold{The function should be
#' regarded as experimental and has not thoroughly been tested.}
#'
#' @usage getDTXSMILES(key, api_key)
#' @param key ID to be converted
#' @param api_key API key for CCTE
#' @return The SMILES (in string type)
#' @author Tobias Schulze
#' @references CCTE search: \url{https://api-ccte.epa.gov/docs}
#'
#' CCTE REST:
#' \url{https://api-ccte.epa.gov/docs}
#' @examples
#' getDTXSMILES("MKXZASYAUGDDCJ-NJAFHUGGSA-N")
#'
#' @export
getDTXSMILES <- function(key, api_key)

{
errorvar <- 0
currEnvir <- environment()

tryCatch({
base_url <- paste0("https://api-ccte.epa.gov/chemical/search/equal/", key)
url <- httr2::request(base_url)
url <- url |> httr2::req_headers("x-api-key" = api_key, "accept" = "application/json")
resp <- httr2::req_perform(url)
data <- resp |> httr2::resp_body_json()

},
error=function(e){
currEnvir$errorvar <- 1
})

if(errorvar){
return(NA)
}

smiles <- data[[1]]$smiles

if(is.null(smiles)){
return(NA)
} else{
return(smiles)
}
}

#' Retrieve information from CTS
#'
Expand Down
9 changes: 6 additions & 3 deletions man/gatherCCTE.Rd

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

2 changes: 1 addition & 1 deletion man/getDTXSID.Rd

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

36 changes: 36 additions & 0 deletions man/getDTXSMILES.Rd

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

0 comments on commit e715196

Please sign in to comment.