This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Loading multiple pdb files in pymol

I want to use the code from example 4 here to iterate over many files:

https://pymolwiki.org/index.php/Get_Area

I tried to look up some documentation about pymol, I think maybe "glob" could be used but idon't really understand it. I found this post here, but couldn't really conenct the two codes together:

https://sourceforge.net/p/pymol/mailman/message/27805952/

I would appreciate if someone could help. P.s. I'm new to both pymol and python.

pymol python

1 answer

Assuming you call your script with

python <scriptname> <path-to-pdb-files>

then this might do it:

import glob
import sys
import os
for _file in glob.glob(os.path.join(sys.argv[1], '*.pdb')):
    cmd.load(_file)  # use the name of your pdb file
    # rest of code-block

didn't quite work and I can't debug it because I'm not entirely sure what glob does! can I message you? please email me at underoath006 at gmail.

Communicating via email is not encouraged, this discussion could be beneficial for everyone.

Glob searches for files matching a pattern, in this case files in the directory specified by sys.argv[1] which end in .pdf.

*end in .pdb, not .pdf :P

@underoath006: What is the error you're getting?

But still no cigar :)

Area-Error: Selection must be within a single object.
Traceback (most recent call last):
  File "pymol_sa.py", line 29, in <module>
    sasa_per_residue.append(cmd.get_area('resi %s' % i))
  File "C:\Users\Ahmad\Anaconda3\lib\site-packages\pymol\querying.py", line 1174
, in get_area
    if _self._raising(): raise pymol.CmdException
pymol.CmdException:  Error:

this is the error Iget, how can I post the entire script for you to look at?

You can put your script on GitHub (gist) and then post the link for public gist in a post here. Biostars understands GitHub and will load the code from the gist automatically.

This code works if I type in the name of the pdb file, it calculates the accessible surface area of each residue in a pdb file and returns a dictionary that could be saved using json dump. I want to iterate this process over many pdb file. The problem with pymol is that it needs to be restarted between runs for some reasons. :(

Currently everything from line 18 on is not covered by the for-loop, so that block would have to be indented. Is there a cmd.unload() or something that you could use at the end of the for-loop?

The easiest way could be to run this script once for each file, e.g. via a bash loop.

Insert a cmd.reinitialize() before line 16 and try if that works.

Concerning the bash loop, that would be a way to run the script for multiple files using the Unix shell. There's also a way to do that on the Windows command line, but I don't know the syntax for it.

Log in to answer this question.