跳转至

Synthemol

Note

  1. 开始训练、评估前,请先下载实验所用数据集 Data.zip ,并对应修改 yaml 配置文件中的 data_dir 为解压后的数据集路径。例如:"./data/Data/...";下载 resources.zip, 将其解压至examples/synthemol/synthemol/下。
  2. 如果需要使用预训练模型进行评估,请先下载预训练模型pretrained.zip并解压,例如./pretrained/pretrained_chemprop.pdparams路径,并在yaml配置文件的PRE_COMPUTE.model_path指明路径。
  3. 开始训练、生成前,请安装 rdkit 等,相关依赖请执行pip install requirements.txt安装。
# 使用antibiotics等数据训练模型chemprop模型,实现Property Predict
# 配置可在conf/synthemol.yaml进行修改
python main.py
# 下载预训练模型(可选,或配置文件指定自己训练的模型)
mkdir -p ./pretrained && wget -O ./pretrained/pretrained_chemprop.pdparams https://paddle-org.bj.bcebos.com/paddlescience/models/synthemol/pretrained_chemprop.pdparams
# 使用antibiotics等数据评估模型chemprop模型,实现Property Predict
# 配置可在conf/synthemol.yaml进行修改
python main.py mode=eval
# 下载预训练模型(可选,或配置文件指定自己训练的模型)
mkdir -p ./pretrained && wget -O ./pretrained/pretrained_chemprop.pdparams https://paddle-org.bj.bcebos.com/paddlescience/models/synthemol/pretrained_chemprop.pdparams
# 使用训练好的模型进行building blocks的分数与计算,以加速下一个生成阶段
# 配置可在conf/synthemol.yaml进行修改
python main.py mode=pre-compute
# 使用预计算的building blocks的分数指引,结合synthemol使用蒙特卡洛树搜索,进行分子生成
# 配置可在conf/synthemol.yaml进行修改
python main.py mode=generate

1. 背景简介

泛耐药菌的迅速出现,使得开发结构全新的抗生素变得刻不容缓。人工智能虽可发现新型抗生素,但现有方法仍有明显缺陷:性质预测模型只能逐一评估分子,面对庞大的化学空间时扩展性极差;而生成式模型虽能快速探索巨量化学空间,却常输出难以合成的分子。为此,作者提出了 SyntheMol,一种生成式模型,可从近 300 亿个分子的化学空间中设计出易于合成的新化合物。作者将 SyntheMol 用于抑制鲍曼不动杆菌(一种棘手的革兰阴性病原菌)的生长,共合成 58 个生成分子并进行实验验证,其中 6 个结构全新的分子对鲍曼不动杆菌及其他多种系统发育差异显著的细菌均表现出抗菌活性。该研究展示了生成式人工智能在庞大化学空间中设计结构新颖、可合成且有效的小分子抗生素候选物的潜力,并提供了实验验证。

2. Synthemol原理

本章节仅对 Synthemol 的模型原理进行简单地介绍,详细的理论推导请阅读 Generative AI for designing and validating easily synthesizable and structurally novel antibiotics

2.1 Property Predictor

Chemprop 是一种分子性质预测模型,它利用有向消息传递神经网络处理分子,并对其性质进行预测。Chemprop 首先从分子图中提取简单的原子与键特征(如原子类型和键类型),为每个原子和键构建特征向量。接着,模型执行三轮消息传递:在每一轮中,神经网络层将邻近原子和键的信息迭代融合。消息传递完成后,Chemprop 将所有融合后的特征向量求和,生成一个代表整个分子的单一特征向量。该向量再输入一个两层的前馈神经网络,以预测分子性质;在本研究中,即预测抑制鲍曼不动杆菌生长的概率。我们使用的版本为 Chemprop v1.5.2,迁移自 PyTorch v1.12.0.post2。另外两种predictor请参考原文。

2.2 Synthemol

