1
$\begingroup$

I'm trying to print out the measured distances for each .pdbqt file, and then write a new output file for each .pdbqt, using the original name of the .pdbqt file as the new .txt file name. Below is the script.

I'm using python3 & I'm trying to do this via Linux CLI.

from pymol import stored
stored.a, stored.b, stored.c, stored.d = [],[],[],[]
cmd.iterate_state(1, "r55_01///UNL`1/N*", "stored.a.append(ID)")
cmd.iterate_state(1, "r55_01///UNL`1/O*", "stored.b.append(ID)")
cmd.iterate_state(1, "r55_01//R/ASP`113/OD2", "stored.c.append(ID)")
cmd.iterate_state(1, "r55_01//R/ASN`312/ND2", "stored.d.append(ID)")

f=open("tt.txt", 'w')

for a in stored.a:
  for c in stored.c:
      d1=cmd.distance("id %s" % a, "id %s" % c,cutoff=5)
      f.write("%s D113 %s\n" % (a,d1))
      print(d1)

for b in stored.b:
  for d in stored.d:
      d2 = cmd.distance("id %s" % b, "id %s" % d, cutoff=5)
      f.write("%s N312 %s\n" % (b,d2))
      print(d2)

f.close()

My folder looks something like this

.
├── dst6.txt
├── sp
│   ├── r55_01.pdbqt
│   ├── r55_02.pdbqt
│   ├── r55_03.pdbqt
│   ├── r55_04.pdbqt
│   ├── r55_05.pdbqt
│   ├── r55_06.pdbqt
│   ├── r55_07.pdbqt
│   ├── r55_08.pdbqt
│   ├── r55_09.pdbqt
│   ├── r55_10.pdbqt
│   ├── r56_01.pdbqt
│   ├── r56_02.pdbqt
│   ├── r56_03.pdbqt
│   ├── r56_04.pdbqt
│   ├── r56_05.pdbqt
│   ├── r56_06.pdbqt
│   ├── r56_07.pdbqt
│   ├── r56_08.pdbqt
│   ├── r56_09.pdbqt
│   └── r56_10.pdbqt
└── t6.py

THANKS IN ADVANCE!!

$\endgroup$
1
$\begingroup$

You made the hardest work, you were not far to achieve what you wanted ;)

This script.py should work after you made it executable (in a terminal type: chmod u+x script.py), just type ./script.py in your terminal in your working directory (so here you should be in your "sp" directory). Note that I got inspired by: https://www.biostars.org/p/42474/ to know how to launch pymol within the script and also that I would personally not use pymol for doing this kind of analysis/data extraction from a pdb/pdbqt file but rather use a dedicated analysis package (there are plenty like MDanalysis). I personally use pymol (pymol script) for visualization purpose.

#!/usr/bin/env python


import __main__
__main__.pymol_argv = ['pymol', '-qc']  # Quiet and no GUI

from pymol import stored
import pymol, os

pymol.finish_launching()

#put what you've done into a function
def myfunc(file):
    spath = os.path.abspath(file)
    sname = spath.split('/')[-1].split('.')[0]

    pymol.cmd.load(spath, sname)

    stored.a, stored.b, stored.c, stored.d = [], [], [], []
    pymol.cmd.iterate_state(1, "%s///UNL`1/N*" % sname, "stored.a.append(ID)")
    pymol.cmd.iterate_state(1, "%s///UNL`1/O*" % sname, "stored.b.append(ID)")
    pymol.cmd.iterate_state(1, "%s//R/ASP`113/OD2" % sname, "stored.c.append(ID)")
    pymol.cmd.iterate_state(1, "%s//R/ASN`312/ND2" % sname, "stored.d.append(ID)")

    f = open('%s.txt' % sname, 'w')

    for a in stored.a:
        for c in stored.c:
            d1 = pymol.cmd.distance("id %s" % a, "id %s" % c, cutoff=5)
            f.write("%s D113 %s\n" % (a, d1))
            print(d1)

    for b in stored.b:
        for d in stored.d:
            d2 = pymol.cmd.distance("id %s" % b, "id %s" % d, cutoff=5)
            f.write("%s N312 %s\n" % (b, d2))
            print(d2)

    f.close()


mypdbqt = os.popen('ls *.pdbqt').read().split()

for i in mypdbqt:
    myfunc(i)

pymol.cmd.quit()
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.