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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# %%
|
| 2 |
+
|
| 3 |
+
from pathlib import Path
|
| 4 |
+
|
| 5 |
+
import altair as alt
|
| 6 |
+
import numpy as np
|
| 7 |
+
import pandas as pd
|
| 8 |
+
import solara
|
| 9 |
+
import solara.lab
|
| 10 |
+
import sympy as sp
|
| 11 |
+
from scipy.optimize import root_scalar
|
| 12 |
+
|
| 13 |
+
# %%
|
| 14 |
+
P1, P2, P4, PT, kD1, kD2 = sp.symbols("P_1 P_2 P_4 P_T k_D1 k_D2", positive=True)
|
| 15 |
+
|
| 16 |
+
# %%
|
| 17 |
+
sub_p1_p2 = (P1, sp.solve(kD1 * (P2 / P1**2) - 1, P1)[0])
|
| 18 |
+
sub_p2_p4 = (P2, sp.solve(kD2 * (P4 / P2**2) - 1, P2)[0])
|
| 19 |
+
sub_p4_p2 = (P4, sp.solve(kD2 * (P4 / P2**2) - 1, P4)[0])
|
| 20 |
+
|
| 21 |
+
# %%
|
| 22 |
+
mass_balance = P1 + 2 * P2 + 4 * P4 - PT
|
| 23 |
+
|
| 24 |
+
eq_p4 = mass_balance.subs([sub_p1_p2, sub_p2_p4])
|
| 25 |
+
eq_p2 = mass_balance.subs([sub_p1_p2, sub_p4_p2])
|
| 26 |
+
|
| 27 |
+
# %%
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def make_df(vmin: float, vmax: float, kD_1_v: float, kD2_v: float) -> pd.DataFrame:
|
| 31 |
+
PT_values = np.logspace(np.log10(vmin), np.log10(vmax), endpoint=True, num=100)
|
| 32 |
+
|
| 33 |
+
kd_subs = [(kD1, kD_1_v), (kD2, kD2_v)]
|
| 34 |
+
|
| 35 |
+
ld = sp.lambdify([P4, PT], eq_p4.subs(kd_subs))
|
| 36 |
+
P4_values = np.array(
|
| 37 |
+
[root_scalar(ld, bracket=(0, PT_v), args=(PT_v,)).root for PT_v in PT_values]
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
ld = sp.lambdify([P2, PT], eq_p2.subs(kd_subs))
|
| 41 |
+
P2_values = np.array(
|
| 42 |
+
[root_scalar(ld, bracket=(0, PT_v), args=(PT_v,)).root for PT_v in PT_values]
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
P1_values = PT_values - 2 * P2_values - 4 * P4_values
|
| 46 |
+
|
| 47 |
+
columns = {"P1": P1_values, "P2": P2_values, "P4": P4_values}
|
| 48 |
+
total = np.sum(list(columns.values()), axis=0)
|
| 49 |
+
|
| 50 |
+
df = pd.DataFrame(dict(PT=PT_values) | {k: v / total for k, v in columns.items()})
|
| 51 |
+
|
| 52 |
+
return df
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
def make_chart(df: pd.DataFrame) -> alt.LayerChart:
|
| 56 |
+
source = df.melt("PT", var_name="species", value_name="y")
|
| 57 |
+
|
| 58 |
+
# Create a selection that chooses the nearest point & selects based on x-value
|
| 59 |
+
nearest = alt.selection_point(
|
| 60 |
+
nearest=True, on="pointerover", fields=["PT"], empty=False
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
# The basic line
|
| 64 |
+
line = (
|
| 65 |
+
alt.Chart(source)
|
| 66 |
+
.mark_line(interpolate="basis")
|
| 67 |
+
.encode(
|
| 68 |
+
x=alt.X(
|
| 69 |
+
"PT:Q",
|
| 70 |
+
scale=alt.Scale(type="log"),
|
| 71 |
+
title="Total protomer concentration",
|
| 72 |
+
),
|
| 73 |
+
y=alt.Y("y:Q", title="Fraction of total"),
|
| 74 |
+
color="species:N",
|
| 75 |
+
)
|
| 76 |
+
.properties(width="container")
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
# Draw points on the line, and highlight based on selection
|
| 80 |
+
points = (
|
| 81 |
+
line.mark_point()
|
| 82 |
+
.encode(opacity=alt.condition(nearest, alt.value(1), alt.value(0)))
|
| 83 |
+
.properties(width="container")
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
# Draw a rule at the location of the selection
|
| 87 |
+
rules = (
|
| 88 |
+
alt.Chart(source)
|
| 89 |
+
.transform_pivot("species", value="y", groupby=["PT"])
|
| 90 |
+
.mark_rule(color="black")
|
| 91 |
+
.encode(
|
| 92 |
+
x="PT:Q",
|
| 93 |
+
opacity=alt.condition(nearest, alt.value(0.3), alt.value(0)),
|
| 94 |
+
tooltip=[
|
| 95 |
+
alt.Tooltip(c, type="quantitative", format=".2f") for c in df.columns
|
| 96 |
+
],
|
| 97 |
+
)
|
| 98 |
+
.add_params(nearest)
|
| 99 |
+
.properties(width="container")
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
# Put the five layers into a chart and bind the data
|
| 103 |
+
chart = (
|
| 104 |
+
alt.layer(line, points, rules)
|
| 105 |
+
.properties(height=300)
|
| 106 |
+
.configure(autosize="fit-x")
|
| 107 |
+
)
|
| 108 |
+
|
| 109 |
+
return chart
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
md = """
|
| 113 |
+
This app calculates monomer and dimer concentrations given a total amount of protomer PT and the
|
| 114 |
+
dissociation constant KD. More info on how and why can be found [HuggingFace](https://huggingface.co/spaces/Jhsmit/binding-kinetics) (right click, open new tab).
|
| 115 |
+
"""
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
@solara.component
|
| 119 |
+
def Page():
|
| 120 |
+
solara.Style(Path("style.css"))
|
| 121 |
+
|
| 122 |
+
dark_effective = solara.lab.use_dark_effective()
|
| 123 |
+
if dark_effective is True:
|
| 124 |
+
alt.themes.enable("dark")
|
| 125 |
+
|
| 126 |
+
elif dark_effective is False:
|
| 127 |
+
alt.themes.enable("default")
|
| 128 |
+
|
| 129 |
+
kD1 = solara.use_reactive(1.0)
|
| 130 |
+
kD2 = solara.use_reactive(100)
|
| 131 |
+
|
| 132 |
+
vmin = solara.use_reactive(1e-3)
|
| 133 |
+
vmax = solara.use_reactive(1e3)
|
| 134 |
+
|
| 135 |
+
async def update():
|
| 136 |
+
df = make_df(vmin.value, vmax.value, kD1.value, kD2.value)
|
| 137 |
+
chart = make_chart(df)
|
| 138 |
+
|
| 139 |
+
return chart
|
| 140 |
+
|
| 141 |
+
task: solara.lab.Task = solara.lab.use_task(
|
| 142 |
+
update, dependencies=[kD1.value, kD2.value, vmin.value, vmax.value]
|
| 143 |
+
)
|
| 144 |
+
|
| 145 |
+
solara.Title("Tetramerization Kinetics")
|
| 146 |
+
|
| 147 |
+
with solara.Card("Fraction monomer/dimer/tetramer"):
|
| 148 |
+
with solara.GridFixed(columns=2):
|
| 149 |
+
with solara.Tooltip("Dissociation constant monomer/dimer"):
|
| 150 |
+
solara.InputFloat("kD1", value=kD1)
|
| 151 |
+
with solara.Tooltip("Dissociation constant dimer/tetramer"):
|
| 152 |
+
solara.InputFloat("kD2", value=kD2)
|
| 153 |
+
with solara.Tooltip("X axis lower limit"):
|
| 154 |
+
solara.InputFloat("xmin", value=vmin)
|
| 155 |
+
with solara.Tooltip("X axis upper limit"):
|
| 156 |
+
solara.InputFloat("xmax", value=vmax)
|
| 157 |
+
solara.HTML(tag="div", style="height: 10px")
|
| 158 |
+
|
| 159 |
+
if task.finished:
|
| 160 |
+
solara.FigureAltair(task.value)
|
| 161 |
+
|
| 162 |
+
|
| 163 |
+
# %%
|