-
Notifications
You must be signed in to change notification settings - Fork 8
/
modal_pdb2png.py
372 lines (320 loc) · 11.6 KB
/
modal_pdb2png.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
"""
Visualize a pdb file as a png.
TODO: center on the ligand, if there is one, and find the best orientation.
"""
import json
from pathlib import Path
from typing import Union
from modal import App, Image
app = App("pdb2png")
image = (
Image.micromamba(python_version="3.11")
.micromamba_install("pymol-open-source==2.5.0", channels=["conda-forge"])
.apt_install("libgl1")
.apt_install("g++")
.pip_install(["ProDy==2.4.1"])
)
RENDER_OPTIONS = {
"default": {
"ray_opaque_background": "off",
"antialias": "2",
"orthoscopic": "on",
"depth_cue": "0",
"ray_trace_mode": "1",
"ray_trace_color": "black",
},
"default_bw": {
"bg_color": "white",
"ray_opaque_background": "on",
"antialias": "2",
"orthoscopic": "on",
"depth_cue": "0",
"ray_trace_mode": "2",
},
"default_cartoon": {
"ray_opaque_background": "off",
"antialias": "2",
"orthoscopic": "on",
"depth_cue": "0",
"ray_trace_mode": "3",
},
"dark": {
"bg_color": "black",
"ray_opaque_background": "on",
"antialias": "2",
"orthoscopic": "on",
"light_count": "2",
"specular": "1",
"depth_cue": "1",
"fog_start": "0.35",
"ray_trace_mode": "1",
"ray_trace_color": "black",
},
"flat": {
"bg_color": "white",
"valence": "0",
"bg_rgb": "white",
"reflect": "0",
"spec_direct": "0",
"light_count": "1",
"spec_count": "0",
"shininess": "0",
"power": "1",
"specular": "0",
"ambient_occlusion_mode": "1",
"ambient_occlusion_scale": "15",
"ambient_occlusion_smooth": "15",
"ray_trace_gain": "0.1",
"ambient": "0.9",
"direct": "0.2",
"ray_trace_mode": "0",
},
"cartoon": {
"cartoon_oval_length": "1.5",
"cartoon_oval_width": "0.5",
"cartoon_rect_length": "1.5",
"cartoon_rect_width": "0.5",
"cartoon_loop_radius": "0.3",
"ray_trace_mode": "1",
"ray_trace_color": "black",
"opaque_background": "off",
},
}
# in groups of three
DEFAULT_PROTEIN_COLORS = (0.8, 0.8, 0.6, 0.8, 0.6, 0.8, 0.6, 0.8, 0.8)
DEFAULT_HETATM_COLORS = (0.15, 0.7, 0.9, 0.9, 0.75, 0.15, 0.9, 0.15, 0.75)
def apply_render_style(render_style: str) -> None:
"""Apply render styles from a dict.
Everything is global, because pymol.
I cannot just use hasattr to tell what is cmd.set vs an attribute,
because some attributes are both cmd attributes and cmd.set attributes
e.g., valence is both an attribute of cmd and can be
set with cmd.set and seems to have different meanings
"""
from pymol import cmd
if render_style in RENDER_OPTIONS:
render_style_dict = RENDER_OPTIONS[render_style]
else:
render_style_dict = json.loads(render_style)
for k, v in render_style_dict.items():
if k == "bg_color":
cmd.bg_color(v)
else:
cmd.set(k, v)
def get_orientation_for_ligand(
pdb_file: str, ligand_id_or_chain: Union[str, tuple[str, str]]
) -> tuple[list, float]:
import numpy as np
from prody import calcCenter, parsePDB
structure = parsePDB(pdb_file)
if isinstance(ligand_id_or_chain, tuple):
ligand_center = calcCenter(
structure.select(f"resname {ligand_id_or_chain[0]} and chain {ligand_id_or_chain[1]}")
)
nonligand_center = calcCenter(
structure.select(
f"not resname {ligand_id_or_chain[0]} and chain {ligand_id_or_chain[1]}"
)
)
else:
ligand_chain = structure.select(f"chain {ligand_id_or_chain}")
ligand_resname = structure.select(f"resname {ligand_id_or_chain}")
if ligand_chain is not None:
ligand_center = calcCenter(structure.select(f"chain {ligand_id_or_chain}"))
nonligand_center = calcCenter(structure.select(f"not chain {ligand_id_or_chain}"))
elif ligand_resname is not None:
# ideally arbitrarily pick one chain if there are multiple
ligand_center = calcCenter(structure.select(f"resname {ligand_id_or_chain}"))
nonligand_center = calcCenter(structure.select(f"not resname {ligand_id_or_chain}"))
else:
raise ValueError(f"Could not find ligand with id or chain {ligand_id_or_chain}")
# calculate rotation to orient ligand forward here
forward_vector = ligand_center - nonligand_center
forward_vector = forward_vector / np.linalg.norm(forward_vector)
axis = np.cross(forward_vector, np.array([0, 0, 1]))
angle = np.arccos(np.dot(forward_vector, np.array([0, 0, 1])))
return [float(v) for v in axis], float(angle * 180 / np.pi)
@app.function(
image=image,
gpu=None,
timeout=60 * 15,
)
def pdb2png(
pdb_name: str,
pdb_str: str,
protein_rotates: list[tuple[float, float, float]] = None,
protein_color: tuple[float, float, float] = None,
protein_zoom: float = None,
hetatm_color: tuple[float, float, float] = None,
ligand_id: str = None,
ligand_chain: str = None,
ligand_zoom: float = None,
ligand_color: str = "red",
show_water: bool = False,
render_style: str = "default",
width: int = 1600,
height: int = 1600,
) -> list:
"""
Input is a pdb file.
Output is a png file.
"""
from pymol import cmd
in_dir = "/tmp/in_pp"
out_dir = "/tmp/out_pp"
Path(in_dir).mkdir(parents=True, exist_ok=True)
Path(out_dir).mkdir(parents=True, exist_ok=True)
in_pdb_file = Path(in_dir) / pdb_name
open(in_pdb_file, "w").write(pdb_str)
# --------------------------------------------------------------------------
# Rotation
#
png_num = None
png_num_str = ""
for protein_rotate in protein_rotates or [None]:
cmd.reinitialize() # move here
cmd.load(in_pdb_file)
if protein_rotate is not None:
cmd.rotate("x", protein_rotate[0])
cmd.rotate("y", protein_rotate[1])
cmd.rotate("z", protein_rotate[2])
png_num = png_num + 1 if png_num is not None else 0
png_num_str = f"_{png_num:04d}"
elif ligand_id is not None or ligand_chain is not None:
ligand_id_or_chain = (
(ligand_id, ligand_chain)
if ligand_id and ligand_chain
else (ligand_id or ligand_chain)
)
_axis, _angle = get_orientation_for_ligand(str(in_pdb_file), ligand_id_or_chain)
cmd.rotate(_axis, _angle)
else:
cmd.orient()
# --------------------------------------------------------------------------
# Colors
#
if protein_color is None:
protein_color = DEFAULT_PROTEIN_COLORS
if isinstance(protein_color, tuple):
n = 0
for chain in cmd.get_chains():
cmd.set_color("protein_color", protein_color[n : n + 3])
cmd.color("protein_color", f"chain {chain} and not hetatm")
n = (n + 3) % len(protein_color)
else:
# color is a string like "red"
cmd.color(protein_color, "not hetatm")
# Color proteins and hetatms
for hp_id, hp_color, hp_sel in [
("protein", protein_color, "not hetatm"),
("hetatm", hetatm_color, "hetatm"),
]:
if hp_color is not None:
if isinstance(hp_color, tuple):
n = 0
for chain in cmd.get_chains():
cmd.select(f"sel_{hp_id}_{chain}", f"chain {chain} and {hp_sel}")
if cmd.count_atoms(f"sel_{hp_id}_{chain}") > 0:
cmd.set_color(f"{hp_id}_color_{chain}", hp_color[n : n + 3])
cmd.color(f"{hp_id}_color_{chain}", f"sel_{hp_id}_{chain}")
n = (n + 3) % len(hp_color)
else:
cmd.color(hp_color, hp_sel)
if protein_zoom is not None:
cmd.zoom("all", protein_zoom)
# --------------------------------------------------------------------------
# Ligand
#
if ligand_id is not None:
and_chain = f"and chain {ligand_chain}" if ligand_chain else ""
cmd.select("ligand", f"resn {ligand_id} {and_chain}")
if ligand_zoom is not None:
cmd.zoom("ligand", ligand_zoom)
if ligand_color is None:
ligand_color = DEFAULT_HETATM_COLORS
if isinstance(ligand_color, tuple):
cmd.set_color("ligand_color", ligand_color)
cmd.color("ligand_color", "ligand")
else:
cmd.color(ligand_color, "ligand")
if not show_water:
cmd.select("HOH", "resn HOH")
cmd.hide("everything", "HOH")
# --------------------------------------------------------------------------
# Render and save
#
apply_render_style(render_style)
cmd.ray(width, height)
out_png_path = Path(out_dir) / f"{Path(pdb_name).with_suffix('')}{png_num_str}.png"
out_png_path.parent.mkdir(parents=True, exist_ok=True)
cmd.save(out_png_path, in_pdb_file)
return [
(out_file.relative_to(out_dir), open(out_file, "rb").read())
for out_file in Path(out_dir).glob("**/*")
if Path(out_file).is_file()
]
def _parse_rotation_range(rotate_str):
"""convert arg string to list of tuples for animation
e.g., "100-200,0,0,10" -> [(100,0,0), (110,0,0), ...]
"""
*ranges, num_steps = rotate_str.split(",")
steps = int(num_steps)
# if there is no range given, then just double up the number
start_end = [
(float(r), float(r)) if "-" not in r else (float(r.split("-")[0]), float(r.split("-")[1]))
for r in ranges
]
steps_sizes = [(end - start) / steps for start, end in start_end]
return [
tuple(start + (step * i) for (start, _), step in zip(start_end, steps_sizes))
for i in range(steps)
]
@app.local_entrypoint()
def main(
input_pdb,
protein_rotate: str = None,
protein_color: str = None,
protein_zoom: float = None,
hetatm_color: str = None,
ligand_id: str = None,
ligand_chain: str = None,
ligand_zoom: float = None,
ligand_color: str = "red",
show_water: bool = False,
render_style: str = "default",
width: int = 1600,
height: int = 1600,
out_dir: str = ".",
):
if protein_rotate is not None and "-" in protein_rotate:
protein_rotates = _parse_rotation_range(protein_rotate)
elif protein_rotate is not None:
protein_rotates = [tuple(map(float, protein_rotate.split(",")))][:3]
else:
protein_rotates = None
if protein_color is not None and "," in protein_color:
protein_color = tuple(map(float, protein_color.split(",")))
if ligand_color is not None and "," in ligand_color:
ligand_color = tuple(map(float, ligand_color.split(",")))
pdb_str = open(input_pdb).read()
outputs = pdb2png.remote(
Path(input_pdb).name,
pdb_str,
protein_rotates,
protein_color,
protein_zoom,
hetatm_color,
ligand_id,
ligand_chain,
ligand_zoom,
ligand_color,
show_water,
render_style,
width,
height,
)
for out_file, out_content in outputs:
(Path(out_dir) / Path(out_file)).parent.mkdir(parents=True, exist_ok=True)
if out_content:
with open((Path(out_dir) / Path(out_file)), "wb") as out:
out.write(out_content)