OpenFold.ipynb 52.1 KB
Newer Older
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "pc5-mbsX9PZC"
      },
      "source": [
        "# OpenFold Colab\n",
        "\n",
        "Runs a simplified version of [OpenFold](https://github.com/aqlaboratory/openfold) on a target sequence. Adapted from DeepMind's [official AlphaFold Colab](https://colab.research.google.com/github/deepmind/alphafold/blob/main/notebooks/AlphaFold.ipynb).\n",
        "\n",
        "**Differences to AlphaFold v2.0**\n",
        "\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
15
        "OpenFold is a trainable PyTorch reimplementation of AlphaFold 2. For the purposes of inference, it is practically identical to the original (\"practically\" because ensembling is excluded from OpenFold (recycling is enabled, however)).\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
16
        "\n",
17
        "In this notebook, OpenFold is run with your choice of our original OpenFold parameters or DeepMind's publicly released parameters for AlphaFold 2.\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
18
19
20
21
22
23
24
25
26
27
28
        "\n",
        "**Note**\n",
        "\n",
        "Like DeepMind's official Colab, this notebook uses **no templates (homologous structures)** and a selected portion of the full [BFD database](https://bfd.mmseqs.com/).\n",
        "\n",
        "**Citing this work**\n",
        "\n",
        "Any publication that discloses findings arising from using this notebook should [cite](https://github.com/deepmind/alphafold/#citing-this-work) DeepMind's [AlphaFold paper](https://doi.org/10.1038/s41586-021-03819-2).\n",
        "\n",
        "**Licenses**\n",
        "\n",
29
        "This Colab supports inference with the [AlphaFold model parameters](https://github.com/deepmind/alphafold/#model-parameters-license), made available under the Creative Commons Attribution 4.0 International ([CC BY 4.0](https://creativecommons.org/licenses/by/4.0/legalcode)) license. The Colab itself is provided under the [Apache 2.0 license](https://www.apache.org/licenses/LICENSE-2.0). See the full license statement below.\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
30
31
32
33
34
35
36
37
38
39
40
        "\n",
        "**More information**\n",
        "\n",
        "You can find more information about how AlphaFold/OpenFold works in DeepMind's two Nature papers:\n",
        "\n",
        "*   [AlphaFold methods paper](https://www.nature.com/articles/s41586-021-03819-2)\n",
        "*   [AlphaFold predictions of the human proteome paper](https://www.nature.com/articles/s41586-021-03828-1)\n",
        "\n",
        "FAQ on how to interpret AlphaFold/OpenFold predictions are [here](https://alphafold.ebi.ac.uk/faq)."
      ]
    },
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
41
42
    {
      "cell_type": "code",
jnwei's avatar
jnwei committed
43
      "execution_count": null,
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
44
      "metadata": {
45
46
47
48
49
        "id": "rowN0bVYLe9n",
        "outputId": "a947f86a-e860-44e6-eb51-81c26608fdfc",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
50
      },
51
52
53
54
55
56
57
58
59
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Length of input sequence : 136\n"
          ]
        }
      ],
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
60
61
      "source": [
        "#@markdown ### Enter the amino acid sequence to fold ⬇️\n",
62
        "#@markdown For multiple sequences, separate sequences with a colon `:`\n",
jnwei's avatar
jnwei committed
63
        "input_sequence = 'MKLKQVADKLEEVASKLYHNANELARVAKLLGER:MKLKQVADKLEEVASKLYHNANELARVAKLLGER: MKLKQVADKLEEVASKLYHNANELARVAKLLGER:MKLKQVADKLEEVASKLYHNANELARVAKLLGER'  #@param {type:\"string\"}\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
64
65
66
        "\n",
        "#@markdown ### Configure the model ⬇️\n",
        "\n",
67
68
        "weight_set = 'AlphaFold' #@param [\"OpenFold\", \"AlphaFold\"]\n",
        "model_mode = 'multimer' #@param [\"monomer\", \"multimer\"]\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
69
70
        "relax_prediction = True #@param {type:\"boolean\"}\n",
        "\n",
71
        "\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
72
        "# Remove all whitespaces, tabs and end lines; upper-case\n",
73
        "input_sequence = input_sequence.translate(str.maketrans('', '', ' \\n\\t')).upper()\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
74
        "aatypes = set('ACDEFGHIKLMNPQRSTVWY')  # 20 standard aatypes\n",
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
        "allowed_chars = aatypes.union({':'})\n",
        "if not set(input_sequence).issubset(allowed_chars):\n",
        "  raise Exception(f'Input sequence contains non-amino acid letters: {set(input_sequence) - allowed_chars}. OpenFold only supports 20 standard amino acids as inputs.')\n",
        "\n",
        "if ':' in input_sequence and weight_set != 'AlphaFold':\n",
        "  raise ValueError('Input sequence is a multimer, must select Alphafold weight set')\n",
        "\n",
        "import enum\n",
        "@enum.unique\n",
        "class ModelType(enum.Enum):\n",
        "  MONOMER = 0\n",
        "  MULTIMER = 1\n",
        "\n",
        "model_type_dict = {\n",
        "    'monomer': ModelType.MONOMER,\n",
        "    'multimer': ModelType.MULTIMER,\n",
        "}\n",
        "\n",
        "model_type = model_type_dict[model_mode]\n",
        "print(f'Length of input sequence : {len(input_sequence.replace(\":\", \"\"))}')\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
95
96
        "#@markdown After making your selections, execute this cell by pressing the\n",
        "#@markdown *Play* button on the left."
jnwei's avatar
jnwei committed
97
      ]
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
98
    },
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
99
100
    {
      "cell_type": "code",
jnwei's avatar
jnwei committed
101
      "execution_count": null,
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
102
      "metadata": {
jnwei's avatar
jnwei committed
103
        "id": "woIxeCPygt7K"
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
104
      },
jnwei's avatar
jnwei committed
105
      "outputs": [],
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
106
107
      "source": [
        "#@title Install third-party software\n",
108
        "#@markdown Please execute this cell by pressing the *Play* button on\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
109
        "#@markdown the left.\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
110
111
        "\n",
        "\n",
112
        "#@markdown **Note**: This installs the software on the Colab\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
113
114
        "#@markdown notebook in the cloud and not on your computer.\n",
        "\n",
jnwei's avatar
jnwei committed
115
        "import os, time\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
116
        "from IPython.utils import io\n",
jnwei's avatar
jnwei committed
117
        "from sys import version_info\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
118
119
        "import subprocess\n",
        "\n",
jnwei's avatar
jnwei committed
120
        "python_version = f\"{version_info.major}.{version_info.minor}\"\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
121
        "\n",
jnwei's avatar
jnwei committed
122
123
124
125
        "\n",
        "os.system(\"wget -qnc https://github.com/conda-forge/miniforge/releases/latest/download/Mambaforge-Linux-x86_64.sh\")\n",
        "os.system(\"bash Mambaforge-Linux-x86_64.sh -bfp /usr/local\")\n",
        "os.system(\"mamba config --set auto_update_conda false\")\n",
126
127
        "os.system(f\"mamba install -y -c conda-forge -c bioconda kalign2=2.04 hhsuite=3.3.0 openmm=7.7.0 python={python_version} pdbfixer biopython=1.79\")\n",
        "os.system(\"pip install -q torch ml_collections py3Dmol modelcif\")\n",
128
        "\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
129
        "try:\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
130
131
132
        "  with io.capture_output() as captured:\n",
        "\n",
        "    # Create a ramdisk to store a database chunk to make Jackhmmer run fast.\n",
jnwei's avatar
jnwei committed
133
        "    %shell sudo apt install --quiet --yes hmmer\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
134
135
136
137
138
139
        "    %shell sudo mkdir -m 777 --parents /tmp/ramdisk\n",
        "    %shell sudo mount -t tmpfs -o size=9G ramdisk /tmp/ramdisk\n",
        "\n",
        "    %shell wget -q -P /content \\\n",
        "      https://git.scicore.unibas.ch/schwede/openstructure/-/raw/7102c63615b64735c4941278d92b554ec94415f8/modules/mol/alg/src/stereo_chemical_props.txt\n",
        "\n",
140
141
        "    %shell mkdir -p /content/openfold/openfold/resources\n",
        "\n",
142
        "    commit = \"a96ffd67f8c96f8c4decc3abdd2cffbb57fc5764\"\n",
jnwei's avatar
jnwei committed
143
        "    os.system(f\"pip install -q git+https://github.com/aqlaboratory/openfold.git@{commit}\")\n",
144
145
        "\n",
        "    os.system(f\"cp -f -p /content/stereo_chemical_props.txt /usr/local/lib/python{python_version}/site-packages/openfold/resources/\")\n",
jnwei's avatar
jnwei committed
146
        "\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
147
        "except subprocess.CalledProcessError as captured:\n",
jnwei's avatar
jnwei committed
148
149
        "  print(captured)"
      ]
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
150
151
152
    },
    {
      "cell_type": "code",
jnwei's avatar
jnwei committed
153
      "execution_count": null,
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
154
      "metadata": {
jnwei's avatar
jnwei committed
155
        "id": "VzJ5iMjTtoZw"
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
156
      },
jnwei's avatar
jnwei committed
157
      "outputs": [],
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
158
      "source": [
159
160
        "#@title Download model weights\n",
        "#@markdown Please execute this cell by pressing the *Play* button on\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
161
162
        "#@markdown the left.\n",
        "\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
163
164
        "# Define constants\n",
        "GIT_REPO='https://github.com/aqlaboratory/openfold'\n",
165
        "ALPHAFOLD_PARAM_SOURCE_URL = 'https://storage.googleapis.com/alphafold/alphafold_params_2022-12-06.tar'\n",
166
        "OPENFOLD_PARAMS_DIR = './openfold/openfold/resources/openfold_params'\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
167
168
169
170
        "ALPHAFOLD_PARAMS_DIR = './openfold/openfold/resources/params'\n",
        "ALPHAFOLD_PARAMS_PATH = os.path.join(\n",
        "  ALPHAFOLD_PARAMS_DIR, os.path.basename(ALPHAFOLD_PARAM_SOURCE_URL)\n",
        ")\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
171
172
        "\n",
        "try:\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
173
174
        "  with io.capture_output() as captured:\n",
        "    if(weight_set == 'AlphaFold'):\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
175
176
177
178
179
        "      %shell mkdir --parents \"{ALPHAFOLD_PARAMS_DIR}\"\n",
        "      %shell wget -O {ALPHAFOLD_PARAMS_PATH} {ALPHAFOLD_PARAM_SOURCE_URL}\n",
        "      %shell tar --extract --verbose --file=\"{ALPHAFOLD_PARAMS_PATH}\" \\\n",
        "        --directory=\"{ALPHAFOLD_PARAMS_DIR}\" --preserve-permissions\n",
        "      %shell rm \"{ALPHAFOLD_PARAMS_PATH}\"\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
180
        "    elif(weight_set == 'OpenFold'):\n",
jnwei's avatar
jnwei committed
181
182
183
184
185
186
        "      # Install AWS CLI\n",
        "      %shell curl \"https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip\" -o \"awscliv2.zip\"\n",
        "      %shell unzip -qq awscliv2.zip\n",
        "      %shell sudo ./aws/install\n",
        "      %shell rm awscliv2.zip\n",
        "      %shell rm -rf ./aws\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
187
        "      %shell mkdir --parents \"{OPENFOLD_PARAMS_DIR}\"\n",
jnwei's avatar
jnwei committed
188
        "\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
189
190
191
192
193
194
195
196
        "      %shell aws s3 cp \\\n",
        "        --no-sign-request \\\n",
        "        --region us-east-1 \\\n",
        "        s3://openfold/openfold_params \"{OPENFOLD_PARAMS_DIR}\" \\\n",
        "        --recursive\n",
        "    else:\n",
        "      raise ValueError(\"Invalid weight set\")\n",
        "except subprocess.CalledProcessError as captured:\n",
jnwei's avatar
jnwei committed
197
198
        "  print(captured)"
      ]
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
199
    },
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
200
201
    {
      "cell_type": "code",
jnwei's avatar
jnwei committed
202
      "execution_count": null,
jnwei's avatar
jnwei committed
203
      "metadata": {
204
205
206
207
208
209
        "id": "_FpxxMo-mvcP",
        "outputId": "e6d601db-0945-4091-91a1-2aba04ebeba7",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 492
        }
jnwei's avatar
jnwei committed
210
      },
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
      "outputs": [
        {
          "output_type": "stream",
          "name": "stderr",
          "text": [
            "/usr/local/lib/python3.10/site-packages/openfold/data/templates.py:86: FutureWarning: In the future `np.object` will be defined as the corresponding NumPy scalar.\n",
            "  \"template_domain_names\": np.object,\n"
          ]
        },
        {
          "output_type": "error",
          "ename": "AttributeError",
          "evalue": "module 'numpy' has no attribute 'object'.\n`np.object` was a deprecated alias for the builtin `object`. To avoid this error in existing code, use `object` by itself. Doing this will not modify any behavior and is safe. \nThe aliases was originally deprecated in NumPy 1.20; for more details and guidance see the original release note at:\n    https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations",
          "traceback": [
            "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
            "\u001b[0;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
            "\u001b[0;32m<ipython-input-6-24dea6794813>\u001b[0m in \u001b[0;36m<cell line: 50>\u001b[0;34m()\u001b[0m\n\u001b[1;32m     48\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mopenfold\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdata\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mfeature_pipeline\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     49\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mopenfold\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdata\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mparsers\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 50\u001b[0;31m \u001b[0;32mfrom\u001b[0m \u001b[0mopenfold\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdata\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mdata_pipeline\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     51\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mopenfold\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdata\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mmsa_pairing\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     52\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mopenfold\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdata\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mfeature_processing_multimer\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
            "\u001b[0;32m/usr/local/lib/python3.10/site-packages/openfold/data/data_pipeline.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m     26\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mtorch\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     27\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mpickle\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 28\u001b[0;31m \u001b[0;32mfrom\u001b[0m \u001b[0mopenfold\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdata\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mtemplates\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mparsers\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmmcif_parsing\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmsa_identifiers\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmsa_pairing\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfeature_processing_multimer\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     29\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mopenfold\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdata\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtemplates\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mget_custom_template_features\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mempty_template_feats\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     30\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mopenfold\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdata\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtools\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mjackhmmer\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mhhblits\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mhhsearch\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mhmmsearch\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
            "\u001b[0;32m/usr/local/lib/python3.10/site-packages/openfold/data/templates.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m     84\u001b[0m     \u001b[0;34m\"template_all_atom_mask\"\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfloat32\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     85\u001b[0m     \u001b[0;34m\"template_all_atom_positions\"\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfloat32\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 86\u001b[0;31m     \u001b[0;34m\"template_domain_names\"\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mobject\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     87\u001b[0m     \u001b[0;34m\"template_sequence\"\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mobject\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     88\u001b[0m     \u001b[0;34m\"template_sum_probs\"\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfloat32\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
            "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/numpy/__init__.py\u001b[0m in \u001b[0;36m__getattr__\u001b[0;34m(attr)\u001b[0m\n\u001b[1;32m    317\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    318\u001b[0m         \u001b[0;32mif\u001b[0m \u001b[0mattr\u001b[0m \u001b[0;32min\u001b[0m \u001b[0m__former_attrs__\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 319\u001b[0;31m             \u001b[0;32mraise\u001b[0m \u001b[0mAttributeError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0m__former_attrs__\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mattr\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    320\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    321\u001b[0m         \u001b[0;32mif\u001b[0m \u001b[0mattr\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;34m'testing'\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
            "\u001b[0;31mAttributeError\u001b[0m: module 'numpy' has no attribute 'object'.\n`np.object` was a deprecated alias for the builtin `object`. To avoid this error in existing code, use `object` by itself. Doing this will not modify any behavior and is safe. \nThe aliases was originally deprecated in NumPy 1.20; for more details and guidance see the original release note at:\n    https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations"
          ]
        }
      ],
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
235
236
      "source": [
        "#@title Import Python packages\n",
237
        "#@markdown Please execute this cell by pressing the *Play* button on\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
238
239
        "#@markdown the left.\n",
        "\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
240
        "import unittest.mock\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
241
        "import sys\n",
242
        "from typing import Dict, Sequence\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
243
        "\n",
jnwei's avatar
jnwei committed
244
        "sys.path.insert(0, f'/usr/local/lib/python{python_version}/dist-packages/')\n",
245
        "sys.path.insert(0, f'/usr/local/lib/python{python_version}/site-packages/')\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
246
247
248
249
250
251
252
253
254
255
256
257
        "\n",
        "# Allows us to skip installing these packages\n",
        "unnecessary_modules = [\n",
        "  \"dllogger\",\n",
        "  \"pytorch_lightning\",\n",
        "  \"pytorch_lightning.utilities\",\n",
        "  \"pytorch_lightning.callbacks.early_stopping\",\n",
        "  \"pytorch_lightning.utilities.seed\",\n",
        "]\n",
        "for unnecessary_module in unnecessary_modules:\n",
        "  sys.modules[unnecessary_module] = unittest.mock.MagicMock()\n",
        "\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
258
259
260
261
262
263
264
265
266
267
268
        "import os\n",
        "\n",
        "from urllib import request\n",
        "from concurrent import futures\n",
        "from google.colab import files\n",
        "import json\n",
        "from matplotlib import gridspec\n",
        "import matplotlib.pyplot as plt\n",
        "import numpy as np\n",
        "import py3Dmol\n",
        "import torch\n",
269
        "import shutil\n",
jnwei's avatar
jnwei committed
270
271
272
273
        "import tqdm\n",
        "import tqdm.notebook\n",
        "\n",
        "TQDM_BAR_FORMAT = '{l_bar}{bar}| {n_fmt}/{total_fmt} [elapsed: {elapsed} remaining: {remaining}]'\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
274
        "\n",
275
276
277
278
279
280
281
        "# Prevent shell magic being broken by openmm, prevent this cryptic error:\n",
        "# \"NotImplementedError: A UTF-8 locale is required. Got ANSI_X3.4-1968\"\n",
        "import locale\n",
        "def getpreferredencoding(do_setlocale = True):\n",
        "    return \"UTF-8\"\n",
        "locale.getpreferredencoding = getpreferredencoding\n",
        "\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
282
283
284
285
        "from openfold import config\n",
        "from openfold.data import feature_pipeline\n",
        "from openfold.data import parsers\n",
        "from openfold.data import data_pipeline\n",
286
287
        "from openfold.data import msa_pairing\n",
        "from openfold.data import feature_processing_multimer\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
288
289
290
        "from openfold.data.tools import jackhmmer\n",
        "from openfold.model import model\n",
        "from openfold.np import protein\n",
291
        "from openfold.np.relax import relax\n",
292
        "from openfold.np.relax.utils import overwrite_b_factors\n",
293
        "from openfold.utils.import_weights import import_jax_weights_, import_openfold_weights_\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
294
295
296
297
298
        "from openfold.utils.tensor_utils import tensor_tree_map\n",
        "\n",
        "from IPython import display\n",
        "from ipywidgets import GridspecLayout\n",
        "from ipywidgets import Output"
jnwei's avatar
jnwei committed
299
      ]
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
300
    },
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "W4JpOs6oA-QS"
      },
      "source": [
        "## Making a prediction\n",
        "\n",
        "Note that the search against databases and the actual prediction can take some time, from minutes to hours, depending on the length of the protein and what type of GPU you are allocated by Colab (see FAQ below)."
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "#@title Search against genetic databases\n",
        "\n",
        "#@markdown Once this cell has been executed, you will see\n",
318
319
320
        "#@markdown statistics about the multiple sequence alignment\n",
        "#@markdown (MSA) that will be used by OpenFold. In particular,\n",
        "#@markdown you’ll see how well each residue is covered by similar\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
321
322
        "#@markdown sequences in the MSA.\n",
        "\n",
323
        "# --- Find the closest source --\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
        "test_url_pattern = 'https://storage.googleapis.com/alphafold-colab{:s}/latest/uniref90_2021_03.fasta.1'\n",
        "ex = futures.ThreadPoolExecutor(3)\n",
        "def fetch(source):\n",
        "  request.urlretrieve(test_url_pattern.format(source))\n",
        "  return source\n",
        "fs = [ex.submit(fetch, source) for source in ['', '-europe', '-asia']]\n",
        "source = None\n",
        "for f in futures.as_completed(fs):\n",
        "  source = f.result()\n",
        "  ex.shutdown()\n",
        "  break\n",
        "\n",
        "# Run the search against chunks of genetic databases (since the genetic\n",
        "# databases don't fit in Colab ramdisk).\n",
        "\n",
        "jackhmmer_binary_path = '/usr/bin/jackhmmer'\n",
        "\n",
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
        "# --- Parse multiple sequences, if there are any ---\n",
        "def split_multiple_sequences(sequence):\n",
        "  seqs = sequence.split(':')\n",
        "  sorted_seqs = sorted(seqs, key=lambda s: len(s))\n",
        "\n",
        "  # TODO: Handle the homomer case when writing fasta sequences\n",
        "  fasta_path_tuples = []\n",
        "  for idx, seq in enumerate(set(sorted_seqs)):\n",
        "    fasta_path = f'target_{idx+1}.fasta'\n",
        "    with open(fasta_path, 'wt') as f:\n",
        "      f.write(f'>query\\n{seq}\\n')\n",
        "    fasta_path_tuples.append((seq, fasta_path))\n",
        "  fasta_path_by_seq = dict(fasta_path_tuples)\n",
        "\n",
        "  return sorted_seqs, fasta_path_by_seq\n",
        "\n",
        "sequences, fasta_path_by_sequence = split_multiple_sequences(input_sequence)\n",
        "db_results_by_sequence = {seq: {} for seq in fasta_path_by_sequence.keys()}\n",
        "\n",
        "DB_ROOT_PATH = f'https://storage.googleapis.com/alphafold-colab{source}/latest/'\n",
        "db_configs = {}\n",
        "db_configs['smallbfd'] = {\n",
        "    'database_path': f'{DB_ROOT_PATH}uniref90_2021_03.fasta',\n",
        "    'z_value': 65984053,\n",
        "    'num_jackhmmer_chunks': 17,\n",
        "}\n",
        "db_configs['mgnify'] = {\n",
        "    'database_path': f'{DB_ROOT_PATH}mgy_clusters_2022_05.fasta',\n",
        "    'z_value': 304820129,\n",
        "    'num_jackhmmer_chunks': 120,\n",
        "}\n",
        "db_configs['uniref90'] = {\n",
        "     'database_path': f'{DB_ROOT_PATH}uniref90_2022_01.fasta',\n",
        "     'z_value': 144113457,\n",
        "     'num_jackhmmer_chunks': 62,\n",
        "}\n",
        "\n",
        "# Search UniProt and construct the all_seq features only for heteromers, not homomers.\n",
        "if model_type == ModelType.MULTIMER and len(set(sequences)) > 1:\n",
        "  db_configs['uniprot'] = {\n",
        "       'database_path': f'{DB_ROOT_PATH}uniprot_2021_04.fasta',\n",
        "       'z_value': 225013025 + 565928,\n",
        "       'num_jackhmmer_chunks': 101,\n",
        "       }\n",
        "\n",
        "total_jackhmmer_chunks = sum([d['num_jackhmmer_chunks'] for d in db_configs.values()])\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
387
388
389
390
        "with tqdm.notebook.tqdm(total=total_jackhmmer_chunks, bar_format=TQDM_BAR_FORMAT) as pbar:\n",
        "  def jackhmmer_chunk_callback(i):\n",
        "    pbar.update(n=1)\n",
        "\n",
391
392
393
394
395
396
397
398
399
400
401
402
403
        "  for db_name, db_config in db_configs.items():\n",
        "    pbar.set_description(f'Searching {db_name}')\n",
        "    jackhmmer_runner = jackhmmer.Jackhmmer(\n",
        "        binary_path=jackhmmer_binary_path,\n",
        "        database_path=db_config['database_path'],\n",
        "        get_tblout=True,\n",
        "        num_streamed_chunks=db_config['num_jackhmmer_chunks'],\n",
        "        streaming_callback=jackhmmer_chunk_callback,\n",
        "        z_value=db_config['z_value'])\n",
        "\n",
        "    db_results = jackhmmer_runner.query_multiple(fasta_path_by_sequence.values())\n",
        "    for seq, result in zip(fasta_path_by_sequence.keys(), db_results):\n",
        "      db_results_by_sequence[seq][db_name] = result\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
404
405
406
407
408
409
        "\n",
        "\n",
        "# --- Extract the MSAs and visualize ---\n",
        "# Extract the MSAs from the Stockholm files.\n",
        "# NB: deduplication happens later in data_pipeline.make_msa_features.\n",
        "\n",
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
        "MAX_HITS_BY_DB = {\n",
        "    'uniref90': 10000,\n",
        "    'smallbfd': 5000,\n",
        "    'mgnify': 501,\n",
        "    'uniprot': 50000,\n",
        "}\n",
        "\n",
        "msas_by_seq_by_db = {seq: {} for seq in sequences}\n",
        "full_msa_by_seq = {seq: [] for seq in sequences}\n",
        "\n",
        "for seq, sequence_result in db_results_by_sequence.items():\n",
        "  print(f'parsing_results_for_sequence {seq}')\n",
        "  for db_name, db_results in sequence_result.items():\n",
        "    unsorted_results = []\n",
        "    for i, result in enumerate(db_results):\n",
        "      msa_obj = parsers.parse_stockholm(result['sto'])\n",
        "      e_values_dict = parsers.parse_e_values_from_tblout(result['tbl'])\n",
        "      target_names = msa_obj.descriptions\n",
        "      e_values = [e_values_dict[t.split('/')[0]] for t in target_names]\n",
        "      zipped_results = zip(msa_obj.sequences, msa_obj.deletion_matrix, target_names, e_values)\n",
        "      if i != 0:\n",
        "        # Only take query from the first chunk\n",
        "        zipped_results = [x for x in zipped_results if x[2] != 'query']\n",
        "      unsorted_results.extend(zipped_results)\n",
        "    sorted_by_evalue = sorted(unsorted_results, key=lambda x: x[3])\n",
        "    msas, del_matrix, targets, _ = zip(*sorted_by_evalue)\n",
        "    db_msas = parsers.Msa(msas, del_matrix, targets)\n",
        "    if db_msas:\n",
        "      if db_name in MAX_HITS_BY_DB:\n",
        "        db_msas.truncate(MAX_HITS_BY_DB[db_name])\n",
        "      msas_by_seq_by_db[seq][db_name] = db_msas\n",
        "      full_msa_by_seq[seq].extend(db_msas.sequences)\n",
        "      msa_size = len(set(db_msas.sequences))\n",
        "      print(f'{msa_size} Sequences Found in {db_name}')\n",
        "\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
445
446
        "\n",
        "fig = plt.figure(figsize=(12, 3))\n",
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
        "max_num_alignments = 0\n",
        "\n",
        "for seq_idx, seq in enumerate(set(sequences)):\n",
        "  full_msas = full_msa_by_seq[seq]\n",
        "  deduped_full_msa = list(dict.fromkeys(full_msas))\n",
        "  total_msa_size = len(deduped_full_msa)\n",
        "  print(f'\\n{total_msa_size} Sequences Found in Total\\n')\n",
        "\n",
        "  aa_map = {restype: i for i, restype in enumerate('ABCDEFGHIJKLMNOPQRSTUVWXYZ-')}\n",
        "  msa_arr = np.array([[aa_map[aa] for aa in seq] for seq in deduped_full_msa])\n",
        "  num_alignments, num_res = msa_arr.shape\n",
        "  plt.plot(np.sum(msa_arr != aa_map['-'], axis=0), label=f'Chain {seq_idx}')\n",
        "  max_num_alignments = max(num_alignments, max_num_alignments)\n",
        "\n",
        "\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
462
463
        "plt.title('Per-Residue Count of Non-Gap Amino Acids in the MSA')\n",
        "plt.ylabel('Non-Gap Count')\n",
464
465
        "plt.yticks(range(0, max_num_alignments + 1, max(1, int(max_num_alignments / 3))))\n",
        "plt.legend()\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
466
        "plt.show()"
467
468
469
470
471
472
      ],
      "metadata": {
        "id": "o7BqQN_gfYtq"
      },
      "execution_count": null,
      "outputs": []
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
473
474
475
    },
    {
      "cell_type": "code",
jnwei's avatar
jnwei committed
476
      "execution_count": null,
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
477
      "metadata": {
jnwei's avatar
jnwei committed
478
        "id": "XUo6foMQxwS2"
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
479
      },
jnwei's avatar
jnwei committed
480
      "outputs": [],
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
481
482
483
      "source": [
        "#@title Run OpenFold and download prediction\n",
        "\n",
484
485
        "#@markdown Once this cell has been executed, a zip-archive with\n",
        "#@markdown the obtained prediction will be automatically downloaded\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
486
487
        "#@markdown to your computer.\n",
        "\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
488
489
490
491
492
493
494
495
        "# Color bands for visualizing plddt\n",
        "PLDDT_BANDS = [\n",
        "  (0, 50, '#FF7D45'),\n",
        "  (50, 70, '#FFDB13'),\n",
        "  (70, 90, '#65CBF3'),\n",
        "  (90, 100, '#0053D6')\n",
        "]\n",
        "\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
496
        "# --- Run the model ---\n",
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
        "if model_type == ModelType.MONOMER:\n",
        "  model_names = [\n",
        "   'finetuning_3.pt',\n",
        "   'finetuning_4.pt',\n",
        "   'finetuning_5.pt',\n",
        "   'finetuning_ptm_2.pt',\n",
        "   'finetuning_no_templ_ptm_1.pt'\n",
        "  ]\n",
        "elif model_type == ModelType.MULTIMER:\n",
        "  model_names = [\n",
        "    'model_1_multimer_v3',\n",
        "    'model_2_multimer_v3',\n",
        "    'model_3_multimer_v3',\n",
        "    'model_4_multimer_v3',\n",
        "    'model_5_multimer_v3',\n",
        "  ]\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
513
514
515
        "\n",
        "def _placeholder_template_feats(num_templates_, num_res_):\n",
        "  return {\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
516
517
518
519
520
        "      'template_aatype': np.zeros((num_templates_, num_res_, 22), dtype=np.int64),\n",
        "      'template_all_atom_positions': np.zeros((num_templates_, num_res_, 37, 3), dtype=np.float32),\n",
        "      'template_all_atom_mask': np.zeros((num_templates_, num_res_, 37), dtype=np.float32),\n",
        "      'template_domain_names': np.zeros((num_templates_,), dtype=np.float32),\n",
        "      'template_sum_probs': np.zeros((num_templates_, 1), dtype=np.float32),\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
521
522
        "  }\n",
        "\n",
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
        "\n",
        "def make_features(\n",
        "    sequences: Sequence[str],\n",
        "    msas_by_seq_by_db: Dict[str, Dict[str, parsers.Msa]],\n",
        "    model_type: ModelType):\n",
        "  num_templates = 1 # Placeholder for generating fake template features\n",
        "  feature_dict = {}\n",
        "\n",
        "  for idx, seq in enumerate(sequences, start=1):\n",
        "    _chain_id = f'chain_{idx}'\n",
        "    num_res = len(seq)\n",
        "\n",
        "    feats = data_pipeline.make_sequence_features(seq, _chain_id, num_res)\n",
        "    msas_without_uniprot = [msas_by_seq_by_db[seq][db] for db in db_configs.keys() if db != 'uniprot']\n",
        "    msa_feats = data_pipeline.make_msa_features(msas_without_uniprot)\n",
        "    feats.update(msa_feats)\n",
        "    feats.update(_placeholder_template_feats(num_templates, num_res))\n",
        "\n",
        "    if model_type == ModelType.MONOMER:\n",
        "      feature_dict[seq] = feats\n",
        "    if model_type == ModelType.MULTIMER:\n",
        "      # Perform extra pair processing steps for heteromers\n",
        "      if len(set(sequences)) > 1:\n",
        "        uniprot_msa = msas_by_seq_by_db[seq]['uniprot']\n",
        "        uniprot_msa_features = data_pipeline.make_msa_features([uniprot_msa])\n",
        "        valid_feat_names = msa_pairing.MSA_FEATURES + (\n",
        "                'msa_species_identifiers',\n",
        "            )\n",
        "        pair_feats = {\n",
        "                f'{k}_all_seq': v for k, v in uniprot_msa_features.items()\n",
        "                if k in valid_feat_names\n",
        "            }\n",
        "        feats.update(pair_feats)\n",
        "\n",
        "      feats = data_pipeline.convert_monomer_features(feats, _chain_id)\n",
        "      feature_dict[_chain_id] = feats\n",
        "\n",
        "  if model_type == ModelType.MONOMER:\n",
        "    np_example = feature_dict[sequences[0]]\n",
        "  elif model_type == ModelType.MULTIMER:\n",
        "    all_chain_feats = data_pipeline.add_assembly_features(feature_dict)\n",
        "    features = feature_processing_multimer.pair_and_merge(all_chain_features=all_chain_feats)\n",
        "    np_example = data_pipeline.pad_msa(features, 512)\n",
        "\n",
        "  return np_example\n",
        "\n",
        "\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
570
571
572
573
574
        "output_dir = 'prediction'\n",
        "os.makedirs(output_dir, exist_ok=True)\n",
        "\n",
        "plddts = {}\n",
        "pae_outputs = {}\n",
575
        "weighted_ptms = {}\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
576
577
        "unrelaxed_proteins = {}\n",
        "\n",
578
579
        "with tqdm.notebook.tqdm(total=len(model_names), bar_format=TQDM_BAR_FORMAT) as pbar:\n",
        "  for i, model_name in enumerate(model_names, start = 1):\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
580
        "    pbar.set_description(f'Running {model_name}')\n",
581
582
        "\n",
        "    feature_dict = make_features(sequences, msas_by_seq_by_db, model_type)\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
583
        "\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
584
        "    if(weight_set == \"AlphaFold\"):\n",
585
586
587
588
        "      if model_type == ModelType.MONOMER:\n",
        "        config_preset = f\"model_{i}\"\n",
        "      elif model_type == ModelType.MULTIMER:\n",
        "        config_preset = f'model_{i}_multimer_v3'\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
589
        "    else:\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
590
591
592
593
        "      if(\"_no_templ_\" in model_name):\n",
        "        config_preset = \"model_3\"\n",
        "      else:\n",
        "        config_preset = \"model_1\"\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
594
595
        "      if(\"_ptm_\" in model_name):\n",
        "        config_preset += \"_ptm\"\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
596
597
        "\n",
        "    cfg = config.model_config(config_preset)\n",
598
599
600
601
602
        "\n",
        "    # Force the model to only use 3 recycling updates\n",
        "    cfg.data.common.max_recycling_iters = 3\n",
        "    cfg.model.recycle_early_stop_tolerance = -1\n",
        "\n",
603
        "    openfold_model = model.AlphaFold(cfg)\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
604
        "    openfold_model = openfold_model.eval()\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
605
        "    if(weight_set == \"AlphaFold\"):\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
606
607
608
609
        "      params_name = os.path.join(\n",
        "        ALPHAFOLD_PARAMS_DIR, f\"params_{config_preset}.npz\"\n",
        "      )\n",
        "      import_jax_weights_(openfold_model, params_name, version=config_preset)\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
610
611
612
        "    elif(weight_set == \"OpenFold\"):\n",
        "      params_name = os.path.join(\n",
        "        OPENFOLD_PARAMS_DIR,\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
613
        "        model_name,\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
614
615
        "      )\n",
        "      d = torch.load(params_name)\n",
616
        "      import_openfold_weights_(model=openfold_model, state_dict=d)\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
617
618
619
        "    else:\n",
        "      raise ValueError(f\"Invalid weight set: {weight_set}\")\n",
        "\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
620
621
        "    openfold_model = openfold_model.cuda()\n",
        "\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
622
        "    pipeline = feature_pipeline.FeaturePipeline(cfg.data)\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
623
        "    processed_feature_dict = pipeline.process_features(\n",
624
625
626
        "      feature_dict,\n",
        "      mode='predict',\n",
        "      is_multimer = (model_type == ModelType.MULTIMER),\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
627
628
        "    )\n",
        "\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
629
630
631
        "    processed_feature_dict = tensor_tree_map(\n",
        "        lambda t: t.cuda(), processed_feature_dict\n",
        "    )\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
632
633
634
635
636
637
638
639
640
641
642
643
644
645
        "\n",
        "    with torch.no_grad():\n",
        "      prediction_result = openfold_model(processed_feature_dict)\n",
        "\n",
        "    # Move the batch and output to np for further processing\n",
        "    processed_feature_dict = tensor_tree_map(\n",
        "      lambda t: np.array(t[..., -1].cpu()), processed_feature_dict\n",
        "    )\n",
        "    prediction_result = tensor_tree_map(\n",
        "      lambda t: np.array(t.cpu()), prediction_result\n",
        "    )\n",
        "\n",
        "    mean_plddt = prediction_result['plddt'].mean()\n",
        "\n",
646
647
648
649
650
651
652
653
654
655
656
657
        "    if model_type == ModelType.MONOMER:\n",
        "      if 'predicted_aligned_error' in prediction_result:\n",
        "        pae_outputs[model_name] = (\n",
        "            prediction_result['predicted_aligned_error'],\n",
        "            prediction_result['max_predicted_aligned_error']\n",
        "        )\n",
        "      else:\n",
        "        # Get the pLDDT confidence metrics. Do not put pTM models here as they\n",
        "        # should never get selected.\n",
        "        plddts[model_name] = prediction_result['plddt']\n",
        "    elif model_type == ModelType.MULTIMER:\n",
        "      # Multimer models are sorted by pTM+ipTM.\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
658
        "      plddts[model_name] = prediction_result['plddt']\n",
659
660
661
662
        "      pae_outputs[model_name] = (prediction_result['predicted_aligned_error'],\n",
        "                                 prediction_result['max_predicted_aligned_error'])\n",
        "\n",
        "      weighted_ptms[model_name] = prediction_result['weighted_ptm_score']\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
663
664
665
666
667
        "\n",
        "    # Set the b-factors to the per-residue plddt.\n",
        "    final_atom_mask = prediction_result['final_atom_mask']\n",
        "    b_factors = prediction_result['plddt'][:, None] * final_atom_mask\n",
        "    unrelaxed_protein = protein.from_prediction(\n",
668
669
670
671
        "      processed_feature_dict,\n",
        "      prediction_result,\n",
        "      remove_leading_feature_dimension=False,\n",
        "      b_factors=b_factors,\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
672
673
674
675
676
677
678
679
680
681
        "    )\n",
        "    unrelaxed_proteins[model_name] = unrelaxed_protein\n",
        "\n",
        "    # Delete unused outputs to save memory.\n",
        "    del openfold_model\n",
        "    del processed_feature_dict\n",
        "    del prediction_result\n",
        "    pbar.update(n=1)\n",
        "\n",
        "  # Find the best model according to the mean pLDDT.\n",
682
683
684
685
        "  if model_type == ModelType.MONOMER:\n",
        "    best_model_name = max(plddts.keys(), key=lambda x: plddts[x].mean())\n",
        "  elif model_type == ModelType.MULTIMER:\n",
        "    best_model_name = max(weighted_ptms.keys(), key=lambda x: weighted_ptms[x])\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
686
687
688
689
690
691
692
693
694
695
696
        "  best_pdb = protein.to_pdb(unrelaxed_proteins[best_model_name])\n",
        "\n",
        "  # --- AMBER relax the best model ---\n",
        "  if(relax_prediction):\n",
        "    pbar.set_description(f'AMBER relaxation')\n",
        "    amber_relaxer = relax.AmberRelaxation(\n",
        "        max_iterations=0,\n",
        "        tolerance=2.39,\n",
        "        stiffness=10.0,\n",
        "        exclude_residues=[],\n",
        "        max_outer_iterations=20,\n",
697
        "        use_gpu=True,\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
698
699
700
701
702
703
        "    )\n",
        "    relaxed_pdb, _, _ = amber_relaxer.process(\n",
        "        prot=unrelaxed_proteins[best_model_name]\n",
        "    )\n",
        "    best_pdb = relaxed_pdb\n",
        "\n",
704
705
706
707
708
        "  # Write out the prediction\n",
        "  pred_output_path = os.path.join(output_dir, 'selected_prediction.pdb')\n",
        "  with open(pred_output_path, 'w') as f:\n",
        "    f.write(best_pdb)\n",
        "\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
709
710
711
712
713
714
715
716
717
718
719
        "  pbar.update(n=1)  # Finished AMBER relax.\n",
        "\n",
        "# Construct multiclass b-factors to indicate confidence bands\n",
        "# 0=very low, 1=low, 2=confident, 3=very high\n",
        "banded_b_factors = []\n",
        "for plddt in plddts[best_model_name]:\n",
        "  for idx, (min_val, max_val, _) in enumerate(PLDDT_BANDS):\n",
        "    if plddt >= min_val and plddt <= max_val:\n",
        "      banded_b_factors.append(idx)\n",
        "      break\n",
        "banded_b_factors = np.array(banded_b_factors)[:, None] * final_atom_mask\n",
720
        "to_visualize_pdb = overwrite_b_factors(best_pdb, banded_b_factors)\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
        "\n",
        "# --- Visualise the prediction & confidence ---\n",
        "show_sidechains = True\n",
        "def plot_plddt_legend():\n",
        "  \"\"\"Plots the legend for pLDDT.\"\"\"\n",
        "  thresh = [\n",
        "            'Very low (pLDDT < 50)',\n",
        "            'Low (70 > pLDDT > 50)',\n",
        "            'Confident (90 > pLDDT > 70)',\n",
        "            'Very high (pLDDT > 90)']\n",
        "\n",
        "  colors = [x[2] for x in PLDDT_BANDS]\n",
        "\n",
        "  plt.figure(figsize=(2, 2))\n",
        "  for c in colors:\n",
        "    plt.bar(0, 0, color=c)\n",
        "  plt.legend(thresh, frameon=False, loc='center', fontsize=20)\n",
        "  plt.xticks([])\n",
        "  plt.yticks([])\n",
        "  ax = plt.gca()\n",
        "  ax.spines['right'].set_visible(False)\n",
        "  ax.spines['top'].set_visible(False)\n",
        "  ax.spines['left'].set_visible(False)\n",
        "  ax.spines['bottom'].set_visible(False)\n",
        "  plt.title('Model Confidence', fontsize=20, pad=20)\n",
        "  return plt\n",
        "\n",
748
749
750
751
752
753
754
755
756
        "# Show the structure coloured by chain if the multimer model has been used.\n",
        "if model_type == ModelType.MULTIMER:\n",
        "  multichain_view = py3Dmol.view(width=800, height=600)\n",
        "  multichain_view.addModelsAsFrames(to_visualize_pdb)\n",
        "  multichain_style = {'cartoon': {'colorscheme': 'chain'}}\n",
        "  multichain_view.setStyle({'model': -1}, multichain_style)\n",
        "  multichain_view.zoomTo()\n",
        "  multichain_view.show()\n",
        "\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
        "# Color the structure by per-residue pLDDT\n",
        "color_map = {i: bands[2] for i, bands in enumerate(PLDDT_BANDS)}\n",
        "view = py3Dmol.view(width=800, height=600)\n",
        "view.addModelsAsFrames(to_visualize_pdb)\n",
        "style = {'cartoon': {\n",
        "    'colorscheme': {\n",
        "        'prop': 'b',\n",
        "        'map': color_map}\n",
        "        }}\n",
        "if show_sidechains:\n",
        "  style['stick'] = {}\n",
        "view.setStyle({'model': -1}, style)\n",
        "view.zoomTo()\n",
        "\n",
        "grid = GridspecLayout(1, 2)\n",
        "out = Output()\n",
        "with out:\n",
        "  view.show()\n",
        "grid[0, 0] = out\n",
        "\n",
        "out = Output()\n",
        "with out:\n",
        "  plot_plddt_legend().show()\n",
        "grid[0, 1] = out\n",
        "\n",
        "display.display(grid)\n",
        "\n",
        "# Display pLDDT and predicted aligned error (if output by the model).\n",
        "if pae_outputs:\n",
        "  num_plots = 2\n",
        "else:\n",
        "  num_plots = 1\n",
        "\n",
        "plt.figure(figsize=[8 * num_plots, 6])\n",
        "plt.subplot(1, num_plots, 1)\n",
        "plt.plot(plddts[best_model_name])\n",
        "plt.title('Predicted LDDT')\n",
        "plt.xlabel('Residue')\n",
        "plt.ylabel('pLDDT')\n",
        "\n",
        "if num_plots == 2:\n",
        "  plt.subplot(1, 2, 2)\n",
        "  pae, max_pae = list(pae_outputs.values())[0]\n",
        "  plt.imshow(pae, vmin=0., vmax=max_pae, cmap='Greens_r')\n",
        "  plt.colorbar(fraction=0.046, pad=0.04)\n",
802
803
804
805
806
807
808
809
810
        "\n",
        "  # Display lines at chain boundaries.\n",
        "  best_unrelaxed_prot = unrelaxed_proteins[best_model_name]\n",
        "  total_num_res = best_unrelaxed_prot.residue_index.shape[-1]\n",
        "  chain_ids = best_unrelaxed_prot.chain_index\n",
        "  for chain_boundary in np.nonzero(chain_ids[:-1] - chain_ids[1:]):\n",
        "    if chain_boundary.size:\n",
        "      plt.plot([0, total_num_res], [chain_boundary, chain_boundary], color='red')\n",
        "      plt.plot([chain_boundary, chain_boundary], [0, total_num_res], color='red')\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
        "  plt.title('Predicted Aligned Error')\n",
        "  plt.xlabel('Scored residue')\n",
        "  plt.ylabel('Aligned residue')\n",
        "\n",
        "# Save pLDDT and predicted aligned error (if it exists)\n",
        "pae_output_path = os.path.join(output_dir, 'predicted_aligned_error.json')\n",
        "if pae_outputs:\n",
        "  # Save predicted aligned error in the same format as the AF EMBL DB\n",
        "  rounded_errors = np.round(pae.astype(np.float64), decimals=1)\n",
        "  indices = np.indices((len(rounded_errors), len(rounded_errors))) + 1\n",
        "  indices_1 = indices[0].flatten().tolist()\n",
        "  indices_2 = indices[1].flatten().tolist()\n",
        "  pae_data = json.dumps([{\n",
        "      'residue1': indices_1,\n",
        "      'residue2': indices_2,\n",
        "      'distance': rounded_errors.flatten().tolist(),\n",
        "      'max_predicted_aligned_error': max_pae.item()\n",
        "  }],\n",
        "                        indent=None,\n",
        "                        separators=(',', ':'))\n",
        "  with open(pae_output_path, 'w') as f:\n",
        "    f.write(pae_data)\n",
        "\n",
        "\n",
        "# --- Download the predictions ---\n",
836
        "shutil.make_archive(base_name='prediction', format='zip', root_dir=output_dir)\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
837
        "files.download(f'{output_dir}.zip')"
jnwei's avatar
jnwei committed
838
      ]
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
839
840
841
842
843
844
845
846
847
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "lUQAn5LYC5n4"
      },
      "source": [
        "### Interpreting the prediction\n",
        "\n",
848
        "Please see the [AlphaFold methods paper](https://www.nature.com/articles/s41586-021-03819-2) and the [AlphaFold predictions of the human proteome paper](https://www.nature.com/articles/s41586-021-03828-1), as well as [DeepMind's FAQ](https://alphafold.ebi.ac.uk/faq) on how to interpret AlphaFold/OpenFold predictions. More information about the predictions of the AlphaFold Multimer model can be found in the [Alphafold Multimer paper](https://www.biorxiv.org/content/10.1101/2022.03.11.484043v3.full.pdf)."
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "jeb2z8DIA4om"
      },
      "source": [
        "## FAQ & Troubleshooting\n",
        "\n",
        "\n",
        "*   How do I get a predicted protein structure for my protein?\n",
        "    *   Click on the _Connect_ button on the top right to get started.\n",
        "    *   Paste the amino acid sequence of your protein (without any headers) into the “Enter the amino acid sequence to fold”.\n",
        "    *   Run all cells in the Colab, either by running them individually (with the play button on the left side) or via _Runtime_ > _Run all._\n",
        "    *   The predicted protein structure will be downloaded once all cells have been executed. Note: This can take minutes to hours - see below.\n",
        "*   How long will this take?\n",
        "    *   Downloading the OpenFold source code can take up to a few minutes.\n",
        "    *   Downloading and installing the third-party software can take up to a few minutes.\n",
        "    *   The search against genetic databases can take minutes to hours.\n",
        "    *   Running OpenFold and generating the prediction can take minutes to hours, depending on the length of your protein and on which GPU-type Colab has assigned you.\n",
        "*   My Colab no longer seems to be doing anything, what should I do?\n",
        "    *   Some steps may take minutes to hours to complete.\n",
        "    *   Sometimes, running the \"installation\" cells more than once can corrupt the OpenFold installation.\n",
        "    *   If nothing happens or if you receive an error message, try restarting your Colab runtime via _Runtime_ > _Restart runtime_.\n",
        "    *   If this doesn’t help, reset your Colab runtime via _Runtime_ > _Factory reset runtime_.\n",
        "*   How does what's run in this notebook compare to the full versions of Alphafold/Openfold?\n",
        "    *   This Colab version of OpenFold searches a selected portion of the BFD dataset and currently doesn’t use templates, so its accuracy is reduced in comparison to the full version, which is analogous to what's described in the [AlphaFold paper](https://doi.org/10.1038/s41586-021-03819-2) and [Github repo](https://github.com/deepmind/alphafold/). The full version of OpenFold can be run from our own [GitHub repo](https://github.com/aqlaboratory/openfold).\n",
        "*   What is a Colab?\n",
        "    *   See the [Colab FAQ](https://research.google.com/colaboratory/faq.html).\n",
        "*   I received a warning “Notebook requires high RAM”, what do I do?\n",
        "    *   The resources allocated to your Colab vary. See the [Colab FAQ](https://research.google.com/colaboratory/faq.html) for more details.\n",
        "    *   You can execute the Colab nonetheless.\n",
        "*   I received an error “Colab CPU runtime not supported” or “No GPU/TPU found”, what do I do?\n",
        "    *   Colab CPU runtime is not supported. Try changing your runtime via _Runtime_ > _Change runtime type_ > _Hardware accelerator_ > _GPU_.\n",
        "    *   The type of GPU allocated to your Colab varies. See the [Colab FAQ](https://research.google.com/colaboratory/faq.html) for more details.\n",
        "    *   If you receive “Cannot connect to GPU backend”, you can try again later to see if Colab allocates you a GPU.\n",
886
        "    *   [Colab Pro](https://colab.research.google.com/signup) offers priority access to GPUs.\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
        "*   Does this tool install anything on my computer?\n",
        "    *   No, everything happens in the cloud on Google Colab.\n",
        "    *   At the end of the Colab execution a zip-archive with the obtained prediction will be automatically downloaded to your computer.\n",
        "*   How should I share feedback and bug reports?\n",
        "    *   Please share any feedback and bug reports as an [issue](https://github.com/aqlaboratory/openfold/issues) on Github.\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "YfPhvYgKC81B"
      },
      "source": [
        "# License and Disclaimer\n",
        "\n",
        "This Colab notebook and other information provided is for theoretical modelling only, caution should be exercised in its use. It is provided ‘as-is’ without any warranty of any kind, whether expressed or implied. Information is not intended to be a substitute for professional medical advice, diagnosis, or treatment, and does not constitute medical or other professional advice.\n",
        "\n",
        "## AlphaFold/OpenFold Code License\n",
        "\n",
        "Copyright 2021 AlQuraishi Laboratory\n",
        "\n",
        "Copyright 2021 DeepMind Technologies Limited.\n",
        "\n",
        "Licensed under the Apache License, Version 2.0 (the \"License\"); you may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.apache.org/licenses/LICENSE-2.0.\n",
        "\n",
        "Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.\n",
        "\n",
        "## Model Parameters License\n",
        "\n",
916
        "DeepMind's AlphaFold parameters are made available under the terms of the Creative Commons Attribution 4.0 International (CC BY 4.0) license. You can find details at: https://creativecommons.org/licenses/by/4.0/legalcode\n",
Gustaf Ahdritz's avatar
Gustaf Ahdritz committed
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
        "\n",
        "\n",
        "## Third-party software\n",
        "\n",
        "Use of the third-party software, libraries or code referred to in this notebook may be governed by separate terms and conditions or license provisions. Your use of the third-party software, libraries or code is subject to any such terms and you should check that you can comply with any applicable restrictions or terms and conditions before use.\n",
        "\n",
        "\n",
        "## Mirrored Databases\n",
        "\n",
        "The following databases have been mirrored by DeepMind, and are available with reference to the following:\n",
        "* UniRef90: v2021\\_03 (unmodified), by The UniProt Consortium, available under a [Creative Commons Attribution-NoDerivatives 4.0 International License](http://creativecommons.org/licenses/by-nd/4.0/).\n",
        "* MGnify: v2019\\_05 (unmodified), by Mitchell AL et al., available free of all copyright restrictions and made fully and freely available for both non-commercial and commercial use under [CC0 1.0 Universal (CC0 1.0) Public Domain Dedication](https://creativecommons.org/publicdomain/zero/1.0/).\n",
        "* BFD: (modified), by Steinegger M. and Söding J., modified by DeepMind, available under a [Creative Commons Attribution-ShareAlike 4.0 International License](https://creativecommons.org/licenses/by/4.0/). See the Methods section of the [AlphaFold proteome paper](https://www.nature.com/articles/s41586-021-03828-1) for details."
      ]
    }
jnwei's avatar
jnwei committed
932
933
934
  ],
  "metadata": {
    "colab": {
935
      "provenance": [],
jnwei's avatar
jnwei committed
936
937
      "gpuType": "T4",
      "toc_visible": true
jnwei's avatar
jnwei committed
938
939
940
941
942
943
944
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    },
    "language_info": {
      "name": "python"
945
946
    },
    "accelerator": "GPU"
jnwei's avatar
jnwei committed
947
948
949
  },
  "nbformat": 4,
  "nbformat_minor": 0
950
}