File: count_molecules.py

package info (click to toggle)
pymol 1.8.4.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 42,248 kB
  • ctags: 24,095
  • sloc: cpp: 474,635; python: 75,034; ansic: 22,888; sh: 236; makefile: 78; csh: 21
file content (31 lines) | stat: -rw-r--r-- 965 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# This script illustrates how you can use PyMOL to count the
# number of molecules in a selection

if os.path.exists("$PYMOL_PATH/test/dat/1tii.pdb"):
    cmd.load("$PYMOL_PATH/test/dat/1tii.pdb")
else:
    from inspect import getsourcefile
    current_file_dir = os.path.dirname(os.path.abspath(getsourcefile(lambda:0)))
    cmd.load(os.path.join(current_file_dir, "../../test/dat/1tii.pdb"))

input_sele = "1tii and polymer"

prefix = "tmp_cnt_"

iter_sele = prefix + "iter"
mole_sele = prefix + "mole"

mole_cnt = 0

if cmd.select(iter_sele, input_sele)>0:
    while 1:
        atom_cnt = cmd.select(mole_sele, "bymol (first "+iter_sele+")")
        togo_cnt = cmd.select(iter_sele, iter_sele+" and not "+mole_sele)
        if atom_cnt>0:
            mole_cnt = mole_cnt + 1
            print "molecule %d contains %d atoms"%(mole_cnt,atom_cnt)
        if togo_cnt<=0:
            break
        

print "(%s) contains %d molecules total."%(input_sele, mole_cnt)