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,82 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""Audit the split size used by the isolated full-train/validation control.
|
|
3
|
+
|
|
4
|
+
This does not alter caches or checkpoints. It verifies that molecule-count
|
|
5
|
+
limits are disabled while preserving the established old-best conformer cap.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import argparse
|
|
11
|
+
import json
|
|
12
|
+
import sys
|
|
13
|
+
from collections import Counter
|
|
14
|
+
from pathlib import Path
|
|
15
|
+
|
|
16
|
+
ROOT = Path(__file__).resolve().parents[1]
|
|
17
|
+
if str(ROOT) not in sys.path:
|
|
18
|
+
sys.path.insert(0, str(ROOT))
|
|
19
|
+
|
|
20
|
+
from dataset import QM9Dataset
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def summarize(dataset, indices):
|
|
24
|
+
molecule_ids = []
|
|
25
|
+
for index in indices:
|
|
26
|
+
item = dataset[int(index)]
|
|
27
|
+
molecule_ids.append(int(item.mol_id.item()) if hasattr(item, "mol_id") else int(index))
|
|
28
|
+
per_molecule = Counter(molecule_ids)
|
|
29
|
+
return {
|
|
30
|
+
"conformers": len(molecule_ids),
|
|
31
|
+
"molecules": len(per_molecule),
|
|
32
|
+
"min_conformers_per_molecule": min(per_molecule.values(), default=0),
|
|
33
|
+
"max_conformers_per_molecule": max(per_molecule.values(), default=0),
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def main():
|
|
38
|
+
parser = argparse.ArgumentParser(description="Audit the full-train/validation old-best control split.")
|
|
39
|
+
# Accepted for compatibility with the shared YAML task runner; unused here.
|
|
40
|
+
parser.add_argument("--fixed_dump_name", action="store_true")
|
|
41
|
+
parser.add_argument("--data_root", default="data")
|
|
42
|
+
parser.add_argument("--dataset", default="Drugs")
|
|
43
|
+
parser.add_argument("--raw_prefix", default="geom_drugs")
|
|
44
|
+
parser.add_argument("--max_confs", type=int, default=5)
|
|
45
|
+
parser.add_argument("--train_mol_limit", type=int, default=0)
|
|
46
|
+
parser.add_argument("--valid_mol_limit", type=int, default=0)
|
|
47
|
+
parser.add_argument("--test_mol_limit", type=int, default=50)
|
|
48
|
+
parser.add_argument("--output", required=True)
|
|
49
|
+
args = parser.parse_args()
|
|
50
|
+
|
|
51
|
+
if args.train_mol_limit != 0 or args.valid_mol_limit != 0:
|
|
52
|
+
raise ValueError("This audit is only valid when train_mol_limit=0 and valid_mol_limit=0.")
|
|
53
|
+
|
|
54
|
+
dataset = QM9Dataset(
|
|
55
|
+
name=args.dataset,
|
|
56
|
+
root=args.data_root,
|
|
57
|
+
max_confs=args.max_confs,
|
|
58
|
+
raw_prefix=args.raw_prefix,
|
|
59
|
+
keep_full_test_conformers=False,
|
|
60
|
+
)
|
|
61
|
+
report = {
|
|
62
|
+
"dataset": args.dataset,
|
|
63
|
+
"raw_prefix": args.raw_prefix,
|
|
64
|
+
"max_confs": int(dataset.max_confs),
|
|
65
|
+
"limits": {
|
|
66
|
+
"train_mol_limit": args.train_mol_limit,
|
|
67
|
+
"valid_mol_limit": args.valid_mol_limit,
|
|
68
|
+
"test_mol_limit": args.test_mol_limit,
|
|
69
|
+
},
|
|
70
|
+
"train": summarize(dataset, dataset.train_index),
|
|
71
|
+
"validation": summarize(dataset, dataset.valid_index),
|
|
72
|
+
"test_before_inference_limit": summarize(dataset, dataset.test_index),
|
|
73
|
+
}
|
|
74
|
+
output = Path(args.output)
|
|
75
|
+
output.parent.mkdir(parents=True, exist_ok=True)
|
|
76
|
+
output.write_text(json.dumps(report, indent=2) + "\n", encoding="utf-8")
|
|
77
|
+
print(json.dumps(report, indent=2))
|
|
78
|
+
print(f"Saved: {output}")
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
if __name__ == "__main__":
|
|
82
|
+
main()
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""Audit PDBbind receptor-contact pharmacophores using RDKit and PLIP.
|
|
3
|
+
|
|
4
|
+
This produces a transparent post-hoc audit. It does not select generated
|
|
5
|
+
conformers and does not use crystal contacts during inference.
|
|
6
|
+
"""
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import argparse
|
|
10
|
+
import json
|
|
11
|
+
import tempfile
|
|
12
|
+
from pathlib import Path
|
|
13
|
+
|
|
14
|
+
import pandas as pd
|
|
15
|
+
from rdkit import Chem, RDConfig
|
|
16
|
+
from rdkit.Chem import ChemicalFeatures
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
FAMILY = {
|
|
20
|
+
"Donor": "HBD",
|
|
21
|
+
"Acceptor": "HBA",
|
|
22
|
+
"Aromatic": "Aromatic",
|
|
23
|
+
"Hydrophobe": "Hydrophobic",
|
|
24
|
+
"LumpedHydrophobe": "Hydrophobic",
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def features(mol):
|
|
29
|
+
factory = ChemicalFeatures.BuildFeatureFactory(str(Path(RDConfig.RDDataDir) / "BaseFeatures.fdef"))
|
|
30
|
+
conf = mol.GetConformer()
|
|
31
|
+
out = []
|
|
32
|
+
for idx, feat in enumerate(factory.GetFeaturesForMol(mol)):
|
|
33
|
+
family = FAMILY.get(feat.GetFamily())
|
|
34
|
+
if family is None:
|
|
35
|
+
continue
|
|
36
|
+
atoms = [int(i) for i in feat.GetAtomIds()]
|
|
37
|
+
xyz = [conf.GetAtomPosition(i) for i in atoms]
|
|
38
|
+
out.append({
|
|
39
|
+
"feature_id": idx,
|
|
40
|
+
"family": family,
|
|
41
|
+
"atom_ids": atoms,
|
|
42
|
+
"coord": [sum(p[k] for p in xyz) / len(xyz) for k in range(3)],
|
|
43
|
+
})
|
|
44
|
+
return out
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def ligand_pdb(mol):
|
|
48
|
+
block = Chem.MolToPDBBlock(mol)
|
|
49
|
+
lines = []
|
|
50
|
+
for line in block.splitlines():
|
|
51
|
+
if line.startswith(("ATOM ", "HETATM")):
|
|
52
|
+
line = "HETATM" + line[6:]
|
|
53
|
+
line = line[:17] + "LIG" + line[20:21] + "A" + line[22:]
|
|
54
|
+
lines.append(line)
|
|
55
|
+
return "\n".join(lines) + "\nEND\n"
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def plip_contacts(protein_path: Path, mol):
|
|
59
|
+
from plip.structure.preparation import PDBComplex
|
|
60
|
+
|
|
61
|
+
protein_lines = []
|
|
62
|
+
for line in protein_path.read_text(errors="ignore").splitlines():
|
|
63
|
+
if line.startswith(("END", "CONECT")):
|
|
64
|
+
continue
|
|
65
|
+
protein_lines.append(line)
|
|
66
|
+
with tempfile.TemporaryDirectory(prefix="pdbbind_plip_") as tmp:
|
|
67
|
+
path = Path(tmp) / "complex.pdb"
|
|
68
|
+
path.write_text("\n".join(protein_lines) + "\n" + ligand_pdb(mol))
|
|
69
|
+
complex_obj = PDBComplex()
|
|
70
|
+
complex_obj.load_pdb(str(path))
|
|
71
|
+
rows = []
|
|
72
|
+
for ligand in complex_obj.ligands:
|
|
73
|
+
complex_obj.characterize_complex(ligand)
|
|
74
|
+
interactions = complex_obj.interaction_sets.get(ligand.hetid + ":" + ligand.chain + ":" + str(ligand.position))
|
|
75
|
+
if interactions is None:
|
|
76
|
+
continue
|
|
77
|
+
ligand_indices = set(range(int(mol.GetNumAtoms())))
|
|
78
|
+
|
|
79
|
+
def collect_ligand_indices(value, seen=None):
|
|
80
|
+
seen = set() if seen is None else seen
|
|
81
|
+
ident = id(value)
|
|
82
|
+
if ident in seen:
|
|
83
|
+
return set()
|
|
84
|
+
seen.add(ident)
|
|
85
|
+
if isinstance(value, (str, bytes, int, float, bool, type(None))):
|
|
86
|
+
return {int(value)} if isinstance(value, int) and int(value) in ligand_indices else set()
|
|
87
|
+
if hasattr(value, "idx"):
|
|
88
|
+
idx = int(value.idx)
|
|
89
|
+
return {idx} if idx in ligand_indices else set()
|
|
90
|
+
if isinstance(value, dict):
|
|
91
|
+
out = set()
|
|
92
|
+
for item in value.values():
|
|
93
|
+
out |= collect_ligand_indices(item, seen)
|
|
94
|
+
return out
|
|
95
|
+
if isinstance(value, (list, tuple, set)):
|
|
96
|
+
out = set()
|
|
97
|
+
for item in value:
|
|
98
|
+
out |= collect_ligand_indices(item, seen)
|
|
99
|
+
return out
|
|
100
|
+
out = set()
|
|
101
|
+
for attr in ("ligatom", "ligandring", "ring", "atoms", "a", "d", "positive", "negative", "donor", "acceptor", "x", "atom"):
|
|
102
|
+
if hasattr(value, attr):
|
|
103
|
+
out |= collect_ligand_indices(getattr(value, attr), seen)
|
|
104
|
+
return out
|
|
105
|
+
|
|
106
|
+
for name in ("hydrogen_bonds", "hydrophobic_contacts", "pistacking", "pication", "saltbridges"):
|
|
107
|
+
for interaction in getattr(interactions, name, []) or []:
|
|
108
|
+
atom_ids = sorted(collect_ligand_indices(interaction))
|
|
109
|
+
rows.append({"interaction_type": name, "atom_ids": sorted(set(atom_ids)), "repr": str(interaction)})
|
|
110
|
+
return rows
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def main():
|
|
114
|
+
p = argparse.ArgumentParser()
|
|
115
|
+
p.add_argument("--raw-root", required=True)
|
|
116
|
+
p.add_argument("--out", required=True)
|
|
117
|
+
args = p.parse_args()
|
|
118
|
+
root = Path(args.raw_root)
|
|
119
|
+
records = torch_load(root / "pdbbind_test.pt")
|
|
120
|
+
audit, attrition = [], []
|
|
121
|
+
for rec in records:
|
|
122
|
+
pdbid = rec["pdbid"]
|
|
123
|
+
mol = rec["rdmol"]
|
|
124
|
+
try:
|
|
125
|
+
feats = features(mol)
|
|
126
|
+
contacts = plip_contacts(Path(rec["protein_path"]), mol)
|
|
127
|
+
if not contacts:
|
|
128
|
+
attrition.append({"pdbid": pdbid, "reason": "no_plip_contacts"})
|
|
129
|
+
continue
|
|
130
|
+
audit.append({"pdbid": pdbid, "n_features": len(feats), "features_json": json.dumps(feats), "n_contacts": len(contacts), "contacts_json": json.dumps(contacts)})
|
|
131
|
+
except Exception as exc:
|
|
132
|
+
attrition.append({"pdbid": pdbid, "reason": type(exc).__name__, "detail": str(exc)[:300]})
|
|
133
|
+
out = Path(args.out)
|
|
134
|
+
out.parent.mkdir(parents=True, exist_ok=True)
|
|
135
|
+
pd.DataFrame(audit).to_csv(out, index=False)
|
|
136
|
+
out.with_name(out.stem + "_attrition.csv").write_text(pd.DataFrame(attrition).to_csv(index=False))
|
|
137
|
+
print(json.dumps({"audited": len(audit), "excluded": len(attrition), "output": str(out)}, indent=2))
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
def torch_load(path):
|
|
141
|
+
import torch
|
|
142
|
+
return torch.load(path, map_location="cpu")
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
if __name__ == "__main__":
|
|
146
|
+
main()
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""Audit whether a MANIMOL QM9 raw split matches GeoMol's test SMILES."""
|
|
3
|
+
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
|
|
6
|
+
import argparse
|
|
7
|
+
import csv
|
|
8
|
+
import json
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
|
|
11
|
+
import torch
|
|
12
|
+
from rdkit import Chem
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def canonical_smiles(value: str) -> str:
|
|
16
|
+
mol = Chem.MolFromSmiles(str(value))
|
|
17
|
+
if mol is None:
|
|
18
|
+
return str(value).strip()
|
|
19
|
+
return Chem.MolToSmiles(mol, isomericSmiles=True)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def record_smiles(record) -> str:
|
|
23
|
+
if isinstance(record, dict):
|
|
24
|
+
value = record.get("smiles") or record.get("smi")
|
|
25
|
+
mol = record.get("rdmol") or record.get("mol")
|
|
26
|
+
else:
|
|
27
|
+
value = getattr(record, "smiles", None)
|
|
28
|
+
mol = getattr(record, "rdmol", None) or getattr(record, "mol", None)
|
|
29
|
+
if value:
|
|
30
|
+
return canonical_smiles(value)
|
|
31
|
+
if mol is not None:
|
|
32
|
+
return Chem.MolToSmiles(mol, isomericSmiles=True)
|
|
33
|
+
raise ValueError("Record has neither SMILES nor an RDKit molecule")
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def load_official(path: Path) -> list[str]:
|
|
37
|
+
values = []
|
|
38
|
+
with path.open(newline="") as handle:
|
|
39
|
+
for row in csv.reader(handle):
|
|
40
|
+
if not row:
|
|
41
|
+
continue
|
|
42
|
+
value = row[0].strip()
|
|
43
|
+
if value.lower() in {"smiles", "smi"}:
|
|
44
|
+
continue
|
|
45
|
+
values.append(canonical_smiles(value))
|
|
46
|
+
return values
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def load_raw_unique(path: Path) -> list[str]:
|
|
50
|
+
records = torch.load(path, map_location="cpu")
|
|
51
|
+
values = []
|
|
52
|
+
previous = None
|
|
53
|
+
for record in records:
|
|
54
|
+
value = record_smiles(record)
|
|
55
|
+
if value != previous:
|
|
56
|
+
values.append(value)
|
|
57
|
+
previous = value
|
|
58
|
+
return values
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def main() -> None:
|
|
62
|
+
parser = argparse.ArgumentParser()
|
|
63
|
+
parser.add_argument("--raw-test", type=Path, required=True)
|
|
64
|
+
parser.add_argument("--official-csv", type=Path, required=True)
|
|
65
|
+
parser.add_argument("--output-json", type=Path, required=True)
|
|
66
|
+
args = parser.parse_args()
|
|
67
|
+
|
|
68
|
+
official = load_official(args.official_csv)
|
|
69
|
+
current = load_raw_unique(args.raw_test)
|
|
70
|
+
official_set = set(official)
|
|
71
|
+
current_set = set(current)
|
|
72
|
+
prefix_size = min(len(official), len(current))
|
|
73
|
+
prefix_matches = sum(a == b for a, b in zip(official, current))
|
|
74
|
+
report = {
|
|
75
|
+
"official_molecules": len(official),
|
|
76
|
+
"official_unique": len(official_set),
|
|
77
|
+
"current_test_molecules": len(current),
|
|
78
|
+
"current_test_unique": len(current_set),
|
|
79
|
+
"ordered_prefix_size": prefix_size,
|
|
80
|
+
"ordered_prefix_matches": prefix_matches,
|
|
81
|
+
"ordered_prefix_match_fraction": prefix_matches / max(1, prefix_size),
|
|
82
|
+
"set_overlap": len(official_set & current_set),
|
|
83
|
+
"official_covered_fraction": len(official_set & current_set) / max(1, len(official_set)),
|
|
84
|
+
"official_is_exact_current_prefix": current[: len(official)] == official,
|
|
85
|
+
"missing_official_examples": sorted(official_set - current_set)[:20],
|
|
86
|
+
"extra_prefix_examples": sorted(set(current[: len(official)]) - official_set)[:20],
|
|
87
|
+
}
|
|
88
|
+
args.output_json.parent.mkdir(parents=True, exist_ok=True)
|
|
89
|
+
args.output_json.write_text(json.dumps(report, indent=2) + "\n")
|
|
90
|
+
print(json.dumps(report, indent=2))
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
if __name__ == "__main__":
|
|
94
|
+
main()
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""Audit matched Stage-I/Stage-II checkpoint crosses from raw-pool caches."""
|
|
3
|
+
|
|
4
|
+
import argparse
|
|
5
|
+
import csv
|
|
6
|
+
import json
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
|
|
9
|
+
import numpy as np
|
|
10
|
+
import torch
|
|
11
|
+
from scipy.stats import spearmanr
|
|
12
|
+
|
|
13
|
+
from dataset.drugdataset import build_torsion_index
|
|
14
|
+
from models.mean_dispersion_torsion_context import mean_dispersion_torsion_context
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def load_records(path):
|
|
18
|
+
records = torch.load(path, map_location="cpu", weights_only=False)["records"]
|
|
19
|
+
return {int(record["mol_id"]): record for record in records}
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def audit(inputs):
|
|
23
|
+
records = {label: load_records(path) for label, path in inputs.items()}
|
|
24
|
+
labels = list(records)
|
|
25
|
+
ids = set(records[labels[0]])
|
|
26
|
+
if any(set(group) != ids for group in records.values()):
|
|
27
|
+
raise ValueError("Cross runs contain different molecule IDs")
|
|
28
|
+
|
|
29
|
+
rows = []
|
|
30
|
+
for mol_id in sorted(ids):
|
|
31
|
+
reference = records[labels[0]][mol_id]
|
|
32
|
+
reference_count = int(reference["ref_confs"].size(0))
|
|
33
|
+
target_count = int(reference["target_count"])
|
|
34
|
+
candidate_count = int(reference["raw_pool"].size(0))
|
|
35
|
+
smiles = reference["smiles"]
|
|
36
|
+
for label in labels:
|
|
37
|
+
record = records[label][mol_id]
|
|
38
|
+
if (int(record["ref_confs"].size(0)), int(record["target_count"]),
|
|
39
|
+
int(record["raw_pool"].size(0)), record["smiles"]) != (
|
|
40
|
+
reference_count, target_count, candidate_count, smiles):
|
|
41
|
+
raise ValueError(f"Unmatched protocol for mol_id={mol_id}, run={label}")
|
|
42
|
+
torsions = build_torsion_index(record["mol"])
|
|
43
|
+
prior = record["pglobal"].float()
|
|
44
|
+
width = record["dispersion_torsion_widths"].float()
|
|
45
|
+
score = record["dispersion_torsion_scores"].float()
|
|
46
|
+
valid = record["dispersion_torsion_valid"].bool()
|
|
47
|
+
context, context_valid = mean_dispersion_torsion_context(
|
|
48
|
+
prior, torch.zeros_like(prior), record["mol"], torsions, len(width)
|
|
49
|
+
)
|
|
50
|
+
if not torch.equal(valid, context_valid):
|
|
51
|
+
raise ValueError(f"Torsion ordering mismatch for mol_id={mol_id}, run={label}")
|
|
52
|
+
torsion_rows = torsions.t()
|
|
53
|
+
for bond_id in torch.where(valid)[0].tolist():
|
|
54
|
+
row = torsion_rows[bond_id]
|
|
55
|
+
rows.append({
|
|
56
|
+
"mol_id": mol_id,
|
|
57
|
+
"run": label,
|
|
58
|
+
"prior": label.split("_prior_")[0],
|
|
59
|
+
"decoder": label.split("_decoder")[0].split("_prior_")[1],
|
|
60
|
+
"bond_id": bond_id,
|
|
61
|
+
"atom_j": int(row[1]),
|
|
62
|
+
"atom_k": int(row[2]),
|
|
63
|
+
"mu_cross_mean": float(context[bond_id, 0]),
|
|
64
|
+
"mu_cross_q90": float(context[bond_id, 2]),
|
|
65
|
+
"mu_cross_max": float(context[bond_id, 3]),
|
|
66
|
+
"disp_cross_mean": float(score[bond_id]),
|
|
67
|
+
"proposal_width": float(width[bond_id]),
|
|
68
|
+
"width_at_bound": int(
|
|
69
|
+
float(width[bond_id]) <= 0.500001 or float(width[bond_id]) >= 1.999999
|
|
70
|
+
),
|
|
71
|
+
"reference_count": reference_count,
|
|
72
|
+
"target_count": target_count,
|
|
73
|
+
"candidate_count": candidate_count,
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
by_run = {}
|
|
77
|
+
for label in labels:
|
|
78
|
+
subset = [row for row in rows if row["run"] == label]
|
|
79
|
+
by_run[label] = {
|
|
80
|
+
"molecules": len(ids),
|
|
81
|
+
"valid_bond_rows": len(subset),
|
|
82
|
+
"mu_cross_mean": float(np.mean([row["mu_cross_mean"] for row in subset])),
|
|
83
|
+
"mu_cross_q90": float(np.mean([row["mu_cross_q90"] for row in subset])),
|
|
84
|
+
"mu_cross_max": float(np.mean([row["mu_cross_max"] for row in subset])),
|
|
85
|
+
"disp_cross_mean": float(np.mean([row["disp_cross_mean"] for row in subset])),
|
|
86
|
+
"proposal_width_mean": float(np.mean([row["proposal_width"] for row in subset])),
|
|
87
|
+
"width_at_bound_fraction": float(np.mean([row["width_at_bound"] for row in subset])),
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
rank_rows = []
|
|
91
|
+
for mol_id in sorted(ids):
|
|
92
|
+
old = [row for row in rows if row["mol_id"] == mol_id and row["run"] == "old_prior_old_decoder"]
|
|
93
|
+
new = [row for row in rows if row["mol_id"] == mol_id and row["run"] == "new_prior_new_decoder"]
|
|
94
|
+
if len(old) != len(new):
|
|
95
|
+
raise ValueError(f"Different valid-bond counts for mol_id={mol_id}")
|
|
96
|
+
old.sort(key=lambda row: row["bond_id"])
|
|
97
|
+
new.sort(key=lambda row: row["bond_id"])
|
|
98
|
+
rho = float(spearmanr(
|
|
99
|
+
[row["mu_cross_mean"] for row in old],
|
|
100
|
+
[row["mu_cross_mean"] for row in new],
|
|
101
|
+
).statistic) if len(old) >= 2 else float("nan")
|
|
102
|
+
rank_rows.append({
|
|
103
|
+
"mol_id": mol_id,
|
|
104
|
+
"valid_bonds": len(old),
|
|
105
|
+
"mean_context_spearman": rho if np.isfinite(rho) else None,
|
|
106
|
+
})
|
|
107
|
+
|
|
108
|
+
return rows, {"runs": by_run, "per_molecule_rank": rank_rows}
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def main():
|
|
112
|
+
parser = argparse.ArgumentParser()
|
|
113
|
+
parser.add_argument("--input", action="append", required=True, help="run=raw_pool.pt")
|
|
114
|
+
parser.add_argument("--output-csv", required=True)
|
|
115
|
+
parser.add_argument("--summary-json", required=True)
|
|
116
|
+
args = parser.parse_args()
|
|
117
|
+
inputs = dict(item.split("=", 1) for item in args.input)
|
|
118
|
+
rows, summary = audit(inputs)
|
|
119
|
+
csv_path = Path(args.output_csv)
|
|
120
|
+
csv_path.parent.mkdir(parents=True, exist_ok=True)
|
|
121
|
+
with csv_path.open("w", newline="") as handle:
|
|
122
|
+
writer = csv.DictWriter(handle, fieldnames=list(rows[0]))
|
|
123
|
+
writer.writeheader()
|
|
124
|
+
writer.writerows(rows)
|
|
125
|
+
json_path = Path(args.summary_json)
|
|
126
|
+
json_path.write_text(json.dumps(summary, indent=2, allow_nan=False) + "\n")
|
|
127
|
+
print(json.dumps(summary["runs"], indent=2))
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
if __name__ == "__main__":
|
|
131
|
+
main()
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""Freeze a result-blind, low-reference-count GEOM-Drugs diagnostic cohort."""
|
|
3
|
+
|
|
4
|
+
import argparse
|
|
5
|
+
import collections
|
|
6
|
+
import hashlib
|
|
7
|
+
import json
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
|
|
10
|
+
import torch
|
|
11
|
+
from rdkit.Chem import rdMolDescriptors
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def sha256(path):
|
|
15
|
+
digest = hashlib.sha256()
|
|
16
|
+
with Path(path).open("rb") as handle:
|
|
17
|
+
for block in iter(lambda: handle.read(1024 * 1024), b""):
|
|
18
|
+
digest.update(block)
|
|
19
|
+
return digest.hexdigest()
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def main():
|
|
23
|
+
parser = argparse.ArgumentParser()
|
|
24
|
+
parser.add_argument("--input", required=True)
|
|
25
|
+
parser.add_argument("--output", required=True)
|
|
26
|
+
parser.add_argument("--manifest", required=True)
|
|
27
|
+
parser.add_argument("--count", type=int, default=20)
|
|
28
|
+
parser.add_argument("--min-refs", type=int, default=8)
|
|
29
|
+
parser.add_argument("--max-refs", type=int, default=20)
|
|
30
|
+
parser.add_argument("--min-rotatable-bonds", type=int, default=2)
|
|
31
|
+
args = parser.parse_args()
|
|
32
|
+
|
|
33
|
+
source = Path(args.input)
|
|
34
|
+
output = Path(args.output)
|
|
35
|
+
manifest_path = Path(args.manifest)
|
|
36
|
+
if output.exists() or manifest_path.exists():
|
|
37
|
+
raise FileExistsError("Refusing to overwrite an existing frozen cohort")
|
|
38
|
+
|
|
39
|
+
records = torch.load(source, map_location="cpu", weights_only=False)
|
|
40
|
+
groups = collections.defaultdict(list)
|
|
41
|
+
for record in records:
|
|
42
|
+
groups[int(record["source_mol_id"])].append(record)
|
|
43
|
+
|
|
44
|
+
selected = []
|
|
45
|
+
for mol_id in sorted(groups):
|
|
46
|
+
group = groups[mol_id]
|
|
47
|
+
n_refs = len(group)
|
|
48
|
+
rotatable = rdMolDescriptors.CalcNumRotatableBonds(group[0]["rdmol"])
|
|
49
|
+
if args.min_refs <= n_refs <= args.max_refs and rotatable >= args.min_rotatable_bonds:
|
|
50
|
+
selected.append({
|
|
51
|
+
"source_mol_id": mol_id,
|
|
52
|
+
"reference_conformers": n_refs,
|
|
53
|
+
"rotatable_bonds": rotatable,
|
|
54
|
+
})
|
|
55
|
+
if len(selected) == args.count:
|
|
56
|
+
break
|
|
57
|
+
if len(selected) != args.count:
|
|
58
|
+
raise RuntimeError(f"Only {len(selected)} eligible molecules; requested {args.count}")
|
|
59
|
+
|
|
60
|
+
subset = [record for entry in selected for record in groups[entry["source_mol_id"]]]
|
|
61
|
+
assert len(subset) == sum(entry["reference_conformers"] for entry in selected)
|
|
62
|
+
output.parent.mkdir(parents=True, exist_ok=True)
|
|
63
|
+
manifest_path.parent.mkdir(parents=True, exist_ok=True)
|
|
64
|
+
torch.save(subset, output)
|
|
65
|
+
manifest = {
|
|
66
|
+
"purpose": "fixed low-reference-count mechanistic diagnostic, not representative benchmark",
|
|
67
|
+
"selection_rule": "first eligible source_mol_id in ascending order; no model outputs inspected",
|
|
68
|
+
"source_path": str(source),
|
|
69
|
+
"source_sha256": sha256(source),
|
|
70
|
+
"subset_path": str(output),
|
|
71
|
+
"subset_sha256": sha256(output),
|
|
72
|
+
"count": len(selected),
|
|
73
|
+
"total_reference_conformers": len(subset),
|
|
74
|
+
"min_refs": args.min_refs,
|
|
75
|
+
"max_refs": args.max_refs,
|
|
76
|
+
"min_rotatable_bonds": args.min_rotatable_bonds,
|
|
77
|
+
"molecules": selected,
|
|
78
|
+
}
|
|
79
|
+
manifest_path.write_text(json.dumps(manifest, indent=2) + "\n")
|
|
80
|
+
print(json.dumps({"count": len(selected), "total_refs": len(subset), "ids": [x["source_mol_id"] for x in selected]}))
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
if __name__ == "__main__":
|
|
84
|
+
main()
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""Build a deterministic QM9 test manifest that backfills skipped molecules."""
|
|
3
|
+
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
|
|
6
|
+
import argparse
|
|
7
|
+
import csv
|
|
8
|
+
import random
|
|
9
|
+
import sys
|
|
10
|
+
from pathlib import Path
|
|
11
|
+
|
|
12
|
+
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
|
13
|
+
|
|
14
|
+
from dataset import QM9Dataset
|
|
15
|
+
from infer_base_conformers import build_raw_reference_groups, get_data_mol_key, get_split_indices
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def parse_args():
|
|
19
|
+
parser = argparse.ArgumentParser()
|
|
20
|
+
parser.add_argument("--data-root", default="data/geom_qm9_full")
|
|
21
|
+
parser.add_argument("--raw-prefix", default="geom_qm9")
|
|
22
|
+
parser.add_argument("--output-dir", required=True)
|
|
23
|
+
parser.add_argument("--shards", type=int, default=4)
|
|
24
|
+
parser.add_argument("--target-per-shard", type=int, default=250)
|
|
25
|
+
parser.add_argument("--candidates-per-shard", type=int, default=500)
|
|
26
|
+
parser.add_argument("--seed", type=int, default=20260901)
|
|
27
|
+
return parser.parse_args()
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def main():
|
|
31
|
+
args = parse_args()
|
|
32
|
+
if args.candidates_per_shard < args.target_per_shard:
|
|
33
|
+
raise ValueError("candidates-per-shard must be at least target-per-shard")
|
|
34
|
+
|
|
35
|
+
root = Path(args.data_root)
|
|
36
|
+
dataset = QM9Dataset(
|
|
37
|
+
name="QM9",
|
|
38
|
+
root=str(root),
|
|
39
|
+
max_confs=5,
|
|
40
|
+
raw_prefix=args.raw_prefix,
|
|
41
|
+
keep_full_test_conformers=True,
|
|
42
|
+
)
|
|
43
|
+
test_subset = dataset[get_split_indices(dataset, "test")]
|
|
44
|
+
records = {}
|
|
45
|
+
for index in range(len(test_subset)):
|
|
46
|
+
data = test_subset[index]
|
|
47
|
+
records.setdefault(get_data_mol_key(data), data)
|
|
48
|
+
|
|
49
|
+
raw_groups = build_raw_reference_groups(
|
|
50
|
+
str(root / f"{args.raw_prefix}_test.pt"), test_subset
|
|
51
|
+
)
|
|
52
|
+
eligible = [
|
|
53
|
+
key for key in records
|
|
54
|
+
if key in raw_groups and len(raw_groups[key]["confs"]) > 0
|
|
55
|
+
]
|
|
56
|
+
random.Random(args.seed).shuffle(eligible)
|
|
57
|
+
|
|
58
|
+
requested = args.shards * args.candidates_per_shard
|
|
59
|
+
if len(eligible) < requested:
|
|
60
|
+
raise RuntimeError(f"Need {requested} eligible molecules, found {len(eligible)}")
|
|
61
|
+
|
|
62
|
+
selected = eligible[:requested]
|
|
63
|
+
items = sorted(
|
|
64
|
+
[(len(raw_groups[key]["confs"]), key) for key in selected], reverse=True
|
|
65
|
+
)
|
|
66
|
+
shards = [[] for _ in range(args.shards)]
|
|
67
|
+
loads = [0] * args.shards
|
|
68
|
+
for ref_count, key in items:
|
|
69
|
+
choices = [index for index in range(args.shards) if len(shards[index]) < args.candidates_per_shard]
|
|
70
|
+
shard = min(choices, key=lambda index: (loads[index], len(shards[index])))
|
|
71
|
+
row = (key, key.rsplit("__", 1)[0], ref_count, ref_count * 30)
|
|
72
|
+
shards[shard].append(row)
|
|
73
|
+
loads[shard] += ref_count * 30
|
|
74
|
+
|
|
75
|
+
output = Path(args.output_dir)
|
|
76
|
+
output.mkdir(parents=True, exist_ok=True)
|
|
77
|
+
header = ["mol_key", "smiles", "ref_count", "estimated_candidates"]
|
|
78
|
+
with (output / "manifest_candidates.csv").open("w", newline="") as handle:
|
|
79
|
+
writer = csv.writer(handle)
|
|
80
|
+
writer.writerow(header)
|
|
81
|
+
for ref_count, key in items:
|
|
82
|
+
writer.writerow([key, key.rsplit("__", 1)[0], ref_count, ref_count * 30])
|
|
83
|
+
for index, rows in enumerate(shards):
|
|
84
|
+
with (output / f"shard{index}.csv").open("w", newline="") as handle:
|
|
85
|
+
writer = csv.writer(handle)
|
|
86
|
+
writer.writerow(header)
|
|
87
|
+
writer.writerows(rows)
|
|
88
|
+
|
|
89
|
+
print({
|
|
90
|
+
"eligible_test_molecules": len(eligible),
|
|
91
|
+
"candidate_manifest_size": len(selected),
|
|
92
|
+
"target_valid_molecules": args.shards * args.target_per_shard,
|
|
93
|
+
"candidate_counts": [len(shard) for shard in shards],
|
|
94
|
+
"estimated_candidate_loads": loads,
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
if __name__ == "__main__":
|
|
99
|
+
main()
|