PageRenderTime 47ms CodeModel.GetById 13ms app.highlight 28ms RepoModel.GetById 1ms app.codeStats 0ms

/rcdk/R/props.R

http://github.com/rajarshi/cdkr
R | 180 lines | 93 code | 13 blank | 74 comment | 38 complexity | 1b6e8d84758be66202435f2b52db485a MD5 | raw file
  1#' Set a property value of the molecule.
  2#' 
  3#' This function sets the value of a keyed property on the molecule. 
  4#' Properties enable us to associate arbitrary pieces of data with a 
  5#' molecule. Such data can be text, numeric or a Java object 
  6#' (represented as a `jobjRef`).
  7#' 
  8#' @param molecule The molecule to query. Should be a `jobjRef` representing an `IAtomContainer`
  9#' @param key The property key as a character string
 10#' @param value The value of the property. This can be a character, numeric or 
 11#' `jobjRef` R object
 12#' @seealso \code{\link{get.property}}, \code{\link{get.properties}}, \code{\link{remove.property}}
 13#' @export
 14#' @author Rajarshi Guha (\email{rajarshi.guha@@gmail.com})
 15#' @examples
 16#' mol <- parse.smiles("CC1CC(C=O)CCC1")[[1]]
 17#' set.property(mol, 'prop1', 23.45)
 18#' set.property(mol, 'prop2', 'inactive')
 19#' get.property(mol, 'prop1')
 20#' 
 21set.property <- function(molecule, key, value) {
 22  if (!is.character(key)) {
 23    stop("The property key must be a character")
 24  }
 25  if (!.check.class(molecule, "org/openscience/cdk/interfaces/IAtomContainer") &&
 26      !.check.class(molecule, "org/openscience/cdk/AtomContainer"))
 27    stop("Must supply an AtomContainer or IAtomContainer object")
 28  if (.check.class(molecule, "org/openscience/cdk/AtomContainer"))
 29    atom <- .jcast(molecule, "org/openscience/cdk/interfaces/IAtomContainer")
 30  
 31  if (is.character(value)) {
 32    value <- .jcall('org/guha/rcdk/util/Misc', 'V', 'setProperty',
 33                    molecule, as.character(key),
 34                    .jcast( .jnew("java/lang/String", value), "java/lang/Object"))
 35  } else if (is.integer(value)) {
 36    value <-.jcall('org/guha/rcdk/util/Misc', 'V', 'setProperty',
 37                   molecule, as.character(key), as.integer(value))
 38  } else if (is.double(value)) {
 39    value <-.jcall('org/guha/rcdk/util/Misc', 'V', 'setProperty',
 40                   molecule, as.character(key), as.double(value))
 41  } else if (class(value) == 'jobjRef') {
 42    value <-.jcall('org/guha/rcdk/util/Misc', 'V', 'setProperty',
 43                   molecule, as.character(key),
 44                   .jcast(value, 'java/lang/Object'))
 45  }
 46  
 47}
 48
 49#' Get a property value of the molecule.
 50#' 
 51#' This function retrieves the value of a keyed property that has
 52#' previously been set on the molecule. Properties enable us to 
 53#' associate arbitrary pieces of data with a molecule. Such data
 54#' can be text, numeric or a Java object (represented as a `jobjRef`).
 55#' 
 56#' @param molecule The molecule to query. Should be a `jobjRef` representing an `IAtomContainer`
 57#' @param key The property key as a character string
 58#' @return The value of the property. If there is no property with the specified key, `NA` is returned
 59#' @seealso \code{\link{set.property}}, \code{\link{get.properties}}
 60#' @export
 61#' @author Rajarshi Guha (\email{rajarshi.guha@@gmail.com})
 62#' @examples 
 63#' mol <- parse.smiles("CC1CC(C=O)CCC1")[[1]]
 64#' set.property(mol, 'prop1', 23.45)
 65#' set.property(mol, 'prop2', 'inactive')
 66#' get.property(mol, 'prop1')
 67get.property <- function(molecule, key) {
 68  if (is.jnull(molecule)) {
 69    warning("Molecule object was null")
 70    return(NA)
 71  }
 72  if (is.null(key) || is.na(key) || !is.character(key)) {
 73    stop("The property key must be a character")
 74  }
 75  if (!.check.class(molecule, "org/openscience/cdk/interfaces/IAtomContainer") &&
 76      !.check.class(molecule, "org/openscience/cdk/AtomContainer"))
 77    stop("Must supply an AtomContainer or IAtomContainer object")
 78  if (.check.class(molecule, "org/openscience/cdk/AtomContainer"))
 79    atom <- .jcast(molecule, "org/openscience/cdk/interfaces/IAtomContainer")
 80
 81  value <- .jcall('org/guha/rcdk/util/Misc', 'Ljava/lang/Object;', 'getProperty',
 82                  molecule, as.character(key), check=FALSE)
 83  e <- .jgetEx()
 84  if (.jcheck(silent=TRUE)) {
 85    return(NA)
 86  }
 87
 88  if (is.jnull(value)) return(NA)
 89  else return(.jsimplify(value))
 90}
 91
 92#' Get all properties associated with a molecule.
 93#' 
 94#' In this context a property is a value associated with a key and stored
 95#' with the molecule. This methd returns a list of all the properties of 
 96#' a molecule. The names of the list are set to the property names.
 97#' 
 98#' @param molecule The molecule to query. Should be a `jobjRef` representing an `IAtomContainer`
 99#' @return A named `list` with the property values. Element names are the keys 
