Skip to content

Commit f2e8566

Browse files
authored
Merge pull request #3 from enveda/data_prep_notebooks
Reviewed data prep notebooks
2 parents 63d4c7d + 45f2b2b commit f2e8566

4 files changed

Lines changed: 1336 additions & 2 deletions

File tree

.gitignore

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ DATABASE_SCHEMA.md
2222
*.svg
2323
*.log
2424
/notebooks/*.py
25-
/notebooks/debug/*.py
2625

2726
# File exceptions
2827
!files/autodock_vina_box.txt
@@ -56,7 +55,6 @@ DATABASE_SCHEMA.md
5655
/.databrickscfg
5756
/.data
5857
/.local
59-
/notebooks/data_prep/
6058

6159
# Chache
6260
/.pytest_cache
Lines changed: 382 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,382 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"import warnings\n",
10+
"\n",
11+
"warnings.filterwarnings(\"ignore\")\n",
12+
"import os\n",
13+
"\n",
14+
"import matplotlib.pyplot as plt\n",
15+
"import pandas as pd\n",
16+
"\n",
17+
"from guild.tools.ligand_properties import (\n",
18+
" assign_properties,\n",
19+
" compound_filter,\n",
20+
" generate_equal_mw_distributions,\n",
21+
")\n",
22+
"from guild.transformers.chembl import get_assays_locally, get_decoys_locally"
23+
]
24+
},
25+
{
26+
"cell_type": "markdown",
27+
"metadata": {},
28+
"source": [
29+
"# Get protein info"
30+
]
31+
},
32+
{
33+
"cell_type": "code",
34+
"execution_count": null,
35+
"metadata": {},
36+
"outputs": [],
37+
"source": [
38+
"support_dir = \"../../guild/support\"\n",
39+
"\n",
40+
"os.makedirs(f\"{support_dir}/binders\", exist_ok=True)\n",
41+
"os.makedirs(f\"{support_dir}/decoys\", exist_ok=True)"
42+
]
43+
},
44+
{
45+
"cell_type": "code",
46+
"execution_count": null,
47+
"metadata": {},
48+
"outputs": [],
49+
"source": [
50+
"gpcrdb_mapping = pd.read_csv(f\"{support_dir}/uniprot_gpcrdb_mapping.txt\", sep=\" \")\n",
51+
"gpcrdb_mapping.head(2)"
52+
]
53+
},
54+
{
55+
"cell_type": "code",
56+
"execution_count": null,
57+
"metadata": {},
58+
"outputs": [],
59+
"source": [
60+
"uniprot_gene_list = set(gpcrdb_mapping[\"uniprot_id\"].tolist())\n",
61+
"len(uniprot_gene_list)"
62+
]
63+
},
64+
{
65+
"cell_type": "markdown",
66+
"metadata": {},
67+
"source": [
68+
"# Update binders to ChEMBL 36"
69+
]
70+
},
71+
{
72+
"cell_type": "code",
73+
"execution_count": null,
74+
"metadata": {},
75+
"outputs": [],
76+
"source": [
77+
"assay_df = get_assays_locally(uniprot_gene_list, min_mol_wt=250, max_mol_wt=450)"
78+
]
79+
},
80+
{
81+
"cell_type": "code",
82+
"execution_count": null,
83+
"metadata": {},
84+
"outputs": [],
85+
"source": [
86+
"# Filter out compounds with pchembl value <= 0\n",
87+
"assay_df = assay_df[assay_df[\"pchembl_value\"] > 0]\n",
88+
"\n",
89+
"# Assign activity based on pchembl value 6\n",
90+
"assay_df[\"activity\"] = assay_df[\"pchembl_value\"].apply(\n",
91+
" lambda x: \"weak-binder\" if x < 6 else \"strong-binder\"\n",
92+
")\n",
93+
"chembl_version = assay_df[\"chembl_version\"].values[0]\n",
94+
"chembl_version"
95+
]
96+
},
97+
{
98+
"cell_type": "code",
99+
"execution_count": null,
100+
"metadata": {},
101+
"outputs": [],
102+
"source": [
103+
"BINDER_FILE = f\"{support_dir}/binders/chembl_binders_{chembl_version}.tsv\"\n",
104+
"\n",
105+
"if not os.path.exists(BINDER_FILE):\n",
106+
" assay_df_filtered = assay_df[\n",
107+
" [\n",
108+
" \"protein_symbol\",\n",
109+
" \"activity\",\n",
110+
" \"chembl_id\",\n",
111+
" \"smiles\",\n",
112+
" \"pchembl_value\",\n",
113+
" ]\n",
114+
" ]\n",
115+
" assay_df_filtered[\"chembl_id\"] = \"CHEMBL\" + assay_df_filtered[\"chembl_id\"].astype(\n",
116+
" str\n",
117+
" )\n",
118+
" assay_df_filtered.rename(columns={\"protein_symbol\": \"uniprot_id\"}, inplace=True)\n",
119+
"\n",
120+
" assay_df_filtered = pd.merge(\n",
121+
" assay_df_filtered, gpcrdb_mapping, how=\"outer\", on=\"uniprot_id\"\n",
122+
" )\n",
123+
"\n",
124+
" assay_df_filtered.to_csv(\n",
125+
" f\"{support_dir}/binders/chembl_binders_{chembl_version}.tsv\",\n",
126+
" sep=\"\\t\",\n",
127+
" index=False,\n",
128+
" )\n",
129+
"else:\n",
130+
" assay_df_filtered = pd.read_csv(BINDER_FILE, sep=\"\\t\")"
131+
]
132+
},
133+
{
134+
"cell_type": "markdown",
135+
"metadata": {},
136+
"source": [
137+
"# Update decoys to ChEMBL 36"
138+
]
139+
},
140+
{
141+
"cell_type": "code",
142+
"execution_count": null,
143+
"metadata": {},
144+
"outputs": [],
145+
"source": [
146+
"MIN_MW = 250\n",
147+
"MAX_MW = 450\n",
148+
"MAX_RING_SIZE = 10"
149+
]
150+
},
151+
{
152+
"cell_type": "code",
153+
"execution_count": null,
154+
"metadata": {},
155+
"outputs": [],
156+
"source": [
157+
"decoy_df = get_decoys_locally(\n",
158+
" min_mol_wt=MIN_MW, max_mol_wt=MAX_MW, version=\"chembl_36\"\n",
159+
") # without the version flag, the latest chembl data is pulled\n",
160+
"decoy_df.head()"
161+
]
162+
},
163+
{
164+
"cell_type": "markdown",
165+
"metadata": {},
166+
"source": [
167+
"### Add molecular properties to the df"
168+
]
169+
},
170+
{
171+
"cell_type": "code",
172+
"execution_count": null,
173+
"metadata": {},
174+
"outputs": [],
175+
"source": [
176+
"if not os.path.exists(f\"{support_dir}/decoys/{chembl_version}_with_properties.tsv\"):\n",
177+
" decoy_df_with_properties = assign_properties(\n",
178+
" decoy_df[[\"chembl_id\", \"canonical_smiles\"]]\n",
179+
" )\n",
180+
" decoy_df_with_properties[\"scaffold_smiles\"] = decoy_df_with_properties[\n",
181+
" \"scaffold_smiles\"\n",
182+
" ].replace(\"\", None)\n",
183+
"\n",
184+
" decoy_df_with_properties.to_csv(\n",
185+
" f\"{support_dir}/decoys/{chembl_version}_with_properties.tsv\",\n",
186+
" sep=\"\\t\",\n",
187+
" index=False,\n",
188+
" )\n",
189+
"else:\n",
190+
" decoy_df_with_properties = pd.read_csv(\n",
191+
" f\"{support_dir}/decoys/{chembl_version}_with_properties.tsv\",\n",
192+
" sep=\"\\t\",\n",
193+
" low_memory=False,\n",
194+
" )\n",
195+
"\n",
196+
"decoy_df_with_properties.head()"
197+
]
198+
},
199+
{
200+
"cell_type": "markdown",
201+
"metadata": {},
202+
"source": [
203+
"## Subset to match the same properties as other ligands"
204+
]
205+
},
206+
{
207+
"cell_type": "code",
208+
"execution_count": null,
209+
"metadata": {},
210+
"outputs": [],
211+
"source": [
212+
"all_decoy_df = pd.merge(\n",
213+
" decoy_df,\n",
214+
" decoy_df_with_properties,\n",
215+
" left_on=\"chembl_id\",\n",
216+
" right_on=\"id\",\n",
217+
" how=\"inner\",\n",
218+
")\n",
219+
"all_decoy_df.drop(columns=[\"id\"], inplace=True)"
220+
]
221+
},
222+
{
223+
"cell_type": "code",
224+
"execution_count": null,
225+
"metadata": {},
226+
"outputs": [],
227+
"source": [
228+
"all_decoy_df.to_csv(\n",
229+
" f\"{support_dir}/decoys/{chembl_version}_decoys_with_properties.tsv\",\n",
230+
" sep=\"\\t\",\n",
231+
" index=False,\n",
232+
")"
233+
]
234+
},
235+
{
236+
"cell_type": "code",
237+
"execution_count": null,
238+
"metadata": {},
239+
"outputs": [],
240+
"source": [
241+
"all_decoy_df[\"molecular_weight\"].plot.hist(bins=100)\n",
242+
"plt.show()"
243+
]
244+
},
245+
{
246+
"cell_type": "markdown",
247+
"metadata": {},
248+
"source": [
249+
"### Subset the df based on scaffolds and MW in the distribution"
250+
]
251+
},
252+
{
253+
"cell_type": "code",
254+
"execution_count": null,
255+
"metadata": {},
256+
"outputs": [],
257+
"source": [
258+
"if os.path.exists(f\"{support_dir}/decoys/{chembl_version}_decoys_filtered.tsv\"):\n",
259+
" decoys_filtered = pd.read_csv(\n",
260+
" f\"{support_dir}/decoys/{chembl_version}_decoys_filtered.tsv\", sep=\"\\t\"\n",
261+
" )\n",
262+
"else:\n",
263+
" decoys_filtered = compound_filter(\n",
264+
" all_decoy_df,\n",
265+
" min_MW=MIN_MW,\n",
266+
" max_MW=MAX_MW,\n",
267+
" scaffold_col=\"scaffold_smiles\",\n",
268+
" ro5_fulfilled_col=\"ro5_fulfilled\",\n",
269+
" largest_ring_size_col=\"max_ring_size\",\n",
270+
" max_per_scaffold=5,\n",
271+
" seed=42,\n",
272+
" MW_column=\"molecular_weight\",\n",
273+
" max_ring_size=MAX_RING_SIZE,\n",
274+
" )\n",
275+
"\n",
276+
" decoys_filtered.to_csv(\n",
277+
" f\"{support_dir}/decoys/{chembl_version}_decoys_filtered.tsv\",\n",
278+
" sep=\"\\t\",\n",
279+
" index=False,\n",
280+
" )"
281+
]
282+
},
283+
{
284+
"cell_type": "markdown",
285+
"metadata": {},
286+
"source": [
287+
"# Generate decoys sublists"
288+
]
289+
},
290+
{
291+
"cell_type": "code",
292+
"execution_count": null,
293+
"metadata": {},
294+
"outputs": [],
295+
"source": [
296+
"# Decoys with 1000 samples\n",
297+
"decoys_sample_1000 = generate_equal_mw_distributions(\n",
298+
" df_1=decoys_filtered,\n",
299+
" mw_column=\"molecular_weight\",\n",
300+
" lipinski_column=\"ro5_fulfilled\",\n",
301+
" n_bins=100,\n",
302+
" target_size=1000,\n",
303+
")\n",
304+
"\n",
305+
"decoys_sample_1000.to_csv(\n",
306+
" f\"{support_dir}/decoys/{chembl_version}_decoys_1000.tsv\",\n",
307+
" sep=\"\\t\",\n",
308+
" index=False,\n",
309+
")"
310+
]
311+
},
312+
{
313+
"cell_type": "code",
314+
"execution_count": null,
315+
"metadata": {},
316+
"outputs": [],
317+
"source": [
318+
"decoys_sample_1000[\"molecular_weight\"].plot.hist(bins=100)\n",
319+
"plt.show()"
320+
]
321+
},
322+
{
323+
"cell_type": "markdown",
324+
"metadata": {},
325+
"source": [
326+
"### Generate 100 decoys list"
327+
]
328+
},
329+
{
330+
"cell_type": "code",
331+
"execution_count": null,
332+
"metadata": {},
333+
"outputs": [],
334+
"source": [
335+
"decoys_sample_100 = generate_equal_mw_distributions(\n",
336+
" df_1=decoys_filtered,\n",
337+
" mw_column=\"molecular_weight\",\n",
338+
" lipinski_column=\"ro5_fulfilled\",\n",
339+
" n_bins=20,\n",
340+
" target_size=100,\n",
341+
")\n",
342+
"\n",
343+
"decoys_sample_100.to_csv(\n",
344+
" f\"{support_dir}/decoys/{chembl_version}_decoys_100.tsv\",\n",
345+
" sep=\"\\t\",\n",
346+
" index=False,\n",
347+
")"
348+
]
349+
},
350+
{
351+
"cell_type": "code",
352+
"execution_count": null,
353+
"metadata": {},
354+
"outputs": [],
355+
"source": [
356+
"decoys_sample_100[\"molecular_weight\"].plot.hist(bins=20)\n",
357+
"plt.show()"
358+
]
359+
}
360+
],
361+
"metadata": {
362+
"kernelspec": {
363+
"display_name": ".venv",
364+
"language": "python",
365+
"name": "python3"
366+
},
367+
"language_info": {
368+
"codemirror_mode": {
369+
"name": "ipython",
370+
"version": 3
371+
},
372+
"file_extension": ".py",
373+
"mimetype": "text/x-python",
374+
"name": "python",
375+
"nbconvert_exporter": "python",
376+
"pygments_lexer": "ipython3",
377+
"version": "3.10.19"
378+
}
379+
},
380+
"nbformat": 4,
381+
"nbformat_minor": 2
382+
}

0 commit comments

Comments
 (0)