{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "view-in-github", "colab_type": "text" }, "source": [ "\"Open" ] }, { "cell_type": "markdown", "metadata": { "id": "OA2k3sAYuiXe" }, "source": [ "#af_cyc_design\n", "\n", "**Cyclic peptide structure prediction and design using AlphaFold**\n", "\n", "Stephen Rettie, Katelyn Campbell, Asim Bera, Alex Kang, Simon Kozlov, Joshmyn De La Cruz, Victor Adebomi, Guangfeng Zhou, Frank DiMaio, Sergey Ovchinnikov, Gaurav Bhardwaj\n", "\n", "doi: https://doi.org/10.1101/2023.02.25.529956\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "cellView": "form", "id": "-AXy0s_4cKaK" }, "outputs": [], "source": [ "#@title setup (~2 minutes)\n", "%%time\n", "import os\n", "if not os.path.isdir(\"params\"):\n", " # get code\n", " os.system(\"pip -q install git+https://github.com/sokrypton/ColabDesign.git@v1.1.1\")\n", " # for debugging\n", " os.system(\"ln -s /usr/local/lib/python3.*/dist-packages/colabdesign colabdesign\")\n", " # download params\n", " os.system(\"mkdir params\")\n", " os.system(\"apt-get install aria2 -qq\")\n", " os.system(\"aria2c -q -x 16 https://storage.googleapis.com/alphafold/alphafold_params_2022-12-06.tar\")\n", " os.system(\"tar -xf alphafold_params_2022-12-06.tar -C params\")\n", "\n", "import warnings\n", "warnings.simplefilter(action='ignore', category=FutureWarning)\n", "\n", "import os\n", "from colabdesign import mk_afdesign_model, clear_mem\n", "from IPython.display import HTML\n", "from google.colab import files\n", "import numpy as np\n", "\n", "import jax\n", "import jax.numpy as jnp\n", "from colabdesign.af.alphafold.common import residue_constants\n", "\n", "def get_pdb(pdb_code=\"\"):\n", " if pdb_code is None or pdb_code == \"\":\n", " upload_dict = files.upload()\n", " pdb_string = upload_dict[list(upload_dict.keys())[0]]\n", " with open(\"tmp.pdb\",\"wb\") as out: out.write(pdb_string)\n", " return \"tmp.pdb\"\n", " elif os.path.isfile(pdb_code):\n", " return pdb_code\n", " elif len(pdb_code) == 4:\n", " os.system(f\"wget -qnc https://files.rcsb.org/view/{pdb_code}.pdb\")\n", " return f\"{pdb_code}.pdb\"\n", " else:\n", " os.system(f\"wget -qnc https://alphafold.ebi.ac.uk/files/AF-{pdb_code}-F1-model_v3.pdb\")\n", " return f\"AF-{pdb_code}-F1-model_v3.pdb\"\n", "\n", "def add_cyclic_offset(self, offset_type=2):\n", " '''add cyclic offset to connect N and C term'''\n", " def cyclic_offset(L):\n", " i = np.arange(L)\n", " ij = np.stack([i,i+L],-1)\n", " offset = i[:,None] - i[None,:]\n", " c_offset = np.abs(ij[:,None,:,None] - ij[None,:,None,:]).min((2,3))\n", " if offset_type == 1:\n", " c_offset = c_offset\n", " elif offset_type >= 2:\n", " a = c_offset < np.abs(offset)\n", " c_offset[a] = -c_offset[a]\n", " if offset_type == 3:\n", " idx = np.abs(c_offset) > 2\n", " c_offset[idx] = (32 * c_offset[idx] )/ abs(c_offset[idx])\n", " return c_offset * np.sign(offset)\n", " idx = self._inputs[\"residue_index\"]\n", " offset = np.array(idx[:,None] - idx[None,:])\n", "\n", " if self.protocol == \"binder\":\n", " c_offset = cyclic_offset(self._binder_len)\n", " offset[self._target_len:,self._target_len:] = c_offset\n", "\n", " if self.protocol in [\"fixbb\",\"partial\",\"hallucination\"]:\n", " Ln = 0\n", " for L in self._lengths:\n", " offset[Ln:Ln+L,Ln:Ln+L] = cyclic_offset(L)\n", " Ln += L\n", " self._inputs[\"offset\"] = offset\n", "\n", "def add_rg_loss(self, weight=0.1):\n", " '''add radius of gyration loss'''\n", " def loss_fn(inputs, outputs):\n", " xyz = outputs[\"structure_module\"]\n", " ca = xyz[\"final_atom_positions\"][:,residue_constants.atom_order[\"CA\"]]\n", " rg = jnp.sqrt(jnp.square(ca - ca.mean(0)).sum(-1).mean() + 1e-8)\n", " rg_th = 2.38 * ca.shape[0] ** 0.365\n", " rg = jax.nn.elu(rg - rg_th)\n", " return {\"rg\":rg}\n", " self._callbacks[\"model\"][\"loss\"].append(loss_fn)\n", " self.opt[\"weights\"][\"rg\"] = weight" ] }, { "cell_type": "markdown", "metadata": { "id": "UUfKrOzT0gOS" }, "source": [ "# fixed backbone design (fixbb) (~2 minutes)\n", "For a given protein backbone, generate/design a new sequence that AlphaFold thinks folds into that conformation." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "qLd1DsnKzxBJ" }, "outputs": [], "source": [ "clear_mem()\n", "af_model = mk_afdesign_model(protocol=\"fixbb\")\n", "af_model.prep_inputs(pdb_filename=get_pdb(\"7m28\"), chain=\"A\")\n", "add_cyclic_offset(af_model, offset_type=2)\n", "\n", "print(\"length\", af_model._len)\n", "print(\"weights\", af_model.opt[\"weights\"])" ] }, { "cell_type": "code", "source": [ "import matplotlib.pyplot as plt\n", "plt.imshow(af_model._inputs[\"offset\"],cmap=\"bwr\")" ], "metadata": { "id": "xr6mw-CXl_o3" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "af_model.restart()\n", "af_model.design_3stage()" ], "metadata": { "id": "u0AwskJ84NGx" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "af_model.plot_traj()" ], "metadata": { "id": "8FB1v7dn1LL6" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "YEApO8YzBoS0" }, "outputs": [], "source": [ "af_model.save_pdb(f\"{af_model.protocol}.pdb\")\n", "af_model.plot_pdb(show_mainchains=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "cW1KQiHKJpfp" }, "outputs": [], "source": [ "HTML(af_model.animate())" ] }, { "cell_type": "code", "source": [ "af_model.get_seqs()" ], "metadata": { "id": "YDrChASGVUUx" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": [ "# hallucination (~1 minute)\n", "For a given length, generate/hallucinate a protein sequence that AlphaFold thinks folds into a well structured protein (high plddt, low pae, many contacts)." ], "metadata": { "id": "qLwS2s_xcjRI" } }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "sZnYfCbfEvol" }, "outputs": [], "source": [ "clear_mem()\n", "af_model = mk_afdesign_model(protocol=\"hallucination\")\n", "af_model.prep_inputs(length=13, rm_aa=\"C\")\n", "add_cyclic_offset(af_model)\n", "# add_rg_loss(af_model)\n", "\n", "print(\"length\",af_model._len)\n", "print(\"weights\",af_model.opt[\"weights\"])" ] }, { "cell_type": "code", "source": [ "# pre-design with gumbel initialization and softmax activation\n", "af_model.restart()\n", "af_model.set_seq(mode=\"gumbel\")\n", "af_model.set_opt(\"con\", binary=True, cutoff=21.6875, num=af_model._len, seqsep=0)\n", "af_model.set_weights(pae=1, plddt=1, con=0.5)\n", "af_model.design_soft(50)\n", "\n", "# three stage design\n", "af_model.set_seq(seq=af_model.aux[\"seq\"][\"pseudo\"])\n", "af_model.design_3stage(50,50,10)" ], "metadata": { "id": "f76xqCkw0vj9" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "A1GxeLZdTTya" }, "outputs": [], "source": [ "af_model.save_pdb(f\"{af_model.protocol}.pdb\")\n", "af_model.plot_pdb(show_mainchains=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "L2E9Tn2Acchj" }, "outputs": [], "source": [ "HTML(af_model.animate())" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "YSKWYu0_GlUH" }, "outputs": [], "source": [ "af_model.get_seqs()" ] } ], "metadata": { "accelerator": "GPU", "colab": { "collapsed_sections": [ "q4qiU9I0QHSz" ], "provenance": [], "include_colab_link": true }, "kernelspec": { "display_name": "Python 3", "name": "python3" }, "language_info": { "name": "python" } }, "nbformat": 4, "nbformat_minor": 0 }