{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# DeepPurpose Deep Dive\n", "## Tutorial 2: Training a Drug Property Prediction Model from Scratch for Assay Data\n", "#### [@KexinHuang5](https://twitter.com/KexinHuang5)\n", "\n", "In this tutorial, we further extends the use cases of DeepPurpose to assay data where there are only drug information and its affinity score to the protein in the assay. \n", "\n", "Agenda:\n", "\n", "- Part I: Introduction to Assay Data\n", "- Part II: Drug Property Prediction\n", "\n", "Let's start!" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from DeepPurpose import utils, dataset, CompoundPred\n", "import warnings\n", "warnings.filterwarnings(\"ignore\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Part I: Introduction to Assay Data\n", "\n", "Drug-target interaction measures the binding of drug molecules to the protein targets. In drug discovery process, we usually already have a protein of interest and traditionally, high-throughput screening assay is performed on a potential drug library to get affinity scores. But it is expensive and time-consuming, and it cannot go through a huge amount of molecule candidates, which means it potentially can miss a drug candidate. Machine learning can provide an important role to facilitate this process. One way to do it is to narrow down the search space for HTS. Here are the steps:\n", "\n", "1. For a target protein of interest, conduct an initial high-throughput screening assay on a set of drugs (ideally, structurally diverse), where the set size depends on the time and cost constraint.\n", "\n", "2. Train a deep learning model using DeepPurpose and select a threshold to control the false positive rate (e.g., < 0.02).\n", "\n", "3. Apply the trained model on a large set of drugs. Select the set of drugs that meet the threshold and send to HTS.\n", "\n", "Through the above three steps, it greatly cuts down the time of screening and also reduces the likelihood of missing a potential drug candidate. Now, we show the step 2: using DeepPurpose to train a deep learning model for screening the assay. \n", "\n", "(**Data**) DeepPurpose takes into an array of drug's SMILES strings (**d**) and an array of label (**y**), which can either be binary 0/1 indicating interaction outcome or a real number indicating affinity value. Note **y**\\[0\\] is the score for **d**\\[0\\].\n", "\n", "Besides transforming into numpy arrays through some data wrangling on your own, DeepPurpose also provides two ways to help data preparation. \n", "\n", "The first way is to read from local files. For example, to load drug assay data, we expect a file.txt where each line is a drug SMILES string, followed by an affinity score or 0/1 label:\n", "\n", "```CC1=C...C4)N 7.365```\n", "\n", "Then, we use ```dataset.read_file_training_dataset_bioassay``` to load it." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Drug 1: CCOC1=CC=C(C=C1)N2C=CC(=O)C(=N2)C(=O)NC3=CC=C(C=C3)S(=O)(=O)NC4=NC=CC=N4\n", "Score 1: 0.0\n" ] } ], "source": [ "X_drugs, X_targets, y = dataset.read_file_training_dataset_bioassay('./toy_data/AID1706.txt')\n", "print('Drug 1: ' + X_drugs[0])\n", "print('Score 1: ' + str(y[0]))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "DeepPurpose also provides data loaders to ease preprocessing. For example, in this tutorial, we will use the HIV screening data. We can use ```dataset.load_HIV```. It will download, preprocess to the designated data format. " ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Dataset already downloaded in the local system...\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Drug 1: CCC1=[O+][Cu-3]2([O+]=C(CC)C1)[O+]=C(CC)CC(CC)=[O+]2\n", "Score 1: 0\n" ] } ], "source": [ "X_drugs, y, drugs_index = dataset.load_HIV(path = './data')\n", "print('Drug 1: ' + X_drugs[0])\n", "print('Score 1: ' + str(y[0]))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For more detailed examples and tutorials of data loading, checkout this [tutorial](./DEMO/load_data_tutorial.ipynb).\n", "\n", "## Part II: Drug Property Prediction Framework\n", "\n", "DeepPurpose provides a simple framework to do drug property prediction research using 8 encoders for drugs. It basically consists of the following steps, where each step corresponds to one line of code:\n", "\n", "- Encoder specification\n", "- Data encoding and split\n", "- Model configuration generation\n", "- Model initialization\n", "- Model Training\n", "- Model Prediction and Repuposing/Screening\n", "- Model Saving and Loading\n", "\n", "Let's start with data encoding! \n", "\n", "(**Encoder specification**) After we obtain the required data format from Part I, we need to prepare them for the encoders. Hence, we first specify the encoder to use for drug and protein. Here we try MPNN for drug.\n", "\n", "If you find MPNN is too large for the CPUs, you can try smaller encoders by uncommenting the last line:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "#drug_encoding = 'MPNN'\n", "drug_encoding = 'Morgan'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that you can switch encoder just by changing the encoding name above. The full list of encoders are listed [here](https://github.com/kexinhuang12345/DeepPurpose#encodings). \n", "\n", "(**Data encoding and split**) Now, we encode the data into the specified format, using ```utils.data_process``` function. It specifies train/validation/test split fractions, and random seed to ensure same data splits for reproducibility. This function also support data splitting methods such as ```cold_drug```, which splits on drug for model robustness evaluation to test on unseen drug/proteins.\n", "\n", "The function outputs train, val, test pandas dataframes." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Drug Property Prediction Mode...\n", "in total: 41127 drugs\n", "encoding drug...\n", "unique drugs: 41127\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "RDKit WARNING: [09:01:53] WARNING: not removing hydrogen atom without neighbors\n", "RDKit WARNING: [09:01:53] WARNING: not removing hydrogen atom without neighbors\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Done.\n" ] }, { "data": { "text/html": [ "
| \n", " | SMILES | \n", "Label | \n", "drug_encoding | \n", "
|---|---|---|---|
| 0 | \n", "CCC1=[O+][Cu-3]2([O+]=C(CC)C1)[O+]=C(CC)CC(CC)... | \n", "0 | \n", "[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, ... | \n", "