<p>You made the hardest work, you were not far to achieve what you wanted ;)</p>
<p>This script.py should work after you made it executable (in a terminal type: <code>chmod u+x script.py</code>), just type ./script.py in your terminal in your working directory (so here you should be in your &quot;sp&quot; directory). Note that I got inspired by: <a href="https://www.biostars.org/p/42474/" rel="nofollow noreferrer">https://www.biostars.org/p/42474/</a> 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.</p>
<pre><code>#!/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, &quot;%s///UNL`1/N*&quot; % sname, &quot;stored.a.append(ID)&quot;)
    pymol.cmd.iterate_state(1, &quot;%s///UNL`1/O*&quot; % sname, &quot;stored.b.append(ID)&quot;)
    pymol.cmd.iterate_state(1, &quot;%s//R/ASP`113/OD2&quot; % sname, &quot;stored.c.append(ID)&quot;)
    pymol.cmd.iterate_state(1, &quot;%s//R/ASN`312/ND2&quot; % sname, &quot;stored.d.append(ID)&quot;)

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

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

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

    f.close()


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

for i in mypdbqt:
    myfunc(i)

pymol.cmd.quit()
</code></pre>

CC BY-SA 4.0
2020-06-30T19:41:47.913Z
2020-06-30T19:41:47.913Z
jeremy
<p>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.</p>
<p>I'm using python3 &amp; I'm trying to do this via Linux CLI.</p>
<pre><code>from pymol import stored
stored.a, stored.b, stored.c, stored.d = [],[],[],[]
cmd.iterate_state(1, &quot;r55_01///UNL`1/N*&quot;, &quot;stored.a.append(ID)&quot;)
cmd.iterate_state(1, &quot;r55_01///UNL`1/O*&quot;, &quot;stored.b.append(ID)&quot;)
cmd.iterate_state(1, &quot;r55_01//R/ASP`113/OD2&quot;, &quot;stored.c.append(ID)&quot;)
cmd.iterate_state(1, &quot;r55_01//R/ASN`312/ND2&quot;, &quot;stored.d.append(ID)&quot;)

f=open(&quot;tt.txt&quot;, 'w')

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

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

f.close()
</code></pre>
<p>My folder looks something like this</p>
<pre><code>.
├── 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
</code></pre>
<p>THANKS IN ADVANCE!!</p>

<python><pymol>
Multiple input & output files?
# Multiple input & output files?

(Asked by: username_0 on 2020-06-29)

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!!

**Tags:** python, pymol

---

## 1 answers

### Answer by username_1 on 2020-06-30. Score: 1

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()


---

