From c850ccb2c97607e7d6001cae917f63b39d577dbf Mon Sep 17 00:00:00 2001 From: gjgetzinger Date: Mon, 6 Apr 2020 14:21:56 -0400 Subject: [PATCH 1/4] Add function to query SMARTS viewer API Generate a visualization of a SMARTS string using the SMARTSvierwer API provided by ZBH-Center for Bioinformatics at Universitat Hamburg. (https://smartsview.zbh.uni-hamburg.de/rest). For a set of input SMARTS strings, an image file can be returned to R for plotting with rasterImage or directly downloaded in png, pdf or svg format. Throws warning if multiple SMARTS strings provided and download selected to avoid downloading large numbers of files. --- NAMESPACE | 1 + R/smarts_viewer.R | 84 ++++++++++++++++++++++++++++++++++++++++++++ man/smarts_viewer.Rd | 56 +++++++++++++++++++++++++++++ 3 files changed, 141 insertions(+) create mode 100644 R/smarts_viewer.R create mode 100644 man/smarts_viewer.Rd diff --git a/NAMESPACE b/NAMESPACE index 72974226..90f6a3c5 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -69,6 +69,7 @@ export(pp_query) export(ppdb) export(ppdb_parse) export(ppdb_query) +export(smarts_viewer) export(smiles) export(srs_query) export(wd_ident) diff --git a/R/smarts_viewer.R b/R/smarts_viewer.R new file mode 100644 index 00000000..3bbd5eb2 --- /dev/null +++ b/R/smarts_viewer.R @@ -0,0 +1,84 @@ +#' Query SMARTS Viewer +#' +#' Submit a query to the SMARTS viewer webservice at +#' \url{smartsview.zbh.uni-hamburg.de}, ZBH Center for Bioinformatics, +#' University of Hamburg +#' +#' @param smarts A SMARTS string to visualize +#' @param output The type of response generated (image or download). See +#' description for details. +#' @param image_format Image file format (pdf, png, or svg) +#' @param visualization_modus 1 or 2 (1 = Complete Visualization, 2 = Element +#' Symbols) +#' @param legend_option both, none, static, dynamic +#' @param filename Filename to use if output is to be downloaded +#' +#' @return Either an a raster object (output = image) suitable for adding to an +#' R plot using \code{rasterImage} or the image is downloaded in the specified +#' format to the indicated filename. +#' @export +#' +#' @examples +#' img <- smarts_viewer("[CX3](=[OX1])[OX2][CX3](=[OX1])", "image", "png", 1, "both") +#' plot(0:1,0:1, 'n') +#' rasterImage(img[[1]],0,0,1,1) +#' +#' smarts <- c("[CX3](=[OX1])[OX2][CX3](=[OX1])", "[$([nr5]:[nr5,or5,sr5]),$([nr5]:[cr5]:[nr5,or5,sr5])]", "[#6][$([NX2]=O),$(N=C=O),$(OC#N),$(SC#N)]") +#' img <- smarts_viewer(smarts, "image", "png", 1, "both") +#' par(mfcol = c(1,length(smarts))) +#' sapply(img, function(i){ +#' plot(0:1,0:1, 'n') +#' rasterImage(i,0,0,1,1) +#' }) +#' +#' smarts_viewer(smarts, output = "download", image_format = "pdf", visualization_modus = 1, legend_option = "both", filename = "test.pdf") +#' +smarts_viewer <- + function(smarts, + output = c('image', 'download'), + image_format = c('pdf', 'png', 'svg'), + visualization_modus = c(1, 2), + legend_option = c('both', 'none', 'static', 'dynamic'), + filename = NULL) { + entity_url <- "https://smartsview.zbh.uni-hamburg.de/auto" + if(output == 'download' & is.null(filename)){ + stop("Must provide a filename for output type 'download'.") + } + if (length(smarts) > 1 & + output == 'download') { + message( + paste( + "Warning:", + "Multiple SMARTS strings entered with output option = download.", + "One file per SMARTS string will be downloaded automatically.", + sep = '\n' + ) + ) + resp <- NA + while (!resp %in% c("Y", "N", "y", "n")) { + resp <- readline(prompt = "Do you wish to continue? (Y/N)") + } + stopifnot(tolower(resp) == 'y') + } + + lapply(seq(along = smarts), function(x) { + entity_query <- + paste(entity_url, + image_format, + visualization_modus, + legend_option, + utils::URLencode(smarts[x], T), + sep = '/') + response <- httr::GET(entity_query) + if (response$status_code == 200) { + if (output == 'image') { + httr::content(response, as = 'parsed', type = 'Image/png') + } else { + utils::download.file(url = response$url, + destfile = gsub(pattern = paste0('.', image_format), paste0('_', x, '.', image_format),filename)) + } + } else { + stop(httr::http_status(response)$message) + } + }) + } \ No newline at end of file diff --git a/man/smarts_viewer.Rd b/man/smarts_viewer.Rd new file mode 100644 index 00000000..49f9b9a1 --- /dev/null +++ b/man/smarts_viewer.Rd @@ -0,0 +1,56 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/smarts_viewer.R +\name{smarts_viewer} +\alias{smarts_viewer} +\title{Query SMARTS Viewer} +\usage{ +smarts_viewer( + smarts, + output = c("image", "download"), + image_format = c("pdf", "png", "svg"), + visualization_modus = c(1, 2), + legend_option = c("both", "none", "static", "dynamic"), + filename = NULL +) +} +\arguments{ +\item{smarts}{A SMARTS string to visualize} + +\item{output}{The type of response generated (image or download). See +description for details.} + +\item{image_format}{Image file format (pdf, png, or svg)} + +\item{visualization_modus}{1 or 2 (1 = Complete Visualization, 2 = Element +Symbols)} + +\item{legend_option}{both, none, static, dynamic} + +\item{filename}{Filename to use if output is to be downloaded} +} +\value{ +Either an a raster object (output = image) suitable for adding to an + R plot using \code{rasterImage} or the image is downloaded in the specified + format to the indicated filename. +} +\description{ +Submit a query to the SMARTS viewer webservice at +\url{smartsview.zbh.uni-hamburg.de}, ZBH Center for Bioinformatics, +University of Hamburg +} +\examples{ +img <- smarts_viewer("[CX3](=[OX1])[OX2][CX3](=[OX1])", "image", "png", 1, "both") +plot(0:1,0:1, 'n') +rasterImage(img[[1]],0,0,1,1) + +smarts <- c("[CX3](=[OX1])[OX2][CX3](=[OX1])", "[$([nr5]:[nr5,or5,sr5]),$([nr5]:[cr5]:[nr5,or5,sr5])]", "[#6][$([NX2]=O),$(N=C=O),$(OC#N),$(SC#N)]") +img <- smarts_viewer(smarts, "image", "png", 1, "both") +par(mfcol = c(1,length(smarts))) +sapply(img, function(i){ + plot(0:1,0:1, 'n') + rasterImage(i,0,0,1,1) +}) + +smarts_viewer(smarts, output = "download", image_format = "pdf", visualization_modus = 1, legend_option = "both", filename = "test.pdf") + +} From bede0e06d68d0ac89bdf25e776c794fe0624306b Mon Sep 17 00:00:00 2001 From: gjgetzinger Date: Tue, 7 Apr 2020 18:22:26 -0400 Subject: [PATCH 2/4] update docs --- R/smarts_viewer.R | 29 ++++++++++++++++++++++++----- man/smarts_viewer.Rd | 23 +++++++++++++++++++---- 2 files changed, 43 insertions(+), 9 deletions(-) diff --git a/R/smarts_viewer.R b/R/smarts_viewer.R index 3bbd5eb2..706a1723 100644 --- a/R/smarts_viewer.R +++ b/R/smarts_viewer.R @@ -19,20 +19,35 @@ #' @export #' #' @examples -#' img <- smarts_viewer("[CX3](=[OX1])[OX2][CX3](=[OX1])", "image", "png", 1, "both") +#' \donttest{ +#' img <- smarts_viewer( +#' "[CX3](=[OX1])[OX2][CX3](=[OX1])", +#' "image", "png", 1, "both" +#' ) #' plot(0:1,0:1, 'n') #' rasterImage(img[[1]],0,0,1,1) +#' } #' -#' smarts <- c("[CX3](=[OX1])[OX2][CX3](=[OX1])", "[$([nr5]:[nr5,or5,sr5]),$([nr5]:[cr5]:[nr5,or5,sr5])]", "[#6][$([NX2]=O),$(N=C=O),$(OC#N),$(SC#N)]") +#' \donttest{ +#' smarts <- c("[CX3](=[OX1])[OX2][CX3](=[OX1])", +#' "[$([nr5]:[nr5,or5,sr5]),$([nr5]:[cr5]:[nr5,or5,sr5])]", +#' "[#6][$([NX2]=O),$(N=C=O),$(OC#N),$(SC#N)]" +#' ) #' img <- smarts_viewer(smarts, "image", "png", 1, "both") #' par(mfcol = c(1,length(smarts))) #' sapply(img, function(i){ #' plot(0:1,0:1, 'n') #' rasterImage(i,0,0,1,1) #' }) +#' } #' -#' smarts_viewer(smarts, output = "download", image_format = "pdf", visualization_modus = 1, legend_option = "both", filename = "test.pdf") -#' +#' \donttest{ +#' smarts_viewer( +#' smarts, output = "download", +#' image_format = "pdf", +#' visualization_modus = 1, +#' legend_option = "both", filename = "test.pdf") +#' } smarts_viewer <- function(smarts, output = c('image', 'download'), @@ -75,7 +90,11 @@ smarts_viewer <- httr::content(response, as = 'parsed', type = 'Image/png') } else { utils::download.file(url = response$url, - destfile = gsub(pattern = paste0('.', image_format), paste0('_', x, '.', image_format),filename)) + destfile = gsub( + pattern = paste0('.', image_format), + paste0('_', x, '.', image_format), + filename + )) } } else { stop(httr::http_status(response)$message) diff --git a/man/smarts_viewer.Rd b/man/smarts_viewer.Rd index 49f9b9a1..457a62a3 100644 --- a/man/smarts_viewer.Rd +++ b/man/smarts_viewer.Rd @@ -39,18 +39,33 @@ Submit a query to the SMARTS viewer webservice at University of Hamburg } \examples{ -img <- smarts_viewer("[CX3](=[OX1])[OX2][CX3](=[OX1])", "image", "png", 1, "both") +\donttest{ +img <- smarts_viewer( + "[CX3](=[OX1])[OX2][CX3](=[OX1])", + "image", "png", 1, "both" + ) plot(0:1,0:1, 'n') rasterImage(img[[1]],0,0,1,1) +} -smarts <- c("[CX3](=[OX1])[OX2][CX3](=[OX1])", "[$([nr5]:[nr5,or5,sr5]),$([nr5]:[cr5]:[nr5,or5,sr5])]", "[#6][$([NX2]=O),$(N=C=O),$(OC#N),$(SC#N)]") +\donttest{ +smarts <- c("[CX3](=[OX1])[OX2][CX3](=[OX1])", +"[$([nr5]:[nr5,or5,sr5]),$([nr5]:[cr5]:[nr5,or5,sr5])]", +"[#6][$([NX2]=O),$(N=C=O),$(OC#N),$(SC#N)]" +) img <- smarts_viewer(smarts, "image", "png", 1, "both") par(mfcol = c(1,length(smarts))) sapply(img, function(i){ plot(0:1,0:1, 'n') rasterImage(i,0,0,1,1) }) +} -smarts_viewer(smarts, output = "download", image_format = "pdf", visualization_modus = 1, legend_option = "both", filename = "test.pdf") - +\donttest{ +smarts_viewer( + smarts, output = "download", + image_format = "pdf", + visualization_modus = 1, + legend_option = "both", filename = "test.pdf") +} } From de82db0634d233465a9ffc02ab5d70c6cbd441c7 Mon Sep 17 00:00:00 2001 From: gjgetzinger Date: Tue, 7 Apr 2020 18:24:45 -0400 Subject: [PATCH 3/4] update NEWS --- NEWS | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/NEWS b/NEWS index 6c0b09ce..1fa9fe61 100644 --- a/NEWS +++ b/NEWS @@ -2,7 +2,8 @@ webchem 0.5 ====================== NEW FEATURES - +* Submit a query to the ZBH Center for Bioinformatics' SMARTS viewer webservice at (https://smartsview.zbh.uni-hamburg.de) with smarts_viewer(). Retrieves images for plotting in R or downloads image in specified format. +#' University of Hamburg * Retrieve data from ChEBI (https://www.ebi.ac.uk/chebi/) webservice with chebi_lite_entity() and chebi_comp_entity(). ChEBI comprises a rich data base on chemicals with bilogical interest [contributed by @andreasLD]. * Retrieve retention indices from NIST (https://webbook.nist.gov) with nist_ri() [PR #154, contributed by @Aariq] * Get record details from US EPA Substance Registry Services (https://cdxnodengn.epa.gov/cdx-srs-rest/) with srs_query() [PR #179] From 34bc4012e885f314da5a245d6d29fa105ec93a8c Mon Sep 17 00:00:00 2001 From: gjgetzinger Date: Tue, 7 Apr 2020 20:45:51 -0400 Subject: [PATCH 4/4] update docs and README and add unit tests --- DESCRIPTION | 2 +- R/smarts_viewer.R | 11 ++++------- README.Rmd | 7 +++++++ man/smarts_viewer.Rd | 11 ++++------- tests/testthat/test-smartsview.R | 13 +++++++++++++ tests/testthat/test-test_smartsview.R | 3 +++ 6 files changed, 32 insertions(+), 15 deletions(-) create mode 100644 tests/testthat/test-smartsview.R create mode 100644 tests/testthat/test-test_smartsview.R diff --git a/DESCRIPTION b/DESCRIPTION index 9b8df094..2f0c68fa 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -35,6 +35,6 @@ Imports: dplyr, purrr Suggests: - testthat, + testthat (>= 2.1.0), rcdk RoxygenNote: 7.0.2 diff --git a/R/smarts_viewer.R b/R/smarts_viewer.R index 706a1723..de6152dc 100644 --- a/R/smarts_viewer.R +++ b/R/smarts_viewer.R @@ -1,7 +1,7 @@ #' Query SMARTS Viewer #' #' Submit a query to the SMARTS viewer webservice at -#' \url{smartsview.zbh.uni-hamburg.de}, ZBH Center for Bioinformatics, +#' \url{http://smartsview.zbh.uni-hamburg.de}, ZBH Center for Bioinformatics, #' University of Hamburg #' #' @param smarts A SMARTS string to visualize @@ -13,22 +13,20 @@ #' @param legend_option both, none, static, dynamic #' @param filename Filename to use if output is to be downloaded #' -#' @return Either an a raster object (output = image) suitable for adding to an +#' @return Either a raster object (output = image) suitable for adding to an #' R plot using \code{rasterImage} or the image is downloaded in the specified #' format to the indicated filename. #' @export #' #' @examples -#' \donttest{ +#' \dontrun{ #' img <- smarts_viewer( #' "[CX3](=[OX1])[OX2][CX3](=[OX1])", #' "image", "png", 1, "both" #' ) #' plot(0:1,0:1, 'n') #' rasterImage(img[[1]],0,0,1,1) -#' } #' -#' \donttest{ #' smarts <- c("[CX3](=[OX1])[OX2][CX3](=[OX1])", #' "[$([nr5]:[nr5,or5,sr5]),$([nr5]:[cr5]:[nr5,or5,sr5])]", #' "[#6][$([NX2]=O),$(N=C=O),$(OC#N),$(SC#N)]" @@ -39,15 +37,14 @@ #' plot(0:1,0:1, 'n') #' rasterImage(i,0,0,1,1) #' }) -#' } #' -#' \donttest{ #' smarts_viewer( #' smarts, output = "download", #' image_format = "pdf", #' visualization_modus = 1, #' legend_option = "both", filename = "test.pdf") #' } +#' smarts_viewer <- function(smarts, output = c('image', 'download'), diff --git a/README.Rmd b/README.Rmd index 79095986..9f30fcfb 100644 --- a/README.Rmd +++ b/README.Rmd @@ -345,7 +345,14 @@ smiles(wi) # smiles(etox_basic(5564)) ``` +#### SMARTS Viewer +Render a depiction of a SMARTS string (\url{https://www.daylight.com/dayhtml/doc/theory/theory.smarts.html}) by submitting a query to the SMARTS viewer web service at \url{http://smartsview.zbh.uni-hamburg.de}, ZBH Center for Bioinformatics, University of Hamburg. +```{r smartsviewer} +smarts_viewer("[CX3](=[OX1])[OX2][CX3](=[OX1])","image", "png", 1, "both") +plot(0:1,0:1, 'n') +rasterImage(img[[1]],0,0,1,1) +``` #### Misc functions diff --git a/man/smarts_viewer.Rd b/man/smarts_viewer.Rd index 457a62a3..6d243981 100644 --- a/man/smarts_viewer.Rd +++ b/man/smarts_viewer.Rd @@ -29,26 +29,24 @@ Symbols)} \item{filename}{Filename to use if output is to be downloaded} } \value{ -Either an a raster object (output = image) suitable for adding to an +Either a raster object (output = image) suitable for adding to an R plot using \code{rasterImage} or the image is downloaded in the specified format to the indicated filename. } \description{ Submit a query to the SMARTS viewer webservice at -\url{smartsview.zbh.uni-hamburg.de}, ZBH Center for Bioinformatics, +\url{http://smartsview.zbh.uni-hamburg.de}, ZBH Center for Bioinformatics, University of Hamburg } \examples{ -\donttest{ +\dontrun{ img <- smarts_viewer( "[CX3](=[OX1])[OX2][CX3](=[OX1])", "image", "png", 1, "both" ) plot(0:1,0:1, 'n') rasterImage(img[[1]],0,0,1,1) -} -\donttest{ smarts <- c("[CX3](=[OX1])[OX2][CX3](=[OX1])", "[$([nr5]:[nr5,or5,sr5]),$([nr5]:[cr5]:[nr5,or5,sr5])]", "[#6][$([NX2]=O),$(N=C=O),$(OC#N),$(SC#N)]" @@ -59,13 +57,12 @@ sapply(img, function(i){ plot(0:1,0:1, 'n') rasterImage(i,0,0,1,1) }) -} -\donttest{ smarts_viewer( smarts, output = "download", image_format = "pdf", visualization_modus = 1, legend_option = "both", filename = "test.pdf") } + } diff --git a/tests/testthat/test-smartsview.R b/tests/testthat/test-smartsview.R new file mode 100644 index 00000000..ee1ef19e --- /dev/null +++ b/tests/testthat/test-smartsview.R @@ -0,0 +1,13 @@ +context("smartsview") + +a <- smarts_viewer("[CX3](=[OX1])[OX2][CX3](=[OX1])", "image", "png", 1, "both" ) +b <- smarts_viewer("[CX3](=[OX1])[OX2][CX3](=[OX1])", "download", "png", 1, "both", "smartsview_test.png" ) + +test_that("nist returns correct results", { + skip_on_cran() + + expect_is(a[[1]], 'array') + expect_equal(b[[1]], 0) + expect_true(file.exists('smartsview_test.png')) + expect_error(smarts_viewer("[CX3](=[OX1])[OX2][CX3](=[OX1])", "download", "png", 1, "both")) +}) \ No newline at end of file diff --git a/tests/testthat/test-test_smartsview.R b/tests/testthat/test-test_smartsview.R new file mode 100644 index 00000000..8849056e --- /dev/null +++ b/tests/testthat/test-test_smartsview.R @@ -0,0 +1,3 @@ +test_that("multiplication works", { + expect_equal(2 * 2, 4) +})