Skip to content

Commit e18819e

Browse files
committed
Score all ligands and merge best (PanDDA2)
1 parent 1f43fe8 commit e18819e

1 file changed

Lines changed: 46 additions & 35 deletions

File tree

src/dlstbx/wrapper/pandda_xchem.py

Lines changed: 46 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -238,10 +238,54 @@ def run(self):
238238
self.send_attachments_to_ispyb(attachments, final_directory)
239239
return False
240240

241+
# -------------------------------------------------------
242+
# Ligand scoring
243+
# Iterate over rhofit builds and score each one
244+
build_dir = out_dir / "rhofit"
245+
builds = list(build_dir.glob("Hit*.pdb"))
246+
247+
if not builds:
248+
self.log.info(f"No rhofit builds for {dtag}, can't continue")
249+
return False
250+
251+
scores = {}
252+
253+
for build_path in builds:
254+
ligand_score = build_dir / f"{build_path.stem}.txt"
255+
256+
st = gemmi.read_structure(str(build_path))
257+
chain, res = find_residue_by_name(st, "LIG")
258+
ligand_id = chain.name + f"/{res.seqid.num}"
259+
260+
score_command = f"source {PANDDA_2_DIR}/venv/bin/activate; \
261+
python {PANDDA_2_DIR}/scripts/ligand_score.py --mtz_path={mtz_file} --zmap_path={z_map} --ligand_id={ligand_id} --structure_path={build_path} --out_path={ligand_score}"
262+
263+
self.log.info(f"Running Ligand Score command: {score_command}")
264+
265+
try:
266+
os.system(score_command)
267+
268+
except Exception as e:
269+
self.log.error(f"Ligand score command: '{score_command}' failed")
270+
self.log.info(e.stdout)
271+
self.log.error(e.stderr)
272+
273+
with open(ligand_score, "r") as file:
274+
scores[build_path] = float(file.read().strip())
275+
276+
best_build_path = max(scores, key=lambda _x: scores[_x])
277+
score = scores[best_build_path]
278+
279+
self.log.info(f"Best ligand score for {dtag} = {score}")
280+
281+
# -------------------------------------------------------
282+
# Best build merging
283+
241284
# Merge the protein structure with ligand -> pandda model
242285
protein_st_file = dataset_pdir / f"{dtag}-pandda-input.pdb"
243-
ligand_st_file = out_dir / "rhofit" / "best.pdb"
286+
ligand_st_file = best_build_path
244287
pandda_model = modelled_dir / f"{dtag}-pandda-model.pdb"
288+
attachments.extend([pandda_model])
245289

246290
protein_st = gemmi.read_structure(str(protein_st_file))
247291
ligand_st = gemmi.read_structure(str(ligand_st_file))
@@ -254,40 +298,7 @@ def run(self):
254298
protein_st.write_pdb(str(pandda_model))
255299

256300
# -------------------------------------------------------
257-
# Ligand scoring
258-
259-
ligand_score = dataset_pdir / "ligand_score.txt"
260-
attachments.extend([pandda_model, ligand_score])
261-
262-
st = gemmi.read_structure(str(pandda_model))
263-
chain, res = find_residue_by_name(st, "LIG")
264-
ligand_id = chain.name + f"/{res.seqid.num}"
265-
266-
score_command = f"source {PANDDA_2_DIR}/venv/bin/activate; \
267-
python {PANDDA_2_DIR}/scripts/ligand_score.py --mtz_path={mtz_file} --zmap_path={z_map} --ligand_id={ligand_id} --structure_path={pandda_model} --out_path={ligand_score}"
268-
269-
self.log.info(f"Running Ligand Score command: {score_command}")
270-
271-
try:
272-
subprocess.run(
273-
score_command,
274-
shell=True,
275-
capture_output=True,
276-
text=True,
277-
cwd=panddas_dir,
278-
check=True,
279-
timeout=10 * 60,
280-
)
281-
282-
except subprocess.CalledProcessError as e:
283-
self.log.error(f"Ligand score command: '{score_command}' failed")
284-
self.log.info(e.stdout)
285-
self.log.error(e.stderr)
286-
287-
with open(ligand_score, "r") as file:
288-
score = float(file.read().strip())
289-
290-
self.log.info(f"Ligand score for {dtag} = {score}")
301+
# Output
291302

292303
try:
293304
cropped_event_map = save_cropped_map(

0 commit comments

Comments
 (0)