This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How do I access pymol alignment object ("object=" arg of align function ) from python?

I am writing a function in python to get rms_cur for a peripheral area of a pair of proteins after aligning on a core area. Because my proteins are homologs and not exact matches, I am running align with 0 cycles first, to create an alignment object to pass to rms_cur. I am having trouble with the "object=" argument to cmd.align, which if I understand right is just a name to which align will assign an object it creates so I can use it later (a way to get an object as output). This works fine at the pymol command line, and I get an object named my_align that I can use:

align mobile and periph_sel, target and periph_sel, cycles=0, transform=0, object=my_align

Then I write a python function with the following code:

  # align only on core residues
cmd.align('%s and %s'%(mobile, core_sel), '%s and %s'%(target, core_sel))
  # without moving proteins, create an alignment object of matching atoms in peripheral areas
cmd.align('%s and %s'%(mobile, periph_sel), '%s and %s'%(target, periph_sel), cycles=0, transform=0, object=my_align)
  # use that alignment to get the rms_cur of the peripheral areas
my_rms = cmd.rms_cur('%s and %s'%(mobile, my_align), '%s and %s'%(target, my_align), matchmaker=-1)

When I run the function in pymol, python complains the object is not defined:

cmd.align('%s and %s'%(mobile, periph_sel), '%s and %s'%(target, periph_sel), cycles=0, transform=0, object=my_align)
NameError: name 'my_align' is not defined

How can I use the "object=" argument of align from python so that pymol creates the object and I can then use it from my python code?

alignment python pymol scripting

In your python version of the code (for the PyMOL API), try adding double-quotes (since you use single quotes already on those lines) around my_align in your cmd.align() line of code. Note how BLOSUM62 is in quotes in the example under 'PyMOL API' here and those are both shown as string type there.

I think you won't need my_align quoted on the my_rms = cmd.rms_cur() line as running the cmd.align() function on the previous line will then have made that object.

Thank you for your clear and quick answer! Changing object=my_align to object="my_align" did indeed get rid of the 'name not defined' error. I also changed the call to cmd.rms_cur to have my_align as part of the string literal passed to pymol (because my_align is a pymol name and not a python name):

my_rms = cmd.rms_cur('%s and my_align'%mobile, '%s and my_align'%target, matchmaker=-1)

That call to cmd.rms_cur is now passing the python interpreter and failing in pymol with this message:

ExecutiveRMS-Error: Atom counts between selections don't match (2622 vs 2635)
 Executive: Error -- no atoms left after refinement!

However, that is a different problem, so the problem I posted about is solved! Thanks again! [I am looking for the green checkmark to accept Wayne's answer, but can't find it! --ETA this has been resolved.]

ETA: the subsequent atom counts error was actually a related issue: because I was running my code in a python loop, I needed to pass a different string literal as the object name each time through the loop, so that pymol would not try to re-use the same alignment object. Telling pymol to remove the alignment object from memory after using it might also have worked.

I put it as an answer now that you can check put a checkmark then. (They may only have 'solution' checkmark over at Discourse?) I originally posted it just as a comment suggesting it because I hadn't stopped to test it conclusively.

1 answer

In your python version of the code (for the PyMOL API), add double-quotes (since you use single quotes already on those lines) around my_align in your cmd.align() line of code. At present that is a string you'll need to pass in to specify what you want to call it because right now there isn't a Python object my_align yet.

More explanation can be found in the comment I originally posted suggesting that.

Log in to answer this question.