SyntheMol 是一种生成式模型,它在组合化学空间中进行探索,该空间由分子砌块经化学反应所生成的分子构成,以寻找具有目标性质的分子。SyntheMol 采用与 AlphaGo 类似的蒙特卡洛树搜索(MCTS)算法,高效地在这一化学空间中搜寻理想分子。SyntheMol 不仅能迅速识别出有前景的分子,还能同时给出其合成路线(即通过一系列一步或多步化学反应,将分子砌块组合起来的完整步骤)。以下,我们给出描述 SyntheMol MCTS 算法所需的数学符号,并提供相应的伪代码。

SyntheMol MCTS Algorithm

Requires:

  • Synthesis tree T
  • Property prediction model M
  • Maximum number of rollouts n_rollout
  • Maximum number of reactions n_reaction

function MCTS():     for i = 1 to n_rollout do:         rollout(T.root)     end for     return all visited nodes in T with:         1 molecule and ≥ 1 reaction


function rollout(N):     if node N has undergone ≥ n_reaction reactions then         return property prediction score of M applied to molecules in N     end if     E ← expand_node(N)     S ← select child node in E with largest MCTS score     return rollout(S)


function expand_node(N):     E ← empty set of nodes     foreach reaction R do         if R is compatible with molecules in N then             Add new node to E with each product of R applied to molecules in N         end if     end for     foreach building block B do         if any reaction is compatible with B and molecules in N then             Add new node to E with B and molecules in N         end if     end for     return E

3. Synthemol模型实现

接下来开始讲解如何基于 PaddleScience 代码,实现 Synthemol 模型的训练、预计算分数与生成。关于该案例中的其余细节请参考 API文档

3.1 数据集介绍

数据集采用了作者仓库 Synthemol 的 Data.zip 数据集。

训练集由 3 个化合物库组成:

  • 库 1 共 2371 个分子,来自 Pharmakon-1760 库(含 1 360 种 FDA 批准药物和 400 种国际批准药物)以及 800 种从植物、动物和微生物来源分离的天然产物。
  • 库 2 为 Broad Drug Repurposing Hub,共 6 680 个分子,其中多数为 FDA 批准药物或临床候选化合物。
  • 库 3 为一个小分子合成筛选库,含 5 376 个分子,系从 Broad Institute 更大的化合物库中随机抽样获得。

所有 3 个库均以两次生物学重复的形式,对鲍曼不动杆菌 ATCC 17978 进行生长抑制活性筛选。实验流程如下:

  1. 将菌株于 37 °C 在 2 ml LB 培养基中过夜培养,随后以 1:10 000 稀释于新鲜 LB。
  2. 取 49.5 µl(384 孔板)或 99 µl(96 孔板)菌液,使用手工或 Agilent Bravo 移液系统加入 Corning 平底微孔板。
  3. 每孔加入待测化合物,终浓度 50 µM,终体积 50 µl(384 孔板)或 100 µl(96 孔板)。
  4. 37 °C 静置孵育 16 h。
  5. 使用 SpectraMax M3 酶标仪(Molecular Devices)于 600 nm 读取吸光度,数据按板内四分位均值归一化,随后进行汇总与阳性命中判定。

更多详细信息,包括每个模型的超参数调整空间等,请参考作者原始论文。本仓库使用的具体超参数已在yaml配置文件中预设,可根据情况自行调节。

3.2 Chemprop模型训练

3.2.1 约束构建

本案例基于数据驱动的方法求解问题,因此需要使用 PaddleScience 内置的 SupervisedConstraint 构建监督约束。在定义约束之前,需要首先指定监督约束中用于数据加载的各个参数。

数据加载的代码如下:

examples/synthemol/main.py
# set dataloader config
train_dataloader_cfg = {
    "dataset": {
        "name": cfg.DATA.dataset_name,  # "MoleculeDatasetIter",
        "input_keys": tuple(cfg.MODEL.input_keys),
        "args": args,
        "smiles": train_smiles,
        "fingerprints": train_fingerprints,
        "properties": train_properties,
        "label_keys": tuple(cfg.MODEL.label_keys),
    },
    "num_workers": cfg.TRAIN.num_workers,
}

