From 762114a5ea6393cc73a09616b937397349c1a259 Mon Sep 17 00:00:00 2001 From: Johannes Ranke Date: Mon, 5 Dec 2016 16:00:39 +0100 Subject: Use new rdkit drawing code This is SVG only, therefore use grConvert to convert to postscript before importing using grImport. SVG size is currently hardcoded. --- DESCRIPTION | 13 +++++++------ R/chent.R | 24 +++++++++++++++++++----- man/chent.Rd | 2 ++ 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index c50c9b4..a753034 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -2,18 +2,19 @@ Package: chents Type: Package Title: Chemical Entities as R Objects Version: 0.2-5 -Date: 2016-10-14 +Date: 2016-12-05 Authors@R: c(person("Johannes", "Ranke", role = c("aut", "cre", "cph"), email = "jranke@uni-bremen.de")) Description: Utilities for dealing with chemical entities and associated - data as R objects. If Python and RDKit are installed and configured for use - with 'PythonInR', some basic chemoinformatics functions like the calculation - of molecular weight and plotting of chemical structures in R graphics are - available. + data as R objects. If Python and RDKit (> 2015.03) are installed and + configured for use with 'PythonInR', some basic chemoinformatics functions + like the calculation of molecular weight and plotting of chemical + structures in R graphics are available. Imports: webchem, R6, grImport, + grConvert, yaml Suggests: knitr, @@ -25,4 +26,4 @@ LazyData: yes Encoding: UTF-8 VignetteBuilder: knitr URL: http://cgit.jrwb.de/chents -RoxygenNote: 5.0.1 +RoxygenNote: 5.0.1.9000 diff --git a/R/chent.R b/R/chent.R index bbb46b3..b493bc2 100644 --- a/R/chent.R +++ b/R/chent.R @@ -35,6 +35,7 @@ #' @field pubchem List of information retreived from PubChem #' @field rdkit List of information obtained with RDKit, if installed and #' configured for use with PythonInR +#' @field svg SVG code #' @field Picture Graph as a \code{\link{picture}} object obtained using grImport #' @field Pict_font_size Font size as extracted from the intermediate PostScript file #' @field pdf_height Height of the MediaBox in the pdf after cropping @@ -52,6 +53,7 @@ chent <- R6Class("chent", mw = NULL, pubchem = NULL, rdkit = NULL, + svg = NULL, Picture = NULL, Pict_font_size = NULL, pdf_height = NULL, @@ -174,19 +176,31 @@ chent <- R6Class("chent", } } - # Create a grImport Picture + # Create an SVG representation PythonInR::pyImport("Draw", from = "rdkit.Chem") + PythonInR::pyImport("rdMolDraw2D", from = "rdkit.Chem.Draw") + PythonInR::pyImport("rdDepictor", from = "rdkit.Chem") + PythonInR::pyExec("rdDepictor.Compute2DCoords(mol)") + PythonInR::pyExec("d2d = rdMolDraw2D.MolDraw2DSVG(200,250)") + PythonInR::pyExec("d2d.DrawMolecule(mol)") + PythonInR::pyExec("d2d.FinishDrawing()") + self$svg <- PythonInR::pyGet("d2d.GetDrawingText()") + svgfile <- tempfile(fileext = ".svg") + writeLines(self$svg, svgfile) + + # Convert to PostScript, remembering size properties psfile <- tempfile(fileext = ".ps") - xmlfile <- tempfile(fileext = ".xml") - cmd <- paste0("Draw.MolToFile(mol, '", psfile, "')") - PythonInR::pyExec(cmd) + suppressMessages(grConvert::convertPicture(svgfile, psfile)) ps_font_line <- grep("Tm$", readLines(psfile), value = TRUE)[1] ps_font_size <- gsub(" .*$", "", ps_font_line) - self$Pict_font_size = as.numeric(ps_font_size) + + # Read in to create Picture + xmlfile <- tempfile(fileext = ".xml") PostScriptTrace(psfile, outfilename = xmlfile) unlink(paste0("capture", basename(psfile))) self$Picture <- readPicture(xmlfile) + unlink(c(xmlfile, psfile, svgfile)) } }, get_chyaml = function(repo = c("wd", "local", "web"), diff --git a/man/chent.Rd b/man/chent.Rd index 62d6e01..4957a89 100644 --- a/man/chent.Rd +++ b/man/chent.Rd @@ -29,6 +29,8 @@ python bindings are installed and configured for use with PythonInR. \item{\code{rdkit}}{List of information obtained with RDKit, if installed and configured for use with PythonInR} +\item{\code{svg}}{SVG code} + \item{\code{Picture}}{Graph as a \code{\link{picture}} object obtained using grImport} \item{\code{Pict_font_size}}{Font size as extracted from the intermediate PostScript file} -- cgit v1.2.1