-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathutil.py
More file actions
107 lines (92 loc) · 2.86 KB
/
util.py
File metadata and controls
107 lines (92 loc) · 2.86 KB
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
"""
utility functions for the workshop.
"""
def visualize_poses(
protein_file,
pose_file,
cognate_file=None,
animate=True,
cognate_color=None,
pose_color=None,
highlight_residues=None,
):
"""
Creates a 3D visualization of a protein with multiple ligand poses from a single SDF file.
Parameters:
-----------
protein_file : str
Path to the protein PDB file to visualize
pose_file : str
Path to the SDF file containing multiple ligand poses
cognate_file : str, optional
Path to the cognate (static structure) file if available
animate : bool, optional
Whether to animate through the poses (default: True)
cognate_color: str, optional
Color for cognate. Default is yellow.
pose_color: str, optional
Color for ligand poses. Default is green.
highlight_residues: list, optional
List of residue numbers to highlight with VDW representation
Returns:
--------
py3Dmol.view
The 3D visualization view object
"""
import py3Dmol
# Get colors
if cognate_color is None:
cognate_color = "yellow"
if pose_color is None:
pose_color = "green"
# Create the viewer
v = py3Dmol.view()
# Add protein structure
with open(protein_file) as f:
v.addModel(f.read())
# Set protein style
v.setStyle({"cartoon": {}, "stick": {"radius": 0.1}})
# Add VDW representation for highlighted residues if specified
if highlight_residues:
for res in highlight_residues:
v.addStyle(
{"model": 0, "resi": str(res)},
{"sphere": {"color": "magenta", "opacity": 0.7, "scale": 0.7}},
)
# Add cognate ligand if provided
if cognate_file:
with open(cognate_file) as f:
v.addModel(f.read())
v.setStyle(
{"model": 1},
{"stick": {"colorscheme": f"{cognate_color}Carbon", "radius": 0.25}},
)
# Add all poses from the SDF file
model_offset = 1 if cognate_file else 0
with open(pose_file) as f:
pose_content = f.read()
if animate:
# Add all poses as animation frames
v.addModelsAsFrames(pose_content)
v.setStyle(
{"model": model_offset + 1},
{"stick": {"colorscheme": f"{pose_color}Carbon"}},
)
v.animate({"interval": 1000})
else:
# Add as separate models
v.addModel(pose_content)
v.setStyle(
{"model": model_offset + 1},
{"stick": {"colorscheme": f"{pose_color}Carbon"}},
)
# Set view - zoom to the correct structure
if cognate_file:
# Zoom to cognate if provided
zoom_model = 1
else:
# Zoom to docked poses if no cognate
zoom_model = model_offset + 1
v.zoomTo({"model": zoom_model})
v.rotate(270)
return v