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
models/gnnconv.py
ADDED
|
@@ -0,0 +1,512 @@
|
|
|
1
|
+
# models/gnnconv.py
|
|
2
|
+
|
|
3
|
+
import torch
|
|
4
|
+
from torch import nn
|
|
5
|
+
import torch.nn.functional as F
|
|
6
|
+
from torch_geometric.nn import MessagePassing, global_add_pool
|
|
7
|
+
from torch_geometric.utils import degree
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
# ============================================================
|
|
11
|
+
# basic mlp
|
|
12
|
+
# ============================================================
|
|
13
|
+
|
|
14
|
+
class MLP(nn.Module):
|
|
15
|
+
def __init__(
|
|
16
|
+
self,
|
|
17
|
+
num_layers,
|
|
18
|
+
input_dim,
|
|
19
|
+
hidden_dim,
|
|
20
|
+
output_dim,
|
|
21
|
+
):
|
|
22
|
+
super().__init__()
|
|
23
|
+
|
|
24
|
+
self.num_layers = num_layers
|
|
25
|
+
self.linear_or_not = (num_layers == 1)
|
|
26
|
+
|
|
27
|
+
if num_layers < 1:
|
|
28
|
+
raise ValueError("num_layers must be >= 1")
|
|
29
|
+
|
|
30
|
+
if self.linear_or_not:
|
|
31
|
+
self.linear = nn.Linear(
|
|
32
|
+
input_dim,
|
|
33
|
+
output_dim,
|
|
34
|
+
)
|
|
35
|
+
else:
|
|
36
|
+
self.linears = nn.ModuleList()
|
|
37
|
+
self.batch_norms = nn.ModuleList()
|
|
38
|
+
|
|
39
|
+
self.linears.append(
|
|
40
|
+
nn.Linear(input_dim, hidden_dim)
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
for _ in range(num_layers - 2):
|
|
44
|
+
self.linears.append(
|
|
45
|
+
nn.Linear(hidden_dim, hidden_dim)
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
self.linears.append(
|
|
49
|
+
nn.Linear(hidden_dim, output_dim)
|
|
50
|
+
)
|
|
51
|
+
|
|
52
|
+
for _ in range(num_layers - 1):
|
|
53
|
+
self.batch_norms.append(
|
|
54
|
+
nn.BatchNorm1d(hidden_dim)
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
def forward(self, x):
|
|
58
|
+
if self.linear_or_not:
|
|
59
|
+
return self.linear(x)
|
|
60
|
+
|
|
61
|
+
h = x
|
|
62
|
+
|
|
63
|
+
for i in range(self.num_layers - 1):
|
|
64
|
+
h = self.linears[i](h)
|
|
65
|
+
h = self.batch_norms[i](h)
|
|
66
|
+
h = F.relu(h)
|
|
67
|
+
|
|
68
|
+
return self.linears[-1](h)
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
# ============================================================
|
|
72
|
+
# GIN conv
|
|
73
|
+
# ============================================================
|
|
74
|
+
|
|
75
|
+
class GINConv(MessagePassing):
|
|
76
|
+
def __init__(
|
|
77
|
+
self,
|
|
78
|
+
emb_dim,
|
|
79
|
+
edge_dim,
|
|
80
|
+
):
|
|
81
|
+
super().__init__(aggr="add")
|
|
82
|
+
|
|
83
|
+
self.mlp = nn.Sequential(
|
|
84
|
+
nn.Linear(emb_dim, 2 * emb_dim),
|
|
85
|
+
nn.BatchNorm1d(2 * emb_dim),
|
|
86
|
+
nn.ReLU(),
|
|
87
|
+
nn.Linear(2 * emb_dim, emb_dim),
|
|
88
|
+
)
|
|
89
|
+
|
|
90
|
+
self.eps = nn.Parameter(
|
|
91
|
+
torch.zeros(1)
|
|
92
|
+
)
|
|
93
|
+
|
|
94
|
+
self.bond_encoder = MLP(
|
|
95
|
+
num_layers=1,
|
|
96
|
+
input_dim=edge_dim,
|
|
97
|
+
hidden_dim=emb_dim,
|
|
98
|
+
output_dim=emb_dim,
|
|
99
|
+
)
|
|
100
|
+
|
|
101
|
+
def forward(
|
|
102
|
+
self,
|
|
103
|
+
x,
|
|
104
|
+
edge_index,
|
|
105
|
+
edge_attr,
|
|
106
|
+
):
|
|
107
|
+
edge_embedding = self.bond_encoder(
|
|
108
|
+
edge_attr.float()
|
|
109
|
+
)
|
|
110
|
+
|
|
111
|
+
out = self.propagate(
|
|
112
|
+
edge_index=edge_index,
|
|
113
|
+
x=x,
|
|
114
|
+
edge_attr=edge_embedding,
|
|
115
|
+
)
|
|
116
|
+
|
|
117
|
+
out = (1 + self.eps) * x + out
|
|
118
|
+
out = self.mlp(out)
|
|
119
|
+
|
|
120
|
+
return out
|
|
121
|
+
|
|
122
|
+
def message(
|
|
123
|
+
self,
|
|
124
|
+
x_j,
|
|
125
|
+
edge_attr,
|
|
126
|
+
):
|
|
127
|
+
return F.relu(x_j + edge_attr)
|
|
128
|
+
|
|
129
|
+
def update(
|
|
130
|
+
self,
|
|
131
|
+
aggr_out,
|
|
132
|
+
):
|
|
133
|
+
return aggr_out
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
# ============================================================
|
|
137
|
+
# GCN conv
|
|
138
|
+
# ============================================================
|
|
139
|
+
|
|
140
|
+
class GCNConv(MessagePassing):
|
|
141
|
+
def __init__(
|
|
142
|
+
self,
|
|
143
|
+
emb_dim,
|
|
144
|
+
edge_dim,
|
|
145
|
+
):
|
|
146
|
+
super().__init__(aggr="add")
|
|
147
|
+
|
|
148
|
+
self.linear = nn.Linear(
|
|
149
|
+
emb_dim,
|
|
150
|
+
emb_dim,
|
|
151
|
+
)
|
|
152
|
+
|
|
153
|
+
self.root_emb = nn.Embedding(
|
|
154
|
+
1,
|
|
155
|
+
emb_dim,
|
|
156
|
+
)
|
|
157
|
+
|
|
158
|
+
self.bond_encoder = MLP(
|
|
159
|
+
num_layers=1,
|
|
160
|
+
input_dim=edge_dim,
|
|
161
|
+
hidden_dim=emb_dim,
|
|
162
|
+
output_dim=emb_dim,
|
|
163
|
+
)
|
|
164
|
+
|
|
165
|
+
def forward(
|
|
166
|
+
self,
|
|
167
|
+
x,
|
|
168
|
+
edge_index,
|
|
169
|
+
edge_attr,
|
|
170
|
+
):
|
|
171
|
+
x = self.linear(x)
|
|
172
|
+
|
|
173
|
+
edge_embedding = self.bond_encoder(
|
|
174
|
+
edge_attr.float()
|
|
175
|
+
)
|
|
176
|
+
|
|
177
|
+
row, col = edge_index
|
|
178
|
+
|
|
179
|
+
deg = degree(
|
|
180
|
+
row,
|
|
181
|
+
x.size(0),
|
|
182
|
+
dtype=x.dtype,
|
|
183
|
+
) + 1.0
|
|
184
|
+
|
|
185
|
+
deg_inv_sqrt = deg.pow(-0.5)
|
|
186
|
+
deg_inv_sqrt[
|
|
187
|
+
deg_inv_sqrt == float("inf")
|
|
188
|
+
] = 0
|
|
189
|
+
|
|
190
|
+
norm = deg_inv_sqrt[row] * deg_inv_sqrt[col]
|
|
191
|
+
|
|
192
|
+
out = self.propagate(
|
|
193
|
+
edge_index=edge_index,
|
|
194
|
+
x=x,
|
|
195
|
+
edge_attr=edge_embedding,
|
|
196
|
+
norm=norm,
|
|
197
|
+
)
|
|
198
|
+
|
|
199
|
+
root = self.root_emb.weight[0]
|
|
200
|
+
|
|
201
|
+
return out + F.relu(
|
|
202
|
+
x + root
|
|
203
|
+
) / deg.view(-1, 1)
|
|
204
|
+
|
|
205
|
+
def message(
|
|
206
|
+
self,
|
|
207
|
+
x_j,
|
|
208
|
+
edge_attr,
|
|
209
|
+
norm,
|
|
210
|
+
):
|
|
211
|
+
return norm.view(-1, 1) * F.relu(
|
|
212
|
+
x_j + edge_attr
|
|
213
|
+
)
|
|
214
|
+
|
|
215
|
+
def update(
|
|
216
|
+
self,
|
|
217
|
+
aggr_out,
|
|
218
|
+
):
|
|
219
|
+
return aggr_out
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
# ============================================================
|
|
223
|
+
# node gnn
|
|
224
|
+
# ============================================================
|
|
225
|
+
|
|
226
|
+
class GNN_node(nn.Module):
|
|
227
|
+
def __init__(
|
|
228
|
+
self,
|
|
229
|
+
num_layer,
|
|
230
|
+
emb_dim,
|
|
231
|
+
edge_dim,
|
|
232
|
+
node_dim=39,
|
|
233
|
+
drop_ratio=0.5,
|
|
234
|
+
JK="last",
|
|
235
|
+
residual=False,
|
|
236
|
+
gnn_type="gin",
|
|
237
|
+
):
|
|
238
|
+
super().__init__()
|
|
239
|
+
|
|
240
|
+
self.num_layer = num_layer
|
|
241
|
+
self.drop_ratio = drop_ratio
|
|
242
|
+
self.JK = JK
|
|
243
|
+
self.residual = residual
|
|
244
|
+
|
|
245
|
+
self.atom_encoder = MLP(
|
|
246
|
+
num_layers=2,
|
|
247
|
+
input_dim=node_dim,
|
|
248
|
+
hidden_dim=emb_dim,
|
|
249
|
+
output_dim=emb_dim,
|
|
250
|
+
)
|
|
251
|
+
|
|
252
|
+
self.convs = nn.ModuleList()
|
|
253
|
+
self.batch_norms = nn.ModuleList()
|
|
254
|
+
|
|
255
|
+
for _ in range(num_layer):
|
|
256
|
+
if gnn_type == "gin":
|
|
257
|
+
conv = GINConv(
|
|
258
|
+
emb_dim=emb_dim,
|
|
259
|
+
edge_dim=edge_dim,
|
|
260
|
+
)
|
|
261
|
+
elif gnn_type == "gcn":
|
|
262
|
+
conv = GCNConv(
|
|
263
|
+
emb_dim=emb_dim,
|
|
264
|
+
edge_dim=edge_dim,
|
|
265
|
+
)
|
|
266
|
+
else:
|
|
267
|
+
raise ValueError(
|
|
268
|
+
f"Unknown gnn_type={gnn_type}"
|
|
269
|
+
)
|
|
270
|
+
|
|
271
|
+
self.convs.append(conv)
|
|
272
|
+
|
|
273
|
+
self.batch_norms.append(
|
|
274
|
+
nn.BatchNorm1d(emb_dim)
|
|
275
|
+
)
|
|
276
|
+
|
|
277
|
+
def forward(self, *argv):
|
|
278
|
+
if len(argv) == 1:
|
|
279
|
+
batch = argv[0]
|
|
280
|
+
|
|
281
|
+
x = batch.x
|
|
282
|
+
edge_index = batch.edge_index
|
|
283
|
+
edge_attr = batch.edge_attr
|
|
284
|
+
|
|
285
|
+
elif len(argv) == 3:
|
|
286
|
+
x, edge_index, edge_attr = argv
|
|
287
|
+
|
|
288
|
+
else:
|
|
289
|
+
raise ValueError(
|
|
290
|
+
"GNN_node forward args mismatch"
|
|
291
|
+
)
|
|
292
|
+
|
|
293
|
+
h = self.atom_encoder(
|
|
294
|
+
x.float()
|
|
295
|
+
)
|
|
296
|
+
|
|
297
|
+
h_list = [h]
|
|
298
|
+
|
|
299
|
+
for layer in range(self.num_layer):
|
|
300
|
+
h_new = self.convs[layer](
|
|
301
|
+
h_list[layer],
|
|
302
|
+
edge_index,
|
|
303
|
+
edge_attr,
|
|
304
|
+
)
|
|
305
|
+
|
|
306
|
+
h_new = self.batch_norms[layer](
|
|
307
|
+
h_new
|
|
308
|
+
)
|
|
309
|
+
|
|
310
|
+
if layer == self.num_layer - 1:
|
|
311
|
+
h_new = F.dropout(
|
|
312
|
+
h_new,
|
|
313
|
+
p=self.drop_ratio,
|
|
314
|
+
training=self.training,
|
|
315
|
+
)
|
|
316
|
+
else:
|
|
317
|
+
h_new = F.dropout(
|
|
318
|
+
F.relu(h_new),
|
|
319
|
+
p=self.drop_ratio,
|
|
320
|
+
training=self.training,
|
|
321
|
+
)
|
|
322
|
+
|
|
323
|
+
if self.residual:
|
|
324
|
+
h_new = h_new + h_list[layer]
|
|
325
|
+
|
|
326
|
+
h_list.append(h_new)
|
|
327
|
+
|
|
328
|
+
if self.JK == "last":
|
|
329
|
+
return h_list[-1]
|
|
330
|
+
|
|
331
|
+
elif self.JK == "sum":
|
|
332
|
+
out = 0
|
|
333
|
+
for h in h_list:
|
|
334
|
+
out = out + h
|
|
335
|
+
return out
|
|
336
|
+
|
|
337
|
+
else:
|
|
338
|
+
raise ValueError(
|
|
339
|
+
f"Unknown JK={self.JK}"
|
|
340
|
+
)
|
|
341
|
+
|
|
342
|
+
|
|
343
|
+
# ============================================================
|
|
344
|
+
# virtual node gnn
|
|
345
|
+
# ============================================================
|
|
346
|
+
|
|
347
|
+
class GNN_node_Virtualnode(nn.Module):
|
|
348
|
+
def __init__(
|
|
349
|
+
self,
|
|
350
|
+
num_layer,
|
|
351
|
+
emb_dim,
|
|
352
|
+
edge_dim,
|
|
353
|
+
node_dim=39,
|
|
354
|
+
drop_ratio=0.5,
|
|
355
|
+
JK="last",
|
|
356
|
+
residual=False,
|
|
357
|
+
gnn_type="gin",
|
|
358
|
+
):
|
|
359
|
+
super().__init__()
|
|
360
|
+
|
|
361
|
+
if num_layer < 2:
|
|
362
|
+
raise ValueError(
|
|
363
|
+
"num_layer must be >= 2"
|
|
364
|
+
)
|
|
365
|
+
|
|
366
|
+
self.num_layer = num_layer
|
|
367
|
+
self.drop_ratio = drop_ratio
|
|
368
|
+
self.JK = JK
|
|
369
|
+
self.residual = residual
|
|
370
|
+
|
|
371
|
+
self.atom_encoder = MLP(
|
|
372
|
+
num_layers=2,
|
|
373
|
+
input_dim=node_dim,
|
|
374
|
+
hidden_dim=emb_dim,
|
|
375
|
+
output_dim=emb_dim,
|
|
376
|
+
)
|
|
377
|
+
|
|
378
|
+
self.virtualnode_embedding = nn.Embedding(
|
|
379
|
+
1,
|
|
380
|
+
emb_dim,
|
|
381
|
+
)
|
|
382
|
+
|
|
383
|
+
nn.init.constant_(
|
|
384
|
+
self.virtualnode_embedding.weight,
|
|
385
|
+
0,
|
|
386
|
+
)
|
|
387
|
+
|
|
388
|
+
self.convs = nn.ModuleList()
|
|
389
|
+
self.batch_norms = nn.ModuleList()
|
|
390
|
+
self.virtual_mlps = nn.ModuleList()
|
|
391
|
+
|
|
392
|
+
for _ in range(num_layer):
|
|
393
|
+
if gnn_type == "gin":
|
|
394
|
+
conv = GINConv(
|
|
395
|
+
emb_dim=emb_dim,
|
|
396
|
+
edge_dim=edge_dim,
|
|
397
|
+
)
|
|
398
|
+
else:
|
|
399
|
+
conv = GCNConv(
|
|
400
|
+
emb_dim=emb_dim,
|
|
401
|
+
edge_dim=edge_dim,
|
|
402
|
+
)
|
|
403
|
+
|
|
404
|
+
self.convs.append(conv)
|
|
405
|
+
|
|
406
|
+
self.batch_norms.append(
|
|
407
|
+
nn.BatchNorm1d(emb_dim)
|
|
408
|
+
)
|
|
409
|
+
|
|
410
|
+
for _ in range(num_layer - 1):
|
|
411
|
+
self.virtual_mlps.append(
|
|
412
|
+
nn.Sequential(
|
|
413
|
+
nn.Linear(
|
|
414
|
+
emb_dim,
|
|
415
|
+
2 * emb_dim,
|
|
416
|
+
),
|
|
417
|
+
nn.BatchNorm1d(
|
|
418
|
+
2 * emb_dim
|
|
419
|
+
),
|
|
420
|
+
nn.ReLU(),
|
|
421
|
+
nn.Linear(
|
|
422
|
+
2 * emb_dim,
|
|
423
|
+
emb_dim,
|
|
424
|
+
),
|
|
425
|
+
)
|
|
426
|
+
)
|
|
427
|
+
|
|
428
|
+
def forward(self, batch):
|
|
429
|
+
x = batch.x
|
|
430
|
+
edge_index = batch.edge_index
|
|
431
|
+
edge_attr = batch.edge_attr
|
|
432
|
+
batch_idx = batch.batch
|
|
433
|
+
|
|
434
|
+
h = self.atom_encoder(
|
|
435
|
+
x.float()
|
|
436
|
+
)
|
|
437
|
+
|
|
438
|
+
h_list = [h]
|
|
439
|
+
|
|
440
|
+
virtualnode_embedding = self.virtualnode_embedding(
|
|
441
|
+
torch.zeros(
|
|
442
|
+
batch_idx[-1].item() + 1,
|
|
443
|
+
dtype=torch.long,
|
|
444
|
+
device=batch_idx.device,
|
|
445
|
+
)
|
|
446
|
+
)
|
|
447
|
+
|
|
448
|
+
for layer in range(self.num_layer):
|
|
449
|
+
h_list[layer] = (
|
|
450
|
+
h_list[layer]
|
|
451
|
+
+ virtualnode_embedding[batch_idx]
|
|
452
|
+
)
|
|
453
|
+
|
|
454
|
+
h_new = self.convs[layer](
|
|
455
|
+
h_list[layer],
|
|
456
|
+
edge_index,
|
|
457
|
+
edge_attr,
|
|
458
|
+
)
|
|
459
|
+
|
|
460
|
+
h_new = self.batch_norms[layer](
|
|
461
|
+
h_new
|
|
462
|
+
)
|
|
463
|
+
|
|
464
|
+
if layer == self.num_layer - 1:
|
|
465
|
+
h_new = F.dropout(
|
|
466
|
+
h_new,
|
|
467
|
+
p=self.drop_ratio,
|
|
468
|
+
training=self.training,
|
|
469
|
+
)
|
|
470
|
+
else:
|
|
471
|
+
h_new = F.dropout(
|
|
472
|
+
F.relu(h_new),
|
|
473
|
+
p=self.drop_ratio,
|
|
474
|
+
training=self.training,
|
|
475
|
+
)
|
|
476
|
+
|
|
477
|
+
if self.residual:
|
|
478
|
+
h_new = h_new + h_list[layer]
|
|
479
|
+
|
|
480
|
+
h_list.append(h_new)
|
|
481
|
+
|
|
482
|
+
if layer < self.num_layer - 1:
|
|
483
|
+
pooled = global_add_pool(
|
|
484
|
+
h_new,
|
|
485
|
+
batch_idx,
|
|
486
|
+
)
|
|
487
|
+
|
|
488
|
+
update = self.virtual_mlps[layer](
|
|
489
|
+
pooled
|
|
490
|
+
)
|
|
491
|
+
|
|
492
|
+
if self.residual:
|
|
493
|
+
virtualnode_embedding = (
|
|
494
|
+
virtualnode_embedding
|
|
495
|
+
+ update
|
|
496
|
+
)
|
|
497
|
+
else:
|
|
498
|
+
virtualnode_embedding = update
|
|
499
|
+
|
|
500
|
+
if self.JK == "last":
|
|
501
|
+
return h_list[-1]
|
|
502
|
+
|
|
503
|
+
elif self.JK == "sum":
|
|
504
|
+
out = 0
|
|
505
|
+
for h in h_list:
|
|
506
|
+
out = out + h
|
|
507
|
+
return out
|
|
508
|
+
|
|
509
|
+
else:
|
|
510
|
+
raise ValueError(
|
|
511
|
+
f"Unknown JK={self.JK}"
|
|
512
|
+
)
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import torch
|
|
2
|
+
import torch.nn.functional as F
|
|
3
|
+
from typing import List, Optional, Union
|
|
4
|
+
|
|
5
|
+
def prob_low_dim(Y, a, b, eps=1e-12):
|
|
6
|
+
"""
|
|
7
|
+
可微的低维概率矩阵计算。
|
|
8
|
+
Y: torch.Tensor (N, 3)
|
|
9
|
+
a, b: float or torch.Tensor (标量)
|
|
10
|
+
返回 Q: (N, N)
|
|
11
|
+
"""
|
|
12
|
+
a = torch.as_tensor(a, dtype=Y.dtype, device=Y.device)
|
|
13
|
+
b = torch.as_tensor(b, dtype=Y.dtype, device=Y.device)
|
|
14
|
+
D = torch.cdist(Y, Y, p=2) # 欧氏距离
|
|
15
|
+
s = D.pow(2) + eps # d_ij^2
|
|
16
|
+
Q = 1.0 / (1.0 + a * torch.pow(s, b))
|
|
17
|
+
Q.fill_diagonal_(0.0) # 对角线置0
|
|
18
|
+
return Q
|
|
19
|
+
|
|
20
|
+
def cross_entropy_loss(P, Q, eps=1e-12):
|
|
21
|
+
"""
|
|
22
|
+
交叉熵损失,P, Q 均为 (N,N) 张量。
|
|
23
|
+
返回标量损失,除以非对角线元素数量,使损失与原子数无关。
|
|
24
|
+
"""
|
|
25
|
+
Q = torch.clamp(Q, min=eps, max=1-eps)
|
|
26
|
+
P = torch.clamp(P, min=eps, max=1-eps)
|
|
27
|
+
loss = - (P * torch.log(Q) + (1 - P) * torch.log(1 - Q))
|
|
28
|
+
n = P.shape[0]
|
|
29
|
+
loss = loss.sum() / (n * (n-1))
|
|
30
|
+
return loss
|
|
31
|
+
|
|
32
|
+
def kernel_inversion(P_target: Union[torch.Tensor, List[torch.Tensor]],
|
|
33
|
+
Y_init: torch.Tensor,
|
|
34
|
+
a: float,
|
|
35
|
+
b: float,
|
|
36
|
+
steps: int = 200,
|
|
37
|
+
lr: float = 0.1,
|
|
38
|
+
verbose: bool = False,
|
|
39
|
+
device: Optional[torch.device] = None) -> torch.Tensor:
|
|
40
|
+
"""
|
|
41
|
+
核反转函数:给定目标概率图 P_target,从初始坐标 Y_init 开始优化,
|
|
42
|
+
最小化交叉熵损失 CE(P_target, Q(Y)),得到最终坐标 Y_opt。
|
|
43
|
+
|
|
44
|
+
Args:
|
|
45
|
+
P_target: 目标概率图,形状 (N, N),或批次列表长度 B,每个形状 (N_i, N_i)
|
|
46
|
+
Y_init: 初始坐标,形状 (N, 3) 或 (B, N_i, 3) 与 P_target 对应
|
|
47
|
+
a, b: 低维核参数(标量)
|
|
48
|
+
steps: 优化步数
|
|
49
|
+
lr: 学习率
|
|
50
|
+
verbose: 是否打印损失变化
|
|
51
|
+
device: 计算设备
|
|
52
|
+
|
|
53
|
+
Returns:
|
|
54
|
+
优化后的坐标,形状与 Y_init 相同
|
|
55
|
+
"""
|
|
56
|
+
# 统一处理批量输入
|
|
57
|
+
if isinstance(P_target, list) and len(P_target) > 1:
|
|
58
|
+
if not isinstance(Y_init, list):
|
|
59
|
+
raise ValueError("当 P_target 为列表时,Y_init 也应为列表,每个元素对应一个分子")
|
|
60
|
+
results = []
|
|
61
|
+
for p, y in zip(P_target, Y_init):
|
|
62
|
+
res = kernel_inversion(p, y, a, b, steps, lr, verbose, device)
|
|
63
|
+
results.append(res)
|
|
64
|
+
return results
|
|
65
|
+
|
|
66
|
+
# 单分子情况:若 P_target 是单元素列表,取出第一个元素
|
|
67
|
+
if isinstance(P_target, list) and len(P_target) == 1:
|
|
68
|
+
P_target = P_target[0]
|
|
69
|
+
|
|
70
|
+
if device is not None:
|
|
71
|
+
P_target = P_target.to(device)
|
|
72
|
+
Y_init = Y_init.to(device)
|
|
73
|
+
else:
|
|
74
|
+
P_target = P_target.cpu()
|
|
75
|
+
Y_init = Y_init.cpu()
|
|
76
|
+
|
|
77
|
+
Y = Y_init.clone().detach().requires_grad_(True)
|
|
78
|
+
optimizer = torch.optim.Adam([Y], lr=lr)
|
|
79
|
+
|
|
80
|
+
for step in range(steps):
|
|
81
|
+
optimizer.zero_grad()
|
|
82
|
+
Q = prob_low_dim(Y, a, b)
|
|
83
|
+
loss = cross_entropy_loss(P_target, Q)
|
|
84
|
+
loss.backward()
|
|
85
|
+
optimizer.step()
|
|
86
|
+
# 保持坐标中心为原点(刚性平移不变)
|
|
87
|
+
with torch.no_grad():
|
|
88
|
+
Y -= Y.mean(dim=0, keepdim=True)
|
|
89
|
+
if verbose and (step % 50 == 0 or step == steps-1):
|
|
90
|
+
print(f"Step {step:4d}, loss = {loss.item():.6f}")
|
|
91
|
+
|
|
92
|
+
return Y.detach()
|
|
93
|
+
|
|
94
|
+
def generate_conformer_ensemble(P_target: Union[torch.Tensor, List[torch.Tensor]],
|
|
95
|
+
a: float,
|
|
96
|
+
b: float,
|
|
97
|
+
n_confs: int = 10,
|
|
98
|
+
steps: int = 200,
|
|
99
|
+
lr: float = 0.1,
|
|
100
|
+
noise_scale: float = 0.1,
|
|
101
|
+
device: Optional[torch.device] = None) -> List[torch.Tensor]:
|
|
102
|
+
"""
|
|
103
|
+
从同一个概率图 P_target 生成多个构象(多初始化)。
|
|
104
|
+
|
|
105
|
+
Args:
|
|
106
|
+
P_target: 目标概率图,形状 (N, N)
|
|
107
|
+
a, b: 核参数
|
|
108
|
+
n_confs: 生成的构象数量
|
|
109
|
+
steps: 每个构象的优化步数
|
|
110
|
+
lr: 学习率
|
|
111
|
+
noise_scale: 初始坐标的随机噪声标准差(以平均键长 ~1.5 Å 为参考)
|
|
112
|
+
device: 计算设备
|
|
113
|
+
|
|
114
|
+
Returns:
|
|
115
|
+
列表,每个元素为优化后的坐标张量 (N, 3)
|
|
116
|
+
"""
|
|
117
|
+
if isinstance(P_target, list):
|
|
118
|
+
if len(P_target) == 0:
|
|
119
|
+
raise ValueError("P_target 列表为空")
|
|
120
|
+
# 若列表长度大于1,认为每个元素对应一个分子,递归调用本函数处理每个分子
|
|
121
|
+
if len(P_target) > 1:
|
|
122
|
+
results = []
|
|
123
|
+
for p in P_target:
|
|
124
|
+
res = generate_conformer_ensemble(p, a, b, n_confs, steps, lr, noise_scale, device)
|
|
125
|
+
results.extend(res) # 注意:展平
|
|
126
|
+
return results
|
|
127
|
+
# 长度为1,取出单独处理
|
|
128
|
+
P_target = P_target[0]
|
|
129
|
+
|
|
130
|
+
if device is not None:
|
|
131
|
+
P_target = P_target.to(device)
|
|
132
|
+
else:
|
|
133
|
+
P_target = P_target.cpu()
|
|
134
|
+
|
|
135
|
+
N = P_target.shape[0]
|
|
136
|
+
ensemble = []
|
|
137
|
+
for i in range(n_confs):
|
|
138
|
+
Y0 = torch.randn(N, 3, device=P_target.device) * noise_scale
|
|
139
|
+
Y_opt = kernel_inversion(P_target, Y0, a, b, steps, lr, verbose=False, device=device)
|
|
140
|
+
ensemble.append(Y_opt.cpu())
|
|
141
|
+
return ensemble
|