From 132bd73395033a95163080517b5a7c371cf512a2 Mon Sep 17 00:00:00 2001 From: Johannes Ranke Date: Tue, 19 Feb 2019 18:17:38 +0100 Subject: Fix check for rdkit availability Also remove python-rdkit from the travis config as its version is too old on travis (Ubuntu trusty) --- .travis.yml | 2 - ChangeLog | 21 ++++++++ R/chent.R | 122 ++++++++++++++++++++++++-------------------- README.md | 20 ++------ test.log | 25 +++++---- tests/testthat/test_chent.R | 6 +++ 6 files changed, 116 insertions(+), 80 deletions(-) diff --git a/.travis.yml b/.travis.yml index 4aca61b..862f118 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,5 +3,3 @@ cache: packages r: - release - devel -apt_packages: - - python-rdkit diff --git a/ChangeLog b/ChangeLog index 65505cd..e371d83 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,24 @@ +commit aa80ce4f46f9ff1a2851ba9ba873a55a8f6ebc6d +Author: Johannes Ranke +Date: 2019-02-19 17:17:48 +0100 + + Change package URL to github to get source code links + +commit f21038189af44e9dcea8458a66ce00bbf9b6b570 +Author: Johannes Ranke +Date: 2019-02-19 17:03:58 +0100 + + Add python-rdkit to travis configuration + +commit 87f9c5315c751896c2eef1fdbe8f93c5b151577e +Author: Johannes Ranke +Date: 2019-02-19 16:02:19 +0100 + + Start using travis and update a test + + It seems the canonical SMILES for glyphosate on PubChem has dropped the + designation of the two charges + commit b3d9bf2e5571cf506b924a6fc9cbcc366c82e340 Author: Johannes Ranke Date: 2018-09-27 18:57:55 +0200 diff --git a/R/chent.R b/R/chent.R index 941b430..6a5abc7 100644 --- a/R/chent.R +++ b/R/chent.R @@ -1,4 +1,4 @@ -# Copyright (C) 2016,2017,2018 Johannes Ranke +# Copyright (C) 2016-2019 Johannes Ranke # Contact: jranke@uni-bremen.de # This file is part of the R package chents @@ -88,7 +88,7 @@ chent <- R6Class("chent", } if (rdkit) { - if(requireNamespace("PythonInR", quietly = TRUE)) { + if(rdkit_available()) { if (is.null(self$smiles)) { message("RDKit would need a SMILES code") } else { @@ -99,6 +99,8 @@ chent <- R6Class("chent", self$mw <- self$rdkit$mw attr(self$mw, "source") <- "rdkit" } + } else { + message("RDKit is not available via PythonInR") } } @@ -162,62 +164,54 @@ chent <- R6Class("chent", } }, get_rdkit = function(template = NULL) { - if (!requireNamespace("PythonInR")) - stop("PythonInR can not be loaded") - id <- names(self$identifier) - if (!PythonInR::pyIsConnected()) { - PythonInR::pyConnect() + if(!rdkit_available()) { + stop("RDKit is not available via PythonInR") } - try_rdkit <- try(PythonInR::pyImport("Chem", from = "rdkit")) - if (inherits(try_rdkit, "try-error")) { - message("Could not import RDKit in Python session") - } else { - self$rdkit <- list() - PythonInR::pyImport("Descriptors", from = "rdkit.Chem") - PythonInR::pyExec(paste0("mol = Chem.MolFromSmiles('", self$smiles[1], "')")) - self$rdkit$mw <- PythonInR::pyExecg("mw = Descriptors.MolWt(mol)", "mw") - if (!is.null(self$mw)) { - if (round(self$rdkit$mw, 1) != round(self$mw, 1)) { - message("RDKit mw is ", self$rdkit$mw) - message("mw is ", self$mw) - } + self$rdkit <- list() + PythonInR::pyImport("Descriptors", from = "rdkit.Chem") + PythonInR::pyExec(paste0("mol = Chem.MolFromSmiles('", self$smiles[1], "')")) + self$rdkit$mw <- PythonInR::pyExecg("mw = Descriptors.MolWt(mol)", "mw") + if (!is.null(self$mw)) { + if (round(self$rdkit$mw, 1) != round(self$mw, 1)) { + message("RDKit mw is ", self$rdkit$mw) + message("mw is ", self$mw) } + } - # 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)") - if (!is.null(template)) { - PythonInR::pyImport("AllChem", from = "rdkit.Chem") - PythonInR::pyExec(paste0("template = Chem.MolFromSmiles('", template, "')")) - PythonInR::pyExec("AllChem.Compute2DCoords(template)") - PythonInR::pyExec("AllChem.GenerateDepictionMatching2DStructure(mol, template)") - } - PythonInR::pyExec("d2d = rdMolDraw2D.MolDraw2DSVG(400,500)") - PythonInR::pyExec("d2d.DrawMolecule(mol)") - PythonInR::pyExec("d2d.FinishDrawing()") - self$svg <- PythonInR::pyGet("d2d.GetDrawingText()") + # 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)") + if (!is.null(template)) { + PythonInR::pyImport("AllChem", from = "rdkit.Chem") + PythonInR::pyExec(paste0("template = Chem.MolFromSmiles('", template, "')")) + PythonInR::pyExec("AllChem.Compute2DCoords(template)") + PythonInR::pyExec("AllChem.GenerateDepictionMatching2DStructure(mol, template)") + } + PythonInR::pyExec("d2d = rdMolDraw2D.MolDraw2DSVG(400,500)") + PythonInR::pyExec("d2d.DrawMolecule(mol)") + PythonInR::pyExec("d2d.FinishDrawing()") + self$svg <- PythonInR::pyGet("d2d.GetDrawingText()") - if (!requireNamespace("grConvert")) { - stop("grConvert is not available, self$Picture will not be created") - } else { - # Convert to PostScript, remembering size properties - svgfile <- tempfile(fileext = ".svg") - writeLines(self$svg, svgfile) - psfile <- tempfile(fileext = ".ps") - 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)) - } + if (!requireNamespace("grConvert")) { + stop("grConvert is not available, self$Picture will not be created") + } else { + # Convert to PostScript, remembering size properties + svgfile <- tempfile(fileext = ".svg") + writeLines(self$svg, svgfile) + psfile <- tempfile(fileext = ".ps") + 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"), @@ -607,4 +601,24 @@ pp <- R6Class("pp", } ) ) + +rdkit_available <- function() +{ + if(requireNamespace("PythonInR", quietly = TRUE)) { + if (!PythonInR::pyIsConnected()) { + PythonInR::pyConnect() + } + sink(tempfile()) + try_rdkit <- try(PythonInR::pyImport("Chem", from = "rdkit"), + silent = TRUE) + sink() + if (inherits(try_rdkit, "try-error")) { + return(FALSE) + } else { + return(TRUE) + } + } else { + return(FALSE) + } +} # vim: set ts=2 sw=2 expandtab: diff --git a/README.md b/README.md index 75b9d7e..ffa9844 100644 --- a/README.md +++ b/README.md @@ -1,21 +1,9 @@ # chents -The R package **chents** provides some utilities for working with chemical -entities in R, made available under the GNU public license. -This means: - - This program is free software: you can redistribute it and/or modify it under - the terms of the GNU General Public License as published by the Free Software - Foundation, either version 3 of the License, or (at your option) any later - version. - - This program is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS - FOR A PARTICULAR PURPOSE. See the GNU General Public License for more - details. +[![Build Status](https://travis-ci.com/jranke/chents.svg?branch=master)](https://travis-ci.com/jranke/chents) - You should have received a copy of the GNU General Public License along with - this program. If not, see +The R package **chents** provides some utilities for working with chemical +entities in R. ## Features @@ -26,6 +14,8 @@ PythonInR, some additional chemical information is computed and a 2D graph can be plotted - Additional information can be read from a local .yaml file +## Examples + Some examples are available from the [reference on jrwb.de](http://pkgdown.jrwb.de/chents/reference). diff --git a/test.log b/test.log index abb856b..9062ecc 100644 --- a/test.log +++ b/test.log @@ -1,13 +1,20 @@ Loading chents -Loading required package: testthat + +Attaching package: ‘testthat’ + +The following objects are masked from ‘package:devtools’: + + setup, test_file + Testing chents -Generation of chent objects: -Initialize Python Version 3.4.2 (default, Oct 8 2014, 10:47:48) -[GCC 4.9.1] +✔ | OK F W S | Context + ⠏ | 0 | Generation of chent objects ⠋ | 1 | Generation of chent objects ⠙ | 2 | Generation of chent objects ⠹ | 3 | Generation of chent objects ⠸ | 4 | Generation of chent objects ⠼ | 5 | Generation of chent objects ⠴ | 6 | Generation of chent objects ✔ | 6 | Generation of chent objects [2.8 s] + ⠏ | 0 | Generation of pai objects ⠋ | 1 | Generation of pai objects ⠙ | 2 | Generation of pai objects ⠹ | 3 | Generation of pai objects ⠸ | 4 | Generation of pai objects ⠼ | 5 | Generation of pai objects ⠴ | 6 | Generation of pai objects ⠦ | 7 | Generation of pai objects ⠧ | 8 | Generation of pai objects ⠇ | 9 | Generation of pai objects ✔ | 9 | Generation of pai objects [6.4 s] -Trying to get chemical information from RDKit using user SMILES -CCCCCCCCO -...... -Generation of pai objects: ......... +══ Results ═════════════════════════════════════════════════════════════════════ +Duration: 9.2 s -DONE =========================================================================== +OK: 15 +Failed: 0 +Warnings: 0 +Skipped: 0 diff --git a/tests/testthat/test_chent.R b/tests/testthat/test_chent.R index 28d120e..1fa0852 100644 --- a/tests/testthat/test_chent.R +++ b/tests/testthat/test_chent.R @@ -1,8 +1,14 @@ context("Generation of chent objects") +# Check if we can use RDKit +skip_if_no_rdkit <- function() { + if (!chents:::rdkit_available()) skip("RDKit is not available via PythonInR") +} + oct <- chent$new("1-octanol", smiles = "CCCCCCCCO", pubchem = FALSE, chyaml = FALSE) test_that("We can generate a chent object from SMILES using RDKit", { + skip_if_no_rdkit() expect_equivalent(round(oct$mw, 2), 130.23) expect_equal(names(oct$identifier), "X1.octanol") expect_equal(oct$smiles[["user"]], "CCCCCCCCO") -- cgit v1.2.1