Skip to content

Commit

Permalink
Merge pull request #310 from egonw/patches/BioHackEU20
Browse files Browse the repository at this point in the history
  • Loading branch information
stitam committed Feb 5, 2021
2 parents 7857d26 + 5937bc8 commit 4c63e29
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 12 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
## BUG FIXES

* Fix get_csid() so it doesn't break when a query is invalid.
* Typo fix in Wikidata.R
* Added support for fetching LIPID MAPS and SwissLipids identifiers from Wikidata.

# webchem 1.1.0.

Expand Down
14 changes: 7 additions & 7 deletions R/wikidata.R
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ get_wdid <-



#' Retrieve Indentifiers from Wikidata
#' Retrieve identifiers from Wikidata
#'
#' @import jsonlite
#' @import httr
Expand All @@ -127,7 +127,7 @@ get_wdid <-
#' @param verbose logical; print message during processing to console?
#'
#' @return A data.frame of identifiers. Currently these are 'smiles', 'cas', 'cid', 'einecs', 'csid', 'inchi', 'inchikey',
#' 'drugbank', 'zvg', 'chebi', 'chembl', 'unii' and source_url.
#' 'drugbank', 'zvg', 'chebi', 'chembl', 'unii', 'lipidmaps', 'swisslipids' and source_url.
#'
#' @note Only matches in labels are returned. If more than one unique hit is found,
#' only the first is returned.
Expand All @@ -153,19 +153,19 @@ wd_ident <- function(id, verbose = TRUE){
# id <- c( "Q163648", "Q18216")
# id <- 'Q408646'
foo <- function(id, verbose){
empty <- as.list(rep(NA, 13))
empty <- as.list(rep(NA, 15))
names(empty) <- c("smiles", "cas", "cid", "einecs", "csid", "inchi",
"inchikey", "drugbank", "zvg", "chebi", "chembl", "unii",
"source_url")
'lipidmaps', 'swisslipids', "source_url")
if (is.na(id)) {
if (verbose) webchem_message("na")
return(empty)
}
baseurl <- 'https://query.wikidata.org/sparql?format=json&query='
props <- c('P233', 'P231', 'P662', 'P232', 'P661', 'P234', 'P235', 'P715', 'P679',
'P683', 'P592', 'P652')
'P683', 'P592', 'P652', 'P2063', 'P8691')
names <- c('smiles', 'cas', 'cid', 'einecs', 'csid', 'inchi', 'inchikey',
'drugbank', 'zvg', 'chebi', 'chembl', 'unii')
'drugbank', 'zvg', 'chebi', 'chembl', 'unii', 'lipidmaps', 'swisslipids')

sparql_head <- paste('PREFIX wd: <http://www.wikidata.org/entity/>',
'PREFIX wdt: <http://www.wikidata.org/prop/direct/>',
Expand Down Expand Up @@ -195,7 +195,7 @@ wd_ident <- function(id, verbose = TRUE){

if (length(out) == 0) {
if (verbose) webchem_message("not_found")
out <- as.list(rep(NA, 13))
out <- as.list(rep(NA, 15))
names(out) <- c(vars_out, 'source_url')
return(out)
}
Expand Down
6 changes: 3 additions & 3 deletions man/wd_ident.Rd

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

18 changes: 16 additions & 2 deletions tests/testthat/test-wikidata.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,21 @@ test_that("wd_ident returns correct results", {
expect_true(is.na(o1$smiles[4]))
expect_equal(o1$cas[1], '50-29-3')
expect_equal(names(o1), c('smiles', 'cas', 'cid', 'einecs', 'csid', 'inchi', 'inchikey',
'drugbank', 'zvg', 'chebi', 'chembl', 'unii', 'source_url', 'query'))
'drugbank', 'zvg', 'chebi', 'chembl', 'unii', 'lipidmaps', 'swisslipids', 'source_url', 'query'))
})

test_that("wd_ident returns correct results for two lipids", {
skip_on_cran()
skip_if_not(up, "Wikidata service is down")

id <- c( "Q27089367", "Q410888")
o1 <- wd_ident(id)
expect_s3_class(o1, 'data.frame')
expect_equal(nrow(o1), 2)
expect_equal(names(o1), c('smiles', 'cas', 'cid', 'einecs', 'csid', 'inchi', 'inchikey',
'drugbank', 'zvg', 'chebi', 'chembl', 'unii', 'lipidmaps', 'swisslipids', 'source_url', 'query'))
expect_equal(o1$swisslipids[1], 'SLM:000000510')
expect_equal(o1$lipidmaps[2], 'LMPR0102010003')
})


Expand All @@ -51,7 +65,7 @@ test_that("wd integration test", {
f <- wd_ident(get_wdid('xxxxxxxAX', language = 'en', match = 'best')$wdid)

expect_equal(d$cas, "110-54-3")
expect_equal(ncol(d), 14)
expect_equal(ncol(d), 16)
expect_s3_class(d, 'data.frame')
expect_true(all(is.na(f[1, ])))
})

0 comments on commit 4c63e29

Please sign in to comment.