File size: 10,778 Bytes
88496ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
---
frameworks:
- TensorFlow
- Keras
language:
- en
- zh
license: mit
tags:
- OneScience
- life-science
- LSTM
- CDR-sequence
- amino-acid-sequence-generation
tasks:
- training
- sampling
---
<p align="center">
  <strong>
    <span style="font-size: 30px;">LSTM_CDRs</span>
  </strong>
</p>

# Model Introduction

LSTM_CDRs is a CDR amino acid sequence generation model based on long short-term memory networks (Long Short-Term Memory, LSTM). Given a set of CDR sequences, the model learns the sequence distribution in the training set and generates new CDR sequences through sampling after training.

# Model Description

This project uses recurrent neural networks to autoregressively model amino acid sequences. Input sequences are first padded and one-hot encoded, then passed to multilayer LSTM or GRU networks for training. After training, the script can load model weights from a specified epoch and sample new CDR sequences from the learned sequence distribution.

The official code is adapted from `LSTM_peptides` for VHH CDR sequence design tasks.

# Use Cases

| Use case | Description |
| --- | --- |
| CDR sequence generation | Learn the amino acid sequence distribution from a given CDR training set and sample new candidate sequences. |
| Local LSTM/GRU training | Train LSTM/GRU sequence generation models locally or on GPU/DCU platforms using the scripts in this repository. |
| Weight loading and sampling reproduction | Load checkpoint weights generated during training and generate CDR sequences with specified lengths, temperature, and sample count. |

# Usage

## 1. Using OneCode

Experience intelligent one-click AI4S programming in 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**

- A GPU or DCU is recommended for training and sampling tasks.
- A CPU can be used for small-scale connectivity checks, but full training and large-scale sampling are slower.
- DCU users need to use DTK, TensorFlow, and the OneScience environment compatible with the current cluster.

## 3. Quick Start

### Download the Model Package

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

### Set Up the Runtime Environment

#### DCU Environment

```bash
# Activate DTK and CONDA first
conda create -n onescience311 python=3.11 -y
conda activate onescience311
# Install with uv support
pip install onescience[bio] -i http://mirrors.onescience.ai:3141/pypi/simple/  --trusted-host mirrors.onescience.ai
```

#### Environment Notes
- If you encounter missing dependencies or version compatibility issues, refer to the dependency versions specified in `requirements.txt` and `environment.yml` and install or adjust the environment as needed.
- If you encounter TensorFlow-related issues during execution, uninstall TensorFlow from the current environment and resolve them as follows:
```bash
# 1. Download TensorFlow from the platform
wget --content-disposition 'https://download.sourcefind.cn:65024/file/4/tensorflow/DAS1.8/tensorflow-2.13.1+das.opt1.dtk2604-cp311-cp311-manylinux_2_28_x86_64.whl'

# 2. Install TensorFlow
pip install tensorflow* 

# 3. Load the corresponding DTK version
module load compiler/dtk/26.04 
```
- You can also build the runtime environment for your platform according to the dependencies declared in `requirements.txt` and `environment.yml`.

## Quick Verification

```bash
python LSTM_CDRs.py --help
ls data
```

The training data should include:

```text
data/Cluster1.csv
data/Cluster2.csv
data/Cluster3.csv
data/Cluster4.csv
```

The data files are:

| Data file | Number of sequences | Sequence length |
| --- | ---: | --- |
| `data/Cluster1.csv` | 2629 | 36 |
| `data/Cluster2.csv` | 4146 | 36 |
| `data/Cluster3.csv` | 2990 | 35 |
| `data/Cluster4.csv` | 11952 | 36 |

## Weights and Data Preparation

The current repository already includes the training data:

```text
data/Cluster1.csv
data/Cluster2.csv
data/Cluster3.csv
data/Cluster4.csv
```

The repository does not provide pretrained weight files. The weights required for sampling must first be generated through training.

After training, each experiment directory contains:

```text
<run_name>/
  flags.txt
  <run_name>_loss_plot.pdf
  sampled_sequences_temp1.25.csv
  checkpoint/
    model.json
    model.p
    model.hdf5
    model_epoch_0.hdf5
    model_epoch_1.hdf5
    ...
```

For sampling or fine-tuning, `--modfile` should point to an existing epoch weight, for example:

```text
Cluster1_LSTM/checkpoint/model_epoch_100.hdf5
```

The same `checkpoint/` directory must also retain:

```text
model.p
model.hdf5
model_epoch_*.hdf5
```

## Training

### Run a Minimal Training Check with the Script

```bash
python LSTM_CDRs.py \
  --name smoke_Cluster1 \
  --dataset data/Cluster1.csv \
  --layers 1 \
  --neurons 16 \
  --epochs 1 \
  --batch_size 64 \
  --dropout 0.1 \
  --sample 10
```

This command verifies that data loading, padding, one-hot encoding, model training, and sampling work end to end.

View the outputs:

```bash
ls smoke_Cluster1
ls smoke_Cluster1/checkpoint
head smoke_Cluster1/sampled_sequences_temp1.25.csv
```

### Train the Cluster1 Model

```bash
python LSTM_CDRs.py \
  --name Cluster1_LSTM \
  --dataset data/Cluster1.csv \
  --layers 2 \
  --neurons 64 \
  --epochs 200 \
  --dropout 0.2
```

By default, this command samples 100 sequences after training and saves them to:

```text
Cluster1_LSTM/sampled_sequences_temp1.25.csv
```

View the training logs and weights:

```bash
ls Cluster1_LSTM
ls Cluster1_LSTM/checkpoint
```

