Reset repository and upload final project (part 30)
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- .gitattributes +6 -0
- libs/adaptive/._state.py +0 -0
- libs/adaptive/._surrogate_model.py +0 -0
- libs/adaptive/._thresholding.py +0 -0
- libs/adaptive/._weight_schedule.py +0 -0
- libs/analysis/.___init__.py +0 -0
- libs/analysis/.___pycache__ +0 -0
- libs/analysis/._backend_diagnostic.py +0 -0
- libs/analysis/._ranking.py +0 -0
- libs/benchmark/.___init__.py +0 -0
- libs/benchmark/.___pycache__ +0 -0
- libs/benchmark/._budget_efficiency.py +0 -0
- libs/benchmark/._cleanup.py +0 -0
- libs/benchmark/._disk_guard.py +0 -0
- libs/benchmark/._large_library.py +0 -0
- libs/benchmark/._multifidelity.py +0 -0
- libs/benchmark/._ordering.py +0 -0
- libs/benchmark/._overnight_stop.py +0 -0
- libs/benchmark/._policy_repair.py +0 -0
- libs/benchmark/._redocking_validation.py +0 -0
- libs/benchmark/._reuse.py +0 -0
- libs/benchmark/._runtime.py +0 -0
- libs/docking/._.DS_Store +0 -0
- libs/docking/.___init__.py +0 -0
- libs/docking/.___pycache__ +0 -0
- libs/docking/._backend_rdock.py +0 -0
- libs/docking/._backend_smina.py +0 -0
- libs/docking/._base.py +0 -0
- libs/docking/._comparison.py +0 -0
- libs/docking/._constraints.py +0 -0
- libs/docking/._interface_features.py +0 -0
- libs/docking/._plip_interactions.py +0 -0
- libs/docking/._pocket.py +0 -0
- libs/docking/._pose_parsing.py +0 -0
- libs/docking/._prep.py +0 -0
- libs/docking/._rdock_features.py +0 -0
- libs/docking/__init__.py +0 -2
- libs/docking/backend_rdock.py +242 -15
- libs/docking/plip_interactions.py +194 -0
- libs/encoders/.___init__.py +0 -0
- libs/encoders/.___pycache__ +0 -0
- libs/encoders/._graph_builders.py +0 -0
- libs/encoders/._ligand_encoder.py +0 -0
- libs/encoders/._protein_encoder.py +0 -0
- libs/encoders/._schemas.py +0 -0
- libs/encoders/._sequence_features.py +0 -0
- libs/encoders/._structure_features.py +0 -0
- libs/modalities/.___init__.py +0 -0
- libs/modalities/.___pycache__ +0 -0
- libs/modalities/._peptide_like.py +0 -0
.gitattributes
CHANGED
|
@@ -208,3 +208,9 @@ data/datasets/rdock_3PTB_50k_pubchem_local/ligands/all_ligands.sdf filter=lfs di
|
|
| 208 |
data/datasets/rdock_3PTB_50k_pubchem_local/ligands/ligand_metadata.csv filter=lfs diff=lfs merge=lfs -text
|
| 209 |
data/datasets/rdock_3PTB_50k_pubchem_local/target/rdock_prm/receptor.as filter=lfs diff=lfs merge=lfs -text
|
| 210 |
dist/portable_rdock_pipeline-0.1.0-py3-none-any.whl filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 208 |
data/datasets/rdock_3PTB_50k_pubchem_local/ligands/ligand_metadata.csv filter=lfs diff=lfs merge=lfs -text
|
| 209 |
data/datasets/rdock_3PTB_50k_pubchem_local/target/rdock_prm/receptor.as filter=lfs diff=lfs merge=lfs -text
|
| 210 |
dist/portable_rdock_pipeline-0.1.0-py3-none-any.whl filter=lfs diff=lfs merge=lfs -text
|
| 211 |
+
portable_rdock_pipeline/data/examples/rdock_3ptb_pubchem_50k_collect_only/ligands/ligand_metadata.csv filter=lfs diff=lfs merge=lfs -text
|
| 212 |
+
results/benchmark_fixes/reference_ligand_baseline/target/receptor.as filter=lfs diff=lfs merge=lfs -text
|
| 213 |
+
results/benchmark_fixes/reference_sample/full_docking/poses/all_poses.sdf filter=lfs diff=lfs merge=lfs -text
|
| 214 |
+
results/benchmark_fixes/reference_sample/full_docking/target/receptor.as filter=lfs diff=lfs merge=lfs -text
|
| 215 |
+
results/benchmark_fixes/reference_sample/ligands/all_ligands.sdf filter=lfs diff=lfs merge=lfs -text
|
| 216 |
+
results/benchmark_fixes/reference_sample/target/rdock_prm/receptor.as filter=lfs diff=lfs merge=lfs -text
|
libs/adaptive/._state.py
ADDED
|
Binary file (4.1 kB). View file
|
|
|
libs/adaptive/._surrogate_model.py
ADDED
|
Binary file (4.1 kB). View file
|
|
|
libs/adaptive/._thresholding.py
ADDED
|
Binary file (4.1 kB). View file
|
|
|
libs/adaptive/._weight_schedule.py
ADDED
|
Binary file (4.1 kB). View file
|
|
|
libs/analysis/.___init__.py
ADDED
|
Binary file (4.1 kB). View file
|
|
|
libs/analysis/.___pycache__
ADDED
|
Binary file (4.1 kB). View file
|
|
|
libs/analysis/._backend_diagnostic.py
ADDED
|
Binary file (4.1 kB). View file
|
|
|
libs/analysis/._ranking.py
ADDED
|
Binary file (4.1 kB). View file
|
|
|
libs/benchmark/.___init__.py
ADDED
|
Binary file (4.1 kB). View file
|
|
|
libs/benchmark/.___pycache__
ADDED
|
Binary file (4.1 kB). View file
|
|
|
libs/benchmark/._budget_efficiency.py
ADDED
|
Binary file (4.1 kB). View file
|
|
|
libs/benchmark/._cleanup.py
ADDED
|
Binary file (4.1 kB). View file
|
|
|
libs/benchmark/._disk_guard.py
ADDED
|
Binary file (4.1 kB). View file
|
|
|
libs/benchmark/._large_library.py
ADDED
|
Binary file (4.1 kB). View file
|
|
|
libs/benchmark/._multifidelity.py
ADDED
|
Binary file (4.1 kB). View file
|
|
|
libs/benchmark/._ordering.py
ADDED
|
Binary file (4.1 kB). View file
|
|
|
libs/benchmark/._overnight_stop.py
ADDED
|
Binary file (4.1 kB). View file
|
|
|
libs/benchmark/._policy_repair.py
ADDED
|
Binary file (4.1 kB). View file
|
|
|
libs/benchmark/._redocking_validation.py
ADDED
|
Binary file (4.1 kB). View file
|
|
|
libs/benchmark/._reuse.py
ADDED
|
Binary file (4.1 kB). View file
|
|
|
libs/benchmark/._runtime.py
ADDED
|
Binary file (4.1 kB). View file
|
|
|
libs/docking/._.DS_Store
ADDED
|
Binary file (4.1 kB). View file
|
|
|
libs/docking/.___init__.py
ADDED
|
Binary file (4.1 kB). View file
|
|
|
libs/docking/.___pycache__
ADDED
|
Binary file (4.1 kB). View file
|
|
|
libs/docking/._backend_rdock.py
ADDED
|
Binary file (4.1 kB). View file
|
|
|
libs/docking/._backend_smina.py
ADDED
|
Binary file (4.1 kB). View file
|
|
|
libs/docking/._base.py
ADDED
|
Binary file (4.1 kB). View file
|
|
|
libs/docking/._comparison.py
ADDED
|
Binary file (4.1 kB). View file
|
|
|
libs/docking/._constraints.py
ADDED
|
Binary file (4.1 kB). View file
|
|
|
libs/docking/._interface_features.py
ADDED
|
Binary file (4.1 kB). View file
|
|
|
libs/docking/._plip_interactions.py
ADDED
|
Binary file (4.1 kB). View file
|
|
|
libs/docking/._pocket.py
ADDED
|
Binary file (4.1 kB). View file
|
|
|
libs/docking/._pose_parsing.py
ADDED
|
Binary file (4.1 kB). View file
|
|
|
libs/docking/._prep.py
ADDED
|
Binary file (4.1 kB). View file
|
|
|
libs/docking/._rdock_features.py
ADDED
|
Binary file (4.1 kB). View file
|
|
|
libs/docking/__init__.py
CHANGED
|
@@ -1,7 +1,6 @@
|
|
| 1 |
"""Docking backend abstractions and backend adapters."""
|
| 2 |
|
| 3 |
from .backend_rdock import RDockBackend
|
| 4 |
-
from .backend_smina import SminaBackend
|
| 5 |
from .base import BackendCapability, DockingBackend, DockingError, DockingResult
|
| 6 |
from .comparison import build_backend_comparison_table, semantic_compatibility_notes, summarize_backend_table
|
| 7 |
from .pocket import (
|
|
@@ -17,7 +16,6 @@ __all__ = [
|
|
| 17 |
"DockingError",
|
| 18 |
"DockingResult",
|
| 19 |
"RDockBackend",
|
| 20 |
-
"SminaBackend",
|
| 21 |
"PocketSpec",
|
| 22 |
"POCKET_MODE_USER_FIXED",
|
| 23 |
"POCKET_MODE_REFERENCE",
|
|
|
|
| 1 |
"""Docking backend abstractions and backend adapters."""
|
| 2 |
|
| 3 |
from .backend_rdock import RDockBackend
|
|
|
|
| 4 |
from .base import BackendCapability, DockingBackend, DockingError, DockingResult
|
| 5 |
from .comparison import build_backend_comparison_table, semantic_compatibility_notes, summarize_backend_table
|
| 6 |
from .pocket import (
|
|
|
|
| 16 |
"DockingError",
|
| 17 |
"DockingResult",
|
| 18 |
"RDockBackend",
|
|
|
|
| 19 |
"PocketSpec",
|
| 20 |
"POCKET_MODE_USER_FIXED",
|
| 21 |
"POCKET_MODE_REFERENCE",
|
libs/docking/backend_rdock.py
CHANGED
|
@@ -2,7 +2,9 @@ from __future__ import annotations
|
|
| 2 |
|
| 3 |
import json
|
| 4 |
import os
|
|
|
|
| 5 |
import shutil
|
|
|
|
| 6 |
import threading
|
| 7 |
from concurrent.futures import ThreadPoolExecutor
|
| 8 |
from dataclasses import dataclass
|
|
@@ -32,6 +34,7 @@ from .pocket import (
|
|
| 32 |
)
|
| 33 |
from .pose_parsing import parse_rdock_sdf
|
| 34 |
from .prep import prepare_ligand_sdf
|
|
|
|
| 35 |
|
| 36 |
|
| 37 |
@dataclass
|
|
@@ -47,6 +50,12 @@ class RDockConfig:
|
|
| 47 |
mapper_radius: float = 6.0
|
| 48 |
command_timeout_seconds: int = 180
|
| 49 |
parallel_jobs: int = 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
allow_partial_failures: bool = False
|
| 51 |
pocket_mode: str = POCKET_MODE_REFERENCE
|
| 52 |
pocket_center: tuple[float, float, float] | str | list[float] | None = None
|
|
@@ -143,6 +152,143 @@ class RDockBackend(DockingBackend):
|
|
| 143 |
]
|
| 144 |
return any(marker in text for marker in known_error_markers)
|
| 145 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
def check_capability(self) -> BackendCapability:
|
| 147 |
bins = {
|
| 148 |
"rbdock": shutil.which(self.config.rbdock_bin),
|
|
@@ -215,12 +361,35 @@ class RDockBackend(DockingBackend):
|
|
| 215 |
pocket.radius,
|
| 216 |
)
|
| 217 |
|
| 218 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 219 |
"target_prepared": prepared,
|
| 220 |
"receptor_mol2": receptor_mol2,
|
| 221 |
"target_work_dir": wd,
|
| 222 |
"pocket_spec_json": wd / "pocket_spec.json",
|
| 223 |
}
|
|
|
|
|
|
|
|
|
|
| 224 |
|
| 225 |
def prepare_ligand(self, ligand_id: str, smiles: str, work_dir: str | Path) -> Path:
|
| 226 |
wd = Path(work_dir)
|
|
@@ -331,7 +500,7 @@ class RDockBackend(DockingBackend):
|
|
| 331 |
and cached_cavity is not None
|
| 332 |
and cached_prm.exists()
|
| 333 |
and cached_cavity.exists()
|
| 334 |
-
and
|
| 335 |
):
|
| 336 |
prm_file = cached_prm
|
| 337 |
cavity_file = cached_cavity
|
|
@@ -354,7 +523,7 @@ class RDockBackend(DockingBackend):
|
|
| 354 |
)
|
| 355 |
|
| 356 |
cavity_file = site_dir / f"{prm_file.stem}.as"
|
| 357 |
-
cavity_ok =
|
| 358 |
if self._indicates_failure(cavity) or not cavity_ok:
|
| 359 |
msg = (
|
| 360 |
"rbcavity failed or did not produce cavity file. "
|
|
@@ -432,6 +601,53 @@ class RDockBackend(DockingBackend):
|
|
| 432 |
pose_centroid = centroid_from_sdf(pose_file)
|
| 433 |
pocket_distance = pose_distance_to_center(pose_centroid, pocket_spec) if pocket_spec is not None else float("nan")
|
| 434 |
pose_inside = is_pose_in_pocket(pose_centroid, pocket_spec) if pocket_spec is not None else False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 435 |
|
| 436 |
return DockingResult(
|
| 437 |
ligand_id=ligand_id,
|
|
@@ -445,21 +661,11 @@ class RDockBackend(DockingBackend):
|
|
| 445 |
fallback_used=False,
|
| 446 |
success=True,
|
| 447 |
command=" ".join(cmd),
|
| 448 |
-
extra=
|
| 449 |
-
"all_scores": parsed.get("all_scores", []),
|
| 450 |
-
"pose_count": parsed.get("pose_count"),
|
| 451 |
-
"rdock_native": parsed.get("native_features", {}),
|
| 452 |
-
"rdock_derived": parsed.get("derived_features", {}),
|
| 453 |
-
"rdock_feature_provenance": parsed.get("feature_provenance", []),
|
| 454 |
-
"pocket_spec": pocket_spec.to_dict() if pocket_spec is not None else {},
|
| 455 |
-
"pose_centroid": pose_centroid.tolist() if pose_centroid.size == 3 and np.isfinite(pose_centroid).all() else [],
|
| 456 |
-
"pose_distance_to_pocket_center": pocket_distance,
|
| 457 |
-
"pose_in_fixed_pocket": pose_inside,
|
| 458 |
-
},
|
| 459 |
)
|
| 460 |
|
| 461 |
results: list[DockingResult] = []
|
| 462 |
-
jobs =
|
| 463 |
if jobs <= 1 or len(ligand_files) <= 1:
|
| 464 |
for ligand_file in ligand_files:
|
| 465 |
results.append(_dock_single(ligand_file))
|
|
@@ -479,6 +685,10 @@ class RDockBackend(DockingBackend):
|
|
| 479 |
pocket = dict((result.extra or {}).get("pocket_spec", {}))
|
| 480 |
center = pocket.get("center", [nan, nan, nan])
|
| 481 |
box = pocket.get("box_size", [nan, nan, nan])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 482 |
parsed.append(
|
| 483 |
{
|
| 484 |
"ligand_id": result.ligand_id,
|
|
@@ -525,6 +735,23 @@ class RDockBackend(DockingBackend):
|
|
| 525 |
"pocket_box_size_z": float(box[2]) if isinstance(box, (list, tuple)) and len(box) == 3 else nan,
|
| 526 |
"pose_distance_to_pocket_center": float((result.extra or {}).get("pose_distance_to_pocket_center", nan)),
|
| 527 |
"pose_in_fixed_pocket": bool((result.extra or {}).get("pose_in_fixed_pocket", False)),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 528 |
}
|
| 529 |
)
|
| 530 |
return parsed
|
|
|
|
| 2 |
|
| 3 |
import json
|
| 4 |
import os
|
| 5 |
+
import re
|
| 6 |
import shutil
|
| 7 |
+
import subprocess
|
| 8 |
import threading
|
| 9 |
from concurrent.futures import ThreadPoolExecutor
|
| 10 |
from dataclasses import dataclass
|
|
|
|
| 34 |
)
|
| 35 |
from .pose_parsing import parse_rdock_sdf
|
| 36 |
from .prep import prepare_ligand_sdf
|
| 37 |
+
from .plip_interactions import analyze_pose_with_plip, plip_import_available
|
| 38 |
|
| 39 |
|
| 40 |
@dataclass
|
|
|
|
| 50 |
mapper_radius: float = 6.0
|
| 51 |
command_timeout_seconds: int = 180
|
| 52 |
parallel_jobs: int = 1
|
| 53 |
+
auto_batch_memory: bool = True
|
| 54 |
+
memory_safety_fraction: float = 0.85
|
| 55 |
+
min_memory_per_job_mb: int = 256
|
| 56 |
+
memory_probe_ligands: int = 2
|
| 57 |
+
enable_plip_interactions: bool = True
|
| 58 |
+
plip_timeout_seconds: int = 120
|
| 59 |
allow_partial_failures: bool = False
|
| 60 |
pocket_mode: str = POCKET_MODE_REFERENCE
|
| 61 |
pocket_center: tuple[float, float, float] | str | list[float] | None = None
|
|
|
|
| 152 |
]
|
| 153 |
return any(marker in text for marker in known_error_markers)
|
| 154 |
|
| 155 |
+
@staticmethod
|
| 156 |
+
def _available_memory_bytes() -> int | None:
|
| 157 |
+
try:
|
| 158 |
+
pages = os.sysconf("SC_AVPHYS_PAGES")
|
| 159 |
+
page_size = os.sysconf("SC_PAGE_SIZE")
|
| 160 |
+
value = int(pages) * int(page_size)
|
| 161 |
+
return value if value > 0 else None
|
| 162 |
+
except Exception:
|
| 163 |
+
pass
|
| 164 |
+
try:
|
| 165 |
+
proc = subprocess.run(["vm_stat"], check=False, capture_output=True, text=True, timeout=5)
|
| 166 |
+
if proc.returncode != 0:
|
| 167 |
+
return None
|
| 168 |
+
page_size = 4096
|
| 169 |
+
first = proc.stdout.splitlines()[0] if proc.stdout.splitlines() else ""
|
| 170 |
+
match = re.search(r"page size of (\d+) bytes", first)
|
| 171 |
+
if match:
|
| 172 |
+
page_size = int(match.group(1))
|
| 173 |
+
page_counts: dict[str, int] = {}
|
| 174 |
+
for line in proc.stdout.splitlines()[1:]:
|
| 175 |
+
if ":" not in line:
|
| 176 |
+
continue
|
| 177 |
+
key, value = line.split(":", 1)
|
| 178 |
+
text = value.strip().rstrip(".").replace(",", "")
|
| 179 |
+
try:
|
| 180 |
+
page_counts[key.strip()] = int(text)
|
| 181 |
+
except Exception:
|
| 182 |
+
continue
|
| 183 |
+
available_pages = (
|
| 184 |
+
page_counts.get("Pages free", 0)
|
| 185 |
+
+ page_counts.get("Pages inactive", 0)
|
| 186 |
+
+ page_counts.get("Pages speculative", 0)
|
| 187 |
+
+ page_counts.get("File-backed pages", 0)
|
| 188 |
+
)
|
| 189 |
+
value = int(available_pages) * int(page_size)
|
| 190 |
+
return value if value > 0 else None
|
| 191 |
+
except Exception:
|
| 192 |
+
return None
|
| 193 |
+
|
| 194 |
+
@staticmethod
|
| 195 |
+
def _cavity_volume_from_text(text: str) -> float | None:
|
| 196 |
+
for pattern in (r"total\s+volume\s*[:=]?\s*([0-9]+(?:\.[0-9]+)?)", r"volume\s*[:=]\s*([0-9]+(?:\.[0-9]+)?)"):
|
| 197 |
+
match = re.search(pattern, text, flags=re.IGNORECASE)
|
| 198 |
+
if match:
|
| 199 |
+
try:
|
| 200 |
+
return float(match.group(1))
|
| 201 |
+
except Exception:
|
| 202 |
+
return None
|
| 203 |
+
return None
|
| 204 |
+
|
| 205 |
+
def _cavity_artifact_usable(self, cavity_file: Path, log_path: Path | None = None) -> bool:
|
| 206 |
+
if not cavity_file.exists() or cavity_file.stat().st_size <= 0:
|
| 207 |
+
return False
|
| 208 |
+
if log_path is not None and log_path.exists():
|
| 209 |
+
volume = self._cavity_volume_from_text(log_path.read_text(encoding="utf-8", errors="ignore"))
|
| 210 |
+
if volume is not None and volume <= 0.0:
|
| 211 |
+
return False
|
| 212 |
+
return True
|
| 213 |
+
|
| 214 |
+
def _estimate_memory_bytes_per_job(self, ligand_files: Sequence[Path]) -> int:
|
| 215 |
+
sample_n = max(1, int(self.config.memory_probe_ligands))
|
| 216 |
+
sample = list(ligand_files[:sample_n])
|
| 217 |
+
sizes = [path.stat().st_size for path in sample if path.exists()]
|
| 218 |
+
avg_size = float(sum(sizes) / len(sizes)) if sizes else 0.0
|
| 219 |
+
static_estimate = int(avg_size * 80.0 * max(1, int(self.config.n_runs)))
|
| 220 |
+
floor_estimate = int(max(1, int(self.config.min_memory_per_job_mb)) * 1024 * 1024)
|
| 221 |
+
return max(static_estimate, floor_estimate)
|
| 222 |
+
|
| 223 |
+
def _resolve_memory_limited_jobs(self, ligand_files: Sequence[Path], work_dir: Path) -> int:
|
| 224 |
+
configured_jobs = max(1, int(self.config.parallel_jobs))
|
| 225 |
+
requested_jobs = min(configured_jobs, max(1, len(ligand_files)))
|
| 226 |
+
available = self._available_memory_bytes()
|
| 227 |
+
per_job = self._estimate_memory_bytes_per_job(ligand_files)
|
| 228 |
+
memory_jobs = requested_jobs
|
| 229 |
+
if self.config.auto_batch_memory and available is not None and per_job > 0:
|
| 230 |
+
usable = max(1, int(float(available) * max(0.1, min(1.0, float(self.config.memory_safety_fraction)))))
|
| 231 |
+
memory_jobs = max(1, usable // per_job)
|
| 232 |
+
resolved = max(1, min(requested_jobs, memory_jobs))
|
| 233 |
+
plan = {
|
| 234 |
+
"parallel_jobs_configured": configured_jobs,
|
| 235 |
+
"parallel_jobs_requested_for_batch": requested_jobs,
|
| 236 |
+
"parallel_jobs_resolved": resolved,
|
| 237 |
+
"ligand_count": len(ligand_files),
|
| 238 |
+
"sampled_ligand_count": min(len(ligand_files), max(1, int(self.config.memory_probe_ligands))),
|
| 239 |
+
"available_memory_bytes": available,
|
| 240 |
+
"estimated_memory_bytes_per_job": per_job,
|
| 241 |
+
"memory_safety_fraction": float(self.config.memory_safety_fraction),
|
| 242 |
+
"auto_batch_memory": bool(self.config.auto_batch_memory),
|
| 243 |
+
"batching_policy": "memory_limited_parallel_jobs",
|
| 244 |
+
}
|
| 245 |
+
(work_dir / "memory_batch_plan.json").write_text(json.dumps(plan, indent=2), encoding="utf-8")
|
| 246 |
+
return resolved
|
| 247 |
+
|
| 248 |
+
@staticmethod
|
| 249 |
+
def _post_docking_confidence(extra: dict[str, object], score: float, success: bool) -> float:
|
| 250 |
+
if not success or not np.isfinite(score):
|
| 251 |
+
return 0.0
|
| 252 |
+
native = dict(extra.get("rdock_native", {}))
|
| 253 |
+
pose_count = float(native.get("n_generated_poses", extra.get("pose_count", 0)) or 0)
|
| 254 |
+
std_top5 = native.get("std_top5_pose_score", np.nan)
|
| 255 |
+
gap = native.get("pose_score_gap_1_2", np.nan)
|
| 256 |
+
pose_inside = bool(extra.get("pose_in_fixed_pocket", False))
|
| 257 |
+
pose_term = min(1.0, pose_count / 5.0)
|
| 258 |
+
variance_term = 0.5
|
| 259 |
+
try:
|
| 260 |
+
std_value = abs(float(std_top5))
|
| 261 |
+
if np.isfinite(std_value):
|
| 262 |
+
variance_term = 1.0 / (1.0 + std_value)
|
| 263 |
+
except Exception:
|
| 264 |
+
pass
|
| 265 |
+
gap_term = 0.5
|
| 266 |
+
try:
|
| 267 |
+
gap_value = abs(float(gap))
|
| 268 |
+
if np.isfinite(gap_value):
|
| 269 |
+
gap_term = min(1.0, gap_value / 2.0)
|
| 270 |
+
except Exception:
|
| 271 |
+
pass
|
| 272 |
+
pocket_term = 1.0 if pose_inside else 0.25
|
| 273 |
+
return float(max(0.0, min(1.0, 0.25 * pose_term + 0.25 * variance_term + 0.20 * gap_term + 0.30 * pocket_term)))
|
| 274 |
+
|
| 275 |
+
@staticmethod
|
| 276 |
+
def _biological_interaction_proxy(native: dict[str, object], derived: dict[str, object], pose_inside: bool) -> float:
|
| 277 |
+
def _finite(value: object, default: float = 0.0) -> float:
|
| 278 |
+
try:
|
| 279 |
+
out = float(value)
|
| 280 |
+
except Exception:
|
| 281 |
+
return default
|
| 282 |
+
return out if np.isfinite(out) else default
|
| 283 |
+
|
| 284 |
+
polar = abs(_finite(native.get("rdock_polar_term"), 0.0))
|
| 285 |
+
vdw = abs(_finite(native.get("rdock_vdw_term"), 0.0))
|
| 286 |
+
overlap = max(0.0, min(1.0, _finite(derived.get("contact_overlap_consistency"), 0.0)))
|
| 287 |
+
hotspot = max(0.0, min(1.0, _finite(derived.get("hotspot_contact_frequency"), 0.0)))
|
| 288 |
+
energetic = max(0.0, min(1.0, (0.04 * polar) + (0.01 * vdw)))
|
| 289 |
+
pocket_bonus = 0.15 if pose_inside else 0.0
|
| 290 |
+
return float(max(0.0, min(1.0, 0.45 * energetic + 0.20 * overlap + 0.20 * hotspot + pocket_bonus)))
|
| 291 |
+
|
| 292 |
def check_capability(self) -> BackendCapability:
|
| 293 |
bins = {
|
| 294 |
"rbdock": shutil.which(self.config.rbdock_bin),
|
|
|
|
| 361 |
pocket.radius,
|
| 362 |
)
|
| 363 |
|
| 364 |
+
plip_receptor_pdb: Path | None = wd / "receptor_plip.pdb"
|
| 365 |
+
if suffix == ".pdb":
|
| 366 |
+
plip_receptor_pdb.write_bytes(prepared.read_bytes())
|
| 367 |
+
else:
|
| 368 |
+
obabel = shutil.which("obabel")
|
| 369 |
+
if obabel is not None:
|
| 370 |
+
try:
|
| 371 |
+
result = run_command([obabel, str(receptor_mol2), "-O", str(plip_receptor_pdb)], cwd=wd, timeout=120)
|
| 372 |
+
self._record_command(
|
| 373 |
+
stage="prepare_target_plip_pdb",
|
| 374 |
+
result=result,
|
| 375 |
+
cwd=wd,
|
| 376 |
+
stdout_log=wd / "prepare_target_plip_pdb.stdout.log",
|
| 377 |
+
stderr_log=wd / "prepare_target_plip_pdb.stderr.log",
|
| 378 |
+
)
|
| 379 |
+
if result.returncode != 0 or not plip_receptor_pdb.exists() or plip_receptor_pdb.stat().st_size == 0:
|
| 380 |
+
plip_receptor_pdb = prepared if prepared.suffix.lower() == ".pdb" else None
|
| 381 |
+
except Exception:
|
| 382 |
+
plip_receptor_pdb = prepared if prepared.suffix.lower() == ".pdb" else None
|
| 383 |
+
|
| 384 |
+
context = {
|
| 385 |
"target_prepared": prepared,
|
| 386 |
"receptor_mol2": receptor_mol2,
|
| 387 |
"target_work_dir": wd,
|
| 388 |
"pocket_spec_json": wd / "pocket_spec.json",
|
| 389 |
}
|
| 390 |
+
if plip_receptor_pdb is not None:
|
| 391 |
+
context["plip_receptor_pdb"] = plip_receptor_pdb
|
| 392 |
+
return context
|
| 393 |
|
| 394 |
def prepare_ligand(self, ligand_id: str, smiles: str, work_dir: str | Path) -> Path:
|
| 395 |
wd = Path(work_dir)
|
|
|
|
| 500 |
and cached_cavity is not None
|
| 501 |
and cached_prm.exists()
|
| 502 |
and cached_cavity.exists()
|
| 503 |
+
and self._cavity_artifact_usable(cached_cavity, site_dir / "rbcavity.stdout.log")
|
| 504 |
):
|
| 505 |
prm_file = cached_prm
|
| 506 |
cavity_file = cached_cavity
|
|
|
|
| 523 |
)
|
| 524 |
|
| 525 |
cavity_file = site_dir / f"{prm_file.stem}.as"
|
| 526 |
+
cavity_ok = self._cavity_artifact_usable(cavity_file, site_dir / "rbcavity.stdout.log")
|
| 527 |
if self._indicates_failure(cavity) or not cavity_ok:
|
| 528 |
msg = (
|
| 529 |
"rbcavity failed or did not produce cavity file. "
|
|
|
|
| 601 |
pose_centroid = centroid_from_sdf(pose_file)
|
| 602 |
pocket_distance = pose_distance_to_center(pose_centroid, pocket_spec) if pocket_spec is not None else float("nan")
|
| 603 |
pose_inside = is_pose_in_pocket(pose_centroid, pocket_spec) if pocket_spec is not None else False
|
| 604 |
+
native_features = dict(parsed.get("native_features", {}))
|
| 605 |
+
derived_features = dict(parsed.get("derived_features", {}))
|
| 606 |
+
fallback_interaction_proxy = self._biological_interaction_proxy(native_features, derived_features, pose_inside)
|
| 607 |
+
plip_summary = None
|
| 608 |
+
plip_receptor = target_context.get("plip_receptor_pdb")
|
| 609 |
+
if (
|
| 610 |
+
self.config.enable_plip_interactions
|
| 611 |
+
and plip_receptor is not None
|
| 612 |
+
and Path(plip_receptor).exists()
|
| 613 |
+
and plip_import_available()
|
| 614 |
+
):
|
| 615 |
+
plip_summary = analyze_pose_with_plip(
|
| 616 |
+
receptor_pdb=Path(plip_receptor),
|
| 617 |
+
pose_sdf=pose_file,
|
| 618 |
+
ligand_id=ligand_id,
|
| 619 |
+
work_dir=wd / "plip",
|
| 620 |
+
timeout_seconds=int(self.config.plip_timeout_seconds),
|
| 621 |
+
)
|
| 622 |
+
plip_payload = plip_summary.to_dict() if plip_summary is not None else {
|
| 623 |
+
"available": plip_import_available(),
|
| 624 |
+
"success": False,
|
| 625 |
+
"source": "plip",
|
| 626 |
+
"interaction_score": 0.0,
|
| 627 |
+
"interaction_count": 0,
|
| 628 |
+
"message": "PLIP disabled or receptor PDB unavailable",
|
| 629 |
+
}
|
| 630 |
+
interaction_score = (
|
| 631 |
+
float(plip_payload.get("interaction_score", 0.0))
|
| 632 |
+
if bool(plip_payload.get("success", False))
|
| 633 |
+
else fallback_interaction_proxy
|
| 634 |
+
)
|
| 635 |
+
interaction_source = "plip" if bool(plip_payload.get("success", False)) else "rdock_energy_pose_proxy"
|
| 636 |
+
extra = {
|
| 637 |
+
"all_scores": parsed.get("all_scores", []),
|
| 638 |
+
"pose_count": parsed.get("pose_count"),
|
| 639 |
+
"rdock_native": native_features,
|
| 640 |
+
"rdock_derived": derived_features,
|
| 641 |
+
"rdock_feature_provenance": parsed.get("feature_provenance", []),
|
| 642 |
+
"pocket_spec": pocket_spec.to_dict() if pocket_spec is not None else {},
|
| 643 |
+
"pose_centroid": pose_centroid.tolist() if pose_centroid.size == 3 and np.isfinite(pose_centroid).all() else [],
|
| 644 |
+
"pose_distance_to_pocket_center": pocket_distance,
|
| 645 |
+
"pose_in_fixed_pocket": pose_inside,
|
| 646 |
+
"plip_interactions": plip_payload,
|
| 647 |
+
"biological_interaction_proxy_score": interaction_score,
|
| 648 |
+
"biological_interaction_source": interaction_source,
|
| 649 |
+
}
|
| 650 |
+
extra["post_docking_confidence_score"] = self._post_docking_confidence(extra, float(parsed["score"]), True)
|
| 651 |
|
| 652 |
return DockingResult(
|
| 653 |
ligand_id=ligand_id,
|
|
|
|
| 661 |
fallback_used=False,
|
| 662 |
success=True,
|
| 663 |
command=" ".join(cmd),
|
| 664 |
+
extra=extra,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 665 |
)
|
| 666 |
|
| 667 |
results: list[DockingResult] = []
|
| 668 |
+
jobs = self._resolve_memory_limited_jobs(ligand_files, wd)
|
| 669 |
if jobs <= 1 or len(ligand_files) <= 1:
|
| 670 |
for ligand_file in ligand_files:
|
| 671 |
results.append(_dock_single(ligand_file))
|
|
|
|
| 685 |
pocket = dict((result.extra or {}).get("pocket_spec", {}))
|
| 686 |
center = pocket.get("center", [nan, nan, nan])
|
| 687 |
box = pocket.get("box_size", [nan, nan, nan])
|
| 688 |
+
confidence = float((result.extra or {}).get("post_docking_confidence_score", 0.0))
|
| 689 |
+
interaction_proxy = float((result.extra or {}).get("biological_interaction_proxy_score", 0.0))
|
| 690 |
+
interaction_weighted_score = float(result.docking_score) - (0.5 * interaction_proxy) if result.success else nan
|
| 691 |
+
plip_payload = dict((result.extra or {}).get("plip_interactions", {}))
|
| 692 |
parsed.append(
|
| 693 |
{
|
| 694 |
"ligand_id": result.ligand_id,
|
|
|
|
| 735 |
"pocket_box_size_z": float(box[2]) if isinstance(box, (list, tuple)) and len(box) == 3 else nan,
|
| 736 |
"pose_distance_to_pocket_center": float((result.extra or {}).get("pose_distance_to_pocket_center", nan)),
|
| 737 |
"pose_in_fixed_pocket": bool((result.extra or {}).get("pose_in_fixed_pocket", False)),
|
| 738 |
+
"post_docking_confidence_score": confidence,
|
| 739 |
+
"biological_interaction_proxy_score": interaction_proxy,
|
| 740 |
+
"interaction_weighted_docking_score": interaction_weighted_score,
|
| 741 |
+
"interaction_filter_pass": bool(interaction_proxy > 0.0),
|
| 742 |
+
"interaction_feature_source": str((result.extra or {}).get("biological_interaction_source", "")),
|
| 743 |
+
"plip_available": bool(plip_payload.get("available", False)),
|
| 744 |
+
"plip_success": bool(plip_payload.get("success", False)),
|
| 745 |
+
"plip_interaction_count": int(plip_payload.get("interaction_count", 0) or 0),
|
| 746 |
+
"plip_hydrophobic_count": int(plip_payload.get("hydrophobic_count", 0) or 0),
|
| 747 |
+
"plip_hbond_count": int(plip_payload.get("hbond_count", 0) or 0),
|
| 748 |
+
"plip_saltbridge_count": int(plip_payload.get("saltbridge_count", 0) or 0),
|
| 749 |
+
"plip_pistacking_count": int(plip_payload.get("pistacking_count", 0) or 0),
|
| 750 |
+
"plip_pication_count": int(plip_payload.get("pication_count", 0) or 0),
|
| 751 |
+
"plip_halogen_count": int(plip_payload.get("halogen_count", 0) or 0),
|
| 752 |
+
"plip_waterbridge_count": int(plip_payload.get("waterbridge_count", 0) or 0),
|
| 753 |
+
"plip_metal_count": int(plip_payload.get("metal_count", 0) or 0),
|
| 754 |
+
"plip_message": str(plip_payload.get("message", "")),
|
| 755 |
}
|
| 756 |
)
|
| 757 |
return parsed
|
libs/docking/plip_interactions.py
ADDED
|
@@ -0,0 +1,194 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
import json
|
| 4 |
+
import shutil
|
| 5 |
+
from dataclasses import dataclass, asdict
|
| 6 |
+
from pathlib import Path
|
| 7 |
+
from subprocess import TimeoutExpired
|
| 8 |
+
from typing import Any
|
| 9 |
+
|
| 10 |
+
from libs.utils.subprocess_utils import run_command
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
@dataclass(frozen=True)
|
| 14 |
+
class PLIPInteractionSummary:
|
| 15 |
+
available: bool
|
| 16 |
+
success: bool
|
| 17 |
+
source: str
|
| 18 |
+
interaction_score: float
|
| 19 |
+
interaction_count: int
|
| 20 |
+
hydrophobic_count: int = 0
|
| 21 |
+
hbond_count: int = 0
|
| 22 |
+
saltbridge_count: int = 0
|
| 23 |
+
pistacking_count: int = 0
|
| 24 |
+
pication_count: int = 0
|
| 25 |
+
halogen_count: int = 0
|
| 26 |
+
waterbridge_count: int = 0
|
| 27 |
+
metal_count: int = 0
|
| 28 |
+
ligand_key: str = ""
|
| 29 |
+
complex_pdb: str = ""
|
| 30 |
+
message: str = ""
|
| 31 |
+
|
| 32 |
+
def to_dict(self) -> dict[str, Any]:
|
| 33 |
+
return asdict(self)
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def plip_import_available() -> bool:
|
| 37 |
+
try:
|
| 38 |
+
from plip.structure.preparation import PDBComplex # noqa: F401
|
| 39 |
+
|
| 40 |
+
return True
|
| 41 |
+
except Exception:
|
| 42 |
+
return False
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
def _failed(message: str, *, available: bool | None = None) -> PLIPInteractionSummary:
|
| 46 |
+
return PLIPInteractionSummary(
|
| 47 |
+
available=plip_import_available() if available is None else bool(available),
|
| 48 |
+
success=False,
|
| 49 |
+
source="plip",
|
| 50 |
+
interaction_score=0.0,
|
| 51 |
+
interaction_count=0,
|
| 52 |
+
message=message,
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
def _rewrite_ligand_pdb_lines(text: str) -> list[str]:
|
| 57 |
+
out: list[str] = []
|
| 58 |
+
atom_serial = 9000
|
| 59 |
+
for raw in text.splitlines():
|
| 60 |
+
if not raw.startswith(("ATOM", "HETATM")):
|
| 61 |
+
continue
|
| 62 |
+
line = raw.ljust(80)
|
| 63 |
+
atom_serial += 1
|
| 64 |
+
rewritten = (
|
| 65 |
+
"HETATM"
|
| 66 |
+
+ f"{atom_serial:5d}"
|
| 67 |
+
+ line[11:17]
|
| 68 |
+
+ "LIG"
|
| 69 |
+
+ " Z"
|
| 70 |
+
+ f"{1:4d}"
|
| 71 |
+
+ line[26:]
|
| 72 |
+
)
|
| 73 |
+
out.append(rewritten[:80])
|
| 74 |
+
return out
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
def _receptor_pdb_lines(receptor_pdb: Path) -> list[str]:
|
| 78 |
+
lines: list[str] = []
|
| 79 |
+
for raw in receptor_pdb.read_text(encoding="utf-8", errors="ignore").splitlines():
|
| 80 |
+
if raw.startswith(("ATOM", "TER")):
|
| 81 |
+
lines.append(raw[:80])
|
| 82 |
+
return lines
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
def _pose_sdf_to_pdb(pose_sdf: Path, out_pdb: Path, *, timeout_seconds: int) -> Path:
|
| 86 |
+
obabel = shutil.which("obabel")
|
| 87 |
+
if obabel is None:
|
| 88 |
+
raise RuntimeError("obabel executable is required for PLIP ligand conversion")
|
| 89 |
+
result = run_command([obabel, str(pose_sdf), "-O", str(out_pdb), "-f", "1", "-l", "1"], cwd=out_pdb.parent, timeout=timeout_seconds)
|
| 90 |
+
if result.returncode != 0 or not out_pdb.exists() or out_pdb.stat().st_size == 0:
|
| 91 |
+
raise RuntimeError(f"obabel SDF->PDB conversion failed: rc={result.returncode}, stderr={result.stderr.strip()}")
|
| 92 |
+
return out_pdb
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
def _build_complex_pdb(receptor_pdb: Path, ligand_pdb: Path, complex_pdb: Path) -> Path:
|
| 96 |
+
receptor_lines = _receptor_pdb_lines(receptor_pdb)
|
| 97 |
+
ligand_lines = _rewrite_ligand_pdb_lines(ligand_pdb.read_text(encoding="utf-8", errors="ignore"))
|
| 98 |
+
if not receptor_lines:
|
| 99 |
+
raise RuntimeError(f"PLIP receptor PDB has no ATOM/HETATM records: {receptor_pdb}")
|
| 100 |
+
if not ligand_lines:
|
| 101 |
+
raise RuntimeError(f"PLIP ligand PDB has no atom records: {ligand_pdb}")
|
| 102 |
+
complex_pdb.write_text("\n".join(receptor_lines + ["TER"] + ligand_lines + ["END", ""]) , encoding="utf-8")
|
| 103 |
+
return complex_pdb
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
def _count_attr(interaction_set: Any, attr: str) -> int:
|
| 107 |
+
return len(getattr(interaction_set, attr, []) or [])
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
def _score_counts(
|
| 111 |
+
*,
|
| 112 |
+
hydrophobic_count: int,
|
| 113 |
+
hbond_count: int,
|
| 114 |
+
saltbridge_count: int,
|
| 115 |
+
pistacking_count: int,
|
| 116 |
+
pication_count: int,
|
| 117 |
+
halogen_count: int,
|
| 118 |
+
waterbridge_count: int,
|
| 119 |
+
metal_count: int,
|
| 120 |
+
) -> float:
|
| 121 |
+
raw = (
|
| 122 |
+
0.05 * hydrophobic_count
|
| 123 |
+
+ 0.14 * hbond_count
|
| 124 |
+
+ 0.16 * saltbridge_count
|
| 125 |
+
+ 0.12 * pistacking_count
|
| 126 |
+
+ 0.12 * pication_count
|
| 127 |
+
+ 0.10 * halogen_count
|
| 128 |
+
+ 0.06 * waterbridge_count
|
| 129 |
+
+ 0.20 * metal_count
|
| 130 |
+
)
|
| 131 |
+
return max(0.0, min(1.0, float(raw)))
|
| 132 |
+
|
| 133 |
+
|
| 134 |
+
def analyze_pose_with_plip(
|
| 135 |
+
*,
|
| 136 |
+
receptor_pdb: Path,
|
| 137 |
+
pose_sdf: Path,
|
| 138 |
+
ligand_id: str,
|
| 139 |
+
work_dir: Path,
|
| 140 |
+
timeout_seconds: int = 120,
|
| 141 |
+
) -> PLIPInteractionSummary:
|
| 142 |
+
if not plip_import_available():
|
| 143 |
+
return _failed("PLIP Python package is not importable", available=False)
|
| 144 |
+
try:
|
| 145 |
+
from plip.structure.preparation import PDBComplex
|
| 146 |
+
except Exception as exc: # pragma: no cover
|
| 147 |
+
return _failed(f"PLIP import failed: {exc}", available=False)
|
| 148 |
+
|
| 149 |
+
try:
|
| 150 |
+
work_dir.mkdir(parents=True, exist_ok=True)
|
| 151 |
+
ligand_pdb = _pose_sdf_to_pdb(pose_sdf, work_dir / f"{ligand_id}_plip_ligand.pdb", timeout_seconds=timeout_seconds)
|
| 152 |
+
complex_pdb = _build_complex_pdb(receptor_pdb, ligand_pdb, work_dir / f"{ligand_id}_plip_complex.pdb")
|
| 153 |
+
complex_obj = PDBComplex()
|
| 154 |
+
complex_obj.load_pdb(str(complex_pdb))
|
| 155 |
+
ligand = next((item for item in complex_obj.ligands if str(getattr(item, "hetid", "")).strip() == "LIG"), None)
|
| 156 |
+
if ligand is None and complex_obj.ligands:
|
| 157 |
+
ligand = complex_obj.ligands[0]
|
| 158 |
+
if ligand is None:
|
| 159 |
+
return _failed(f"PLIP found no ligand in generated complex {complex_pdb}", available=True)
|
| 160 |
+
complex_obj.characterize_complex(ligand)
|
| 161 |
+
interaction_set = next(iter(complex_obj.interaction_sets.values()), None)
|
| 162 |
+
if interaction_set is None:
|
| 163 |
+
return _failed(f"PLIP produced no interaction set for {ligand_id}", available=True)
|
| 164 |
+
|
| 165 |
+
hbond_count = _count_attr(interaction_set, "hbonds_ldon") + _count_attr(interaction_set, "hbonds_pdon")
|
| 166 |
+
saltbridge_count = _count_attr(interaction_set, "saltbridges_lneg") + _count_attr(interaction_set, "saltbridges_pneg")
|
| 167 |
+
pication_count = _count_attr(interaction_set, "pication_laro") + _count_attr(interaction_set, "pication_paro")
|
| 168 |
+
counts = {
|
| 169 |
+
"hydrophobic_count": _count_attr(interaction_set, "hydrophobic_contacts"),
|
| 170 |
+
"hbond_count": hbond_count,
|
| 171 |
+
"saltbridge_count": saltbridge_count,
|
| 172 |
+
"pistacking_count": _count_attr(interaction_set, "pistacking"),
|
| 173 |
+
"pication_count": pication_count,
|
| 174 |
+
"halogen_count": _count_attr(interaction_set, "halogen_bonds"),
|
| 175 |
+
"waterbridge_count": _count_attr(interaction_set, "water_bridges"),
|
| 176 |
+
"metal_count": _count_attr(interaction_set, "metal_complexes"),
|
| 177 |
+
}
|
| 178 |
+
interaction_count = int(sum(counts.values()))
|
| 179 |
+
summary = PLIPInteractionSummary(
|
| 180 |
+
available=True,
|
| 181 |
+
success=True,
|
| 182 |
+
source="plip",
|
| 183 |
+
interaction_score=_score_counts(**counts),
|
| 184 |
+
interaction_count=interaction_count,
|
| 185 |
+
ligand_key=str(next(iter(complex_obj.interaction_sets.keys()), "")),
|
| 186 |
+
complex_pdb=str(complex_pdb),
|
| 187 |
+
**counts,
|
| 188 |
+
)
|
| 189 |
+
(work_dir / f"{ligand_id}_plip_summary.json").write_text(json.dumps(summary.to_dict(), indent=2), encoding="utf-8")
|
| 190 |
+
return summary
|
| 191 |
+
except TimeoutExpired as exc:
|
| 192 |
+
return _failed(f"PLIP ligand conversion timed out: {exc}", available=True)
|
| 193 |
+
except Exception as exc:
|
| 194 |
+
return _failed(f"PLIP analysis failed: {exc}", available=True)
|
libs/encoders/.___init__.py
ADDED
|
Binary file (4.1 kB). View file
|
|
|
libs/encoders/.___pycache__
ADDED
|
Binary file (4.1 kB). View file
|
|
|
libs/encoders/._graph_builders.py
ADDED
|
Binary file (4.1 kB). View file
|
|
|
libs/encoders/._ligand_encoder.py
ADDED
|
Binary file (4.1 kB). View file
|
|
|
libs/encoders/._protein_encoder.py
ADDED
|
Binary file (4.1 kB). View file
|
|
|
libs/encoders/._schemas.py
ADDED
|
Binary file (4.1 kB). View file
|
|
|
libs/encoders/._sequence_features.py
ADDED
|
Binary file (4.1 kB). View file
|
|
|
libs/encoders/._structure_features.py
ADDED
|
Binary file (4.1 kB). View file
|
|
|
libs/modalities/.___init__.py
ADDED
|
Binary file (4.1 kB). View file
|
|
|
libs/modalities/.___pycache__
ADDED
|
Binary file (4.1 kB). View file
|
|
|
libs/modalities/._peptide_like.py
ADDED
|
Binary file (4.1 kB). View file
|
|
|