PageRenderTime 176ms CodeModel.GetById 171ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/rcdk/R/frags.R

http://github.com/rajarshi/cdkr
R | 77 lines | 39 code | 5 blank | 33 comment | 10 complexity | e13e3d5373213417ca109a3065e64c12 MD5 | raw file
 1#' Generate Bemis-Murcko Fragments
 2#' 
 3#' Fragment the input molecule using the Bemis-Murcko scheme
 4#' 
 5#' A variety of methods for fragmenting molecules are available ranging from
 6#' exhaustive, rings to more specific methods such as Murcko frameworks. Fragmenting a
 7#' collection of molecules can be a useful for a variety of analyses. In addition
 8#' fragment based analysis can be a useful and faster alternative to traditional 
 9#' clustering of the whole collection, especially when it is large.
10#' 
11#' Note that exhaustive fragmentation of large molecules (with many single bonds) can become
12#' time consuming.
13#' 
14#' @param mols A list of `jobjRef` objects of Java class `IAtomContainer`
15#' @param min.frag.size The smallest fragment to consider (in terms of heavy atoms)
16#' @param as.smiles If `TRUE` return the fragments as SMILES strings. If not, then fragments
17#' are returned as `jobjRef` objects
18#' @param single.framework If `TRUE`, then a single framework (i.e., the framework consisting of the
19#' union of all ring systems and linkers) is returned for each molecule. Otherwise, all combinations
20#' of ring systems and linkers are returned
21#' @return Returns a list with each element being a list with two elements: `rings` and
22#' `frameworks`. Each of these elements is either a character vector of SMILES strings or a list of
23#' `IAtomContainer` objects.
24#' @author Rajarshi Guha (\email{rajarshi.guha@@gmail.com})
25#' @seealso [get.exhuastive.fragments()]
26#' @export
27#' @examples 
28#' mol <- parse.smiles('c1ccc(cc1)CN(c2cc(ccc2[N+](=O)[O-])c3c(nc(nc3CC)N)N)C')[[1]]
29#' mf1 <- get.murcko.fragments(mol, as.smiles=TRUE, single.framework=TRUE)
30#' mf1 <- get.murcko.fragments(mol, as.smiles=TRUE, single.framework=FALSE)
31get.murcko.fragments <- function(mols, min.frag.size = 6, as.smiles = TRUE, single.framework = FALSE) {
32  if (!is.list(mols)) mols <- list(mols)
33  klasses <- unlist(lapply(mols, function(x) attr(x, "jclass")))
34  if (!all(klasses ==  "org/openscience/cdk/interfaces/IAtomContainer")) {
35    stop("Must supply an IAtomContainer object")
36  }
37
38  fragmenter <- .jnew("org/openscience/cdk/fragment/MurckoFragmenter",
39                      single.framework, as.integer(min.frag.size))
40  
41  ret <- lapply(mols, function(x) {
42    .jcall(fragmenter, "V", "generateFragments", x)
43    if (as.smiles) {
44      rings <- .jcall(fragmenter, "[S", "getRingSystems")
45      frames <- .jcall(fragmenter, "[S", "getFrameworks")
46    } else {
47      rings <- .jcall(fragmenter, "[Lorg/openscience/cdk/interfaces/IAtomContainer;", "getRingSystemsAsContainers")
48      frames <- .jcall(fragmenter, "[Lorg/openscience/cdk/interfaces/IAtomContainer;", "getFrameworksAsContainers")
49    }
50    return(list(rings = rings, frameworks = frames))    
51  })
52  return(ret)
53}
54
55#' @inherit get.murcko.fragments 
56#' @return returns a list of length equal to the number of input molecules. Each
57#' element is a character vector of SMILES strings or a list of `jobjRef` objects.
58get.exhaustive.fragments <- function(mols, min.frag.size = 6, as.smiles = TRUE) {
59  if (!is.list(mols)) mols <- list(mols)
60  klasses <- unlist(lapply(mols, function(x) attr(x, "jclass")))
61  if (!all(klasses ==  "org/openscience/cdk/interfaces/IAtomContainer")) {
62    stop("Must supply an IAtomContainer object")
63  }
64
65  fragmenter <- .jnew("org/openscience/cdk/fragment/ExhaustiveFragmenter", as.integer(min.frag.size))
66
67  ret <- lapply(mols, function(x) {
68    .jcall(fragmenter, "V", "generateFragments", x)
69    if (as.smiles) {
70      fragments <- .jcall(fragmenter, "[S", "getFragments")
71    } else {
72      fragments <- .jcall(fragmenter, "[Lorg/openscience/cdk/interfaces/IAtomContainer;", "getFragmentsSystemsAsContainers")
73    }
74    return(fragments)    
75  })
76  return(ret)
77}