[Chimera-users] Script

Eric Pettersen pett at cgl.ucsf.edu
Thu Aug 20 16:02:45 PDT 2009

On Aug 20, 2009, at 11:23 AM, Damien Larivière wrote:

> Hi Eric and Elaine,
> Thank you very much for your answers !
> I was blocked in fact by the "cd command" that does not work on my  
> machine for opening a file. I reported the bug this morning.

I will be looking into that.

> So, I have used Eric's suggestions and it works very well.
> I am not a python programmer (but I know Matlab well enough). So in  
> Chimera I would like to open successive pdb files (dock000i.pdb with  
> i = 1 to 20) and perform some actions on each one and then close  
> each session that are previously saved in a Chimera session. The  
> loop "for" in Matlab starts with "for i = 1:20" then the block of  
> actions and ends with "end". What's about in Python programming ?

As Elaine mentioned, if you need to do looping you will need to resort  
to Python.  There is a function in Chimera named runCommand which  
allows you to execute Chimera commands in Python, so that way you  
don't have to spend a lot of time figuring out the equivalent Python  
functions.  This lets us write your script pretty easily in Python:

> I suggest the following but I know it can't work:
> for i = 1 to 20
>   open C:\Users\damien\Fondation\LifeExplorer\3D models\FtsZ_ring 
> \Bond_Set\Docking_Hex\Results_190809\dock\dock000i.pdb
>   color orange red :38-227.A
>   color blue :228-356.A
>   color magenta :203-227.A
>   color green :261-271.A
>   color green :297-307.A
>   save C:\Users\damien\Fondation\LifeExplorer\3D models\FtsZ_ring 
> \Bond_Set\Docking_Hex\Results_190809\dock\dock000i.py
>   close session
> end

from chimera import runCommand as rc
for i in range(1, 21):
	rc(r"open C:\Users\damien\Fondation\LifeExplorer\3D models\FtsZ_ring 
\Bond_Set\Docking_Hex\Results_190809\dock\dock%04d.pdb" % i)
	rc("color orange red :38-227.A")
	rc("color blue :228-356.A")
	rc("color magenta :203-227.A")
	rc("color green :261-271.A")
	rc("color green :297-307.A")
	rc(r"save C:\Users\damien\Fondation\LifeExplorer\3D models\FtsZ_ring 
\Bond_Set\Docking_Hex\Results_190809\dock\dock%04d.pdb" % i)
	rc("close session")

The script is really pretty self-explanatory except for a couple of  
tricky things.  One is that the quoted strings that contained  
backslashes needed to be preceded by an 'r' (marking them as a "raw"  
string) to prevent Python from doing special backslash processing on  
the string.  The other is getting the zero-padded version of 'i' into  
the open and save commands.  This is accomplished with the special  
text %04d which means replace that text with a zero-padded 4-digit  
decimal integer.  The "% i" after the string indicates that the  
variable 'i' should be used in the text replacement.

Good luck! :-)


	Eric Pettersen
	UCSF Computer Graphics Lab

More information about the Chimera-users mailing list