PageRenderTime 49ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/rcdk/R/rcdk.R

http://github.com/rajarshi/cdkr
R | 517 lines | 257 code | 50 blank | 210 comment | 44 complexity | 209093b9c83566475080d702efd82b4e MD5 | raw file
  1. .packageName <- "rcdk"
  2. #' Get the default chemical object builder.
  3. #'
  4. #' The CDK employs a builder design pattern to construct
  5. #' instances of new chemical objects (e.g., atoms, bonds, parsers
  6. #' and so on). Many methods require an instance of a builder
  7. #' object to function. While most functions in this package handle
  8. #' this internally, it is useful to be able to get an instance of
  9. #' a builder object when directly working with the CDK API via
  10. #' `rJava`.
  11. #'
  12. #' This method returns an instance of the \href{http://cdk.github.io/cdk/2.2/docs/api/org/openscience/cdk/silent/SilentChemObjectBuilder.html}{SilentChemObjectBuilder}.
  13. #' Note that this is a static object that is created at package load time,
  14. #' and the same instance is returned whenever this function is called.
  15. #'
  16. #' @return An instance of \href{http://cdk.github.io/cdk/2.3/docs/api/org/openscience/cdk/silent/SilentChemObjectBuilder.html}{SilentChemObjectBuilder}
  17. #' @export
  18. #' @author Rajarshi Guha (\email{rajarshi.guha@@gmail.com})
  19. get.chem.object.builder <- function() {
  20. return(get("dcob", envir = .rcdk.GlobalEnv))
  21. }
  22. .check.class <- function(obj, klass) {
  23. !is.null(attr(obj, 'jclass')) && attr(obj, "jclass") == klass
  24. }
  25. .trim.whitespace <- function(x) {
  26. x <- gsub('^[[:space:]]+', '', x)
  27. gsub('[[:space:]]+$', '',x)
  28. }
  29. .javalist.to.rlist <- function(l) {
  30. size <- .jcall(l, "I", "size")
  31. if (size == 0) return(list())
  32. rl <- list()
  33. for (i in 1:size)
  34. rl[[i]] <- .jcall(l, "Ljava/lang/Object;", "get", as.integer(i-1))
  35. return(rl)
  36. }
  37. #'
  38. #' @import fingerprint
  39. #' @import methods
  40. #' @import rJava
  41. #' @import png
  42. #' @import iterators
  43. #' @import itertools
  44. #' @import rcdklibs
  45. #'
  46. .onLoad <- function(lib, pkg) {
  47. dlp<-Sys.getenv("DYLD_LIBRARY_PATH")
  48. if (dlp!="") { # for Mac OS X we need to remove X11 from lib-path
  49. Sys.setenv("DYLD_LIBRARY_PATH"=sub("/usr/X11R6/lib","",dlp))
  50. }
  51. jar.rcdk <- paste(lib,pkg,"cont","rcdk.jar",sep=.Platform$file.sep)
  52. jar.png <- paste(lib,pkg,"cont","com.objectplanet.image.PngEncoder.jar",sep=.Platform$file.sep)
  53. .jinit(classpath=c(jar.rcdk,jar.png))
  54. # check Java Version
  55. jv <- .jcall("java/lang/System", "S", "getProperty", "java.runtime.version")
  56. if(substr(jv, 1L, 2L) == "1.") {
  57. jvn <- as.numeric(paste0(strsplit(jv, "[.]")[[1L]][1:2], collapse = "."))
  58. if(jvn < 1.8) stop("Java >= 8 is needed for this package but not available")
  59. }
  60. ## generate some Java objects which get reused, so as to avoid repeated .jnew()
  61. nRule <- .jnew("org/openscience/cdk/formula/rules/NitrogenRule");
  62. rdbeRule <- .jnew("org/openscience/cdk/formula/rules/RDBERule");
  63. assign(".rcdk.GlobalEnv", new.env(parent = emptyenv()), envir = topenv())
  64. assign("nRule", nRule, envir = .rcdk.GlobalEnv)
  65. assign("rdbeRule", rdbeRule, envir = .rcdk.GlobalEnv)
  66. assign("dcob", .jcall("org/openscience/cdk/silent/SilentChemObjectBuilder",
  67. "Lorg/openscience/cdk/interfaces/IChemObjectBuilder;",
  68. "getInstance"), envir = .rcdk.GlobalEnv)
  69. assign("mfManipulator", .jnew("org/openscience/cdk/tools/manipulator/MolecularFormulaManipulator"), envir = .rcdk.GlobalEnv)
  70. # Extract the bond order enums so we can return them without going through
  71. # Java each time we want one
  72. assign("BOND_ORDER_SINGLE", J("org.openscience.cdk.interfaces.IBond")$Order$SINGLE,
  73. envir = .rcdk.GlobalEnv)
  74. assign("BOND_ORDER_DOUBLE", J("org.openscience.cdk.interfaces.IBond")$Order$DOUBLE,
  75. envir = .rcdk.GlobalEnv)
  76. assign("BOND_ORDER_TRIPLE", J("org.openscience.cdk.interfaces.IBond")$Order$TRIPLE,
  77. envir = .rcdk.GlobalEnv)
  78. assign("BOND_ORDER_UNSET", J("org.openscience.cdk.interfaces.IBond")$Order$UNSET,
  79. envir = .rcdk.GlobalEnv)
  80. assign("BOND_ORDER_QUADRUPLE", J("org.openscience.cdk.interfaces.IBond")$Order$QUADRUPLE,
  81. envir = .rcdk.GlobalEnv)
  82. assign("BOND_ORDER_QUINTUPLE", J("org.openscience.cdk.interfaces.IBond")$Order$QUINTUPLE,
  83. envir = .rcdk.GlobalEnv)
  84. assign("BOND_ORDER_SEXTUPLE", J("org.openscience.cdk.interfaces.IBond")$Order$SEXTUPLE,
  85. envir = .rcdk.GlobalEnv)
  86. }
  87. #' Get the current CDK version used in the package.
  88. #'
  89. #' @return Returns a character containing the version of the CDK used in this package
  90. #' @export
  91. #' @author Rajarshi Guha (\email{rajarshi.guha@@gmail.com})
  92. cdk.version <- function() {
  93. .jcall("org.openscience.cdk.CDK", "S", "getVersion")
  94. }
  95. #' Remove explicit hydrogens.
  96. #'
  97. #' Create an copy of the original structure with explicit hydrogens removed.
  98. #' Stereochemistry is updated but up and down bonds in a depiction may need
  99. #' to be recalculated. This can also be useful for descriptor calculations.
  100. #'
  101. #' @param mol The molecule to query. Should be a `jobjRef` representing an `IAtomContainer`
  102. #' @return A copy of the original molecule, with explicit hydrogens removed
  103. #' @seealso \code{\link{get.hydrogen.count}}, \code{\link{get.total.hydrogen.count}}
  104. #' @export
  105. #' @author Rajarshi Guha (\email{rajarshi.guha@@gmail.com})
  106. remove.hydrogens <- function(mol) {
  107. if (!.check.class(mol, "org/openscience/cdk/interfaces/IAtomContainer"))
  108. stop("molecule must be of class IAtomContainer")
  109. newmol <- .jcall('org/openscience/cdk/tools/manipulator/AtomContainerManipulator',
  110. 'Lorg/openscience/cdk/interfaces/IAtomContainer;',
  111. 'removeHydrogens',
  112. mol);
  113. newmol
  114. }
  115. #' Get total number of implicit hydrogens in the molecule.
  116. #'
  117. #' Counts the number of hydrogens on the provided molecule. As this method
  118. #' will sum all implicit hydrogens on each atom it is important to ensure
  119. #' the molecule has already been configured (and thus each atom has an
  120. #' implicit hydrogen count).
  121. #'
  122. #' @param mol The molecule to query. Should be a `jobjRef` representing an `IAtomContainer`
  123. #' @return An integer representing the total number of implicit hydrogens
  124. #' @seealso \code{\link{get.hydrogen.count}}, \code{\link{remove.hydrogens}}
  125. #' @export
  126. #' @author Rajarshi Guha (\email{rajarshi.guha@@gmail.com})
  127. get.total.hydrogen.count <- function(mol) {
  128. if (!.check.class(mol, "org/openscience/cdk/interfaces/IAtomContainer"))
  129. stop("molecule must be of class IAtomContainer")
  130. .jcall('org/openscience/cdk/tools/manipulator/AtomContainerManipulator',
  131. 'I',
  132. 'getTotalHydrogenCount',
  133. mol);
  134. }
  135. #' get.exact.mass
  136. #'
  137. #' @param mol The molecule to query. Should be a `jobjRef` representing an `IAtomContainer`
  138. #' @export
  139. get.exact.mass <- function(mol) {
  140. if (!.check.class(mol, "org/openscience/cdk/interfaces/IAtomContainer"))
  141. stop("molecule must be of class IAtomContainer")
  142. formulaJ <- .jcall('org/openscience/cdk/tools/manipulator/MolecularFormulaManipulator',
  143. "Lorg/openscience/cdk/interfaces/IMolecularFormula;",
  144. "getMolecularFormula",
  145. mol,
  146. use.true.class=FALSE);
  147. ret <- .jcall('org/openscience/cdk/tools/manipulator/MolecularFormulaManipulator',
  148. 'D',
  149. 'getTotalExactMass',
  150. formulaJ,
  151. check=FALSE)
  152. ex <- .jgetEx(clear=TRUE)
  153. if (is.null(ex)) return(ret)
  154. else{
  155. print(ex)
  156. stop("Couldn't get exact mass. Maybe you have not performed aromaticity, atom type or isotope configuration?")
  157. }
  158. }
  159. #' get.natural.mass
  160. #'
  161. #' @param mol The molecule to query. Should be a `jobjRef` representing an `IAtomContainer`
  162. #' @export
  163. get.natural.mass <- function(mol) {
  164. if (!.check.class(mol, "org/openscience/cdk/interfaces/IAtomContainer"))
  165. stop("molecule must be of class IAtomContainer")
  166. ret <- .jcall('org/openscience/cdk/tools/manipulator/AtomContainerManipulator',
  167. 'D',
  168. 'getNaturalExactMass',
  169. mol,
  170. check=FALSE)
  171. ex <- .jgetEx(clear=TRUE)
  172. if (is.null(ex)) return(ret)
  173. else{
  174. print(ex)
  175. stop("Couldn't get natural mass. Maybe you have not performed aromaticity, atom type or isotope configuration?")
  176. }
  177. }
  178. #' get.total.charge
  179. #'
  180. #' @param mol The molecule to query. Should be a `jobjRef` representing an `IAtomContainer`
  181. #' @export
  182. get.total.charge <- function(mol) {
  183. if (!.check.class(mol, "org/openscience/cdk/interfaces/IAtomContainer"))
  184. stop("molecule must be of class IAtomContainer")
  185. ## check to see if we have partial charges
  186. atoms <- get.atoms(mol)
  187. pcharges <- unlist(lapply(atoms, get.charge))
  188. ## If any are null, partial charges were not set, so
  189. ## just return the total formal charge
  190. if (any(is.null(pcharges))) return(get.total.formal.charge(mol))
  191. else {
  192. .jcall('org/openscience/cdk/tools/manipulator/AtomContainerManipulator',
  193. 'D',
  194. 'getTotalCharge',
  195. mol);
  196. }
  197. }
  198. #' get.total.formal.charge
  199. #'
  200. #' @param mol The molecule to query. Should be a `jobjRef` representing an `IAtomContainer`
  201. #' @export
  202. get.total.formal.charge <- function(mol) {
  203. if (!.check.class(mol, "org/openscience/cdk/interfaces/IAtomContainer"))
  204. stop("molecule must be of class IAtomContainer")
  205. .jcall('org/openscience/cdk/tools/manipulator/AtomContainerManipulator',
  206. 'I',
  207. 'getTotalFormalCharge',
  208. mol);
  209. }
  210. #' Convert implicit hydrogens to explicit.
  211. #'
  212. #' In some cases, a molecule may not have any hydrogens (such as when read
  213. #' in from an MDL MOL file that did not have hydrogens or SMILES with no
  214. #' explicit hydrogens). In such cases, this method
  215. #' will add implicit hydrogens and then convert them to explicit ones. The
  216. #' newly added H's will not have any 2D or 3D coordinates associated with them.
  217. #' Ensure that the molecule has been typed beforehand.
  218. #'
  219. #' @param mol The molecule to query. Should be a `jobjRef` representing an `IAtomContainer`
  220. #' @seealso \code{\link{get.hydrogen.count}}, \code{\link{remove.hydrogens}}, \code{\link{do.typing}}
  221. #' @export
  222. #' @author Rajarshi Guha (\email{rajarshi.guha@@gmail.com})
  223. convert.implicit.to.explicit <- function(mol) {
  224. if (!.check.class(mol, "org/openscience/cdk/interfaces/IAtomContainer"))
  225. stop("molecule must be of class IAtomContainer")
  226. if (any(is.null(unlist(lapply(get.atoms(mol), .jcall, returnSig = "Ljava/lang/Integer;", method="getImplicitHydrogenCount"))))) {
  227. ## add them in
  228. dcob <- get.chem.object.builder()
  229. hadder <- .jcall("org/openscience/cdk/tools/CDKHydrogenAdder", "Lorg/openscience/cdk/tools/CDKHydrogenAdder;",
  230. "getInstance", dcob)
  231. .jcall(hadder, "V", "addImplicitHydrogens", mol)
  232. }
  233. .jcall('org/openscience/cdk/tools/manipulator/AtomContainerManipulator', 'V', 'convertImplicitToExplicitHydrogens', mol)
  234. }
  235. #' Get the atoms from a molecule or bond.
  236. #'
  237. #' @param object A `jobjRef` representing either a molecule (`IAtomContainer`) or
  238. #' bond (`IBond`) object.
  239. #' @return A list of `jobjRef` representing the `IAtom` objects in the molecule or bond
  240. #' @seealso \code{\link{get.bonds}}, \code{\link{get.connected.atoms}}
  241. #' @export
  242. #' @author Rajarshi Guha (\email{rajarshi.guha@@gmail.com})
  243. get.atoms <- function(object) {
  244. if (is.null(attr(object, 'jclass')))
  245. stop("object must be of class IAtomContainer or IObject or IBond")
  246. if (attr(object, 'jclass') != "org/openscience/cdk/interfaces/IAtomContainer" &&
  247. attr(object, 'jclass') != "org/openscience/cdk/interfaces/IObject" &&
  248. attr(object, 'jclass') != "org/openscience/cdk/interfaces/IBond")
  249. stop("object must be of class IAtomContainer or IObject or IBond")
  250. natom <- .jcall(object, "I", "getAtomCount")
  251. atoms <- list()
  252. for (i in 0:(natom-1))
  253. atoms[[i+1]] <- .jcall(object, "Lorg/openscience/cdk/interfaces/IAtom;", "getAtom", as.integer(i))
  254. atoms
  255. }
  256. #' Get the bonds in a molecule.
  257. #'
  258. #' @param mol A `jobjRef` representing the molecule (`IAtomContainer`) object.
  259. #' @return A list of `jobjRef` representing the bonds (`IBond`) objects in the molecule
  260. #' @seealso \code{\link{get.atoms}}, \code{\link{get.connected.atoms}}
  261. #' @export
  262. #' @author Rajarshi Guha (\email{rajarshi.guha@@gmail.com})
  263. get.bonds <- function(mol) {
  264. if (!.check.class(mol, "org/openscience/cdk/interfaces/IAtomContainer"))
  265. stop("molecule must be of class IAtomContainer")
  266. nbond <- .jcall(mol, "I", "getBondCount")
  267. bonds <- list()
  268. for (i in 0:(nbond-1))
  269. bonds[[i+1]] <- .jcall(mol, "Lorg/openscience/cdk/interfaces/IBond;", "getBond", as.integer(i))
  270. bonds
  271. }
  272. #' do.aromaticity
  273. #'
  274. #' detect aromaticity of an input compound
  275. #'
  276. #' @param mol The molecule to query. Should be a `jobjRef` representing an `IAtomContainer`
  277. #' @export
  278. do.aromaticity <- function(mol) {
  279. if (!.check.class(mol, "org/openscience/cdk/interfaces/IAtomContainer"))
  280. stop("molecule must be of class IAtomContainer")
  281. model <- .jcall("org/openscience/cdk/aromaticity/ElectronDonation",
  282. "Lorg/openscience/cdk/aromaticity/ElectronDonation;",
  283. "daylight")
  284. cycles.all <- .jcall("org/openscience/cdk/graph/Cycles",
  285. "Lorg/openscience/cdk/graph/CycleFinder;",
  286. "all")
  287. cycles.6 <- .jcall("org.openscience.cdk.graph.Cycles",
  288. "Lorg/openscience/cdk/graph/CycleFinder;",
  289. "all", as.integer(6))
  290. cycles <- .jcall("org.openscience.cdk.graph.Cycles",
  291. "Lorg/openscience/cdk/graph/CycleFinder;",
  292. "or", cycles.all, cycles.6)
  293. aromaticity <- .jnew("org/openscience/cdk.aromaticity/Aromaticity",
  294. model, cycles)
  295. .jcall(aromaticity, "Z", "apply", mol)
  296. }
  297. #' do.typing
  298. #'
  299. #' configure atom typings.
  300. #'
  301. #' @param mol The molecule to query. Should be a `jobjRef` representing an `IAtomContainer`
  302. #' @export
  303. do.typing <- function(mol) {
  304. if (!.check.class(mol, "org/openscience/cdk/interfaces/IAtomContainer"))
  305. stop("molecule must be of class IAtomContainer")
  306. .jcall("org.openscience.cdk.tools.manipulator.AtomContainerManipulator",
  307. "V", "percieveAtomTypesAndConfigureAtoms", mol)
  308. }
  309. #' do.isotopes
  310. #'
  311. #' configure isotopes
  312. #'
  313. #' @param mol The molecule to query. Should be a `jobjRef` representing an `IAtomContainer`
  314. #' @export
  315. do.isotopes <- function(mol) {
  316. if (!.check.class(mol, "org/openscience/cdk/interfaces/IAtomContainer"))
  317. stop("molecule must be of class IAtomContainer")
  318. ifac <- .jcall('org.openscience.cdk.config.Isotopes',
  319. 'Lorg/openscience/cdk/config/Isotopes;',
  320. 'getInstance')
  321. .jcall(ifac, 'V', 'configureAtoms', mol)
  322. }
  323. #' Tests whether the molecule is neutral.
  324. #'
  325. #' The test checks whether all atoms in the molecule have a formal charge of 0.
  326. #'
  327. #' @param mol The molecule to query. Should be a `jobjRef` representing an `IAtomContainer`
  328. #' @return `TRUE` if molecule is neutral, `FALSE` otherwise
  329. #' @aliases charge
  330. #' @export
  331. #' @author Rajarshi Guha (\email{rajarshi.guha@@gmail.com})
  332. is.neutral <- function(mol) {
  333. if (!.check.class(mol, "org/openscience/cdk/interfaces/IAtomContainer"))
  334. stop("molecule must be of class IAtomContainer")
  335. atoms <- get.atoms(mol)
  336. fc <- unlist(lapply(atoms, get.formal.charge))
  337. return(all(fc == 0))
  338. }
  339. #' Tests whether the molecule is fully connected.
  340. #'
  341. #' A single molecule will be represented as a
  342. #' \href{https://en.wikipedia.org/wiki/Complete_graph}{complete} graph.
  343. #' In some cases, such as for molecules in salt form, or after certain
  344. #' operations such as bond splits, the molecular graph may contained
  345. #' \href{http://mathworld.wolfram.com/DisconnectedGraph.html}{disconnected components}.
  346. #' This method can be used to tested whether the molecule is complete (i.e. fully
  347. #' connected).
  348. #'
  349. #' @param mol The molecule to query. Should be a `jobjRef` representing an `IAtomContainer`
  350. #' @return `TRUE` if molecule is complete, `FALSE` otherwise
  351. #' @seealso \code{\link{get.largest.component}}
  352. #' @export
  353. #' @author Rajarshi Guha (\email{rajarshi.guha@@gmail.com})
  354. #' @examples
  355. #' m <- parse.smiles("CC.CCCCCC.CCCC")[[1]]
  356. #' is.connected(m)
  357. is.connected <- function(mol) {
  358. if (!.check.class(mol, "org/openscience/cdk/interfaces/IAtomContainer"))
  359. stop("molecule must be of class IAtomContainer")
  360. .jcall("org.openscience.cdk.graph.ConnectivityChecker",
  361. "Z", "isConnected", mol)
  362. }
  363. #' Gets the largest component in a disconnected molecular graph.
  364. #'
  365. #' A molecule may be represented as a
  366. #' \href{http://mathworld.wolfram.com/DisconnectedGraph.html}{disconnected graph}, such as
  367. #' when read in as a salt form. This method will return the larges connected component
  368. #' or if there is only a single component (i.e., the molecular graph is
  369. #' \href{https://en.wikipedia.org/wiki/Complete_graph}{complete} or fully connected), that
  370. #' component is returned.
  371. #'
  372. #' @param mol The molecule to query. Should be a `jobjRef` representing an `IAtomContainer`
  373. #' @return The largest component as an `IAtomContainer` object or else the input molecule itself
  374. #' @seealso \code{\link{is.connected}}
  375. #' @export
  376. #' @author Rajarshi Guha (\email{rajarshi.guha@@gmail.com})
  377. #' @examples
  378. #' m <- parse.smiles("CC.CCCCCC.CCCC")[[1]]
  379. #' largest <- get.largest.component(m)
  380. #' length(get.atoms(largest)) == 6
  381. get.largest.component <- function(mol) {
  382. if (!.check.class(mol, "org/openscience/cdk/interfaces/IAtomContainer"))
  383. stop("molecule must be of class IAtomContainer")
  384. isConnected <- .jcall("org.openscience.cdk.graph.ConnectivityChecker",
  385. "Z", "isConnected", mol)
  386. if (isConnected) return(mol)
  387. molSet <- .jcall("org.openscience.cdk.graph.ConnectivityChecker",
  388. "Lorg/openscience/cdk/interfaces/IAtomContainerSet;",
  389. "partitionIntoMolecules", mol)
  390. ncomp <- .jcall(molSet, "I", "getAtomContainerCount")
  391. max.idx <- -1
  392. max.atom.count <- -1
  393. for (i in seq_len(ncomp)) {
  394. m <- .jcall(molSet, "Lorg/openscience/cdk/interfaces/IAtomContainer;",
  395. "getAtomContainer", as.integer(i-1))
  396. natom <- .jcall(m, "I", "getAtomCount")
  397. if (natom > max.atom.count) {
  398. max.idx <- i
  399. max.atom.count <- natom
  400. }
  401. }
  402. m <- .jcall(molSet, "Lorg/openscience/cdk/interfaces/IAtomContainer;",
  403. "getAtomContainer", as.integer(max.idx-1))
  404. .jcast(m, "org/openscience/cdk/interfaces/IAtomContainer")
  405. }
  406. #' Get the number of atoms in the molecule.
  407. #'
  408. #' @param mol The molecule to query. Should be a `jobjRef` representing an `IAtomContainer`
  409. #' @return An integer representing the number of atoms in the molecule
  410. #' @export
  411. #' @author Rajarshi Guha (\email{rajarshi.guha@@gmail.com})
  412. get.atom.count <- function(mol) {
  413. if (!.check.class(mol, "org/openscience/cdk/interfaces/IAtomContainer"))
  414. stop("molecule must be of class IAtomContainer")
  415. .jcall(mol, "I", "getAtomCount")
  416. }
  417. #' Get the title of the molecule.
  418. #'
  419. #' Some molecules may not have a title (such as when parsing in a SMILES
  420. #' with not title).
  421. #'
  422. #' @param mol The molecule to query. Should be a `jobjRef` representing an `IAtomContainer`
  423. #' @return A character string with the title, `NA` is no title is specified
  424. #' @seealso \code{\link{set.title}}
  425. #' @export
  426. #' @author Rajarshi Guha (\email{rajarshi.guha@@gmail.com})
  427. get.title <- function(mol) {
  428. if (!.check.class(mol, "org/openscience/cdk/interfaces/IAtomContainer"))
  429. stop("molecule must be of class IAtomContainer")
  430. get.property(mol, "cdk:Title")
  431. }
  432. #' Set the title of the molecule.
  433. #'
  434. #' @param mol The molecule to query. Should be a `jobjRef` representing an `IAtomContainer`
  435. #' @param title The title of the molecule as a character string. This will overwrite
  436. #' any pre-existing title. The default value is an empty string.
  437. #' @seealso \code{\link{get.title}}
  438. #' @export
  439. #' @author Rajarshi Guha (\email{rajarshi.guha@@gmail.com})
  440. set.title <- function(mol, title = "") {
  441. if (!.check.class(mol, "org/openscience/cdk/interfaces/IAtomContainer"))
  442. stop("molecule must be of class IAtomContainer")
  443. set.property(mol, "cdk:Title", title)
  444. }
  445. #' Generate 2D coordinates for a molecule.
  446. #'
  447. #' Some file formats such as SMILES do not support 2D (or 3D) coordinates
  448. #' for the atoms. Other formats such as SD or MOL have support for coordinates
  449. #' but may not include them. This method will generate reasonable 2D coordinates
  450. #' based purely on connectivity information, overwriting
  451. #' any existing coordinates if present.
  452. #'
  453. #' Note that when depicting a molecule (\code{\link{view.molecule.2d}}), 2D coordinates
  454. #' are generated, but since it does not modify the input molecule, we do not have access
  455. #' to the generated coordinates.
  456. #'
  457. #' @param mol The molecule to query. Should be a `jobjRef` representing an `IAtomContainer`
  458. #' @return The input molecule, with 2D coordinates added
  459. #' @seealso \code{\link{get.point2d}}, \code{\link{view.molecule.2d}}
  460. #' @export
  461. #' @author Rajarshi Guha (\email{rajarshi.guha@@gmail.com})
  462. generate.2d.coordinates <- function(mol) {
  463. if (!.check.class(mol, "org/openscience/cdk/interfaces/IAtomContainer"))
  464. stop("molecule must be of class IAtomContainer")
  465. .jcall('org/guha/rcdk/util/Misc', 'Lorg/openscience/cdk/interfaces/IAtomContainer;',
  466. 'getMoleculeWithCoordinates', mol)
  467. }