This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Selecting Conserved Residues In Protein Using Pymol Script By Importing Residue List From A Text File

Hi,

I wanted to select certain highly conserved residues in a protein (computed by a scoring mechanism and listed in a text file - each residue in a single line) using a PyMol script. The PyMol script below that I am using is not working. I will be very grateful to you if someone could help me out.

Parts of the script work perfectly fine when run separately - the Pymol script when a residue number is mentioned in the script without importing the list from the text file and just the python script for loading numbers from a file into an array also works fine when run separately. But the problem is seen when it is combined as in my script below - when residue number needs to be taken from the array as I after importing the list from the text file. Any help is appreciated. Thanks!

#!/usr/bin/python
from pymol import cmd
import string
cmd.load("/home/xyz/proteinA.pdb", 'protein')
cmd.hide()
cmd.bg_color("white")
cmd.show("mesh")
cmd.color("blue")

f=open('/home/xyz/residuedata.txt','r')
array = []
filecontents = f.read().split()
for val in filecontents:
            array.append(int(val))
f.close()
for I in array:
        cmd.select("residuedata", "resi i")
        cmd.show('sphere', "residuedata")
        cmd.color ('red', "residuedata")
pymol python

I've never actually used a script interface to PyMol so don't know about the cmd functions you're using, but cmd.select("residuedata", "resi i") isn't using the integer stored in i - it's just passing the string "resi i". I guess instead you want "resi " + str(i)

Thank you Ben. That was a very important clue for me.

1 answer

It works now: by changing it like this

for i in range(len(array)):
    cmd.select('scorecons', 'resi %s' % array[i])

You could also write:

for i in array:
    cmd.select('scorecons', 'resi %s' % i)

For loops in Python behave like foreach loops in other languages.

Log in to answer this question.