Skip to content

Commit

Permalink
- update highlight function (#16)
Browse files Browse the repository at this point in the history
- add a few more examples
  • Loading branch information
zachcp authored Nov 23, 2022
1 parent 58a6031 commit 1801a98
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 15 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: depict
Type: Package
Title: Draw Beautiful Molecular Structures
Version: 0.3.0
Version: 0.3.1
Authors@R: c(person('Zachary', 'Charlop-Powers', role=c('cre','aut',"cph"), email='[email protected]'))
Description: Generate images of molecules using the Depiction API of the 'Chemistry Development Kit'.
CDK's Depict Api
Expand All @@ -14,7 +14,7 @@ Depends:
Imports:
png,
magrittr
RoxygenNote: 7.2.1
RoxygenNote: 7.2.2
Suggests:
knitr,
rmarkdown,
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# depict 0.3.1-dev

* extend match_smarts to work on collections of IAtomContainers

# depict 0.3.0

* modify `match_smarts` to use `toChemObjects` methodinstead of `toAtomBondMap` due to an issue with Java reflection.
Expand Down
46 changes: 34 additions & 12 deletions R/selection_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#' Find matches to a given molecule using a SMARTs pattern.
#'
#' @param smarts a SMARTS string
#' @param mol a CDK IAtomContainer
#' @param mol a CDK IAtomContainer or a java.util.Arraylist of them
#' @param limit limit of the number of matches
#'
#' @seealso \url{https://cdk.github.io/cdk/latest/docs/api/org/openscience/cdk/isomorphism/Mappings.html}
Expand All @@ -14,22 +14,44 @@
#'
#'
match_smarts <- function(smarts, mol, limit=10) {


hashset <- J("java/util/HashSet")
smartspattern <- J("org/openscience/cdk/smiles/smarts/SmartsPattern")
spattern <- smartspattern$create(smarts, NULL)

matches <- spattern$matchAll(mol)
matches <- matches$limit(as.integer(limit))

# Note: switching below to chemobjects due to reflection access with the atom-bond map
# atombondmap <- matches$uniqueAtoms()$toAtomBondMap()
atombonds <- matches$uniqueAtoms()$toChemObjects()

# somewhat convoluted code to get all of the matching atoms
highlight <- new(hashset)
# handle the base case first
if (!.jclass(mol) == "java.util.ArrayList") {

matches <- spattern$matchAll(mol)
matches <- matches$limit(as.integer(limit))
# Note: switching below to chemobjects due to reflection access with the atom-bond map
# atombondmap <- matches$uniqueAtoms()$toAtomBondMap()
atombonds <- matches$uniqueAtoms()$toChemObjects()

# somewhat convoluted code to get all of the matching atoms
highlight <- new(hashset)

for (l in as.list(atombonds)) {
highlight$add(l)
}

for (l in as.list(atombonds)) {
highlight$add(l)
# then handle the array list of atom containers
} else {

# somewhat convoluted code to get all of the matching atoms
highlight <- new(hashset)

for (atmcont in as.list(mol)) {
matches <- spattern$matchAll(atmcont)
matches <- matches$limit(as.integer(limit))
# Note: switching below to chemobjects due to reflection access with the atom-bond map
# atombondmap <- matches$uniqueAtoms()$toAtomBondMap()
atombonds <- matches$uniqueAtoms()$toChemObjects()
for (l in as.list(atombonds)) {
highlight$add(l)
}
}
}

highlight
Expand Down
2 changes: 1 addition & 1 deletion man/match_smarts.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions vignettes/Basic-Use.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,46 @@ depiction() %>%
```


## SMARTS for highlighting mutiple containers

```{r}
small_highlight <- match_smarts("C1CCC1", many_containers)
depiction() %>%
highlight_atoms(small_highlight, color$RED) %>%
set_size(400, 400) %>%
set_zoom(3) %>%
outerglow() %>%
add_title() %>%
depict(many_containers) %>%
get_image() %>%
grid::grid.raster()
```

## Multiple Colors

```{r}
mol <- parse_smiles(paste("CC(n1c(C)ncc1c1ccnc(n1)Nc1ccc(cc1)S(=O)(=O)C)C",
"CMGC:CDC2:6gu3:A:FB8", sep=" "))
# imid <- 'c1cnc[nH]1' #1H-imidazole
highlight_imid <- match_smarts("c1cncn1", mol)
highlight_2 <- match_smarts("n1cccnc1Nc2ccccc2", mol)
color <- J("java.awt.Color")
depiction() |>
highlight_atoms(highlight_imid, color$GREEN) |>
highlight_atoms(highlight_2, color$RED) |>
set_size(400, 400) |>
set_zoom(4) |>
outerglow() |>
add_title() |>
depict(mol) |>
get_image() |>
grid::grid.raster()
```

## A Larger Example

Expand Down

0 comments on commit 1801a98

Please sign in to comment.