File size: 26,396 Bytes
3ac1d94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3f4bfb5
3ac1d94
 
 
 
 
 
36499be
3ac1d94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36499be
3ac1d94
 
 
 
 
 
 
 
 
 
1693cd0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
---
frameworks:
- ""
language:
- en
license: mit
tags:
- OneScience
- affinity prediction
- molecule generation
---
<p align="center">
  <strong>
    <span style="font-size: 30px;">TargetDiff</span>
  </strong>
</p>

# Model Overview

TargetDiff is a bioinformatics model for target-aware molecule generation and protein-ligand affinity prediction.

The original paper is _3D Equivariant Diffusion for Target-Aware Molecule Generation and Affinity Prediction_ (ICLR 2023).

# Model Description

TargetDiff is based on a 3D equivariant diffusion network. Given a protein binding pocket, it generates candidate small molecules and can use an EGNN property prediction branch to predict protein-ligand complex affinity.

The current package organizes TargetDiff example scripts, a snapshot of the model implementation, sample inputs, and pretrained weights in the same directory. Datasets will be uploaded later.

# Use Cases

| Use case | Description |
| :---: | :---: |
| Protein-ligand affinity prediction | Takes a protein PDB file and ligand SDF file as input, and outputs molar concentration predictions for `Ki`, `Kd`, or `IC50` |
| Target-aware small molecule generation | Takes a protein binding pocket PDB file as input, and outputs generated molecule `sample.pt` files and SDF files for molecules that can be reconstructed |
| Diffusion model training | Trains the TargetDiff molecule generation model using CrossDocked2020 pocket data |
| Property prediction training | Trains an EGNN affinity prediction model using PDBbind data |
| Generated result evaluation | Computes metrics such as stability, reconstruction success rate, QED, SA, and optional Vina docking metrics for sampling results |

# Usage

## 1. Using OneCode

You can try intelligent one-click AI4S programming through the OneCode online environment:

