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