{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# DeepPurpose Deep Dive\n", "## Tutorial 1: Training a Drug-Target Interaction Model from Scratch\n", "#### [@KexinHuang5](https://twitter.com/KexinHuang5)\n", "\n", "In this tutorial, we take a deep dive into DeepPurpose and show how it builds a drug-target interaction model from scratch. \n", "\n", "Agenda:\n", "\n", "- Part I: Overview of DeepPurpose and Data\n", "- Part II: Drug Target Interaction Prediction\n", " - DeepPurpose Framework\n", " - Applications to Drug Repurposing and Virtual Screening\n", " - Pretrained Models\n", " - Hyperparameter Tuning\n", " - Model Robustness Evaluation\n", "\n", "Let's start!" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "RDKit WARNING: [15:58:25] Enabling RDKit 2019.09.3 jupyter extensions\n" ] } ], "source": [ "from DeepPurpose import utils, dataset\n", "from DeepPurpose import DTI as models\n", "import warnings\n", "warnings.filterwarnings(\"ignore\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Part I: Overview of DeepPurpose and Data\n", "\n", "Drug-target interaction measures the binding of drug molecules to the protein targets. Accurate identification of DTI is fundamental for drug discovery and supports many downstream tasks. Among others, drug screening and repurposing are two main applications based on DTI. Drug screening helps identify ligand candidates that can bind to the protein of interest, whereas drug repurposing finds new therapeutic purposes for existing drugs. Both tasks could alleviate the costly, time-consuming, and labor-intensive process of synthesis and analysis, which is extremely important, especially in the cases of hunting effective and safe treatments for COVID-19.\n", "\n", "DeepPurpose is a pytorch-based deep learning framework that is initiated to provide a simple but powerful toolkit for drug-target interaction prediction and its related applications. We see many exciting recent works in this direction, but to leverage these models, it takes lots of efforts due to the esoteric instructions and interface. DeepPurpose is designed to make things as simple as possible using a unified framework.\n", "\n", "DeepPurpose uses an encoder-decoder framework. Drug repurposing and screening are two applications after we obtain DTI models. The input to the model is a drug target pair, where drug uses the simplified molecular-input line-entry system (SMILES) string and target uses the amino acid sequence. The output is a score indicating the binding activity of the drug target pair. Now, we begin talking about the data format expected.\n", "\n", "\n", "(**Data**) DeepPurpose takes into an array of drug's SMILES strings (**d**), an array of target protein's amino acid sequence (**t**), and an array of label (**y**), which can either be binary 0/1 indicating interaction outcome or a real number indicating affinity value. The input drug and target arrays should be paired, i.e. **y**\\[0\\] is the score for **d**\\[0\\] and **t**\\[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 target pairs, we expect a file.txt where each line is a drug SMILES string, followed by a protein sequence, and an affinity score or 0/1 label:\n", "\n", "```CC1=C...C4)N MKK...LIDL 7.365``` \\\n", "```CC1=C...C4)N QQP...EGKH 4.999```\n", "\n", "Then, we use ```dataset.read_file_training_dataset_drug_target_pairs``` to load it." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Drug 1: CC1=C2C=C(C=CC2=NN1)C3=CC(=CN=C3)OCC(CC4=CC=CC=C4)N\n", "Target 1: MKKFFDSRREQGGSGLGSGSSGGGGSTSGLGSGYIGRVFGIGRQQVTVDEVLAEGGFAIVFLVRTSNGMKCALKRMFVNNEHDLQVCKREIQIMRDLSGHKNIVGYIDSSINNVSSGDVWEVLILMDFCRGGQVVNLMNQRLQTGFTENEVLQIFCDTCEAVARLHQCKTPIIHRDLKVENILLHDRGHYVLCDFGSATNKFQNPQTEGVNAVEDEIKKYTTLSYRAPEMVNLYSGKIITTKADIWALGCLLYKLCYFTLPFGESQVAICDGNFTIPDNSRYSQDMHCLIRYMLEPDPDKRPDIYQVSYFSFKLLKKECPIPNVQNSPIPAKLPEPVKASEAAAKKTQPKARLTDPIPTTETSIAPRQRPKAGQTQPNPGILPIQPALTPRKRATVQPPPQAAGSSNQPGLLASVPQPKPQAPPSQPLPQTQAKQPQAPPTPQQTPSTQAQGLPAQAQATPQHQQQLFLKQQQQQQQPPPAQQQPAGTFYQQQQAQTQQFQAVHPATQKPAIAQFPVVSQGGSQQQLMQNFYQQQQQQQQQQQQQQLATALHQQQLMTQQAALQQKPTMAAGQQPQPQPAAAPQPAPAQEPAIQAPVRQQPKVQTTPPPAVQGQKVGSLTPPSSPKTQRAGHRRILSDVTHSAVFGVPASKSTQLLQAAAAEASLNKSKSATTTPSGSPRTSQQNVYNPSEGSTWNPFDDDNFSKLTAEELLNKDFAKLGEGKHPEKLGGSAESLIPGFQSTQGDAFATTSFSAGTAEKRKGGQTVDSGLPLLSVSDPFIPLQVPDAPEKLIEGLKSPDTSLLLPDLLPMTDPFGSTSDAVIEKADVAVESLIPGLEPPVPQRLPSQTESVTSNRTDSLTGEDSLLDCSLLSNPTTDLLEEFAPTAISAPVHKAAEDSNLISGFDVPEGSDKVAEDEFDPIPVLITKNPQGGHSRNSSGSSESSLPNLARSLLLVDQLIDL\n", "Score 1: 7.365\n" ] } ], "source": [ "X_drugs, X_targets, y = dataset.read_file_training_dataset_drug_target_pairs('./toy_data/dti.txt')\n", "print('Drug 1: ' + X_drugs[0])\n", "print('Target 1: ' + X_targets[0])\n", "print('Score 1: ' + str(y[0]))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Many method researchers want to test on benchmark datasets such as KIBA/DAVIS/BindingDB, DeepPurpose also provides data loaders to ease preprocessing. For example, we want to load the DAVIS dataset, we can use ```dataset.load_process_DAVIS```. It will download, preprocess to the designated data format. It supports label log-scale transformation for easier regression and also allows label binarization given a customized threshold." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Beginning Processing...\n", "Beginning to extract zip file...\n", "Default set to logspace (nM -> p) for easier regression\n", "Done!\n", "Drug 1: CC1=C2C=C(C=CC2=NN1)C3=CC(=CN=C3)OCC(CC4=CC=CC=C4)N\n", "Target 1: MKKFFDSRREQGGSGLGSGSSGGGGSTSGLGSGYIGRVFGIGRQQVTVDEVLAEGGFAIVFLVRTSNGMKCALKRMFVNNEHDLQVCKREIQIMRDLSGHKNIVGYIDSSINNVSSGDVWEVLILMDFCRGGQVVNLMNQRLQTGFTENEVLQIFCDTCEAVARLHQCKTPIIHRDLKVENILLHDRGHYVLCDFGSATNKFQNPQTEGVNAVEDEIKKYTTLSYRAPEMVNLYSGKIITTKADIWALGCLLYKLCYFTLPFGESQVAICDGNFTIPDNSRYSQDMHCLIRYMLEPDPDKRPDIYQVSYFSFKLLKKECPIPNVQNSPIPAKLPEPVKASEAAAKKTQPKARLTDPIPTTETSIAPRQRPKAGQTQPNPGILPIQPALTPRKRATVQPPPQAAGSSNQPGLLASVPQPKPQAPPSQPLPQTQAKQPQAPPTPQQTPSTQAQGLPAQAQATPQHQQQLFLKQQQQQQQPPPAQQQPAGTFYQQQQAQTQQFQAVHPATQKPAIAQFPVVSQGGSQQQLMQNFYQQQQQQQQQQQQQQLATALHQQQLMTQQAALQQKPTMAAGQQPQPQPAAAPQPAPAQEPAIQAPVRQQPKVQTTPPPAVQGQKVGSLTPPSSPKTQRAGHRRILSDVTHSAVFGVPASKSTQLLQAAAAEASLNKSKSATTTPSGSPRTSQQNVYNPSEGSTWNPFDDDNFSKLTAEELLNKDFAKLGEGKHPEKLGGSAESLIPGFQSTQGDAFATTSFSAGTAEKRKGGQTVDSGLPLLSVSDPFIPLQVPDAPEKLIEGLKSPDTSLLLPDLLPMTDPFGSTSDAVIEKADVAVESLIPGLEPPVPQRLPSQTESVTSNRTDSLTGEDSLLDCSLLSNPTTDLLEEFAPTAISAPVHKAAEDSNLISGFDVPEGSDKVAEDEFDPIPVLITKNPQGGHSRNSSGSSESSLPNLARSLLLVDQLIDL\n", "Score 1: 7.3655227298392685\n" ] } ], "source": [ "X_drugs, X_targets, y = dataset.load_process_DAVIS(path = './data', binary = False, convert_to_log = True, threshold = 30)\n", "print('Drug 1: ' + X_drugs[0])\n", "print('Target 1: ' + X_targets[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 Target Interaction Prediction Framework\n", "\n", "DeepPurpose provides a simple framework to conduct DTI research using 8 encoders for drugs and 7 for proteins. 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 and CNN for target.\n", "\n", "If you find MPNN and CNN are 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, target_encoding = 'MPNN', 'CNN'\n", "#drug_encoding, target_encoding = 'Morgan', 'Conjoint_triad'" ] }, { "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). Here, we are using the message passing neural network encoder for drug and convolutional neural network encoder for protein.\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``` and ```cold_protein```, which splits on drug/proteins 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": [ "in total: 30056 drug-target pairs\n", "encoding drug...\n", "unique drugs: 68\n", "drug encoding finished...\n", "encoding protein...\n", "unique target sequence: 379\n", "protein encoding finished...\n", "splitting dataset...\n", "Done.\n" ] }, { "data": { "text/html": [ "
| \n", " | SMILES | \n", "Target Sequence | \n", "Label | \n", "drug_encoding | \n", "target_encoding | \n", "
|---|---|---|---|---|---|
| 0 | \n", "CC1=C2C=C(C=CC2=NN1)C3=CC(=CN=C3)OCC(CC4=CC=CC... | \n", "PFWKILNPLLERGTYYYFMGQQPGKVLGDQRRPSLPALHFIKGAGK... | \n", "4.999996 | \n", "[[[tensor(1.), tensor(0.), tensor(0.), tensor(... | \n", "[P, F, W, K, I, L, N, P, L, L, E, R, G, T, Y, ... | \n", "