Search code examples
pythonscriptingpymol

How can I save command outputs in PyMOL to a txt file?


I'm new to PyMOL, and I'm trying to write a python script that will generate a .txt file and save a PyMOL command output to it. Let's say it has an array containing names of pdb files and a for loop that aligns each one to some specific protein:

pdb = ["191L", "192L", "193L", "194L"]

cmd.fetch("190L")

for i in pdb:
cmd.fetch(i)
cmd.align(i, "190L")

PyMOL will calculate the RMSD for each alignment. How can I write my script so that it will take each RMSD and save it to a text file?

Here's what I have so far:

def get_rmsd():

cmd.fetch("190L")
for i in pdb:
   cmd.fetch(i)
   output = open("rmsd.txt", "w")
   data = cmd.align(i, "190L")
   data = str(data)
   output.write(data)
   stored.f.close()

When I call the function on PyMOL, it fetches and aligns the file like it's supposed to, but no text file is created.


Solution

  • Figured out the answer to my own question, the solution was embarrassingly easy lol.

    Assign variables to each desired output, e.g.:

    output = cmd.align("190L", "191L")
    

    Run the script in PyMOL as is, then feed the python code into it line by line, beginning with "python" and closing with "python end." In my case, something like:

    python
    with open("rmsd.txt", "w") as f:
       f.write(output)
    f.close()
    python end
    

    A really basic example but that's the gist of it.