This post is just a quick introduction on how to use rdkit python bindings to draw chemical molecules.
I have a very special friend that downloads terrible jpegs/pngs from the internet to use in her lectures. Every time, I promise myself that I will make her better figures. Well, now it is time to fulfill that promise!
First, I had to familiarize myself with the Simplified Molecular Input Line Entry System or SMILES. In a nutshell SMILES is a string representation for molecules.
Also, I'm not a chemist, so I relied on PubChem to find the SMILES strings for the figures below.
I will use the
Chem submodule from
rdkit.Chem has an option to override
IPython.display and show the molecules objects in the notebook.
But I was unable to make it work in my environment. No biggie though, I just used the
MolToMPL() method and displayed the plots as a matplotlib figures.
from rdkit import Chem from rdkit.Chem import Draw size = (120, 120) # Smaller figures than the default.
First a simple figure with some ions.
m = Chem.MolFromSmiles('[Na+].[Cl-]') fig = Draw.MolToMPL(m, size=size)
An organic-something ring (I think).
m = Chem.MolFromSmiles('c1ccccc1') fig = Draw.MolToMPL(m, size=size)
One of the molecules that I'm sure she will need.
m = Chem.MolFromSmiles('C1=C2C(=CC(=C1Cl)Cl)OC3=CC(=C(C=C3O2)Cl)Cl') fig = Draw.MolToMPL(m, size=size)
Every nerd's favorite molecule.
m = Chem.MolFromSmiles('Cn1cnc2c1c(=O)n(c(=O)n2C)C') fig = Draw.MolToMPL(m, size=size)
And last, but not least, a Breaking Bad Good bye.
m = Chem.MolFromSmiles('C[C@@H](CC1=CC=CC=C1)NC') fig = Draw.MolToMPL(m, size=size)
As you can see it is extremely easy to draw molecules using
rdkit and the
rdkit package is much more than just drawing molecules and I believe that
a chemist might enjoy it even more that I did making these figures.
python4oceanographers by Filipe Fernandes is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
Based on a work at https://ocefpaf.github.io/.