100#' for each property. If no properties have been defined, an empty list.
101#' @seealso \code{\link{set.property}}, \code{\link{get.property}}, \code{\link{remove.property}}
102#' @export
103#' @author Rajarshi Guha (\email{rajarshi.guha@@gmail.com})
104#' @examples 
105#' mol <- parse.smiles("CC1CC(C=O)CCC1")[[1]]
106#' set.property(mol, 'prop1', 23.45)
107#' set.property(mol, 'prop2', 'inactive')
108#' get.properties(mol)
109get.properties <- function(molecule) {
110  if (!.check.class(molecule, "org/openscience/cdk/interfaces/IAtomContainer") &&
111      !.check.class(molecule, "org/openscience/cdk/AtomContainer"))
112    stop("Must supply an AtomContainer or IAtomContainer object")
113  if (.check.class(molecule, "org/openscience/cdk/AtomContainer"))
114    atom <- .jcast(molecule, "org/openscience/cdk/interfaces/IAtomContainer")
115
116  map <- .jcall(molecule, "Ljava/util/Map;", method = "getProperties")
117  keySet <- .jcall(map, "Ljava/util/Set;", method="keySet")
118  size <- .jcall(map, "I", method="size")
119  if (size == 0) return(list())
120  keyIter <- .jcall(keySet, "Ljava/util/Iterator;", method="iterator")
121  keys <- list()
122  for (i in 1:size) {
123    ##    keys[[i]] <-.jcall(keyIter, "Ljava/lang/Object;", method="next")
124    keys[[i]] <- J(keyIter, "next")
125  }
126
127  
128  values <- list()
129  for (i in 1:length(keys)) {
130    the.value <- .jcall(map, "Ljava/lang/Object;", "get", .jcast(new(J("java/lang/String"),keys[[i]]),"java/lang/Object") )
131    if (is.jnull(the.value)) values[[i]] <- NA
132    else values[[i]] <- the.value
133  }
134  
135  ret <- list()
136  for (i in 1:length(keys)) {
137    k <- keys[[i]]
138    if ('jobjRef' %in% class(values[[i]])) ret[[k]] <- .jsimplify(values[[i]])
139    else if (is.na(values[[i]])) ret[[k]] <- NA
140    else ret[[k]] <- values[[i]]
141  }
142  ret
143}
144
145#' Remove a property associated with a molecule.
146#' 
147#' In this context a property is a value associated with a key and stored
148#' with the molecule. This methd will remove the property defined by the key.
149#' If there is such key, a warning is raised.
150#' 
151#' @param molecule The molecule to query. Should be a `jobjRef` representing an `IAtomContainer`
152#' @param key The property key as a character string
153#' @seealso \code{\link{set.property}}, \code{\link{get.property}}, \code{\link{get.properties}}
154#' @export
155#' @author Rajarshi Guha (\email{rajarshi.guha@@gmail.com})
156#' @examples 
157#' mol <- parse.smiles("CC1CC(C=O)CCC1")[[1]]
158#' set.property(mol, 'prop1', 23.45)
159#' set.property(mol, 'prop2', 'inactive')
160#' get.properties(mol)
161#' remove.property(mol, 'prop2')
162#' get.properties(mol)
163remove.property <- function(molecule, key) {
164  if (!is.character(key)) {
165    stop("The property key must be a character")
166  }
167  if (!.check.class(molecule, "org/openscience/cdk/interfaces/IAtomContainer") &&
168      !.check.class(molecule, "org/openscience/cdk/AtomContainer"))
169    stop("Must supply an AtomContainer or IAtomContainer object")
170  if (.check.class(molecule, "org/openscience/cdk/AtomContainer"))
171    atom <- .jcast(molecule, "org/openscience/cdk/interfaces/IAtomContainer")
172
173  if (is.na(get.property(molecule, key))) {
174    warning("No such key exists")
175  } else {
176    value <- .jcall('org/guha/rcdk/util/Misc', 'V', 'removeProperty',
177                    molecule, as.character(key))
178  }
179}
180