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