Skip to content

Commit 762edbe

Browse files
committed
Move ligand_fit html generation to dlstbx
1 parent 97d7ebd commit 762edbe

File tree

3 files changed

+113
-24
lines changed

3 files changed

+113
-24
lines changed

src/dlstbx/util/mvs/__init__.py

Whitespace-only changes.

src/dlstbx/util/mvs/ligandfit.py

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
from __future__ import annotations
2+
3+
import molviewspec as mvs
4+
5+
6+
def gen_html_ligandfit(pdb_file, map_file, outdir, acr, smiles, cc):
7+
# make a story from snapshots
8+
builder = mvs.create_builder()
9+
structure = builder.download(url=pdb_file).parse(format="pdb").model_structure()
10+
structure.component(selector="polymer").representation(
11+
type="surface", size_factor=0.7
12+
).opacity(opacity=0.2).color(color="#AABDF1")
13+
structure.component(selector="polymer").representation().opacity(
14+
opacity=0.25
15+
).color(custom={"molstar_color_theme_name": "chain_id"})
16+
structure.component(selector="ligand").representation(type="ball_and_stick").color(
17+
custom={"molstar_color_theme_name": "element-symbol"}
18+
)
19+
structure.component(selector="ligand").representation(type="surface").opacity(
20+
opacity=0.1
21+
).color(custom={"molstar_color_theme_name": "element-symbol"})
22+
23+
ccp4 = builder.download(url=map_file).parse(format="map")
24+
ccp4.volume().representation(
25+
type="isosurface",
26+
relative_isovalue=1.5,
27+
show_wireframe=True,
28+
show_faces=False,
29+
).color(color="blue").opacity(opacity=0.25)
30+
31+
snapshot1 = builder.get_snapshot(
32+
title="Main View",
33+
description=f"## Ligand_Fit Results: \n ### {acr} with ligand & electron density map \n - SMILES: {smiles} \n - 2FO-FC at 1.5σ, blue \n - Fitting CC = {cc}",
34+
transition_duration_ms=2000,
35+
linger_duration_ms=5000,
36+
)
37+
38+
# SNAPSHOT2
39+
builder = mvs.create_builder()
40+
structure = builder.download(url=pdb_file).parse(format="pdb").model_structure()
41+
structure.component(selector="polymer").representation(
42+
type="surface", size_factor=0.7
43+
).opacity(opacity=0.5).color(color="#D8BFD8")
44+
structure.component(selector="polymer").representation().opacity(opacity=0.6).color(
45+
color="grey"
46+
)
47+
structure.component(selector="ligand").focus().representation(
48+
type="ball_and_stick"
49+
).color(custom={"molstar_color_theme_name": "element-symbol"})
50+
51+
ccp4 = builder.download(url=map_file).parse(format="map")
52+
ccp4.volume().representation(
53+
type="isosurface",
54+
relative_isovalue=1.5,
55+
show_wireframe=True,
56+
show_faces=False,
57+
).color(color="blue").opacity(opacity=0.25)
58+
59+
# add a label
60+
# info = get_chain_and_residue_numbers(pdb_file, "LIG")
61+
# resid = info[0][1]
62+
residue = mvs.ComponentExpression(label_seq_id=202)
63+
(
64+
structure.component(
65+
selector=residue,
66+
custom={
67+
"molstar_show_non_covalent_interactions": True,
68+
"molstar_non_covalent_interactions_radius_ang": 5.0,
69+
},
70+
).label(text=f"CC = {cc}")
71+
)
72+
73+
snapshot2 = builder.get_snapshot(
74+
title="Focus View",
75+
description=f"## Ligand_Fit Results: \n ### {acr} with ligand & electron density map \n - SMILES: {smiles} \n - 2FO-FC at 1.5σ, blue \n - Fitting CC = {cc}",
76+
transition_duration_ms=2000,
77+
linger_duration_ms=5000,
78+
)
79+
80+
states = mvs.States(
81+
snapshots=[snapshot1, snapshot2],
82+
metadata=mvs.GlobalMetadata(description="Ligand_fit Results"),
83+
)
84+
85+
with open(pdb_file) as f:
86+
pdb_data = f.read()
87+
88+
with open(map_file, mode="rb") as f:
89+
map_data = f.read()
90+
91+
html = mvs.molstar_html(
92+
states,
93+
data={pdb_file: pdb_data, map_file: map_data},
94+
ui="stories",
95+
)
96+
97+
with open(f"{outdir}/ligand_fit.html", "w") as f:
98+
f.write(html)

