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.
Files changed (186) hide show
  1. args_parse.py +593 -0
  2. candidate_corrector.py +161 -0
  3. candidate_scorer.py +359 -0
  4. conformend/__init__.py +36 -0
  5. conformend/refiner.py +25 -0
  6. dataset/__init__.py +1 -0
  7. dataset/drugdataset.py +644 -0
  8. dataset/manifold.py +237 -0
  9. dataset/sharded_drugdataset.py +124 -0
  10. dataset/smiles2graph.py +217 -0
  11. dualmapref/__init__.py +24 -0
  12. dualmapref/refiner.py +16 -0
  13. dualmapref-0.2.8.dist-info/LICENSE +21 -0
  14. dualmapref-0.2.8.dist-info/METADATA +167 -0
  15. dualmapref-0.2.8.dist-info/RECORD +186 -0
  16. dualmapref-0.2.8.dist-info/WHEEL +5 -0
  17. dualmapref-0.2.8.dist-info/entry_points.txt +10 -0
  18. dualmapref-0.2.8.dist-info/top_level.txt +29 -0
  19. exputils.py +289 -0
  20. infer.py +23 -0
  21. infer_base_conformers.py +5589 -0
  22. infer_mean_dispersion_torsion.py +1117 -0
  23. infer_torsion.py +752 -0
  24. infer_torsional_denoiser.py +1021 -0
  25. manimol/__init__.py +30 -0
  26. manimol/__main__.py +6 -0
  27. manimol/cli.py +36 -0
  28. manimol/full_cli.py +41 -0
  29. manimol/legacy_imports.py +43 -0
  30. manimol/manifest.py +56 -0
  31. manimol/postprocess.py +148 -0
  32. manimol/priors.py +60 -0
  33. manimol/protocols.py +119 -0
  34. manimol/refiner.py +645 -0
  35. manimol/runtime.py +194 -0
  36. manimol/selection.py +85 -0
  37. manimol/torsion.py +22 -0
  38. manimol/train_cli.py +14 -0
  39. mixture_flow/src/__init__.py +1 -0
  40. mixture_flow/src/_path.py +7 -0
  41. mixture_flow/src/infer28_mixture_torsion_flow.py +1093 -0
  42. mixture_flow/src/mixture_torsion_flow.py +291 -0
  43. mixture_flow/src/summarize_mixture_debug.py +80 -0
  44. mixture_flow/src/train28_mixture_torsion_flow.py +583 -0
  45. mixture_torsion_flow.py +7 -0
  46. models/ manifold_learning.py +86 -0
  47. models/__init__.py +8 -0
  48. models/dist.py +273 -0
  49. models/dist2coords.py +23 -0
  50. models/egnn.py +107 -0
  51. models/experimental_backbones.py +364 -0
  52. models/gnnconv.py +512 -0
  53. models/kernel_inversion.py +141 -0
  54. models/kernels.py +427 -0
  55. models/losses.py +154 -0
  56. models/mean_dispersion_bridge.py +119 -0
  57. models/mean_dispersion_torsion_context.py +155 -0
  58. models/model.py +73 -0
  59. models/model0.py +2375 -0
  60. models/vis.py +128 -0
  61. pgraph_controls.py +107 -0
  62. scripts/__init__.py +2 -0
  63. scripts/analyze_basin_alignment.py +63 -0
  64. scripts/analyze_hif2a_conformer_aggregation.py +267 -0
  65. scripts/analyze_hif2a_contact_pharmacophore.py +1111 -0
  66. scripts/analyze_hif2a_fixed_budget.py +202 -0
  67. scripts/analyze_hif2a_pharmacophore.py +405 -0
  68. scripts/analyze_hif2a_pharmacophore_match_metric.py +386 -0
  69. scripts/analyze_p_control_candidate_flow.py +154 -0
  70. scripts/analyze_scaffold_basin.py +83 -0
  71. scripts/audit_dispersion_proposals.py +208 -0
  72. scripts/audit_etflow_protocol.py +258 -0
  73. scripts/audit_external_overlap.py +150 -0
  74. scripts/audit_oldbest_fulltrainval_split.py +82 -0
  75. scripts/audit_pdbbind_contact_pharmacophores.py +146 -0
  76. scripts/audit_qm9_geomol_test_split.py +94 -0
  77. scripts/audit_stage1_stage2_cross.py +131 -0
  78. scripts/build_geom_drugs_lowref_diagnostic.py +84 -0
  79. scripts/build_qm9_backfill_manifest.py +99 -0
  80. scripts/check_etflow_runtime.py +56 -0
  81. scripts/compare_prior_sources.py +122 -0
  82. scripts/compose_binding_mode_figure.py +87 -0
  83. scripts/compose_hif2a_contact_pharmacophore_suppfig.py +255 -0
  84. scripts/compose_hif2a_functional_geometry_pipeline.py +266 -0
  85. scripts/compose_manimol_internal_architecture.py +291 -0
  86. scripts/diagnose_flow_basin_intervention.py +54 -0
  87. scripts/diagnose_hif2a_pharmacophore_budget.py +185 -0
  88. scripts/diagnose_route2_p_ranking.py +123 -0
  89. scripts/discover_binding_mode_inputs.py +30 -0
  90. scripts/evaluate_functional_prior_stage1.py +230 -0
  91. scripts/evaluate_geomanimol.py +74 -0
  92. scripts/evaluate_geomanimol_flow.py +23 -0
  93. scripts/evaluate_geomanimol_multiscaffold.py +46 -0
  94. scripts/evaluate_geomanimol_residual.py +45 -0
  95. scripts/evaluate_geomanimol_torsion.py +40 -0
  96. scripts/evaluate_hif2a_bioactive_enrichment.py +91 -0
  97. scripts/evaluate_hif2a_pms_suite.py +440 -0
  98. scripts/evaluate_pdbbind_contact_pms.py +155 -0
  99. scripts/evaluate_platinum_external.py +114 -0
  100. scripts/evaluate_platinum_pool.py +82 -0
  101. scripts/evaluate_route2_calibrated_p.py +104 -0
  102. scripts/evaluate_route2_library_components.py +596 -0
  103. scripts/evaluate_route2_p_basin_budget.py +288 -0
  104. scripts/evaluate_stage1_checkpoint_only.py +30 -0
  105. scripts/export_dispersion_bond_audit.py +136 -0
  106. scripts/export_top20_shards.py +207 -0
  107. scripts/external_validation_metrics.py +152 -0
  108. scripts/fit_topology_prior.py +117 -0
  109. scripts/frozen_dispersion_audit.py +281 -0
  110. scripts/geomanimol_debug.py +50 -0
  111. scripts/geomanimol_smoke.py +37 -0
  112. scripts/infer_etflow_matched.py +524 -0
  113. scripts/inspect_checkpoint.py +12 -0
  114. scripts/inspect_conformer_capacity.py +71 -0
  115. scripts/inspect_pool.py +12 -0
  116. scripts/make_figure3_selection_loss.py +474 -0
  117. scripts/make_figure3_selection_summary.py +275 -0
  118. scripts/make_geom_debug_subset.py +62 -0
  119. scripts/make_low_reference_test_subset.py +70 -0
  120. scripts/merge_stage1_into_stage2.py +82 -0
  121. scripts/place_hif2a_candidates_for_evaluation.py +66 -0
  122. scripts/plot_hif2a_low_budget_efficiency.py +100 -0
  123. scripts/plot_prior_validation.py +48 -0
  124. scripts/precompute_stage2_assignments.py +90 -0
  125. scripts/prepare_bace_activity.py +98 -0
  126. scripts/prepare_bace_etkdg_benchmark.py +90 -0
  127. scripts/prepare_figure5_4x4_cases.py +470 -0
  128. scripts/prepare_geom_drugs_geomol_top20.py +472 -0
  129. scripts/prepare_pdbbind_core_raw.py +90 -0
  130. scripts/prepare_platinum_raw.py +74 -0
  131. scripts/prepare_pubchem_etkdg.py +54 -0
  132. scripts/rebuild_hif2a_final_sdf_and_metrics.py +55 -0
  133. scripts/rebuild_hif2a_fixed_pool_sweep.py +175 -0
  134. scripts/rebuild_hif2a_strict.py +220 -0
  135. scripts/recompute_hif2a_bootstrap.py +63 -0
  136. scripts/reference_free_geometry.py +112 -0
  137. scripts/render_binding_modes.py +413 -0
  138. scripts/render_figure5_4x4.py +330 -0
  139. scripts/render_figure6_pharmacophore_pymol.py +162 -0
  140. scripts/render_hif2a_contact_pharmacophore_pymol.py +414 -0
  141. scripts/render_hif2a_figure11.py +397 -0
  142. scripts/run_bace_2d_baseline.py +73 -0
  143. scripts/run_drugs_validation_matrix.py +225 -0
  144. scripts/run_external_reference_free.py +53 -0
  145. scripts/run_manimol_finalk_sensitivity.py +106 -0
  146. scripts/run_manimol_oversampling_sensitivity.py +109 -0
  147. scripts/run_pdisp_matched_intervention.py +116 -0
  148. scripts/run_stage1_stage2_cross20.py +83 -0
  149. scripts/score_hif2a_candidates_with_pgraph.py +221 -0
  150. scripts/summarize_dispersion_intervention.py +88 -0
  151. scripts/test_mean_dispersion_bridge.py +50 -0
  152. scripts/train_dispersion_head_diagnostic.py +320 -0
  153. scripts/train_dispersion_head_streaming.py +519 -0
  154. scripts/train_geomanimol.py +100 -0
  155. scripts/train_geomanimol_flow.py +26 -0
  156. scripts/train_geomanimol_residual.py +70 -0
  157. scripts/train_geomanimol_torsion.py +50 -0
  158. scripts/train_quantile_representation_diagnostic.py +204 -0
  159. scripts/validate_mean_dispersion_prior.py +173 -0
  160. scripts/validate_packed_shard.py +41 -0
  161. scripts/validate_stage1_prior.py +221 -0
  162. scripts/validate_top20_shards.py +36 -0
  163. scripts/validation/__init__.py +1 -0
  164. scripts/validation/hif2a_downstream_baselines.py +105 -0
  165. scripts/validation/hif2a_pairwise_ranker.py +218 -0
  166. scripts/verify_binding_mode_figure.py +47 -0
  167. torsion_manifold.py +281 -0
  168. train.py +78 -0
  169. train_graph_encoder.py +206 -0
  170. train_local_torsion_corrector.py +511 -0
  171. train_mean_dispersion_torsion_prior.py +329 -0
  172. train_probability_basin.py +235 -0
  173. train_torsion_generator.py +1631 -0
  174. train_torsion_manifold_prior.py +258 -0
  175. train_torsional_denoiser.py +415 -0
  176. trainer.py +1038 -0
  177. utils/__init__.py +7 -0
  178. utils/checkpoint.py +40 -0
  179. utils/device.py +17 -0
  180. utils/helpers.py +98 -0
  181. utils/kabsch.py +29 -0
  182. utils/lookup_table.py +164 -0
  183. utils/metrics.py +50 -0
  184. utils/optuna.py +111 -0
  185. utils/save_mol.py +366 -0
  186. utils/util.py +36 -0
