środa, maja 21, 2008

Lately I've been showing Phosphoros, a friend of mine, some Pymol features especially the basics of structure modification. I thought I know almost everything about it since there's no much to learn about it, but then he asked me a question I had no answer for.

- How do you make this aromatic rings look so like in this indigo molecule you've showed me earlier?

The point is that some chemical file formats save the information about the delocalized bonds (like MDL Molfile) and some not (PDB for example). If you load a molecule with delocalized bonds into Pymol the program displays them as dashed lines/sticks. Don't know why this feature was not implemented into the internal builder, but since Pymol can represent these bonds you could probably also convert single and double bonds to delocalized. Following this idea I wrote a small wizard for changing the bond order.

# -*- coding: utf-8 -*-# -----------------------------------------------------------------------# resonance.py - delocalized bonds wizard for Pymol version 1.0# -----------------------------------------------------------------------# Copyright (C) 2008 by Lightnir - lightnir@gmail.com# This script is free software; you can redistribute it and#or modify# it under the terms of the GNU Library General Public License as# published by the Free Software Foundation; either version 2 of the# License, or (at your option) any later version.# This script is distributed in the hope that it will be useful,# but WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the# GNU General Public License for more details.# You should have received a copy of the GNU Library General Public# License along with this program; if not, write to the# Free Software Foundation, Inc.,# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.frompymol.wizardimportWizardfrompymolimportcmdimportpymolimporttypesimportstringimporttracebackclassResonanse(Wizard):def__init__(self):cmd.unpick();Wizard.__init__(self)# some attributes to do with pickingself.error=Noneself.pick_count=0self.object_prefix="res"self.selection_mode=cmd.get_setting_legacy("mouse_selection_mode")cmd.set("mouse_selection_mode",0)# set selection mode to atomiccmd.deselect()# generic set routinesdefget_prompt(self):self.prompt=Noneifself.pick_count==0:self.prompt=['Pick first atom...']elifself.pick_count==1:self.prompt=['Pick second atom...']ifself.error!=None:self.prompt.append(self.error)returnself.promptdefget_panel(self):return[[1,'Delocalized bonds',''],[2,'Done','cmd.set_wizard()']]defcleanup(self):self.clear()cmd.set("mouse_selection_mode",self.selection_mode)# restore selection modedefclear(self):cmd.delete(self.object_prefix+"*")defdo_select(self,name):# "edit" only this atom, and not others with the object prefixtry:cmd.edit("%s and not %s*"%(name,self.object_prefix))self.do_pick(0)exceptpymol.CmdException,pmce:printpmcedefpickNextAtom(self,atom_name):cmd.select(atom_name,"(pk1)")cmd.unpick()self.pick_count+=1self.error=Nonecmd.refresh_wizard()defdo_pick(self,picked_bond):# this shouldn't actually happen if going through the "do_select"ifpicked_bond:self.error="Error: please select bonds, not atoms"printself.errorreturnatom_name=self.object_prefix+str(self.pick_count)ifself.pick_count<1:self.pickNextAtom(atom_name)else:self.pickNextAtom(atom_name)cmd.unbond(self.object_prefix+"0",self.object_prefix+"1")cmd.bond(self.object_prefix+"0",self.object_prefix+"1",4)self.clearcmd.delete(self.object_prefix+"*")self.pick_count=0cmd.unpick()cmd.refresh_wizard()

Just place the script in the wizrads directory (pymol_install_directory/modules/pymol/wizard). You can call it now by typing in the pymol command line:wizard resonance
If you are as lazy as I am you can go further and add yourself a menu shortcut for this. Open the file pymol_install_directory/modules/pmg_tk/skins/normal/__init__.py (make a backup first) and locate these lines:

After Pymol restart a new option called "Resonance" in the wizard menu should be available. The image at the beginning of this post shows sodium benzoate, a common food preservative (E211). The whole molecule was entirely build in Pymol. First using the internal builder and then using the resonance wizard for changing bond order of the aromatic ring and the carboxylic group.
The wizard works, however I've found a bug. If you pick an atom, then click somewhere in the main window(for example the builder button) and than want to pick the second atom Pymol rises an exception. Let me know if you know the reason of this - I'm biting my fingers on it and can't find a solution for this.

niedziela, maja 18, 2008

If you do a lot molecule visualization in Pymol like I do, then after a while you'll probably ask yourself how to cut your working time. For this I wrote myself some scripts for batched molecule rendering. But today I had another idea. The most common animation I do is rotating a molecule along the Y axis. It doesn't matter if it's done by Pymols intern commands or by using the movie.py or emovie.py scripts - every time the molecule gets rotated all the coordinates (atoms, surface & mesh points) have to get recalculated. And that's CPU expensive if you have a slower machine or just a lot of coordinates. If you look at the ray commands API , then you'll notice the angle parameter. It can be used to render a stereo pair of images both looked at a different angle. But why just to render two images, when you can render a sequence of images of a full rotation along the Y axis? To test how does it look like a wrote myself this little script:

And then run it with Pymol as a background tread like this:pymol -qcr rsd.py
Note that the morphine.mol file used in this script have to be in the same directory as the script. What exactly does the script? First it renders a full-angle rotation of the "camera" along the Y axis using a five degree step. Second it renders a full-angle rotation of the molecule (morphine in my case) along the Y axis also using a five degree step. The output is dumped into a log file and the rendered images are saved in the current directory. The final result looks like this:

I was amazed by myself when i looked at this. Not only I created an interesting animation effect, but also cut down the rendering time by four. For small molecules the difference in time taken to render is not so visible, but if you use a bigger model like a protein or increase the quality of displayed model (in my case more detailed surface) you'll better take a longer coffee break or go for a longer walk. Sure, the two renders looks different, but only because I'd set Pymol to render heavy dropped shadows. By default you don't see a difference or a slightly one. I think I'll add this method of rendering to my batch rendering scripts. ]:)