其中,"dataset" 字段定义了使用的 Dataset 类名为 MoleculeDatasetIternum_works 为 1。

定义监督约束的代码如下:

examples/synthemol/main.py
# set constraint
sup_constraint = ppsci.constraint.SupervisedConstraint(
    train_dataloader_cfg,
    output_expr={"pred": lambda out: out["pred"]},
    loss=ppsci.loss.FunctionalLoss(get_train_loss_func(args)),
    name="Sup",
)

# wrap constraints together
constraint = {sup_constraint.name: sup_constraint}

SupervisedConstraint 的第一个参数是数据的加载方式,这里使用上文中定义的 train_dataloader_cfg

第二个参数是损失函数的定义,这里使用自定义的损失函数;作者通过 get_loss_func 函数通过传递参数控制损失函数选择: 文中Chemprop模型使用的为 CrossEntropyLoss

第三个参数是约束条件的名字,方便后续对其索引。此处命名为 Sup

3.2.2 模型构建

在该案例中,分子属性预测模型基于 Chemprop 网络模型实现,用 PaddleScience 代码表示如下:

examples/synthemol/main.py
# set model
model = ppsci.arch.chemprop_molecule.MoleculeModel(cfg=cfg)

网络模型的参数通过配置文件进行设置如下:

examples/synthemol/conf/synthemol.yaml
MODEL:
  input_keys: ["mol_batch", "features_batch", "atom_descriptors_batch",
                "atom_features_batch", "bond_features_batch"]
  output_keys: ["pred"]
  label_keys: ["targets", "data_weights", "mask", "target_weights"]

其中,input_keysoutput_keys 分别代表网络模型输入、输出变量的名称。

3.2.3 学习率与优化器构建

本案例中使用的学习率大小设置为 0.0001。优化器使用 Adam,并将参数进行分组,用 PaddleScience 代码表示如下:

examples/synthemol/main.py
# set optimizer
optimizer = ppsci.optimizer.Adam(
    learning_rate=cfg.TRAIN.learning_rate,
    weight_decay=None,  # 0.001
)(model)

3.2.4 模型训练

完成上述设置之后,只需要将上述实例化的对象按顺序传递给 ppsci.solver.Solver,然后启动训练。

examples/synthemol/main.py
# initialize solver
solver = ppsci.solver.Solver(
    model,
    constraint,
    cfg.output_dir,
    optimizer,
    None,
    cfg.TRAIN.epochs,
    cfg.TRAIN.iters_per_epoch,
    save_freq=cfg.TRAIN.save_freq,
    eval_during_train=cfg.TRAIN.eval_during_train,
    eval_freq=cfg.TRAIN.eval_freq,
    eval_with_no_grad=cfg.EVAL.eval_with_no_grad,
    checkpoint_path=cfg.TRAIN.checkpoint_path,
)

# train model
solver.train()

3.3 building blocks分数预计算

构建模型的代码为:

examples/synthemol/main.py
model = ppsci.arch.chemprop_molecule.MoleculeModel(cfg=cfg)

3.4 Synthemol生成分子

构建Generator的代码为:

examples/synthemol/main.py
print("Setting up generator...")
generator = Generator(
    building_block_smiles_to_id=building_block_smiles_to_id,
    max_reactions=max_reactions,
    scoring_fn=model_scoring_fn,
    explore_weight=explore_weight,
    num_expand_nodes=num_expand_nodes,
    optimization=optimization,
    reactions=reactions,
    rng_seed=rng_seed,
    no_building_block_diversity=no_building_block_diversity,
    store_nodes=store_nodes,
    verbose=verbose,
    replicate=replicate,
)

4. 完整代码

examples/synthemol/main.py
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
# Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

#     http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from datetime import datetime
from pathlib import Path
from random import Random

