In my previous post, I built a molecule in RDKit and saved it for later use. The construction process may not have created a molecule that is suitable for our purposes. Let’s examine the geometry of the molceule and visualize it using the PyMOL xmlrpc server.
from rdkit import Chem
from rdkit.Chem import AllChem
from rdkit.Chem.Draw import IPythonConsole
from rdkit.Chem.Draw.MolDrawing import MolDrawing, DrawingOptions
import cPickle as pickle
ibuH = pickle.load(open('ibuH.pkl','rb'))
ibuH
Let's take another look at a text representation of the molecule:
print Chem.MolToMolBlock(ibuH)
The first three columns in the atom block of this output are the x, y, and z coordinates of each atom. They haven't been created yet!
AllChem.EmbedMolecule(ibuH)
ibuH
The explicit-hydrogen pseudo-3D line drawings can look a bit odd for all but the smallest molecules.
Pymol integration for 3D visualization¶
There are a few alternative methods for 3D visualization. If you are already familiar with PyMOL, a very convenient method is to connect RDKit to a running instance of PyMOL, using a communication protocol called xmlrpc
. You need to start PyMOL using a special option to start the xmlrpc server, which on my Mac looks like this:
/Applications/MacPyMOL.app/Contents/MacOS/MacPyMOL -R
We then can import the PyMOL communication into our notebook:
from rdkit.Chem import PyMol
v = PyMol.MolViewer()
v.ShowMol(ibuH);
If you switch over to your copy of PyMOL, you should now see a 3D representation of the molecule that you can manipulate using the typical methods. We probably notice that the aromatic ring is not flat as expected. We can save our view by importing a png back into the notebook:
ibuH
v.server.do('ray')
v.GetPNG()
Force-field optimization of a molecular geometry¶
In the picture above, it is clear that our molecular geometry isn't correct. We expect the aromatic benzene ring to lie flat in a plane. We can clean up the geometry by minimizing the geometry by the application of a molecular mechanics force field. RDKit provides both UFF
and MMFF
families of force fields for small molecules.
AllChem.MMFFOptimizeMolecule(ibuH)
v.ShowMol(ibuH,name='optimized',showOnly=True);
v.server.do('ray')
v.GetPNG()
Application of the force field has flattened our benzene ring as desired!
pickle.dump(ibuH, open('ibuH_opt.pkl','wb'))