src/dlstbx/wrapper/ligand_fit.py

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
import subprocess
99
from shutil import ignore_patterns
1010

11-
# import molviewspec as mvs
1211
import dlstbx.util.symlink
12+
from dlstbx.util.mvs.ligandfit import gen_html_ligandfit
1313
from dlstbx.wrapper import Wrapper
1414

1515

@@ -28,7 +28,7 @@ def pull_CC_from_log(self, pipeline_directory):
2828
if match:
2929
llist.append(match.group(1))
3030
file_read.close()
31-
CC = llist[-1] # take the final CC
31+
CC = float(llist[-1]) # take the final CC
3232
return CC
3333

3434
def send_attachments_to_ispyb(self, pipeline_directory, final_directory):
@@ -139,22 +139,20 @@ def run(self):
139139
self.send_attachments_to_ispyb(pipeline_directory)
140140
return False
141141

142+
os.system(
143+
f"phenix.mtz2map {pipeline_directory / 'LIG_final.mtz'} {pipeline_directory / 'LIG_final.pdb'} directory={pipeline_directory} selection='resname LIG' buffer=3.5 labels=2FOFCWT,PH2FOFCWT"
144+
)
145+
out_map = str(pipeline_directory / "LIG_final_2mFo-DFc.ccp4")
146+
out_pdb = str(pipeline_directory / "LIG_final.pdb")
147+
acr = params.get("acronym", "Protein")
148+
# self.generate_smiles_png(smiles, pipeline_directory)
142149
CC = self.pull_CC_from_log(pipeline_directory)
143-
144-
if CC >= min_cc_keep:
145-
os.system(
146-
f"phenix.mtz2map {pipeline_directory / 'LIG_final.mtz'} {pipeline_directory / 'LIG_final.pdb'} directory={pipeline_directory} selection='resname LIG' buffer=3.5 labels=2FOFCWT,PH2FOFCWT"
147-
)
148-
out_map = str(pipeline_directory / "LIG_final_2mFo-DFc.ccp4")
149-
out_pdb = str(pipeline_directory / "LIG_final.pdb")
150-
acr = params.get("acronym", "Protein")
151-
152-
os.system(
153-
f"module load molviewspec; gen_html_ligandfit.py --pdb_file {out_pdb} --map_file {out_map} --cc {CC} --outdir {pipeline_directory} --smiles '{smiles}' --acr {acr}"
150+
try:
151+
gen_html_ligandfit(
152+
out_pdb, out_map, pipeline_directory, cc=CC, smiles=smiles, acr=acr
154153
)
155-
156-
# self.generate_smiles_png(smiles, pipeline_directory)
157-
# self.generate_html_visualisation(out_pdb, out_map, pipeline_directory, cc=CC, smiles=smiles, acr=acr)
154+
except Exception as e:
155+
self.log.debug(f"Exception generating mvs html: {e}")
158156

159157
data = [
160158
["Ligand_fit pipeline", "SMILES code", "Fitting CC"],
@@ -182,14 +180,7 @@ def run(self):
182180
self.log.info("Sending results to ISPyB")
183181
self.send_attachments_to_ispyb(pipeline_directory, final_directory)
184182

185-
if CC >= min_cc_keep:
186-
self.log.info("Ligand_fitting pipeline finished successfully")
187-
return True
188-
else:
189-
self.log.info(
190-
f"Ligand_fitting pipeline finished but ligand fitting CC ({CC}) did not meet quality threshold ({min_cc_keep})"
191-
)
192-
return False
183+
return True
193184

194185
# def generate_smiles_png(self, smiles, outdir):
195186
# mol = pybel.readstring("smi", smiles)

0 commit comments

Comments
 (0)