From 1df33dbb3d310f94ccddbf57906e12bd6fa93c3f Mon Sep 17 00:00:00 2001 From: Johannes Ranke Date: Fri, 11 Sep 2015 10:04:54 +0200 Subject: Working version with basic functionality --- ChangeLog | 2 +- DESCRIPTION | 4 +- GNUmakefile | 4 +- NAMESPACE | 3 + R/chent.R | 172 ++++++++++++++++++++++++++++++++++++++++++++----- inst/examples/chents.R | 3 +- man/chent.Rd | 5 +- man/pp.Rd | 26 ++++++++ man/print.chent.Rd | 17 +++++ man/print.pai.Rd | 17 +++++ roxygen.log | 3 +- 11 files changed, 232 insertions(+), 24 deletions(-) create mode 100644 man/pp.Rd create mode 100644 man/print.chent.Rd create mode 100644 man/print.pai.Rd diff --git a/ChangeLog b/ChangeLog index 5a1734e..ee3afc4 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,4 +1,4 @@ -commit 11bf23fcf72ec7c537c67786fb377635ab3d3932 +commit 7cc5df2ad1e2a1aa5c6d4d9f5865491c6b30ee2a Author: Johannes Ranke Date: 2015-08-26 13:24:57 +0200 diff --git a/DESCRIPTION b/DESCRIPTION index c86a2ea..4a650e0 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,8 +1,8 @@ Package: chents Type: Package Title: Chemical Entities as R Objects -Version: 0.1-1.9000 -Date: 2015-08-26 +Version: 0.1-2 +Date: 2015-09-11 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. diff --git a/GNUmakefile b/GNUmakefile index 482ec37..5e8e5c1 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -30,13 +30,13 @@ clean: roxygen: @echo "Roxygenizing package..." - "$(R_HOME)/bin/Rscript" -e 'library(devtools); document(".")' 2>&1 | tee roxygen.log + "$(R_HOME)/bin/Rscript" -e 'library(devtools); document(".")' @echo "DONE." $(TGZ): $(pkgfiles) sed -i -e "s/Date:.*/Date: $(DATE)/" DESCRIPTION @echo "Roxygenizing package..." - "$(R_HOME)/bin/Rscript" -e 'library(devtools); document(".")' 2>&1 | tee roxygen.log + "$(R_HOME)/bin/Rscript" -e 'library(devtools); document(".")' @echo "Building package..." git log --no-merges -M --date=iso > ChangeLog "$(R_HOME)/bin/R" CMD build . diff --git a/NAMESPACE b/NAMESPACE index 4cdb514..e96d4f7 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,7 +1,10 @@ # Generated by roxygen2 (4.1.1): do not edit by hand +S3method(print,chent) +S3method(print,pai) export(chent) export(pai) +export(pp) importFrom(R6,R6Class) importFrom(webchem,cid_compinfo) importFrom(webchem,get_cid) diff --git a/R/chent.R b/R/chent.R index 90e3baf..1ef26be 100644 --- a/R/chent.R +++ b/R/chent.R @@ -30,6 +30,7 @@ #' @field smiles SMILES code, with attribute 'source' #' @field mw Molecular weight, with attribute 'source' #' @field pubchem List of information retreived from PubChem +#' @field rdkit List of information obtained with RDKit #' @example inst/examples/chents.R #' @keywords data @@ -40,20 +41,31 @@ chent <- R6Class("chent", smiles = NULL, mw = NULL, pubchem = NULL, - initialize = function(identifier, type = c("name", "smiles"), - source = c("pubchem")) { + rdkit = NULL, + initialize = function(identifier, smiles = NULL, + source = c("rdkit", "pubchem")) { self$identifier <- identifier - type = match.arg(type) - attr(self$identifier, "type") <- type - source = match.arg(source) + names(self$identifier) <- make.names(identifier) + source = match.arg(source) switch(source, pubchem = { self$try_pubchem(identifier) + }, + rdkit = { + if (is.null(smiles)) { + stop("rdkit needs smiles as input") + } else { + self$smiles <- smiles + self$get_rdkit() + self$mw <- self$rdkit$mw + attr(self$mw, "source") <- "rdkit" + } } ) invisible(self) }, try_pubchem = function(identifier) { + if (missing(identifier)) identifier <- self$identifier pubchem_cids = webchem::get_cid(identifier) if (is.na(pubchem_cids[1])) { @@ -83,19 +95,94 @@ chent <- R6Class("chent", } } }, - show = function() { - cat(" built using $identifier", self$identifier, "\n") - cat ("InChI Key $inchikey", self$inchikey, "\n") - cat ("SMILES string $smiles", self$smiles, "\n") - if (!is.null(self$mw)) cat ("Molecular weight $mw:", round(self$mw, 1), "\n") - if (!is.null(self$pubchem)) { - cat ("PubChem synonyms (first 10):\n") - print(head(self$pubchem$synonyms, n = 10L)) + get_rdkit = function() { + if (require(PythonInR)) { + id <- names(self$identifier) + try_rdkit <- try(pyImport("Chem", from = "rdkit")) + if (inherits(try_rdkit, "try-error")) { + message("Could not import RDKit in Python session") + } else { + self$rdkit <- list() + pyImport("Descriptors", from = "rdkit.Chem") + pyExec(paste0("mol = Chem.MolFromSmiles('", self$smiles, "')")) + self$rdkit$mw <- 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) + } + } + } + } else { + stop("rdkit not available as PythonInR is not installed") + } + }, + TPs = list(), + add_TP = function(x, smiles = NULL) { + if (inherits(x, "chent")) { + id <- names(x$identifier) + chent <- x + } else { + id <- make.names(x) + chent <- chent$new(x, smiles) } + self$TPs[[id]] <- chent + }, + transformations = data.frame(study_type = character(0), + TP_identifier = character(0), + max_occurrence = numeric(0), + source = character(0), + pages = character(0), + stringsAsFactors = FALSE), + add_transformation = function(study_type, TP_identifier, max_occurrence, + comment = "", source = NA, pages = NA) { + TP_name = make.names(TP_identifier) + if (!inherits(self$TPs[[TP_name]], "chent")) { + stop(paste("Please add the TP", TP_identifier, "first using chent$add_TP()")) + } + TP_chent <- self$TPs[TP_name] + if (is.numeric(pages)) pages <- paste(pages, collapse = ", ") + cn <- colnames(self$transformations) + self$transformations <- rbind(self$transformations, + data.frame(study_type = study_type, + TP_identifier = TP_identifier, + max_occurrence = max_occurrence, + comment = comment, + source = source, + pages = pages)) + }, + soil_degradation_endpoints = data.frame(destination = character(0), + DT50 = numeric(0), + comment = character(0), + pages = character(0), + stringsAsFactors = FALSE), + add_soil_degradation_endpoints = function(destination, DT50 = NA, + comment = "", pages = NA) { + i <- nrow(self$soil_degradation_endpoints) + 1 + self$soil_degradation_endpoints[i, c("destination", "comment", "pages")] <- + c(destination, comment, pages) + self$soil_degradation_endpoints[i, "DT50"] <- DT50 } ) ) +#' Printing method for chent objects +#' +#' @param x The chent object to be printed +#' @param ... Further arguments for compatibility with the S3 method +#' @export +print.chent = function(x, ...) { + cat("\n") + cat("Identifier $identifier", x$identifier, "\n") + cat ("InChI Key $inchikey", x$inchikey, "\n") + cat ("SMILES string $smiles", x$smiles, "\n") + if (!is.null(x$mw)) cat ("Molecular weight $mw:", round(x$mw, 1), "\n") + if (!is.null(x$pubchem)) { + cat ("PubChem synonyms (first 10):\n") + print(head(x$pubchem$synonyms, n = 10L)) + } +} + #' An R6 class for pesticidal active ingredients and associated data #' #' The class is initialised with an identifier which is generally an ISO common name. @@ -118,6 +205,7 @@ pai <- R6Class("pai", initialize = function(identifier, type = c("name", "smiles"), source = c("alanwood", "pubchem")) { self$identifier <- identifier + names(self$identifier) <- make.names(identifier) type = match.arg(type) attr(self$identifier, "type") <- type source = match.arg(source) @@ -137,17 +225,71 @@ pai <- R6Class("pai", # Get additional information from PubChem pubchem_cids = get_cid(identifier) self$get_pubchem(pubchem_cids[[1]]) + self$get_rdkit() } }, pubchem = { self$try_pubchem(identifier) + self$get_rdkit() } ) invisible(self) + } + ) +) + +#' Printing method for pai objects (pesticidal active ingredients) +#' +#' @param x The chent object to be printed +#' @param ... Further arguments for compatibility with the S3 method +#' @export +print.pai = function(x, ...) { + cat(" with ISO common name $iso", x$iso, "\n") + print.chent(x) + if (length(x$TPs) > 0) { + cat("\nTransformation products:\n") + print(x$TPs) + } + if (nrow(x$transformations) > 0) { + cat("\nTransformations:\n") + print(x$transformations) + } +} + +#' R6 class for holding a product with at least one active ingredient +#' +#' An R6 class for holding information about a product with at least one active ingredient +#' +#' @docType class +#' @importFrom R6 R6Class +#' @export +#' @format An \code{\link{R6Class}} generator object. +#' @field name The name of the product +#' @field ais A list of active ingredients +#' @field concentrations The concentration of the ais +#' @field concentration_units Defaults to g/L +#' @keywords data + +pp <- R6Class("pp", + public <- list( + name = NULL, + ais = list(), + concentrations = NULL, + concentration_units = NULL, + density = NULL, + density_units = "g/L", + initialize = function(name, ..., concentrations, concentration_units = "g/L", + density = 1000, density_units = "g/L") { + self$name <- name + self$ais <- list(...) + self$concentrations <- concentrations + self$density <- density + self$density_units <- density_units + names(self$concentrations) <- names(self$ais) + self$concentration_units <- concentration_units }, print = function() { - cat(" with ISO common name $iso", self$iso, "\n") - super$show() + cat(" named", self$name, "\n") } ) ) diff --git a/inst/examples/chents.R b/inst/examples/chents.R index 7fb3286..0309e2b 100644 --- a/inst/examples/chents.R +++ b/inst/examples/chents.R @@ -1,2 +1,3 @@ -oct <- chent$new("1-octanol") +oct <- chent$new("1-octanol", smiles = "CCCCCCCCO") +oct$try_pubchem() print(oct) diff --git a/man/chent.Rd b/man/chent.Rd index ed304f5..c42863d 100644 --- a/man/chent.Rd +++ b/man/chent.Rd @@ -24,9 +24,12 @@ from the internet. \item{\code{mw}}{Molecular weight, with attribute 'source'} \item{\code{pubchem}}{List of information retreived from PubChem} + +\item{\code{rdkit}}{List of information obtained with RDKit} }} \examples{ -oct <- chent$new("1-octanol") +oct <- chent$new("1-octanol", smiles = "CCCCCCCCO") +oct$try_pubchem() print(oct) } \keyword{data} diff --git a/man/pp.Rd b/man/pp.Rd new file mode 100644 index 0000000..5be85bf --- /dev/null +++ b/man/pp.Rd @@ -0,0 +1,26 @@ +% Generated by roxygen2 (4.1.1): do not edit by hand +% Please edit documentation in R/chent.R +\docType{class} +\name{pp} +\alias{pp} +\title{R6 class for holding a product with at least one active ingredient} +\format{An \code{\link{R6Class}} generator object.} +\usage{ +pp +} +\description{ +An R6 class for holding information about a product with at least one active ingredient +} +\section{Fields}{ + +\describe{ +\item{\code{name}}{The name of the product} + +\item{\code{ais}}{A list of active ingredients} + +\item{\code{concentrations}}{The concentration of the ais} + +\item{\code{concentration_units}}{Defaults to g/L} +}} +\keyword{data} + diff --git a/man/print.chent.Rd b/man/print.chent.Rd new file mode 100644 index 0000000..56f04f5 --- /dev/null +++ b/man/print.chent.Rd @@ -0,0 +1,17 @@ +% Generated by roxygen2 (4.1.1): do not edit by hand +% Please edit documentation in R/chent.R +\name{print.chent} +\alias{print.chent} +\title{Printing method for chent objects} +\usage{ +\method{print}{chent}(x, ...) +} +\arguments{ +\item{x}{The chent object to be printed} + +\item{...}{Further arguments for compatibility with the S3 method} +} +\description{ +Printing method for chent objects +} + diff --git a/man/print.pai.Rd b/man/print.pai.Rd new file mode 100644 index 0000000..d7ee332 --- /dev/null +++ b/man/print.pai.Rd @@ -0,0 +1,17 @@ +% Generated by roxygen2 (4.1.1): do not edit by hand +% Please edit documentation in R/chent.R +\name{print.pai} +\alias{print.pai} +\title{Printing method for pai objects (pesticidal active ingredients)} +\usage{ +\method{print}{pai}(x, ...) +} +\arguments{ +\item{x}{The chent object to be printed} + +\item{...}{Further arguments for compatibility with the S3 method} +} +\description{ +Printing method for pai objects (pesticidal active ingredients) +} + diff --git a/roxygen.log b/roxygen.log index f691b87..0205542 100644 --- a/roxygen.log +++ b/roxygen.log @@ -1,4 +1,3 @@ Updating chents documentation Loading chents -Writing chent.Rd -Writing pai.Rd +Writing print.pai.Rd -- cgit v1.2.1