Friday, 9 March 2012

Han's cells and Grätzel

My PhD supervisor, Han Vos, marked his retirement from Dublin City University this week with a symposium at Smart Surfaces 2012, a conference on solar energy devices and sensors. During my PhD I did some experimental and computational work related to dye-sensitised solar cells, and it was great to finally get to see Michael Grätzel in person talking about new developments in the field (which he originated).

Han was a great supervisor, and as a wet-behind-the-ears postgrad I think I learnt a lot from him. Something that I'd like to pass on to any postgrads reading this is to avoid at all costs co-authoring a review with a distinguished professor unless you have an impressive biography. Consider mine from back in 2005 (click for a bigger image) ...
Image Credits:
(1) W. R. Browne, N. M. O'Boyle, J. J. McGarvey and J. G. Vos. Elucidating excited state electronic structure and intercomponent interactions in multicomponent and supramolecular systems Chem. Soc. Rev. 2005, 34, 641-663. Reproduced by permission of The Royal Society of Chemistry.
(2) "Piled Higher and Deeper" by Jorge Cham. www.phdcomics.com. Reproduced with permission.

Create animated graphs in a GIFfy

Although animated GIFs are "so 90s", they are quite handy for use in presentations as they work out of the box in Powerpoint (on Windows anyway), or alternatively, they can be displayed in the browser of your choice.

Here's an animated GIF I created to show the progress of a Genetic Algorithm towards creating a molecule with a specific electronic structure (for more details on the background, see these posts [1] and [2]):
To make this sort of animation, the first step is to make a set of graphs. I use Matplotlib from Python for plotting, so I have a loop looking something like the following:
vals = range(0, 10) + range(19, 100 ,10)
for N in vals:
     homo, trans = self.xy[N]
     lumo = [x+y for x,y in zip(homo,trans)]
     pylab.title("Generation %d of 100" % (N+1,))
     pylab.plot(trans, lumo, ".")
     pylab.savefig("pictures/animation%02d.png" % N)
     pylab.clf()
You can then use ImageMagick to do the PNG to animated GIF conversion. ImageMagick is a bit like Open Babel - it is a one-stop shop for file format conversion, plus it can do a whole set of transformations, and furthermore can be accessed as a programming library. It is available cross-platform (I used it on Windows). The main binary is called "convert", and the conversion is done as follows:
convert.exe -delay 20 -loop 0 animation*.png GA_animation.gif
You can also specify different timings for every frame as I did above. I wrote a little script to help with this:

Monday, 5 March 2012

Grab, build and install the development version of Open Babel on Linux Mint 12

This is one of those "Notes to self" posts. I just installed Linux Mint 12 into a VM. Here's what I had to do to build the development version of Open Babel, complete with GUI and Python bindings. As far as I know, these instructions should also work for Ubuntu 11.10 (Oneiric oh-whatever).
export OB=$HOME/openbabel
sudo apt-get update
sudo apt-get install cmake subversion g++ swig2.0 python-dev libxml2-dev wx-common wx2.8-headers libwxbase2.8-dev libeigen3-dev libwxgtk2.8-dev
mkdir $OB
cd $OB
svn checkout http://openbabel.svn.sourceforge.net/svnroot/openbabel/openbabel/trunk trunk
mkdir build
cd build
cmake ../trunk -DCMAKE_INSTALL_PREFIX=../install -DPYTHON_BINDINGS=ON -DRUN_SWIG=ON
make -j2 && make install
export PATH=$OB/install/bin:$PATH
export PYTHONPATH=$OB/install/lib:$PYTHONPATH
Thereafter, just type "svn update" in $OB/trunk, and "make && make install" in $OB/build, to get the latest and greatest.

Friday, 2 March 2012

It all adds up to a new descriptor

Group contribution descriptors such as TPSA and LogP are pretty popular. I've just written up some docs describing how to add a new group contribution descriptor to Open Babel. It's fairly easy to do once you have a set of SMARTS strings and contributions.
Group contribution descriptors are a common type of molecular descriptor whose value is a sum of contributions from substructures of the molecule. Such a descriptor can easily be added to Open Babel without the need to recompile the code. All you need is a set of SMARTS strings for each group, and their corresponding contributions to the descriptor value.