[Try intelligent one-click AI4S programming](https://web-2069360198568017922-iaaj.ksai.scnet.cn:58043/home)

## 2. Manual Installation and Usage

**Hardware Requirements**

- Running on a GPU or DCU is recommended.
- CPU can be used for connectivity checks, but it is relatively slow.
- DCU users need to install DTK in advance. DTK 25.04.2 or later is recommended, or the OneScience-recommended version that matches the current cluster.

**Software Requirements**

First enter an environment where the OneScience bioinformatics dependencies have been installed.

**Environment Checks**

- NVIDIA GPU:

```bash
nvidia-smi
```

- Hygon DCU:

```bash
hy-smi
```

## Quick Start

### 1. Install the Runtime Environment

```bash
conda create -n onescience311 python=3.11 -y
conda activate onescience311
pip install onescience[bio] -i http://mirrors.onescience.ai:3141/pypi/simple/  --trusted-host mirrors.onescience.ai
```

If the following code cannot find required libraries at runtime, activate CUDA as shown below.

```bash
source ${ROCM_PATH}/cuda/env.sh
export LD_LIBRARY_PATH="$CONDA_PREFIX/lib:$LD_LIBRARY_PATH"
export LD_LIBRARY_PATH="$CONDA_PREFIX/lib/python3.11/site-packages/fastpt/torch/lib:$LD_LIBRARY_PATH"
```

### 2. Download the Model

```bash
hf download OneScience-Group/targetdiff --local-dir ./targetdiff
cd targetdiff
```

### Training Weights and Datasets

Training weights are already included in the `weights` folder and can be used directly after downloading the model package.
Datasets will be uploaded to Hugging Face soon, and command-line downloads will be supported later.

### 3. Affinity Prediction

#### 3.1 Affinity Prediction Training

```bash
bash scripts/train_prop.sh
```

This script automatically performs the following steps:

1. Extract binding pockets from the PDBbind refined set.
2. Split the training, validation, and test sets according to the coreset.
3. Train an EGNN-based protein-ligand binding affinity prediction model.

#### 3.2 Affinity Prediction Evaluation

Use the trained affinity prediction model to evaluate on the test set. The expected official metrics on PDBBind v2016 are:

| RMSE | MAE | R² | Pearson | Spearman |
|------|-----|----|---------|----------|
| 1.316 | 1.031 | 0.633 | 0.797 | 0.782 |

Run:

```bash
export PYTHONPATH=../../../src:$PYTHONPATH
python scripts/property_prediction/eval_prop.py \
    --ckpt_path ${ONESCIENCE_MODELS_DIR}/targetdiff/pretrained_models/egnn_pdbbind_v2016.pt \
    --device cuda
```

#### 3.3 Affinity Prediction Inference

```bash
bash scripts/inference.sh
```

By default, this script performs affinity prediction on the example protein-ligand pair `3ug2`, using the default weights and example data:

- Model weights: `${ONESCIENCE_MODELS_DIR}/targetdiff/pretrained_models/egnn_pdbbind_v2016.pt`
- Protein: `${ONESCIENCE_DATASETS_DIR}/targetdiff/examples/3ug2_protein.pdb`
- Ligand: `${ONESCIENCE_DATASETS_DIR}/targetdiff/examples/3ug2_ligand.sdf`
- Affinity type: default `Kd`
- Compute device: default `cuda`

### 4. Molecular Sampling

#### 4.1 Single Test-Set Sample

```bash
export PYTHONPATH=../../../src:$PYTHONPATH
python -m scripts.sample_diffusion configs/sampling.yml -i 0 --batch_size 50 --result_path ./outputs
```

This script reads the diffusion model checkpoint from `configs/sampling.yml`, generates candidate ligand molecules for the `i`-th pocket in the test set, and saves the result as `result_i.pt`.

#### 4.2 Multi-GPU Batch Sampling

```bash
bash scripts/batch_sample_diffusion.sh configs/sampling.yml outputs 4 0 0
```

Parameter descriptions:

| Parameter | Description |
|------|------|
| `$1` | Sampling configuration file path |
| `$2` | Result output directory |
| `$3` | Total number of worker nodes |
| `$4` | Current node index, starting from 0 |
| `$5` | Starting data index |

Example of multi-GPU parallel sampling:

```bash
CUDA_VISIBLE_DEVICES=0 bash scripts/batch_sample_diffusion.sh configs/sampling.yml outputs 4 0 0 &
CUDA_VISIBLE_DEVICES=1 bash scripts/batch_sample_diffusion.sh configs/sampling.yml outputs 4 1 0 &
CUDA_VISIBLE_DEVICES=2 bash scripts/batch_sample_diffusion.sh configs/sampling.yml outputs 4 2 0 &
CUDA_VISIBLE_DEVICES=3 bash scripts/batch_sample_diffusion.sh configs/sampling.yml outputs 4 3 0 &
wait
```

The script internally fixes `TOTAL_TASKS=100` and `BATCH_SIZE=50`, and assigns samples to nodes by index modulo.

#### 4.3 Sampling for a Custom PDB Pocket

```bash
export PYTHONPATH=../../../src:$PYTHONPATH
python -m scripts.sample_for_pocket configs/sampling.yml \
    --pdb_path /path/to/pocket.pdb \
    --result_path ./outputs_pdb \
    --num_samples 5 \
    --batch_size 1
```

Parameter descriptions:

| Parameter | Required | Description |
|------|----------|------|
| `config` | Yes | Sampling configuration file path |
| `--pdb_path` | Yes | Protein pocket PDB file path. A 10 Å pocket is recommended. |
| `--result_path` | No | Result output directory, default `./outputs_pdb` |
| `--num_samples` | No | Number of molecules to sample, read from the configuration file by default |
| `--batch_size` | No | Batch size, default `100` |
| `--device` | No | Compute device, default `cuda:0` |

Sampling results are saved as `outputs_pdb/sample.pt`, and successfully reconstructed molecules are additionally written to `outputs_pdb/sdf/`.

### 5. Generated Molecule Evaluation

#### 5.1 Evaluation from Sampling Results

If docking evaluation is required (`vina_score` / `vina_dock` / `qvina`), also install:

```bash
pip install meeko==0.1.dev3 scipy pdb2pqr vina==1.2.2
python -m pip install git+https://github.com/Valdes-Tresanco-MS/AutoDockTools_py3
```

```bash
export PYTHONPATH=../../../src:$PYTHONPATH
python scripts/evaluate_diffusion.py ./outputs --docking_mode vina_score --protein_root /path/to/protein_root
```

Parameter descriptions:

| Parameter | Required | Description |
|------|----------|------|
| `sample_path` | Yes | Sampling result directory containing `result_*.pt` files |
| `--docking_mode` | Yes | Docking mode. Options: `none`, `vina_score`, `vina_dock`, `qvina` |
| `--protein_root` | No | Root directory of the original protein files, used for docking evaluation |
| `--eval_step` | No | Which sampling step to evaluate, default `-1` (the final step) |
| `--eval_num_examples` | No | Number of samples to evaluate, default all |
| `--exhaustiveness` | No | Docking search intensity, default `16` |
| `--save` | No | Whether to save evaluation results, default `True` |

Supported docking modes:

| Mode | Description |
|------|------|
| `none` | Computes only metrics such as validity, uniqueness, and novelty, without docking |
| `vina_score` | Uses AutoDock Vina to score generated molecules |
| `vina_dock` | Uses AutoDock Vina to redock generated molecules |
| `qvina` | Uses QuickVina for docking |

The first run in `vina_score` or `vina_dock` mode may take some time to prepare `pdbqt` and `pqr` files.

#### 5.2 Evaluation from Meta Files

The official project provides sampled and docked meta files, including TargetDiff and baselines such as CVAE, AR, and Pocket2Mol. These files can be downloaded and evaluated directly:

| Meta file | Corresponding paper |
|-----------|----------|
| `crossdocked_test_vina_docked.pt` | Original test-set docking results |
| `cvae_vina_docked.pt` | liGAN |
| `ar_vina_docked.pt` | AR |
| `pocket2mol_vina_docked.pt` | Pocket2Mol |
| `targetdiff_vina_docked.pt` | TargetDiff |

Official meta file download URL: https://drive.google.com/drive/folders/19imu-mlwrjnQhgbXpwsLgA17s1Rv70YS?usp=share_link

Evaluation command:

```bash
export PYTHONPATH=../../../src:$PYTHONPATH
python scripts/evaluate_from_meta.py sampling_results/targetdiff_vina_docked.pt --result_path eval_targetdiff
```

Parameter descriptions:

| Parameter | Required | Description |
|------|----------|------|
| `meta_file` | Yes | `.pt` file containing sampling and docking results |
| `--result_path` | No | Evaluation result output directory, default `eval_results` |

---

### 6. Diffusion Model Training

```bash
bash train_diffusion.sh
```

By default, this reads `configs/training.yml`, trains on the CrossDocked2020 pocket dataset, and saves logs and checkpoints to `./logs_diffusion/`.

**Custom configuration or parameter overrides:**

```bash
# Specify a custom configuration file
bash train_diffusion.sh configs/custom_training.yml

# Override training parameters from the command line
bash train_diffusion.sh --train.batch_size 8 --train.max_iters 500000
```

**Main training parameters:**

| Parameter | Default value | Description |
|------|--------|------|
| `data.path` | `${ONESCIENCE_DATASETS_DIR}/targetdiff/data/crossdocked_v1.1_rmsd1.0_pocket10` | Training data directory |
| `data.split` | `${ONESCIENCE_DATASETS_DIR}/targetdiff/data/crossdocked_pocket10_pose_split.pt` | Training/validation/test split file |
| `train.batch_size` | `4` | Batch size |
| `train.max_iters` | `10000000` | Maximum number of iterations |
| `train.lr` | `5.e-4` | Learning rate |
| `logdir` | `./logs_diffusion` | Log output directory |

---

### 7. Data Preprocessing

#### 7.1 CrossDocked2020 Data Preprocessing

To process CrossDocked2020 data from scratch, follow these steps:

1. Download CrossDocked2020 v1.1 and save it to `data/CrossDocked2020`.
2. Filter samples with RMSD < 1 Å:

    ```bash
    export PYTHONPATH=../../../src:$PYTHONPATH
    python scripts/data_preparation/clean_crossdocked.py \
        --source data/CrossDocked2020 \
        --dest data/crossdocked_v1.1_rmsd1.0 \
        --rmsd_thr 1.0
    ```

3. Extract 10 Å binding pockets from proteins:

    ```bash
    python scripts/data_preparation/extract_pockets.py \
        --source data/crossdocked_v1.1_rmsd1.0 \
        --dest data/crossdocked_v1.1_rmsd1.0_pocket10
    ```

4. Split the training and test sets:

    ```bash
    python scripts/data_preparation/split_pl_dataset.py \
        --path data/crossdocked_v1.1_rmsd1.0_pocket10 \
        --dest data/crossdocked_pocket10_pose_split.pt \
        --fixed_split data/split_by_name.pt
    ```

#### 7.2 PDBbind Data Preprocessing

The affinity prediction training script `train_prop.sh` automatically performs pocket extraction and dataset splitting. To run these steps separately, use the following commands:

```bash
export PYTHONPATH=../../../src:$PYTHONPATH

python scripts/property_prediction/extract_pockets.py \
    --source data/pdbbind_v2020 \
    --dest data/pdbbind_v2020_processed \
    --subset refined \
    --num_workers 16

python scripts/property_prediction/pdbbind_split.py \
    --split_mode coreset \
    --index_path data/pdbbind_v2020_processed/pocket_10_refined/index.pkl \
    --test_path data/pdbbind_v2016/coreset \
    --save_path data/pdbbind_v2020_processed/pocket_10_refined/split.pt
```

---

# Official OneScience Information

| Platform | OneScience main repository | Skills repository |
| --- | --- | --- |
| Gitee | https://gitee.com/onescience-ai/onescience | https://gitee.com/onescience-ai/oneskills |
| GitHub | https://github.com/onescience-ai/OneScience | https://github.com/onescience-ai/oneskills |

# Citation and License

- The original TargetDiff code is licensed under the MIT License. This repository retains source attribution and is organized for OneScience Hugging Face automated runtime scenarios.
- If you use TargetDiff results in research, we recommend citing the original TargetDiff paper and relevant OneScience project information. Depending on the actual task, also add citations for datasets or tools such as CrossDocked2020, PDBbind, RDKit, OpenBabel, and Vina/QVina.

```bibtex
@inproceedings{guan3d,
  title={3D Equivariant Diffusion for Target-Aware Molecule Generation and Affinity Prediction},
  author={Guan, Jiaqi and Qian, Wesley Wei and Peng, Xingang and Su, Yufeng and Peng, Jian and Ma, Jianzhu},
  booktitle={International Conference on Learning Representations},
  year={2023}
}
```

---

# 中文说明

## 模型概述

TargetDiff 是用于靶标感知分子生成和蛋白质-配体亲和力预测的生物信息学模型。

原始论文为 _面向靶标感知分子生成和亲和力预测的 3D 等变扩散_(ICLR 2023)。

## 模型描述

TargetDiff 基于 3D 等变扩散网络。给定蛋白质结合口袋,它会生成候选小分子,并可使用 EGNN 属性预测分支预测蛋白质-配体复合物的亲和力。

当前软件包将 TargetDiff 示例脚本、模型实现快照、样例输入和预训练权重组织在同一目录中。数据集将于后续上传。

## 使用场景

| 使用场景 | 说明 |
| :---: | :---: |
| 蛋白质-配体亲和力预测 | 以蛋白质 PDB 文件和配体 SDF 文件为输入,输出 `Ki`、`Kd` 或 `IC50` 的摩尔浓度预测值 |
| 靶标感知小分子生成 | 以蛋白质结合口袋 PDB 文件为输入,输出生成分子的 `sample.pt` 文件以及可重建分子的 SDF 文件 |
| 扩散模型训练 | 使用 CrossDocked2020 口袋数据训练 TargetDiff 分子生成模型 |
| 属性预测训练 | 使用 PDBbind 数据训练 EGNN 亲和力预测模型 |
| 生成结果评估 | 计算采样结果的稳定性、重建成功率、QED、SA 及可选的 Vina 分子对接(molecular docking)指标 |

## 使用方法

### 1. 使用 OneCode

可通过 OneCode 在线环境体验智能一键 AI4S 编程。

[体验智能一键 AI4S 编程](https://web-2069360198568017922-iaaj.ksai.scnet.cn:58043/home)

### 2. 手动安装与使用

**硬件要求**

- 建议在 GPU 或 DCU 上运行。
- 可使用 CPU 进行连通性检查,但速度相对较慢。
- DCU 用户需要提前安装 DTK。建议使用 DTK 25.04.2 或更高版本,或使用 OneScience 推荐的与当前集群匹配的版本。

**软件要求**

先进入已安装 OneScience 生物信息学依赖的环境。

**环境检查**

- NVIDIA GPU

```bash
nvidia-smi
```

- Hygon DCU

```bash
hy-smi
```

### 快速开始

#### 1. 安装运行环境

```bash
conda create -n onescience311 python=3.11 -y
conda activate onescience311
pip install onescience[bio] -i http://mirrors.onescience.ai:3141/pypi/simple/  --trusted-host mirrors.onescience.ai
```

如果以下代码在运行时找不到所需库,请按下文所示激活 CUDA。

```bash
source ${ROCM_PATH}/cuda/env.sh
export LD_LIBRARY_PATH="$CONDA_PREFIX/lib:$LD_LIBRARY_PATH"
export LD_LIBRARY_PATH="$CONDA_PREFIX/lib/python3.11/site-packages/fastpt/torch/lib:$LD_LIBRARY_PATH"
```

#### 2. 下载模型

```bash
hf download OneScience-Group/targetdiff --local-dir ./targetdiff
cd targetdiff
```

#### 训练权重与数据集

训练权重已包含在 `weights` 文件夹中,下载模型包后可直接使用。
数据集将很快上传至 Hugging Face,之后将支持命令行下载。

#### 3. 亲和力预测

##### 3.1 亲和力预测训练

```bash
bash scripts/train_prop.sh
```

此脚本自动执行以下步骤。

1. 从 PDBbind refined set 提取结合口袋。
2. 按 coreset 划分训练集、验证集和测试集。
3. 训练基于 EGNN 的蛋白质-配体结合亲和力预测模型。

##### 3.2 亲和力预测评估

使用已训练的亲和力预测模型在测试集上评估。PDBBind v2016 上预期的官方指标如下。

| RMSE | MAE | R² | Pearson | Spearman |
|------|-----|----|---------|----------|
| 1.316 | 1.031 | 0.633 | 0.797 | 0.782 |

运行以下命令。

```bash
export PYTHONPATH=../../../src:$PYTHONPATH
python scripts/property_prediction/eval_prop.py \
    --ckpt_path ${ONESCIENCE_MODELS_DIR}/targetdiff/pretrained_models/egnn_pdbbind_v2016.pt \
    --device cuda
```

##### 3.3 亲和力预测推理(inference)

```bash
bash scripts/inference.sh
```

默认情况下,脚本使用默认权重和示例数据,对示例蛋白质-配体对 `3ug2` 进行亲和力预测。

- 模型权重 `${ONESCIENCE_MODELS_DIR}/targetdiff/pretrained_models/egnn_pdbbind_v2016.pt`
- 蛋白质 `${ONESCIENCE_DATASETS_DIR}/targetdiff/examples/3ug2_protein.pdb`
- 配体 `${ONESCIENCE_DATASETS_DIR}/targetdiff/examples/3ug2_ligand.sdf`
- 亲和力类型,默认为 `Kd`
- 计算设备,默认为 `cuda`

#### 4. 分子采样

##### 4.1 单个测试集样本

```bash
export PYTHONPATH=../../../src:$PYTHONPATH
python -m scripts.sample_diffusion configs/sampling.yml -i 0 --batch_size 50 --result_path ./outputs
```

此脚本从 `configs/sampling.yml` 读取扩散模型权重(检查点)(checkpoint),为测试集中的第 `i` 个口袋生成候选配体分子,将结果保存为 `result_i.pt`。

##### 4.2 多 GPU 批量采样

```bash
bash scripts/batch_sample_diffusion.sh configs/sampling.yml outputs 4 0 0
```

参数说明

| 参数 | 说明 |
|------|------|
| `$1` | 采样配置文件路径 |
| `$2` | 结果输出目录 |
| `$3` | 工作节点总数 |
| `$4` | 当前节点索引,从 0 开始 |
| `$5` | 起始数据索引 |

多 GPU 并行采样示例

```bash
CUDA_VISIBLE_DEVICES=0 bash scripts/batch_sample_diffusion.sh configs/sampling.yml outputs 4 0 0 &
CUDA_VISIBLE_DEVICES=1 bash scripts/batch_sample_diffusion.sh configs/sampling.yml outputs 4 1 0 &
CUDA_VISIBLE_DEVICES=2 bash scripts/batch_sample_diffusion.sh configs/sampling.yml outputs 4 2 0 &
CUDA_VISIBLE_DEVICES=3 bash scripts/batch_sample_diffusion.sh configs/sampling.yml outputs 4 3 0 &
wait
```

脚本内部固定 `TOTAL_TASKS=100` 和 `BATCH_SIZE=50`,并按索引取模将样本分配给各节点。

##### 4.3 对自定义 PDB 口袋采样

```bash
export PYTHONPATH=../../../src:$PYTHONPATH
python -m scripts.sample_for_pocket configs/sampling.yml \
    --pdb_path /path/to/pocket.pdb \
    --result_path ./outputs_pdb \
    --num_samples 5 \
    --batch_size 1
```

参数说明

| 参数 | 必需 | 说明 |
|------|----------|------|
| `config` | 是 | 采样配置文件路径 |
| `--pdb_path` | 是 | 蛋白质口袋 PDB 文件路径。建议使用 10 Å 的口袋。 |
| `--result_path` | 否 | 结果输出目录,默认为 `./outputs_pdb` |
| `--num_samples` | 否 | 要采样的分子数量,默认从配置文件读取 |
| `--batch_size` | 否 | 批大小,默认为 `100` |
| `--device` | 否 | 计算设备,默认为 `cuda:0` |

采样结果保存为 `outputs_pdb/sample.pt`,成功重建的分子还会写入 `outputs_pdb/sdf/`。

#### 5. 生成分子评估

##### 5.1 根据采样结果评估

如需进行分子对接评估(`vina_score` / `vina_dock` / `qvina`),还需安装以下依赖。

```bash
pip install meeko==0.1.dev3 scipy pdb2pqr vina==1.2.2
python -m pip install git+https://github.com/Valdes-Tresanco-MS/AutoDockTools_py3
```

```bash
export PYTHONPATH=../../../src:$PYTHONPATH
python scripts/evaluate_diffusion.py ./outputs --docking_mode vina_score --protein_root /path/to/protein_root
```

参数说明

| 参数 | 必需 | 说明 |
|------|----------|------|
| `sample_path` | 是 | 包含 `result_*.pt` 文件的采样结果目录 |
| `--docking_mode` | 是 | 分子对接模式。选项为 `none`、`vina_score`、`vina_dock`、`qvina` |
| `--protein_root` | 否 | 原始蛋白质文件根目录,用于分子对接评估 |
| `--eval_step` | 否 | 要评估的采样步骤,默认为 `-1`(最后一步) |
| `--eval_num_examples` | 否 | 要评估的样本数,默认为全部 |
| `--exhaustiveness` | 否 | 分子对接搜索强度,默认为 `16` |
| `--save` | 否 | 是否保存评估结果,默认为 `True` |

支持的分子对接模式

| 模式 | 说明 |
|------|------|
| `none` | 仅计算有效性、唯一性和新颖性等指标,无需分子对接 |
| `vina_score` | 使用 AutoDock Vina 为生成的分子评分 |
| `vina_dock` | 使用 AutoDock Vina 对生成的分子重新进行分子对接 |
| `qvina` | 使用 QuickVina 进行分子对接 |

首次以 `vina_score` 或 `vina_dock` 模式运行时,准备 `pdbqt` 和 `pqr` 文件可能需要一些时间。

##### 5.2 根据元数据文件评估

官方项目提供已采样并完成分子对接的元数据文件,涵盖 TargetDiff 及 CVAE、AR 和 Pocket2Mol 等基线。可直接下载并评估这些文件。

| 元数据文件 | 对应论文 |
|-----------|----------|
| `crossdocked_test_vina_docked.pt` | 原始测试集的分子对接结果 |
| `cvae_vina_docked.pt` | liGAN |
| `ar_vina_docked.pt` | AR |
| `pocket2mol_vina_docked.pt` | Pocket2Mol |
| `targetdiff_vina_docked.pt` | TargetDiff |

官方元数据文件下载地址 https://drive.google.com/drive/folders/19imu-mlwrjnQhgbXpwsLgA17s1Rv70YS?usp=share_link

评估命令

```bash
export PYTHONPATH=../../../src:$PYTHONPATH
python scripts/evaluate_from_meta.py sampling_results/targetdiff_vina_docked.pt --result_path eval_targetdiff
```

参数说明

| 参数 | 必需 | 说明 |
|------|----------|------|
| `meta_file` | 是 | 包含采样和分子对接结果的 `.pt` 文件 |
| `--result_path` | 否 | 评估结果输出目录,默认为 `eval_results` |

---

#### 6. 扩散模型训练

```bash
bash train_diffusion.sh
```

默认读取 `configs/training.yml`,在 CrossDocked2020 口袋数据集上训练,并将日志和模型权重(检查点)保存到 `./logs_diffusion/`。

**自定义配置或覆盖参数**

```bash
# Specify a custom configuration file
bash train_diffusion.sh configs/custom_training.yml

# Override training parameters from the command line
bash train_diffusion.sh --train.batch_size 8 --train.max_iters 500000
```

**主要训练参数**

| 参数 | 默认值 | 说明 |
|------|--------|------|
| `data.path` | `${ONESCIENCE_DATASETS_DIR}/targetdiff/data/crossdocked_v1.1_rmsd1.0_pocket10` | 训练数据目录 |
| `data.split` | `${ONESCIENCE_DATASETS_DIR}/targetdiff/data/crossdocked_pocket10_pose_split.pt` | 训练/验证/测试划分文件 |
| `train.batch_size` | `4` | 批大小 |
| `train.max_iters` | `10000000` | 最大迭代次数 |
| `train.lr` | `5.e-4` | 学习率 |
| `logdir` | `./logs_diffusion` | 日志输出目录 |

---

#### 7. 数据预处理

##### 7.1 CrossDocked2020 数据预处理

要从头处理 CrossDocked2020 数据,请执行以下步骤。

1. 下载 CrossDocked2020 v1.1 并保存至 `data/CrossDocked2020`。
2. 筛选 RMSD < 1 Å 的样本。

    ```bash
    export PYTHONPATH=../../../src:$PYTHONPATH
    python scripts/data_preparation/clean_crossdocked.py \
        --source data/CrossDocked2020 \
        --dest data/crossdocked_v1.1_rmsd1.0 \
        --rmsd_thr 1.0
    ```

3. 从蛋白质中提取 10 Å 结合口袋。

    ```bash
    python scripts/data_preparation/extract_pockets.py \
        --source data/crossdocked_v1.1_rmsd1.0 \
        --dest data/crossdocked_v1.1_rmsd1.0_pocket10
    ```

4. 划分训练集和测试集。

    ```bash
    python scripts/data_preparation/split_pl_dataset.py \
        --path data/crossdocked_v1.1_rmsd1.0_pocket10 \
        --dest data/crossdocked_pocket10_pose_split.pt \
        --fixed_split data/split_by_name.pt
    ```

##### 7.2 PDBbind 数据预处理

亲和力预测训练脚本 `train_prop.sh` 自动执行口袋提取和数据集划分。如需分别运行这些步骤,请使用以下命令。

```bash
export PYTHONPATH=../../../src:$PYTHONPATH

python scripts/property_prediction/extract_pockets.py \
    --source data/pdbbind_v2020 \
    --dest data/pdbbind_v2020_processed \
    --subset refined \
    --num_workers 16

python scripts/property_prediction/pdbbind_split.py \
    --split_mode coreset \
    --index_path data/pdbbind_v2020_processed/pocket_10_refined/index.pkl \
    --test_path data/pdbbind_v2016/coreset \
    --save_path data/pdbbind_v2020_processed/pocket_10_refined/split.pt
```

---

## OneScience 官方信息

| 平台 | OneScience 主仓库 | Skills 仓库 |
| --- | --- | --- |
| Gitee | https://gitee.com/onescience-ai/onescience | https://gitee.com/onescience-ai/oneskills |
| GitHub | https://github.com/onescience-ai/OneScience | https://github.com/onescience-ai/oneskills |

## 引用与许可证

- TargetDiff 原始代码采用 MIT 许可证。本仓库保留来源署名,并按 OneScience Hugging Face 自动运行场景组织。
- 若在研究中使用 TargetDiff 结果,建议引用 TargetDiff 原始论文和相关 OneScience 项目信息。根据实际任务,也请引用 CrossDocked2020、PDBbind、RDKit、OpenBabel 和 Vina/QVina 等数据集或工具。

```bibtex
@inproceedings{guan3d,
  title={3D Equivariant Diffusion for Target-Aware Molecule Generation and Affinity Prediction},
  author={Guan, Jiaqi and Qian, Wesley Wei and Peng, Xingang and Su, Yufeng and Peng, Jian and Ma, Jianzhu},
  booktitle={International Conference on Learning Representations},
  year={2023}
}
```