PageRenderTime 43ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/rcdk/R/visual.R

http://github.com/rajarshi/cdkr
R | 274 lines | 152 code | 29 blank | 93 comment | 52 complexity | 56190e04832253be2f7cda351ed5aea8 MD5 | raw file
  1. .packageName <- "rcdk"
  2. ## draw.molecule <- function(molecule = NA) {
  3. ## if (is.na(molecule)) {
  4. ## editor <- .jnew("org/guha/rcdk/draw/Get2DStructureFromJCP")
  5. ## } else {
  6. ## if (attr(molecule, "jclass") != 'org/openscience/cdk/interfaces/IAtomContainer') {
  7. ## stop("Supplied object should be a Java reference to an IAtomContainer")
  8. ## }
  9. ## editor <- .jnew("org/guha/rcdk/draw/Get2DStructureFromJCP", molecule)
  10. ## }
  11. ## .jcall(editor, "V", "showWindow")
  12. ## molecule <- .jcall(editor, "[Lorg/openscience/cdk/interfaces/IAtomContainer;", "getMolecules")
  13. ## return(molecule)
  14. ## }
  15. ## script should be a valid Jmol script string
  16. ## view.molecule.3d <- function(molecule, ncol = 4, cellx = 200, celly = 200, script = NA) {
  17. ## if (class(molecule) != 'character' &&
  18. ## class(molecule) != 'list' &&
  19. ## class(molecule) != 'jobjRef') {
  20. ## stop("Must supply a filename, single molecule object or list of molecule objects")
  21. ## }
  22. ## if (class(molecule) == 'character') {
  23. ## molecule <- load.molecules(molecule)
  24. ## if (length(molecule) == 1) molecule <- molecule[[1]]
  25. ## }
  26. ## if (class(molecule) != 'list') { ## single molecule
  27. ## if (attr(molecule, "jclass") != 'org/openscience/cdk/interfaces/IAtomContainer') {
  28. ## stop("Supplied object should be a Java reference to an IAtomContainer")
  29. ## }
  30. ## viewer <- .jnew("org/guha/rcdk/view/ViewMolecule3D", molecule)
  31. ## .jcall(viewer, 'V', 'show')
  32. ## if (!is.na(script)) {
  33. ## .jcall(viewer, "V", "setScript", script)
  34. ## }
  35. ## } else { ## script is not run for the grid case
  36. ## array <- .jarray(molecule, contents.class="org/openscience/cdk/interfaces/IAtomContainer")
  37. ## v3d <- .jnew("org/guha/rcdk/view/ViewMolecule3DTable", array,
  38. ## as.integer(ncol), as.integer(cellx), as.integer(celly))
  39. ## .jcall(v3d, 'V', 'show')
  40. ## }
  41. ## }
  42. #' get.depictor
  43. #'
  44. #' return an RcdkDepictor.
  45. #'
  46. #' @param width Default. \code{200}
  47. #' @param height Default. \code{200}
  48. #' @param zoom Default. \code{1.3}
  49. #' @param style Default. \code{cow}
  50. #' @param annotate Default. \code{off}
  51. #' @param abbr Default. \code{on}
  52. #' @param suppressh Default. \code{TRUE}
  53. #' @param showTitle Default. \code{FALSE}
  54. #' @param smaLimit Default. \code{100}
  55. #' @param sma Default. \code{NULL}
  56. #'
  57. #' @export
  58. #'
  59. get.depictor <- function(width = 200, height = 200, zoom = 1.3, style = "cow", annotate = "off", abbr = "on",
  60. suppressh = TRUE, showTitle = FALSE, smaLimit = 100, sma = NULL) {
  61. if (is.null(sma)) sma <- ""
  62. return(.jnew("org/guha/rcdk/view/RcdkDepictor",
  63. as.integer(width),
  64. as.integer(height),
  65. as.double(zoom),
  66. as.character(style),
  67. as.character(annotate),
  68. as.character(abbr),
  69. as.logical(suppressh),
  70. as.logical(showTitle),
  71. as.integer(smaLimit),
  72. as.character(sma)))
  73. }
  74. #' view.molecule.2d
  75. #'
  76. #' Create a 2D depiction of a molecule. If there are more than
  77. #' one molecules supplied, return a grid woth \code{ncol} columns,.
  78. #'
  79. #' @param molecule The molecule to query. Should be a `jobjRef` representing an `IAtomContainer`
  80. #' @param ncol Default \code{4}
  81. #' @param width Default \code{200}
  82. #' @param height Default \code{200}
  83. #' @param depictor Default \code{NULL}
  84. #'
  85. #' @importFrom utils write.table
  86. #' @export
  87. view.molecule.2d <- function(molecule, ncol = 4, width = 200, height = 200, depictor = NULL) {
  88. if (class(molecule) != 'character' &&
  89. class(molecule) != 'list' &&
  90. class(molecule) != 'jobjRef') {
  91. stop("Must supply a filename, single molecule object or list of molecule objects")
  92. }
  93. if (is.null(depictor))
  94. depictor <- get.depictor()
  95. ## in case we're on OS X we need to prep some stuff
  96. ## so we can shell out
  97. is.osx <- Sys.info()[1] == 'Darwin'
  98. jarfile <- NULL
  99. rcdklibs <- NULL
  100. if (is.osx) {
  101. jarfile <- system.file(package='rcdk')
  102. rcdklibs <- system.file(package='rcdklibs')
  103. }
  104. ## if we got a file name, lets load all the molecules
  105. if (class(molecule) == 'character') {
  106. molecule <- load.molecules(molecule)
  107. if (length(molecule) == 1) molecule <- molecule[[1]]
  108. }
  109. if (class(molecule) != 'list') { ## single molecule
  110. if (attr(molecule, "jclass") != 'org/openscience/cdk/interfaces/IAtomContainer') {
  111. stop("Supplied object should be a Java reference to an IAtomContainer")
  112. }
  113. if (is.osx) {
  114. smi <- get.smiles(molecule)
  115. if (depictor$getSma() == "")
  116. sma = "\"\""
  117. else
  118. sma = depictor$getSma()
  119. cmd <- sprintf('java -cp \"%s/cont/*:%s/cont/rcdk.jar\" org.guha.rcdk.app.OSXHelper viewMolecule2D "%s" %d %d %f %s %s %s %s %s %d "%s" &', rcdklibs, jarfile, smi,
  120. depictor$getWidth(), depictor$getHeight(),
  121. depictor$getZoom(), depictor$getStyle(), depictor$getAnnotate(),
  122. depictor$getAbbr(), depictor$isSuppressh(), depictor$isShowTitle(),
  123. depictor$getSmaLimit(), sma)
  124. return(system(cmd))
  125. } else {
  126. v2d <- .jnew("org/guha/rcdk/view/ViewMolecule2D", molecule, as.integer(width), as.integer(height), depictor)
  127. ret <- .jcall(v2d, "V", "draw")
  128. }
  129. } else { ## multiple molecules
  130. if (is.osx) {
  131. ## write out mols as a temp smi file
  132. smi <- sapply(molecule, get.smiles, flavor = smiles.flavors(c('Isomeric')))
  133. titles <- sapply(molecule, get.title)
  134. tmp <- data.frame(smi, titles)
  135. tf <- tempfile(pattern='rcdkv-', fileext='.smi')
  136. write.table(tmp, file=tf, sep='\t', row.names=FALSE, col.names=FALSE, quote=FALSE)
  137. if (depictor$getSma() == "") {
  138. sma = "\"\""
  139. } else {
  140. sma = depictor$getSma()
  141. }
  142. cmd <- sprintf('java -cp \"%s/cont/*:%s/cont/rcdk.jar\" org.guha.rcdk.app.OSXHelper viewMolecule2Dtable "%s" %d %d %f %s %s %s %s %s %d "%s" %d &',
  143. rcdklibs, jarfile, tf,
  144. depictor$getWidth(), depictor$getHeight(),
  145. depictor$getZoom(), depictor$getStyle(), depictor$getAnnotate(),
  146. depictor$getAbbr(), depictor$isSuppressh(), depictor$isShowTitle(),
  147. depictor$getSmaLimit(), sma,
  148. ncol)
  149. return(system(cmd))
  150. } else {
  151. array <- .jarray(molecule, contents.class="org/openscience/cdk/interfaces/IAtomContainer")
  152. v2d <- .jnew("org/guha/rcdk/view/ViewMolecule2DTable", array,
  153. as.integer(ncol), depictor)
  154. }
  155. }
  156. }
  157. view.table <- function(molecules, dat, depictor = NULL) {
  158. if (!is.list(molecules)) {
  159. stop("Must provide a list of molecule objects")
  160. }
  161. if (!is.matrix(dat) && !is.data.frame(dat)) {
  162. stop("datatable must be a matrix or data.frame")
  163. }
  164. if (length(molecules) != nrow(dat)) {
  165. stop("The number of rows in datatable must be the same as the number of molecules")
  166. }
  167. if (is.null(depictor))
  168. depictor <- get.depictor()
  169. if (is.null(names(dat))) cnames <- c('Molecule', paste('V',1:ncol(dat)), sep='')
  170. else cnames <- c('Molecule', names(dat))
  171. ## we need to convert the R vectors to Java arrays
  172. ## and the datatable data.frame to an Object[][]
  173. molecules <- .jarray(molecules, "org/openscience/cdk/interfaces/IAtomContainer")
  174. carr <- .jarray(cnames)
  175. rows <- list()
  176. for (i in 1:nrow(dat)) {
  177. row <- list()
  178. ## for a given row we have to construct a Object[] and add
  179. ## it to our list
  180. for (j in 1:ncol(dat)) {
  181. if (is.numeric(dat[i,j])) {
  182. row[j] <- .jnew("java/lang/Double", dat[i,j])
  183. }
  184. else if (is.character(dat[i,j]) || is.factor(dat[i,j]) || is.logical(dat[i,j])) {
  185. row[j] <- .jnew("java/lang/String", as.character(dat[i,j]))
  186. }
  187. }
  188. rows[i] <- .jarray(row, "java/lang/Object")
  189. }
  190. ## now make our object table
  191. xval.arr <- .jarray(rows, "[Ljava/lang/Object;")
  192. obj <- .jnew("org/guha/rcdk/view/ViewMolecule2DDataTable",
  193. molecules, carr, xval.arr, depictor)
  194. .jcall(obj, "V", "display")
  195. }
  196. #' view.image.2d
  197. #'
  198. #' @param molecule The molecule to display Should be a `jobjRef` representing an `IAtomContainer`
  199. #' @param depictor Default \code{NULL}
  200. #'
  201. #' @export
  202. view.image.2d <- function(molecule, depictor = NULL) {
  203. if (is.null(depictor))
  204. depictor <- get.depictor()
  205. if (attr(molecule,"jclass") != "org/openscience/cdk/interfaces/IAtomContainer")
  206. stop("Must supply an IAtomContainer object")
  207. mi <- .jnew("org/guha/rcdk/view/MoleculeImage", molecule, depictor)
  208. bytes <- .jcall(mi, "[B", "getBytes", as.integer(depictor$getWidth()), as.integer(depictor$getHeight()), "png")
  209. return(readPNG(bytes))
  210. }
  211. #' copy.image.to.clipboard
  212. #'
  213. #' generate an image and make it available to the system
  214. #' clipboard.
  215. #'
  216. #' @param molecule The molecule to query. Should be a `jobjRef` representing an `IAtomContainer`
  217. #' @param depictor Optional. Default \code{NULL}. Depictor from \code{get.depictor}
  218. #' @export
  219. copy.image.to.clipboard <- function(molecule, depictor = NULL) {
  220. if (is.null(depictor))
  221. depictor <- get.depictor()
  222. if (Sys.info()[1] == 'Darwin') { ## try the standalone helper
  223. smi <- get.smiles(molecule)
  224. jarfile <- system.file(package='rcdk')
  225. rcdklibs <- system.file(package='rcdklibs')
  226. if (depictor$getSma() == "") {
  227. sma = "\"\""
  228. } else {
  229. sma = depictor$getSma()
  230. }
  231. cmd <- sprintf('java -cp %s/cont/*:%s/cont/rcdk.jar org.guha.rcdk.app.OSXHelper copyToClipboard "%s" %d %d %f %s %s %s %s %s %d %s',
  232. rcdklibs, jarfile, smi,
  233. depictor$getWidth(), depictor$getHeight(),
  234. depictor$getZoom(), depictor$getStyle(), depictor$getAnnotate(),
  235. depictor$getAbbr(), depictor$isSuppressh(), depictor$isShowTitle(),
  236. depictor$getSmaLimit(), sma)
  237. return(system(cmd))
  238. }
  239. if (attr(molecule,"jclass") != "org/openscience/cdk/interfaces/IAtomContainer")
  240. stop("Must supply an IAtomContainer object")
  241. .jcall('org/guha/rcdk/view/MoleculeImageToClipboard',
  242. 'V',
  243. 'copyImageToClipboard',
  244. molecule, depictor);
  245. }