@@ -0,0 +1,221 @@
1
+ #!/usr/bin/env python3
2
+ """Directly validate a Stage-I pairwise prior on held-out molecules.
3
+
4
+ The primary evaluation mirrors the Stage-I supervision convention: at most
5
+ ``max_confs`` reference conformers are converted to teacher graphs and averaged
6
+ per molecule. Metrics are computed on unique off-diagonal atom pairs before
7
+ being summarized across molecules.
8
+ """
9
+
10
+ from __future__ import annotations
11
+
12
+ import argparse
13
+ import json
14
+ import math
15
+ from pathlib import Path
16
+ import sys
17
+
18
+ import numpy as np
19
+ import pandas as pd
20
+ import torch
21
+ from scipy.stats import pearsonr, spearmanr
22
+ from torch_geometric.data import Batch
23
+
24
+ ROOT = Path(__file__).resolve().parents[1]
25
+ if str(ROOT) not in sys.path:
26
+ sys.path.insert(0, str(ROOT))
27
+
28
+ from dataset.sharded_drugdataset import ShardedDrugDataset
29
+ from infer_base_conformers import checkpoint_max_confs
30
+ from models.model0 import prob_low_dim_kernel
31
+ from train_torsional_denoiser import load_base_model
32
+
33
+
34
+ def parse_args() -> argparse.Namespace:
35
+ parser = argparse.ArgumentParser(description=__doc__)
36
+ parser.add_argument("--checkpoint", required=True)
37
+ parser.add_argument("--sharded-root", required=True)
38
+ parser.add_argument("--split", default="test", choices=("train", "val", "test"))
39
+ parser.add_argument("--max-molecules", type=int, default=50)
40
+ parser.add_argument("--max-conformers", type=int, default=0,
41
+ help="Per-molecule teacher cap; 0 uses checkpoint capacity.")
42
+ parser.add_argument("--kernel", default="umap")
43
+ parser.add_argument("--top-fraction", type=float, default=0.10)
44
+ parser.add_argument("--bootstrap", type=int, default=10000)
45
+ parser.add_argument("--seed", type=int, default=20260909)
46
+ parser.add_argument("--device", default="cuda")
47
+ parser.add_argument("--output-dir", default="results/prior_validation")
48
+ return parser.parse_args()
49
+
50
+
51
+ def torsion_count(item) -> int:
52
+ torsions = getattr(item, "torsion_index", None)
53
+ if torsions is None or not torch.is_tensor(torsions) or torsions.numel() == 0:
54
+ return 0
55
+ if torsions.dim() != 2:
56
+ return 0
57
+ rows = torsions.t() if torsions.size(0) == 4 else torsions
58
+ if rows.size(1) < 3:
59
+ return 0
60
+ central_bonds = {
61
+ tuple(sorted((int(row[1].item()), int(row[2].item())))) for row in rows
62
+ }
63
+ return len(central_bonds)
64
+
65
+
66
+ def finite_correlation(fn, x: np.ndarray, y: np.ndarray) -> float:
67
+ if x.size < 2 or np.ptp(x) <= 1e-12 or np.ptp(y) <= 1e-12:
68
+ return float("nan")
69
+ result = fn(x, y)
70
+ return float(getattr(result, "statistic", result[0]))
71
+
72
+
73
+ def top_overlap(x: np.ndarray, y: np.ndarray, fraction: float) -> float:
74
+ count = max(1, int(math.ceil(float(fraction) * x.size)))
75
+ x_top = set(np.argpartition(x, -count)[-count:].tolist())
76
+ y_top = set(np.argpartition(y, -count)[-count:].tolist())
77
+ union = x_top | y_top
78
+ return float(len(x_top & y_top) / len(union)) if union else float("nan")
79
+
80
+
81
+ def bootstrap_ci(values: np.ndarray, replicates: int, seed: int) -> tuple[float, float]:
82
+ values = values[np.isfinite(values)]
83
+ if values.size == 0:
84
+ return float("nan"), float("nan")
85
+ rng = np.random.default_rng(seed)
86
+ means = np.empty(int(replicates), dtype=np.float64)
87
+ for index in range(int(replicates)):
88
+ means[index] = rng.choice(values, size=values.size, replace=True).mean()
89
+ return tuple(np.quantile(means, [0.025, 0.975]).tolist())
90
+
91
+
92
+ def molecule_groups(dataset: ShardedDrugDataset, limit: int):
93
+ groups = [group for block in dataset.molecule_groups_by_shard for group in block]
94
+ if limit > 0:
95
+ groups = groups[:limit]
96
+ return groups
97
+
98
+
99
+ @torch.inference_mode()
100
+ def evaluate_molecule(model, dataset, group, device, max_conformers, kernel, top_fraction):
101
+ indices = list(group)[:max_conformers]
102
+ conformers = [dataset[index] for index in indices]
103
+ first = conformers[0]
104
+ graph = Batch.from_data_list([first]).to(device)
105
+ node_feat = model.gnn(graph)
106
+ graph_feat = model.pool(node_feat, graph.batch)[0]
107
+ edge_index = model._get_local_edge_index(graph.edge_index, graph.batch == 0)
108
+ prediction, _ = model._predict_P_global(node_feat, edge_index)
109
+
110
+ teachers = []
111
+ for conformer in conformers:
112
+ conf_id = int(torch.as_tensor(conformer.conf_id).reshape(-1)[0].item())
113
+ a, b = model.predict_ab(graph_feat, conf_id)
114
+ teachers.append(
115
+ prob_low_dim_kernel(
116
+ conformer.pos.to(device=device, dtype=node_feat.dtype),
117
+ a,
118
+ b,
119
+ kernel=kernel,
120
+ )
121
+ )
122
+ target = torch.stack(teachers, dim=0).mean(dim=0)
123
+ upper = torch.triu(torch.ones_like(prediction, dtype=torch.bool), diagonal=1)
124
+ pred = prediction[upper].detach().cpu().numpy().astype(np.float64)
125
+ truth = target[upper].detach().cpu().numpy().astype(np.float64)
126
+ delta = pred - truth
127
+ mol_id = int(torch.as_tensor(first.mol_id).reshape(-1)[0].item())
128
+ return {
129
+ "molecule_id": mol_id,
130
+ "n_atoms": int(first.pos.size(0)),
131
+ "n_rotatable_bonds": torsion_count(first),
132
+ "n_reference_conformers": len(group),
133
+ "n_teacher_conformers": len(conformers),
134
+ "n_pairs": int(pred.size),
135
+ "mae": float(np.mean(np.abs(delta))),
136
+ "rmse": float(np.sqrt(np.mean(delta * delta))),
137
+ "pearson": finite_correlation(pearsonr, pred, truth),
138
+ "spearman": finite_correlation(spearmanr, pred, truth),
139
+ "topk_overlap": top_overlap(pred, truth, top_fraction),
140
+ "pred_mean": float(pred.mean()),
141
+ "pred_std": float(pred.std()),
142
+ "target_mean": float(truth.mean()),
143
+ "target_std": float(truth.std()),
144
+ }
145
+
146
+
147
+ def summarize(frame: pd.DataFrame, bootstrap: int, seed: int) -> pd.DataFrame:
148
+ rows = []
149
+ metrics = ("mae", "rmse", "pearson", "spearman", "topk_overlap")
150
+ strata = [("all", frame)]
151
+ strata.extend([
152
+ ("low_0_5", frame[frame.n_rotatable_bonds <= 5]),
153
+ ("medium_6_8", frame[(frame.n_rotatable_bonds >= 6) & (frame.n_rotatable_bonds <= 8)]),
154
+ ("high_ge_9", frame[frame.n_rotatable_bonds >= 9]),
155
+ ])
156
+ for stratum, subset in strata:
157
+ for offset, metric in enumerate(metrics):
158
+ values = subset[metric].to_numpy(dtype=float)
159
+ finite = values[np.isfinite(values)]
160
+ low, high = bootstrap_ci(finite, bootstrap, seed + offset)
161
+ rows.append({
162
+ "stratum": stratum,
163
+ "metric": metric,
164
+ "n_molecules": int(finite.size),
165
+ "mean": float(finite.mean()) if finite.size else float("nan"),
166
+ "median": float(np.median(finite)) if finite.size else float("nan"),
167
+ "ci95_low": low,
168
+ "ci95_high": high,
169
+ })
170
+ return pd.DataFrame(rows)
171
+
172
+
173
+ def main() -> None:
174
+ args = parse_args()
175
+ torch.manual_seed(args.seed)
176
+ np.random.seed(args.seed)
177
+ device = torch.device(args.device if args.device != "auto" else ("cuda" if torch.cuda.is_available() else "cpu"))
178
+ capacity = checkpoint_max_confs(args.checkpoint)
179
+ max_conformers = int(args.max_conformers or capacity)
180
+ if max_conformers > capacity:
181
+ raise ValueError(f"Teacher cap {max_conformers} exceeds checkpoint capacity {capacity}.")
182
+
183
+ loader_args = argparse.Namespace(checkpoint=args.checkpoint, max_confs=capacity)
184
+ model = load_base_model(loader_args, device)
185
+ dataset = ShardedDrugDataset(args.sharded_root, args.split, cache_size=1)
186
+ groups = molecule_groups(dataset, args.max_molecules)
187
+ rows = []
188
+ for number, group in enumerate(groups, start=1):
189
+ row = evaluate_molecule(
190
+ model, dataset, group, device, max_conformers, args.kernel, args.top_fraction
191
+ )
192
+ rows.append(row)
193
+ print(
194
+ f"[{number}/{len(groups)}] mol={row['molecule_id']} refs={row['n_reference_conformers']} "
195
+ f"MAE={row['mae']:.4f} Pearson={row['pearson']:.4f}",
196
+ flush=True,
197
+ )
198
+
199
+ output = Path(args.output_dir)
200
+ output.mkdir(parents=True, exist_ok=True)
201
+ per_molecule = pd.DataFrame(rows)
202
+ summary = summarize(per_molecule, args.bootstrap, args.seed)
203
+ per_path = output / "prior_validation_per_molecule.csv"
204
+ summary_path = output / "prior_validation_summary.csv"
205
+ meta_path = output / "prior_validation_config.json"
206
+ per_molecule.to_csv(per_path, index=False)
207
+ summary.to_csv(summary_path, index=False)
208
+ meta_path.write_text(json.dumps({
209
+ **vars(args),
210
+ "checkpoint_capacity": capacity,
211
+ "effective_teacher_cap": max_conformers,
212
+ "pair_mask": "all atoms, unique upper-triangular off-diagonal pairs",
213
+ "target": "arithmetic mean of per-conformer teacher graphs",
214
+ }, indent=2), encoding="utf-8")
215
+ print(summary.to_string(index=False, float_format=lambda value: f"{value:.5f}"))
216
+ print(f"Saved {per_path}")
217
+ print(f"Saved {summary_path}")
218
+
219
+
220
+ if __name__ == "__main__":
221
+ main()
@@ -0,0 +1,36 @@
1
+ #!/usr/bin/env python3
2
+ """Cheap structural smoke test for the isolated tensor-only shard cache."""
3
+ import argparse
4
+ import json
5
+ import os
6
+ import torch
7
+
8
+
9
+ def main():
10
+ p = argparse.ArgumentParser()
11
+ p.add_argument("--root", required=True)
12
+ p.add_argument("--sample_per_split", type=int, default=2)
13
+ args = p.parse_args()
14
+ with open(os.path.join(args.root, "manifest.json")) as handle:
15
+ manifest = json.load(handle)
16
+ checked = 0
17
+ for split, info in manifest["splits"].items():
18
+ shards = info["shards"]
19
+ if not shards or sum(int(s["samples"]) for s in shards) != int(info["samples"]):
20
+ raise RuntimeError("invalid sample counts for %s" % split)
21
+ for shard in (shards[:args.sample_per_split] + shards[-args.sample_per_split:]):
22
+ payload = torch.load(os.path.join(args.root, shard["file"]), map_location="cpu", weights_only=False)
23
+ for item in payload["records"]:
24
+ if hasattr(item, "rdmol") or hasattr(item, "rdmol_embedded"):
25
+ raise RuntimeError("RDKit object survived in %s" % shard["file"])
26
+ for field in ("x", "edge_index", "edge_attr", "pos", "mol_id", "conf_id", "smiles", "num_atoms"):
27
+ if not hasattr(item, field):
28
+ raise RuntimeError("missing %s in %s" % (field, shard["file"]))
29
+ if int(item.x.size(0)) != int(item.num_atoms.item()) or int(item.pos.size(0)) != int(item.num_atoms.item()):
30
+ raise RuntimeError("atom/coordinate mismatch in %s" % shard["file"])
31
+ checked += 1
32
+ print("SHARD_SMOKE_OK records_checked=%d identity=%s" % (checked, manifest["identity"]["sha256"]))
33
+
34
+
35
+ if __name__ == "__main__":
36
+ main()
@@ -0,0 +1 @@
1
+ """Validation helpers copied from the active MANIMOL project."""
@@ -0,0 +1,105 @@
1
+ #!/usr/bin/env python3
2
+ """Evaluate ranking baselines on the fixed HIF2A conformer pools."""
3
+ from pathlib import Path
4
+ import sys
5
+ import zlib
6
+ import numpy as np
7
+ import pandas as pd
8
+ from rdkit import Chem
9
+ from rdkit.Chem import AllChem, rdFreeSASA
10
+
11
+ ROOT = Path('/mnt/liyong/MixManifold')
12
+ MANIFEST = ROOT / 'results/downstream_drugs_stage2_hif2a_20260907/manifest.csv'
13
+ RMSD = ROOT / 'results/downstream_drugs_stage2_hif2a_20260907/bioactive_enrichment/candidate_bioactive_rmsd.csv'
14
+ MODEL = ROOT / 'results/validation_20260909/hif2a_pairwise_ranker_aug_60k_s7/groupwise_geometry_pairwise.joblib'
15
+ RANKER = ROOT / 'scripts/validation/hif2a_pairwise_ranker.py'
16
+ OUT = ROOT / 'results/validation_20260909/hif2a_downstream_baselines'
17
+
18
+ sys.path.insert(0, str(RANKER.parent))
19
+ from hif2a_pairwise_ranker import read_mols, mol_features
20
+
21
+
22
+ def mmff_score(mol):
23
+ try:
24
+ props = AllChem.MMFFGetMoleculeProperties(mol, mmffVariant='MMFF94s')
25
+ if props is None:
26
+ return np.nan
27
+ ff = AllChem.MMFFGetMoleculeForceField(mol, props, confId=0)
28
+ return -float(ff.CalcEnergy()) if ff is not None else np.nan
29
+ except Exception:
30
+ return np.nan
31
+
32
+
33
+ def sasa_score(mol):
34
+ try:
35
+ # Use coordinate-dependent FreeSASA; LabuteASA is essentially
36
+ # topology-dependent and cannot rank conformers of one molecule.
37
+ radii = rdFreeSASA.classifyAtoms(mol)
38
+ area = rdFreeSASA.CalcSASA(mol, radii, confIdx=0)
39
+ return -float(area)
40
+ except Exception:
41
+ return np.nan
42
+
43
+
44
+ def main():
45
+ OUT.mkdir(parents=True, exist_ok=True)
46
+ import joblib
47
+ ranker = joblib.load(MODEL)
48
+ rmsd = pd.read_csv(RMSD)
49
+ rmap = {(str(r.complex_id), str(r.method).upper(), int(r.candidate_index)): float(r.bioactive_rmsd)
50
+ for r in rmsd.itertuples()}
51
+ rows = []
52
+ manifest = pd.read_csv(MANIFEST)
53
+ for item in manifest.itertuples(index=False):
54
+ cid = str(item.complex_id)
55
+ paths = {
56
+ 'ETKDG': ROOT / 'outputs/docking/hif2a_conformers' / f'{cid}_etkdg.sdf',
57
+ 'MANIMOL': ROOT / 'results/downstream_drugs_stage2_hif2a_20260907/manimol_sdf' / f'{cid}.sdf'}
58
+ for source, path in paths.items():
59
+ mols = read_mols(path)[:20]
60
+ if len(mols) < 20:
61
+ continue
62
+ bio = np.array([rmap.get((cid, source, k), rmap.get((cid, source, k + 1), np.nan))
63
+ for k in range(len(mols))], dtype=float)
64
+ bio_score = ranker.decision_function(np.vstack([mol_features(m, augmented=True) for m in mols]))
65
+ mmff = np.array([mmff_score(m) for m in mols])
66
+ sasa = np.array([sasa_score(m) for m in mols])
67
+ rng = np.random.default_rng(zlib.crc32(cid.encode()) + (0 if source == 'ETKDG' else 100000))
68
+ random_score = rng.random(len(mols))
69
+ scores = {
70
+ f'{source}-Original': -np.arange(len(mols), dtype=float),
71
+ f'{source}-Random': random_score,
72
+ f'{source}-MMFF94s': np.nan_to_num(mmff, nan=-1e12),
73
+ f'{source}-SASA': np.nan_to_num(sasa, nan=-1e12),
74
+ f'{source}-BioRank': bio_score,
75
+ f'{source}-Oracle': -bio,
76
+ }
77
+ for method, score in scores.items():
78
+ for k, (s, r) in enumerate(zip(score, bio)):
79
+ rows.append({'complex_id': cid, 'source': source, 'method': method,
80
+ 'candidate_index': k, 'score': float(s), 'bioactive_rmsd': float(r)})
81
+ cand = pd.DataFrame(rows)
82
+ cand.to_csv(OUT / 'candidate_scores.csv', index=False)
83
+ summary = []
84
+ for method, group in cand.groupby('method'):
85
+ best = []
86
+ for k in [1, 5, 10, 20]:
87
+ values = []
88
+ for _, ligand in group.groupby('complex_id'):
89
+ top = ligand.sort_values('score', ascending=False).head(k)
90
+ values.append(float(top.bioactive_rmsd.min()))
91
+ arr = np.asarray(values)
92
+ summary.append({'method': method, 'K': k, 'n_ligands': len(arr),
93
+ 'best_rmsd_mean': float(arr.mean()),
94
+ 'best_rmsd_median': float(np.median(arr)),
95
+ 'hit_lt_1.0': float(np.mean(arr < 1.0)),
96
+ 'hit_lt_0.75': float(np.mean(arr < .75)),
97
+ 'hit_lt_0.5': float(np.mean(arr < .5))})
98
+ result = pd.DataFrame(summary)
99
+ result.to_csv(OUT / 'summary.csv', index=False)
100
+ print(result.to_string(index=False))
101
+ print({'n_ligands': int(cand.complex_id.nunique()), 'n_rows': len(cand), 'output': str(OUT)})
102
+
103
+
104
+ if __name__ == '__main__':
105
+ main()
@@ -0,0 +1,218 @@
1
+ #!/usr/bin/env python3
2
+ """Within-molecule bioactive ranking diagnostic on fixed HIF2A pools."""
3
+ from pathlib import Path
4
+ import time
5
+ import argparse
6
+ import numpy as np
7
+ import pandas as pd
8
+ import torch
9
+ from rdkit import Chem
10
+ from scipy.stats import spearmanr
11
+ from sklearn.linear_model import LogisticRegression
12
+ from sklearn.pipeline import make_pipeline
13
+ from sklearn.preprocessing import StandardScaler
14
+
15
+ ROOT = Path('/mnt/liyong/MixManifold')
16
+ DATA = ROOT / 'data/pdbbind_bioactive_atnn/unpacked/data/processed/pdbbind_data.pt'
17
+ IDS = ROOT / 'data/pdbbind_bioactive_atnn/unpacked/data/pyg_mol_ids.csv'
18
+ SPLIT = ROOT / 'data/pdbbind_bioactive_atnn/unpacked/data/splits/scaffold/0'
19
+ MANIFEST = ROOT / 'results/downstream_drugs_stage2_hif2a_20260907/manifest.csv'
20
+ RMSD = ROOT / 'results/downstream_drugs_stage2_hif2a_20260907/bioactive_enrichment/candidate_bioactive_rmsd.csv'
21
+ OUT = ROOT / 'results/validation_20260909/hif2a_pairwise_ranker'
22
+
23
+ def feat_xyz(xyz, atom_numbers=None, bond_pairs=None):
24
+ d = np.linalg.norm(xyz[:, None, :] - xyz[None, :, :], axis=-1)
25
+ tri = d[np.triu_indices(len(xyz), 1)]
26
+ if len(tri) == 0:
27
+ tri = np.array([0.0])
28
+ xc = xyz - xyz.mean(0)
29
+ sv = np.linalg.svd(xc, compute_uv=False)
30
+ sv = np.pad(sv, (0, max(0, 3-len(sv))))[:3]
31
+ rg = np.sqrt(np.mean(np.sum(xc * xc, axis=1)))
32
+ base = np.array([len(xyz), tri.mean(), tri.std(), np.quantile(tri, .10),
33
+ np.quantile(tri, .25), np.median(tri), np.quantile(tri, .75),
34
+ np.quantile(tri, .90), tri.max(), rg, sv[0]/max(sv[1], 1e-8),
35
+ sv[1]/max(sv[2], 1e-8), sv[2], sv[0]], dtype=float)
36
+ if atom_numbers is None:
37
+ return base
38
+ # Add composition and local covalent-geometry summaries. These features
39
+ # are cheap, invariant to rigid motions, and remain available at inference.
40
+ bins = np.array([1, 6, 7, 8, 9, 15, 16, 17, 35, 53], dtype=float)
41
+ z = np.asarray(atom_numbers, dtype=float).reshape(-1)
42
+ comp = np.array([(z == b).sum() for b in bins] +
43
+ [np.sum(~np.isin(z, bins))], dtype=float) / max(len(z), 1)
44
+ if bond_pairs is None or len(bond_pairs) == 0:
45
+ bond = np.zeros(6, dtype=float)
46
+ else:
47
+ edges = np.asarray(bond_pairs, dtype=int)
48
+ bl = np.linalg.norm(xyz[edges[:, 0]] - xyz[edges[:, 1]], axis=1)
49
+ bond = np.array([bl.mean(), bl.std(), np.quantile(bl, .10),
50
+ np.median(bl), np.quantile(bl, .90), bl.max()], dtype=float)
51
+ return np.concatenate([base, comp, bond])
52
+
53
+ def mol_features(mol, augmented=False):
54
+ xyz = np.asarray(mol.GetConformer().GetPositions(), dtype=float)
55
+ if not augmented:
56
+ return feat_xyz(xyz)
57
+ z = [a.GetAtomicNum() for a in mol.GetAtoms()]
58
+ bonds = np.array([[b.GetBeginAtomIdx(), b.GetEndAtomIdx()] for b in mol.GetBonds()], dtype=int)
59
+ return feat_xyz(xyz, z, bonds)
60
+
61
+ def read_mols(path):
62
+ return [m for m in Chem.SDMolSupplier(str(path), removeHs=False, sanitize=True) if m is not None]
63
+
64
+ def load_train(limit=5000, cache_dir=None, chunk=500, split='train', augmented=False):
65
+ ids = pd.read_csv(IDS)
66
+ col = 'mol_id' if 'mol_id' in ids.columns else ids.columns[0]
67
+ idx = {str(v): i for i, v in enumerate(ids[col].astype(str))}
68
+ data, slices = torch.load(DATA, map_location='cpu')
69
+ df = pd.read_csv(SPLIT / f'{split}_rmsds.csv').head(limit)
70
+ xs, ys, groups = [], [], []
71
+ cache_dir = Path(cache_dir) if cache_dir else None
72
+ if cache_dir:
73
+ cache_dir.mkdir(parents=True, exist_ok=True)
74
+ started = time.time()
75
+ records = df.to_dict('records')
76
+ for start in range(0, len(records), chunk):
77
+ part = cache_dir / f'features_{start:06d}_{min(start+chunk, len(records)):06d}.npz' if cache_dir else None
78
+ if part is not None and part.exists():
79
+ cached = np.load(part, allow_pickle=True)
80
+ xs.extend(cached['x']); ys.extend(cached['y']); groups.extend(cached['g'].astype(str))
81
+ print(f'features {min(start+chunk, len(records))}/{len(records)} ({min(start+chunk, len(records))/len(records):.1%}) cached', flush=True)
82
+ continue
83
+ bx, by, bg = [], [], []
84
+ for raw in records[start:start+chunk]:
85
+ i = idx.get(str(raw['mol_id']))
86
+ if i is None:
87
+ continue
88
+ a, b = int(slices['pos'][i]), int(slices['pos'][i+1])
89
+ atom_z = data.x[a:b].view(-1).numpy()
90
+ e0, e1 = int(slices['edge_index'][i]), int(slices['edge_index'][i+1])
91
+ # The processed PyG file stores edge indices local to each graph;
92
+ # only the slice offset is global.
93
+ edges = data.edge_index[:, e0:e1].t().numpy()
94
+ # PyG molecular graphs usually store each bond in both directions.
95
+ edges = edges[edges[:, 0] < edges[:, 1]]
96
+ bx.append(feat_xyz(data.pos[a:b].numpy(), atom_z, edges) if augmented
97
+ else feat_xyz(data.pos[a:b].numpy()))
98
+ by.append(float(raw['rmsd']))
99
+ bg.append(str(raw['mol_id']).split('__')[0])
100
+ xs.extend(bx); ys.extend(by); groups.extend(bg)
101
+ if part is not None:
102
+ np.savez_compressed(part, x=np.asarray(bx), y=np.asarray(by), g=np.asarray(bg))
103
+ done = min(start+chunk, len(records))
104
+ rate = done / max(time.time()-started, 1e-6)
105
+ eta = (len(records)-done) / max(rate, 1e-6)
106
+ print(f'features {done}/{len(records)} ({done/len(records):.1%}), ETA {eta/60:.1f} min', flush=True)
107
+ return np.vstack(xs), np.asarray(ys), np.asarray(groups)
108
+
109
+ def fit_pairwise(x, y, groups, seed=7):
110
+ rng = np.random.default_rng(seed)
111
+ diffs, labels = [], []
112
+ for group in np.unique(groups):
113
+ ix = np.flatnonzero(groups == group)
114
+ if len(ix) < 2:
115
+ continue
116
+ max_pairs = min(12, len(ix) * (len(ix)-1) // 2)
117
+ # Deterministic bounded pairing avoids an infinite loop when a group
118
+ # contains tied RMSD values.
119
+ added = 0
120
+ shuffled = ix.copy()
121
+ rng.shuffle(shuffled)
122
+ for ai, a in enumerate(shuffled[:-1]):
123
+ for b in shuffled[ai+1:]:
124
+ if y[a] == y[b]:
125
+ continue
126
+ first, second = (a, b) if y[a] < y[b] else (b, a)
127
+ d = x[first] - x[second]
128
+ diffs.extend([d, -d])
129
+ labels.extend([1, 0])
130
+ added += 1
131
+ if added >= max_pairs:
132
+ break
133
+ if added >= max_pairs:
134
+ break
135
+ model = make_pipeline(StandardScaler(), LogisticRegression(C=1.0, max_iter=300, random_state=seed))
136
+ model.fit(np.asarray(diffs), np.asarray(labels))
137
+ return model, len(labels)
138
+
139
+ def main():
140
+ parser = argparse.ArgumentParser()
141
+ parser.add_argument('--train-limit', type=int, default=5000)
142
+ parser.add_argument('--cache-dir', default=str(OUT / 'cache'))
143
+ parser.add_argument('--seed', type=int, default=7)
144
+ parser.add_argument('--out-tag', default='hif2a_pairwise_ranker')
145
+ parser.add_argument('--val-limit', type=int, default=8000)
146
+ parser.add_argument('--val-cache-dir', default=str(OUT / 'val_cache'))
147
+ parser.add_argument('--augmented', action='store_true',
148
+ help='include atom-composition and bond-length features')
149
+ args = parser.parse_args()
150
+ out_dir = ROOT / 'results/validation_20260909' / args.out_tag
151
+ out_dir.mkdir(parents=True, exist_ok=True)
152
+ t0 = time.time()
153
+ x, y, groups = load_train(args.train_limit, args.cache_dir, split='train', augmented=args.augmented)
154
+ model, npairs = fit_pairwise(x, y, groups, seed=args.seed)
155
+ import joblib
156
+ joblib.dump(model, out_dir / 'groupwise_geometry_pairwise.joblib')
157
+ print({'train_records': len(x), 'pair_examples': npairs}, flush=True)
158
+
159
+ vx, vy, vg = load_train(args.val_limit, args.val_cache_dir, split='val', augmented=args.augmented)
160
+ val_scores = model.decision_function(vx)
161
+ val_pairs = []
162
+ for group in np.unique(vg):
163
+ ix = np.flatnonzero(vg == group)
164
+ for ai, a in enumerate(ix[:-1]):
165
+ for b in ix[ai+1:]:
166
+ if vy[a] != vy[b]:
167
+ first, second = (a, b) if vy[a] < vy[b] else (b, a)
168
+ val_pairs.append(float(val_scores[first] > val_scores[second]))
169
+ print({'validation_records': len(vx), 'validation_pair_accuracy': float(np.mean(val_pairs)),
170
+ 'validation_pairs': len(val_pairs)}, flush=True)
171
+ pd.DataFrame([{'validation_records': len(vx), 'validation_pairs': len(val_pairs),
172
+ 'validation_pair_accuracy': float(np.mean(val_pairs))}]).to_csv(
173
+ out_dir / 'validation_metrics.csv', index=False)
174
+
175
+ rmsd = pd.read_csv(RMSD)
176
+ rmap = {(str(r.complex_id), str(r.method).upper(), int(r.candidate_index)): float(r.bioactive_rmsd)
177
+ for r in rmsd.itertuples()}
178
+ rows = []
179
+ for item in pd.read_csv(MANIFEST).itertuples(index=False):
180
+ cid = str(item.complex_id)
181
+ paths = {
182
+ 'ETKDG': ROOT / 'outputs/docking/hif2a_conformers' / f'{cid}_etkdg.sdf',
183
+ 'MANIMOL': ROOT / 'results/downstream_drugs_stage2_hif2a_20260907/manimol_sdf' / f'{cid}.sdf'}
184
+ for method, path in paths.items():
185
+ mols = read_mols(path)
186
+ if len(mols) < 20:
187
+ continue
188
+ # Use the continuous ranking score; class predictions collapse all
189
+ # candidates to 0/1 and cannot define an ordering.
190
+ scores = model.decision_function(np.vstack([mol_features(m, augmented=args.augmented) for m in mols[:20]]))
191
+ for k, score in enumerate(scores):
192
+ actual = rmap.get((cid, method, k), rmap.get((cid, method, k+1)))
193
+ rows.append({'complex_id': cid, 'method': method, 'candidate_index': k,
194
+ 'bioactive_rmsd': actual, 'pairwise_score': float(score)})
195
+ cand = pd.DataFrame(rows)
196
+ cand.to_csv(out_dir / 'candidate_scores.csv', index=False)
197
+
198
+ summary = []
199
+ for method in ['ETKDG', 'MANIMOL']:
200
+ sm = cand[cand.method == method]
201
+ rho = spearmanr(sm.pairwise_score, -sm.bioactive_rmsd).statistic
202
+ for k in [1, 5, 10, 20]:
203
+ best = []
204
+ for _, g in sm.groupby('complex_id'):
205
+ top = g.sort_values('pairwise_score', ascending=False).head(k)
206
+ best.append(float(top.bioactive_rmsd.min()))
207
+ arr = np.asarray(best)
208
+ summary.append({'method': method, 'K': k, 'n_ligands': len(arr),
209
+ 'candidate_spearman': float(rho), 'best_rmsd_mean': float(arr.mean()),
210
+ 'best_rmsd_median': float(np.median(arr)), 'hit_lt_1.0': float(np.mean(arr < 1.0)),
211
+ 'hit_lt_0.75': float(np.mean(arr < .75)), 'hit_lt_0.5': float(np.mean(arr < .5))})
212
+ result = pd.DataFrame(summary)
213
+ result.to_csv(out_dir / 'summary.csv', index=False)
214
+ print(result.to_string(index=False), flush=True)
215
+ print({'output': str(out_dir), 'seconds': time.time()-t0}, flush=True)
216
+
217
+ if __name__ == '__main__':
218
+ main()
@@ -0,0 +1,47 @@
1
+ #!/usr/bin/env python3
2
+ """Validate rendering outputs without modifying receptor or ligand inputs."""
3
+
4
+ from __future__ import annotations
5
+
6
+ import argparse
7
+ import json
8
+ from pathlib import Path
9
+
10
+ from PIL import Image
11
+
12
+
13
+ def main() -> None:
14
+ parser = argparse.ArgumentParser(description=__doc__)
15
+ parser.add_argument("--render-dir", default="outputs/binding_mode_figure")
16
+ parser.add_argument("--figure", default="outputs/binding_modes.png")
17
+ parser.add_argument("--expected-width", type=int, default=1800)
18
+ parser.add_argument("--expected-height", type=int, default=1000)
19
+ args = parser.parse_args()
20
+ root = Path(args.render_dir)
21
+ checks = {"systems": {}, "figure": {}, "ok": True}
22
+ for panel_file in sorted(root.glob("*/panels.json")):
23
+ system = panel_file.parent.name
24
+ panels = json.loads(panel_file.read_text())
25
+ dimensions = [Image.open(item["path"]).size for item in panels]
26
+ camera = panel_file.parent / "camera_view.json"
27
+ system_ok = bool(camera.exists()) and all(size == (args.expected_width, args.expected_height) for size in dimensions)
28
+ checks["systems"][system] = {
29
+ "panel_count": len(panels), "dimensions": dimensions,
30
+ "shared_camera_file": str(camera), "camera_present": camera.exists(), "ok": system_ok,
31
+ }
32
+ checks["ok"] = checks["ok"] and system_ok
33
+ figure = Path(args.figure)
34
+ checks["figure"] = {"path": str(figure), "exists": figure.exists()}
35
+ if figure.exists():
36
+ checks["figure"]["dimensions"] = Image.open(figure).size
37
+ else:
38
+ checks["ok"] = False
39
+ out = root / "verification_report.json"
40
+ out.write_text(json.dumps(checks, indent=2) + "\n")
41
+ print(f"Saved {out}; ok={checks['ok']}")
42
+ if not checks["ok"]:
43
+ raise SystemExit(1)
44
+
45
+
46
+ if __name__ == "__main__":
47
+ main()