AlexSychovUN commited on
Commit
7e792a6
·
1 Parent(s): 229e134

Added files

Browse files
Files changed (4) hide show
  1. EDA.ipynb +340 -0
  2. dataset.py +167 -0
  3. model.py +116 -0
  4. train.py +70 -0
EDA.ipynb ADDED
@@ -0,0 +1,340 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "id": "initial_id",
6
+ "metadata": {
7
+ "collapsed": true,
8
+ "ExecuteTime": {
9
+ "end_time": "2025-12-14T17:07:51.424473Z",
10
+ "start_time": "2025-12-14T17:07:50.941145Z"
11
+ }
12
+ },
13
+ "source": "import pandas as pd",
14
+ "outputs": [],
15
+ "execution_count": 1
16
+ },
17
+ {
18
+ "metadata": {
19
+ "ExecuteTime": {
20
+ "end_time": "2025-12-14T17:07:51.576127Z",
21
+ "start_time": "2025-12-14T17:07:51.545229Z"
22
+ }
23
+ },
24
+ "cell_type": "code",
25
+ "source": "dataset = pd.read_csv(\"pdbbind_refined_dataset.csv\")",
26
+ "id": "5e25fb1118050711",
27
+ "outputs": [],
28
+ "execution_count": 2
29
+ },
30
+ {
31
+ "metadata": {
32
+ "ExecuteTime": {
33
+ "end_time": "2025-12-14T17:07:55.548660Z",
34
+ "start_time": "2025-12-14T17:07:55.528324Z"
35
+ }
36
+ },
37
+ "cell_type": "code",
38
+ "source": "dataset",
39
+ "id": "f1f5daab7e2df86d",
40
+ "outputs": [
41
+ {
42
+ "data": {
43
+ "text/plain": [
44
+ " pdb_id smiles \\\n",
45
+ "0 2r58 C[NH+](C)CCCC[C@H]([NH3+])C(=O)O \n",
46
+ "1 3c2f O=P(O)(O)OC[C@H]1O[C@H](O[P@](=O)(O)OP(=O)(O)O... \n",
47
+ "2 3g2y CCc1c(C)[nH]n(-c2nnn[nH]2)c1=O \n",
48
+ "3 3pce O=C(O)Cc1cccc(O)c1 \n",
49
+ "4 4qsu C[C@H]1C=NC(=O)NC1=O \n",
50
+ "... ... ... \n",
51
+ "5311 4f3c CCCCSC[C@H]1C[N@@H+](Cc2c[nH]c3c(N)ncnc23)C[C@... \n",
52
+ "5312 5bry C[NH2+][C@H]1CO[C@@H]2OC[C@H](OC(=O)N[C@@H](Cc... \n",
53
+ "5313 1sl3 O=C(Cn1c(Cl)cnc(NCC(F)(F)c2cccc[n+]2[O-])c1=O)... \n",
54
+ "5314 1ctu O=C1N[C@H](O)C=CN1[C@@H]1O[C@H](CO)[C@@H](O)[C... \n",
55
+ "5315 6e9a COc1ccc(S(=O)(=O)N(CC(C)C)C[C@@H](O)[C@H](Cc2c... \n",
56
+ "\n",
57
+ " sequence affinity \n",
58
+ "0 AFDWDAYLEETGSEAAPAKCFKQAQNPPNNDFKIGMKLEALDPRNV... 2.00 \n",
59
+ "1 PVYEHLLPVNGAWRQDVTNWLSEDVPSFDFGGYVVGSDLKEANLYC... 2.00 \n",
60
+ "2 TSAVQQKLAALEKSSGGRLGVALIDTADNTQVLYRGDERFPMCSTS... 2.00 \n",
61
+ "3 PAQDNSRFVIRDRNWHPKALTPDYKTSIARSPRQALVSIPQSISET... 2.00 \n",
62
+ "4 SMQEEDTFRELRIFLRNVTHRLAIDKRFRVFTKPVDPDEVPDYVTV... 2.00 \n",
63
+ "... ... ... \n",
64
+ "5311 MKIGIIGAMEEEVTLLRDKIDNRQTITLGGCEIYTGQLNGTEVALL... 11.82 \n",
65
+ "5312 PQITLWKRPLVTIKIGGQLKEALLDTGADDTVIEEMSLPGRWKPKM... 11.82 \n",
66
+ "5313 DCGLRPLFEKKSLEDKTERELLESYIIVEGSDAEIGMSPWQVMLFR... 11.85 \n",
67
+ "5314 MHPRFQTAFAQLADNLQSALEPILADKYFPALLTGEQVSSLKSATG... 11.92 \n",
68
+ "5315 PQITLWKRPLVTIKIGGQLKEALLDTGADDTVIEEMSLPGRWKPKM... 11.92 \n",
69
+ "\n",
70
+ "[5316 rows x 4 columns]"
71
+ ],
72
+ "text/html": [
73
+ "<div>\n",
74
+ "<style scoped>\n",
75
+ " .dataframe tbody tr th:only-of-type {\n",
76
+ " vertical-align: middle;\n",
77
+ " }\n",
78
+ "\n",
79
+ " .dataframe tbody tr th {\n",
80
+ " vertical-align: top;\n",
81
+ " }\n",
82
+ "\n",
83
+ " .dataframe thead th {\n",
84
+ " text-align: right;\n",
85
+ " }\n",
86
+ "</style>\n",
87
+ "<table border=\"1\" class=\"dataframe\">\n",
88
+ " <thead>\n",
89
+ " <tr style=\"text-align: right;\">\n",
90
+ " <th></th>\n",
91
+ " <th>pdb_id</th>\n",
92
+ " <th>smiles</th>\n",
93
+ " <th>sequence</th>\n",
94
+ " <th>affinity</th>\n",
95
+ " </tr>\n",
96
+ " </thead>\n",
97
+ " <tbody>\n",
98
+ " <tr>\n",
99
+ " <th>0</th>\n",
100
+ " <td>2r58</td>\n",
101
+ " <td>C[NH+](C)CCCC[C@H]([NH3+])C(=O)O</td>\n",
102
+ " <td>AFDWDAYLEETGSEAAPAKCFKQAQNPPNNDFKIGMKLEALDPRNV...</td>\n",
103
+ " <td>2.00</td>\n",
104
+ " </tr>\n",
105
+ " <tr>\n",
106
+ " <th>1</th>\n",
107
+ " <td>3c2f</td>\n",
108
+ " <td>O=P(O)(O)OC[C@H]1O[C@H](O[P@](=O)(O)OP(=O)(O)O...</td>\n",
109
+ " <td>PVYEHLLPVNGAWRQDVTNWLSEDVPSFDFGGYVVGSDLKEANLYC...</td>\n",
110
+ " <td>2.00</td>\n",
111
+ " </tr>\n",
112
+ " <tr>\n",
113
+ " <th>2</th>\n",
114
+ " <td>3g2y</td>\n",
115
+ " <td>CCc1c(C)[nH]n(-c2nnn[nH]2)c1=O</td>\n",
116
+ " <td>TSAVQQKLAALEKSSGGRLGVALIDTADNTQVLYRGDERFPMCSTS...</td>\n",
117
+ " <td>2.00</td>\n",
118
+ " </tr>\n",
119
+ " <tr>\n",
120
+ " <th>3</th>\n",
121
+ " <td>3pce</td>\n",
122
+ " <td>O=C(O)Cc1cccc(O)c1</td>\n",
123
+ " <td>PAQDNSRFVIRDRNWHPKALTPDYKTSIARSPRQALVSIPQSISET...</td>\n",
124
+ " <td>2.00</td>\n",
125
+ " </tr>\n",
126
+ " <tr>\n",
127
+ " <th>4</th>\n",
128
+ " <td>4qsu</td>\n",
129
+ " <td>C[C@H]1C=NC(=O)NC1=O</td>\n",
130
+ " <td>SMQEEDTFRELRIFLRNVTHRLAIDKRFRVFTKPVDPDEVPDYVTV...</td>\n",
131
+ " <td>2.00</td>\n",
132
+ " </tr>\n",
133
+ " <tr>\n",
134
+ " <th>...</th>\n",
135
+ " <td>...</td>\n",
136
+ " <td>...</td>\n",
137
+ " <td>...</td>\n",
138
+ " <td>...</td>\n",
139
+ " </tr>\n",
140
+ " <tr>\n",
141
+ " <th>5311</th>\n",
142
+ " <td>4f3c</td>\n",
143
+ " <td>CCCCSC[C@H]1C[N@@H+](Cc2c[nH]c3c(N)ncnc23)C[C@...</td>\n",
144
+ " <td>MKIGIIGAMEEEVTLLRDKIDNRQTITLGGCEIYTGQLNGTEVALL...</td>\n",
145
+ " <td>11.82</td>\n",
146
+ " </tr>\n",
147
+ " <tr>\n",
148
+ " <th>5312</th>\n",
149
+ " <td>5bry</td>\n",
150
+ " <td>C[NH2+][C@H]1CO[C@@H]2OC[C@H](OC(=O)N[C@@H](Cc...</td>\n",
151
+ " <td>PQITLWKRPLVTIKIGGQLKEALLDTGADDTVIEEMSLPGRWKPKM...</td>\n",
152
+ " <td>11.82</td>\n",
153
+ " </tr>\n",
154
+ " <tr>\n",
155
+ " <th>5313</th>\n",
156
+ " <td>1sl3</td>\n",
157
+ " <td>O=C(Cn1c(Cl)cnc(NCC(F)(F)c2cccc[n+]2[O-])c1=O)...</td>\n",
158
+ " <td>DCGLRPLFEKKSLEDKTERELLESYIIVEGSDAEIGMSPWQVMLFR...</td>\n",
159
+ " <td>11.85</td>\n",
160
+ " </tr>\n",
161
+ " <tr>\n",
162
+ " <th>5314</th>\n",
163
+ " <td>1ctu</td>\n",
164
+ " <td>O=C1N[C@H](O)C=CN1[C@@H]1O[C@H](CO)[C@@H](O)[C...</td>\n",
165
+ " <td>MHPRFQTAFAQLADNLQSALEPILADKYFPALLTGEQVSSLKSATG...</td>\n",
166
+ " <td>11.92</td>\n",
167
+ " </tr>\n",
168
+ " <tr>\n",
169
+ " <th>5315</th>\n",
170
+ " <td>6e9a</td>\n",
171
+ " <td>COc1ccc(S(=O)(=O)N(CC(C)C)C[C@@H](O)[C@H](Cc2c...</td>\n",
172
+ " <td>PQITLWKRPLVTIKIGGQLKEALLDTGADDTVIEEMSLPGRWKPKM...</td>\n",
173
+ " <td>11.92</td>\n",
174
+ " </tr>\n",
175
+ " </tbody>\n",
176
+ "</table>\n",
177
+ "<p>5316 rows × 4 columns</p>\n",
178
+ "</div>"
179
+ ]
180
+ },
181
+ "execution_count": 3,
182
+ "metadata": {},
183
+ "output_type": "execute_result"
184
+ }
185
+ ],
186
+ "execution_count": 3
187
+ },
188
+ {
189
+ "metadata": {
190
+ "ExecuteTime": {
191
+ "end_time": "2025-12-14T17:08:03.034505Z",
192
+ "start_time": "2025-12-14T17:08:03.021615Z"
193
+ }
194
+ },
195
+ "cell_type": "code",
196
+ "source": "dataset.describe()",
197
+ "id": "3607df642b3b7c29",
198
+ "outputs": [
199
+ {
200
+ "data": {
201
+ "text/plain": [
202
+ " affinity\n",
203
+ "count 5316.000000\n",
204
+ "mean 6.392466\n",
205
+ "std 1.952730\n",
206
+ "min 2.000000\n",
207
+ "25% 4.930000\n",
208
+ "50% 6.420000\n",
209
+ "75% 7.740000\n",
210
+ "max 11.920000"
211
+ ],
212
+ "text/html": [
213
+ "<div>\n",
214
+ "<style scoped>\n",
215
+ " .dataframe tbody tr th:only-of-type {\n",
216
+ " vertical-align: middle;\n",
217
+ " }\n",
218
+ "\n",
219
+ " .dataframe tbody tr th {\n",
220
+ " vertical-align: top;\n",
221
+ " }\n",
222
+ "\n",
223
+ " .dataframe thead th {\n",
224
+ " text-align: right;\n",
225
+ " }\n",
226
+ "</style>\n",
227
+ "<table border=\"1\" class=\"dataframe\">\n",
228
+ " <thead>\n",
229
+ " <tr style=\"text-align: right;\">\n",
230
+ " <th></th>\n",
231
+ " <th>affinity</th>\n",
232
+ " </tr>\n",
233
+ " </thead>\n",
234
+ " <tbody>\n",
235
+ " <tr>\n",
236
+ " <th>count</th>\n",
237
+ " <td>5316.000000</td>\n",
238
+ " </tr>\n",
239
+ " <tr>\n",
240
+ " <th>mean</th>\n",
241
+ " <td>6.392466</td>\n",
242
+ " </tr>\n",
243
+ " <tr>\n",
244
+ " <th>std</th>\n",
245
+ " <td>1.952730</td>\n",
246
+ " </tr>\n",
247
+ " <tr>\n",
248
+ " <th>min</th>\n",
249
+ " <td>2.000000</td>\n",
250
+ " </tr>\n",
251
+ " <tr>\n",
252
+ " <th>25%</th>\n",
253
+ " <td>4.930000</td>\n",
254
+ " </tr>\n",
255
+ " <tr>\n",
256
+ " <th>50%</th>\n",
257
+ " <td>6.420000</td>\n",
258
+ " </tr>\n",
259
+ " <tr>\n",
260
+ " <th>75%</th>\n",
261
+ " <td>7.740000</td>\n",
262
+ " </tr>\n",
263
+ " <tr>\n",
264
+ " <th>max</th>\n",
265
+ " <td>11.920000</td>\n",
266
+ " </tr>\n",
267
+ " </tbody>\n",
268
+ "</table>\n",
269
+ "</div>"
270
+ ]
271
+ },
272
+ "execution_count": 4,
273
+ "metadata": {},
274
+ "output_type": "execute_result"
275
+ }
276
+ ],
277
+ "execution_count": 4
278
+ },
279
+ {
280
+ "metadata": {
281
+ "ExecuteTime": {
282
+ "end_time": "2025-12-14T17:08:23.940133Z",
283
+ "start_time": "2025-12-14T17:08:23.925955Z"
284
+ }
285
+ },
286
+ "cell_type": "code",
287
+ "source": "dataset.info()",
288
+ "id": "c747950337bfdc2b",
289
+ "outputs": [
290
+ {
291
+ "name": "stdout",
292
+ "output_type": "stream",
293
+ "text": [
294
+ "<class 'pandas.core.frame.DataFrame'>\n",
295
+ "RangeIndex: 5316 entries, 0 to 5315\n",
296
+ "Data columns (total 4 columns):\n",
297
+ " # Column Non-Null Count Dtype \n",
298
+ "--- ------ -------------- ----- \n",
299
+ " 0 pdb_id 5316 non-null object \n",
300
+ " 1 smiles 5314 non-null object \n",
301
+ " 2 sequence 5316 non-null object \n",
302
+ " 3 affinity 5316 non-null float64\n",
303
+ "dtypes: float64(1), object(3)\n",
304
+ "memory usage: 166.3+ KB\n"
305
+ ]
306
+ }
307
+ ],
308
+ "execution_count": 5
309
+ },
310
+ {
311
+ "metadata": {},
312
+ "cell_type": "code",
313
+ "outputs": [],
314
+ "execution_count": null,
315
+ "source": "",
316
+ "id": "28375d839fb776f6"
317
+ }
318
+ ],
319
+ "metadata": {
320
+ "kernelspec": {
321
+ "display_name": "Python 3",
322
+ "language": "python",
323
+ "name": "python3"
324
+ },
325
+ "language_info": {
326
+ "codemirror_mode": {
327
+ "name": "ipython",
328
+ "version": 2
329
+ },
330
+ "file_extension": ".py",
331
+ "mimetype": "text/x-python",
332
+ "name": "python",
333
+ "nbconvert_exporter": "python",
334
+ "pygments_lexer": "ipython2",
335
+ "version": "2.7.6"
336
+ }
337
+ },
338
+ "nbformat": 4,
339
+ "nbformat_minor": 5
340
+ }
dataset.py ADDED
@@ -0,0 +1,167 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import torch
3
+ import pandas as pd
4
+ from rdkit import Chem, rdBase
5
+ from torch_geometric.data import Data
6
+ from torch.utils.data import Dataset, random_split
7
+
8
+ rdBase.DisableLog("rdApp.*")
9
+
10
+
11
+ def one_of_k_encoding(x, allowable_set):
12
+ # last position - unknown
13
+ if x not in allowable_set:
14
+ x = allowable_set[-1]
15
+ return list(map(lambda s: x == s, allowable_set))
16
+
17
+
18
+ def get_atom_features(atom):
19
+ symbols_list = [
20
+ "C",
21
+ "N",
22
+ "O",
23
+ "S",
24
+ "F",
25
+ "Si",
26
+ "P",
27
+ "Cl",
28
+ "Br",
29
+ "Mg",
30
+ "Na",
31
+ "Ca",
32
+ "Fe",
33
+ "As",
34
+ "Al",
35
+ "I",
36
+ "B",
37
+ "V",
38
+ "K",
39
+ "Tl",
40
+ "Yb",
41
+ "Sb",
42
+ "Sn",
43
+ "Ag",
44
+ "Pd",
45
+ "Co",
46
+ "Se",
47
+ "Ti",
48
+ "Zn",
49
+ "H",
50
+ "Li",
51
+ "Ge",
52
+ "Cu",
53
+ "Au",
54
+ "Ni",
55
+ "Cd",
56
+ "In",
57
+ "Mn",
58
+ "Zr",
59
+ "Cr",
60
+ "Pt",
61
+ "Hg",
62
+ "Pb",
63
+ "Unknown",
64
+ ]
65
+ degrees_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
66
+ numhs_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
67
+ implicit_valences_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
68
+ return np.array(
69
+ # Type of atom (Symbol)
70
+ one_of_k_encoding(atom.GetSymbol(), symbols_list)
71
+ +
72
+ # Number of neighbours (Degree)
73
+ one_of_k_encoding(atom.GetDegree(), degrees_list)
74
+ +
75
+ # Number of hydrogen atoms (Implicit Hs) - bond donors
76
+ one_of_k_encoding(atom.GetTotalNumHs(), numhs_list)
77
+ +
78
+ # Valence - chemical potential
79
+ one_of_k_encoding(atom.GetImplicitValence(), implicit_valences_list)
80
+ +
81
+ # Hybridization - so important for 3d structure, sp2 - Trigonal planar, sp3 - Tetrahedral
82
+ one_of_k_encoding(
83
+ atom.GetHybridization(),
84
+ [
85
+ Chem.rdchem.HybridizationType.SP,
86
+ Chem.rdchem.HybridizationType.SP2,
87
+ Chem.rdchem.HybridizationType.SP3,
88
+ Chem.rdchem.HybridizationType.SP3D,
89
+ Chem.rdchem.HybridizationType.SP3D2,
90
+ "other",
91
+ ],
92
+ )
93
+ +
94
+ # Aromaticity (Boolean)
95
+ [atom.GetIsAromatic()]
96
+ )
97
+
98
+ def get_protein_features(char):
99
+ prot_vocab= {
100
+ 'A': 1, 'R': 2, 'N': 3, 'D': 4, 'C': 5, 'Q': 6, 'E': 7, 'G': 8, 'H': 9,
101
+ 'I': 10, 'L': 11, 'K': 12, 'M': 13, 'F': 14, 'P': 15, 'S': 16, 'T': 17,
102
+ 'W': 18, 'Y': 19, 'V': 20, 'X': 21, 'Z': 21, 'B': 21,
103
+ 'PAD': 0, 'UNK': 21
104
+ }
105
+ return prot_vocab.get(char, prot_vocab['UNK'])
106
+
107
+
108
+ class BindingDataset(Dataset):
109
+ def __init__(self, dataframe, max_seq_length=1000):
110
+ self.data = dataframe
111
+ self.max_seq_length = max_seq_length # Define a maximum sequence length for padding/truncation
112
+
113
+ def __len__(self):
114
+ return len(self.data)
115
+
116
+ def __getitem__(self, idx):
117
+ row = self.data.iloc[idx]
118
+ smiles = row["smiles"]
119
+ sequence = row["sequence"]
120
+ affinity = row["affinity"]
121
+
122
+ mol = Chem.MolFromSmiles(smiles)
123
+ if mol is None:
124
+ return None
125
+
126
+ # Ligand (Graph)
127
+ # Nodes
128
+ atom_features = [get_atom_features(atom) for atom in mol.GetAtoms()]
129
+ x = torch.tensor(np.array(atom_features), dtype=torch.float)
130
+
131
+ # Edges
132
+ edge_indexes = []
133
+ for bond in mol.GetBonds():
134
+ i = bond.GetBeginAtomIdx()
135
+ j = bond.GetEndAtomIdx()
136
+ edge_indexes.append((i, j))
137
+ edge_indexes.append((j, i))
138
+
139
+ # t - transpose, [num_of_edges, 2] -> [2, num_of_edges]
140
+ # contiguous - take the virtually transposed tensor and make its physical copy and lay bytes sequentially
141
+
142
+ edge_index = torch.tensor(edge_indexes, dtype=torch.long).t().contiguous()
143
+
144
+ # Protein (Sequence, tensor of integers)
145
+ tokens = [get_protein_features(char) for char in sequence]
146
+ if len(tokens) > self.max_seq_length:
147
+ tokens = tokens[:self.max_seq_length]
148
+ else:
149
+ tokens.extend([get_protein_features("PAD")] * (self.max_seq_length - len(tokens)))
150
+ protein_tensor = torch.tensor(tokens, dtype=torch.long)
151
+
152
+ # Affinity
153
+ y = torch.tensor([affinity], dtype=torch.float)
154
+ return Data(x=x, edge_index=edge_index, protein_seq=protein_tensor, y=y)
155
+
156
+
157
+ if __name__ == "__main__":
158
+ dataset = pd.read_csv("pdbbind_refined_dataset.csv")
159
+ dataset = BindingDataset(dataset)
160
+
161
+ train_size = int(0.8 * len(dataset))
162
+ test_size = len(dataset) - train_size
163
+ train_dataset, test_dataset = random_split(dataset, [train_size, test_size])
164
+
165
+ print(len(train_dataset))
166
+ print(len(test_dataset))
167
+
model.py ADDED
@@ -0,0 +1,116 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import math
2
+
3
+ import torch
4
+ import torch.nn as nn
5
+
6
+
7
+ from torch_geometric.nn import GCNConv, global_mean_pool
8
+
9
+ class PositionalEncoding(nn.Module):
10
+ def __init__(self, d_model: int, seq_len: int = 5000, dropout: float = 0.1):
11
+ super().__init__()
12
+ self.d_model = d_model
13
+ self.seq_len = seq_len
14
+ self.dropout = nn.Dropout(dropout)
15
+
16
+ # Create a matrix of shape (seq_len, d_model)
17
+ pe = torch.zeros(seq_len, d_model)
18
+
19
+ # Create a vector of shape (seq_len, 1)
20
+ position = torch.arange(0, seq_len, dtype=torch.float).unsqueeze(
21
+ 1
22
+ ) # (Seq_len, 1)
23
+ # Compute the positional encodings once in log space.
24
+ div_term = torch.exp(
25
+ torch.arange(0, d_model, 2).float() * (-math.log(10000.0) / d_model)
26
+ )
27
+ # Apply the sin to even positions
28
+ pe[:, 0::2] = torch.sin(position * div_term)
29
+ # Apply the cos to odd positions
30
+ pe[:, 1::2] = torch.cos(position * div_term)
31
+
32
+ pe = pe.unsqueeze(0) # (1, Seq_len, d_model) batch dimension
33
+ self.register_buffer("pe", pe)
34
+
35
+ def forward(self, x):
36
+ # x: [batch_size, seq_len, d_model]
37
+ x = x + (self.pe[:, :x.shape[1], :]).requires_grad_(False)
38
+ return self.dropout(x)
39
+
40
+
41
+
42
+ class LigandGNN(nn.Module):
43
+ def __init__(self, input_dim, hidden_channels):
44
+ super().__init__()
45
+ self.hidden_channels = hidden_channels
46
+
47
+ self.conv1 = GCNConv(input_dim, hidden_channels)
48
+ self.conv2 = GCNConv(hidden_channels, hidden_channels)
49
+ self.conv3 = GCNConv(hidden_channels, hidden_channels)
50
+ self.dropout = nn.Dropout(0.2)
51
+
52
+ def forward(self, x, edge_index, batch):
53
+ x = self.conv1(x, edge_index)
54
+ x = x.relu()
55
+ x = self.dropout(x)
56
+
57
+ x = self.conv2(x, edge_index)
58
+ x = x.relu()
59
+ x = self.conv3(x, edge_index)
60
+ x = self.dropout(x)
61
+
62
+ # Averaging nodes and got the molecula vector
63
+ x = global_mean_pool(x, batch) # [batch_size, hidden_channels]
64
+ return x
65
+
66
+ class ProteinTransformer(nn.Module):
67
+ def __init__(self, vocab_size, d_model=128, N=2, h=4, output_dim=128):
68
+ super().__init__()
69
+ self.d_model = d_model
70
+ self.embedding = nn.Embedding(vocab_size, d_model)
71
+ self.pos_encoder = PositionalEncoding(d_model)
72
+ encoder_layer = nn.TransformerEncoderLayer(d_model=d_model, nhead=h, batch_first=True)
73
+ self.transformer = nn.TransformerEncoder(encoder_layer, num_layers=N)
74
+
75
+ self.fc = nn.Linear(d_model, output_dim)
76
+
77
+ def forward(self, x):
78
+ # x: [batch_size, seq_len]
79
+ padding_mask = (x == 0) # mask for PAD tokens
80
+ x = self.embedding(x) * math.sqrt(self.d_model)
81
+ x = self.pos_encoder(x)
82
+ x = self.transformer(x, src_key_padding_mask=padding_mask)
83
+
84
+ mask = (~padding_mask).float().unsqueeze(-1)
85
+ x = x * mask
86
+
87
+ sum_x = x.sum(dim=1) # Global average pooling
88
+ token_counts = mask.sum(dim=1).clamp(min=1e-9)
89
+ x = sum_x / token_counts
90
+ x = self.fc(x)
91
+ return x
92
+
93
+ class BindingAffinityModel(nn.Module):
94
+ def __init__(self, num_node_features, hidden_channels_gnn):
95
+ super().__init__()
96
+ # Tower 1 - Ligand GNN
97
+ self.ligand_gnn = LigandGNN(input_dim=num_node_features, hidden_channels=hidden_channels_gnn)
98
+ # Tower 2 - Protein Transformer
99
+ self.protein_transformer = ProteinTransformer(vocab_size=26)
100
+
101
+ self.head = nn.Sequential(
102
+ nn.Linear(128 + 128, 256),
103
+ nn.ReLU(),
104
+ nn.Dropout(0.2),
105
+ nn.Linear(256, 1),
106
+ )
107
+ def forward(self, x, edge_index, batch, protein_seq):
108
+ ligand_vec = self.ligand_gnn(x, edge_index, batch)
109
+ batch_size = batch.max().item() + 1
110
+ protein_seq = protein_seq.view(batch_size, -1)
111
+
112
+ protein_vec = self.protein_transformer(protein_seq)
113
+ combined = torch.cat([ligand_vec, protein_vec], dim=1)
114
+ return self.head(combined)
115
+
116
+
train.py ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torch.nn as nn
3
+ import pandas as pd
4
+ from torch.utils.data import random_split
5
+ from torch_geometric.loader import DataLoader
6
+ from dataset import BindingDataset
7
+ from model import BindingAffinityModel
8
+ from tqdm import tqdm
9
+
10
+ DEVICE = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
11
+
12
+
13
+ def train_epoch(model, loader, optimizer, criterion):
14
+ model.train()
15
+ total_loss = 0
16
+ for batch in tqdm(loader, desc="Training"):
17
+ batch = batch.to(DEVICE)
18
+ optimizer.zero_grad()
19
+
20
+ out = model(batch.x, batch.edge_index, batch.batch, batch.protein_seq)
21
+ loss = criterion(out.squeeze(), batch.y.squeeze())
22
+
23
+ loss.backward()
24
+ optimizer.step()
25
+ total_loss += loss.item()
26
+ return total_loss / len(loader)
27
+
28
+ def evaluate(model, loader, criterion):
29
+ model.eval()
30
+ total_loss = 0
31
+ with torch.no_grad():
32
+ for batch in loader:
33
+ batch = batch.to(DEVICE)
34
+ out = model(batch.x, batch.edge_index, batch.batch, batch.protein_seq)
35
+ loss = criterion(out.squeeze(), batch.y.squeeze())
36
+ total_loss += loss.item()
37
+ return total_loss / len(loader)
38
+
39
+ def main():
40
+ # Load dataset
41
+ dataframe = pd.read_csv('pdbbind_refined_dataset.csv')
42
+ dataframe.dropna(inplace=True)
43
+ print("Dataset loaded with {} samples".format(len(dataframe)))
44
+ dataset = BindingDataset(dataframe)
45
+ print("Dataset transformed with {} samples".format(len(dataset)))
46
+
47
+
48
+ train_size = int(0.8 * len(dataset))
49
+ test_size = len(dataset) - train_size
50
+ train_dataset, test_dataset = random_split(dataset, [train_size, test_size])
51
+
52
+ train_loader = DataLoader(train_dataset, batch_size=32, shuffle=True)
53
+ test_loader = DataLoader(test_dataset, batch_size=32, shuffle=False)
54
+ num_features = train_dataset[0].x.shape[1]
55
+ print("Number of node features:", num_features)
56
+
57
+ model = BindingAffinityModel(num_node_features=num_features, hidden_channels_gnn=128).to(DEVICE)
58
+ optimizer = torch.optim.Adam(model.parameters(), lr=0.0005, weight_decay=1e-4)
59
+ criterion = nn.MSELoss()
60
+
61
+ num_epochs = 20
62
+ for epoch in range(num_epochs):
63
+ train_loss = train_epoch(model, train_loader, optimizer, criterion)
64
+ test_loss = evaluate(model, test_loader, criterion)
65
+ print(f'Epoch {epoch+1}, Train Loss: {train_loss:.4f}, Test Loss: {test_loss:.4f}')
66
+ torch.save(model.state_dict(), './model.pth')
67
+
68
+
69
+ if __name__ == "__main__":
70
+ main()