import hydra
import numpy as np
import paddle
import pandas as pd
from chemprop_models import chemprop_predict
from chemprop_models import my_chemprop_load
from evaluation import evaluate_auto
from loss_functions import get_loss_func
from omegaconf import DictConfig
from synthemol.generate.generator import Generator
from synthemol.generate.utils import create_model_scoring_fn
from synthemol.generate.utils import save_generated_molecules
from synthemol.reactions import REACTIONS
from synthemol.reactions import load_and_set_allowed_reaction_building_blocks
from synthemol.reactions import set_all_building_blocks
from tqdm import tqdm

import ppsci
import ppsci.arch.chemprop_molecule
from ppsci.arch.chemprop_molecule_utils import TrainArgs


def get_train_loss_func(args):  #:paddle.Tensor=None):
    def train_loss_func(output_dict, label_dict, weight_dict):
        preds = output_dict["pred"]

        targets = label_dict["targets"]
        target_weights = label_dict["target_weights"]
        data_weights = label_dict["data_weights"]
        mask = label_dict["mask"]

        loss_func = get_loss_func(args)
        if args.loss_function == "bounded_mse":
            # lt_target_batch = lt_target_batch
            # gt_target_batch = gt_target_batch
            pass
        if args.loss_function == "mcc" and args.dataset_type == "classification":
            loss = loss_func(
                preds, targets, data_weights, mask
            ) * target_weights.squeeze(axis=0)
        elif args.loss_function == "mcc":
            targets = targets.astype(dtype="int64")
            target_losses = []
            for target_index in range(preds.shape[1]):
                target_loss = loss_func(
                    preds[:, target_index, :],
                    targets[:, target_index],
                    data_weights,
                    mask[:, target_index],
                ).unsqueeze(axis=0)
                target_losses.append(target_loss)
            loss = paddle.concat(x=target_losses) * target_weights.squeeze(axis=0)
        elif args.dataset_type == "multiclass":
            targets = targets.astype(dtype="int64")
            if args.loss_function == "dirichlet":
                loss = (
                    loss_func(preds, targets, args.evidential_regularization)
                    * target_weights
                    * data_weights
                    * mask
                )
            else:
                target_losses = []
                for target_index in range(preds.shape[1]):
                    target_loss = loss_func(
                        preds[:, target_index, :], targets[:, target_index]
                    ).unsqueeze(axis=1)
                    target_losses.append(target_loss)
                loss = (
                    paddle.concat(x=target_losses, axis=1)
                    * target_weights
                    * data_weights
                    * mask
                )
        elif args.dataset_type == "spectra":
            loss = (
                loss_func(preds, targets, mask) * target_weights * data_weights * mask
            )
        elif args.loss_function == "bounded_mse":
            pass
            """
            loss = (
                loss_func(preds, targets, lt_target_batch, gt_target_batch)
                * target_weights
                * data_weights
                * mask
            )
            """
        elif args.loss_function == "evidential":
            loss = (
                loss_func(preds, targets, args.evidential_regularization)
                * target_weights
                * data_weights
                * mask
            )
        elif args.loss_function == "dirichlet":
            loss = (
                loss_func(preds, targets, args.evidential_regularization)
                * target_weights
                * data_weights
                * mask
            )
        else:
            loss = loss_func(preds, targets) * target_weights * data_weights * mask
        loss = loss.sum() / mask.sum()

        return {"pred": loss.astype("float32")}

    return train_loss_func


