-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathpymol_arrows.py
executable file
·144 lines (121 loc) · 4.95 KB
/
pymol_arrows.py
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/env python3
import sys
import os
import argparse
def write_pymol_arrows(base, structs, scale, color, radius, hradius, hlength, threshold):
pymol_file = base + '_arrows.pymol'
lines = []
arrow_objs = set()
t2 = threshold**2
s2 = scale**2
for i, struct in enumerate(structs):
for j, atom in enumerate(struct):
arrow_obj = base + '_arrow_' + str(j)
arrow_objs.add(arrow_obj)
elem, xi, yi, zi, dx, dy, dz = atom
xf = xi + scale*dx
yf = yi + scale*dy
zf = zi + scale*dz
line = 'cgo_arrow [{}, {}, {}], [{}, {}, {}]'.format(xi, yi, zi, xf, yf, zf)
if len(structs) > 1:
line += ', state={}'.format(i+1)
if radius:
line += ', radius={}'.format(radius)
if hradius > 0:
line += ', hradius={}'.format(hradius)
if hlength > 0:
line += ', hlength={}'.format(hlength)
if color:
line += ', color={}'.format(color)
line += ', name={}'.format(arrow_obj)
if (dx**2 + dy**2 + dz**2)*s2 > t2:
lines.append(line)
arrow_group = base + '_arrows'
line = 'group {}, {}'.format(arrow_group, ' '.join(arrow_objs))
lines.append(line)
with open(pymol_file, 'w') as f:
f.write('\n'.join(lines))
def xyz_line_to_atom(xyz_line):
fields = xyz_line.split()
elem = fields[0]
x = float(fields[1])
y = float(fields[2])
z = float(fields[3])
dx = float(fields[4])
dy = float(fields[5])
dz = float(fields[6])
return elem, x, y, z, dx, dy, dz
def atom_to_pdb_line(atom, idx, dosum):
if not isinstance(idx, int) or idx < 0 or idx > 99999:
raise TypeError('idx must be an integer from 0 to 99999 ({})'.format(idx))
elem, x, y, z, dx, dy, dz = atom
if len(elem) not in {1, 2}:
raise IndexError('atom elem must be a string of length 1 or 2 ({})'.format(elem))
if dosum:
d = dx+dy+dz
else:
d = (dx**2 + dy**2 + dz**2)**0.5
return '{:6}{:5} {:4}{:1}{:3} {:1}{:4}{:1} {:8.3f}{:8.3f}{:8.3f}{:6.2f}{:6f} {:2}{:2}' \
.format('ATOM', idx, '', '', '', '', '', '', x, y, z, 1.0, d, elem.rjust(2), '')
def read_xyz_file(xyz_file, header_len=2):
with open(xyz_file, 'r') as f:
lines = f.readlines()
structs = []
struct_start = 0
for i, line in enumerate(lines):
try:
# line index relative to struct start
j = i - struct_start
if j == 0 or j >= header_len + n_atoms:
struct_start = i
structs.append([])
n_atoms = int(lines[i])
elif j < header_len:
continue
else:
atom = xyz_line_to_atom(lines[i])
structs[-1].append(atom)
except:
print('{}:{} {}'.format(xyz_file, i, repr(line)), file=sys.stderr)
raise
return structs
def write_pdb_file(pdb_file, atoms, dosum):
lines = []
for i, atom in enumerate(atoms):
line = atom_to_pdb_line(atom, i, dosum)
lines.append(line)
if pdb_file:
with open(pdb_file, 'w') as f:
f.write('\n'.join(lines))
else:
print('\n'.join(lines))
def parse_args():
parser = argparse.ArgumentParser(description='Output a pymol script that creates \
arrows from an .xyz file containing atom coordinates and gradient components, \
can also create a .pdb file where the b-factor is the gradient magnitude')
parser.add_argument('xyz_file')
parser.add_argument('-s', '--scale', type=float, default=1.0,
help='Arrow length scaling factor')
parser.add_argument('-c', '--color', type=str, default='',
help='Arrow color or pair of colors, e.g. "white black"')
parser.add_argument('-r', '--radius', type=float, default=0.2,
help='Radius of arrow body')
parser.add_argument('-hr', '--hradius', type=float, default=-1,
help='Radius of arrow head')
parser.add_argument('-hl', '--hlength', type=float, default=-1,
help='Length of arrow head')
parser.add_argument('-p', '--pdb_file', action='store_true', default=False,
help='Output a .pdb file where the b-factor is gradient magnitude')
parser.add_argument('--sum', action='store_true', default=False,
help='Sum gradient components instead of taking magnitude')
parser.add_argument('-t', '--threshold', type=float, default=0,
help="Gradient threshold for drawing arrows (using scale factor)")
return parser.parse_args()
if __name__ == '__main__':
args = parse_args()
structs = read_xyz_file(args.xyz_file)
base_name = args.xyz_file.replace('.xyz', '')
write_pymol_arrows(base_name, structs, args.scale, args.color, args.radius, args.hradius, args.hlength, args.threshold)
if args.pdb_file:
pdb_file = base_name + '.pdb'
write_pdb_file(pdb_file, atoms, args.sum)