Jhsmit commited on
Commit
3c1a832
·
1 Parent(s): dce6708

initial commit

Browse files
Files changed (4) hide show
  1. Dockerfile +28 -0
  2. app.py +9 -0
  3. monomer_dimer.ipynb +517 -0
  4. requirements.txt +5 -0
Dockerfile ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.11
2
+
3
+ # Set up a new user named "user" with user ID 1000
4
+ RUN useradd -m -u 1000 user
5
+
6
+ # Switch to the "user" user
7
+ USER user
8
+
9
+ # Set home to the user's home directory
10
+ ENV HOME=/home/user \
11
+ PATH=/home/user/.local/bin:$PATH
12
+
13
+ # Set the working directory to the user's home directory
14
+ WORKDIR $HOME/app
15
+
16
+ # Try and run pip command after setting the user with `USER user` to avoid permission issues with Python
17
+ RUN pip install --no-cache-dir --upgrade pip
18
+
19
+ # Copy the current directory contents into the container at $HOME/app setting the owner to the user
20
+ COPY --chown=user . $HOME/app
21
+
22
+ COPY --chown=user requirements.txt .
23
+
24
+ RUN pip install --no-cache-dir --upgrade -r requirements.txt
25
+
26
+ COPY --chown=user app.py .
27
+
28
+ ENTRYPOINT ["solara", "run", "app.py", "--host=0.0.0.0", "--port", "7860"]
app.py ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ from pathlib import Path
2
+
3
+ import solara
4
+ from solara.website.components.notebook import Notebook
5
+
6
+
7
+ @solara.component
8
+ def Page():
9
+ Notebook(Path("monomer_dimer.ipynb"), show_last_expressions=True)
monomer_dimer.ipynb ADDED
@@ -0,0 +1,517 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "metadata": {},
6
+ "source": [
7
+ "# Protein binding kinetics\n",
8
+ "\n",
9
+ "The aim of these posts is to explore protein and ligand binding kinetics and how they can be calculated. \n",
10
+ "\n",
11
+ "We will use python to derive and visualization of steady-state concentration laws. \n"
12
+ ]
13
+ },
14
+ {
15
+ "cell_type": "markdown",
16
+ "metadata": {},
17
+ "source": [
18
+ "## Monomer - Dimer kinetics\n",
19
+ "\n",
20
+ "Lets start with the simple example of dimer formation from two identical monomer subunits:\n",
21
+ "(TODO: mhchem package)\n",
22
+ "\n",
23
+ "$$\n",
24
+ "M + M <-> D\n",
25
+ "$$\n",
26
+ "\n",
27
+ "From the kinetic scheme above, we can write down the differential equations discribing this system. These differential equations tell us how the concentrations of the reaction (in this case dimerization) change in time. \n",
28
+ "\n",
29
+ "$$\n",
30
+ "\\frac{\\partial [M]}{\\partial t} = - 2 k_{on} [M][M] + 2 k_{off} [D]\n",
31
+ "$$\n",
32
+ "\n",
33
+ "$$\n",
34
+ "\\frac{\\partial [D]}{\\partial t} = k_{on} [M][M] - k_{off} [D]\n",
35
+ "$$\n",
36
+ "\n",
37
+ "Where square brackets are used to indicate the concentration of the species, ie $[M]$ is the concentration of the monomer at a given time point $t$.\n",
38
+ "\n",
39
+ "To sanity check our results, we can multiply the second equation with -2, such that both right-hand sides become equal. We then find that:\n",
40
+ " \n",
41
+ "$$\n",
42
+ "\\frac{\\partial [M]}{\\partial t} = - 2 \\frac{\\partial [D]}{\\partial t}\n",
43
+ "$$\n",
44
+ "\n",
45
+ "When reading the equation above, for the equality to hold true, we find that the dimer $D$ must appear at _half_ the rate at which the monomer $M$ disappears. Cross-checking this with our chemical intuition this makes sense, since two monomers are required to make one dimer. \n",
46
+ "\n",
47
+ "In order to now find which concentrations of monomer and dimer are formed given some starting concentration of the monomer $[M_0]$, we need three more pieces of information. \n",
48
+ "\n",
49
+ "First, we are looking for the concentration of the monomer and dimer at _steady-state_: the system has reached equillibrium and no more changes in the concentration of either the monomer or the dimer occur. This means we can set both differential equations to zero. \n",
50
+ "\n",
51
+ "Second, we realize that the total amount of monomer in the tube never goes up or down; its either monomor or as a protomer in a dimer. The starting amount of monomer is thus:\n",
52
+ "\n",
53
+ "$$\n",
54
+ "[M_0] = [M] + 2[D]\n",
55
+ "$$\n",
56
+ "\n",
57
+ "We can think of [M_0] also as 'total amount of protomer', either in monomer or dimer form. This makes it easy to measure the [M_0] of a tube with a monomer/dimer mixture; we can simply calculate the extinction coefficient of the protomer and since extinction coefficients are generally additive we can use this to measure [M_0] directly. \n",
58
+ "\n",
59
+ "Third, we need to have some value for the forward $k_f$ and backward rates $k_r$. These rates describe how fast the reaction takes place, but at the moment we are only interested in the steady-state concentrations. We can define a single new quantity which depends on the ratio of the forward and backward rates:\n",
60
+ "\n",
61
+ "$$\n",
62
+ "K_d = \\frac{k_{off}}{k_{on}}\n",
63
+ "$$\n",
64
+ "\n",
65
+ "If we look at our differential equations, the left-hand side describes the change of concentration of the monomer over time. The units are therefore molar per second ($\\underline{M} s^{-1}$; we use $\\underline{M}$ to discriminate from monomer $M$). To make the units match on the left and right-hand side, we can deduce that the units of $k_{on}$ must be $\\underline{M}^{-1} s^{-1}$. Similarly, the units of $k_{off}$ are $s^{-1}$. From this unit analysis, we can see that the lifetime or off rate from a complex is independent of concentratation and we can recognize $K_d$ as the dissociation constant with units $\\underline{M}$. "
66
+ ]
67
+ },
68
+ {
69
+ "cell_type": "markdown",
70
+ "metadata": {},
71
+ "source": [
72
+ "We can now use [sympy](https://www.sympy.org/en/index.html) to do some mathematics and figure out what the steady-state concentrations are."
73
+ ]
74
+ },
75
+ {
76
+ "cell_type": "code",
77
+ "execution_count": 1,
78
+ "metadata": {},
79
+ "outputs": [],
80
+ "source": [
81
+ "import sympy as sp\n",
82
+ "\n",
83
+ "M, D, M0, k_on, k_off, kD = sp.symbols(\"M D M0 k_on k_off k_D\", positive=True)"
84
+ ]
85
+ },
86
+ {
87
+ "cell_type": "markdown",
88
+ "metadata": {},
89
+ "source": [
90
+ "Now, we can solve the system of equations as we defined them above. Because of steady-state conditions, the differential equations are zero. We only use one here because the first is just the second multiplied by a constant factor, and thus does now have any additional information. To input equations into `sympy`, we must set the right hand side to zero and input the resulting left hand side:"
91
+ ]
92
+ },
93
+ {
94
+ "cell_type": "code",
95
+ "execution_count": 2,
96
+ "metadata": {},
97
+ "outputs": [
98
+ {
99
+ "data": {
100
+ "text/plain": [
101
+ "[{D: M0/2 - sqrt(k_D)*sqrt(8*M0 + k_D)/8 + k_D/8,\n",
102
+ " M: sqrt(k_D)*sqrt(8*M0 + k_D)/4 - k_D/4,\n",
103
+ " k_off: k_D*k_on}]"
104
+ ]
105
+ },
106
+ "execution_count": 2,
107
+ "metadata": {},
108
+ "output_type": "execute_result"
109
+ }
110
+ ],
111
+ "source": [
112
+ "sol = sp.solve(\n",
113
+ " [\n",
114
+ " -2 * k_on * M * M + 2 * k_off * D,\n",
115
+ " M + 2 * D - M0,\n",
116
+ " (k_off / k_on) - kD,\n",
117
+ " ],\n",
118
+ " [M, D, k_on, k_off],\n",
119
+ " dict=True,\n",
120
+ ")\n",
121
+ "sol"
122
+ ]
123
+ },
124
+ {
125
+ "cell_type": "markdown",
126
+ "metadata": {},
127
+ "source": [
128
+ "This returns a single solution, because we have told `sympy` that all input symbols are positive. We can have a look at the solution for the concentration of the dimer:"
129
+ ]
130
+ },
131
+ {
132
+ "cell_type": "code",
133
+ "execution_count": 3,
134
+ "metadata": {},
135
+ "outputs": [
136
+ {
137
+ "data": {
138
+ "text/latex": [
139
+ "$\\displaystyle \\frac{M_{0}}{2} - \\frac{\\sqrt{k_{D}} \\sqrt{8 M_{0} + k_{D}}}{8} + \\frac{k_{D}}{8}$"
140
+ ],
141
+ "text/plain": [
142
+ "M0/2 - sqrt(k_D)*sqrt(8*M0 + k_D)/8 + k_D/8"
143
+ ]
144
+ },
145
+ "execution_count": 3,
146
+ "metadata": {},
147
+ "output_type": "execute_result"
148
+ }
149
+ ],
150
+ "source": [
151
+ "sol[0][D]"
152
+ ]
153
+ },
154
+ {
155
+ "cell_type": "markdown",
156
+ "metadata": {},
157
+ "source": [
158
+ "We can see that indeed the solution only depends on the dissocation constant $K_D$, and not the individual rates and indeed the 'mass balance' equation still holds ($M + 2D = M_0$), excercise for the reader."
159
+ ]
160
+ },
161
+ {
162
+ "cell_type": "markdown",
163
+ "metadata": {},
164
+ "source": [
165
+ "Next, lets use [solara](https://solara.dev/) to quickly make an interactive component so that we can easily calculate the monomer and dimer concentrations, given a input concentration $M_0$ and the dissociation constant. \n",
166
+ "\n",
167
+ "First, we take the symbolic solutions from `sympy` and lambdify them so that we can input numbers and calculate the output:"
168
+ ]
169
+ },
170
+ {
171
+ "cell_type": "code",
172
+ "execution_count": 4,
173
+ "metadata": {},
174
+ "outputs": [],
175
+ "source": [
176
+ "solve_for = [M, D]\n",
177
+ "inputs = [M0, kD]\n",
178
+ "\n",
179
+ "lambdas = {s: sp.lambdify(inputs, sol[0][s]) for s in solve_for}"
180
+ ]
181
+ },
182
+ {
183
+ "cell_type": "markdown",
184
+ "metadata": {},
185
+ "source": [
186
+ "Next, we make a `solara` component, where a user can input the values for $M_0$ and $K_{D}$ and directly obtain $M$ and $D$ at steady-state:"
187
+ ]
188
+ },
189
+ {
190
+ "cell_type": "code",
191
+ "execution_count": 5,
192
+ "metadata": {},
193
+ "outputs": [
194
+ {
195
+ "data": {
196
+ "application/vnd.jupyter.widget-view+json": {
197
+ "model_id": "e88900b4bfae4f1c82b4ca7f489cb87c",
198
+ "version_major": 2,
199
+ "version_minor": 0
200
+ },
201
+ "text/html": [
202
+ "Cannot show widget. You probably want to rerun the code cell above (<i>Click in the code cell, and press Shift+Enter <kbd>⇧</kbd>+<kbd>↩</kbd></i>)."
203
+ ],
204
+ "text/plain": [
205
+ "Cannot show ipywidgets in text"
206
+ ]
207
+ },
208
+ "metadata": {},
209
+ "output_type": "display_data"
210
+ }
211
+ ],
212
+ "source": [
213
+ "import solara\n",
214
+ "\n",
215
+ "@solara.component\n",
216
+ "def Page():\n",
217
+ " M0 = solara.use_reactive(10.)\n",
218
+ " kD = solara.use_reactive(1.)\n",
219
+ "\n",
220
+ " ans = {k: ld(M0.value, kD.value) for k, ld in lambdas.items()}\n",
221
+ "\n",
222
+ " solara.InputFloat('M0', value=M0)\n",
223
+ " solara.InputFloat('kD', value=kD)\n",
224
+ "\n",
225
+ " for k, v in ans.items():\n",
226
+ " solara.Text(f'{k.name}: {v}')\n",
227
+ "\n",
228
+ "Page()"
229
+ ]
230
+ },
231
+ {
232
+ "cell_type": "markdown",
233
+ "metadata": {},
234
+ "source": [
235
+ "Note that the values are all unitless, so if we enter a dissociation constant of 1 $\\mu \\underline{M}$, the concentration $M_0$ should also be given in $\\mu \\underline{M}$, and the outputs are then $\\mu \\underline{M}$ as well. \n",
236
+ "\n",
237
+ "This litltle widget now tells us that if the monomer concentration if 10 times over the value of $k_D$, in the steady-state equillibrium one out of three species is still a monomer!"
238
+ ]
239
+ },
240
+ {
241
+ "cell_type": "markdown",
242
+ "metadata": {},
243
+ "source": [
244
+ "Lets try if we can visualize the ratio of dimer to monomer over a larger range of concentrations. To do so we evalulate the monomer/dimer concentrations over a range spanning from 0.1 $k_D$ to 1000 $k_D$. "
245
+ ]
246
+ },
247
+ {
248
+ "cell_type": "code",
249
+ "execution_count": 13,
250
+ "metadata": {},
251
+ "outputs": [
252
+ {
253
+ "data": {
254
+ "text/html": [
255
+ "<div>\n",
256
+ "<style scoped>\n",
257
+ " .dataframe tbody tr th:only-of-type {\n",
258
+ " vertical-align: middle;\n",
259
+ " }\n",
260
+ "\n",
261
+ " .dataframe tbody tr th {\n",
262
+ " vertical-align: top;\n",
263
+ " }\n",
264
+ "\n",
265
+ " .dataframe thead th {\n",
266
+ " text-align: right;\n",
267
+ " }\n",
268
+ "</style>\n",
269
+ "<table border=\"1\" class=\"dataframe\">\n",
270
+ " <thead>\n",
271
+ " <tr style=\"text-align: right;\">\n",
272
+ " <th></th>\n",
273
+ " <th>M0</th>\n",
274
+ " <th>M</th>\n",
275
+ " <th>D</th>\n",
276
+ " </tr>\n",
277
+ " </thead>\n",
278
+ " <tbody>\n",
279
+ " <tr>\n",
280
+ " <th>0</th>\n",
281
+ " <td>0.100000</td>\n",
282
+ " <td>0.921311</td>\n",
283
+ " <td>0.078689</td>\n",
284
+ " </tr>\n",
285
+ " <tr>\n",
286
+ " <th>1</th>\n",
287
+ " <td>0.109750</td>\n",
288
+ " <td>0.915248</td>\n",
289
+ " <td>0.084752</td>\n",
290
+ " </tr>\n",
291
+ " <tr>\n",
292
+ " <th>2</th>\n",
293
+ " <td>0.120450</td>\n",
294
+ " <td>0.908825</td>\n",
295
+ " <td>0.091175</td>\n",
296
+ " </tr>\n",
297
+ " <tr>\n",
298
+ " <th>3</th>\n",
299
+ " <td>0.132194</td>\n",
300
+ " <td>0.902035</td>\n",
301
+ " <td>0.097965</td>\n",
302
+ " </tr>\n",
303
+ " <tr>\n",
304
+ " <th>4</th>\n",
305
+ " <td>0.145083</td>\n",
306
+ " <td>0.894871</td>\n",
307
+ " <td>0.105129</td>\n",
308
+ " </tr>\n",
309
+ " </tbody>\n",
310
+ "</table>\n",
311
+ "</div>"
312
+ ],
313
+ "text/plain": [
314
+ " M0 M D\n",
315
+ "0 0.100000 0.921311 0.078689\n",
316
+ "1 0.109750 0.915248 0.084752\n",
317
+ "2 0.120450 0.908825 0.091175\n",
318
+ "3 0.132194 0.902035 0.097965\n",
319
+ "4 0.145083 0.894871 0.105129"
320
+ ]
321
+ },
322
+ "execution_count": 13,
323
+ "metadata": {},
324
+ "output_type": "execute_result"
325
+ }
326
+ ],
327
+ "source": [
328
+ "import numpy as np\n",
329
+ "import pandas as pd\n",
330
+ "\n",
331
+ "\n",
332
+ "# create a new function which calculates total molarity (monomer + dimer)\n",
333
+ "ld_total = sp.lambdify(inputs, sol[0][M] + sol[0][D])\n",
334
+ "\n",
335
+ "# create a vector of M0 values ranging from 0.1 times kD to 1000 times kD\n",
336
+ "M0_values = np.logspace(-1, 3, endpoint=True, num=100)\n",
337
+ "ans = {k: ld(M0_values, 1) / ld_total(M0_values, 1) for k, ld in lambdas.items()}\n",
338
+ "\n",
339
+ "# put the results in a dataframe, together with input M0 values\n",
340
+ "df = pd.DataFrame(dict(M0=M0_values) | {k.name: v for k, v in ans.items()})\n",
341
+ "\n",
342
+ "df.head()"
343
+ ]
344
+ },
345
+ {
346
+ "cell_type": "markdown",
347
+ "metadata": {},
348
+ "source": [
349
+ "The resulting values are _fractions_ of monomer and dimer respectively, rather than their concentrations. We can plot the results with `altair`, adapting the [gallery example](https://altair-viz.github.io/gallery/multiline_tooltip_standard.html#gallery-multiline-tooltip-standard) for a multi-line tooltip graph. "
350
+ ]
351
+ },
352
+ {
353
+ "cell_type": "code",
354
+ "execution_count": 14,
355
+ "metadata": {},
356
+ "outputs": [
357
+ {
358
+ "data": {
359
+ "text/html": [
360
+ "\n",
361
+ "<style>\n",
362
+ " #altair-viz-5c4f57a4e2c64d4eb3fa1e441e4e6725.vega-embed {\n",
363
+ " width: 100%;\n",
364
+ " display: flex;\n",
365
+ " }\n",
366
+ "\n",
367
+ " #altair-viz-5c4f57a4e2c64d4eb3fa1e441e4e6725.vega-embed details,\n",
368
+ " #altair-viz-5c4f57a4e2c64d4eb3fa1e441e4e6725.vega-embed details summary {\n",
369
+ " position: relative;\n",
370
+ " }\n",
371
+ "</style>\n",
372
+ "<div id=\"altair-viz-5c4f57a4e2c64d4eb3fa1e441e4e6725\"></div>\n",
373
+ "<script type=\"text/javascript\">\n",
374
+ " var VEGA_DEBUG = (typeof VEGA_DEBUG == \"undefined\") ? {} : VEGA_DEBUG;\n",
375
+ " (function(spec, embedOpt){\n",
376
+ " let outputDiv = document.currentScript.previousElementSibling;\n",
377
+ " if (outputDiv.id !== \"altair-viz-5c4f57a4e2c64d4eb3fa1e441e4e6725\") {\n",
378
+ " outputDiv = document.getElementById(\"altair-viz-5c4f57a4e2c64d4eb3fa1e441e4e6725\");\n",
379
+ " }\n",
380
+ " const paths = {\n",
381
+ " \"vega\": \"https://cdn.jsdelivr.net/npm/vega@5?noext\",\n",
382
+ " \"vega-lib\": \"https://cdn.jsdelivr.net/npm/vega-lib?noext\",\n",
383
+ " \"vega-lite\": \"https://cdn.jsdelivr.net/npm/[email protected]?noext\",\n",
384
+ " \"vega-embed\": \"https://cdn.jsdelivr.net/npm/vega-embed@6?noext\",\n",
385
+ " };\n",
386
+ "\n",
387
+ " function maybeLoadScript(lib, version) {\n",
388
+ " var key = `${lib.replace(\"-\", \"\")}_version`;\n",
389
+ " return (VEGA_DEBUG[key] == version) ?\n",
390
+ " Promise.resolve(paths[lib]) :\n",
391
+ " new Promise(function(resolve, reject) {\n",
392
+ " var s = document.createElement('script');\n",
393
+ " document.getElementsByTagName(\"head\")[0].appendChild(s);\n",
394
+ " s.async = true;\n",
395
+ " s.onload = () => {\n",
396
+ " VEGA_DEBUG[key] = version;\n",
397
+ " return resolve(paths[lib]);\n",
398
+ " };\n",
399
+ " s.onerror = () => reject(`Error loading script: ${paths[lib]}`);\n",
400
+ " s.src = paths[lib];\n",
401
+ " });\n",
402
+ " }\n",
403
+ "\n",
404
+ " function showError(err) {\n",
405
+ " outputDiv.innerHTML = `<div class=\"error\" style=\"color:red;\">${err}</div>`;\n",
406
+ " throw err;\n",
407
+ " }\n",
408
+ "\n",
409
+ " function displayChart(vegaEmbed) {\n",
410
+ " vegaEmbed(outputDiv, spec, embedOpt)\n",
411
+ " .catch(err => showError(`Javascript Error: ${err.message}<br>This usually means there's a typo in your chart specification. See the javascript console for the full traceback.`));\n",
412
+ " }\n",
413
+ "\n",
414
+ " if(typeof define === \"function\" && define.amd) {\n",
415
+ " requirejs.config({paths});\n",
416
+ " require([\"vega-embed\"], displayChart, err => showError(`Error loading script: ${err.message}`));\n",
417
+ " } else {\n",
418
+ " maybeLoadScript(\"vega\", \"5\")\n",
419
+ " .then(() => maybeLoadScript(\"vega-lite\", \"5.17.0\"))\n",
420
+ " .then(() => maybeLoadScript(\"vega-embed\", \"6\"))\n",
421
+ " .catch(showError)\n",
422
+ " .then(() => displayChart(vegaEmbed));\n",
423
+ " }\n",
424
+ " })({\"config\": {\"view\": {\"continuousWidth\": 300, \"continuousHeight\": 300}}, \"layer\": [{\"mark\": {\"type\": \"line\", \"interpolate\": \"basis\"}, \"encoding\": {\"color\": {\"field\": \"species\", \"type\": \"nominal\"}, \"x\": {\"field\": \"M0\", \"scale\": {\"type\": \"log\"}, \"title\": \"Ratio M0/kD\", \"type\": \"quantitative\"}, \"y\": {\"field\": \"y\", \"title\": \"Fraction of total\", \"type\": \"quantitative\"}}}, {\"mark\": {\"type\": \"point\"}, \"encoding\": {\"color\": {\"field\": \"species\", \"type\": \"nominal\"}, \"opacity\": {\"condition\": {\"param\": \"param_5\", \"value\": 1, \"empty\": false}, \"value\": 0}, \"x\": {\"field\": \"M0\", \"scale\": {\"type\": \"log\"}, \"title\": \"Ratio M0/kD\", \"type\": \"quantitative\"}, \"y\": {\"field\": \"y\", \"title\": \"Fraction of total\", \"type\": \"quantitative\"}}}, {\"mark\": {\"type\": \"rule\", \"color\": \"black\"}, \"encoding\": {\"opacity\": {\"condition\": {\"param\": \"param_5\", \"value\": 0.3, \"empty\": false}, \"value\": 0}, \"tooltip\": [{\"field\": \"M0\", \"format\": \".2f\", \"type\": \"quantitative\"}, {\"field\": \"M\", \"format\": \".2f\", \"type\": \"quantitative\"}, {\"field\": \"D\", \"format\": \".2f\", \"type\": \"quantitative\"}], \"x\": {\"field\": \"M0\", \"type\": \"quantitative\"}}, \"name\": \"view_5\", \"transform\": [{\"pivot\": \"species\", \"value\": \"y\", \"groupby\": [\"M0\"]}]}], \"data\": {\"name\": \"data-8452c92cacf7efa00b398135ad1f4bc6\"}, \"height\": 300, \"params\": [{\"name\": \"param_5\", \"select\": {\"type\": \"point\", \"fields\": [\"M0\"], \"nearest\": true, \"on\": \"pointerover\"}, \"views\": [\"view_5\"]}], \"width\": 600, \"$schema\": \"https://vega.github.io/schema/vega-lite/v5.17.0.json\", \"datasets\": {\"data-8452c92cacf7efa00b398135ad1f4bc6\": [{\"M0\": 0.1, \"species\": \"M\", \"y\": 0.9213106741667366}, {\"M0\": 0.10974987654930562, \"species\": \"M\", \"y\": 0.9152478142703887}, {\"M0\": 0.12045035402587821, \"species\": \"M\", \"y\": 0.9088252563655043}, {\"M0\": 0.1321941148466029, \"species\": \"M\", \"y\": 0.9020350932162419}, {\"M0\": 0.14508287784959398, \"species\": \"M\", \"y\": 0.8948707231715299}, {\"M0\": 0.1592282793341092, \"species\": \"M\", \"y\": 0.887326964051884}, {\"M0\": 0.17475284000076838, \"species\": \"M\", \"y\": 0.8794001529111963}, {\"M0\": 0.19179102616724888, \"species\": \"M\", \"y\": 0.8710882296412226}, {\"M0\": 0.2104904144512021, \"species\": \"M\", \"y\": 0.8623908028192255}, {\"M0\": 0.23101297000831597, \"species\": \"M\", \"y\": 0.8533091966764864}, {\"M0\": 0.2535364493970112, \"species\": \"M\", \"y\": 0.8438464785616561}, {\"M0\": 0.2782559402207124, \"species\": \"M\", \"y\": 0.8340074667631514}, {\"M0\": 0.30538555088334157, \"species\": \"M\", \"y\": 0.8237987190163906}, {\"M0\": 0.33516026509388425, \"species\": \"M\", \"y\": 0.813228502436007}, {\"M0\": 0.36783797718286343, \"species\": \"M\", \"y\": 0.8023067459666666}, {\"M0\": 0.40370172585965547, \"species\": \"M\", \"y\": 0.791044976730576}, {\"M0\": 0.4430621457583881, \"species\": \"M\", \"y\": 0.7794562418623244}, {\"M0\": 0.4862601580065355, \"species\": \"M\", \"y\": 0.7675550175641289}, {\"M0\": 0.533669923120631, \"species\": \"M\", \"y\": 0.7553571071923714}, {\"M0\": 0.5857020818056667, \"species\": \"M\", \"y\": 0.742879530207701}, {\"M0\": 0.6428073117284322, \"species\": \"M\", \"y\": 0.7301404037955845}, {\"M0\": 0.7054802310718644, \"species\": \"M\", \"y\": 0.7171588189022171}, {\"M0\": 0.7742636826811272, \"species\": \"M\", \"y\": 0.7039547123419793}, {\"M0\": 0.8497534359086445, \"species\": \"M\", \"y\": 0.6905487365260083}, {\"M0\": 0.9326033468832199, \"species\": \"M\", \"y\": 0.6769621282444622}, {\"M0\": 1.0235310218990263, \"species\": \"M\", \"y\": 0.6632165778136448}, {\"M0\": 1.1233240329780276, \"species\": \"M\", \"y\": 0.6493340997777712}, {\"M0\": 1.2328467394420666, \"species\": \"M\", \"y\": 0.6353369062367451}, {\"M0\": 1.3530477745798075, \"species\": \"M\", \"y\": 0.6212472837576913}, {\"M0\": 1.484968262254465, \"species\": \"M\", \"y\": 0.6070874747199139}, {\"M0\": 1.6297508346206444, \"species\": \"M\", \"y\": 0.5928795638405217}, {\"M0\": 1.7886495290574351, \"species\": \"M\", \"y\": 0.5786453705308084}, {\"M0\": 1.9630406500402715, \"species\": \"M\", \"y\": 0.5644063476409491}, {\"M0\": 2.1544346900318843, \"species\": \"M\", \"y\": 0.5501834870619822}, {\"M0\": 2.3644894126454084, \"species\": \"M\", \"y\": 0.5359972325687437}, {\"M0\": 2.5950242113997373, \"species\": \"M\", \"y\": 0.5218674002048792}, {\"M0\": 2.848035868435802, \"species\": \"M\", \"y\": 0.5078131064309908}, {\"M0\": 3.1257158496882367, \"species\": \"M\", \"y\": 0.493852704179271}, {\"M0\": 3.4304692863149193, \"species\": \"M\", \"y\": 0.48000372688273857}, {\"M0\": 3.764935806792469, \"species\": \"M\", \"y\": 0.4662828404747031}, {\"M0\": 4.132012400115339, \"species\": \"M\", \"y\": 0.45270580328479026}, {\"M0\": 4.5348785081285845, \"species\": \"M\", \"y\": 0.43928743369225387}, {\"M0\": 4.977023564332112, \"species\": \"M\", \"y\": 0.42604158533598346}, {\"M0\": 5.462277217684343, \"species\": \"M\", \"y\": 0.41298112962415334}, {\"M0\": 5.994842503189412, \"species\": \"M\", \"y\": 0.4001179452353983}, {\"M0\": 6.5793322465756825, \"species\": \"M\", \"y\": 0.3874629142582253}, {\"M0\": 7.220809018385467, \"species\": \"M\", \"y\": 0.3750259245764573}, {\"M0\": 7.924828983539178, \"species\": \"M\", \"y\": 0.3628158780761266}, {\"M0\": 8.697490026177835, \"species\": \"M\", \"y\": 0.3508407042235381}, {\"M0\": 9.545484566618342, \"species\": \"M\", \"y\": 0.33910737854522055}, {\"M0\": 10.476157527896651, \"species\": \"M\", \"y\": 0.32762194552806667}, {\"M0\": 11.497569953977369, \"species\": \"M\", \"y\": 0.3163895454519114}, {\"M0\": 12.61856883066021, \"species\": \"M\", \"y\": 0.30541444466676376}, {\"M0\": 13.848863713938732, \"species\": \"M\", \"y\": 0.2947000688324766}, {\"M0\": 15.199110829529348, \"species\": \"M\", \"y\": 0.28424903864930695}, {\"M0\": 16.68100537200059, \"species\": \"M\", \"y\": 0.27406320762302827}, {\"M0\": 18.307382802953697, \"species\": \"M\", \"y\": 0.2641437014274086}, {\"M0\": 20.09233002565048, \"species\": \"M\", \"y\": 0.2544909584493632}, {\"M0\": 22.051307399030456, \"species\": \"M\", \"y\": 0.24510477112727974}, {\"M0\": 24.201282647943835, \"species\": \"M\", \"y\": 0.2359843277203209}, {\"M0\": 26.560877829466868, \"species\": \"M\", \"y\": 0.2271282541753208}, {\"M0\": 29.150530628251786, \"species\": \"M\", \"y\": 0.21853465578766545}, {\"M0\": 31.992671377973846, \"species\": \"M\", \"y\": 0.2102011583827801}, {\"M0\": 35.111917342151344, \"species\": \"M\", \"y\": 0.20212494877505027}, {\"M0\": 38.535285937105314, \"species\": \"M\", \"y\": 0.1943028142908023}, {\"M0\": 42.29242874389499, \"species\": \"M\", \"y\": 0.1867311811709695}, {\"M0\": 46.41588833612782, \"species\": \"M\", \"y\": 0.17940615169700316}, {\"M0\": 50.9413801481638, \"species\": \"M\", \"y\": 0.17232353991018073}, {\"M0\": 55.90810182512229, \"species\": \"M\", \"y\": 0.16547890581953636}, {\"M0\": 61.35907273413176, \"species\": \"M\", \"y\": 0.15886758801703976}, {\"M0\": 67.34150657750828, \"species\": \"M\", \"y\": 0.1524847346402729}, {\"M0\": 73.90722033525783, \"species\": \"M\", \"y\": 0.14632533264264772}, {\"M0\": 81.11308307896873, \"species\": \"M\", \"y\": 0.14038423534913913}, {\"M0\": 89.02150854450393, \"species\": \"M\", \"y\": 0.1346561882915889}, {\"M0\": 97.70099572992257, \"species\": \"M\", \"y\": 0.12913585333189967}, {\"M0\": 107.22672220103243, \"species\": \"M\", \"y\": 0.12381783109393958}, {\"M0\": 117.68119524349991, \"species\": \"M\", \"y\": 0.11869668173579613}, {\"M0\": 129.1549665014884, \"species\": \"M\", \"y\": 0.11376694410323461}, {\"M0\": 141.7474162926806, \"species\": \"M\", \"y\": 0.10902315331294034}, {\"M0\": 155.56761439304722, \"species\": \"M\", \"y\": 0.10445985682045184}, {\"M0\": 170.7352647470692, \"species\": \"M\", \"y\": 0.10007162903274308}, {\"M0\": 187.3817422860385, \"species\": \"M\", \"y\": 0.09585308452929588}, {\"M0\": 205.65123083486534, \"species\": \"M\", \"y\": 0.0917988899583294}, {\"M0\": 225.70197196339214, \"species\": \"M\", \"y\": 0.08790377467673861}, {\"M0\": 247.70763559917114, \"species\": \"M\", \"y\": 0.08416254020333655}, {\"M0\": 271.85882427329426, \"species\": \"M\", \"y\": 0.0805700685553064}, {\"M0\": 298.364724028334, \"species\": \"M\", \"y\": 0.07712132953743882}, {\"M0\": 327.4549162877732, \"species\": \"M\", \"y\": 0.07381138705285176}, {\"M0\": 359.3813663804629, \"species\": \"M\", \"y\": 0.07063540450254992}, {\"M0\": 394.420605943766, \"species\": \"M\", \"y\": 0.06758864933944884}, {\"M0\": 432.87612810830615, \"species\": \"M\", \"y\": 0.06466649684044665}, {\"M0\": 475.0810162102798, \"species\": \"M\", \"y\": 0.061864433157825666}, {\"M0\": 521.400828799969, \"species\": \"M\", \"y\": 0.05917805770877593}, {\"M0\": 572.236765935022, \"species\": \"M\", \"y\": 0.0566030849591937}, {\"M0\": 628.029144183426, \"species\": \"M\", \"y\": 0.05413534565517292}, {\"M0\": 689.2612104349702, \"species\": \"M\", \"y\": 0.05177078755281282}, {\"M0\": 756.463327554629, \"species\": \"M\", \"y\": 0.04950547569414219}, {\"M0\": 830.2175681319752, \"species\": \"M\", \"y\": 0.04733559227414581}, {\"M0\": 911.1627561154896, \"species\": \"M\", \"y\": 0.045257436141088785}, {\"M0\": 1000.0, \"species\": \"M\", \"y\": 0.04326742196959632}, {\"M0\": 0.1, \"species\": \"D\", \"y\": 0.07868932583326321}, {\"M0\": 0.10974987654930562, \"species\": \"D\", \"y\": 0.08475218572961124}, {\"M0\": 0.12045035402587821, \"species\": \"D\", \"y\": 0.09117474363449568}, {\"M0\": 0.1321941148466029, \"species\": \"D\", \"y\": 0.09796490678375804}, {\"M0\": 0.14508287784959398, \"species\": \"D\", \"y\": 0.10512927682846993}, {\"M0\": 0.1592282793341092, \"species\": \"D\", \"y\": 0.11267303594811592}, {\"M0\": 0.17475284000076838, \"species\": \"D\", \"y\": 0.12059984708880354}, {\"M0\": 0.19179102616724888, \"species\": \"D\", \"y\": 0.12891177035877757}, {\"M0\": 0.2104904144512021, \"species\": \"D\", \"y\": 0.1376091971807744}, {\"M0\": 0.23101297000831597, \"species\": \"D\", \"y\": 0.14669080332351347}, {\"M0\": 0.2535364493970112, \"species\": \"D\", \"y\": 0.1561535214383439}, {\"M0\": 0.2782559402207124, \"species\": \"D\", \"y\": 0.1659925332368486}, {\"M0\": 0.30538555088334157, \"species\": \"D\", \"y\": 0.17620128098360954}, {\"M0\": 0.33516026509388425, \"species\": \"D\", \"y\": 0.18677149756399297}, {\"M0\": 0.36783797718286343, \"species\": \"D\", \"y\": 0.19769325403333335}, {\"M0\": 0.40370172585965547, \"species\": \"D\", \"y\": 0.20895502326942395}, {\"M0\": 0.4430621457583881, \"species\": \"D\", \"y\": 0.22054375813767554}, {\"M0\": 0.4862601580065355, \"species\": \"D\", \"y\": 0.2324449824358711}, {\"M0\": 0.533669923120631, \"species\": \"D\", \"y\": 0.24464289280762855}, {\"M0\": 0.5857020818056667, \"species\": \"D\", \"y\": 0.257120469792299}, {\"M0\": 0.6428073117284322, \"species\": \"D\", \"y\": 0.26985959620441546}, {\"M0\": 0.7054802310718644, \"species\": \"D\", \"y\": 0.2828411810977828}, {\"M0\": 0.7742636826811272, \"species\": \"D\", \"y\": 0.2960452876580207}, {\"M0\": 0.8497534359086445, \"species\": \"D\", \"y\": 0.3094512634739916}, {\"M0\": 0.9326033468832199, \"species\": \"D\", \"y\": 0.3230378717555378}, {\"M0\": 1.0235310218990263, \"species\": \"D\", \"y\": 0.33678342218635515}, {\"M0\": 1.1233240329780276, \"species\": \"D\", \"y\": 0.3506659002222288}, {\"M0\": 1.2328467394420666, \"species\": \"D\", \"y\": 0.36466309376325495}, {\"M0\": 1.3530477745798075, \"species\": \"D\", \"y\": 0.37875271624230866}, {\"M0\": 1.484968262254465, \"species\": \"D\", \"y\": 0.39291252528008624}, {\"M0\": 1.6297508346206444, \"species\": \"D\", \"y\": 0.40712043615947846}, {\"M0\": 1.7886495290574351, \"species\": \"D\", \"y\": 0.42135462946919144}, {\"M0\": 1.9630406500402715, \"species\": \"D\", \"y\": 0.4355936523590508}, {\"M0\": 2.1544346900318843, \"species\": \"D\", \"y\": 0.4498165129380179}, {\"M0\": 2.3644894126454084, \"species\": \"D\", \"y\": 0.4640027674312564}, {\"M0\": 2.5950242113997373, \"species\": \"D\", \"y\": 0.4781325997951208}, {\"M0\": 2.848035868435802, \"species\": \"D\", \"y\": 0.49218689356900924}, {\"M0\": 3.1257158496882367, \"species\": \"D\", \"y\": 0.5061472958207289}, {\"M0\": 3.4304692863149193, \"species\": \"D\", \"y\": 0.5199962731172615}, {\"M0\": 3.764935806792469, \"species\": \"D\", \"y\": 0.533717159525297}, {\"M0\": 4.132012400115339, \"species\": \"D\", \"y\": 0.5472941967152098}, {\"M0\": 4.5348785081285845, \"species\": \"D\", \"y\": 0.5607125663077462}, {\"M0\": 4.977023564332112, \"species\": \"D\", \"y\": 0.5739584146640166}, {\"M0\": 5.462277217684343, \"species\": \"D\", \"y\": 0.5870188703758467}, {\"M0\": 5.994842503189412, \"species\": \"D\", \"y\": 0.5998820547646017}, {\"M0\": 6.5793322465756825, \"species\": \"D\", \"y\": 0.6125370857417747}, {\"M0\": 7.220809018385467, \"species\": \"D\", \"y\": 0.6249740754235427}, {\"M0\": 7.924828983539178, \"species\": \"D\", \"y\": 0.6371841219238735}, {\"M0\": 8.697490026177835, \"species\": \"D\", \"y\": 0.6491592957764619}, {\"M0\": 9.545484566618342, \"species\": \"D\", \"y\": 0.6608926214547796}, {\"M0\": 10.476157527896651, \"species\": \"D\", \"y\": 0.6723780544719333}, {\"M0\": 11.497569953977369, \"species\": \"D\", \"y\": 0.6836104545480887}, {\"M0\": 12.61856883066021, \"species\": \"D\", \"y\": 0.6945855553332363}, {\"M0\": 13.848863713938732, \"species\": \"D\", \"y\": 0.7052999311675234}, {\"M0\": 15.199110829529348, \"species\": \"D\", \"y\": 0.7157509613506929}, {\"M0\": 16.68100537200059, \"species\": \"D\", \"y\": 0.7259367923769716}, {\"M0\": 18.307382802953697, \"species\": \"D\", \"y\": 0.7358562985725914}, {\"M0\": 20.09233002565048, \"species\": \"D\", \"y\": 0.7455090415506367}, {\"M0\": 22.051307399030456, \"species\": \"D\", \"y\": 0.7548952288727201}, {\"M0\": 24.201282647943835, \"species\": \"D\", \"y\": 0.7640156722796789}, {\"M0\": 26.560877829466868, \"species\": \"D\", \"y\": 0.7728717458246792}, {\"M0\": 29.150530628251786, \"species\": \"D\", \"y\": 0.7814653442123345}, {\"M0\": 31.992671377973846, \"species\": \"D\", \"y\": 0.7897988416172199}, {\"M0\": 35.111917342151344, \"species\": \"D\", \"y\": 0.7978750512249496}, {\"M0\": 38.535285937105314, \"species\": \"D\", \"y\": 0.8056971857091976}, {\"M0\": 42.29242874389499, \"species\": \"D\", \"y\": 0.8132688188290305}, {\"M0\": 46.41588833612782, \"species\": \"D\", \"y\": 0.8205938483029968}, {\"M0\": 50.9413801481638, \"species\": \"D\", \"y\": 0.8276764600898192}, {\"M0\": 55.90810182512229, \"species\": \"D\", \"y\": 0.8345210941804637}, {\"M0\": 61.35907273413176, \"species\": \"D\", \"y\": 0.8411324119829602}, {\"M0\": 67.34150657750828, \"species\": \"D\", \"y\": 0.8475152653597271}, {\"M0\": 73.90722033525783, \"species\": \"D\", \"y\": 0.8536746673573522}, {\"M0\": 81.11308307896873, \"species\": \"D\", \"y\": 0.8596157646508609}, {\"M0\": 89.02150854450393, \"species\": \"D\", \"y\": 0.8653438117084111}, {\"M0\": 97.70099572992257, \"species\": \"D\", \"y\": 0.8708641466681002}, {\"M0\": 107.22672220103243, \"species\": \"D\", \"y\": 0.8761821689060604}, {\"M0\": 117.68119524349991, \"species\": \"D\", \"y\": 0.8813033182642038}, {\"M0\": 129.1549665014884, \"species\": \"D\", \"y\": 0.8862330558967654}, {\"M0\": 141.7474162926806, \"species\": \"D\", \"y\": 0.8909768466870598}, {\"M0\": 155.56761439304722, \"species\": \"D\", \"y\": 0.8955401431795483}, {\"M0\": 170.7352647470692, \"species\": \"D\", \"y\": 0.8999283709672571}, {\"M0\": 187.3817422860385, \"species\": \"D\", \"y\": 0.9041469154707042}, {\"M0\": 205.65123083486534, \"species\": \"D\", \"y\": 0.9082011100416706}, {\"M0\": 225.70197196339214, \"species\": \"D\", \"y\": 0.9120962253232613}, {\"M0\": 247.70763559917114, \"species\": \"D\", \"y\": 0.9158374597966633}, {\"M0\": 271.85882427329426, \"species\": \"D\", \"y\": 0.9194299314446935}, {\"M0\": 298.364724028334, \"species\": \"D\", \"y\": 0.9228786704625612}, {\"M0\": 327.4549162877732, \"species\": \"D\", \"y\": 0.9261886129471482}, {\"M0\": 359.3813663804629, \"species\": \"D\", \"y\": 0.9293645954974501}, {\"M0\": 394.420605943766, \"species\": \"D\", \"y\": 0.932411350660551}, {\"M0\": 432.87612810830615, \"species\": \"D\", \"y\": 0.9353335031595534}, {\"M0\": 475.0810162102798, \"species\": \"D\", \"y\": 0.9381355668421743}, {\"M0\": 521.400828799969, \"species\": \"D\", \"y\": 0.9408219422912241}, {\"M0\": 572.236765935022, \"species\": \"D\", \"y\": 0.9433969150408062}, {\"M0\": 628.029144183426, \"species\": \"D\", \"y\": 0.9458646543448271}, {\"M0\": 689.2612104349702, \"species\": \"D\", \"y\": 0.9482292124471873}, {\"M0\": 756.463327554629, \"species\": \"D\", \"y\": 0.9504945243058579}, {\"M0\": 830.2175681319752, \"species\": \"D\", \"y\": 0.9526644077258541}, {\"M0\": 911.1627561154896, \"species\": \"D\", \"y\": 0.9547425638589112}, {\"M0\": 1000.0, \"species\": \"D\", \"y\": 0.9567325780304037}]}}, {\"mode\": \"vega-lite\"});\n",
425
+ "</script>"
426
+ ],
427
+ "text/plain": [
428
+ "alt.LayerChart(...)"
429
+ ]
430
+ },
431
+ "execution_count": 14,
432
+ "metadata": {},
433
+ "output_type": "execute_result"
434
+ }
435
+ ],
436
+ "source": [
437
+ "import altair as alt\n",
438
+ "\n",
439
+ "source = df.melt(\"M0\", var_name=\"species\", value_name=\"y\")\n",
440
+ "\n",
441
+ "# Create a selection that chooses the nearest point & selects based on x-value\n",
442
+ "nearest = alt.selection_point(nearest=True, on=\"pointerover\",\n",
443
+ " fields=[\"M0\"], empty=False)\n",
444
+ "\n",
445
+ "# The basic line\n",
446
+ "line = alt.Chart(source).mark_line(interpolate=\"basis\").encode(\n",
447
+ " x=alt.X(\"M0:Q\", scale=alt.Scale(type=\"log\"), title='Ratio M0/kD'),\n",
448
+ " y=alt.Y(\"y:Q\", title='Fraction of total'),\n",
449
+ " color=\"species:N\",\n",
450
+ ")\n",
451
+ "\n",
452
+ "# Draw points on the line, and highlight based on selection\n",
453
+ "points = line.mark_point().encode(\n",
454
+ " opacity=alt.condition(nearest, alt.value(1), alt.value(0))\n",
455
+ ")\n",
456
+ "\n",
457
+ "# Draw a rule at the location of the selection\n",
458
+ "rules = alt.Chart(source).transform_pivot(\n",
459
+ " \"species\",\n",
460
+ " value=\"y\",\n",
461
+ " groupby=[\"M0\"]\n",
462
+ ").mark_rule(color=\"black\").encode(\n",
463
+ " x=\"M0:Q\",\n",
464
+ " opacity=alt.condition(nearest, alt.value(0.3), alt.value(0)),\n",
465
+ " tooltip=[alt.Tooltip(c, type=\"quantitative\", format=\".2f\") for c in df.columns],\n",
466
+ ").add_params(nearest)\n",
467
+ "\n",
468
+ "\n",
469
+ "# Put the five layers into a chart and bind the data\n",
470
+ "alt.layer(\n",
471
+ " line, points, rules\n",
472
+ ").properties(\n",
473
+ " width=600, height=300\n",
474
+ ")"
475
+ ]
476
+ },
477
+ {
478
+ "cell_type": "markdown",
479
+ "metadata": {},
480
+ "source": [
481
+ "From the graph, we can see that if we are at $k_D$, the ratio monomer to dimer is two-to-one, and 50% dimerization is reached at 3 times $k_D$, and for 95% saturation we need to be at > 500 times $k_D$!\n",
482
+ "\n",
483
+ "So what about the '10x above $k_D$ is full complex formation' rule? This rule only applies some situations where we study the binding between different partners, such as the formation of an enzyme-ligand complex, and will be the subject of the next post (to be published). "
484
+ ]
485
+ },
486
+ {
487
+ "cell_type": "markdown",
488
+ "metadata": {},
489
+ "source": [
490
+ "Meanwhile, we can think about _why_ its so hard to reach full dimerization even if concentrations used are far higher than the dissocation constant. If we think about the reaction from an maximum entropy point of view, out intuition might tell us that the entropic equivalent of making all promomers into a dimer is the equivalent of putting all 'air molecules' into one corner of the room: it has a vanishingly low probability of happing because its so far away from the maximium entropy state of the system. \n",
491
+ "\n",
492
+ "Second, a closer look at the differential equation describing the change of $[D]$ also tells us the answer. There are two terms in this equation: one is positive and depends on $[M]$, the second is negative and depends on $[D]$. Therefore, even when there is a lot of initial monomer $[M_0]$ compared to the $k_D$, as more and more dimer is formed $[M]$ will go down while $[D]$ goes up. Thus the positive term becomes smaller while the negative term becomes bigger, slowing down the formation of dimer, and equillibrium is reached at a point with still a large fraction of $M$ in solution. "
493
+ ]
494
+ }
495
+ ],
496
+ "metadata": {
497
+ "kernelspec": {
498
+ "display_name": ".venv-py311",
499
+ "language": "python",
500
+ "name": "python3"
501
+ },
502
+ "language_info": {
503
+ "codemirror_mode": {
504
+ "name": "ipython",
505
+ "version": 3
506
+ },
507
+ "file_extension": ".py",
508
+ "mimetype": "text/x-python",
509
+ "name": "python",
510
+ "nbconvert_exporter": "python",
511
+ "pygments_lexer": "ipython3",
512
+ "version": "3.11.9"
513
+ }
514
+ },
515
+ "nbformat": 4,
516
+ "nbformat_minor": 2
517
+ }
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ solara
2
+ pandas
3
+ altair
4
+ sympy
5
+ numpy