Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 33 additions & 3 deletions R/dgidb.R
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,35 @@ getGenes <- function(terms, apiUrl = NULL) {
)
}

.filterInteractions <- function(nodes, source = NULL, approved = NULL) {
lapply(nodes, function(node) {
interactions <- node$interactions
if (!is.null(approved)) {
interactions <- Filter(
function(interaction) {
identical(interaction$drug$approved, approved)
},
interactions
)
}
if (!is.null(source)) {
interactions <- Filter(
function(interaction) {
sources <- vapply(
interaction$interactionClaims,
function(claim) claim$source$sourceDbName,
character(1)
)
source %in% sources
},
interactions
)
}
node$interactions <- interactions
node
})
}

#' Get Interactions
#'
#' Performs an interaction lookup for drugs or genes of interest.
Expand All @@ -183,10 +212,10 @@ getGenes <- function(terms, apiUrl = NULL) {
#' @param search Either `"genes"` or `"drugs"`.
#' @param immunotherapy Optionally filter drug searches by immunotherapy use.
#' @param antineoplastic Optionally filter drug searches by antineoplastic use.
#' @param source Optionally filter by source database name.
#' @param source Optionally filter interactions by source database name.
#' @param pmid Optionally filter by PubMed identifier.
#' @param interactionType Optionally filter by interaction type.
#' @param approved Optionally filter drug searches by approval status.
#' @param approved Optionally filter interactions by drug approval status.
#' @param apiUrl DGIdb GraphQL endpoint; defaults to `DGIDB_API_URL`.
#'
#' @return A data frame of interaction records. Multi-valued fields are
Expand Down Expand Up @@ -220,10 +249,10 @@ getInteractions <- function(
) {
search <- match.arg(search, c("genes", "drugs"))
params <- list(names = terms)
if (!is.null(source)) params$sourceDbName <- source
if (!is.null(pmid)) params$pmid <- pmid
if (!is.null(interactionType)) params$interactionType <- interactionType
if (search == "drugs") {
if (!is.null(source)) params$sourceDbName <- source
if (!is.null(immunotherapy)) params$immunotherapy <- immunotherapy
if (!is.null(antineoplastic)) params$antineoplastic <- antineoplastic
if (!is.null(approved)) params$approved <- approved
Expand All @@ -234,6 +263,7 @@ getInteractions <- function(
"queries/get_interactions_by_drug.graphql"
}
results <- .postQuery(apiUrl, queryFile, params)[[search]]$nodes
results <- .filterInteractions(results, source, approved)
.interactionOutput(results)
}

Expand Down
4 changes: 2 additions & 2 deletions man/getInteractions.Rd

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

74 changes: 74 additions & 0 deletions tests/testthat/test-dgidb.R
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,80 @@ test_that("getInteractions() applies filters by search type", {
expect_true(params$approved)
})

test_that("getInteractions() locally filters interactions returned for genes", {
local_mocked_bindings(
.postQuery = function(apiUrl, queryFile, variables) {
expect_equal(queryFile, "queries/get_interactions_by_gene.graphql")
expect_equal(variables, list(names = "KRAS"))
list(genes = list(nodes = list(list(interactions = list(
list(
gene = list(
name = "KRAS", conceptId = "hgnc:6407",
longName = "KRAS proto-oncogene"
),
drug = list(
name = "Approved ChEMBL drug", conceptId = "drug:1",
approved = TRUE
),
interactionScore = 1,
interactionAttributes = list(),
interactionClaims = list(list(
publications = list(),
source = list(sourceDbName = "ChEMBL")
))
),
list(
gene = list(
name = "KRAS", conceptId = "hgnc:6407",
longName = "KRAS proto-oncogene"
),
drug = list(
name = "Unapproved drug", conceptId = "drug:2",
approved = FALSE
),
interactionScore = 1,
interactionAttributes = list(),
interactionClaims = list(list(
publications = list(),
source = list(sourceDbName = "DrugBank")
))
),
list(
gene = list(
name = "KRAS", conceptId = "hgnc:6407",
longName = "KRAS proto-oncogene"
),
drug = list(
name = "Approved non-ChEMBL drug", conceptId = "drug:3",
approved = TRUE
),
interactionScore = 1,
interactionAttributes = list(),
interactionClaims = list(list(
publications = list(),
source = list(sourceDbName = "DrugBank")
))
)
)))))
}
)

approved <- getInteractions("KRAS", approved = TRUE)
expect_true(all(approved$drug_approved))
expect_setequal(
approved$drug_name,
c("Approved ChEMBL drug", "Approved non-ChEMBL drug")
)

chembl <- getInteractions("KRAS", source = "ChEMBL")
expect_equal(chembl$drug_name, "Approved ChEMBL drug")
expect_true(all(vapply(
chembl$interaction_sources,
function(sources) "ChEMBL" %in% sources,
logical(1)
)))
})

test_that("getInteractionTypes() returns all interaction claim types", {
local_mocked_bindings(
.postQuery = function(apiUrl, queryFile, variables) {
Expand Down
Loading