The following example shows how to add a new descriptor, hellohalo, whose value increments by 1, 2, 3 or 4 for each F, Cl, Br, and I (respectively) in the molecule...
To read the rest, check out the development docs.

I'm currently planning* to add a new option to make debugging of these descriptors simpler, so now might be a good time to look into implementing a descriptor if you're interested.

If you do implement a new descriptor, please consider the trees, the number of towels washed every day, and the effort you will save another PhD student, and contribute the result back to Open Babel. If you do so under a liberal license (e.g. CC0), the same SMARTS strings can also be used by other cheminformatics toolkits.

* Now done. In the development version, add ";debug" to the start of the pattern file.

Saturday, 25 February 2012

Portrait of the molecule as a green substructure

I've already mentioned using Open Babel for depicting with SVG. That's a handy way to view a large set of molecules; you can zoom in and out and so forth. Let's look at some more of the features, as I've just been adding them to PNG depiction from SVG.

Let's start with the following basic depiction. Note that information on the SVG output options (e.g. -xC) is available in the docs or via "obabel -H svg", and that you can use the mouse to zoom in, etc.
obabel dataset.sdf -O output1.svg -xC


With some magic, we can convert carboxylic acid groups (and anything else listed in the user-editable superatom.txt) into COOH in the depiction. Let's add thick lines too:
obabel dataset.sdf -O output2.svg -xC --genalias -xA -xt

We can also do some fun stuff with descriptors (see "obabel -L descriptors" for a list). Let's sort by molecular weight and replace the title with the molecular formula and molecular weight:
obabel dataset.sdf -O output3.svg -xC --sort MW
  --title "" --append "formula MW"

You might have noticed that all of the molecules have a substructure in common. Let's highlight some of this in green, and get rid of the other colours:
obabel dataset.sdf -O output4.svg -xC
  -xu -s "[#6]~2~[#6]NCCN=C~2 green"

And finally, if the molecules are related, it can be useful to align the depictions using a substructure in order to identify similarities and differences (this has been improved in the development version):
obabel dataset.sdf -O output5.svg -xC
  -xu -s "[#6]~2~[#6]NCCN=C~2 green" --align

What other depiction features would you find useful?

Thursday, 23 February 2012

On reflection, transforming molecules is tricky

I've just spent quite some time trying to get the mirror-image of a chiral ligand in a metal-ligand complex. It took me a good while to figure out how to do it, so here it is for the record.

The solution uses a combination of Open Babel's API and Numpy. Open Babel can calculate the required transformation matrix given the normal to the mirror plane. To get the normal, we just need three points in the plane, from which we can derive two vectors (it doesn't matter which) which lie in the plane. The cross product of the two vectors is a vector that is orthogonal to both (that's a property of cross products), and thus is a normal to the plane.

It should be possible to do all of the maths with Open Babel, but whatever way the matrix3x3 and vector3 classes are implemented, it doesn't translate into Python (at least not without segfaults). So that explains why I've had to convert to Numpy arrays and do the maths there.

Wednesday, 15 February 2012

Towards a Novel Future

The recent JCAMD issue celebrating 25 years of that journal is full of articles speculating on the future of the field over the coming 25 years (note that these are available free online for 3 months and 6 of the 32 are Open Access). For me, as a science fiction fan, one particular article stands out: Alpha Shock by Mark Murcko and Pat Walters, which imagines a future where drug design will have virtually (le mot juste, eh?) eliminated those pesky experiments.

Science fiction articles don't appear very often in scientific journals (the double entendre aside), so I was intrigued by how the reviewers handled this. Through various nefarious and downright dastardly means, I managed to get a peek at the reviewers' reports and here are some highlights:

"A real Page-Downer of a PDF. I was gripped from the Introduction right through to the Conclusion."
- Reviewer 1
"8 out of 10. A story of great entertainment in the field."
- Reviewer 2
"P2, paragraph 3: Several misspellings. In the future, are there no spell checkers?"
- Reviewer 3
"Rattles along at a rollicking pace. And that's just the abstract!"
- Reviewer 1
"I laughed. I cried. Then I read the article."
- Reviewer 2
"Publish with minor revisions to add tension."
- Reviewer 3

Can anyone think of other examples where fiction has been included in a chemistry article?