Skip to content

Commit

Permalink
add ChEMBL functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Jermiah committed Oct 26, 2023
1 parent b78b62a commit 68ab615
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 17 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export(getCellosaurusAPI)
export(getCellosaurusDataFrame)
export(getCelloxml)
export(getChemblAllMechanisms)
export(getChemblMechanism)
export(getDrugTargets)
export(getFDAOrangeBookProducts)
export(getFailed)
Expand Down
58 changes: 57 additions & 1 deletion R/getChEMBL.R
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,60 @@ moleculeQuery <- function(field, filter_type, value) {
}


#' Query the ChEMBL mechanism API end-point for a given CHEMBL_ID or list of CHEMBL_IDs
#'
#' @md
#' @title Query the ChEMBL mechanism API end-point for a given CHEMBL_ID or list of CHEMBL_IDs
#' @description A specialised function for querying the mechanism resource from the ChEMBL API.
#'
#' @param chembl.ID `character(1)` or `character(n)` CHEMBL_ID(s) to query
#' @param resources `character(1)` Resource to query. Default is "mechanism"
#' @param field `character(1)` or `character(n)` Field(s) to query. Default is "molecule_chembl_id"
#' @param filter_type `character(1)` Filter type. Default is "in"
#' @param returnURL `logical(1)` Return the URL(s) instead of the data table
#'
#' @return `data.table` A table containing all mechanism of action entries and the CHEMBL_IDs queried
#'
#' @examples
#' getChemblMechanism("CHEMBL1413")
#'
#' chembl_ids <- c("CHEMBL515", "CHEMBL235191", "CHEMBL1413")
#' getChemblMechanism(chembl_ids)
#'
#' @export
getChemblMechanism <- function(
chembl.ID,
resources = "mechanism",
field = "molecule_chembl_id",
filter_type = "in",
returnURL = FALSE){


# constructChemblQuery(resource = "mechanism", field = "molecule_chembl_id", filter_type = "in", value = "CHEMBL1413")
urls <- constructChemblQuery(resource = resources, field = field, filter_type = filter_type, value = chembl.ID)
urls <- URLencode(urls)

if(returnURL) return(urls)

# [1] "action_type" "binding_site_comment"
# [3] "direct_interaction" "disease_efficacy"
# [5] "max_phase" "mec_id"
# [7] "mechanism_comment" "mechanism_of_action"
# [9] "mechanism_refs" "molecular_mechanism"
# [11] "molecule_chembl_id" "parent_molecule_chembl_id"
# [13] "record_id" "selectivity_comment"
# [15] "site_id" "target_chembl_id"
# [17] "variant_sequence"

responses <- data.table::rbindlist(lapply(urls, function(url){
response <- httr::GET(url)
response <- AnnotationGx::parseJSON(response)
mechanisms <- data.table::as.data.table(response$mechanisms)
result <- mechanisms[,.(molecule_chembl_id, action_type, mechanism_of_action, molecular_mechanism, mechanism_comment, parent_molecule_chembl_id, target_chembl_id)]
}))

return(responses)
}


# Write testing code here, this is only executed if the file is run as a script
Expand All @@ -233,4 +287,6 @@ if (sys.nframe() == 0) {
library(httr)
test1 <- constructChemblQuery("target", "pref_name", "contains", "kinase")
test2 <- compoundQuery("field", "fil", "j")
}
}


24 changes: 16 additions & 8 deletions R/getPubChem.R
Original file line number Diff line number Diff line change
Expand Up @@ -766,15 +766,23 @@ getAllPubChemAnnotations <-
#' @export
getPubChemAnnotation <- function(
compound,
header = 'ChEMBL ID',
annotationType = 'ChEMBL ID',
url = 'https://pubchem.ncbi.nlm.nih.gov/rest/pug_view/data/compound',
output = 'JSON',
timeout_s = 29,
retries = 3,
quiet = TRUE,
throttleMessage = FALSE
){

header <- annotationType

validHeaders <- c('ChEMBL ID', 'NSC Number', 'Drug Induced Liver Injury', 'DILI', 'CAS', 'ATC Code')

# make sure type is in the list of valid types
if(!(header %in% validHeaders)){
stop(paste0("header must be one of the following: ", paste0(validHeaders, collapse = ", ")))
}

# TODO:: add a check to see if the compound is a valid CID or SID
# TODO:: allow for variaitons of headers due to spelling errors
# Temporary:
Expand Down Expand Up @@ -837,23 +845,23 @@ getPubChemAnnotation <- function(
names(result) <- c("cid", header)
return(result)
}

#' Function that parses the results of the PubChem PUG-VIEW API for the CHEMBL ID header
.parseCHEMBLresponse <- function(response){
.parseCHEMBLresponse <- function(result){
result <- result$Record$Reference$SourceID
result <- gsub("::Compound", "", result)
return(result)
}

#' Function that parses the results of the PubChem PUG-VIEW API for the NSC Number header
.parseNSCresponse <- function(response){
.parseNSCresponse <- function(result){
result <- result$Record$Reference$SourceID[1]
result <- gsub(" ", "", result)
return(result)
}

#' Function that parses the results of the PubChem PUG-VIEW API for the DILI header
.parseDILIresponse <- function(response){
.parseDILIresponse <- function(result){
if(length(result$Record$Section) == 0){
result <- "NA"
}else{
Expand All @@ -879,13 +887,13 @@ getPubChemAnnotation <- function(
}

#' Function that parses the results of the PubChem PUG-VIEW API for the CAS header
.parseCASresponse <- function(response){
.parseCASresponse <- function(result){
result <- result$Record$Reference$SourceID[1]
return(result)
}

#' Function that parses the results of the PubChem PUG-VIEW API for the ATC Code header
.parseATCresponse <- function(response){
.parseATCresponse <- function(result){
if(length(result$Record$Section) == 0){
result <- "NA"

Expand Down
2 changes: 1 addition & 1 deletion man/dot-parseATCresponse.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/dot-parseCASresponse.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/dot-parseCHEMBLresponse.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/dot-parseDILIresponse.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/dot-parseNSCresponse.Rd

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

41 changes: 41 additions & 0 deletions man/getChemblMechanism.Rd

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

6 changes: 3 additions & 3 deletions man/getPubChemAnnotation.Rd

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

0 comments on commit 68ab615

Please sign in to comment.