R/molecule_utils.R

Defines functions smiles_to_json process_molecule mol_from_smiles

Documented in mol_from_smiles process_molecule smiles_to_json

#' mol_from_smiles
#'
#' get an AtomContainer with 2d coords from a smiles string
#'
#' @importFrom rcdk parse.smiles
#' @importFrom rcdk generate.2d.coordinates
#'
#' @return IAtomContainer with coordinates
#' @keywords internal
#'
mol_from_smiles <- function(smiles){
    smi <- parse.smiles(smiles)
    generate.2d.coordinates(smi[[1]]) #take the first of the list
}

#' process_molecule
#'
#' create JSON from an AtomContainer
#'
#' @importFrom rcdk get.atoms
#' @importFrom rcdk get.bonds
#'
#' @return list compatible with ChemDoodle Webcomponenets API
#' @keywords internal
process_molecule <- function(mol){
    #atom and bond info
    atoms = get.atoms(mol)
    bonds = get.bonds(mol)
    atomhashes <- sapply(atoms, FUN=function(x){x$hashCode()})

    process_atom <- function(atom){
        coord <- atom$getPoint2d()
        x = coord$x
        y = coord$y
        data.frame(x = coord$x,
                   y = coord$y,
                   l = atom$getSymbol())
    }

    process_bond <- function(bond, mol= mol){
        order <- bond$getOrder()$ordinal() + 1
        bondatoms <- get.atoms(bond)
        source_atom = bondatoms[[1]]
        target_atom = bondatoms[[2]]
        data.frame(o = order,
                   b = which(atomhashes == source_atom$hashCode()) - 1,
                   e = which(atomhashes == target_atom$hashCode()) - 1)
    }

    atomdata = Reduce(rbind, Map(process_atom, atoms))
    bonddata = Reduce(rbind, Map(process_bond, bonds))

    list(b = bonddata, a = atomdata )

}

#' smiles_to_json
#'
#' smiles string -> chemdoodle JSON
#'
#' @importFrom dplyr %>%
#' @export
smiles_to_json <- function(smiles){
    smiles %>%
        mol_from_smiles() %>%
        process_molecule()
}
zachcp/chemdoodle documentation built on April 13, 2021, 7:36 a.m.