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
dataset/drugdataset.py
ADDED
|
@@ -0,0 +1,644 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import pickle
|
|
3
|
+
from collections import OrderedDict
|
|
4
|
+
|
|
5
|
+
import torch
|
|
6
|
+
from torch_geometric.data import InMemoryDataset, Data
|
|
7
|
+
from rdkit import Chem, RDLogger
|
|
8
|
+
from rdkit.Geometry import Point3D
|
|
9
|
+
from tqdm import tqdm
|
|
10
|
+
|
|
11
|
+
from .smiles2graph import smile2graph4GEOM
|
|
12
|
+
|
|
13
|
+
# The debug/top20 files were serialized with a newer RDKit. Older RDKit
|
|
14
|
+
# emits a repetitive depickling compatibility notice although the records
|
|
15
|
+
# remain readable for this pipeline; suppress that non-actionable noise.
|
|
16
|
+
RDLogger.DisableLog("rdApp.*")
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def _as_float_tensor(x):
|
|
20
|
+
if x is None:
|
|
21
|
+
return None
|
|
22
|
+
if torch.is_tensor(x):
|
|
23
|
+
return x.float()
|
|
24
|
+
return torch.tensor(x, dtype=torch.float)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def build_torsion_index(mol):
|
|
28
|
+
"""
|
|
29
|
+
torsion_index shape:
|
|
30
|
+
[4, T]
|
|
31
|
+
"""
|
|
32
|
+
torsions = []
|
|
33
|
+
|
|
34
|
+
if mol is None:
|
|
35
|
+
return torch.empty((4, 0), dtype=torch.long)
|
|
36
|
+
|
|
37
|
+
for bond in mol.GetBonds():
|
|
38
|
+
if bond.IsInRing():
|
|
39
|
+
continue
|
|
40
|
+
|
|
41
|
+
j = bond.GetBeginAtomIdx()
|
|
42
|
+
k = bond.GetEndAtomIdx()
|
|
43
|
+
|
|
44
|
+
nbr_j = [
|
|
45
|
+
atom.GetIdx()
|
|
46
|
+
for atom in mol.GetAtomWithIdx(j).GetNeighbors()
|
|
47
|
+
if atom.GetIdx() != k
|
|
48
|
+
]
|
|
49
|
+
|
|
50
|
+
nbr_k = [
|
|
51
|
+
atom.GetIdx()
|
|
52
|
+
for atom in mol.GetAtomWithIdx(k).GetNeighbors()
|
|
53
|
+
if atom.GetIdx() != j
|
|
54
|
+
]
|
|
55
|
+
|
|
56
|
+
for i in nbr_j:
|
|
57
|
+
for l in nbr_k:
|
|
58
|
+
if len({i, j, k, l}) == 4:
|
|
59
|
+
torsions.append([i, j, k, l])
|
|
60
|
+
|
|
61
|
+
if len(torsions) == 0:
|
|
62
|
+
return torch.empty((4, 0), dtype=torch.long)
|
|
63
|
+
|
|
64
|
+
return torch.tensor(
|
|
65
|
+
torsions,
|
|
66
|
+
dtype=torch.long,
|
|
67
|
+
).t().contiguous()
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def enrich_edge_attr_with_bond_flags(mol, edge_index, edge_attr):
|
|
71
|
+
"""
|
|
72
|
+
append:
|
|
73
|
+
aromatic
|
|
74
|
+
conjugated
|
|
75
|
+
ring
|
|
76
|
+
"""
|
|
77
|
+
if edge_index is None:
|
|
78
|
+
return edge_attr
|
|
79
|
+
|
|
80
|
+
extras = []
|
|
81
|
+
|
|
82
|
+
for eid in range(edge_index.size(1)):
|
|
83
|
+
u = int(edge_index[0, eid])
|
|
84
|
+
v = int(edge_index[1, eid])
|
|
85
|
+
|
|
86
|
+
bond = mol.GetBondBetweenAtoms(u, v)
|
|
87
|
+
|
|
88
|
+
if bond is None:
|
|
89
|
+
extras.append([0.0, 0.0, 0.0])
|
|
90
|
+
else:
|
|
91
|
+
extras.append([
|
|
92
|
+
float(bond.GetIsAromatic()),
|
|
93
|
+
float(bond.GetIsConjugated()),
|
|
94
|
+
float(bond.IsInRing()),
|
|
95
|
+
])
|
|
96
|
+
|
|
97
|
+
extras = torch.tensor(extras, dtype=torch.float)
|
|
98
|
+
|
|
99
|
+
if edge_attr is None:
|
|
100
|
+
return extras
|
|
101
|
+
|
|
102
|
+
edge_attr = _as_float_tensor(edge_attr)
|
|
103
|
+
|
|
104
|
+
if edge_attr.dim() == 1:
|
|
105
|
+
edge_attr = edge_attr.view(-1, 1)
|
|
106
|
+
|
|
107
|
+
if edge_attr.size(0) != extras.size(0):
|
|
108
|
+
return extras
|
|
109
|
+
|
|
110
|
+
return torch.cat([edge_attr, extras], dim=-1)
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
class QM9Dataset(InMemoryDataset):
|
|
114
|
+
def __init__(
|
|
115
|
+
self,
|
|
116
|
+
name="QM9",
|
|
117
|
+
root="data",
|
|
118
|
+
transform=None,
|
|
119
|
+
pre_transform=None,
|
|
120
|
+
pre_filter=None,
|
|
121
|
+
max_confs=5,
|
|
122
|
+
raw_prefix="converted",
|
|
123
|
+
keep_full_test_conformers=False,
|
|
124
|
+
test_raw_filename=None,
|
|
125
|
+
train_raw_filename=None,
|
|
126
|
+
val_raw_filename=None,
|
|
127
|
+
process_train_mol_limit=0,
|
|
128
|
+
process_val_mol_limit=0,
|
|
129
|
+
process_test_mol_limit=0,
|
|
130
|
+
store_rdkit_objects=True,
|
|
131
|
+
):
|
|
132
|
+
self.raw_prefix = str(raw_prefix)
|
|
133
|
+
self.name = name
|
|
134
|
+
self.root = root
|
|
135
|
+
self.requested_max_confs = int(max_confs)
|
|
136
|
+
self.keep_full_test_conformers = bool(keep_full_test_conformers)
|
|
137
|
+
self.test_raw_filename = str(test_raw_filename) if test_raw_filename else None
|
|
138
|
+
self.train_raw_filename = str(train_raw_filename) if train_raw_filename else None
|
|
139
|
+
self.val_raw_filename = str(val_raw_filename) if val_raw_filename else None
|
|
140
|
+
self.process_train_mol_limit = int(process_train_mol_limit)
|
|
141
|
+
self.process_val_mol_limit = int(process_val_mol_limit)
|
|
142
|
+
self.process_test_mol_limit = int(process_test_mol_limit)
|
|
143
|
+
self.store_rdkit_objects = bool(store_rdkit_objects)
|
|
144
|
+
# ``0`` is an explicit auto mode. We resolve it before PyG checks the
|
|
145
|
+
# processed-file path so caches from different conformer caps can never
|
|
146
|
+
# be silently reused.
|
|
147
|
+
self.max_confs = self._resolve_max_confs(self.requested_max_confs)
|
|
148
|
+
|
|
149
|
+
super().__init__(
|
|
150
|
+
root,
|
|
151
|
+
transform,
|
|
152
|
+
pre_transform,
|
|
153
|
+
pre_filter,
|
|
154
|
+
)
|
|
155
|
+
|
|
156
|
+
self.data, self.slices = torch.load(
|
|
157
|
+
self.processed_paths[0],
|
|
158
|
+
weights_only=False,
|
|
159
|
+
)
|
|
160
|
+
|
|
161
|
+
self.train_index, self.valid_index, self.test_index = pickle.load(
|
|
162
|
+
open(self.processed_paths[1], "rb")
|
|
163
|
+
)
|
|
164
|
+
|
|
165
|
+
@property
|
|
166
|
+
def raw_dir(self):
|
|
167
|
+
return self.root
|
|
168
|
+
|
|
169
|
+
@property
|
|
170
|
+
def raw_file_names(self):
|
|
171
|
+
if any((self.train_raw_filename, self.val_raw_filename, self.test_raw_filename)):
|
|
172
|
+
return [
|
|
173
|
+
self.train_raw_filename or f"{self.raw_prefix}_train.pt",
|
|
174
|
+
self.val_raw_filename or f"{self.raw_prefix}_val.pt",
|
|
175
|
+
self.test_raw_filename or f"{self.raw_prefix}_test.pt",
|
|
176
|
+
]
|
|
177
|
+
if self.raw_prefix == "geom_qm9":
|
|
178
|
+
return [
|
|
179
|
+
"geom_qm9_train.pt",
|
|
180
|
+
"geom_qm9_val.pt",
|
|
181
|
+
"geom_qm9_test.pt",
|
|
182
|
+
]
|
|
183
|
+
if self.raw_prefix == "geom_qm9_full":
|
|
184
|
+
return [
|
|
185
|
+
"geom_qm9_train_full.pt",
|
|
186
|
+
"geom_qm9_val_full.pt",
|
|
187
|
+
"geom_qm9_test_full.pt",
|
|
188
|
+
]
|
|
189
|
+
if self.raw_prefix == "geom_drugs":
|
|
190
|
+
return [
|
|
191
|
+
"geom_drugs_train.pt",
|
|
192
|
+
"geom_drugs_val.pt",
|
|
193
|
+
"geom_drugs_test.pt",
|
|
194
|
+
]
|
|
195
|
+
if self.raw_prefix == "geom_drugs_revisited":
|
|
196
|
+
return [
|
|
197
|
+
"geom_drugs_revisited_train.pt",
|
|
198
|
+
"geom_drugs_revisited_val.pt",
|
|
199
|
+
"geom_drugs_revisited_test.pt",
|
|
200
|
+
]
|
|
201
|
+
if self.raw_prefix.startswith("geom_drugs75k"):
|
|
202
|
+
return [
|
|
203
|
+
f"{self.raw_prefix}_train.pt",
|
|
204
|
+
f"{self.raw_prefix}_val.pt",
|
|
205
|
+
f"{self.raw_prefix}_test.pt",
|
|
206
|
+
]
|
|
207
|
+
return [
|
|
208
|
+
"train_converted_data1K.pt",
|
|
209
|
+
"val_converted_data100.pt",
|
|
210
|
+
"test_converted_data100.pt",
|
|
211
|
+
]
|
|
212
|
+
|
|
213
|
+
@property
|
|
214
|
+
def processed_dir(self):
|
|
215
|
+
return os.path.join(self.root, "processed_data")
|
|
216
|
+
|
|
217
|
+
@property
|
|
218
|
+
def processed_file_names(self):
|
|
219
|
+
# Legacy defaults stay byte-for-byte compatible with the established
|
|
220
|
+
# best-method cache. Full-test retention is opt-in for new studies.
|
|
221
|
+
if self.max_confs == 5 and not self.keep_full_test_conformers:
|
|
222
|
+
suffix = ""
|
|
223
|
+
elif self.keep_full_test_conformers:
|
|
224
|
+
suffix = f"_v2_c{self.max_confs}_fulltest"
|
|
225
|
+
else:
|
|
226
|
+
suffix = f"_v2_c{self.max_confs}"
|
|
227
|
+
if self.process_train_mol_limit or self.process_val_mol_limit or self.process_test_mol_limit:
|
|
228
|
+
suffix += (f"_train{self.process_train_mol_limit}"
|
|
229
|
+
f"_val{self.process_val_mol_limit}"
|
|
230
|
+
f"_test{self.process_test_mol_limit}")
|
|
231
|
+
if self.train_raw_filename or self.val_raw_filename or self.test_raw_filename:
|
|
232
|
+
suffix += "_customraw"
|
|
233
|
+
if not self.store_rdkit_objects:
|
|
234
|
+
suffix += "_no_rdkit"
|
|
235
|
+
if self.raw_prefix == "geom_qm9":
|
|
236
|
+
return [
|
|
237
|
+
f"data_geom_qm9_probmanifold_conformer_unit{suffix}.pt",
|
|
238
|
+
f"split_geom_qm9_probmanifold_conformer_unit{suffix}.pt",
|
|
239
|
+
]
|
|
240
|
+
if self.raw_prefix == "geom_qm9_full":
|
|
241
|
+
return [
|
|
242
|
+
f"data_geom_qm9_full_probmanifold_conformer_unit{suffix}.pt",
|
|
243
|
+
f"split_geom_qm9_full_probmanifold_conformer_unit{suffix}.pt",
|
|
244
|
+
]
|
|
245
|
+
if self.raw_prefix == "geom_drugs":
|
|
246
|
+
return [
|
|
247
|
+
f"data_geom_drugs_probmanifold_conformer_unit{suffix}.pt",
|
|
248
|
+
f"split_geom_drugs_probmanifold_conformer_unit{suffix}.pt",
|
|
249
|
+
]
|
|
250
|
+
if self.raw_prefix == "geom_drugs_revisited":
|
|
251
|
+
return [
|
|
252
|
+
f"data_geom_drugs_revisited_probmanifold_conformer_unit{suffix}.pt",
|
|
253
|
+
f"split_geom_drugs_revisited_probmanifold_conformer_unit{suffix}.pt",
|
|
254
|
+
]
|
|
255
|
+
if self.raw_prefix.startswith("geom_drugs75k"):
|
|
256
|
+
return [
|
|
257
|
+
f"data_{self.raw_prefix}_probmanifold_conformer_unit{suffix}.pt",
|
|
258
|
+
f"split_{self.raw_prefix}_probmanifold_conformer_unit{suffix}.pt",
|
|
259
|
+
]
|
|
260
|
+
return [
|
|
261
|
+
f"data_probmanifold_conformer_unit{suffix}.pt",
|
|
262
|
+
f"split_probmanifold_conformer_unit{suffix}.pt",
|
|
263
|
+
]
|
|
264
|
+
|
|
265
|
+
def _resolve_max_confs(self, requested_max_confs):
|
|
266
|
+
"""Resolve ``max_confs=0`` from train/validation conformer groups.
|
|
267
|
+
|
|
268
|
+
The conformer-ID embedding is trained only on train/validation data.
|
|
269
|
+
Test sets may contain many more reference conformers, but using their
|
|
270
|
+
maximum here would create embedding rows that never receive training
|
|
271
|
+
supervision. Full test ensembles are still available separately to
|
|
272
|
+
raw-reference evaluation in ``infer_base_conformers.py``.
|
|
273
|
+
"""
|
|
274
|
+
if requested_max_confs > 0:
|
|
275
|
+
return requested_max_confs
|
|
276
|
+
if requested_max_confs < 0:
|
|
277
|
+
raise ValueError("max_confs must be positive, or 0 for automatic detection.")
|
|
278
|
+
|
|
279
|
+
# Raw GEOM files can contain RDKit objects serialized by a newer
|
|
280
|
+
# version. Their repetitive compatibility warnings add no diagnostic
|
|
281
|
+
# value during a count-only pass.
|
|
282
|
+
RDLogger.DisableLog("rdApp.warning")
|
|
283
|
+
maximum = 0
|
|
284
|
+
for filename in self.raw_file_names[:2]:
|
|
285
|
+
path = os.path.join(self.root, filename)
|
|
286
|
+
if not os.path.exists(path):
|
|
287
|
+
raise FileNotFoundError(path)
|
|
288
|
+
data_list = torch.load(path, map_location="cpu", weights_only=False)
|
|
289
|
+
counts = {}
|
|
290
|
+
for datapoint in data_list:
|
|
291
|
+
try:
|
|
292
|
+
mol = datapoint["rdmol"]
|
|
293
|
+
smiles = str(datapoint["smiles"])
|
|
294
|
+
pos = torch.as_tensor(datapoint["pos"])
|
|
295
|
+
except (KeyError, TypeError, AttributeError):
|
|
296
|
+
continue
|
|
297
|
+
if mol is None or pos.dim() != 2 or pos.size(0) != mol.GetNumAtoms():
|
|
298
|
+
continue
|
|
299
|
+
key = (smiles, int(mol.GetNumAtoms()))
|
|
300
|
+
counts[key] = counts.get(key, 0) + 1
|
|
301
|
+
if counts:
|
|
302
|
+
maximum = max(maximum, max(counts.values()))
|
|
303
|
+
del data_list
|
|
304
|
+
|
|
305
|
+
if maximum < 1:
|
|
306
|
+
raise RuntimeError("Automatic max_confs detection found no valid conformers.")
|
|
307
|
+
print(
|
|
308
|
+
f"Auto-detected max_confs={maximum} from train/validation data "
|
|
309
|
+
f"for raw_prefix={self.raw_prefix}."
|
|
310
|
+
)
|
|
311
|
+
return maximum
|
|
312
|
+
|
|
313
|
+
def _make_mol_with_conformer(self, mol, coords):
|
|
314
|
+
mol_conf = Chem.Mol(mol)
|
|
315
|
+
|
|
316
|
+
try:
|
|
317
|
+
mol_conf.RemoveAllConformers()
|
|
318
|
+
except Exception:
|
|
319
|
+
pass
|
|
320
|
+
|
|
321
|
+
conf = Chem.Conformer(mol_conf.GetNumAtoms())
|
|
322
|
+
|
|
323
|
+
coords = coords.cpu()
|
|
324
|
+
|
|
325
|
+
for atom_idx in range(mol_conf.GetNumAtoms()):
|
|
326
|
+
x, y, z = coords[atom_idx].tolist()
|
|
327
|
+
conf.SetAtomPosition(
|
|
328
|
+
atom_idx,
|
|
329
|
+
Point3D(float(x), float(y), float(z)),
|
|
330
|
+
)
|
|
331
|
+
|
|
332
|
+
mol_conf.AddConformer(conf, assignId=True)
|
|
333
|
+
|
|
334
|
+
return mol_conf
|
|
335
|
+
|
|
336
|
+
def _group_by_molecule(self, datalist, molecule_limit=0):
|
|
337
|
+
groups = OrderedDict()
|
|
338
|
+
|
|
339
|
+
for idx, datapoint in enumerate(datalist):
|
|
340
|
+
smiles = datapoint.get("smiles", None)
|
|
341
|
+
mol = datapoint.get("rdmol", None)
|
|
342
|
+
|
|
343
|
+
if smiles is None or mol is None:
|
|
344
|
+
continue
|
|
345
|
+
|
|
346
|
+
key = (
|
|
347
|
+
smiles,
|
|
348
|
+
mol.GetNumAtoms(),
|
|
349
|
+
)
|
|
350
|
+
|
|
351
|
+
if key not in groups:
|
|
352
|
+
if molecule_limit and len(groups) >= int(molecule_limit):
|
|
353
|
+
# Raw GEOM records are ordered by molecule. Once the
|
|
354
|
+
# requested prefix is complete, avoid retaining or
|
|
355
|
+
# processing the rest of a large debug split.
|
|
356
|
+
continue
|
|
357
|
+
groups[key] = []
|
|
358
|
+
|
|
359
|
+
groups[key].append((idx, datapoint))
|
|
360
|
+
|
|
361
|
+
return groups
|
|
362
|
+
|
|
363
|
+
def _process_split(
|
|
364
|
+
self,
|
|
365
|
+
datalist,
|
|
366
|
+
split_name,
|
|
367
|
+
mol_offset,
|
|
368
|
+
sample_offset,
|
|
369
|
+
molecule_limit=0,
|
|
370
|
+
):
|
|
371
|
+
processed = []
|
|
372
|
+
|
|
373
|
+
groups = self._group_by_molecule(datalist, molecule_limit)
|
|
374
|
+
|
|
375
|
+
print(
|
|
376
|
+
f"[{split_name}] "
|
|
377
|
+
f"molecules={len(groups)} "
|
|
378
|
+
f"raw_conformers={len(datalist)}"
|
|
379
|
+
)
|
|
380
|
+
|
|
381
|
+
local_sample_id = 0
|
|
382
|
+
|
|
383
|
+
for local_mol_id, ((smiles, num_atoms), items) in enumerate(
|
|
384
|
+
tqdm(groups.items())
|
|
385
|
+
):
|
|
386
|
+
global_mol_id = mol_offset + local_mol_id
|
|
387
|
+
|
|
388
|
+
mol = items[0][1]["rdmol"]
|
|
389
|
+
|
|
390
|
+
try:
|
|
391
|
+
x, edge_index, edge_attr, vdw_radii = smile2graph4GEOM(
|
|
392
|
+
items[0][1]
|
|
393
|
+
)
|
|
394
|
+
except Exception as e:
|
|
395
|
+
print(f"skip graph fail: {smiles}, {e}")
|
|
396
|
+
continue
|
|
397
|
+
|
|
398
|
+
if x.size(0) != num_atoms:
|
|
399
|
+
continue
|
|
400
|
+
|
|
401
|
+
edge_attr = enrich_edge_attr_with_bond_flags(
|
|
402
|
+
mol,
|
|
403
|
+
edge_index,
|
|
404
|
+
edge_attr,
|
|
405
|
+
)
|
|
406
|
+
|
|
407
|
+
torsion_index = build_torsion_index(mol)
|
|
408
|
+
|
|
409
|
+
valid = []
|
|
410
|
+
|
|
411
|
+
for conf_id, (data_id, datapoint) in enumerate(items):
|
|
412
|
+
# Full test references are an opt-in dataset mode for new
|
|
413
|
+
# ensemble studies; the historical protocol remains intact.
|
|
414
|
+
if (split_name != "test" or not self.keep_full_test_conformers) and len(valid) >= self.max_confs:
|
|
415
|
+
break
|
|
416
|
+
|
|
417
|
+
if "pos" not in datapoint:
|
|
418
|
+
continue
|
|
419
|
+
|
|
420
|
+
pos = torch.as_tensor(
|
|
421
|
+
datapoint["pos"],
|
|
422
|
+
dtype=torch.float,
|
|
423
|
+
)
|
|
424
|
+
|
|
425
|
+
if pos.dim() != 2:
|
|
426
|
+
continue
|
|
427
|
+
|
|
428
|
+
if pos.size(0) != x.size(0):
|
|
429
|
+
continue
|
|
430
|
+
|
|
431
|
+
valid.append(
|
|
432
|
+
(
|
|
433
|
+
conf_id,
|
|
434
|
+
data_id,
|
|
435
|
+
datapoint,
|
|
436
|
+
pos,
|
|
437
|
+
)
|
|
438
|
+
)
|
|
439
|
+
|
|
440
|
+
if len(valid) == 0:
|
|
441
|
+
continue
|
|
442
|
+
|
|
443
|
+
num_confs = len(valid)
|
|
444
|
+
|
|
445
|
+
for conf_id, data_id, datapoint, pos in valid:
|
|
446
|
+
sample_id = sample_offset + local_sample_id
|
|
447
|
+
|
|
448
|
+
mol_embedded = (self._make_mol_with_conformer(mol, pos)
|
|
449
|
+
if self.store_rdkit_objects else None)
|
|
450
|
+
|
|
451
|
+
data = Data(
|
|
452
|
+
x=x.clone(),
|
|
453
|
+
edge_index=edge_index.clone(),
|
|
454
|
+
edge_attr=edge_attr.clone(),
|
|
455
|
+
torsion_index=torsion_index.clone(),
|
|
456
|
+
|
|
457
|
+
pos=pos,
|
|
458
|
+
|
|
459
|
+
smiles=smiles,
|
|
460
|
+
mol_key=f"{smiles}__{num_atoms}",
|
|
461
|
+
|
|
462
|
+
mol_id=torch.tensor(
|
|
463
|
+
global_mol_id,
|
|
464
|
+
dtype=torch.long,
|
|
465
|
+
),
|
|
466
|
+
|
|
467
|
+
split_mol_id=torch.tensor(
|
|
468
|
+
local_mol_id,
|
|
469
|
+
dtype=torch.long,
|
|
470
|
+
),
|
|
471
|
+
|
|
472
|
+
conf_id=torch.tensor(
|
|
473
|
+
conf_id,
|
|
474
|
+
dtype=torch.long,
|
|
475
|
+
),
|
|
476
|
+
|
|
477
|
+
idx=torch.tensor(
|
|
478
|
+
sample_id,
|
|
479
|
+
dtype=torch.long,
|
|
480
|
+
),
|
|
481
|
+
|
|
482
|
+
sample_id=torch.tensor(
|
|
483
|
+
sample_id,
|
|
484
|
+
dtype=torch.long,
|
|
485
|
+
),
|
|
486
|
+
|
|
487
|
+
num_atoms=torch.tensor(
|
|
488
|
+
x.size(0),
|
|
489
|
+
dtype=torch.long,
|
|
490
|
+
),
|
|
491
|
+
|
|
492
|
+
num_confs_of_mol=torch.tensor(
|
|
493
|
+
num_confs,
|
|
494
|
+
dtype=torch.long,
|
|
495
|
+
),
|
|
496
|
+
|
|
497
|
+
boltzmannweight=torch.tensor(
|
|
498
|
+
[
|
|
499
|
+
float(
|
|
500
|
+
datapoint.get(
|
|
501
|
+
"boltzmannweight",
|
|
502
|
+
1.0,
|
|
503
|
+
)
|
|
504
|
+
)
|
|
505
|
+
],
|
|
506
|
+
dtype=torch.float,
|
|
507
|
+
),
|
|
508
|
+
|
|
509
|
+
totalenergy=torch.tensor(
|
|
510
|
+
[
|
|
511
|
+
float(
|
|
512
|
+
datapoint.get(
|
|
513
|
+
"totalenergy",
|
|
514
|
+
0.0,
|
|
515
|
+
)
|
|
516
|
+
)
|
|
517
|
+
],
|
|
518
|
+
dtype=torch.float,
|
|
519
|
+
),
|
|
520
|
+
|
|
521
|
+
rdmol=mol if self.store_rdkit_objects else None,
|
|
522
|
+
rdmol_embedded=mol_embedded,
|
|
523
|
+
vdw_radii=vdw_radii,
|
|
524
|
+
)
|
|
525
|
+
|
|
526
|
+
data.batch_num_nodes = data.num_nodes
|
|
527
|
+
|
|
528
|
+
if self.pre_filter is not None:
|
|
529
|
+
if not self.pre_filter(data):
|
|
530
|
+
continue
|
|
531
|
+
|
|
532
|
+
if self.pre_transform is not None:
|
|
533
|
+
data = self.pre_transform(data)
|
|
534
|
+
|
|
535
|
+
processed.append(data)
|
|
536
|
+
local_sample_id += 1
|
|
537
|
+
|
|
538
|
+
return processed, len(processed), len(groups)
|
|
539
|
+
|
|
540
|
+
def process(self):
|
|
541
|
+
train_file, valid_file, test_file = self.raw_file_names
|
|
542
|
+
|
|
543
|
+
train_path = os.path.join(self.root, train_file)
|
|
544
|
+
valid_path = os.path.join(self.root, valid_file)
|
|
545
|
+
test_path = os.path.join(self.root, test_file)
|
|
546
|
+
|
|
547
|
+
if not os.path.exists(train_path):
|
|
548
|
+
raise FileNotFoundError(train_path)
|
|
549
|
+
|
|
550
|
+
if not os.path.exists(valid_path):
|
|
551
|
+
raise FileNotFoundError(valid_path)
|
|
552
|
+
|
|
553
|
+
if not os.path.exists(test_path):
|
|
554
|
+
raise FileNotFoundError(test_path)
|
|
555
|
+
|
|
556
|
+
# Load and release each raw split sequentially. This is important for
|
|
557
|
+
# large GEOM files: holding train, validation, and test RDKit objects
|
|
558
|
+
# simultaneously can exceed laptop memory before graph processing starts.
|
|
559
|
+
if self.process_train_mol_limit < 0:
|
|
560
|
+
train_list, train_num, train_mols = [], 0, 0
|
|
561
|
+
else:
|
|
562
|
+
train_data = torch.load(train_path, weights_only=False)
|
|
563
|
+
train_list, train_num, train_mols = self._process_split(
|
|
564
|
+
train_data,
|
|
565
|
+
"train",
|
|
566
|
+
0,
|
|
567
|
+
0,
|
|
568
|
+
self.process_train_mol_limit,
|
|
569
|
+
)
|
|
570
|
+
del train_data
|
|
571
|
+
|
|
572
|
+
if self.process_val_mol_limit < 0:
|
|
573
|
+
valid_list, valid_num, valid_mols = [], 0, 0
|
|
574
|
+
else:
|
|
575
|
+
valid_data = torch.load(valid_path, weights_only=False)
|
|
576
|
+
valid_list, valid_num, valid_mols = self._process_split(
|
|
577
|
+
valid_data,
|
|
578
|
+
"valid",
|
|
579
|
+
train_mols,
|
|
580
|
+
train_num,
|
|
581
|
+
self.process_val_mol_limit,
|
|
582
|
+
)
|
|
583
|
+
del valid_data
|
|
584
|
+
|
|
585
|
+
if self.process_test_mol_limit < 0:
|
|
586
|
+
# Training-only pilot caches do not need any test graphs. Avoid
|
|
587
|
+
# loading a large held-out raw file until the separate evaluation
|
|
588
|
+
# task requests it explicitly.
|
|
589
|
+
test_list, test_num, test_mols = [], 0, 0
|
|
590
|
+
else:
|
|
591
|
+
test_data = torch.load(test_path, weights_only=False)
|
|
592
|
+
test_list, test_num, test_mols = self._process_split(
|
|
593
|
+
test_data,
|
|
594
|
+
"test",
|
|
595
|
+
train_mols + valid_mols,
|
|
596
|
+
train_num + valid_num,
|
|
597
|
+
self.process_test_mol_limit,
|
|
598
|
+
)
|
|
599
|
+
del test_data
|
|
600
|
+
|
|
601
|
+
all_data = train_list + valid_list + test_list
|
|
602
|
+
|
|
603
|
+
train_index = list(range(train_num))
|
|
604
|
+
|
|
605
|
+
valid_index = list(
|
|
606
|
+
range(
|
|
607
|
+
train_num,
|
|
608
|
+
train_num + valid_num,
|
|
609
|
+
)
|
|
610
|
+
)
|
|
611
|
+
|
|
612
|
+
test_index = list(
|
|
613
|
+
range(
|
|
614
|
+
train_num + valid_num,
|
|
615
|
+
train_num + valid_num + test_num,
|
|
616
|
+
)
|
|
617
|
+
)
|
|
618
|
+
|
|
619
|
+
print(
|
|
620
|
+
f"train conformers={train_num}, "
|
|
621
|
+
f"valid={valid_num}, "
|
|
622
|
+
f"test={test_num}"
|
|
623
|
+
)
|
|
624
|
+
|
|
625
|
+
torch.save(
|
|
626
|
+
self.collate(all_data),
|
|
627
|
+
self.processed_paths[0],
|
|
628
|
+
)
|
|
629
|
+
|
|
630
|
+
with open(
|
|
631
|
+
self.processed_paths[1],
|
|
632
|
+
"wb",
|
|
633
|
+
) as f:
|
|
634
|
+
pickle.dump(
|
|
635
|
+
[
|
|
636
|
+
train_index,
|
|
637
|
+
valid_index,
|
|
638
|
+
test_index,
|
|
639
|
+
],
|
|
640
|
+
f,
|
|
641
|
+
)
|
|
642
|
+
|
|
643
|
+
def __repr__(self):
|
|
644
|
+
return f"{self.name}({len(self)})"
|