niedziela, maja 18, 2008

In the shadow of the molecule

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:

rsd.py(Toggle Plain Text)

from pymol import *
from time import *

# Variables

width    = 500
height   = 375
step     = 5
angle    = 360
molecule = 'morphine.mol'

# Main settings

logfile = open("rsd.log","w")
logfile.write('Script started '+strftime("%Y-%m-%d %H:%M:%S"+"\n"))
logfile.write('====================================================================\n')
logfile.close()

cmd.load(molecule,"molecule")       # Load molecule
cmd.hide("everything","molecule")   # Hide everything
cmd.show("surface"   ,"molecule")   # Show surface
cmd.set("surface_quality",3)        # Set a high surface resolution
util.ray_shadows('heavy')           # Set heavy shadows drop
cmd.zoom("all",1)                   # Zoom out
util.cba(29,"molecule")             # Apply color scheme

# Rendering A (rotating the camera)

logfile = open("rsd.log","a")
t=time()
logfile.write('Starting rendering A => '+strftime("%Y-%m-%d %H:%M:%S",gmtime(t))+"\n")
logfile.close()

i=0

while i<angle:
    name = "%s%.4d" % ("molecule_A_",i)
    cmd.ray(width,height,0,i)
    cmd.png(name)
    i+=step

logfile = open("rsd.log","a")
t2=time()
logfile.write('Stoping  rendering A => '+strftime("%Y-%m-%d %H:%M:%S",gmtime(t2))+"\n")
logfile.write('--------------------------------------------------------------------\n')
diff=t2-t
logfile.write('Summary: '+strftime("%H hours, %M minutes, ",gmtime(diff))+str(diff % 60)+' seconds taken to render '+str(angle/step)+' frames'+"\n")
logfile.write('====================================================================\n')
logfile.close()

i=0

# Rendering B (rotating the model)

logfile = open("rsd.log","a")
t=time()
logfile.write('Starting rendering B => '+strftime("%Y-%m-%d %H:%M:%S",gmtime(t))+"\n")
logfile.close()

while i<angle:
    name = "%s%.4d" % ("molecule_B_",i)
    cmd.rotate("y",step)
    cmd.ray(width,height)
    cmd.png(name)
    i+=step

logfile = open("rsd.log","a")
t2=time()
logfile.write('Stoping  rendering B => '+strftime("%Y-%m-%d %H:%M:%S",gmtime(t2))+"\n")
logfile.write('--------------------------------------------------------------------\n')
diff=t2-t
logfile.write('Summary: '+strftime("%H hours, %M minutes, ",gmtime(diff))+str(diff % 60)+' seconds taken to render '+str(angle/step)+' frames'+"\n")
logfile.write('====================================================================\n')
logfile.close()

# End log

logfile = open("rsd.log","a")
logfile.write('Script stoped '+strftime("%Y-%m-%d %H:%M:%S")+"\n")
logfile.close()
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:

(Download video)
Here's the output of my log file(rsd.log):
Script started 2008-05-18 15:27:04
====================================================================
Starting rendering A => 2008-05-18 13:27:04
Stoping  rendering A => 2008-05-18 13:34:18
--------------------------------------------------------------------
Summary: 00 hours, 07 minutes, 14.7353949547 seconds taken to render 72 frames
====================================================================
Starting rendering B => 2008-05-18 13:34:18
Stoping  rendering B => 2008-05-18 14:04:11
--------------------------------------------------------------------
Summary: 00 hours, 29 minutes, 52.1074898243 seconds taken to render 72 frames
====================================================================
Script stoped 2008-05-18 16:04:11
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. ]:)

1 komentarz:

Anonimowy pisze...

whats up!, im new . This website's fantastic, ive learned tons from it today!