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,470 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""Prepare traceable coordinates for the Figure 5 4x4 qualitative comparison.
|
|
3
|
+
|
|
4
|
+
The script generates ETKDG and ET-Flow candidates, reads an existing MANIMOL
|
|
5
|
+
candidate ensemble, recomputes symmetry-aware heavy-atom RMSD with one RDKit
|
|
6
|
+
pipeline, and writes pair SDFs for rendering. It never edits the manuscript
|
|
7
|
+
figure directly.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
import argparse
|
|
13
|
+
import csv
|
|
14
|
+
import json
|
|
15
|
+
import os
|
|
16
|
+
import sys
|
|
17
|
+
import time
|
|
18
|
+
from dataclasses import dataclass
|
|
19
|
+
from pathlib import Path
|
|
20
|
+
from typing import Dict, Iterable, List, Optional, Sequence, Tuple
|
|
21
|
+
|
|
22
|
+
import numpy as np
|
|
23
|
+
from rdkit import Chem, RDLogger
|
|
24
|
+
from rdkit.Chem import AllChem, rdMolAlign, rdMolDescriptors
|
|
25
|
+
from rdkit.Geometry import Point3D
|
|
26
|
+
|
|
27
|
+
RDLogger.DisableLog("rdApp.warning")
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
@dataclass
|
|
31
|
+
class CandidateResult:
|
|
32
|
+
method: str
|
|
33
|
+
rmsd: float
|
|
34
|
+
mol: Chem.Mol
|
|
35
|
+
num_candidates: int
|
|
36
|
+
note: str = ""
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def parse_args() -> argparse.Namespace:
|
|
40
|
+
parser = argparse.ArgumentParser(
|
|
41
|
+
description="Generate and screen Figure 5 ETKDG/ET-Flow/MANIMOL qualitative cases."
|
|
42
|
+
)
|
|
43
|
+
parser.add_argument("--dataset_dir", default="data/dataset-1")
|
|
44
|
+
parser.add_argument("--csv_name", default="dataset-1_with-index.csv")
|
|
45
|
+
parser.add_argument("--sdf_name", default="dataset-1.sdf")
|
|
46
|
+
parser.add_argument("--manimol_selected_dir", default="outputs/fastsmcg_dataset1/top10/selected_sdf")
|
|
47
|
+
parser.add_argument("--output_dir", default="outputs/figure5_4x4")
|
|
48
|
+
parser.add_argument("--candidate_budget", type=int, default=10)
|
|
49
|
+
parser.add_argument("--seed", type=int, default=20260729)
|
|
50
|
+
parser.add_argument("--limit_molecules", type=int, default=0)
|
|
51
|
+
parser.add_argument("--start_index", type=int, default=0)
|
|
52
|
+
parser.add_argument("--max_cases", type=int, default=4)
|
|
53
|
+
parser.add_argument("--etflow_model", default="drugs-o3")
|
|
54
|
+
parser.add_argument("--etflow_steps", type=int, default=50)
|
|
55
|
+
parser.add_argument("--etflow_batch_size", type=int, default=1)
|
|
56
|
+
parser.add_argument("--etflow_device", default="cpu")
|
|
57
|
+
parser.add_argument("--etflow_cache", default="")
|
|
58
|
+
parser.add_argument("--etkdg_mmff", action="store_true")
|
|
59
|
+
parser.add_argument("--skip_etflow", action="store_true")
|
|
60
|
+
parser.add_argument("--strict_order", action="store_true", default=True)
|
|
61
|
+
parser.add_argument("--relax_order_if_needed", action="store_true")
|
|
62
|
+
parser.add_argument("--rmsd_max_matches", type=int, default=100000)
|
|
63
|
+
parser.add_argument("--min_etkdg_rmsd", type=float, default=0.0)
|
|
64
|
+
parser.add_argument("--max_manimol_rmsd", type=float, default=999.0)
|
|
65
|
+
return parser.parse_args()
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def safe_name(text: str) -> str:
|
|
69
|
+
return "".join(ch if ch.isalnum() or ch in "._-" else "_" for ch in str(text))
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def strip_index_suffix(title: str) -> str:
|
|
73
|
+
head, sep, tail = str(title).rpartition("_")
|
|
74
|
+
return head if sep and tail.isdigit() else str(title)
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def read_dataset_rows(csv_path: Path) -> List[dict]:
|
|
78
|
+
with csv_path.open(newline="", encoding="utf-8") as handle:
|
|
79
|
+
return list(csv.DictReader(handle))
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def read_reference_sdf(sdf_path: Path) -> Dict[str, Chem.Mol]:
|
|
83
|
+
refs: Dict[str, Chem.Mol] = {}
|
|
84
|
+
for mol in Chem.SDMolSupplier(str(sdf_path), removeHs=False):
|
|
85
|
+
if mol is None:
|
|
86
|
+
continue
|
|
87
|
+
name = mol.GetProp("_Name") if mol.HasProp("_Name") else ""
|
|
88
|
+
if name:
|
|
89
|
+
refs[name] = mol
|
|
90
|
+
return refs
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def heavy_copy(mol: Chem.Mol) -> Chem.Mol:
|
|
94
|
+
out = Chem.RemoveHs(Chem.Mol(mol), sanitize=False)
|
|
95
|
+
Chem.SanitizeMol(out)
|
|
96
|
+
return out
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def mol_with_single_conf(template: Chem.Mol, source: Chem.Mol, conf_id: int = 0, name: str = "") -> Chem.Mol:
|
|
100
|
+
out = Chem.Mol(template)
|
|
101
|
+
out.RemoveAllConformers()
|
|
102
|
+
src = source.GetConformer(conf_id)
|
|
103
|
+
conf = Chem.Conformer(out.GetNumAtoms())
|
|
104
|
+
if source.GetNumAtoms() != out.GetNumAtoms():
|
|
105
|
+
raise ValueError(f"atom-count mismatch: source={source.GetNumAtoms()} template={out.GetNumAtoms()}")
|
|
106
|
+
for atom_idx in range(out.GetNumAtoms()):
|
|
107
|
+
pos = src.GetAtomPosition(atom_idx)
|
|
108
|
+
conf.SetAtomPosition(atom_idx, Point3D(float(pos.x), float(pos.y), float(pos.z)))
|
|
109
|
+
out.AddConformer(conf, assignId=True)
|
|
110
|
+
if name:
|
|
111
|
+
out.SetProp("_Name", name)
|
|
112
|
+
return out
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
def mol_from_positions(template: Chem.Mol, positions: np.ndarray, name: str) -> Chem.Mol:
|
|
116
|
+
if positions.shape[0] != template.GetNumAtoms():
|
|
117
|
+
raise ValueError(f"position atom-count mismatch: positions={positions.shape[0]} template={template.GetNumAtoms()}")
|
|
118
|
+
out = Chem.Mol(template)
|
|
119
|
+
out.RemoveAllConformers()
|
|
120
|
+
conf = Chem.Conformer(out.GetNumAtoms())
|
|
121
|
+
for atom_idx, xyz in enumerate(positions):
|
|
122
|
+
conf.SetAtomPosition(atom_idx, Point3D(float(xyz[0]), float(xyz[1]), float(xyz[2])))
|
|
123
|
+
out.AddConformer(conf, assignId=True)
|
|
124
|
+
out.SetProp("_Name", name)
|
|
125
|
+
return out
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def best_rmsd_and_aligned(ref: Chem.Mol, pred: Chem.Mol, max_matches: int) -> Tuple[float, Chem.Mol]:
|
|
129
|
+
ref_h = heavy_copy(ref)
|
|
130
|
+
pred_h = heavy_copy(pred)
|
|
131
|
+
if ref_h.GetNumAtoms() != pred_h.GetNumAtoms():
|
|
132
|
+
raise ValueError(f"heavy atom-count mismatch: ref={ref_h.GetNumAtoms()} pred={pred_h.GetNumAtoms()}")
|
|
133
|
+
aligned = Chem.Mol(pred_h)
|
|
134
|
+
rmsd = rdMolAlign.GetBestRMS(aligned, ref_h, maxMatches=int(max_matches))
|
|
135
|
+
return float(rmsd), aligned
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
def choose_best(ref: Chem.Mol, candidates: Sequence[Chem.Mol], max_matches: int, method: str) -> CandidateResult:
|
|
139
|
+
best: Optional[CandidateResult] = None
|
|
140
|
+
for idx, cand in enumerate(candidates):
|
|
141
|
+
try:
|
|
142
|
+
rmsd, aligned = best_rmsd_and_aligned(ref, cand, max_matches)
|
|
143
|
+
except Exception:
|
|
144
|
+
continue
|
|
145
|
+
aligned.SetProp("_Name", f"{method}_best")
|
|
146
|
+
aligned.SetProp("method", method)
|
|
147
|
+
aligned.SetProp("candidate_index", str(idx))
|
|
148
|
+
aligned.SetProp("rmsd_to_reference", f"{rmsd:.8f}")
|
|
149
|
+
if best is None or rmsd < best.rmsd:
|
|
150
|
+
best = CandidateResult(method=method, rmsd=rmsd, mol=aligned, num_candidates=len(candidates))
|
|
151
|
+
if best is None:
|
|
152
|
+
raise ValueError(f"no valid {method} candidates after atom/RMSD checks")
|
|
153
|
+
return best
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
def generate_etkdg_candidates(ref: Chem.Mol, budget: int, seed: int, use_mmff: bool) -> List[Chem.Mol]:
|
|
157
|
+
heavy = heavy_copy(ref)
|
|
158
|
+
work = Chem.AddHs(heavy, addCoords=False)
|
|
159
|
+
params = AllChem.ETKDGv3()
|
|
160
|
+
params.randomSeed = int(seed)
|
|
161
|
+
params.useRandomCoords = True
|
|
162
|
+
params.pruneRmsThresh = -1.0
|
|
163
|
+
conf_ids = list(AllChem.EmbedMultipleConfs(work, numConfs=int(budget), params=params))
|
|
164
|
+
out: List[Chem.Mol] = []
|
|
165
|
+
for conf_id in conf_ids:
|
|
166
|
+
if use_mmff:
|
|
167
|
+
try:
|
|
168
|
+
AllChem.MMFFOptimizeMolecule(work, confId=int(conf_id), maxIters=250)
|
|
169
|
+
except Exception:
|
|
170
|
+
pass
|
|
171
|
+
no_h = Chem.RemoveHs(work, sanitize=False)
|
|
172
|
+
Chem.SanitizeMol(no_h)
|
|
173
|
+
if no_h.GetNumConformers() > 1:
|
|
174
|
+
# RemoveHs keeps all conformers; select the corresponding one by order.
|
|
175
|
+
tmp = Chem.Mol(no_h)
|
|
176
|
+
conf = no_h.GetConformer(int(conf_id) if conf_id < no_h.GetNumConformers() else 0)
|
|
177
|
+
single = Chem.Mol(heavy)
|
|
178
|
+
single.RemoveAllConformers()
|
|
179
|
+
new_conf = Chem.Conformer(single.GetNumAtoms())
|
|
180
|
+
for atom_idx in range(single.GetNumAtoms()):
|
|
181
|
+
pos = conf.GetAtomPosition(atom_idx)
|
|
182
|
+
new_conf.SetAtomPosition(atom_idx, pos)
|
|
183
|
+
single.AddConformer(new_conf, assignId=True)
|
|
184
|
+
out.append(single)
|
|
185
|
+
else:
|
|
186
|
+
out.append(no_h)
|
|
187
|
+
return out[: int(budget)]
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
def load_manimol_candidates(selected_dir: Path, mol_title: str, budget: int) -> List[Chem.Mol]:
|
|
191
|
+
selected_path = selected_dir / f"{safe_name(mol_title)}_selected.sdf"
|
|
192
|
+
if selected_path.exists():
|
|
193
|
+
mols = [m for m in Chem.SDMolSupplier(str(selected_path), removeHs=False) if m is not None]
|
|
194
|
+
if not mols:
|
|
195
|
+
raise ValueError(f"empty MANIMOL selected SDF: {selected_path}")
|
|
196
|
+
return mols[: int(budget)]
|
|
197
|
+
|
|
198
|
+
best_pair_path = selected_dir / f"{safe_name(mol_title)}_best_pair.sdf"
|
|
199
|
+
if best_pair_path.exists():
|
|
200
|
+
mols = [m for m in Chem.SDMolSupplier(str(best_pair_path), removeHs=False) if m is not None]
|
|
201
|
+
if len(mols) < 2:
|
|
202
|
+
raise ValueError(f"MANIMOL best-pair SDF should contain reference and generated conformers: {best_pair_path}")
|
|
203
|
+
return [mols[1]]
|
|
204
|
+
|
|
205
|
+
raise FileNotFoundError(f"{selected_path} or {best_pair_path}")
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
def load_etflow_model(args: argparse.Namespace):
|
|
209
|
+
if args.skip_etflow:
|
|
210
|
+
return None
|
|
211
|
+
from etflow import BaseFlow
|
|
212
|
+
|
|
213
|
+
cache = args.etflow_cache or None
|
|
214
|
+
return BaseFlow.from_default(model=args.etflow_model, device=args.etflow_device, cache=cache)
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
def generate_etflow_candidates(model, smiles: str, template_ref: Chem.Mol, args: argparse.Namespace) -> List[Chem.Mol]:
|
|
218
|
+
if model is None:
|
|
219
|
+
raise RuntimeError("ET-Flow generation was disabled")
|
|
220
|
+
result = model.predict(
|
|
221
|
+
[smiles],
|
|
222
|
+
max_batch_size=int(args.etflow_batch_size),
|
|
223
|
+
num_samples=int(args.candidate_budget),
|
|
224
|
+
n_timesteps=int(args.etflow_steps),
|
|
225
|
+
seed=int(args.seed),
|
|
226
|
+
device=str(args.etflow_device),
|
|
227
|
+
as_mol=True,
|
|
228
|
+
)
|
|
229
|
+
item = result[smiles] if isinstance(result, dict) else result
|
|
230
|
+
candidates: List[Chem.Mol] = []
|
|
231
|
+
if isinstance(item, np.ndarray):
|
|
232
|
+
heavy_ref = heavy_copy(template_ref)
|
|
233
|
+
arr = item
|
|
234
|
+
if arr.ndim != 3:
|
|
235
|
+
raise ValueError(f"unexpected ET-Flow coordinate array shape: {arr.shape}")
|
|
236
|
+
for idx, pos in enumerate(arr[: int(args.candidate_budget)]):
|
|
237
|
+
candidates.append(mol_from_positions(heavy_ref, np.asarray(pos), f"ET-Flow_{idx:03d}"))
|
|
238
|
+
else:
|
|
239
|
+
if isinstance(item, Chem.Mol):
|
|
240
|
+
base = heavy_copy(item)
|
|
241
|
+
mols = [mol_with_single_conf(base, base, cid, f"ET-Flow_{cid:03d}") for cid in range(base.GetNumConformers())]
|
|
242
|
+
else:
|
|
243
|
+
mols = [heavy_copy(m) for m in list(item)]
|
|
244
|
+
candidates = mols[: int(args.candidate_budget)]
|
|
245
|
+
return candidates
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
def write_pair_sdf(path: Path, ref: Chem.Mol, result: CandidateResult) -> None:
|
|
249
|
+
path.parent.mkdir(parents=True, exist_ok=True)
|
|
250
|
+
ref_h = heavy_copy(ref)
|
|
251
|
+
ref_h.SetProp("_Name", "reference")
|
|
252
|
+
ref_h.SetProp("role", "reference")
|
|
253
|
+
gen = Chem.Mol(result.mol)
|
|
254
|
+
gen.SetProp("_Name", f"{result.method}_best")
|
|
255
|
+
gen.SetProp("role", "generated")
|
|
256
|
+
gen.SetProp("method", result.method)
|
|
257
|
+
gen.SetProp("rmsd_to_reference", f"{result.rmsd:.8f}")
|
|
258
|
+
writer = Chem.SDWriter(str(path))
|
|
259
|
+
writer.write(ref_h)
|
|
260
|
+
writer.write(gen)
|
|
261
|
+
writer.close()
|
|
262
|
+
|
|
263
|
+
|
|
264
|
+
def write_csv(path: Path, rows: List[dict]) -> None:
|
|
265
|
+
path.parent.mkdir(parents=True, exist_ok=True)
|
|
266
|
+
if not rows:
|
|
267
|
+
path.write_text("", encoding="utf-8")
|
|
268
|
+
return
|
|
269
|
+
keys: List[str] = []
|
|
270
|
+
for row in rows:
|
|
271
|
+
for key in row:
|
|
272
|
+
if key not in keys:
|
|
273
|
+
keys.append(key)
|
|
274
|
+
with path.open("w", newline="", encoding="utf-8") as handle:
|
|
275
|
+
writer = csv.DictWriter(handle, fieldnames=keys)
|
|
276
|
+
writer.writeheader()
|
|
277
|
+
writer.writerows(rows)
|
|
278
|
+
|
|
279
|
+
|
|
280
|
+
def diversity_bin(row: dict) -> Tuple[int, int, int]:
|
|
281
|
+
return (
|
|
282
|
+
int(float(row.get("num_rotatable_bonds", 0))) // 3,
|
|
283
|
+
int(float(row.get("num_heavy_atoms", 0))) // 10,
|
|
284
|
+
len(str(row.get("SMILES", ""))) // 30,
|
|
285
|
+
)
|
|
286
|
+
|
|
287
|
+
|
|
288
|
+
def select_cases(screening_rows: List[dict], max_cases: int, min_etkdg_rmsd: float = 0.0, max_manimol_rmsd: float = 999.0) -> List[dict]:
|
|
289
|
+
valid = [r for r in screening_rows if str(r.get("ordering_valid", "")).lower() == "true"]
|
|
290
|
+
preferred = [
|
|
291
|
+
r
|
|
292
|
+
for r in valid
|
|
293
|
+
if float(r["ETKDG_RMSD"]) >= float(min_etkdg_rmsd)
|
|
294
|
+
and float(r["MANIMOL_RMSD"]) <= float(max_manimol_rmsd)
|
|
295
|
+
]
|
|
296
|
+
if len(preferred) >= max_cases:
|
|
297
|
+
valid = preferred
|
|
298
|
+
valid.sort(key=lambda r: (-(float(r["ETKDG_RMSD"]) - float(r["MANIMOL_RMSD"])), float(r["MANIMOL_RMSD"])))
|
|
299
|
+
chosen: List[dict] = []
|
|
300
|
+
used_bins = set()
|
|
301
|
+
for row in valid:
|
|
302
|
+
b = diversity_bin(row)
|
|
303
|
+
if b in used_bins and len(valid) - len(chosen) > max_cases - len(chosen):
|
|
304
|
+
continue
|
|
305
|
+
chosen.append(row)
|
|
306
|
+
used_bins.add(b)
|
|
307
|
+
if len(chosen) >= max_cases:
|
|
308
|
+
return chosen
|
|
309
|
+
for row in valid:
|
|
310
|
+
if row not in chosen:
|
|
311
|
+
chosen.append(row)
|
|
312
|
+
if len(chosen) >= max_cases:
|
|
313
|
+
break
|
|
314
|
+
return chosen
|
|
315
|
+
|
|
316
|
+
|
|
317
|
+
def main() -> int:
|
|
318
|
+
args = parse_args()
|
|
319
|
+
out_dir = Path(args.output_dir)
|
|
320
|
+
pair_dir = out_dir / "pairs"
|
|
321
|
+
out_dir.mkdir(parents=True, exist_ok=True)
|
|
322
|
+
log_path = out_dir / "figure5_generation_log.txt"
|
|
323
|
+
log_lines: List[str] = []
|
|
324
|
+
started = time.time()
|
|
325
|
+
log_lines.append(f"started={time.strftime('%Y-%m-%d %H:%M:%S')}")
|
|
326
|
+
log_lines.append(f"python={sys.executable}")
|
|
327
|
+
log_lines.append(f"candidate_budget={args.candidate_budget}")
|
|
328
|
+
log_lines.append(f"seed={args.seed}")
|
|
329
|
+
|
|
330
|
+
rows = read_dataset_rows(Path(args.dataset_dir) / args.csv_name)
|
|
331
|
+
if args.start_index:
|
|
332
|
+
rows = rows[int(args.start_index) :]
|
|
333
|
+
if args.limit_molecules:
|
|
334
|
+
rows = rows[: int(args.limit_molecules)]
|
|
335
|
+
refs = read_reference_sdf(Path(args.dataset_dir) / args.sdf_name)
|
|
336
|
+
etflow_model = None
|
|
337
|
+
try:
|
|
338
|
+
etflow_model = load_etflow_model(args)
|
|
339
|
+
log_lines.append(f"etflow_model={args.etflow_model if etflow_model is not None else 'skipped'}")
|
|
340
|
+
except Exception as exc:
|
|
341
|
+
log_lines.append(f"ET-Flow model load failed: {type(exc).__name__}: {exc}")
|
|
342
|
+
(out_dir / "figure5_generation_log.txt").write_text("\n".join(log_lines) + "\n", encoding="utf-8")
|
|
343
|
+
raise
|
|
344
|
+
|
|
345
|
+
screening: List[dict] = []
|
|
346
|
+
result_cache: Dict[str, Dict[str, CandidateResult]] = {}
|
|
347
|
+
ref_cache: Dict[str, Chem.Mol] = {}
|
|
348
|
+
for idx, row in enumerate(rows):
|
|
349
|
+
mol_title = str(row["mol_title"])
|
|
350
|
+
base_title = strip_index_suffix(mol_title)
|
|
351
|
+
ref = refs.get(base_title)
|
|
352
|
+
if ref is None:
|
|
353
|
+
screening.append({"molecule_id": mol_title, "SMILES": row.get("smiles", ""), "skipped_reason": "missing_reference"})
|
|
354
|
+
continue
|
|
355
|
+
try:
|
|
356
|
+
rb = rdMolDescriptors.CalcNumRotatableBonds(heavy_copy(ref))
|
|
357
|
+
heavy = heavy_copy(ref).GetNumAtoms()
|
|
358
|
+
etkdg = choose_best(
|
|
359
|
+
ref,
|
|
360
|
+
generate_etkdg_candidates(ref, args.candidate_budget, args.seed + idx, args.etkdg_mmff),
|
|
361
|
+
args.rmsd_max_matches,
|
|
362
|
+
"ETKDG",
|
|
363
|
+
)
|
|
364
|
+
etflow = None
|
|
365
|
+
if etflow_model is not None:
|
|
366
|
+
etflow = choose_best(
|
|
367
|
+
ref,
|
|
368
|
+
generate_etflow_candidates(etflow_model, str(row["smiles"]), ref, args),
|
|
369
|
+
args.rmsd_max_matches,
|
|
370
|
+
"ET-Flow",
|
|
371
|
+
)
|
|
372
|
+
manimol = choose_best(
|
|
373
|
+
ref,
|
|
374
|
+
load_manimol_candidates(Path(args.manimol_selected_dir), mol_title, args.candidate_budget),
|
|
375
|
+
args.rmsd_max_matches,
|
|
376
|
+
"MANIMOL",
|
|
377
|
+
)
|
|
378
|
+
if etflow is None:
|
|
379
|
+
ordering_valid = bool(etkdg.rmsd > manimol.rmsd)
|
|
380
|
+
else:
|
|
381
|
+
ordering_valid = bool(etkdg.rmsd > etflow.rmsd > manimol.rmsd)
|
|
382
|
+
rec = {
|
|
383
|
+
"molecule_id": mol_title,
|
|
384
|
+
"SMILES": row.get("smiles", ""),
|
|
385
|
+
"num_heavy_atoms": int(heavy),
|
|
386
|
+
"num_rotatable_bonds": int(rb),
|
|
387
|
+
"candidate_budget": int(args.candidate_budget),
|
|
388
|
+
"ETKDG_RMSD": etkdg.rmsd,
|
|
389
|
+
"ETFlow_RMSD": "" if etflow is None else etflow.rmsd,
|
|
390
|
+
"MANIMOL_RMSD": manimol.rmsd,
|
|
391
|
+
"ordering_valid": str(ordering_valid),
|
|
392
|
+
"selected_for_figure": "False",
|
|
393
|
+
"skipped_reason": "",
|
|
394
|
+
}
|
|
395
|
+
screening.append(rec)
|
|
396
|
+
result_cache[mol_title] = {"ETKDG": etkdg, "MANIMOL": manimol}
|
|
397
|
+
if etflow is not None:
|
|
398
|
+
result_cache[mol_title]["ET-Flow"] = etflow
|
|
399
|
+
ref_cache[mol_title] = ref
|
|
400
|
+
middle = "" if etflow is None else f" ET-Flow={etflow.rmsd:.3f}"
|
|
401
|
+
print(
|
|
402
|
+
f"[{idx+1}/{len(rows)}] {mol_title}: "
|
|
403
|
+
f"ETKDG={etkdg.rmsd:.3f}{middle} MANIMOL={manimol.rmsd:.3f} valid={ordering_valid}"
|
|
404
|
+
)
|
|
405
|
+
except Exception as exc:
|
|
406
|
+
screening.append(
|
|
407
|
+
{
|
|
408
|
+
"molecule_id": mol_title,
|
|
409
|
+
"SMILES": row.get("smiles", ""),
|
|
410
|
+
"selected_for_figure": "False",
|
|
411
|
+
"ordering_valid": "False",
|
|
412
|
+
"skipped_reason": f"{type(exc).__name__}: {exc}",
|
|
413
|
+
}
|
|
414
|
+
)
|
|
415
|
+
print(f"[{idx+1}/{len(rows)}] skip {mol_title}: {type(exc).__name__}: {exc}")
|
|
416
|
+
|
|
417
|
+
chosen = select_cases(screening, args.max_cases, args.min_etkdg_rmsd, args.max_manimol_rmsd)
|
|
418
|
+
if len(chosen) < args.max_cases:
|
|
419
|
+
msg = f"Only {len(chosen)} molecules satisfied ETKDG > ET-Flow > MANIMOL; requested {args.max_cases}."
|
|
420
|
+
log_lines.append(msg)
|
|
421
|
+
print(msg)
|
|
422
|
+
if args.strict_order and not args.relax_order_if_needed:
|
|
423
|
+
write_csv(out_dir / "figure5_case_screening.csv", screening)
|
|
424
|
+
write_csv(out_dir / "figure5_selected_cases.csv", chosen)
|
|
425
|
+
log_lines.append(f"elapsed_sec={time.time() - started:.2f}")
|
|
426
|
+
log_path.write_text("\n".join(log_lines) + "\n", encoding="utf-8")
|
|
427
|
+
return 2
|
|
428
|
+
|
|
429
|
+
selected_ids = {row["molecule_id"] for row in chosen}
|
|
430
|
+
for rec in screening:
|
|
431
|
+
if rec.get("molecule_id") in selected_ids:
|
|
432
|
+
rec["selected_for_figure"] = "True"
|
|
433
|
+
for row_idx, rec in enumerate(chosen, start=1):
|
|
434
|
+
mol_title = rec["molecule_id"]
|
|
435
|
+
ref = ref_cache[mol_title]
|
|
436
|
+
for method, result in result_cache[mol_title].items():
|
|
437
|
+
write_pair_sdf(pair_dir / safe_name(mol_title) / f"{safe_name(method)}.sdf", ref, result)
|
|
438
|
+
ref_heavy = heavy_copy(ref)
|
|
439
|
+
ref_path = pair_dir / safe_name(mol_title) / "Reference.sdf"
|
|
440
|
+
writer = Chem.SDWriter(str(ref_path))
|
|
441
|
+
ref_heavy.SetProp("_Name", "reference")
|
|
442
|
+
writer.write(ref_heavy)
|
|
443
|
+
writer.close()
|
|
444
|
+
rec["row_label"] = f"({chr(96 + row_idx)}) {mol_title}"
|
|
445
|
+
|
|
446
|
+
write_csv(out_dir / "figure5_case_screening.csv", screening)
|
|
447
|
+
write_csv(out_dir / "figure5_selected_cases.csv", chosen)
|
|
448
|
+
render_config = {
|
|
449
|
+
"candidate_budget": int(args.candidate_budget),
|
|
450
|
+
"seed": int(args.seed),
|
|
451
|
+
"rmsd_protocol": "RDKit GetBestRMS on heavy-atom molecules after RemoveHs; maxMatches=%d" % int(args.rmsd_max_matches),
|
|
452
|
+
"methods": ["Reference", "ETKDG", "MANIMOL"] if args.skip_etflow else ["Reference", "ETKDG", "ET-Flow", "MANIMOL"],
|
|
453
|
+
"colors": {
|
|
454
|
+
"reference": "neutral medium gray",
|
|
455
|
+
"ETKDG": "muted orange",
|
|
456
|
+
"ET-Flow": "muted teal",
|
|
457
|
+
"MANIMOL": "deep academic blue",
|
|
458
|
+
},
|
|
459
|
+
"pair_sdf_dir": str(pair_dir),
|
|
460
|
+
"selected_case_count": len(chosen),
|
|
461
|
+
}
|
|
462
|
+
(out_dir / "figure5_render_config.json").write_text(json.dumps(render_config, indent=2), encoding="utf-8")
|
|
463
|
+
log_lines.append(f"selected_cases={len(chosen)}")
|
|
464
|
+
log_lines.append(f"elapsed_sec={time.time() - started:.2f}")
|
|
465
|
+
log_path.write_text("\n".join(log_lines) + "\n", encoding="utf-8")
|
|
466
|
+
return 0
|
|
467
|
+
|
|
468
|
+
|
|
469
|
+
if __name__ == "__main__":
|
|
470
|
+
raise SystemExit(main())
|