Diff of /demo_heteroencoder.ipynb [000000] .. [58db57]

Switch to unified view

a b/demo_heteroencoder.ipynb
1
{
2
 "cells": [
3
  {
4
   "cell_type": "code",
5
   "execution_count": null,
6
   "metadata": {},
7
   "outputs": [],
8
   "source": [
9
    "%load_ext autoreload\n",
10
    "%autoreload 2\n",
11
    "\n",
12
    "\n",
13
    "import numpy as np\n",
14
    "import rdkit\n",
15
    "from rdkit import Chem\n",
16
    "\n",
17
    "import h5py, ast, pickle\n",
18
    "\n",
19
    "# Occupy a GPU for the model to be loaded \n",
20
    "%env CUDA_DEVICE_ORDER=PCI_BUS_ID\n",
21
    "# GPU ID, if occupied change to an available GPU ID listed under !nvidia-smi\n",
22
    "%env CUDA_VISIBLE_DEVICES=2 \n",
23
    "\n",
24
    "from ddc_pub import ddc_v3 as ddc"
25
   ]
26
  },
27
  {
28
   "cell_type": "markdown",
29
   "metadata": {},
30
   "source": [
31
    "# Load model"
32
   ]
33
  },
34
  {
35
   "cell_type": "code",
36
   "execution_count": null,
37
   "metadata": {},
38
   "outputs": [],
39
   "source": [
40
    "# Import existing (trained) model\n",
41
    "# Ignore any warning(s) about training configuration or non-seriazable keyword arguments\n",
42
    "model_name = \"models/heteroencoder_model\"\n",
43
    "model = ddc.DDC(model_name=model_name)"
44
   ]
45
  },
46
  {
47
   "cell_type": "markdown",
48
   "metadata": {},
49
   "source": [
50
    "# Load data from dataset"
51
   ]
52
  },
53
  {
54
   "cell_type": "code",
55
   "execution_count": null,
56
   "metadata": {},
57
   "outputs": [],
58
   "source": [
59
    "dataset_name = \"datasets/CHEMBL25_TEST.h5\"\n",
60
    "npoints = 1000\n",
61
    "\n",
62
    "dataset = h5py.File(dataset_name, \"r\")\n",
63
    "mols    = dataset[\"mols\"][:]\n",
64
    "# Select random npoints\n",
65
    "mols_in = mols[np.random.choice(len(mols), npoints, replace=False)]\n",
66
    "dataset.close()\n",
67
    "\n",
68
    "# Get the SMILES behind the binary mols\n",
69
    "smiles_in = [Chem.MolToSmiles(Chem.Mol(mol)) for mol in mols_in]"
70
   ]
71
  },
72
  {
73
   "cell_type": "markdown",
74
   "metadata": {},
75
   "source": [
76
    "# Alternatively, use your own SMILES"
77
   ]
78
  },
79
  {
80
   "cell_type": "code",
81
   "execution_count": null,
82
   "metadata": {},
83
   "outputs": [],
84
   "source": [
85
    "# Input SMILES to auto-encode\n",
86
    "smiles_in = ['Cc1cccn2c(CN(C)C3CCCc4ccccc43)c(C(=O)N3CCOCC3)nc12',\n",
87
    "             'COC(=O)NN=C(c1ccc(O)cc1)C1C(=O)N(C)C(=O)N(C)C1=O',\n",
88
    "             'CCc1cc(CC)nc(OCCCn2c3c(c4cc(-c5nc(C)no5)ccc42)CC(F)(F)CC3)n1',\n",
89
    "             'Cc1ccc2c(C(=O)Nc3ccccc3)c(SSc3c(C(=O)Nc4ccccc4)c4ccc(C)cc4n3C)n(C)c2c1',\n",
90
    "             'Cc1cccc(-c2ccccc2)c1Oc1nc(O)nc(NCc2ccc3occc3c2)n1',\n",
91
    "             'Cn1nnnc1SCC(=O)NN=Cc1ccc(Cl)cc1',\n",
92
    "             'COc1cccc(NS(=O)(=O)c2ccc(OC)c(OC)c2)c1',\n",
93
    "             'COc1ccc(OC)c(S(=O)(=O)n2nc(C)cc2C)c1',\n",
94
    "             'NCCCn1cc(C2=C(c3ccncc3)C(=O)NC2=O)c2ccccc21',\n",
95
    "             'CN(C)C(=O)N1CCN(C(c2ccc(Cl)cc2)c2cccnc2)CC1']\n",
96
    "\n",
97
    "# MUST convert SMILES to binary mols for the model to accept them (it re-converts them to SMILES internally)\n",
98
    "mols_in = [Chem.rdchem.Mol.ToBinary(Chem.MolFromSmiles(smiles)) for smiles in smiles_in]"
99
   ]
100
  },
101
  {
102
   "cell_type": "code",
103
   "execution_count": null,
104
   "metadata": {},
105
   "outputs": [],
106
   "source": [
107
    "# Encode the binary mols into their latent representations\n",
108
    "latent = model.transform(model.vectorize(mols_in))"
109
   ]
110
  },
111
  {
112
   "cell_type": "code",
113
   "execution_count": null,
114
   "metadata": {},
115
   "outputs": [],
116
   "source": [
117
    "# Convert back to SMILES\n",
118
    "smiles_out = []\n",
119
    "for lat in latent:   \n",
120
    "    smiles, _ = model.predict(lat, temp=0)\n",
121
    "    smiles_out.append(smiles)"
122
   ]
123
  },
124
  {
125
   "cell_type": "code",
126
   "execution_count": null,
127
   "metadata": {},
128
   "outputs": [],
129
   "source": [
130
    "# To compare the results, convert smiles_out to CANONICAL\n",
131
    "for idx, smiles in enumerate(smiles_out):\n",
132
    "    mol = Chem.MolFromSmiles(smiles)\n",
133
    "    if mol:\n",
134
    "        smiles_out[idx] = Chem.MolToSmiles(mol, canonical=True)\n",
135
    "    else:\n",
136
    "        smiles_out[idx] = \"INVALID\""
137
   ]
138
  },
139
  {
140
   "cell_type": "code",
141
   "execution_count": null,
142
   "metadata": {
143
    "scrolled": true
144
   },
145
   "outputs": [],
146
   "source": [
147
    "smiles_in"
148
   ]
149
  },
150
  {
151
   "cell_type": "code",
152
   "execution_count": null,
153
   "metadata": {
154
    "scrolled": true
155
   },
156
   "outputs": [],
157
   "source": [
158
    "smiles_out"
159
   ]
160
  }
161
 ],
162
 "metadata": {
163
  "kernelspec": {
164
   "display_name": "ddc",
165
   "language": "python",
166
   "name": "ddc"
167
  },
168
  "language_info": {
169
   "codemirror_mode": {
170
    "name": "ipython",
171
    "version": 3
172
   },
173
   "file_extension": ".py",
174
   "mimetype": "text/x-python",
175
   "name": "python",
176
   "nbconvert_exporter": "python",
177
   "pygments_lexer": "ipython3",
178
   "version": "3.6.7"
179
  }
180
 },
181
 "nbformat": 4,
182
 "nbformat_minor": 2
183
}