Search code examples
pythonchemistrypymol

In PyMOL, how can I make the "resi" selection work with variables?


If I want to select a residue on a protein (let's say the tenth residue), when writing a PyMOL script, I can use the following code to assign it to a variable "pep"

select pep, (resi 10) 

However, if I try to use a predefined variable in place of the numbers in the selection, no atoms are assigned to the selection.

    a=10
    select pep, (resi a)

No error is returned and no atoms are assigned to the selection. I've tried typecasting the variable as a string and as an integer, but neither one has worked. If I use the variable a elsewhere, such as a print statement or addition, it works perfectly fine. Does anyone know how to make the resi selection work with variables? I'm trying to use this to color amino acids differently based on some data we collected and I don't want to hard code the residues each time I analyze a new set of data.


Solution

  • The following is equivalent to select pep, (resi 10):

    a=10
    cmd.select("pep", "resi " + str(a))
    

    or

    cmd.select("pep", "resi %i"%(a))