File size: 3,152 Bytes
a48f0ae |
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 |
# ---
# jupyter:
# jupytext:
# notebook_metadata_filter: -kernelspec
# text_representation:
# extension: .py
# format_name: percent
# format_version: '1.3'
# jupytext_version: 1.14.1
# ---
# %% [markdown]
# ## General imports
# %%
import sys
sys.path.insert(
0, "/"
) # this depends on the notebook depth and must be adapted per notebook
# %%
import numpy as np
from compert.paths import DATA_DIR, EMBEDDING_DIR
from dgllife.utils import (
CanonicalAtomFeaturizer,
CanonicalBondFeaturizer,
smiles_to_bigraph,
)
# %% [markdown]
# ## Load Smiles list
# %%
dataset_name = "lincs_trapnell"
# %%
import pandas as pd
smiles_df = pd.read_csv(EMBEDDING_DIR / f"{dataset_name}.smiles")
smiles_list = smiles_df["smiles"].values
# %%
print(f"Number of smiles strings: {len(smiles_list)}")
# %% [markdown]
# ## Featurizer functions
# %%
node_feats = CanonicalAtomFeaturizer(atom_data_field="h")
edge_feats = CanonicalBondFeaturizer(bond_data_field="h", self_loop=True)
# %% [markdown]
# ## Create graphs from smiles and featurizers
# %%
mol_graphs = []
for smiles in smiles_list:
mol_graphs.append(
smiles_to_bigraph(
smiles=smiles,
add_self_loop=True,
node_featurizer=node_feats,
edge_featurizer=edge_feats,
)
)
# %%
print(f"Number of molecular graphs: {len(mol_graphs)}")
# %% [markdown]
# ## Batch graphs
# %%
import dgl
mol_batch = dgl.batch(mol_graphs)
# %%
mol_batch
# %% [markdown]
# ## Load pretrained model
# %% [markdown]
# Choose a model form [here](https://lifesci.dgl.ai/api/model.pretrain.html)
# %%
model_name = "GCN_canonical_PCBA"
# model_name = 'MPNN_canonical_PCBA'
# model_name = 'AttentiveFP_canonical_PCBA'
# model_name = 'Weave_canonical_PCBA'
# model_name = 'GCN_Tox21'
# %%
from dgllife.model import load_pretrained
model = load_pretrained(model_name)
verbose = True
if verbose:
print(model)
# %% [markdown]
# ## Predict with pretrained model
# %% [markdown]
# ### Take readout, just before prediction
# %%
model.eval()
# no edge features
prediction = model(mol_batch, mol_batch.ndata["h"])
# # with edge features
# prediction = model(mol_batch, mol_batch.ndata['h'], mol_batch.edata['h'])
print(f"Prediction has shape: {prediction.shape}")
prediction
# %% [markdown]
# ## Save
# %%
import pandas as pd
df = pd.DataFrame(
data=prediction.detach().numpy(),
index=smiles_list,
columns=[f"latent_{i+1}" for i in range(prediction.size()[1])],
)
# %%
import os
fname = f"{model_name}_embedding_{dataset_name}.parquet"
directory = EMBEDDING_DIR / "dgl" / "data" / "embeddings"
if not directory.exists():
os.makedirs(directory)
print(f"Created folder: {directory}")
df.to_parquet(directory / fname)
# %% [markdown]
# Check that it worked
# %%
df = pd.read_parquet(directory / fname)
df
# %%
df.std()
# %% [markdown]
# ## Drawing molecules
# %%
from IPython.display import SVG
from rdkit import Chem
from rdkit.Chem import Draw
# %%
mols = [Chem.MolFromSmiles(s) for s in smiles_list[:14]]
Draw.MolsToGridImage(mols, molsPerRow=7, subImgSize=(180, 150))
# %%
|