-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModule 3 (Single).py
More file actions
411 lines (358 loc) · 14.5 KB
/
Copy pathModule 3 (Single).py
File metadata and controls
411 lines (358 loc) · 14.5 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
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Module 3: SDF -> ligand PDBQT via Meeko (QUIET + GRACEFUL STOP)
- Inputs:
- 3D_Structures/<id>.sdf
- state/manifest.csv (optional)
- Outputs:
- prepared_ligands/<id>.pdbqt
- manifest updated (pdbqt_* fields, tools_meeko)
Behavior changes:
- No per-ligand Meeko logs are written (suppresses *_meeko.log).
- Subprocess stdout/stderr are discarded to avoid clutter.
- Ctrl+C once: finish current ligand, flush manifest, exit cleanly.
- Ctrl+C twice: exit loop ASAP after a safe checkpoint and flush manifest.
- On start, optionally purges old *_meeko.log in prepared_ligands.
"""
from __future__ import annotations
def run_meeko_prepare(infile, outfile, extra_args=None, quiet=False):
"""Cross-platform Meeko ligand preparation.
Tries mk_prepare_ligand, then module forms (main_prepare_ligand / cli_prepare_ligand).
Uses the same Python interpreter running this script.
"""
if extra_args is None:
extra_args = []
# Normalize to strings
infile = str(infile)
outfile = str(outfile)
# Try direct CLI first
try:
meeko_exe = shutil.which("mk_prepare_ligand")
except Exception:
meeko_exe = None
pyexe = sys.executable or "python3"
candidates = []
if meeko_exe:
candidates.append([meeko_exe, "-i", infile, "-o", outfile] + extra_args)
# modern then legacy module paths
candidates.append([pyexe, "-m", "meeko.main_prepare_ligand", "-i", infile, "-o", outfile] + extra_args)
candidates.append([pyexe, "-m", "meeko.cli_prepare_ligand", "-i", infile, "-o", outfile] + extra_args)
last_err = None
for cmd in candidates:
try:
# Smoke test with -h to see if command exists
try:
test_cmd = cmd[:]
# replace input/output with -h for the smoke test
test_cmd = test_cmd[:2] if test_cmd[0].endswith("mk_prepare_ligand") else test_cmd[:3]
test_cmd += ["-h"]
subprocess.run(test_cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, check=True)
except Exception:
# If smoke test fails, try running anyway (some envs return nonzero for -h)
pass
if not quiet:
print("▶ Preparing ligand via:", " ".join(cmd))
res = subprocess.run(cmd, capture_output=True, text=True)
if res.returncode == 0 and os.path.exists(outfile):
if not quiet:
print(f"✓ Ligand prepared: {outfile}")
return True
else:
last_err = res.stderr or res.stdout
if not quiet:
print(f"⚠️ Meeko returned {res.returncode} for {infile}\n{last_err}")
except FileNotFoundError as e:
last_err = str(e)
continue
except Exception as e:
last_err = str(e)
continue
raise RuntimeError("""❌ Meeko CLI not found or failed to run.
Tried: mk_prepare_ligand, python -m meeko.main_prepare_ligand, python -m meeko.cli_prepare_ligand
Last error:
{}
Install/upgrade with:
python3 -m pip install --upgrade meeko
Or install latest dev:
python3 -m pip install --no-cache-dir git+https://github.com/forlilab/meeko.git
""".format(last_err if last_err else "(no stderr)"))
import shutil
import sys
import csv
import hashlib
import json
import shlex
import subprocess
import signal
from pathlib import Path
from datetime import datetime, timezone
# ------------------------------ Graceful stop --------------------------------
STOP_REQUESTED = False
HARD_STOP = False
def _handle_sigint(sig, frame):
global STOP_REQUESTED, HARD_STOP
if not STOP_REQUESTED:
STOP_REQUESTED = True
print("\n⏹️ Ctrl+C detected — finishing current ligand, then exiting cleanly...")
print(" (Press Ctrl+C again to stop ASAP after checkpoint.)")
else:
HARD_STOP = True
print("\n⏭️ Second Ctrl+C — will abort the loop ASAP and finalize outputs.")
signal.signal(signal.SIGINT, _handle_sigint)
# ------------------------------ Optional: Meeko ver --------------------------
try:
import meeko # type: ignore
MEEKO_VER = getattr(meeko, "__version__", "unknown")
except Exception:
MEEKO_VER = ""
# ------------------------------ Optional YAML --------------------------------
try:
import yaml # pip install pyyaml
except Exception:
yaml = None
# ------------------------------ Paths ----------------------------------------
BASE = Path(".").resolve()
DIR_INPUT = BASE / "input"
DIR_STATE = BASE / "state"
DIR_SDF = BASE / "3D_Structures"
DIR_PDBQT = BASE / "prepared_ligands"
DIR_LOGS = BASE / "logs"
FILE_INPUT = DIR_INPUT / "input.csv"
FILE_MANIFEST = DIR_STATE / "manifest.csv"
FILE_RUNYML = BASE / "config" / "run.yml"
FILE_MACHINEYML = BASE / "config" / "machine.yml"
for d in (DIR_PDBQT, DIR_LOGS):
d.mkdir(parents=True, exist_ok=True)
# ------------------------------ Helpers --------------------------------------
def now_iso() -> str:
return datetime.now(timezone.utc).replace(microsecond=0).isoformat().replace("+00:00", "Z")
def read_csv(path: Path) -> list[dict]:
if not path.exists():
return []
with path.open("r", newline="", encoding="utf-8") as f:
return [dict(row) for row in csv.DictReader(f)]
def write_csv(path: Path, rows: list[dict], headers: list[str]) -> None:
path.parent.mkdir(parents=True, exist_ok=True)
with path.open("w", newline="", encoding="utf-8") as f:
w = csv.DictWriter(f, fieldnames=headers)
w.writeheader()
for r in rows:
w.writerow({k: r.get(k, "") for k in headers})
def deep_update(dst: dict, src: dict):
for k, v in (src or {}).items():
if isinstance(v, dict) and isinstance(dst.get(k), dict):
deep_update(dst[k], v)
else:
dst[k] = v
def load_yaml(path: Path) -> dict:
if not (yaml and path.exists()):
return {}
try:
data = yaml.safe_load(path.read_text(encoding="utf-8")) or {}
return data if isinstance(data, dict) else {}
except Exception:
return {}
def load_config() -> dict:
# Defaults keep it quiet and fast
cfg = {
"tools": {
"meeko_cmd": "mk_prepare_ligand.py.exe", # or mk_prepare_ligand if on PATH
"python_exe": "python"
},
"policy": {
"skip_if_done": True,
"purge_old_meeko_logs": True, # delete lingering *_meeko.log on start
"write_logs": False, # do NOT write per-ligand logs
"quiet_subprocess": True # discard stdout/stderr from meeko
}
}
deep_update(cfg, load_yaml(FILE_RUNYML))
deep_update(cfg, load_yaml(FILE_MACHINEYML))
return cfg
def config_hash() -> str:
chunks = []
for p in (FILE_RUNYML, FILE_MACHINEYML):
if p.exists():
chunks.append(p.read_text(encoding="utf-8"))
if not chunks:
chunks.append("{}")
return hashlib.sha1("||".join(chunks).encode("utf-8")).hexdigest()[:10]
# ------------------------------ Manifest -------------------------------------
MANIFEST_FIELDS = [
"id","smiles","inchikey",
"admet_status","admet_reason",
"sdf_status","sdf_path","sdf_reason",
"pdbqt_status","pdbqt_path","pdbqt_reason",
"vina_status","vina_score","vina_pose","vina_reason",
"config_hash","receptor_sha1","tools_rdkit","tools_meeko","tools_vina",
"created_at","updated_at"
]
def load_manifest() -> dict[str, dict]:
if not FILE_MANIFEST.exists():
return {}
rows = read_csv(FILE_MANIFEST)
out = {}
for r in rows:
row = {k: r.get(k, "") for k in MANIFEST_FIELDS}
out[row["id"]] = row
return out
def save_manifest(manifest: dict[str, dict]) -> None:
rows = [{k: v.get(k, "") for k in MANIFEST_FIELDS} for _, v in sorted(manifest.items())]
write_csv(FILE_MANIFEST, rows, MANIFEST_FIELDS)
# ------------------------------ Discovery ------------------------------------
def discover_sdf(manifest: dict[str, dict]) -> dict[str, Path]:
id2sdf: dict[str, Path] = {}
for lig_id, row in manifest.items():
p = (row.get("sdf_path") or "").strip()
if p:
path = Path(p)
if not path.is_absolute():
path = (BASE / p).resolve()
if path.exists():
id2sdf[lig_id] = path
for sdf in sorted(DIR_SDF.glob("*.sdf")):
lig_id = sdf.stem
id2sdf.setdefault(lig_id, sdf.resolve())
return id2sdf
# ------------------------------ Validation -----------------------------------
def pdbqt_is_valid(path: Path) -> bool:
try:
if not path.exists() or path.stat().st_size < 200:
return False
txt = path.read_text(errors="ignore")
has_atom = ("ATOM " in txt) or ("HETATM" in txt)
has_tors = "TORSDOF" in txt
return has_atom and has_tors
except Exception:
return False
# ------------------------------ Meeko call (QUIET) ---------------------------
def run_meeko_quiet(meeko_cmd: str, python_exe: str, in_sdf: Path, out_pdbqt: Path,
quiet: bool = True) -> tuple[bool, str]:
"""
Try in order:
1) meeko_cmd (default: mk_prepare_ligand.py.exe)
2) mk_prepare_ligand
3) python -m meeko.cli_prepare_ligand
Writes to out_pdbqt.tmp first, validates, then renames.
Returns (ok, reason)
"""
in_sdf = in_sdf.resolve()
out_pdbqt = out_pdbqt.resolve()
out_pdbqt.parent.mkdir(parents=True, exist_ok=True)
tmp_pdbqt = out_pdbqt.with_suffix(".pdbqt.tmp")
# Clean stale outputs
for p in (out_pdbqt, tmp_pdbqt):
try:
if Path(p).exists():
Path(p).unlink()
except Exception:
pass
def _exec(cmd_list: list[str]) -> int:
if quiet:
res = subprocess.run(
cmd_list,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
shell=False
)
else:
res = subprocess.run(cmd_list, shell=False)
return res.returncode
# Candidate commands
candidates = [
[meeko_cmd, "-i", str(in_sdf), "-o", str(tmp_pdbqt)],
["mk_prepare_ligand", "-i", str(in_sdf), "-o", str(tmp_pdbqt)],
[python_exe, "-m", "meeko.cli_prepare_ligand", "-i", str(in_sdf), "-o", str(tmp_pdbqt)],
]
for cmd in candidates:
try:
rc = _exec(cmd)
if rc == 0 and pdbqt_is_valid(tmp_pdbqt):
tmp_pdbqt.replace(out_pdbqt)
return True, f"OK via {cmd[0]}"
except FileNotFoundError:
continue
except Exception:
continue
# Failure cleanup
try:
if tmp_pdbqt.exists():
tmp_pdbqt.unlink()
except Exception:
pass
return False, "All Meeko attempts failed"
# ------------------------------ Main -----------------------------------------
def main():
cfg = load_config()
chash = config_hash()
meeko_cmd = str(cfg.get("tools", {}).get("meeko_cmd", "mk_prepare_ligand.py.exe"))
python_exe = str(cfg.get("tools", {}).get("python_exe", "python"))
skip_if_done = bool(cfg.get("policy", {}).get("skip_if_done", True))
purge_old_logs = bool(cfg.get("policy", {}).get("purge_old_meeko_logs", True))
quiet_subprocess = bool(cfg.get("policy", {}).get("quiet_subprocess", True))
# write_logs is intentionally unused (always False here); we keep the knob for future.
manifest = load_manifest()
id2sdf = discover_sdf(manifest)
if not id2sdf:
raise SystemExit("❌ No SDFs found. Run Module 2 first.")
# Optional: purge old *_meeko.log files that could interfere with Vina-GPU tools
if purge_old_logs:
for logf in DIR_PDBQT.glob("*_meeko.log"):
try:
logf.unlink()
except Exception:
pass
done, failed = 0, 0
created_ts = now_iso()
try:
for idx, (lig_id, sdf_path) in enumerate(sorted(id2sdf.items()), 1):
if STOP_REQUESTED or HARD_STOP:
print("🧾 Stop requested — finalizing after this checkpoint...")
break
out_pdbqt = (DIR_PDBQT / f"{lig_id}.pdbqt").resolve()
# Skip only if an existing PDBQT validates
if skip_if_done and pdbqt_is_valid(out_pdbqt):
m = manifest.get(lig_id, {k:"" for k in MANIFEST_FIELDS})
m["id"] = lig_id
m["sdf_path"] = str(sdf_path)
m["pdbqt_status"] = "DONE"
m["pdbqt_path"] = str(out_pdbqt)
m["pdbqt_reason"] = "Found existing valid PDBQT"
m["config_hash"] = chash
m["tools_meeko"] = MEEKO_VER or "Meeko"
m.setdefault("created_at", created_ts)
m["updated_at"] = now_iso()
manifest[lig_id] = m
done += 1
if idx % 50 == 0:
save_manifest(manifest)
continue
ok, reason = run_meeko_quiet(meeko_cmd, python_exe, sdf_path, out_pdbqt, quiet=quiet_subprocess)
# Update manifest
m = manifest.get(lig_id, {k:"" for k in MANIFEST_FIELDS})
m["id"] = lig_id
m["sdf_path"] = str(sdf_path)
m["pdbqt_status"] = "DONE" if ok else "FAILED"
m["pdbqt_path"] = str(out_pdbqt)
m["pdbqt_reason"] = "OK" if ok else reason
m["config_hash"] = chash
m["tools_meeko"] = MEEKO_VER or "Meeko"
m.setdefault("created_at", created_ts)
m["updated_at"] = now_iso()
manifest[lig_id] = m
if ok:
done += 1
else:
failed += 1
if idx % 50 == 0:
save_manifest(manifest)
finally:
save_manifest(manifest)
print(f"✅ SDF → PDBQT complete (or stopped). DONE: {done} FAILED: {failed}")
print(f" Outputs in: {DIR_PDBQT}")
print(f" Manifest updated: {FILE_MANIFEST}")
if STOP_REQUESTED or HARD_STOP:
print(" (Exited early by user request.)")
if __name__ == "__main__":
main()