dualmapref 0.2.8__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- args_parse.py +593 -0
- candidate_corrector.py +161 -0
- candidate_scorer.py +359 -0
- conformend/__init__.py +36 -0
- conformend/refiner.py +25 -0
- dataset/__init__.py +1 -0
- dataset/drugdataset.py +644 -0
- dataset/manifold.py +237 -0
- dataset/sharded_drugdataset.py +124 -0
- dataset/smiles2graph.py +217 -0
- dualmapref/__init__.py +24 -0
- dualmapref/refiner.py +16 -0
- dualmapref-0.2.8.dist-info/LICENSE +21 -0
- dualmapref-0.2.8.dist-info/METADATA +167 -0
- dualmapref-0.2.8.dist-info/RECORD +186 -0
- dualmapref-0.2.8.dist-info/WHEEL +5 -0
- dualmapref-0.2.8.dist-info/entry_points.txt +10 -0
- dualmapref-0.2.8.dist-info/top_level.txt +29 -0
- exputils.py +289 -0
- infer.py +23 -0
- infer_base_conformers.py +5589 -0
- infer_mean_dispersion_torsion.py +1117 -0
- infer_torsion.py +752 -0
- infer_torsional_denoiser.py +1021 -0
- manimol/__init__.py +30 -0
- manimol/__main__.py +6 -0
- manimol/cli.py +36 -0
- manimol/full_cli.py +41 -0
- manimol/legacy_imports.py +43 -0
- manimol/manifest.py +56 -0
- manimol/postprocess.py +148 -0
- manimol/priors.py +60 -0
- manimol/protocols.py +119 -0
- manimol/refiner.py +645 -0
- manimol/runtime.py +194 -0
- manimol/selection.py +85 -0
- manimol/torsion.py +22 -0
- manimol/train_cli.py +14 -0
- mixture_flow/src/__init__.py +1 -0
- mixture_flow/src/_path.py +7 -0
- mixture_flow/src/infer28_mixture_torsion_flow.py +1093 -0
- mixture_flow/src/mixture_torsion_flow.py +291 -0
- mixture_flow/src/summarize_mixture_debug.py +80 -0
- mixture_flow/src/train28_mixture_torsion_flow.py +583 -0
- mixture_torsion_flow.py +7 -0
- models/ manifold_learning.py +86 -0
- models/__init__.py +8 -0
- models/dist.py +273 -0
- models/dist2coords.py +23 -0
- models/egnn.py +107 -0
- models/experimental_backbones.py +364 -0
- models/gnnconv.py +512 -0
- models/kernel_inversion.py +141 -0
- models/kernels.py +427 -0
- models/losses.py +154 -0
- models/mean_dispersion_bridge.py +119 -0
- models/mean_dispersion_torsion_context.py +155 -0
- models/model.py +73 -0
- models/model0.py +2375 -0
- models/vis.py +128 -0
- pgraph_controls.py +107 -0
- scripts/__init__.py +2 -0
- scripts/analyze_basin_alignment.py +63 -0
- scripts/analyze_hif2a_conformer_aggregation.py +267 -0
- scripts/analyze_hif2a_contact_pharmacophore.py +1111 -0
- scripts/analyze_hif2a_fixed_budget.py +202 -0
- scripts/analyze_hif2a_pharmacophore.py +405 -0
- scripts/analyze_hif2a_pharmacophore_match_metric.py +386 -0
- scripts/analyze_p_control_candidate_flow.py +154 -0
- scripts/analyze_scaffold_basin.py +83 -0
- scripts/audit_dispersion_proposals.py +208 -0
- scripts/audit_etflow_protocol.py +258 -0
- scripts/audit_external_overlap.py +150 -0
- scripts/audit_oldbest_fulltrainval_split.py +82 -0
- scripts/audit_pdbbind_contact_pharmacophores.py +146 -0
- scripts/audit_qm9_geomol_test_split.py +94 -0
- scripts/audit_stage1_stage2_cross.py +131 -0
- scripts/build_geom_drugs_lowref_diagnostic.py +84 -0
- scripts/build_qm9_backfill_manifest.py +99 -0
- scripts/check_etflow_runtime.py +56 -0
- scripts/compare_prior_sources.py +122 -0
- scripts/compose_binding_mode_figure.py +87 -0
- scripts/compose_hif2a_contact_pharmacophore_suppfig.py +255 -0
- scripts/compose_hif2a_functional_geometry_pipeline.py +266 -0
- scripts/compose_manimol_internal_architecture.py +291 -0
- scripts/diagnose_flow_basin_intervention.py +54 -0
- scripts/diagnose_hif2a_pharmacophore_budget.py +185 -0
- scripts/diagnose_route2_p_ranking.py +123 -0
- scripts/discover_binding_mode_inputs.py +30 -0
- scripts/evaluate_functional_prior_stage1.py +230 -0
- scripts/evaluate_geomanimol.py +74 -0
- scripts/evaluate_geomanimol_flow.py +23 -0
- scripts/evaluate_geomanimol_multiscaffold.py +46 -0
- scripts/evaluate_geomanimol_residual.py +45 -0
- scripts/evaluate_geomanimol_torsion.py +40 -0
- scripts/evaluate_hif2a_bioactive_enrichment.py +91 -0
- scripts/evaluate_hif2a_pms_suite.py +440 -0
- scripts/evaluate_pdbbind_contact_pms.py +155 -0
- scripts/evaluate_platinum_external.py +114 -0
- scripts/evaluate_platinum_pool.py +82 -0
- scripts/evaluate_route2_calibrated_p.py +104 -0
- scripts/evaluate_route2_library_components.py +596 -0
- scripts/evaluate_route2_p_basin_budget.py +288 -0
- scripts/evaluate_stage1_checkpoint_only.py +30 -0
- scripts/export_dispersion_bond_audit.py +136 -0
- scripts/export_top20_shards.py +207 -0
- scripts/external_validation_metrics.py +152 -0
- scripts/fit_topology_prior.py +117 -0
- scripts/frozen_dispersion_audit.py +281 -0
- scripts/geomanimol_debug.py +50 -0
- scripts/geomanimol_smoke.py +37 -0
- scripts/infer_etflow_matched.py +524 -0
- scripts/inspect_checkpoint.py +12 -0
- scripts/inspect_conformer_capacity.py +71 -0
- scripts/inspect_pool.py +12 -0
- scripts/make_figure3_selection_loss.py +474 -0
- scripts/make_figure3_selection_summary.py +275 -0
- scripts/make_geom_debug_subset.py +62 -0
- scripts/make_low_reference_test_subset.py +70 -0
- scripts/merge_stage1_into_stage2.py +82 -0
- scripts/place_hif2a_candidates_for_evaluation.py +66 -0
- scripts/plot_hif2a_low_budget_efficiency.py +100 -0
- scripts/plot_prior_validation.py +48 -0
- scripts/precompute_stage2_assignments.py +90 -0
- scripts/prepare_bace_activity.py +98 -0
- scripts/prepare_bace_etkdg_benchmark.py +90 -0
- scripts/prepare_figure5_4x4_cases.py +470 -0
- scripts/prepare_geom_drugs_geomol_top20.py +472 -0
- scripts/prepare_pdbbind_core_raw.py +90 -0
- scripts/prepare_platinum_raw.py +74 -0
- scripts/prepare_pubchem_etkdg.py +54 -0
- scripts/rebuild_hif2a_final_sdf_and_metrics.py +55 -0
- scripts/rebuild_hif2a_fixed_pool_sweep.py +175 -0
- scripts/rebuild_hif2a_strict.py +220 -0
- scripts/recompute_hif2a_bootstrap.py +63 -0
- scripts/reference_free_geometry.py +112 -0
- scripts/render_binding_modes.py +413 -0
- scripts/render_figure5_4x4.py +330 -0
- scripts/render_figure6_pharmacophore_pymol.py +162 -0
- scripts/render_hif2a_contact_pharmacophore_pymol.py +414 -0
- scripts/render_hif2a_figure11.py +397 -0
- scripts/run_bace_2d_baseline.py +73 -0
- scripts/run_drugs_validation_matrix.py +225 -0
- scripts/run_external_reference_free.py +53 -0
- scripts/run_manimol_finalk_sensitivity.py +106 -0
- scripts/run_manimol_oversampling_sensitivity.py +109 -0
- scripts/run_pdisp_matched_intervention.py +116 -0
- scripts/run_stage1_stage2_cross20.py +83 -0
- scripts/score_hif2a_candidates_with_pgraph.py +221 -0
- scripts/summarize_dispersion_intervention.py +88 -0
- scripts/test_mean_dispersion_bridge.py +50 -0
- scripts/train_dispersion_head_diagnostic.py +320 -0
- scripts/train_dispersion_head_streaming.py +519 -0
- scripts/train_geomanimol.py +100 -0
- scripts/train_geomanimol_flow.py +26 -0
- scripts/train_geomanimol_residual.py +70 -0
- scripts/train_geomanimol_torsion.py +50 -0
- scripts/train_quantile_representation_diagnostic.py +204 -0
- scripts/validate_mean_dispersion_prior.py +173 -0
- scripts/validate_packed_shard.py +41 -0
- scripts/validate_stage1_prior.py +221 -0
- scripts/validate_top20_shards.py +36 -0
- scripts/validation/__init__.py +1 -0
- scripts/validation/hif2a_downstream_baselines.py +105 -0
- scripts/validation/hif2a_pairwise_ranker.py +218 -0
- scripts/verify_binding_mode_figure.py +47 -0
- torsion_manifold.py +281 -0
- train.py +78 -0
- train_graph_encoder.py +206 -0
- train_local_torsion_corrector.py +511 -0
- train_mean_dispersion_torsion_prior.py +329 -0
- train_probability_basin.py +235 -0
- train_torsion_generator.py +1631 -0
- train_torsion_manifold_prior.py +258 -0
- train_torsional_denoiser.py +415 -0
- trainer.py +1038 -0
- utils/__init__.py +7 -0
- utils/checkpoint.py +40 -0
- utils/device.py +17 -0
- utils/helpers.py +98 -0
- utils/kabsch.py +29 -0
- utils/lookup_table.py +164 -0
- utils/metrics.py +50 -0
- utils/optuna.py +111 -0
- utils/save_mol.py +366 -0
- utils/util.py +36 -0
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""Rebuild latest HIF2A final libraries from saved raw pools for audits."""
|
|
3
|
+
import argparse
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
import torch
|
|
6
|
+
from rdkit import Chem
|
|
7
|
+
|
|
8
|
+
import sys
|
|
9
|
+
ROOT = Path(__file__).resolve().parents[1]
|
|
10
|
+
sys.path.insert(0, str(ROOT))
|
|
11
|
+
from infer_base_conformers import optimize_mmff94s_pool, select_precision2r_conformers
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def write_sdf(mol, coords, path):
|
|
15
|
+
out = Chem.Mol(mol)
|
|
16
|
+
while out.GetNumConformers():
|
|
17
|
+
out.RemoveConformer(0)
|
|
18
|
+
conf = Chem.Conformer(out.GetNumAtoms())
|
|
19
|
+
for i, xyz in enumerate(coords.tolist()):
|
|
20
|
+
conf.SetAtomPosition(i, tuple(float(v) for v in xyz))
|
|
21
|
+
out.AddConformer(conf, assignId=True)
|
|
22
|
+
path.parent.mkdir(parents=True, exist_ok=True)
|
|
23
|
+
w = Chem.SDWriter(str(path))
|
|
24
|
+
w.write(out)
|
|
25
|
+
w.close()
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def main():
|
|
29
|
+
ap = argparse.ArgumentParser()
|
|
30
|
+
ap.add_argument("--pool", required=True)
|
|
31
|
+
ap.add_argument("--out", required=True)
|
|
32
|
+
ap.add_argument("--method", required=True)
|
|
33
|
+
ap.add_argument("--manifest", required=True)
|
|
34
|
+
args = ap.parse_args()
|
|
35
|
+
data = torch.load(args.pool, map_location="cpu", weights_only=False)
|
|
36
|
+
root = Path(args.out)
|
|
37
|
+
for rec in data["records"]:
|
|
38
|
+
ligand = f"hif2a_lig{int(rec['mol_id']) + 1:03d}"
|
|
39
|
+
mol = rec["mol"]
|
|
40
|
+
pool = rec["raw_pool"].float()
|
|
41
|
+
heavy = rec.get("heavy_mask")
|
|
42
|
+
pool = optimize_mmff94s_pool(pool, mol, max_steps=80)
|
|
43
|
+
selected = select_precision2r_conformers(
|
|
44
|
+
pool, int(rec["target_count"]), mol=mol, heavy_mask=heavy,
|
|
45
|
+
metric="centered_rmsd", density_k=20, prefilter_quantile=0.45,
|
|
46
|
+
density_weight=1.0, centrality_weight=0.45,
|
|
47
|
+
duplicate_radius=0.04, energy_prefilter_quantile=0.70,
|
|
48
|
+
)
|
|
49
|
+
for i, coords in enumerate(selected):
|
|
50
|
+
write_sdf(mol, coords, root / ligand / args.method / f"conf_{i:04d}_local_only.sdf")
|
|
51
|
+
print(f"rebuilt {len(data['records'])} ligands under {root}")
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
if __name__ == "__main__":
|
|
55
|
+
main()
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""Build nested HIF2A libraries from one fixed candidate pool per method.
|
|
3
|
+
|
|
4
|
+
The script intentionally separates candidate generation from retained-library
|
|
5
|
+
size. A single deterministic selector pass produces an ordered list, and
|
|
6
|
+
K=1,5,10,20 are prefixes of that same list. Crystal coordinates are used
|
|
7
|
+
only for post hoc placement, never for selection.
|
|
8
|
+
"""
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
import argparse
|
|
12
|
+
import csv
|
|
13
|
+
import hashlib
|
|
14
|
+
import json
|
|
15
|
+
from pathlib import Path
|
|
16
|
+
|
|
17
|
+
import torch
|
|
18
|
+
from rdkit import Chem
|
|
19
|
+
from rdkit.Chem import rdMolAlign, rdMolTransforms
|
|
20
|
+
|
|
21
|
+
import sys
|
|
22
|
+
ROOT = Path(__file__).resolve().parents[1]
|
|
23
|
+
sys.path.insert(0, str(ROOT))
|
|
24
|
+
from rebuild_hif2a_strict import ( # noqa: E402
|
|
25
|
+
load_manifest,
|
|
26
|
+
mol_with_coords,
|
|
27
|
+
optimize_with_mmff94s_and_energy,
|
|
28
|
+
strict_select,
|
|
29
|
+
write_sdf,
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def sha256(path: Path) -> str:
|
|
34
|
+
h = hashlib.sha256()
|
|
35
|
+
with path.open("rb") as handle:
|
|
36
|
+
for block in iter(lambda: handle.read(1 << 20), b""):
|
|
37
|
+
h.update(block)
|
|
38
|
+
return h.hexdigest()
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def load_reference(path: Path):
|
|
42
|
+
mol = next((m for m in Chem.SDMolSupplier(str(path), removeHs=False, sanitize=False) if m), None)
|
|
43
|
+
if mol is None or not mol.GetNumConformers():
|
|
44
|
+
raise RuntimeError(f"Invalid reference ligand: {path}")
|
|
45
|
+
return mol
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def place_posthoc(local_mol, reference_mol):
|
|
49
|
+
placed = Chem.Mol(local_mol)
|
|
50
|
+
placed_heavy = Chem.RemoveHs(Chem.Mol(placed))
|
|
51
|
+
ref_heavy = Chem.RemoveHs(Chem.Mol(reference_mol))
|
|
52
|
+
_, transform, _ = rdMolAlign.GetBestAlignmentTransform(placed_heavy, ref_heavy)
|
|
53
|
+
rdMolTransforms.TransformConformer(placed.GetConformer(), transform)
|
|
54
|
+
return placed
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def main() -> None:
|
|
58
|
+
ap = argparse.ArgumentParser()
|
|
59
|
+
ap.add_argument("--pool", required=True, help="One fixed raw_pool.pt for one method")
|
|
60
|
+
ap.add_argument("--manifest", required=True)
|
|
61
|
+
ap.add_argument("--out", required=True)
|
|
62
|
+
ap.add_argument("--method", required=True, choices=["manimol", "etkdg"])
|
|
63
|
+
ap.add_argument("--budgets", default="1,5,10,20")
|
|
64
|
+
ap.add_argument("--expected-pool-size", type=int, default=300)
|
|
65
|
+
ap.add_argument("--energy-window", type=float, default=6.0)
|
|
66
|
+
ap.add_argument("--dedup-rmsd", type=float, default=0.5)
|
|
67
|
+
args = ap.parse_args()
|
|
68
|
+
|
|
69
|
+
pool_path = Path(args.pool).resolve()
|
|
70
|
+
out = Path(args.out).resolve()
|
|
71
|
+
budgets = sorted({int(x) for x in args.budgets.split(",") if x.strip()})
|
|
72
|
+
if not budgets or min(budgets) < 1:
|
|
73
|
+
raise ValueError("budgets must be positive")
|
|
74
|
+
max_budget = max(budgets)
|
|
75
|
+
if args.expected_pool_size < max_budget:
|
|
76
|
+
raise ValueError("expected pool must be at least max retained budget")
|
|
77
|
+
|
|
78
|
+
payload = torch.load(pool_path, map_location="cpu", weights_only=False)
|
|
79
|
+
records = payload.get("records", [])
|
|
80
|
+
manifest = load_manifest(Path(args.manifest))
|
|
81
|
+
audit_rows = []
|
|
82
|
+
|
|
83
|
+
for record in records:
|
|
84
|
+
ligand_id = f"hif2a_lig{int(record['mol_id']) + 1:03d}"
|
|
85
|
+
entry = manifest.get(ligand_id)
|
|
86
|
+
if entry is None:
|
|
87
|
+
raise RuntimeError(f"Missing manifest entry for {ligand_id}")
|
|
88
|
+
raw = record["raw_pool"].float()
|
|
89
|
+
raw_count = int(raw.size(0))
|
|
90
|
+
if raw_count != int(args.expected_pool_size):
|
|
91
|
+
raise RuntimeError(
|
|
92
|
+
f"{args.method}/{ligand_id}: expected {args.expected_pool_size} candidates, got {raw_count}"
|
|
93
|
+
)
|
|
94
|
+
mol = record["mol"]
|
|
95
|
+
if mol is None:
|
|
96
|
+
raise RuntimeError(f"Missing RDKit molecule for {ligand_id}")
|
|
97
|
+
reference = load_reference(Path(entry.get("crystal_ligand_sdf", entry["ligand_sdf"])))
|
|
98
|
+
|
|
99
|
+
# Apply the same geometric preparation before selection as the
|
|
100
|
+
# external-validation pipeline. Reference coordinates are not used.
|
|
101
|
+
prepared = []
|
|
102
|
+
for coords in raw:
|
|
103
|
+
prepared.append(optimize_with_mmff94s_and_energy(coords, mol, max_steps=80)[0])
|
|
104
|
+
prepared = torch.stack(prepared)
|
|
105
|
+
|
|
106
|
+
# One selector pass only. Prefixes define nested retained libraries.
|
|
107
|
+
selected, energies, n_energy = strict_select(
|
|
108
|
+
prepared,
|
|
109
|
+
mol,
|
|
110
|
+
record.get("heavy_mask"),
|
|
111
|
+
max_budget,
|
|
112
|
+
args.energy_window,
|
|
113
|
+
args.dedup_rmsd,
|
|
114
|
+
)
|
|
115
|
+
achieved = len(selected)
|
|
116
|
+
for budget in budgets:
|
|
117
|
+
library = selected[:budget]
|
|
118
|
+
kroot = out / f"K{budget}" / ligand_id / args.method / "placed_sdf"
|
|
119
|
+
local_root = out / f"K{budget}" / ligand_id / args.method / "local_sdf"
|
|
120
|
+
kroot.mkdir(parents=True, exist_ok=True)
|
|
121
|
+
local_root.mkdir(parents=True, exist_ok=True)
|
|
122
|
+
for index, coords in enumerate(library):
|
|
123
|
+
local_mol = mol_with_coords(mol, coords)
|
|
124
|
+
write_sdf(mol, coords, local_root / f"conf_{index:04d}_local_only.sdf")
|
|
125
|
+
placed = place_posthoc(local_mol, reference)
|
|
126
|
+
writer = Chem.SDWriter(str(kroot / f"conf_{index:04d}_placed.sdf"))
|
|
127
|
+
writer.write(placed)
|
|
128
|
+
writer.close()
|
|
129
|
+
audit_rows.append(
|
|
130
|
+
{
|
|
131
|
+
"method": args.method,
|
|
132
|
+
"ligand_id": ligand_id,
|
|
133
|
+
"K_requested": budget,
|
|
134
|
+
"candidate_pool_size": raw_count,
|
|
135
|
+
"energy_evaluable": int(torch.isfinite(energies).sum()),
|
|
136
|
+
"energy_window_candidates": int(n_energy),
|
|
137
|
+
"K_achieved": len(library),
|
|
138
|
+
"max_selector_achieved": achieved,
|
|
139
|
+
"nested_prefix": True,
|
|
140
|
+
"raw_pool_sha256": sha256(pool_path),
|
|
141
|
+
}
|
|
142
|
+
)
|
|
143
|
+
|
|
144
|
+
if not audit_rows:
|
|
145
|
+
raise RuntimeError("No HIF2A records were processed")
|
|
146
|
+
out.mkdir(parents=True, exist_ok=True)
|
|
147
|
+
with (out / "library_audit.csv").open("w", newline="", encoding="utf-8") as handle:
|
|
148
|
+
writer = csv.DictWriter(handle, fieldnames=audit_rows[0].keys())
|
|
149
|
+
writer.writeheader()
|
|
150
|
+
writer.writerows(audit_rows)
|
|
151
|
+
(out / "protocol_manifest.json").write_text(
|
|
152
|
+
json.dumps(
|
|
153
|
+
{
|
|
154
|
+
"method": args.method,
|
|
155
|
+
"raw_pool": str(pool_path),
|
|
156
|
+
"raw_pool_sha256": sha256(pool_path),
|
|
157
|
+
"candidate_pool_size": args.expected_pool_size,
|
|
158
|
+
"budgets": budgets,
|
|
159
|
+
"selector_passes_per_ligand": 1,
|
|
160
|
+
"nested_prefix_libraries": True,
|
|
161
|
+
"energy_window_kcal_mol": args.energy_window,
|
|
162
|
+
"symmetry_aware_dedup_rmsd_A": args.dedup_rmsd,
|
|
163
|
+
"reference_used": "post hoc alignment only",
|
|
164
|
+
},
|
|
165
|
+
indent=2,
|
|
166
|
+
)
|
|
167
|
+
+ "\n",
|
|
168
|
+
encoding="utf-8",
|
|
169
|
+
)
|
|
170
|
+
print(f"{args.method}: processed {len(records)} ligands from fixed N_C={args.expected_pool_size}")
|
|
171
|
+
print(f"Saved nested libraries under {out}")
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
if __name__ == "__main__":
|
|
175
|
+
main()
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""Rebuild HIF2A libraries with auditable post-processing and alignment.
|
|
3
|
+
|
|
4
|
+
This script is deliberately separate from the legacy HIF2A rebuild path. It
|
|
5
|
+
does not use reference coordinates for candidate generation or selection. The
|
|
6
|
+
crystal ligand is used only after selection to place candidates in the
|
|
7
|
+
receptor frame for contact-conditioned evaluation.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
import argparse
|
|
13
|
+
import csv
|
|
14
|
+
import hashlib
|
|
15
|
+
import json
|
|
16
|
+
from pathlib import Path
|
|
17
|
+
|
|
18
|
+
import numpy as np
|
|
19
|
+
import torch
|
|
20
|
+
from rdkit import Chem
|
|
21
|
+
from rdkit.Chem import AllChem, rdMolAlign, rdMolTransforms
|
|
22
|
+
|
|
23
|
+
import sys
|
|
24
|
+
|
|
25
|
+
ROOT = Path(__file__).resolve().parents[1]
|
|
26
|
+
sys.path.insert(0, str(ROOT))
|
|
27
|
+
from infer_base_conformers import ( # noqa: E402
|
|
28
|
+
_normalize_01,
|
|
29
|
+
_pairwise_torsion_signature_distance,
|
|
30
|
+
compute_mmff_energy,
|
|
31
|
+
optimize_with_mmff94s_and_energy,
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def mol_with_coords(template, coords):
|
|
36
|
+
mol = Chem.Mol(template)
|
|
37
|
+
mol.RemoveAllConformers()
|
|
38
|
+
conf = Chem.Conformer(mol.GetNumAtoms())
|
|
39
|
+
for i, xyz in enumerate(coords.detach().cpu().numpy()):
|
|
40
|
+
conf.SetAtomPosition(i, tuple(float(v) for v in xyz))
|
|
41
|
+
mol.AddConformer(conf, assignId=True)
|
|
42
|
+
return mol
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def symmetry_rmsd(left, right):
|
|
46
|
+
try:
|
|
47
|
+
return float(AllChem.GetBestRMS(Chem.RemoveHs(left), Chem.RemoveHs(right)))
|
|
48
|
+
except Exception:
|
|
49
|
+
return float("inf")
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def sha256(path: Path) -> str:
|
|
53
|
+
digest = hashlib.sha256()
|
|
54
|
+
with path.open("rb") as handle:
|
|
55
|
+
for block in iter(lambda: handle.read(1 << 20), b""):
|
|
56
|
+
digest.update(block)
|
|
57
|
+
return digest.hexdigest()
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def strict_select(coords, mol, heavy_mask, target_count, energy_window, dedup_rmsd):
|
|
61
|
+
"""Density-centrality ranking followed by strict symmetry-aware dedup.
|
|
62
|
+
|
|
63
|
+
No duplicate backfill is allowed. A short library is returned as a real
|
|
64
|
+
failure and is recorded by the caller.
|
|
65
|
+
"""
|
|
66
|
+
energies = torch.tensor([compute_mmff_energy(x, mol) for x in coords], dtype=torch.float32)
|
|
67
|
+
finite = torch.isfinite(energies)
|
|
68
|
+
if not bool(finite.any()):
|
|
69
|
+
return [], energies, 0
|
|
70
|
+
min_energy = float(energies[finite].min())
|
|
71
|
+
keep = finite & (energies <= min_energy + float(energy_window))
|
|
72
|
+
keep_idx = torch.nonzero(keep, as_tuple=False).view(-1)
|
|
73
|
+
if keep_idx.numel() == 0:
|
|
74
|
+
return [], energies, 0
|
|
75
|
+
pool = coords[keep_idx]
|
|
76
|
+
try:
|
|
77
|
+
dist = _pairwise_torsion_signature_distance(
|
|
78
|
+
pool, mol, fallback_metric="centered_rmsd", heavy_mask=heavy_mask
|
|
79
|
+
)
|
|
80
|
+
except Exception:
|
|
81
|
+
dist = torch.cdist(pool.reshape(pool.size(0), -1), pool.reshape(pool.size(0), -1))
|
|
82
|
+
n = int(pool.size(0))
|
|
83
|
+
eye = torch.eye(n, dtype=dist.dtype, device=dist.device)
|
|
84
|
+
k = max(1, min(20, max(1, n - 1)))
|
|
85
|
+
local = torch.sort(dist + eye * 1e6, dim=1).values[:, :k].mean(dim=1)
|
|
86
|
+
centrality = dist.mean(dim=1)
|
|
87
|
+
quality = _normalize_01(local) + 0.45 * _normalize_01(centrality)
|
|
88
|
+
order = torch.argsort(quality).detach().cpu().tolist()
|
|
89
|
+
|
|
90
|
+
selected = []
|
|
91
|
+
selected_mols = []
|
|
92
|
+
for local_idx in order:
|
|
93
|
+
candidate = mol_with_coords(mol, pool[int(local_idx)])
|
|
94
|
+
if any(symmetry_rmsd(candidate, previous) < float(dedup_rmsd) for previous in selected_mols):
|
|
95
|
+
continue
|
|
96
|
+
selected.append(pool[int(local_idx)])
|
|
97
|
+
selected_mols.append(candidate)
|
|
98
|
+
if len(selected) >= int(target_count):
|
|
99
|
+
break
|
|
100
|
+
return selected, energies, int(keep_idx.numel())
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def load_manifest(path: Path):
|
|
104
|
+
rows = {}
|
|
105
|
+
with path.open(newline="", encoding="utf-8") as handle:
|
|
106
|
+
for row in csv.DictReader(handle):
|
|
107
|
+
rows[str(row["complex_id"])] = row
|
|
108
|
+
return rows
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def write_sdf(mol, coords, path: Path):
|
|
112
|
+
out = mol_with_coords(mol, coords)
|
|
113
|
+
path.parent.mkdir(parents=True, exist_ok=True)
|
|
114
|
+
writer = Chem.SDWriter(str(path))
|
|
115
|
+
writer.write(out)
|
|
116
|
+
writer.close()
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
def main():
|
|
120
|
+
parser = argparse.ArgumentParser()
|
|
121
|
+
parser.add_argument("--raw-pool", required=True)
|
|
122
|
+
parser.add_argument("--manifest", required=True)
|
|
123
|
+
parser.add_argument("--out", required=True)
|
|
124
|
+
parser.add_argument("--method", required=True, choices=["manimol", "etkdg"])
|
|
125
|
+
parser.add_argument("--budgets", default="1,5,10,20")
|
|
126
|
+
parser.add_argument("--energy-window", type=float, default=6.0)
|
|
127
|
+
parser.add_argument("--dedup-rmsd", type=float, default=0.5)
|
|
128
|
+
args = parser.parse_args()
|
|
129
|
+
|
|
130
|
+
raw_path = Path(args.raw_pool)
|
|
131
|
+
out = Path(args.out)
|
|
132
|
+
manifest = load_manifest(Path(args.manifest))
|
|
133
|
+
payload = torch.load(raw_path, map_location="cpu", weights_only=False)
|
|
134
|
+
records = payload["records"]
|
|
135
|
+
budgets = [int(v) for v in args.budgets.split(",") if v.strip()]
|
|
136
|
+
rows = []
|
|
137
|
+
|
|
138
|
+
for record in records:
|
|
139
|
+
ligand_id = f"hif2a_lig{int(record['mol_id']) + 1:03d}"
|
|
140
|
+
entry = manifest.get(ligand_id)
|
|
141
|
+
if entry is None:
|
|
142
|
+
raise RuntimeError(f"Missing manifest entry for {ligand_id}")
|
|
143
|
+
ref_path = Path(entry.get("crystal_ligand_sdf", entry["ligand_sdf"]))
|
|
144
|
+
ref = next((m for m in Chem.SDMolSupplier(str(ref_path), removeHs=False, sanitize=False) if m), None)
|
|
145
|
+
if ref is None or not ref.GetNumConformers():
|
|
146
|
+
raise RuntimeError(f"Invalid crystal ligand for {ligand_id}: {ref_path}")
|
|
147
|
+
mol = record["mol"]
|
|
148
|
+
raw = record["raw_pool"].float()
|
|
149
|
+
prepared = []
|
|
150
|
+
for coords in raw:
|
|
151
|
+
prepared.append(optimize_with_mmff94s_and_energy(coords, mol, max_steps=80)[0])
|
|
152
|
+
prepared = torch.stack(prepared)
|
|
153
|
+
heavy = record.get("heavy_mask")
|
|
154
|
+
|
|
155
|
+
for budget in budgets:
|
|
156
|
+
selected, energies, n_energy = strict_select(
|
|
157
|
+
prepared, mol, heavy, budget, args.energy_window, args.dedup_rmsd
|
|
158
|
+
)
|
|
159
|
+
library_dir = out / ligand_id / args.method / f"K{budget}"
|
|
160
|
+
library_dir.mkdir(parents=True, exist_ok=True)
|
|
161
|
+
for index, coords in enumerate(selected):
|
|
162
|
+
local_path = library_dir / f"conf_{index:04d}_local_only.sdf"
|
|
163
|
+
placed_path = library_dir / f"conf_{index:04d}_placed.sdf"
|
|
164
|
+
local_mol = mol_with_coords(mol, coords)
|
|
165
|
+
write_sdf(mol, coords, local_path)
|
|
166
|
+
placed_mol = Chem.Mol(local_mol)
|
|
167
|
+
# Alignment is post hoc and uses the heavy-atom topology so
|
|
168
|
+
# explicit-H differences between generated and crystal SDFs
|
|
169
|
+
# cannot prevent a valid symmetry-aware transform.
|
|
170
|
+
placed_heavy = Chem.RemoveHs(Chem.Mol(placed_mol))
|
|
171
|
+
ref_heavy = Chem.RemoveHs(Chem.Mol(ref))
|
|
172
|
+
_, transform, _ = rdMolAlign.GetBestAlignmentTransform(placed_heavy, ref_heavy)
|
|
173
|
+
rdMolTransforms.TransformConformer(placed_mol.GetConformer(), transform)
|
|
174
|
+
writer = Chem.SDWriter(str(placed_path))
|
|
175
|
+
writer.write(placed_mol)
|
|
176
|
+
writer.close()
|
|
177
|
+
rows.append(
|
|
178
|
+
{
|
|
179
|
+
"ligand_id": ligand_id,
|
|
180
|
+
"method": args.method,
|
|
181
|
+
"K": budget,
|
|
182
|
+
"raw_candidates": int(raw.size(0)),
|
|
183
|
+
"energy_evaluable": int(torch.isfinite(energies).sum()),
|
|
184
|
+
"energy_window_candidates": int(n_energy),
|
|
185
|
+
"unique_selected": len(selected),
|
|
186
|
+
"target_K": budget,
|
|
187
|
+
"short_library": len(selected) < budget,
|
|
188
|
+
"raw_pool_sha256": sha256(raw_path),
|
|
189
|
+
}
|
|
190
|
+
)
|
|
191
|
+
|
|
192
|
+
out.mkdir(parents=True, exist_ok=True)
|
|
193
|
+
with (out / "library_audit.csv").open("w", newline="", encoding="utf-8") as handle:
|
|
194
|
+
writer = csv.DictWriter(handle, fieldnames=rows[0].keys())
|
|
195
|
+
writer.writeheader()
|
|
196
|
+
writer.writerows(rows)
|
|
197
|
+
(out / "protocol_manifest.json").write_text(
|
|
198
|
+
json.dumps(
|
|
199
|
+
{
|
|
200
|
+
"method": args.method,
|
|
201
|
+
"raw_pool": str(raw_path),
|
|
202
|
+
"raw_pool_sha256": sha256(raw_path),
|
|
203
|
+
"budgets": budgets,
|
|
204
|
+
"energy_window_kcal_mol": args.energy_window,
|
|
205
|
+
"symmetry_aware_dedup_rmsd_A": args.dedup_rmsd,
|
|
206
|
+
"reference_used": "post hoc alignment only",
|
|
207
|
+
"duplicate_backfill": False,
|
|
208
|
+
},
|
|
209
|
+
indent=2,
|
|
210
|
+
)
|
|
211
|
+
+ "\n",
|
|
212
|
+
encoding="utf-8",
|
|
213
|
+
)
|
|
214
|
+
print(pd.DataFrame(rows).to_string(index=False))
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
if __name__ == "__main__":
|
|
218
|
+
import pandas as pd
|
|
219
|
+
|
|
220
|
+
main()
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""Recompute paired HIF2A budget metrics from ligand-level raw outputs."""
|
|
3
|
+
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
|
|
6
|
+
import argparse
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
import numpy as np
|
|
9
|
+
import pandas as pd
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def parse_args():
|
|
13
|
+
p = argparse.ArgumentParser()
|
|
14
|
+
p.add_argument("--input", required=True)
|
|
15
|
+
p.add_argument("--output-dir", required=True)
|
|
16
|
+
p.add_argument("--methods", default="etkdg,manimol")
|
|
17
|
+
p.add_argument("--seed", type=int, default=20260909)
|
|
18
|
+
p.add_argument("--bootstrap", type=int, default=10000)
|
|
19
|
+
return p.parse_args()
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def paired_bootstrap(delta, rng, replicates):
|
|
23
|
+
delta = np.asarray(delta, dtype=float)
|
|
24
|
+
samples = rng.integers(0, len(delta), size=(replicates, len(delta)))
|
|
25
|
+
means = delta[samples].mean(axis=1)
|
|
26
|
+
return float(means.mean()), *np.quantile(means, [0.025, 0.975]).tolist()
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def main():
|
|
30
|
+
args = parse_args()
|
|
31
|
+
frame = pd.read_csv(args.input)
|
|
32
|
+
required = {"ligand_id", "method", "K", "functional_state_coverage", "topk_functional_recovery"}
|
|
33
|
+
missing = required - set(frame.columns)
|
|
34
|
+
if missing:
|
|
35
|
+
raise ValueError(f"Missing columns: {sorted(missing)}")
|
|
36
|
+
methods = [item.strip() for item in args.methods.split(",") if item.strip()]
|
|
37
|
+
frame = frame[frame.method.isin(methods)].copy()
|
|
38
|
+
frame["K"] = frame["K"].astype(int)
|
|
39
|
+
rng = np.random.default_rng(args.seed)
|
|
40
|
+
rows = []
|
|
41
|
+
for k in sorted(frame.K.unique()):
|
|
42
|
+
subset = frame[frame.K == k]
|
|
43
|
+
wide = subset.pivot(index="ligand_id", columns="method", values=["functional_state_coverage", "topk_functional_recovery"])
|
|
44
|
+
for metric in ("functional_state_coverage", "topk_functional_recovery"):
|
|
45
|
+
for method in methods:
|
|
46
|
+
if (metric, method) in wide:
|
|
47
|
+
values = wide[(metric, method)].dropna().to_numpy(dtype=float)
|
|
48
|
+
rows.append({"metric": metric, "K": k, "method": method, "n_ligands": len(values), "mean": values.mean(), "median": np.median(values)})
|
|
49
|
+
if all((metric, method) in wide for method in methods):
|
|
50
|
+
pair = wide.loc[:, [(metric, method) for method in methods]].dropna()
|
|
51
|
+
delta = pair[(metric, methods[-1])].to_numpy() - pair[(metric, methods[0])].to_numpy()
|
|
52
|
+
mean, low, high = paired_bootstrap(delta, rng, args.bootstrap)
|
|
53
|
+
rows.append({"metric": metric, "K": k, "method": f"{methods[-1]}-minus-{methods[0]}", "n_ligands": len(delta), "mean": mean, "median": np.median(delta), "ci95_low": low, "ci95_high": high})
|
|
54
|
+
output = Path(args.output_dir)
|
|
55
|
+
output.mkdir(parents=True, exist_ok=True)
|
|
56
|
+
summary = pd.DataFrame(rows)
|
|
57
|
+
summary.to_csv(output / "hif2a_budget_bootstrap_recomputed.csv", index=False)
|
|
58
|
+
frame.to_csv(output / "hif2a_budget_source_snapshot.csv", index=False)
|
|
59
|
+
print(summary.to_string(index=False))
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
if __name__ == "__main__":
|
|
63
|
+
main()
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
"""Reference-free geometry preparation for external conformer evaluation.
|
|
2
|
+
|
|
3
|
+
The template molecule may carry a crystallographic conformer for post hoc
|
|
4
|
+
evaluation. Every preparation function therefore removes all template
|
|
5
|
+
conformers before installing the generated candidate coordinates.
|
|
6
|
+
"""
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import numpy as np
|
|
10
|
+
import torch
|
|
11
|
+
from rdkit import Chem
|
|
12
|
+
from rdkit.Chem import AllChem
|
|
13
|
+
from rdkit.Geometry import Point3D
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def _candidate_molecule(template_mol: Chem.Mol, coords):
|
|
17
|
+
array = np.asarray(torch.as_tensor(coords).detach().cpu(), dtype=float)
|
|
18
|
+
if array.shape != (template_mol.GetNumAtoms(), 3):
|
|
19
|
+
raise ValueError(
|
|
20
|
+
f"coordinate shape {array.shape} does not match {template_mol.GetNumAtoms()} atoms"
|
|
21
|
+
)
|
|
22
|
+
mol = Chem.RWMol(template_mol)
|
|
23
|
+
mol.RemoveAllConformers()
|
|
24
|
+
conformer = Chem.Conformer(mol.GetNumAtoms())
|
|
25
|
+
for atom_index, point in enumerate(array):
|
|
26
|
+
conformer.SetAtomPosition(atom_index, Point3D(*map(float, point)))
|
|
27
|
+
conformer_id = mol.AddConformer(conformer, assignId=True)
|
|
28
|
+
return mol, conformer_id
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def _restore_type(array, template):
|
|
32
|
+
if torch.is_tensor(template):
|
|
33
|
+
return torch.as_tensor(array, dtype=template.dtype, device=template.device)
|
|
34
|
+
return np.asarray(array, dtype=np.asarray(template).dtype)
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def optimize_with_mmff_reference_free(coords, mol: Chem.Mol, max_steps: int = 80):
|
|
38
|
+
"""Minimize only the supplied candidate conformer with MMFF94."""
|
|
39
|
+
try:
|
|
40
|
+
candidate_mol, conformer_id = _candidate_molecule(mol, coords)
|
|
41
|
+
properties = AllChem.MMFFGetMoleculeProperties(candidate_mol)
|
|
42
|
+
if properties is None:
|
|
43
|
+
return coords
|
|
44
|
+
force_field = AllChem.MMFFGetMoleculeForceField(
|
|
45
|
+
candidate_mol, properties, confId=conformer_id
|
|
46
|
+
)
|
|
47
|
+
if force_field is None:
|
|
48
|
+
return coords
|
|
49
|
+
force_field.Initialize()
|
|
50
|
+
force_field.Minimize(maxIts=max(1, int(max_steps)))
|
|
51
|
+
prepared = candidate_mol.GetConformer(conformer_id).GetPositions()
|
|
52
|
+
return _restore_type(prepared, coords)
|
|
53
|
+
except Exception:
|
|
54
|
+
return coords
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def _dihedral_atoms(mol: Chem.Mol, atom_j: int, atom_k: int):
|
|
58
|
+
neighbors_j = [
|
|
59
|
+
atom.GetIdx() for atom in mol.GetAtomWithIdx(atom_j).GetNeighbors()
|
|
60
|
+
if atom.GetIdx() != atom_k
|
|
61
|
+
]
|
|
62
|
+
neighbors_k = [
|
|
63
|
+
atom.GetIdx() for atom in mol.GetAtomWithIdx(atom_k).GetNeighbors()
|
|
64
|
+
if atom.GetIdx() != atom_j
|
|
65
|
+
]
|
|
66
|
+
if not neighbors_j or not neighbors_k:
|
|
67
|
+
return None
|
|
68
|
+
return neighbors_j[0], atom_j, atom_k, neighbors_k[0]
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def _mmff_energy(candidate_mol, conformer_id):
|
|
72
|
+
properties = AllChem.MMFFGetMoleculeProperties(candidate_mol)
|
|
73
|
+
if properties is None:
|
|
74
|
+
return float("inf")
|
|
75
|
+
force_field = AllChem.MMFFGetMoleculeForceField(
|
|
76
|
+
candidate_mol, properties, confId=conformer_id
|
|
77
|
+
)
|
|
78
|
+
return float(force_field.CalcEnergy()) if force_field is not None else float("inf")
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def optimize_torsions_reference_free(coords, mol: Chem.Mol, rotatable_bonds, max_trials: int = 20):
|
|
82
|
+
"""Search torsions without retaining any conformer stored on ``mol``."""
|
|
83
|
+
if not rotatable_bonds or int(max_trials) <= 0:
|
|
84
|
+
return coords
|
|
85
|
+
try:
|
|
86
|
+
initial_mol, initial_id = _candidate_molecule(mol, coords)
|
|
87
|
+
best_energy = _mmff_energy(initial_mol, initial_id)
|
|
88
|
+
best = np.asarray(initial_mol.GetConformer(initial_id).GetPositions(), dtype=float)
|
|
89
|
+
except Exception:
|
|
90
|
+
return coords
|
|
91
|
+
|
|
92
|
+
for _ in range(int(max_trials)):
|
|
93
|
+
try:
|
|
94
|
+
trial_mol, trial_id = _candidate_molecule(mol, coords)
|
|
95
|
+
conformer = trial_mol.GetConformer(trial_id)
|
|
96
|
+
for atom_j, atom_k in rotatable_bonds:
|
|
97
|
+
atoms = _dihedral_atoms(trial_mol, int(atom_j), int(atom_k))
|
|
98
|
+
if atoms is not None:
|
|
99
|
+
AllChem.SetDihedralDeg(conformer, *atoms, float(np.random.uniform(0.0, 360.0)))
|
|
100
|
+
energy = _mmff_energy(trial_mol, trial_id)
|
|
101
|
+
if energy < best_energy:
|
|
102
|
+
best_energy = energy
|
|
103
|
+
best = np.asarray(conformer.GetPositions(), dtype=float)
|
|
104
|
+
except Exception:
|
|
105
|
+
continue
|
|
106
|
+
return _restore_type(best, coords)
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def patch_inference_geometry(inference_module):
|
|
110
|
+
"""Install reference-free preparation in an imported legacy inference module."""
|
|
111
|
+
inference_module.optimize_with_mmff = optimize_with_mmff_reference_free
|
|
112
|
+
inference_module.optimize_torsions_fast = optimize_torsions_reference_free
|