### Train All Four Clusters

```bash
python LSTM_CDRs.py --name Cluster1_LSTM --dataset data/Cluster1.csv --layers 2 --neurons 64 --epochs 200 --dropout 0.2
python LSTM_CDRs.py --name Cluster2_LSTM --dataset data/Cluster2.csv --layers 2 --neurons 64 --epochs 200 --dropout 0.2
python LSTM_CDRs.py --name Cluster3_LSTM --dataset data/Cluster3.csv --layers 2 --neurons 64 --epochs 200 --dropout 0.2
python LSTM_CDRs.py --name Cluster4_LSTM --dataset data/Cluster4.csv --layers 2 --neurons 64 --epochs 200 --dropout 0.2
```

## Sampling

Sampling is the generation step performed after training. The script loads existing model weights and generates new sequences from the learned CDR sequence distribution.

### Cluster1 Sampling

```bash
python LSTM_CDRs.py \
  --name Cluster1_LSTM \
  --dataset data/Cluster1.csv \
  --modfile Cluster1_LSTM/checkpoint/model_epoch_100.hdf5 \
  --train False \
  --sample 10000 \
  -f 36 \
  -m 36
```

This command uses the following by default:

```text
Training data: data/Cluster1.csv
Model weights: Cluster1_LSTM/checkpoint/model_epoch_100.hdf5
Sample count: 10000
Minimum length: 36
Maximum length: 36
Output file: Cluster1_LSTM/sampled_sequences_temp1.25.csv
```

### Cluster3 Sampling

The original Cluster3 sequences have length 35, so `-f 35 -m 35` is recommended for sampling:

```bash
python LSTM_CDRs.py \
  --name Cluster3_LSTM \
  --dataset data/Cluster3.csv \
  --modfile Cluster3_LSTM/checkpoint/model_epoch_100.hdf5 \
  --train False \
  --sample 10000 \
  -f 35 \
  -m 35
```

### View Sampling Results

```bash
wc -l Cluster1_LSTM/sampled_sequences_temp1.25.csv
head Cluster1_LSTM/sampled_sequences_temp1.25.csv
```

Check generated sequence lengths:

```bash
awk '{print length($0)}' Cluster1_LSTM/sampled_sequences_temp1.25.csv | sort -n | uniq -c
```

Check the number of generated sequences duplicated in the training set:

```bash
grep -Fxf data/Cluster1.csv Cluster1_LSTM/sampled_sequences_temp1.25.csv | wc -l
```

### Fine-Tuning

```bash
python LSTM_CDRs.py \
  --name Cluster1_to_Cluster2_finetune \
  --dataset data/Cluster2.csv \
  --modfile Cluster1_LSTM/checkpoint/model_epoch_100.hdf5 \
  --train False \
  --finetune True \
  --epochs 50 \
  --layers 2 \
  --neurons 64 \
  --dropout 0.2
```

### Cross-Validation

```bash
python LSTM_CDRs.py \
  --name Cluster1_CV \
  --dataset data/Cluster1.csv \
  --layers 2 \
  --neurons 64 \
  --epochs 50 \
  --dropout 0.2 \
  --cv 5
```
## Common Parameters

### Training Parameters

| Parameter | Description | Default/Example |
| --- | --- | --- |
| `--dataset` | Path to the training data CSV file | `data/Cluster1.csv` |
| `--name` | Experiment name and output directory name | `Cluster1_LSTM` |
| `--layers` | Number of LSTM/GRU layers | Example: `2` |
| `--neurons` | Number of neurons per layer | Example: `64` |
| `--epochs` | Number of training epochs | Example: `200` |
| `--batch_size` | Batch size | Default: `128` |
| `--dropout` | Dropout ratio; layer n uses `n * dropout` | Example: `0.2` |
| `--cell` | Recurrent neural network cell type | `LSTM` or `GRU` |
| `--lr` | Adam learning rate | Default: `0.01` |
| `--valsplit` | Validation split ratio | Default: `0.2` |
| `--cv` | Number of cross-validation folds | Disabled by default |

### Sampling Parameters

| Parameter | Description | Default/Example |
| --- | --- | --- |
| `--train False` | Do not train; load an existing model for sampling | Required for sampling |
| `--modfile` | Path to trained epoch weights | `Cluster1_LSTM/checkpoint/model_epoch_100.hdf5` |
| `--sample` | Number of sequences to generate | Example: `10000` |
| `--temp` | Sampling temperature | Default: `1.25` |
| `-f`, `--fminlen` | Minimum generated sequence length | `36` for Cluster1/2/4; `35` for Cluster3 |
| `-m`, `--maxlen` | Maximum generated sequence length | `36` for Cluster1/2/4; `35` for Cluster3 |
| `--startchar` | Sampling start character | Default: `B` |

## Official OneScience Information

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

## Citation and License

- Related work: A. T. Mueller, J. A. Hiss, G. Schneider, "Recurrent Neural Network Model for Constructive Peptide Design", Journal of Chemical Information and Modeling, 2018, DOI: 10.1021/acs.jcim.7b00414.
- Application paper: P. Arras et al., "AI/ML combined with Next Generation Sequencing of VHH immune repertoires enables the rapid identification of de novo humanized and sequence-optimized single domain antibodies: a prospective case study", Frontiers in Molecular Biosciences, 2023, DOI: 10.3389/fmolb.2023.1249247.
- This project uses the MIT License; see `LICENSE` in the repository root. For specific terms governing the use of data and model weights, follow the information provided by the respective publishers.