def make_args(
    dataset_type,
    epochs,
    use_gpu,
    fingerprint_type,
    property_name,
    train_smiles,
    train_fingerprints,
):
    # Create args
    arg_list = [
        "--data_path",
        "foo.csv",
        "--dataset_type",
        dataset_type,
        "--save_dir",
        "foo",
        "--epochs",
        str(epochs),
        "--quiet",
    ] + ([] if use_gpu else ["--no_cuda"])

    if fingerprint_type == "morgan":
        arg_list += ["--features_generator", "morgan"]
    elif fingerprint_type == "rdkit":
        arg_list += [
            "--features_generator",
            "rdkit_2d_normalized",
            "--no_features_scaling",
        ]
    elif fingerprint_type is None:
        pass
    else:
        raise ValueError(f'Fingerprint type "{fingerprint_type}" is not supported.')

    args = TrainArgs().parse_args(arg_list)
    args.task_names = [property_name]
    if train_smiles is not None:
        args.train_data_size = len(train_smiles)

    if fingerprint_type is not None:
        args.features_size = train_fingerprints.shape[1]
    return args


def load_raw_data(cfg):
    data_path = cfg.DATA.data_path
    data = pd.read_csv(data_path)
    print(f"Data size = {len(data):,}")
    num_models = cfg.DATA.num_models  # 10
    num_folds = cfg.DATA.num_folds  # 10
    indices = np.tile(np.arange(num_folds), 1 + len(data) // num_folds)[: len(data)]
    random = Random(0)
    random.shuffle(indices)
    assert 1 <= num_models <= num_folds
    smiles_column = cfg.DATA.smiles_column  #'smiles'
    property_column = cfg.DATA.property_column  #'antibiotic_activity'

    model_num = 1
    test_index = model_num
    val_index = (model_num + 1) % num_folds
    test_mask = indices == test_index
    val_mask = indices == val_index
    train_mask = ~(test_mask | val_mask)
    test_data = data[test_mask]
    val_data = data[val_mask]
    train_data = data[train_mask]
    print(
        "test_data:",
        len(test_data),
        "train_data:",
        len(train_data),
        "val_data:",
        len(val_data),
    )
    train_smiles = train_data[smiles_column]
    train_fingerprints = None
    train_properties = train_data[property_column]
    return train_smiles, train_fingerprints, train_properties


def train(cfg: DictConfig):
    train_smiles, train_fingerprints, train_properties = load_raw_data(cfg)

    args = make_args(
        dataset_type=cfg.DATA.dataset_type,  # "classification",
        epochs=cfg.TRAIN.epochs,  # 1,
        use_gpu=cfg.TRAIN.use_gpu,
        fingerprint_type=cfg.DATA.fingerprint_type,  # None,
        property_name=cfg.DATA.property_column,  # "antibiotic_activity"
        train_smiles=train_smiles,
        train_fingerprints=train_fingerprints,
    )

    # set dataloader config
    train_dataloader_cfg = {
        "dataset": {
            "name": cfg.DATA.dataset_name,  # "MoleculeDatasetIter",
            "input_keys": tuple(cfg.MODEL.input_keys),
            "args": args,
            "smiles": train_smiles,
            "fingerprints": train_fingerprints,
            "properties": train_properties,
            "label_keys": tuple(cfg.MODEL.label_keys),
        },
        "num_workers": cfg.TRAIN.num_workers,
    }

    # set constraint
    sup_constraint = ppsci.constraint.SupervisedConstraint(
        train_dataloader_cfg,
        output_expr={"pred": lambda out: out["pred"]},
        loss=ppsci.loss.FunctionalLoss(get_train_loss_func(args)),
        name="Sup",
    )

    # wrap constraints together
    constraint = {sup_constraint.name: sup_constraint}

    # set model
    model = ppsci.arch.chemprop_molecule.MoleculeModel(cfg=cfg)

    # set optimizer
    optimizer = ppsci.optimizer.Adam(
        learning_rate=cfg.TRAIN.learning_rate,
        weight_decay=None,  # 0.001
    )(model)

    # initialize solver
    solver = ppsci.solver.Solver(
        model,
        constraint,
        cfg.output_dir,
        optimizer,
        None,
        cfg.TRAIN.epochs,
        cfg.TRAIN.iters_per_epoch,
        save_freq=cfg.TRAIN.save_freq,
        eval_during_train=cfg.TRAIN.eval_during_train,
        eval_freq=cfg.TRAIN.eval_freq,
        eval_with_no_grad=cfg.EVAL.eval_with_no_grad,
        checkpoint_path=cfg.TRAIN.checkpoint_path,
    )

    # train model
    solver.train()


def evaluate(cfg):
    data_path = cfg.DATA.data_path
    data = pd.read_csv(data_path)
    print(f"Data size = {len(data):,}")
    num_models = cfg.DATA.num_models
    num_folds = cfg.DATA.num_folds
    indices = np.tile(np.arange(num_folds), 1 + len(data) // num_folds)[: len(data)]
    random = Random(0)
    random.shuffle(indices)
    assert 1 <= num_models <= num_folds
    smiles_column = cfg.DATA.smiles_column  #'smiles'
    property_column = cfg.DATA.property_column  #'antibiotic_activity'

    model_num = 1
    test_index = model_num
    val_index = (model_num + 1) % num_folds
    test_mask = indices == test_index
    val_mask = indices == val_index
    train_mask = ~(test_mask | val_mask)
    test_data = data[test_mask]
    val_data = data[val_mask]
    train_data = data[train_mask]
    print(
        "test_data:",
        len(test_data),
        "train_data:",
        len(train_data),
        "val_data:",
        len(val_data),
    )

    # load model
    model_path = Path(cfg.PRE_COMPUTE.model_path)
    use_gpu = cfg.PRE_COMPUTE.use_gpu
    model_type = cfg.PRE_COMPUTE.model_type  #'chemprop'

    model = ppsci.arch.chemprop_molecule.MoleculeModel(cfg=cfg)

    if model_type == "chemprop":
        if use_gpu:
            device = str("cuda").replace("cuda", "gpu")
        else:
            device = paddle.CPUPlace()
        paddle.seed(seed=0)
    m = my_chemprop_load(model, model_path=model_path, device=device)
    test_preds = chemprop_predict(
        model=m, smiles=test_data[smiles_column], fingerprints=None, num_workers=1
    )

    scores = evaluate_auto(
        true=test_data[property_column],
        preds=test_preds,
        dataset_type=cfg.DATA.dataset_type,
    )
    for score_name, score_value in scores.items():
        print(f"Test {score_name} = {score_value:.3f}")


def pre_compute(cfg):
    data_path = Path(cfg.PRE_COMPUTE.data_path)
    model_path = Path(cfg.PRE_COMPUTE.model_path)
    smiles_column = cfg.PRE_COMPUTE.smiles_column
    model_type = cfg.PRE_COMPUTE.model_type  #'chemprop'
    fingerprint_type = cfg.PRE_COMPUTE.fingerprint_type
    use_gpu = cfg.PRE_COMPUTE.use_gpu
    average_preds = cfg.PRE_COMPUTE.average_preds
    num_workers = cfg.PRE_COMPUTE.num_workers
    preds_column_prefix = cfg.PRE_COMPUTE.preds_column_prefix
    save_path = Path(cfg.PRE_COMPUTE.save_path)

    model = ppsci.arch.chemprop_molecule.MoleculeModel(cfg=cfg)

    data = pd.read_csv(data_path)
    smiles = list(data[smiles_column])
    if model_type != "chemprop" and fingerprint_type is None:
        raise ValueError("Must define fingerprint_type if using sklearn model.")
    if fingerprint_type is not None:
        # fingerprints = compute_fingerprints(smiles, fingerprint_type=
        #    fingerprint_type)
        pass
    else:
        fingerprints = None
    if model_path.is_dir():
        model_paths = list(
            model_path.glob("**/*.pt" if model_type == "chemprop" else "**/*.pkl")
        )
        if len(model_paths) == 0:
            raise ValueError(f"Could not find any models in directory {model_path}.")
    else:
        model_paths = [model_path]
    if model_type == "chemprop":
        if use_gpu:
            device = str("cuda").replace("cuda", "gpu")
        else:
            device = paddle.CPUPlace()
        paddle.seed(seed=0)

        models = [
            my_chemprop_load(model, model_path=model_path, device=device)
            for model_path in model_paths
        ]

    print(model_paths, models)

    if model_type == "chemprop":
        preds = np.array(
            [
                chemprop_predict(
                    model=m,
                    smiles=smiles,
                    fingerprints=fingerprints,
                    num_workers=num_workers,
                )
                for m in tqdm(models, desc="models")
            ]
        )

    if average_preds:
        preds = np.mean(preds, axis=0)
    model_string = (
        f"{model_type}{f'_{fingerprint_type}' if fingerprint_type is not None else ''}"
    )
    preds_string = f"{f'{preds_column_prefix}_' if preds_column_prefix is not None else ''}{model_string}"
    if average_preds:
        data[f"{preds_string}_ensemble_preds"] = preds
    else:
        for model_num, model_preds in enumerate(preds):
            data[f"{preds_string}_model_{model_num}_preds"] = model_preds
    if save_path is None:
        save_path = data_path
    save_path.parent.mkdir(parents=True, exist_ok=True)
    data.to_csv(save_path, index=False)


def generate(cfg):
    model_path = cfg.GENERATE.model_path
    model_type = cfg.GENERATE.model_type  #'chemprop'
    save_dir = Path(cfg.GENERATE.save_dir)

    building_blocks_path = cfg.GENERATE.building_blocks_path
    fingerprint_type = cfg.GENERATE.fingerprint_type
    reaction_to_building_blocks_path = cfg.GENERATE.reaction_to_building_blocks_path
    building_blocks_id_column = cfg.GENERATE.building_blocks_id_column
    building_blocks_score_column = cfg.GENERATE.building_blocks_score_column

    building_blocks_smiles_column = cfg.GENERATE.building_blocks_smiles_column
    reactions = REACTIONS
    max_reactions = cfg.GENERATE.max_reactions
    n_rollout = cfg.GENERATE.n_rollout

    explore_weight = cfg.GENERATE.explore_weight
    num_expand_nodes = cfg.GENERATE.num_expand_nodes

    optimization = cfg.GENERATE.optimization
    rng_seed = cfg.GENERATE.rng_seed

    no_building_block_diversity = cfg.GENERATE.no_building_block_diversity
    store_nodes = cfg.GENERATE.store_nodes

    verbose = cfg.GENERATE.verbose
    replicate = cfg.GENERATE.replicate

    save_dir.mkdir(parents=True, exist_ok=True)
    print("Loading building blocks...")
    if replicate:
        building_block_data = pd.read_csv(
            building_blocks_path, dtype={building_blocks_score_column: str}
        )
        building_block_data[building_blocks_score_column] = building_block_data[
            building_blocks_score_column
        ].astype(float)
        old_reactions_order = [
            275592,
            22,
            11,
            527,
            2430,
            2708,
            240690,
            2230,
            2718,
            40,
            1458,
            271948,
            27,
        ]
        reactions = tuple(
            sorted(
                reactions, key=lambda reaction: old_reactions_order.index(reaction.id)
            )
        )
        building_block_data.drop_duplicates(
            subset=building_blocks_smiles_column, inplace=True
        )
    else:
        building_block_data = pd.read_csv(building_blocks_path)
    print(f"Loaded {len(building_block_data):,} building blocks")
    if building_block_data[building_blocks_id_column].nunique() != len(
        building_block_data
    ):
        raise ValueError("Building block IDs are not unique.")
    building_block_smiles_to_id = dict(
        zip(
            building_block_data[building_blocks_smiles_column],
            building_block_data[building_blocks_id_column],
        )
    )
    building_block_id_to_smiles = dict(
        zip(
            building_block_data[building_blocks_id_column],
            building_block_data[building_blocks_smiles_column],
        )
    )
    building_block_smiles_to_score = dict(
        zip(
            building_block_data[building_blocks_smiles_column],
            building_block_data[building_blocks_score_column],
        )
    )
    print(f"Found {len(building_block_smiles_to_id):,} unique building blocks")
    set_all_building_blocks(
        reactions=reactions, building_blocks=set(building_block_smiles_to_id)
    )
    if reaction_to_building_blocks_path is not None:
        print("Loading and setting allowed building blocks for each reaction...")
        load_and_set_allowed_reaction_building_blocks(
            reactions=reactions,
            reaction_to_reactant_to_building_blocks_path=reaction_to_building_blocks_path,
        )
    print("Loading models and creating model scoring function...")
    model_scoring_fn = create_model_scoring_fn(
        model_path=model_path,
        model_type=model_type,
        fingerprint_type=fingerprint_type,
        smiles_to_score=building_block_smiles_to_score,
    )
    print("Setting up generator...")
    generator = Generator(
        building_block_smiles_to_id=building_block_smiles_to_id,
        max_reactions=max_reactions,
        scoring_fn=model_scoring_fn,
        explore_weight=explore_weight,
        num_expand_nodes=num_expand_nodes,
        optimization=optimization,
        reactions=reactions,
        rng_seed=rng_seed,
        no_building_block_diversity=no_building_block_diversity,
        store_nodes=store_nodes,
        verbose=verbose,
        replicate=replicate,
    )
    print("Generating molecules...")
    start_time = datetime.now()
    nodes = generator.generate(n_rollout=n_rollout)
    stats = {
        "mcts_time": datetime.now() - start_time,
        "num_nonzero_reaction_molecules": len(nodes),
        "approx_num_nodes_searched": generator.approx_num_nodes_searched,
    }
    print(f"MCTS time = {stats['mcts_time']}")
    print(
        f"Number of full molecule, nonzero reaction nodes = {stats['num_nonzero_reaction_molecules']:,}"
    )
    print(
        f"Approximate total number of nodes searched = {stats['approx_num_nodes_searched']:,}"
    )
    if store_nodes:
        stats["num_nodes_searched"] = generator.num_nodes_searched
        print(f"Total number of nodes searched = {stats['num_nodes_searched']:,}")
    pd.DataFrame(data=[stats]).to_csv(save_dir / "mcts_stats.csv", index=False)
    print("Saving molecules...")
    save_generated_molecules(
        nodes=nodes,
        building_block_id_to_smiles=building_block_id_to_smiles,
        save_path=save_dir / "molecules.csv",
    )


@hydra.main(version_base=None, config_path="./conf", config_name="synthemol.yaml")
def main(cfg: DictConfig):
    if cfg.mode == "train":
        train(cfg)
    elif cfg.mode == "eval":
        evaluate(cfg)
    elif cfg.mode == "pre-compute":
        pre_compute(cfg)
    elif cfg.mode == "generate":
        generate(cfg)
    else:
        raise ValueError(
            f"cfg.mode should in ['train', 'eval', 'pre-compute', 'generate'], but got '{cfg.mode}'"
        )


if __name__ == "__main__":
    main()

5. 结果展示

评估第一步Chemprop模型的训练效果,通过加载预训练模型并执行评估命令,可以得到结果:

roc_auc prc_auc
chemprop 0.797 0.332

查看生成的molecules.csv,可以看到类似于下表的生成的分子信息:

smiles node_id num_expansions rollout_num score Q_value num_reactions reaction_1_id building_block_1_1_id building_block_1_1_smiles building_block_1_2_id building_block_1_2_smiles
C#CCN(C(=O)C(C)(C)C#C)C1CCN(C(=O)OC(C)(C)C)CC1 91431 20 1 1 22 4349560 C#CCNC1CCN(C(=O)OC(C)(C)C)CC1 2998277 C#CC(C)(C)C(=O)O

可以看到生成了符合要求的分子信息,符合作者的设计目的。

6. 参考文献