File size: 9,673 Bytes
e76b79a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# High-level protein programming language tutorial\n",
    "\n",
    "In this notebook, we will walk through creating a simple program encoding a symmetric protein and running an optimization run.\n",
    "At the end of the notebook, we will also link to examples of more complex design programs.\n",
    "\n",
    "We design proteins guided by [ESMFold](https://github.com/facebookresearch/esm#esmfold), so let's get set up by loading the ESMFold model."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "%load_ext autoreload\n",
    "%autoreload 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from language import EsmFoldv1\n",
    "\n",
    "folding_callback = EsmFoldv1()\n",
    "folding_callback.load(device=\"cuda:0\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now let's write a program.\n",
    "Our high-level programming language is organized by a *syntax tree*, where *leaf nodes* correspond to protein sequence segments. To begin, let's create a leaf node defined on a fixed-length sequence segment with 25 amino acid residues:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "from language import FixedLengthSequenceSegment\n",
    "from language import ProgramNode\n",
    "\n",
    "protomer = FixedLengthSequenceSegment(25)\n",
    "def make_protomer_node():\n",
    "    return ProgramNode(sequence_segment=protomer)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For this tutorial, we will make a symmetric protein by repeating the above protomer sequence $N$ times, where $N$ is the desired rotational symmetry.\n",
    "\n",
    "Our programming language enables *internal nodes* in the syntax tree that let us hierarchically control multiple child nodes. To program a protein with 3-fold symmetry, let's create an internal node with $N = 3$ children."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "N = 3\n",
    "_node = ProgramNode(\n",
    "    children=[make_protomer_node() for _ in range(N)],\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To complete our programming language, we require a set of *constraints*.\n",
    "An important set of constraints to apply to all proteins is to maximize ESMFold's confidence in the model structure, which we quantify by the pTM and mean pLDDT scores.\n",
    "We also want to constrain the protein to have rotational symmetry.\n",
    "\n",
    "Adding those three constraints (on pTM, pLDDT, and symmetry) completes the simple program for a symmetric protein.\n",
    "We can also weight different constraints differently; for this tutorial, we will double the weight on the symmetry constraint.\n",
    "The constraints are compiled into a single *energy function* (right now, via a linear combination of constraint functions), which we will use to guide the optimization algorithm\n",
    "\n",
    "The code below will add the constraints and weights, compile the program, and print out a summary of the energy function."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "root:MaximizePTM = 1.0 * 0.84\n",
      "root:MaximizePLDDT = 1.0 * 0.65\n",
      "root:SymmetryRing = 1.0 * 0.57\n"
     ]
    }
   ],
   "source": [
    "from language import MaximizePTM, MaximizePLDDT, SymmetryRing\n",
    "\n",
    "# Define the program.\n",
    "program = ProgramNode(\n",
    "    energy_function_terms=[MaximizePTM(), MaximizePLDDT(), SymmetryRing()],\n",
    "    energy_function_weights=[1.0, 1.0, 1.0],\n",
    "    children=[make_protomer_node() for _ in range(N)],\n",
    ")\n",
    "\n",
    "# Set up the program.\n",
    "sequence, residue_indices = program.get_sequence_and_set_residue_index_ranges()\n",
    "\n",
    "# Compute and print the energy function.\n",
    "energy_terms = program.get_energy_term_functions()\n",
    "folding_output = folding_callback.fold(sequence, residue_indices)\n",
    "for name, weight, energy_fn in energy_terms:\n",
    "    print(f\"{name} = {weight:.1f} * {energy_fn(folding_output):.2f}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We are now ready to use this program to guide an optimization loop to design a protein sequence and structure.\n",
    "We currently perform simulated annealing of an MCMC optimization algorithm to do design.\n",
    "The code below will set up the simulated annealing algorithm, including the relevant parameters, and run the optimization loop. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "2883ee40821e4f5ca4f699b96fe837e2",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Output()"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">/private/home/brianhie/protein-bert-model/examples/protein-programming-language/language/optimize.py:56: \n",
       "RuntimeWarning: overflow encountered in exp\n",
       "  np.exp(energy_differential / temperature),\n",
       "</pre>\n"
      ],
      "text/plain": [
       "/private/home/brianhie/protein-bert-model/examples/protein-programming-language/language/optimize.py:56: \n",
       "RuntimeWarning: overflow encountered in exp\n",
       "  np.exp(energy_differential / temperature),\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from language import run_simulated_annealing\n",
    "\n",
    "optimized_program = run_simulated_annealing(\n",
    "    program=program,\n",
    "    initial_temperature=1.0,\n",
    "    annealing_rate=0.97,\n",
    "    total_num_steps=10_000,\n",
    "    folding_callback=folding_callback,\n",
    "    display_progress=True,\n",
    ")\n",
    "print(\"Final sequence = {}\".format(optimized_program.get_sequence_and_set_residue_index_ranges()[0]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This program provides a minimal example of symmetric protein design, but we can also design much more complex proteins, as described in [our paper](https://www.biorxiv.org/content/10.1101/2022.12.21.521526v1).\n",
    "\n",
    "We provide example programs corresponding to our paper figures, as described in the table below:\n",
    "\n",
    "| Design task                                 | Figure in paper | Program file                                                                |\n",
    "|:--------------------------------------------|:---------------|:----------------------------------------------------------------------------|\n",
    "| Free hallucination                          | Figure 2A       | [free_hallucination.py](programs/free_hallucination.py)                     |\n",
    "| Fixed backbone design                       | Figure 2D       | [fixed_backbone.py](programs/fixed_backbone.py)                             |\n",
    "| Secondary structure design                  | Figure 2G       | [secondary_structure.py](programs/secondary_structure.py)                   |\n",
    "| Functional site scaffolding                 | Figure 2H       | [functional_site_scaffolding.py](programs/functional_site_scaffolding.py)   |\n",
    "| Symmetric monomer design                    | Figure 3A       | [symmetric_monomer.py](programs/symmetric_monomer.py)                       |\n",
    "| Two-level symmetric<br>homo-oligomer design | Figure 4A       | [symmetric_two_level_multimer.py](programs/symmetric_two_level_multimer.py) |\n",
    "| Symmetric binding site<br>scaffolding       | Figure 5A       | [symmetric_binding.py](programs/symmetric_binding.py)                       |\n",
    "\n",
    "These can easily be imported and run.\n",
    "For example, the following code imports a symmetric binding site scaffolding program and computes the energy."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from programs.symmetric_binding import symmetric_binding_il10\n",
    "\n",
    "program = symmetric_binding_il10()\n",
    "\n",
    "sequence, residue_indices = program.get_sequence_and_set_residue_index_ranges()\n",
    "\n",
    "folding_output = folding_callback.fold(sequence, residue_indices)\n",
    "\n",
    "energy_terms = program.get_energy_term_functions()\n",
    "for name, weight, energy_fn in energy_terms:\n",
    "    print(f\"{name} = {weight:.1f} * {energy_fn(folding_output):.2f}\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}