1
0
Entering edit mode
4.9 years ago

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:

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

pymol python • 5.2k views
0
Entering edit mode
4.9 years ago
cschu181 ★ 2.6k

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')):
# rest of code-block

0
Entering edit mode

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.

0
Entering edit mode

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.

0
Entering edit mode

*end in .pdb, not .pdf :P

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

0
Entering edit mode

Gha close enough ;)

0
Entering edit mode

But still no cigar :)

0
Entering edit mode
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))
, 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?

0
Entering edit mode

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.

0
Entering edit mode

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. :(

0
Entering edit mode

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.

0
Entering edit mode

sorry my bad, but I get the same error. Can you elaborate what you mean by a bash loop? I found this, now how can we use it?

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

0
Entering edit mode

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.