PageRenderTime 52ms CodeModel.GetById 30ms app.highlight 3ms RepoModel.GetById 18ms app.codeStats 0ms

/rcdk/R/matching.R

http://github.com/rajarshi/cdkr
R | 37 lines | 26 code | 4 blank | 7 comment | 6 complexity | aa3f35954deec551ee32d7d8665063de MD5 | raw file
 1#' matches 
 2#' 
 3#' @param query Required. A SMARTSQuery
 4#' @param target Required. The molecule to query. Should be a `jobjRef` representing an `IAtomContainer`
 5#' @param return.matches Optional. Default \code{FALSE}
 6#' @export
 7matches <- function(query, target, return.matches=FALSE) {
 8  if (!is.list(target)) target <- list(target)
 9  if (!all(unlist(lapply(target, class)) == 'jobjRef'))
10    stop("targets must be a list of IAtomContainer objects or a single IAtomContainer object")
11
12  dcob <- get.chem.object.builder()
13
14  ## make an SQT
15  sqt <- new(J("org/openscience/cdk/smiles/smarts/SMARTSQueryTool"), query, dcob)
16  matchings <- unlist(lapply(target, function(z) sqt$matches(z)))
17
18  matchings <- lapply(target, function(z) {
19    status <- sqt$matches(z)
20    if (status) {
21      mappings <- sqt$getUniqueMatchingAtoms()
22      mappings <- lapply(1:mappings$size(), function(i) {
23        atomIndices <- mappings$get(as.integer(i-1))
24        atomIndinces <- .javalist.to.rlist(atomIndices)
25        sapply(atomIndices, .jsimplify)
26      })
27    } else{
28      mappings <- NULL
29    }
30    return(list(match=status, mapping=mappings))
31  })
32
33  if (!return.matches) return(unlist(lapply(matchings, "[", 1)))
34  else {
35    return(matchings)
36  }
37}