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,472 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""Prepare GEOM-Drugs with the GeoMol split and top-energy conformer caps.
|
|
3
|
+
|
|
4
|
+
The script is intentionally isolated from the legacy MANIMOL preprocessing.
|
|
5
|
+
It downloads the official GEOM RDKit archive, obtains GeoMol's split0.npy,
|
|
6
|
+
and writes MANIMOL-compatible raw .pt files:
|
|
7
|
+
|
|
8
|
+
geom_drugs_geomol_top20_train.pt
|
|
9
|
+
geom_drugs_geomol_top20_val.pt
|
|
10
|
+
geom_drugs_geomol_top20_test.pt
|
|
11
|
+
|
|
12
|
+
Train/validation molecules keep the lowest-energy top-N conformers. Test
|
|
13
|
+
molecules retain every available conformer.
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
from __future__ import annotations
|
|
17
|
+
|
|
18
|
+
import argparse
|
|
19
|
+
import csv
|
|
20
|
+
import json
|
|
21
|
+
import math
|
|
22
|
+
import os
|
|
23
|
+
import pickle
|
|
24
|
+
import shutil
|
|
25
|
+
import subprocess
|
|
26
|
+
import sys
|
|
27
|
+
import tarfile
|
|
28
|
+
import time
|
|
29
|
+
import urllib.error
|
|
30
|
+
import urllib.request
|
|
31
|
+
from pathlib import Path
|
|
32
|
+
from typing import Any, Dict, Iterable, List, Optional, Sequence, Tuple
|
|
33
|
+
|
|
34
|
+
GEOM_RDKIT_URL = "https://dataverse.harvard.edu/api/access/datafile/4327252"
|
|
35
|
+
GEOMOL_REPO_URL = "https://github.com/PattanaikL/GeoMol.git"
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def require_deps() -> Tuple[Any, Any, Any]:
|
|
39
|
+
missing = []
|
|
40
|
+
try:
|
|
41
|
+
import numpy as np
|
|
42
|
+
except ImportError:
|
|
43
|
+
np = None
|
|
44
|
+
missing.append("numpy")
|
|
45
|
+
try:
|
|
46
|
+
import torch
|
|
47
|
+
except ImportError:
|
|
48
|
+
torch = None
|
|
49
|
+
missing.append("torch")
|
|
50
|
+
try:
|
|
51
|
+
from rdkit import Chem, RDLogger
|
|
52
|
+
except ImportError:
|
|
53
|
+
Chem = None
|
|
54
|
+
RDLogger = None
|
|
55
|
+
missing.append("rdkit")
|
|
56
|
+
if missing:
|
|
57
|
+
raise SystemExit("Missing required packages: " + ", ".join(missing))
|
|
58
|
+
RDLogger.DisableLog("rdApp.warning")
|
|
59
|
+
return np, torch, Chem
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
NP, TORCH, CHEM = require_deps()
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def human_bytes(value: int) -> str:
|
|
66
|
+
units = ["B", "KiB", "MiB", "GiB", "TiB"]
|
|
67
|
+
amount = float(value)
|
|
68
|
+
for unit in units:
|
|
69
|
+
if amount < 1024 or unit == units[-1]:
|
|
70
|
+
return f"{amount:.2f} {unit}"
|
|
71
|
+
amount /= 1024.0
|
|
72
|
+
return f"{value} B"
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def download_with_resume(url: str, destination: Path) -> None:
|
|
76
|
+
destination.parent.mkdir(parents=True, exist_ok=True)
|
|
77
|
+
partial = destination.with_suffix(destination.suffix + ".part")
|
|
78
|
+
if destination.exists() and destination.stat().st_size > 0:
|
|
79
|
+
print(f"[download] exists, skipping: {destination}")
|
|
80
|
+
return
|
|
81
|
+
|
|
82
|
+
existing = partial.stat().st_size if partial.exists() else 0
|
|
83
|
+
headers = {"User-Agent": "manimol-geom-prep/1.0"}
|
|
84
|
+
if existing:
|
|
85
|
+
headers["Range"] = f"bytes={existing}-"
|
|
86
|
+
|
|
87
|
+
request = urllib.request.Request(url, headers=headers)
|
|
88
|
+
try:
|
|
89
|
+
response = urllib.request.urlopen(request, timeout=120)
|
|
90
|
+
except urllib.error.HTTPError as exc:
|
|
91
|
+
if exc.code == 416 and partial.exists():
|
|
92
|
+
partial.replace(destination)
|
|
93
|
+
return
|
|
94
|
+
raise
|
|
95
|
+
|
|
96
|
+
status = getattr(response, "status", 200)
|
|
97
|
+
if existing and status != 206:
|
|
98
|
+
partial.unlink(missing_ok=True)
|
|
99
|
+
existing = 0
|
|
100
|
+
|
|
101
|
+
total_header = response.headers.get("Content-Length")
|
|
102
|
+
total = int(total_header) + existing if total_header else None
|
|
103
|
+
downloaded = existing
|
|
104
|
+
last_report = time.time()
|
|
105
|
+
mode = "ab" if existing else "wb"
|
|
106
|
+
print(f"[download] {url}\n -> {destination}")
|
|
107
|
+
with partial.open(mode) as fh:
|
|
108
|
+
while True:
|
|
109
|
+
chunk = response.read(8 * 1024 * 1024)
|
|
110
|
+
if not chunk:
|
|
111
|
+
break
|
|
112
|
+
fh.write(chunk)
|
|
113
|
+
downloaded += len(chunk)
|
|
114
|
+
now = time.time()
|
|
115
|
+
if now - last_report > 5:
|
|
116
|
+
if total:
|
|
117
|
+
print(
|
|
118
|
+
f"\r {human_bytes(downloaded)} / "
|
|
119
|
+
f"{human_bytes(total)}",
|
|
120
|
+
end="",
|
|
121
|
+
flush=True,
|
|
122
|
+
)
|
|
123
|
+
else:
|
|
124
|
+
print(f"\r {human_bytes(downloaded)}", end="", flush=True)
|
|
125
|
+
last_report = now
|
|
126
|
+
print()
|
|
127
|
+
partial.replace(destination)
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
def safe_extract_tar(archive: Path, destination: Path) -> None:
|
|
131
|
+
marker = destination / ".geom_rdkit_extracted"
|
|
132
|
+
if marker.exists():
|
|
133
|
+
print(f"[extract] exists, skipping: {destination}")
|
|
134
|
+
return
|
|
135
|
+
destination.mkdir(parents=True, exist_ok=True)
|
|
136
|
+
print(f"[extract] {archive} -> {destination}")
|
|
137
|
+
with tarfile.open(archive, mode="r:*") as tf:
|
|
138
|
+
root = destination.resolve()
|
|
139
|
+
for member in tf.getmembers():
|
|
140
|
+
target = (destination / member.name).resolve()
|
|
141
|
+
if target != root and root not in target.parents:
|
|
142
|
+
raise RuntimeError(f"Unsafe tar path: {member.name}")
|
|
143
|
+
tf.extractall(destination)
|
|
144
|
+
marker.touch()
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
def ensure_geomol_repo(repo_dir: Path) -> None:
|
|
148
|
+
if (repo_dir / ".git").exists():
|
|
149
|
+
print(f"[geomol] repo exists: {repo_dir}")
|
|
150
|
+
return
|
|
151
|
+
repo_dir.parent.mkdir(parents=True, exist_ok=True)
|
|
152
|
+
print(f"[geomol] cloning {GEOMOL_REPO_URL} -> {repo_dir}")
|
|
153
|
+
subprocess.run(
|
|
154
|
+
["git", "clone", "--depth", "1", GEOMOL_REPO_URL, str(repo_dir)],
|
|
155
|
+
check=True,
|
|
156
|
+
)
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
def find_drugs_pickle_dir(root: Path) -> Path:
|
|
160
|
+
candidates: List[Tuple[int, Path]] = []
|
|
161
|
+
for directory, _, files in os.walk(root):
|
|
162
|
+
count = sum(1 for name in files if name.endswith(".pickle"))
|
|
163
|
+
if count:
|
|
164
|
+
path = Path(directory)
|
|
165
|
+
score = count + (1000000 if "drug" in str(path).lower() else 0)
|
|
166
|
+
candidates.append((score, path))
|
|
167
|
+
if not candidates:
|
|
168
|
+
raise FileNotFoundError(f"No .pickle files found under {root}")
|
|
169
|
+
candidates.sort(key=lambda item: item[0], reverse=True)
|
|
170
|
+
chosen = candidates[0][1]
|
|
171
|
+
print(f"[locate] drugs pickle directory: {chosen}")
|
|
172
|
+
print(f"[locate] pickle files: {sum(1 for _ in chosen.glob('*.pickle')):,}")
|
|
173
|
+
return chosen
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
def load_pickle(path: Path) -> Any:
|
|
177
|
+
with path.open("rb") as fh:
|
|
178
|
+
return pickle.load(fh)
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
def get_conformers(mol_dict: Dict[str, Any]) -> List[Dict[str, Any]]:
|
|
182
|
+
confs = mol_dict.get("conformers", [])
|
|
183
|
+
if not isinstance(confs, list):
|
|
184
|
+
return []
|
|
185
|
+
return confs
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
def conformer_energy(conf: Dict[str, Any]) -> float:
|
|
189
|
+
for key in ("totalenergy", "total_energy", "energy"):
|
|
190
|
+
value = conf.get(key)
|
|
191
|
+
if value is not None:
|
|
192
|
+
try:
|
|
193
|
+
value = float(value)
|
|
194
|
+
if math.isfinite(value):
|
|
195
|
+
return value
|
|
196
|
+
except (TypeError, ValueError):
|
|
197
|
+
pass
|
|
198
|
+
weight = conf.get("boltzmannweight")
|
|
199
|
+
try:
|
|
200
|
+
weight = float(weight)
|
|
201
|
+
if math.isfinite(weight):
|
|
202
|
+
return -weight
|
|
203
|
+
except (TypeError, ValueError):
|
|
204
|
+
pass
|
|
205
|
+
return float("inf")
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
def smiles_from_mol_dict(mol_dict: Dict[str, Any], mol: Any) -> str:
|
|
209
|
+
for key in ("smiles", "smile", "canonical_smiles", "name"):
|
|
210
|
+
value = mol_dict.get(key)
|
|
211
|
+
if isinstance(value, str) and value:
|
|
212
|
+
return value
|
|
213
|
+
return CHEM.MolToSmiles(mol, isomericSmiles=True)
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
def make_record(
|
|
217
|
+
source_file: Path,
|
|
218
|
+
mol_dict: Dict[str, Any],
|
|
219
|
+
conf: Dict[str, Any],
|
|
220
|
+
split: str,
|
|
221
|
+
local_mol_id: int,
|
|
222
|
+
conf_rank: int,
|
|
223
|
+
) -> Optional[Dict[str, Any]]:
|
|
224
|
+
mol = conf.get("rd_mol")
|
|
225
|
+
if mol is None:
|
|
226
|
+
return None
|
|
227
|
+
if mol.GetNumConformers() < 1:
|
|
228
|
+
return None
|
|
229
|
+
pos = TORCH.as_tensor(mol.GetConformer().GetPositions(), dtype=TORCH.float32)
|
|
230
|
+
if pos.dim() != 2 or pos.size(0) != mol.GetNumAtoms():
|
|
231
|
+
return None
|
|
232
|
+
return {
|
|
233
|
+
"rdmol": mol,
|
|
234
|
+
"smiles": smiles_from_mol_dict(mol_dict, mol),
|
|
235
|
+
"pos": pos,
|
|
236
|
+
"boltzmannweight": float(conf.get("boltzmannweight", float("nan"))),
|
|
237
|
+
"totalenergy": float(conformer_energy(conf)),
|
|
238
|
+
"source_file": source_file.name,
|
|
239
|
+
"split": split,
|
|
240
|
+
"source_mol_id": int(local_mol_id),
|
|
241
|
+
"conf_rank_by_energy": int(conf_rank),
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
|
|
245
|
+
def process_split(
|
|
246
|
+
split_name: str,
|
|
247
|
+
indices: Sequence[int],
|
|
248
|
+
pickle_files: Sequence[Path],
|
|
249
|
+
max_confs: int,
|
|
250
|
+
) -> Tuple[List[Dict[str, Any]], Dict[str, Any]]:
|
|
251
|
+
records: List[Dict[str, Any]] = []
|
|
252
|
+
rows: List[Dict[str, Any]] = []
|
|
253
|
+
try:
|
|
254
|
+
from tqdm import tqdm
|
|
255
|
+
except ImportError:
|
|
256
|
+
tqdm = None
|
|
257
|
+
|
|
258
|
+
iterator: Iterable[int] = indices
|
|
259
|
+
if tqdm:
|
|
260
|
+
iterator = tqdm(indices, desc=f"process {split_name}", total=len(indices))
|
|
261
|
+
|
|
262
|
+
for out_mol_id, raw_index in enumerate(iterator):
|
|
263
|
+
raw_index = int(raw_index)
|
|
264
|
+
source_file = pickle_files[raw_index]
|
|
265
|
+
status = "ok"
|
|
266
|
+
kept = 0
|
|
267
|
+
available = 0
|
|
268
|
+
smiles = ""
|
|
269
|
+
try:
|
|
270
|
+
mol_dict = load_pickle(source_file)
|
|
271
|
+
confs = get_conformers(mol_dict)
|
|
272
|
+
available = len(confs)
|
|
273
|
+
confs = sorted(confs, key=conformer_energy)
|
|
274
|
+
if split_name in {"train", "val"} and max_confs > 0:
|
|
275
|
+
confs = confs[:max_confs]
|
|
276
|
+
for conf_rank, conf in enumerate(confs):
|
|
277
|
+
record = make_record(
|
|
278
|
+
source_file,
|
|
279
|
+
mol_dict,
|
|
280
|
+
conf,
|
|
281
|
+
split_name,
|
|
282
|
+
out_mol_id,
|
|
283
|
+
conf_rank,
|
|
284
|
+
)
|
|
285
|
+
if record is None:
|
|
286
|
+
continue
|
|
287
|
+
if not smiles:
|
|
288
|
+
smiles = str(record["smiles"])
|
|
289
|
+
records.append(record)
|
|
290
|
+
kept += 1
|
|
291
|
+
if kept == 0:
|
|
292
|
+
status = "skip_no_valid_conformer"
|
|
293
|
+
except Exception as exc:
|
|
294
|
+
status = "error"
|
|
295
|
+
rows.append(
|
|
296
|
+
{
|
|
297
|
+
"split": split_name,
|
|
298
|
+
"raw_index": raw_index,
|
|
299
|
+
"source_file": source_file.name,
|
|
300
|
+
"status": status,
|
|
301
|
+
"available_confs": available,
|
|
302
|
+
"kept_confs": kept,
|
|
303
|
+
"smiles": smiles,
|
|
304
|
+
"error": repr(exc),
|
|
305
|
+
}
|
|
306
|
+
)
|
|
307
|
+
continue
|
|
308
|
+
rows.append(
|
|
309
|
+
{
|
|
310
|
+
"split": split_name,
|
|
311
|
+
"raw_index": raw_index,
|
|
312
|
+
"source_file": source_file.name,
|
|
313
|
+
"status": status,
|
|
314
|
+
"available_confs": available,
|
|
315
|
+
"kept_confs": kept,
|
|
316
|
+
"smiles": smiles,
|
|
317
|
+
"error": "",
|
|
318
|
+
}
|
|
319
|
+
)
|
|
320
|
+
summary = {
|
|
321
|
+
"split": split_name,
|
|
322
|
+
"molecules_requested": len(indices),
|
|
323
|
+
"molecules_ok": sum(1 for row in rows if row["status"] == "ok"),
|
|
324
|
+
"conformers_written": len(records),
|
|
325
|
+
"min_kept": min((int(row["kept_confs"]) for row in rows if row["status"] == "ok"), default=0),
|
|
326
|
+
"max_kept": max((int(row["kept_confs"]) for row in rows if row["status"] == "ok"), default=0),
|
|
327
|
+
}
|
|
328
|
+
return records, {"summary": summary, "rows": rows}
|
|
329
|
+
|
|
330
|
+
|
|
331
|
+
def save_rows_csv(path: Path, rows: Sequence[Dict[str, Any]]) -> None:
|
|
332
|
+
path.parent.mkdir(parents=True, exist_ok=True)
|
|
333
|
+
fieldnames = [
|
|
334
|
+
"split",
|
|
335
|
+
"raw_index",
|
|
336
|
+
"source_file",
|
|
337
|
+
"status",
|
|
338
|
+
"available_confs",
|
|
339
|
+
"kept_confs",
|
|
340
|
+
"smiles",
|
|
341
|
+
"error",
|
|
342
|
+
]
|
|
343
|
+
with path.open("w", newline="") as fh:
|
|
344
|
+
writer = csv.DictWriter(fh, fieldnames=fieldnames)
|
|
345
|
+
writer.writeheader()
|
|
346
|
+
writer.writerows(rows)
|
|
347
|
+
|
|
348
|
+
|
|
349
|
+
def parse_args() -> argparse.Namespace:
|
|
350
|
+
parser = argparse.ArgumentParser(
|
|
351
|
+
formatter_class=argparse.ArgumentDefaultsHelpFormatter
|
|
352
|
+
)
|
|
353
|
+
parser.add_argument("--workdir", type=Path, required=True)
|
|
354
|
+
parser.add_argument("--output-dir", type=Path, default=None)
|
|
355
|
+
parser.add_argument("--reports-dir", type=Path, default=None)
|
|
356
|
+
parser.add_argument("--output-prefix", default="geom_drugs_geomol_top20")
|
|
357
|
+
parser.add_argument("--top-train-confs", type=int, default=20)
|
|
358
|
+
parser.add_argument("--top-val-confs", type=int, default=20)
|
|
359
|
+
parser.add_argument("--geom-url", default=GEOM_RDKIT_URL)
|
|
360
|
+
parser.add_argument("--geomol-repo", type=Path, default=None)
|
|
361
|
+
parser.add_argument("--skip-download", action="store_true")
|
|
362
|
+
parser.add_argument("--skip-extract", action="store_true")
|
|
363
|
+
parser.add_argument("--overwrite", action="store_true")
|
|
364
|
+
parser.add_argument(
|
|
365
|
+
"--limit-molecules",
|
|
366
|
+
type=int,
|
|
367
|
+
default=0,
|
|
368
|
+
help="Debug only: cap molecules per split after applying split0.",
|
|
369
|
+
)
|
|
370
|
+
parser.add_argument(
|
|
371
|
+
"--test-limit",
|
|
372
|
+
type=int,
|
|
373
|
+
default=0,
|
|
374
|
+
help="Keep only the first N molecules from the test split; train/val are unchanged.",
|
|
375
|
+
)
|
|
376
|
+
parser.add_argument(
|
|
377
|
+
"--only-split",
|
|
378
|
+
choices=("all", "train", "val", "test"),
|
|
379
|
+
default="all",
|
|
380
|
+
help="Process only one split when creating an additional dataset variant.",
|
|
381
|
+
)
|
|
382
|
+
return parser.parse_args()
|
|
383
|
+
|
|
384
|
+
|
|
385
|
+
def main() -> None:
|
|
386
|
+
args = parse_args()
|
|
387
|
+
workdir = args.workdir.expanduser().resolve()
|
|
388
|
+
downloads = workdir / "downloads"
|
|
389
|
+
raw_root = workdir / "raw"
|
|
390
|
+
reports = (args.reports_dir or (workdir / "reports")).expanduser().resolve()
|
|
391
|
+
output_dir = (args.output_dir or (workdir / "processed")).expanduser().resolve()
|
|
392
|
+
geomol_repo = args.geomol_repo or (workdir / "geomol_repo" / "GeoMol")
|
|
393
|
+
|
|
394
|
+
for directory in (downloads, raw_root, reports, output_dir):
|
|
395
|
+
directory.mkdir(parents=True, exist_ok=True)
|
|
396
|
+
|
|
397
|
+
archive = downloads / "rdkit_folder.tar"
|
|
398
|
+
if not args.skip_download:
|
|
399
|
+
download_with_resume(args.geom_url, archive)
|
|
400
|
+
if not archive.exists():
|
|
401
|
+
raise FileNotFoundError(archive)
|
|
402
|
+
|
|
403
|
+
if not args.skip_extract:
|
|
404
|
+
safe_extract_tar(archive, raw_root)
|
|
405
|
+
ensure_geomol_repo(geomol_repo)
|
|
406
|
+
|
|
407
|
+
split_path = geomol_repo / "data" / "DRUGS" / "splits" / "split0.npy"
|
|
408
|
+
if not split_path.exists():
|
|
409
|
+
raise FileNotFoundError(split_path)
|
|
410
|
+
splits = NP.load(split_path, allow_pickle=True)
|
|
411
|
+
if len(splits) != 3:
|
|
412
|
+
raise RuntimeError(f"Expected split0.npy to contain 3 splits, got {len(splits)}")
|
|
413
|
+
train_idx, val_idx, test_idx = [list(map(int, split)) for split in splits]
|
|
414
|
+
if args.limit_molecules > 0:
|
|
415
|
+
train_idx = train_idx[: args.limit_molecules]
|
|
416
|
+
val_idx = val_idx[: args.limit_molecules]
|
|
417
|
+
test_idx = test_idx[: args.limit_molecules]
|
|
418
|
+
if args.test_limit > 0:
|
|
419
|
+
test_idx = test_idx[: args.test_limit]
|
|
420
|
+
|
|
421
|
+
pickle_dir = find_drugs_pickle_dir(raw_root)
|
|
422
|
+
pickle_files = sorted(pickle_dir.glob("*.pickle"))
|
|
423
|
+
max_index = max(max(train_idx), max(val_idx), max(test_idx))
|
|
424
|
+
if max_index >= len(pickle_files):
|
|
425
|
+
raise RuntimeError(
|
|
426
|
+
f"Split index {max_index} exceeds raw file count {len(pickle_files)}"
|
|
427
|
+
)
|
|
428
|
+
|
|
429
|
+
config = {
|
|
430
|
+
"geom_url": args.geom_url,
|
|
431
|
+
"geom_archive": str(archive),
|
|
432
|
+
"geomol_repo": str(geomol_repo),
|
|
433
|
+
"split_path": str(split_path),
|
|
434
|
+
"pickle_dir": str(pickle_dir),
|
|
435
|
+
"pickle_file_count": len(pickle_files),
|
|
436
|
+
"top_train_confs": args.top_train_confs,
|
|
437
|
+
"top_val_confs": args.top_val_confs,
|
|
438
|
+
"test_confs": "all",
|
|
439
|
+
"test_limit": args.test_limit,
|
|
440
|
+
"only_split": args.only_split,
|
|
441
|
+
"skip_extract": args.skip_extract,
|
|
442
|
+
"output_prefix": args.output_prefix,
|
|
443
|
+
}
|
|
444
|
+
(reports / "prepare_config.json").write_text(json.dumps(config, indent=2))
|
|
445
|
+
|
|
446
|
+
split_specs = [
|
|
447
|
+
("train", train_idx, args.top_train_confs),
|
|
448
|
+
("val", val_idx, args.top_val_confs),
|
|
449
|
+
("test", test_idx, 0),
|
|
450
|
+
]
|
|
451
|
+
summaries = []
|
|
452
|
+
for split_name, indices, cap in split_specs:
|
|
453
|
+
if args.only_split != "all" and split_name != args.only_split:
|
|
454
|
+
continue
|
|
455
|
+
out_path = output_dir / f"{args.output_prefix}_{split_name}.pt"
|
|
456
|
+
if out_path.exists() and not args.overwrite:
|
|
457
|
+
print(f"[write] exists, skipping: {out_path}")
|
|
458
|
+
continue
|
|
459
|
+
records, report = process_split(split_name, indices, pickle_files, cap)
|
|
460
|
+
TORCH.save(records, out_path)
|
|
461
|
+
save_rows_csv(reports / f"{split_name}_molecule_report.csv", report["rows"])
|
|
462
|
+
summaries.append(report["summary"])
|
|
463
|
+
print(f"[write] {out_path} records={len(records):,}")
|
|
464
|
+
|
|
465
|
+
(reports / "summary.json").write_text(json.dumps(summaries, indent=2))
|
|
466
|
+
print("[done] summaries:")
|
|
467
|
+
for summary in summaries:
|
|
468
|
+
print(summary)
|
|
469
|
+
|
|
470
|
+
|
|
471
|
+
if __name__ == "__main__":
|
|
472
|
+
main()
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""Prepare an inference-only raw cache from a PDBbind directory tree.
|
|
3
|
+
|
|
4
|
+
The crystal ligand coordinates are retained for post-hoc evaluation only. The
|
|
5
|
+
external reference-free launcher removes template conformers before geometry
|
|
6
|
+
preparation, so this cache is safe to use as an inference input.
|
|
7
|
+
"""
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import argparse
|
|
11
|
+
import json
|
|
12
|
+
from pathlib import Path
|
|
13
|
+
|
|
14
|
+
import torch
|
|
15
|
+
from rdkit import Chem
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def parse_args():
|
|
19
|
+
p = argparse.ArgumentParser()
|
|
20
|
+
p.add_argument("--pdbbind-root", required=True)
|
|
21
|
+
p.add_argument("--out-root", required=True)
|
|
22
|
+
p.add_argument("--limit", type=int, default=0)
|
|
23
|
+
return p.parse_args()
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def read_ligand(path: Path):
|
|
27
|
+
supplier = Chem.SDMolSupplier(str(path), removeHs=False, sanitize=False)
|
|
28
|
+
for mol in supplier:
|
|
29
|
+
if mol is None or mol.GetNumConformers() == 0:
|
|
30
|
+
continue
|
|
31
|
+
try:
|
|
32
|
+
Chem.SanitizeMol(mol)
|
|
33
|
+
except Exception:
|
|
34
|
+
continue
|
|
35
|
+
if mol.GetNumAtoms() == 0:
|
|
36
|
+
continue
|
|
37
|
+
return mol
|
|
38
|
+
return None
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def main():
|
|
42
|
+
args = parse_args()
|
|
43
|
+
root = Path(args.pdbbind_root).expanduser()
|
|
44
|
+
out = Path(args.out_root)
|
|
45
|
+
out.mkdir(parents=True, exist_ok=True)
|
|
46
|
+
records, attrition = [], []
|
|
47
|
+
for complex_dir in sorted(p for p in root.iterdir() if p.is_dir()):
|
|
48
|
+
pdbid = complex_dir.name
|
|
49
|
+
ligand_path = complex_dir / f"{pdbid}_ligand.sdf"
|
|
50
|
+
protein_path = complex_dir / f"{pdbid}_protein.pdb"
|
|
51
|
+
if not ligand_path.exists():
|
|
52
|
+
attrition.append({"pdbid": pdbid, "reason": "missing_ligand_sdf"})
|
|
53
|
+
continue
|
|
54
|
+
if not protein_path.exists():
|
|
55
|
+
attrition.append({"pdbid": pdbid, "reason": "missing_protein_pdb"})
|
|
56
|
+
continue
|
|
57
|
+
mol = read_ligand(ligand_path)
|
|
58
|
+
if mol is None:
|
|
59
|
+
attrition.append({"pdbid": pdbid, "reason": "invalid_or_no_3d_ligand"})
|
|
60
|
+
continue
|
|
61
|
+
smiles = Chem.MolToSmiles(mol, isomericSmiles=True)
|
|
62
|
+
if not smiles:
|
|
63
|
+
attrition.append({"pdbid": pdbid, "reason": "empty_smiles"})
|
|
64
|
+
continue
|
|
65
|
+
rec = {
|
|
66
|
+
"smiles": smiles,
|
|
67
|
+
"rdmol": Chem.Mol(mol),
|
|
68
|
+
"pos": torch.tensor(mol.GetConformer(0).GetPositions(), dtype=torch.float32),
|
|
69
|
+
"boltzmannweight": 1.0,
|
|
70
|
+
"totalenergy": 0.0,
|
|
71
|
+
"source_id": pdbid,
|
|
72
|
+
"pdbid": pdbid,
|
|
73
|
+
"protein_path": str(protein_path),
|
|
74
|
+
"ligand_path": str(ligand_path),
|
|
75
|
+
}
|
|
76
|
+
records.append(rec)
|
|
77
|
+
if args.limit > 0 and len(records) >= args.limit:
|
|
78
|
+
break
|
|
79
|
+
torch.save([], out / "pdbbind_train.pt")
|
|
80
|
+
torch.save([], out / "pdbbind_val.pt")
|
|
81
|
+
torch.save(records, out / "pdbbind_test.pt")
|
|
82
|
+
(out / "attrition.json").write_text(json.dumps(attrition, indent=2) + "\n")
|
|
83
|
+
(out / "manifest.json").write_text(
|
|
84
|
+
json.dumps({"records": len(records), "excluded": len(attrition), "source_root": str(root)}, indent=2) + "\n"
|
|
85
|
+
)
|
|
86
|
+
print(json.dumps({"records": len(records), "excluded": len(attrition), "out_root": str(out)}, indent=2))
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
if __name__ == "__main__":
|
|
90
|
+
main()
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""Build an inference-only MANIMOL raw cache from Platinum SDF.
|
|
3
|
+
|
|
4
|
+
The cache contains no training molecules. Empty train/validation files are
|
|
5
|
+
created because QM9Dataset's processing contract expects all three raw files;
|
|
6
|
+
the inference-only path skips processing those two splits.
|
|
7
|
+
"""
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import argparse
|
|
11
|
+
from pathlib import Path
|
|
12
|
+
|
|
13
|
+
import torch
|
|
14
|
+
from rdkit import Chem
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def parse_args():
|
|
18
|
+
p = argparse.ArgumentParser()
|
|
19
|
+
p.add_argument("--sdf", required=True)
|
|
20
|
+
p.add_argument("--out-root", required=True)
|
|
21
|
+
p.add_argument("--limit", type=int, default=0)
|
|
22
|
+
return p.parse_args()
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def mol_record(mol):
|
|
26
|
+
if mol is None or mol.GetNumConformers() == 0:
|
|
27
|
+
return None
|
|
28
|
+
# Platinum records are already protein-bound 3-D conformers. Keep the
|
|
29
|
+
# atom ordering and coordinates exactly as supplied by the dataset.
|
|
30
|
+
smiles = Chem.MolToSmiles(mol, isomericSmiles=True)
|
|
31
|
+
if not smiles:
|
|
32
|
+
return None
|
|
33
|
+
coords = torch.tensor(mol.GetConformer(0).GetPositions(), dtype=torch.float32)
|
|
34
|
+
return {
|
|
35
|
+
"smiles": smiles,
|
|
36
|
+
"rdmol": Chem.Mol(mol),
|
|
37
|
+
"pos": coords,
|
|
38
|
+
"boltzmannweight": 1.0,
|
|
39
|
+
"totalenergy": 0.0,
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def main():
|
|
44
|
+
args = parse_args()
|
|
45
|
+
out = Path(args.out_root)
|
|
46
|
+
out.mkdir(parents=True, exist_ok=True)
|
|
47
|
+
sdf = Chem.SDMolSupplier(args.sdf, removeHs=False, sanitize=True)
|
|
48
|
+
records = []
|
|
49
|
+
seen = set()
|
|
50
|
+
skipped = 0
|
|
51
|
+
for idx, mol in enumerate(sdf):
|
|
52
|
+
if args.limit > 0 and len(records) >= args.limit:
|
|
53
|
+
break
|
|
54
|
+
rec = mol_record(mol)
|
|
55
|
+
if rec is None:
|
|
56
|
+
skipped += 1
|
|
57
|
+
continue
|
|
58
|
+
key = (rec["smiles"], int(rec["pos"].shape[0]))
|
|
59
|
+
# Platinum Diverse is intended to contain unique molecules, but keep
|
|
60
|
+
# this guard so duplicate records cannot silently inflate a test set.
|
|
61
|
+
if key in seen:
|
|
62
|
+
skipped += 1
|
|
63
|
+
continue
|
|
64
|
+
seen.add(key)
|
|
65
|
+
rec["source_id"] = mol.GetProp("_Name") if mol.HasProp("_Name") else str(idx)
|
|
66
|
+
records.append(rec)
|
|
67
|
+
torch.save([], out / "platinum_train.pt")
|
|
68
|
+
torch.save([], out / "platinum_val.pt")
|
|
69
|
+
torch.save(records, out / "platinum_test.pt")
|
|
70
|
+
print({"records": len(records), "skipped": skipped, "out_root": str(out)})
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
if __name__ == "__main__":
|
|
74
|
+
main()
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
#!/usr/bin/env python
|
|
2
|
+
"""Convert a PubChem SDF/SMILES collection to the isolated GeoManimol format."""
|
|
3
|
+
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
|
|
6
|
+
import argparse
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
|
|
9
|
+
import torch
|
|
10
|
+
from rdkit import Chem
|
|
11
|
+
from rdkit.Chem import AllChem
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def main():
|
|
15
|
+
p = argparse.ArgumentParser()
|
|
16
|
+
p.add_argument("--input-sdf", required=True)
|
|
17
|
+
p.add_argument("--output", default="data/pubchem_etkdg_pretrain.pt")
|
|
18
|
+
p.add_argument("--max-molecules", type=int, default=0)
|
|
19
|
+
p.add_argument("--conformers", type=int, default=10)
|
|
20
|
+
p.add_argument("--seed", type=int, default=20260808)
|
|
21
|
+
args = p.parse_args()
|
|
22
|
+
supplier = Chem.SDMolSupplier(args.input_sdf, removeHs=False, sanitize=True)
|
|
23
|
+
rows = []
|
|
24
|
+
for idx, mol in enumerate(supplier):
|
|
25
|
+
if args.max_molecules and idx >= args.max_molecules:
|
|
26
|
+
break
|
|
27
|
+
if mol is None or mol.GetNumAtoms() < 3:
|
|
28
|
+
continue
|
|
29
|
+
mol = Chem.RemoveHs(mol)
|
|
30
|
+
mol.RemoveAllConformers()
|
|
31
|
+
params = AllChem.ETKDGv3(); params.randomSeed = args.seed + idx
|
|
32
|
+
params.numThreads = 0; params.pruneRmsThresh = 0.25
|
|
33
|
+
ids = list(AllChem.EmbedMultipleConfs(mol, numConfs=args.conformers, params=params))
|
|
34
|
+
if not ids:
|
|
35
|
+
continue
|
|
36
|
+
try:
|
|
37
|
+
AllChem.MMFFOptimizeMoleculeConfs(mol, numThreads=0)
|
|
38
|
+
except Exception:
|
|
39
|
+
pass
|
|
40
|
+
smiles = Chem.MolToSmiles(mol, isomericSmiles=True)
|
|
41
|
+
for cid in ids:
|
|
42
|
+
conf = mol.GetConformer(cid)
|
|
43
|
+
pos = [[conf.GetAtomPosition(i).x, conf.GetAtomPosition(i).y, conf.GetAtomPosition(i).z]
|
|
44
|
+
for i in range(mol.GetNumAtoms())]
|
|
45
|
+
rows.append({"smiles": smiles, "rdmol": Chem.Mol(mol), "pos": pos, "energy": 0.0})
|
|
46
|
+
if (idx + 1) % 1000 == 0:
|
|
47
|
+
print(f"processed={idx + 1} molecules rows={len(rows)}", flush=True)
|
|
48
|
+
out = Path(args.output); out.parent.mkdir(parents=True, exist_ok=True)
|
|
49
|
+
torch.save(rows, out)
|
|
50
|
+
print(f"PUBCHEM_ETKDG_PREPARED molecules={len(set(r['smiles'] for r in rows))} rows={len(rows)} output={out}")
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
if __name__ == "__main__":
|
|
54
|
+
main()
|