Wednesday, June 6, 2012

Creating a table of molecules with R

We are using the excellent rCDK package by Rajarshi Guha,
to quickly summarize or work with chemical structures in R.

One thing that always irritated me was that the view.molecule.2d() function opened a separate Java window to display a table of molecule renderings. Because the Java window has no connection with R, this does not allow to use the rich functionality of the R graphics capabilities, while  view.image.2d() , which can return something I can use in conjunction with rasterImage(), handles only a single molecule but not lists.

library(rcdk)

# load demo data
data(bpdata)
mols <- parse.smiles(bpdata$SMILES)

# Set 10x10 molecules per page, and remove border area
par(mfrow=c(10,10))
par(mai=c(0,0,0,0)+0.1)

# Create and plot images
imgs <- lapply(mols, view.image.2d)
sapply (imgs, function(img) {plot.new(); rasterImage(img, 0,0,1,1)})

As a bonus, you can also output to a PDF or PNG file, and even set the page area for PDF to use a full print page.

Enjoy!


No comments: