File size: 30,962 Bytes
00a075b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "a1af2321-8860-4a3e-8406-a9ae587b97bf",
   "metadata": {},
   "outputs": [],
   "source": [
    "from transformers import AutoModelForCausalLM, AutoTokenizer\n",
    "import selfies as sf\n",
    "from rdkit import Chem\n",
    "from typing import Optional\n",
    "import numpy as np\n",
    "import py3Dmol\n",
    "from rdkit import Chem, DataStructs\n",
    "from rdkit.Chem import AllChem\n",
    "import torch\n",
    "\n",
    "def smiles_to_3d(smiles_list, width=400, height=300):\n",
    "    # Visualize the 3D structure using py3Dmol\n",
    "    view = py3Dmol.view(width=width, height=height)\n",
    "    for smiles in smiles_list:\n",
    "        # Generate the RDKit molecule object\n",
    "        mol = Chem.MolFromSmiles(smiles)\n",
    "        if mol is None:\n",
    "            raise ValueError(\"Invalid SMILES string\")\n",
    "\n",
    "        # Add hydrogens to the molecule\n",
    "        mol = Chem.AddHs(mol)\n",
    "\n",
    "        # Generate 3D coordinates\n",
    "        AllChem.EmbedMolecule(mol, randomSeed=42)\n",
    "        AllChem.UFFOptimizeMolecule(mol)\n",
    "\n",
    "        # Generate the 3D structure in the form of a pdb string\n",
    "        pdb = Chem.MolToPDBBlock(mol)\n",
    "        view.addModel(pdb, 'pdb')\n",
    "        view.setStyle({'stick': {}})\n",
    "        view.zoomTo()\n",
    "    return view\n",
    "\n",
    "    \n",
    "# Load the checkpoint and the tokenizer\n",
    "checkpoint_path = \"lamthuy/SelfiesGen\"\n",
    "model = AutoModelForCausalLM.from_pretrained(checkpoint_path)\n",
    "tokenizer = AutoTokenizer.from_pretrained(checkpoint_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "9b7066a4-6637-4d45-a0d9-3cc5e2ca0409",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/3dmoljs_load.v0": "<div id=\"3dmolviewer_1753263966683991\"  style=\"position: relative; width: 400px; height: 300px;\">\n        <p id=\"3dmolwarning_1753263966683991\" style=\"background-color:#ffcccc;color:black\">3Dmol.js failed to load for some reason.  Please check your browser console for error messages.<br></p>\n        </div>\n<script>\n\nvar loadScriptAsync = function(uri){\n  return new Promise((resolve, reject) => {\n    //this is to ignore the existence of requirejs amd\n    var savedexports, savedmodule;\n    if (typeof exports !== 'undefined') savedexports = exports;\n    else exports = {}\n    if (typeof module !== 'undefined') savedmodule = module;\n    else module = {}\n\n    var tag = document.createElement('script');\n    tag.src = uri;\n    tag.async = true;\n    tag.onload = () => {\n        exports = savedexports;\n        module = savedmodule;\n        resolve();\n    };\n  var firstScriptTag = document.getElementsByTagName('script')[0];\n  firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);\n});\n};\n\nif(typeof $3Dmolpromise === 'undefined') {\n$3Dmolpromise = null;\n  $3Dmolpromise = loadScriptAsync('https://cdnjs.cloudflare.com/ajax/libs/3Dmol/2.4.2/3Dmol-min.js');\n}\n\nvar viewer_1753263966683991 = null;\nvar warn = document.getElementById(\"3dmolwarning_1753263966683991\");\nif(warn) {\n    warn.parentNode.removeChild(warn);\n}\n$3Dmolpromise.then(function() {\nviewer_1753263966683991 = $3Dmol.createViewer(document.getElementById(\"3dmolviewer_1753263966683991\"),{backgroundColor:\"white\"});\nviewer_1753263966683991.zoomTo();\n\tviewer_1753263966683991.addModel(\"HETATM    1  C1  UNL     1      -2.975  -2.217   0.190  1.00  0.00           C  \\nHETATM    2  C2  UNL     1      -2.096  -1.150  -0.368  1.00  0.00           C  \\nHETATM    3  O1  UNL     1      -2.392  -0.615  -1.471  1.00  0.00           O  \\nHETATM    4  O2  UNL     1      -0.898  -0.839   0.288  1.00  0.00           O  \\nHETATM    5  C3  UNL     1      -0.095   0.284   0.025  1.00  0.00           C  \\nHETATM    6  C4  UNL     1      -0.671   1.501  -0.391  1.00  0.00           C  \\nHETATM    7  C5  UNL     1       0.129   2.617  -0.638  1.00  0.00           C  \\nHETATM    8  C6  UNL     1       1.509   2.540  -0.463  1.00  0.00           C  \\nHETATM    9  C7  UNL     1       2.094   1.348  -0.032  1.00  0.00           C  \\nHETATM   10  C8  UNL     1       1.307   0.209   0.223  1.00  0.00           C  \\nHETATM   11  C9  UNL     1       1.972  -1.038   0.682  1.00  0.00           C  \\nHETATM   12  O3  UNL     1       1.307  -2.087   0.898  1.00  0.00           O  \\nHETATM   13  O4  UNL     1       3.351  -1.062   0.878  1.00  0.00           O  \\nHETATM   14  H1  UNL     1      -4.038  -1.995  -0.040  1.00  0.00           H  \\nHETATM   15  H2  UNL     1      -2.850  -2.271   1.291  1.00  0.00           H  \\nHETATM   16  H3  UNL     1      -2.699  -3.195  -0.257  1.00  0.00           H  \\nHETATM   17  H4  UNL     1      -1.742   1.599  -0.498  1.00  0.00           H  \\nHETATM   18  H5  UNL     1      -0.324   3.547  -0.957  1.00  0.00           H  \\nHETATM   19  H6  UNL     1       2.126   3.408  -0.653  1.00  0.00           H  \\nHETATM   20  H7  UNL     1       3.168   1.322   0.102  1.00  0.00           H  \\nHETATM   21  H8  UNL     1       3.818  -1.905   1.190  1.00  0.00           H  \\nCONECT    1    2   14   15   16\\nCONECT    2    3    3    4\\nCONECT    4    5\\nCONECT    5    6    6   10\\nCONECT    6    7   17\\nCONECT    7    8    8   18\\nCONECT    8    9   19\\nCONECT    9   10   10   20\\nCONECT   10   11\\nCONECT   11   12   12   13\\nCONECT   13   21\\nEND\\n\",\"pdb\");\n\tviewer_1753263966683991.setStyle({\"stick\": {}});\n\tviewer_1753263966683991.zoomTo();\nviewer_1753263966683991.render();\n});\n</script>",
      "text/html": [
       "<div id=\"3dmolviewer_1753263966683991\"  style=\"position: relative; width: 400px; height: 300px;\">\n",
       "        <p id=\"3dmolwarning_1753263966683991\" style=\"background-color:#ffcccc;color:black\">3Dmol.js failed to load for some reason.  Please check your browser console for error messages.<br></p>\n",
       "        </div>\n",
       "<script>\n",
       "\n",
       "var loadScriptAsync = function(uri){\n",
       "  return new Promise((resolve, reject) => {\n",
       "    //this is to ignore the existence of requirejs amd\n",
       "    var savedexports, savedmodule;\n",
       "    if (typeof exports !== 'undefined') savedexports = exports;\n",
       "    else exports = {}\n",
       "    if (typeof module !== 'undefined') savedmodule = module;\n",
       "    else module = {}\n",
       "\n",
       "    var tag = document.createElement('script');\n",
       "    tag.src = uri;\n",
       "    tag.async = true;\n",
       "    tag.onload = () => {\n",
       "        exports = savedexports;\n",
       "        module = savedmodule;\n",
       "        resolve();\n",
       "    };\n",
       "  var firstScriptTag = document.getElementsByTagName('script')[0];\n",
       "  firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);\n",
       "});\n",
       "};\n",
       "\n",
       "if(typeof $3Dmolpromise === 'undefined') {\n",
       "$3Dmolpromise = null;\n",
       "  $3Dmolpromise = loadScriptAsync('https://cdnjs.cloudflare.com/ajax/libs/3Dmol/2.4.2/3Dmol-min.js');\n",
       "}\n",
       "\n",
       "var viewer_1753263966683991 = null;\n",
       "var warn = document.getElementById(\"3dmolwarning_1753263966683991\");\n",
       "if(warn) {\n",
       "    warn.parentNode.removeChild(warn);\n",
       "}\n",
       "$3Dmolpromise.then(function() {\n",
       "viewer_1753263966683991 = $3Dmol.createViewer(document.getElementById(\"3dmolviewer_1753263966683991\"),{backgroundColor:\"white\"});\n",
       "viewer_1753263966683991.zoomTo();\n",
       "\tviewer_1753263966683991.addModel(\"HETATM    1  C1  UNL     1      -2.975  -2.217   0.190  1.00  0.00           C  \\nHETATM    2  C2  UNL     1      -2.096  -1.150  -0.368  1.00  0.00           C  \\nHETATM    3  O1  UNL     1      -2.392  -0.615  -1.471  1.00  0.00           O  \\nHETATM    4  O2  UNL     1      -0.898  -0.839   0.288  1.00  0.00           O  \\nHETATM    5  C3  UNL     1      -0.095   0.284   0.025  1.00  0.00           C  \\nHETATM    6  C4  UNL     1      -0.671   1.501  -0.391  1.00  0.00           C  \\nHETATM    7  C5  UNL     1       0.129   2.617  -0.638  1.00  0.00           C  \\nHETATM    8  C6  UNL     1       1.509   2.540  -0.463  1.00  0.00           C  \\nHETATM    9  C7  UNL     1       2.094   1.348  -0.032  1.00  0.00           C  \\nHETATM   10  C8  UNL     1       1.307   0.209   0.223  1.00  0.00           C  \\nHETATM   11  C9  UNL     1       1.972  -1.038   0.682  1.00  0.00           C  \\nHETATM   12  O3  UNL     1       1.307  -2.087   0.898  1.00  0.00           O  \\nHETATM   13  O4  UNL     1       3.351  -1.062   0.878  1.00  0.00           O  \\nHETATM   14  H1  UNL     1      -4.038  -1.995  -0.040  1.00  0.00           H  \\nHETATM   15  H2  UNL     1      -2.850  -2.271   1.291  1.00  0.00           H  \\nHETATM   16  H3  UNL     1      -2.699  -3.195  -0.257  1.00  0.00           H  \\nHETATM   17  H4  UNL     1      -1.742   1.599  -0.498  1.00  0.00           H  \\nHETATM   18  H5  UNL     1      -0.324   3.547  -0.957  1.00  0.00           H  \\nHETATM   19  H6  UNL     1       2.126   3.408  -0.653  1.00  0.00           H  \\nHETATM   20  H7  UNL     1       3.168   1.322   0.102  1.00  0.00           H  \\nHETATM   21  H8  UNL     1       3.818  -1.905   1.190  1.00  0.00           H  \\nCONECT    1    2   14   15   16\\nCONECT    2    3    3    4\\nCONECT    4    5\\nCONECT    5    6    6   10\\nCONECT    6    7   17\\nCONECT    7    8    8   18\\nCONECT    8    9   19\\nCONECT    9   10   10   20\\nCONECT   10   11\\nCONECT   11   12   12   13\\nCONECT   13   21\\nEND\\n\",\"pdb\");\n",
       "\tviewer_1753263966683991.setStyle({\"stick\": {}});\n",
       "\tviewer_1753263966683991.zoomTo();\n",
       "viewer_1753263966683991.render();\n",
       "});\n",
       "</script>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "<py3Dmol.view at 0x2afd09c40>"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Given a SMILES, get its fingerpint\n",
    "smiles = \"CC(=O)OC1=CC=CC=C1C(=O)O\"\n",
    "smiles_to_3d([smiles])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "05f9bf21-c998-4d63-870e-c1033ff91b31",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "The attention mask and the pad token id were not set. As a consequence, you may observe unexpected behavior. Please pass your input's `attention_mask` to obtain reliable results.\n",
      "Setting `pad_token_id` to `eos_token_id`:50256 for open-end generation.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[C][C][=Branch1][C][=O][O][C][=C][C][=C][C][=C][Ring1][=Branch1][C][=Branch1][C][=S][O][SEP]\n",
      "[C][C][=Branch1][C][=O][O][C][=C][C][=C][C][=C][Ring1][=Branch1][C][=Branch1][C][=NH2+1][O]\n",
      "CC(=O)OC1=CC=CC=C1C(=[NH2+1])O\n"
     ]
    },
    {
     "data": {
      "application/3dmoljs_load.v0": "<div id=\"3dmolviewer_1753264053659028\"  style=\"position: relative; width: 400px; height: 300px;\">\n        <p id=\"3dmolwarning_1753264053659028\" style=\"background-color:#ffcccc;color:black\">3Dmol.js failed to load for some reason.  Please check your browser console for error messages.<br></p>\n        </div>\n<script>\n\nvar loadScriptAsync = function(uri){\n  return new Promise((resolve, reject) => {\n    //this is to ignore the existence of requirejs amd\n    var savedexports, savedmodule;\n    if (typeof exports !== 'undefined') savedexports = exports;\n    else exports = {}\n    if (typeof module !== 'undefined') savedmodule = module;\n    else module = {}\n\n    var tag = document.createElement('script');\n    tag.src = uri;\n    tag.async = true;\n    tag.onload = () => {\n        exports = savedexports;\n        module = savedmodule;\n        resolve();\n    };\n  var firstScriptTag = document.getElementsByTagName('script')[0];\n  firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);\n});\n};\n\nif(typeof $3Dmolpromise === 'undefined') {\n$3Dmolpromise = null;\n  $3Dmolpromise = loadScriptAsync('https://cdnjs.cloudflare.com/ajax/libs/3Dmol/2.4.2/3Dmol-min.js');\n}\n\nvar viewer_1753264053659028 = null;\nvar warn = document.getElementById(\"3dmolwarning_1753264053659028\");\nif(warn) {\n    warn.parentNode.removeChild(warn);\n}\n$3Dmolpromise.then(function() {\nviewer_1753264053659028 = $3Dmol.createViewer(document.getElementById(\"3dmolviewer_1753264053659028\"),{backgroundColor:\"white\"});\nviewer_1753264053659028.zoomTo();\n\tviewer_1753264053659028.addModel(\"HETATM    1  C1  UNL     1       2.411  -2.582  -1.327  1.00  0.00           C  \\nHETATM    2  C2  UNL     1       1.777  -1.233  -1.311  1.00  0.00           C  \\nHETATM    3  O1  UNL     1       1.971  -0.444  -2.275  1.00  0.00           O  \\nHETATM    4  O2  UNL     1       0.887  -0.908  -0.278  1.00  0.00           O  \\nHETATM    5  C3  UNL     1       0.406   0.380   0.005  1.00  0.00           C  \\nHETATM    6  C4  UNL     1       1.187   1.525  -0.250  1.00  0.00           C  \\nHETATM    7  C5  UNL     1       0.691   2.798   0.033  1.00  0.00           C  \\nHETATM    8  C6  UNL     1      -0.580   2.947   0.586  1.00  0.00           C  \\nHETATM    9  C7  UNL     1      -1.357   1.822   0.866  1.00  0.00           C  \\nHETATM   10  C8  UNL     1      -0.876   0.529   0.582  1.00  0.00           C  \\nHETATM   11  C9  UNL     1      -1.702  -0.662   0.905  1.00  0.00           C  \\nHETATM   12  N1  UNL     1      -2.996  -0.640   0.776  1.00  0.00           N1+\\nHETATM   13  O3  UNL     1      -1.073  -1.826   1.349  1.00  0.00           O  \\nHETATM   14  H1  UNL     1       1.780  -3.281  -1.914  1.00  0.00           H  \\nHETATM   15  H2  UNL     1       2.512  -2.964  -0.290  1.00  0.00           H  \\nHETATM   16  H3  UNL     1       3.421  -2.524  -1.787  1.00  0.00           H  \\nHETATM   17  H4  UNL     1       2.190   1.435  -0.643  1.00  0.00           H  \\nHETATM   18  H5  UNL     1       1.299   3.672  -0.166  1.00  0.00           H  \\nHETATM   19  H6  UNL     1      -0.957   3.936   0.813  1.00  0.00           H  \\nHETATM   20  H7  UNL     1      -2.325   1.965   1.329  1.00  0.00           H  \\nHETATM   21  H8  UNL     1      -3.567  -1.482   1.010  1.00  0.00           H  \\nHETATM   22  H9  UNL     1      -3.499   0.198   0.408  1.00  0.00           H  \\nHETATM   23  H10 UNL     1      -1.599  -2.662   1.576  1.00  0.00           H  \\nCONECT    1    2   14   15   16\\nCONECT    2    3    3    4\\nCONECT    4    5\\nCONECT    5    6    6   10\\nCONECT    6    7   17\\nCONECT    7    8    8   18\\nCONECT    8    9   19\\nCONECT    9   10   10   20\\nCONECT   10   11\\nCONECT   11   12   12   13\\nCONECT   12   21   22\\nCONECT   13   23\\nEND\\n\",\"pdb\");\n\tviewer_1753264053659028.setStyle({\"stick\": {}});\n\tviewer_1753264053659028.zoomTo();\nviewer_1753264053659028.render();\n});\n</script>",
      "text/html": [
       "<div id=\"3dmolviewer_1753264053659028\"  style=\"position: relative; width: 400px; height: 300px;\">\n",
       "        <p id=\"3dmolwarning_1753264053659028\" style=\"background-color:#ffcccc;color:black\">3Dmol.js failed to load for some reason.  Please check your browser console for error messages.<br></p>\n",
       "        </div>\n",
       "<script>\n",
       "\n",
       "var loadScriptAsync = function(uri){\n",
       "  return new Promise((resolve, reject) => {\n",
       "    //this is to ignore the existence of requirejs amd\n",
       "    var savedexports, savedmodule;\n",
       "    if (typeof exports !== 'undefined') savedexports = exports;\n",
       "    else exports = {}\n",
       "    if (typeof module !== 'undefined') savedmodule = module;\n",
       "    else module = {}\n",
       "\n",
       "    var tag = document.createElement('script');\n",
       "    tag.src = uri;\n",
       "    tag.async = true;\n",
       "    tag.onload = () => {\n",
       "        exports = savedexports;\n",
       "        module = savedmodule;\n",
       "        resolve();\n",
       "    };\n",
       "  var firstScriptTag = document.getElementsByTagName('script')[0];\n",
       "  firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);\n",
       "});\n",
       "};\n",
       "\n",
       "if(typeof $3Dmolpromise === 'undefined') {\n",
       "$3Dmolpromise = null;\n",
       "  $3Dmolpromise = loadScriptAsync('https://cdnjs.cloudflare.com/ajax/libs/3Dmol/2.4.2/3Dmol-min.js');\n",
       "}\n",
       "\n",
       "var viewer_1753264053659028 = null;\n",
       "var warn = document.getElementById(\"3dmolwarning_1753264053659028\");\n",
       "if(warn) {\n",
       "    warn.parentNode.removeChild(warn);\n",
       "}\n",
       "$3Dmolpromise.then(function() {\n",
       "viewer_1753264053659028 = $3Dmol.createViewer(document.getElementById(\"3dmolviewer_1753264053659028\"),{backgroundColor:\"white\"});\n",
       "viewer_1753264053659028.zoomTo();\n",
       "\tviewer_1753264053659028.addModel(\"HETATM    1  C1  UNL     1       2.411  -2.582  -1.327  1.00  0.00           C  \\nHETATM    2  C2  UNL     1       1.777  -1.233  -1.311  1.00  0.00           C  \\nHETATM    3  O1  UNL     1       1.971  -0.444  -2.275  1.00  0.00           O  \\nHETATM    4  O2  UNL     1       0.887  -0.908  -0.278  1.00  0.00           O  \\nHETATM    5  C3  UNL     1       0.406   0.380   0.005  1.00  0.00           C  \\nHETATM    6  C4  UNL     1       1.187   1.525  -0.250  1.00  0.00           C  \\nHETATM    7  C5  UNL     1       0.691   2.798   0.033  1.00  0.00           C  \\nHETATM    8  C6  UNL     1      -0.580   2.947   0.586  1.00  0.00           C  \\nHETATM    9  C7  UNL     1      -1.357   1.822   0.866  1.00  0.00           C  \\nHETATM   10  C8  UNL     1      -0.876   0.529   0.582  1.00  0.00           C  \\nHETATM   11  C9  UNL     1      -1.702  -0.662   0.905  1.00  0.00           C  \\nHETATM   12  N1  UNL     1      -2.996  -0.640   0.776  1.00  0.00           N1+\\nHETATM   13  O3  UNL     1      -1.073  -1.826   1.349  1.00  0.00           O  \\nHETATM   14  H1  UNL     1       1.780  -3.281  -1.914  1.00  0.00           H  \\nHETATM   15  H2  UNL     1       2.512  -2.964  -0.290  1.00  0.00           H  \\nHETATM   16  H3  UNL     1       3.421  -2.524  -1.787  1.00  0.00           H  \\nHETATM   17  H4  UNL     1       2.190   1.435  -0.643  1.00  0.00           H  \\nHETATM   18  H5  UNL     1       1.299   3.672  -0.166  1.00  0.00           H  \\nHETATM   19  H6  UNL     1      -0.957   3.936   0.813  1.00  0.00           H  \\nHETATM   20  H7  UNL     1      -2.325   1.965   1.329  1.00  0.00           H  \\nHETATM   21  H8  UNL     1      -3.567  -1.482   1.010  1.00  0.00           H  \\nHETATM   22  H9  UNL     1      -3.499   0.198   0.408  1.00  0.00           H  \\nHETATM   23  H10 UNL     1      -1.599  -2.662   1.576  1.00  0.00           H  \\nCONECT    1    2   14   15   16\\nCONECT    2    3    3    4\\nCONECT    4    5\\nCONECT    5    6    6   10\\nCONECT    6    7   17\\nCONECT    7    8    8   18\\nCONECT    8    9   19\\nCONECT    9   10   10   20\\nCONECT   10   11\\nCONECT   11   12   12   13\\nCONECT   12   21   22\\nCONECT   13   23\\nEND\\n\",\"pdb\");\n",
       "\tviewer_1753264053659028.setStyle({\"stick\": {}});\n",
       "\tviewer_1753264053659028.zoomTo();\n",
       "viewer_1753264053659028.render();\n",
       "});\n",
       "</script>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "<py3Dmol.view at 0x2afbaf020>"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = sf.encoder(smiles)\n",
    "s = s + \"[SEP]\"\n",
    "print(s)\n",
    "input_ids = tokenizer.encode(s, return_tensors=\"pt\")\n",
    "n = input_ids.size(1)\n",
    "# Generate output sequence\n",
    "output_ids = model.generate(input_ids, max_length=128, num_beams=5, num_return_sequences=5,\n",
    "                            early_stopping=True)\n",
    "output = tokenizer.decode(output_ids[1][n:], skip_special_tokens=True)\n",
    "print(output)\n",
    "smiles = sf.decoder(output)\n",
    "print(smiles)\n",
    "smiles_to_3d([smiles])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "dbe6cebd-c7d5-4da9-aac0-114f232cf147",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "The attention mask and the pad token id were not set. As a consequence, you may observe unexpected behavior. Please pass your input's `attention_mask` to obtain reliable results.\n",
      "Setting `pad_token_id` to `eos_token_id`:50256 for open-end generation.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[C][C][=Branch1][C][=O][N][C][=C][C][=N][C][=C][Ring1][=Branch1][C][=Branch1][C][=S][O-1]\n",
      "CC(=O)NC1=CC=NC=C1C(=S)[O-1]\n"
     ]
    },
    {
     "data": {
      "application/3dmoljs_load.v0": "<div id=\"3dmolviewer_17532640697518232\"  style=\"position: relative; width: 400px; height: 300px;\">\n        <p id=\"3dmolwarning_17532640697518232\" style=\"background-color:#ffcccc;color:black\">3Dmol.js failed to load for some reason.  Please check your browser console for error messages.<br></p>\n        </div>\n<script>\n\nvar loadScriptAsync = function(uri){\n  return new Promise((resolve, reject) => {\n    //this is to ignore the existence of requirejs amd\n    var savedexports, savedmodule;\n    if (typeof exports !== 'undefined') savedexports = exports;\n    else exports = {}\n    if (typeof module !== 'undefined') savedmodule = module;\n    else module = {}\n\n    var tag = document.createElement('script');\n    tag.src = uri;\n    tag.async = true;\n    tag.onload = () => {\n        exports = savedexports;\n        module = savedmodule;\n        resolve();\n    };\n  var firstScriptTag = document.getElementsByTagName('script')[0];\n  firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);\n});\n};\n\nif(typeof $3Dmolpromise === 'undefined') {\n$3Dmolpromise = null;\n  $3Dmolpromise = loadScriptAsync('https://cdnjs.cloudflare.com/ajax/libs/3Dmol/2.4.2/3Dmol-min.js');\n}\n\nvar viewer_17532640697518232 = null;\nvar warn = document.getElementById(\"3dmolwarning_17532640697518232\");\nif(warn) {\n    warn.parentNode.removeChild(warn);\n}\n$3Dmolpromise.then(function() {\nviewer_17532640697518232 = $3Dmol.createViewer(document.getElementById(\"3dmolviewer_17532640697518232\"),{backgroundColor:\"white\"});\nviewer_17532640697518232.zoomTo();\n\tviewer_17532640697518232.addModel(\"HETATM    1  C1  UNL     1       3.341   0.506  -0.349  1.00  0.00           C  \\nHETATM    2  C2  UNL     1       1.994  -0.109  -0.554  1.00  0.00           C  \\nHETATM    3  O1  UNL     1       1.839  -0.974  -1.458  1.00  0.00           O  \\nHETATM    4  N1  UNL     1       0.876   0.346   0.221  1.00  0.00           N  \\nHETATM    5  C3  UNL     1      -0.424  -0.266   0.167  1.00  0.00           C  \\nHETATM    6  C4  UNL     1      -0.537  -1.665   0.219  1.00  0.00           C  \\nHETATM    7  C5  UNL     1      -1.798  -2.258   0.278  1.00  0.00           C  \\nHETATM    8  N2  UNL     1      -2.913  -1.485   0.302  1.00  0.00           N  \\nHETATM    9  C6  UNL     1      -2.844  -0.130   0.260  1.00  0.00           C  \\nHETATM   10  C7  UNL     1      -1.602   0.521   0.177  1.00  0.00           C  \\nHETATM   11  C8  UNL     1      -1.585   1.998   0.085  1.00  0.00           C  \\nHETATM   12  S1  UNL     1      -0.600   2.783  -0.963  1.00  0.00           S  \\nHETATM   13  O2  UNL     1      -2.485   2.742   0.838  1.00  0.00           O1-\\nHETATM   14  H1  UNL     1       3.531   0.642   0.736  1.00  0.00           H  \\nHETATM   15  H2  UNL     1       4.133  -0.148  -0.770  1.00  0.00           H  \\nHETATM   16  H3  UNL     1       3.376   1.493  -0.854  1.00  0.00           H  \\nHETATM   17  H4  UNL     1       1.005   1.188   0.826  1.00  0.00           H  \\nHETATM   18  H5  UNL     1       0.344  -2.292   0.249  1.00  0.00           H  \\nHETATM   19  H6  UNL     1      -1.889  -3.335   0.323  1.00  0.00           H  \\nHETATM   20  H7  UNL     1      -3.764   0.442   0.270  1.00  0.00           H  \\nCONECT    1    2   14   15   16\\nCONECT    2    3    3    4\\nCONECT    4    5   17\\nCONECT    5    6    6   10\\nCONECT    6    7   18\\nCONECT    7    8    8   19\\nCONECT    8    9\\nCONECT    9   10   10   20\\nCONECT   10   11\\nCONECT   11   12   12   13\\nEND\\n\",\"pdb\");\n\tviewer_17532640697518232.setStyle({\"stick\": {}});\n\tviewer_17532640697518232.zoomTo();\nviewer_17532640697518232.render();\n});\n</script>",
      "text/html": [
       "<div id=\"3dmolviewer_17532640697518232\"  style=\"position: relative; width: 400px; height: 300px;\">\n",
       "        <p id=\"3dmolwarning_17532640697518232\" style=\"background-color:#ffcccc;color:black\">3Dmol.js failed to load for some reason.  Please check your browser console for error messages.<br></p>\n",
       "        </div>\n",
       "<script>\n",
       "\n",
       "var loadScriptAsync = function(uri){\n",
       "  return new Promise((resolve, reject) => {\n",
       "    //this is to ignore the existence of requirejs amd\n",
       "    var savedexports, savedmodule;\n",
       "    if (typeof exports !== 'undefined') savedexports = exports;\n",
       "    else exports = {}\n",
       "    if (typeof module !== 'undefined') savedmodule = module;\n",
       "    else module = {}\n",
       "\n",
       "    var tag = document.createElement('script');\n",
       "    tag.src = uri;\n",
       "    tag.async = true;\n",
       "    tag.onload = () => {\n",
       "        exports = savedexports;\n",
       "        module = savedmodule;\n",
       "        resolve();\n",
       "    };\n",
       "  var firstScriptTag = document.getElementsByTagName('script')[0];\n",
       "  firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);\n",
       "});\n",
       "};\n",
       "\n",
       "if(typeof $3Dmolpromise === 'undefined') {\n",
       "$3Dmolpromise = null;\n",
       "  $3Dmolpromise = loadScriptAsync('https://cdnjs.cloudflare.com/ajax/libs/3Dmol/2.4.2/3Dmol-min.js');\n",
       "}\n",
       "\n",
       "var viewer_17532640697518232 = null;\n",
       "var warn = document.getElementById(\"3dmolwarning_17532640697518232\");\n",
       "if(warn) {\n",
       "    warn.parentNode.removeChild(warn);\n",
       "}\n",
       "$3Dmolpromise.then(function() {\n",
       "viewer_17532640697518232 = $3Dmol.createViewer(document.getElementById(\"3dmolviewer_17532640697518232\"),{backgroundColor:\"white\"});\n",
       "viewer_17532640697518232.zoomTo();\n",
       "\tviewer_17532640697518232.addModel(\"HETATM    1  C1  UNL     1       3.341   0.506  -0.349  1.00  0.00           C  \\nHETATM    2  C2  UNL     1       1.994  -0.109  -0.554  1.00  0.00           C  \\nHETATM    3  O1  UNL     1       1.839  -0.974  -1.458  1.00  0.00           O  \\nHETATM    4  N1  UNL     1       0.876   0.346   0.221  1.00  0.00           N  \\nHETATM    5  C3  UNL     1      -0.424  -0.266   0.167  1.00  0.00           C  \\nHETATM    6  C4  UNL     1      -0.537  -1.665   0.219  1.00  0.00           C  \\nHETATM    7  C5  UNL     1      -1.798  -2.258   0.278  1.00  0.00           C  \\nHETATM    8  N2  UNL     1      -2.913  -1.485   0.302  1.00  0.00           N  \\nHETATM    9  C6  UNL     1      -2.844  -0.130   0.260  1.00  0.00           C  \\nHETATM   10  C7  UNL     1      -1.602   0.521   0.177  1.00  0.00           C  \\nHETATM   11  C8  UNL     1      -1.585   1.998   0.085  1.00  0.00           C  \\nHETATM   12  S1  UNL     1      -0.600   2.783  -0.963  1.00  0.00           S  \\nHETATM   13  O2  UNL     1      -2.485   2.742   0.838  1.00  0.00           O1-\\nHETATM   14  H1  UNL     1       3.531   0.642   0.736  1.00  0.00           H  \\nHETATM   15  H2  UNL     1       4.133  -0.148  -0.770  1.00  0.00           H  \\nHETATM   16  H3  UNL     1       3.376   1.493  -0.854  1.00  0.00           H  \\nHETATM   17  H4  UNL     1       1.005   1.188   0.826  1.00  0.00           H  \\nHETATM   18  H5  UNL     1       0.344  -2.292   0.249  1.00  0.00           H  \\nHETATM   19  H6  UNL     1      -1.889  -3.335   0.323  1.00  0.00           H  \\nHETATM   20  H7  UNL     1      -3.764   0.442   0.270  1.00  0.00           H  \\nCONECT    1    2   14   15   16\\nCONECT    2    3    3    4\\nCONECT    4    5   17\\nCONECT    5    6    6   10\\nCONECT    6    7   18\\nCONECT    7    8    8   19\\nCONECT    8    9\\nCONECT    9   10   10   20\\nCONECT   10   11\\nCONECT   11   12   12   13\\nEND\\n\",\"pdb\");\n",
       "\tviewer_17532640697518232.setStyle({\"stick\": {}});\n",
       "\tviewer_17532640697518232.zoomTo();\n",
       "viewer_17532640697518232.render();\n",
       "});\n",
       "</script>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "<py3Dmol.view at 0x11fd5a780>"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "input_ids[0][5] = tokenizer.mask_token_id\n",
    "input_ids[0][9] = tokenizer.mask_token_id\n",
    "input_ids[0][18] = tokenizer.mask_token_id\n",
    "input_ids[0][11] = tokenizer.mask_token_id\n",
    "# Generate output sequence\n",
    "output_ids = model.generate(input_ids, max_length=128, num_beams=5, num_return_sequences=5,\n",
    "                            early_stopping=True)\n",
    "output = tokenizer.decode(output_ids[1][n:], skip_special_tokens=True)\n",
    "print(output)\n",
    "smiles = sf.decoder(output)\n",
    "print(smiles)\n",
    "smiles_to_3d([smiles])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f696bb9c-2870-4b0b-9b62-1623411e5df6",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}