Spaces:
Runtime error
Runtime error
add pycafe code
Browse files- cafe_app.py +138 -0
cafe_app.py
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pathlib import Path
|
| 2 |
+
|
| 3 |
+
import altair as alt
|
| 4 |
+
import numpy as np
|
| 5 |
+
import pandas as pd
|
| 6 |
+
import solara
|
| 7 |
+
import sympy as sp
|
| 8 |
+
|
| 9 |
+
#
|
| 10 |
+
P1, P2, PT, k_on, k_off, kD = sp.symbols("P_1 P_2 P_T k_on k_off k_D", positive=True)
|
| 11 |
+
|
| 12 |
+
sol = sp.solve(
|
| 13 |
+
[
|
| 14 |
+
-2 * k_on * P1 * P1 + 2 * k_off * P2,
|
| 15 |
+
P1 + 2 * P2 - PT,
|
| 16 |
+
(k_off / k_on) - kD,
|
| 17 |
+
],
|
| 18 |
+
[P1, P2, k_on, k_off],
|
| 19 |
+
dict=True,
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
solve_for = [P1, P2]
|
| 23 |
+
inputs = [PT, kD]
|
| 24 |
+
|
| 25 |
+
lambdas = {s: sp.lambdify(inputs, sol[0][s]) for s in solve_for}
|
| 26 |
+
ld_total = sp.lambdify(inputs, sol[0][P1] + sol[0][P2])
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def make_chart(df: pd.DataFrame) -> alt.Chart:
|
| 30 |
+
source = df.melt("PT", var_name="species", value_name="y")
|
| 31 |
+
|
| 32 |
+
# Create a selection that chooses the nearest point & selects based on x-value
|
| 33 |
+
nearest = alt.selection_point(
|
| 34 |
+
nearest=True, on="pointerover", fields=["PT"], empty=False
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
# The basic line
|
| 38 |
+
line = (
|
| 39 |
+
alt.Chart(source)
|
| 40 |
+
.mark_line(interpolate="basis")
|
| 41 |
+
.encode(
|
| 42 |
+
x=alt.X("PT:Q", scale=alt.Scale(type="log"), title="Ratio PT/kD"),
|
| 43 |
+
y=alt.Y("y:Q", title="Fraction of total"),
|
| 44 |
+
color="species:N",
|
| 45 |
+
)
|
| 46 |
+
.properties(width="container")
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
# Draw points on the line, and highlight based on selection
|
| 50 |
+
points = (
|
| 51 |
+
line.mark_point()
|
| 52 |
+
.encode(opacity=alt.condition(nearest, alt.value(1), alt.value(0)))
|
| 53 |
+
.properties(width="container")
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
# Draw a rule at the location of the selection
|
| 57 |
+
rules = (
|
| 58 |
+
alt.Chart(source)
|
| 59 |
+
.transform_pivot("species", value="y", groupby=["PT"])
|
| 60 |
+
.mark_rule(color="black")
|
| 61 |
+
.encode(
|
| 62 |
+
x="PT:Q",
|
| 63 |
+
opacity=alt.condition(nearest, alt.value(0.3), alt.value(0)),
|
| 64 |
+
tooltip=[
|
| 65 |
+
alt.Tooltip(c, type="quantitative", format=".2f") for c in df.columns
|
| 66 |
+
],
|
| 67 |
+
)
|
| 68 |
+
.add_params(nearest)
|
| 69 |
+
.properties(width="container")
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
# Put the five layers into a chart and bind the data
|
| 73 |
+
chart = (
|
| 74 |
+
alt.layer(line, points, rules)
|
| 75 |
+
.properties(height=300)
|
| 76 |
+
.configure(autosize="fit-x")
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
return chart
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
md = """
|
| 83 |
+
This app calculates monomer and dimer concentrations given a total amount of protomer PT and the
|
| 84 |
+
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).
|
| 85 |
+
"""
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
@solara.component
|
| 89 |
+
def Page():
|
| 90 |
+
solara.Style(Path("style.css"))
|
| 91 |
+
|
| 92 |
+
dark_effective = solara.lab.use_dark_effective()
|
| 93 |
+
if dark_effective is True:
|
| 94 |
+
alt.themes.enable("dark")
|
| 95 |
+
|
| 96 |
+
elif dark_effective is False:
|
| 97 |
+
alt.themes.enable("default")
|
| 98 |
+
|
| 99 |
+
PT = solara.use_reactive(10.0)
|
| 100 |
+
kD = solara.use_reactive(1.0)
|
| 101 |
+
|
| 102 |
+
vmin = solara.use_reactive(-1)
|
| 103 |
+
vmax = solara.use_reactive(3)
|
| 104 |
+
|
| 105 |
+
ans = {k: ld(PT.value, kD.value) for k, ld in lambdas.items()}
|
| 106 |
+
|
| 107 |
+
solara.Title("Dimerization Kinetics")
|
| 108 |
+
with solara.Card("Calculate concentrations from kD"):
|
| 109 |
+
solara.Markdown(md)
|
| 110 |
+
with solara.GridFixed(columns=2):
|
| 111 |
+
with solara.Tooltip("Total protomer concentration"):
|
| 112 |
+
solara.InputFloat("PT", value=PT)
|
| 113 |
+
with solara.Tooltip("Dissociation constant"):
|
| 114 |
+
solara.InputFloat("kD", value=kD)
|
| 115 |
+
solara.Markdown(f"### Concentration monomer: {ans[P1]:.2f}")
|
| 116 |
+
solara.Markdown(f"### Concentration dimer: {ans[P2]:.2f}")
|
| 117 |
+
|
| 118 |
+
# create a vector of PT values ranging from 0.1 times kD to 1000 times kD
|
| 119 |
+
def update():
|
| 120 |
+
PT_values = np.logspace(vmin.value, vmax.value, endpoint=True, num=100)
|
| 121 |
+
ans = {
|
| 122 |
+
k: ld(PT_values, 1) / ld_total(PT_values, 1) for k, ld in lambdas.items()
|
| 123 |
+
}
|
| 124 |
+
|
| 125 |
+
# put the results in a dataframe, together with input PT values
|
| 126 |
+
df = pd.DataFrame(dict(PT=PT_values) | {k.name: v for k, v in ans.items()})
|
| 127 |
+
return make_chart(df)
|
| 128 |
+
|
| 129 |
+
chart = solara.use_memo(update, [vmin.value, vmax.value])
|
| 130 |
+
|
| 131 |
+
with solara.Card("Fraction monomer/dimer vs ratio over kD"):
|
| 132 |
+
with solara.Row():
|
| 133 |
+
with solara.Tooltip("X axis lower limit (log10)"):
|
| 134 |
+
solara.InputFloat("xmin", value=vmin)
|
| 135 |
+
with solara.Tooltip("X axis upper limit (log10)"):
|
| 136 |
+
solara.InputFloat("xmax", value=vmax)
|
| 137 |
+
solara.HTML(tag="div", style="height: 10px")
|
| 138 |
+
solara.FigureAltair(chart)
|