|
| 1 | +import streamlit as st |
| 2 | +import pandas as pd |
| 3 | +import numpy as np |
| 4 | +import matplotlib.pyplot as plt |
| 5 | +from enzyme_kinetics_fitter import fit_michaelis_menten, michaelis_menten |
| 6 | + |
| 7 | +st.set_page_config(page_title="Catalytix: Enzyme Kinetics", page_icon="🧪") |
| 8 | + |
| 9 | +st.title("🧪 Catalytix") |
| 10 | +st.markdown(""" |
| 11 | +This tool fits **Michaelis–Menten** enzyme kinetics data. |
| 12 | +Upload a CSV file with substrate concentration $[S]$ and initial velocity $v$. |
| 13 | +""") |
| 14 | + |
| 15 | +# Sidebar for inputs |
| 16 | +st.sidebar.header("Upload Data") |
| 17 | +uploaded_file = st.sidebar.file_uploader("Choose a CSV file", type="csv") |
| 18 | + |
| 19 | +if uploaded_file is not None: |
| 20 | + try: |
| 21 | + df = pd.read_csv(uploaded_file) |
| 22 | + st.write("### Data Preview") |
| 23 | + st.dataframe(df.head()) |
| 24 | + |
| 25 | + # Column selection |
| 26 | + numeric_cols = df.select_dtypes(include=[np.number]).columns.tolist() |
| 27 | + |
| 28 | + if len(numeric_cols) < 2: |
| 29 | + st.error("CSV must contain at least two numeric columns.") |
| 30 | + else: |
| 31 | + st.sidebar.header("Select Columns") |
| 32 | + s_col = st.sidebar.selectbox("Substrate Concentration ([S])", numeric_cols, index=0) |
| 33 | + v_col = st.sidebar.selectbox("Velocity (v)", numeric_cols, index=1) |
| 34 | + |
| 35 | + if st.sidebar.button("Fit Model"): |
| 36 | + S = df[s_col].to_numpy(dtype=float) |
| 37 | + v = df[v_col].to_numpy(dtype=float) |
| 38 | + |
| 39 | + try: |
| 40 | + fit = fit_michaelis_menten(S, v) |
| 41 | + |
| 42 | + # Display Results |
| 43 | + st.success("Fitting successful!") |
| 44 | + |
| 45 | + col1, col2, col3 = st.columns(3) |
| 46 | + col1.metric("Vmax", f"{fit.Vmax:.4g}", delta_color="off") |
| 47 | + col2.metric("Km", f"{fit.Km:.4g}", delta_color="off") |
| 48 | + if fit.r_squared is not None: |
| 49 | + col3.metric("R²", f"{fit.r_squared:.4f}", delta_color="off") |
| 50 | + |
| 51 | + # Detailed stats |
| 52 | + with st.expander("See detailed statistics"): |
| 53 | + st.write(f"**Vmax Std Dev:** {fit.Vmax_std:.4g}" if fit.Vmax_std else "Vmax Std Dev: N/A") |
| 54 | + st.write(f"**Km Std Dev:** {fit.Km_std:.4g}" if fit.Km_std else "Km Std Dev: N/A") |
| 55 | + |
| 56 | + # Plotting |
| 57 | + st.write("### Fitted Curve") |
| 58 | + |
| 59 | + # Generate plot data |
| 60 | + S_range = np.linspace(0, S.max() * 1.1, 200) |
| 61 | + v_fit_curve = michaelis_menten(S_range, fit.Vmax, fit.Km) |
| 62 | + |
| 63 | + fig, ax = plt.subplots(figsize=(8, 5)) |
| 64 | + ax.scatter(S, v, label="Experimental Data", color="black", zorder=5) |
| 65 | + ax.plot(S_range, v_fit_curve, label=f"Fit (Vmax={fit.Vmax:.2f}, Km={fit.Km:.2f})", color="red", linewidth=2) |
| 66 | + |
| 67 | + ax.set_xlabel(s_col) |
| 68 | + ax.set_ylabel(v_col) |
| 69 | + ax.set_title("Michaelis–Menten Fit") |
| 70 | + ax.legend() |
| 71 | + ax.grid(True, linestyle="--", alpha=0.5) |
| 72 | + |
| 73 | + st.pyplot(fig) |
| 74 | + |
| 75 | + # Residuals |
| 76 | + st.write("### Residuals") |
| 77 | + v_pred = michaelis_menten(S, fit.Vmax, fit.Km) |
| 78 | + residuals = v - v_pred |
| 79 | + |
| 80 | + fig_res, ax_res = plt.subplots(figsize=(8, 3)) |
| 81 | + ax_res.axhline(0, linestyle="--", color="gray") |
| 82 | + ax_res.scatter(S, residuals, color="blue") |
| 83 | + ax_res.set_xlabel(s_col) |
| 84 | + ax_res.set_ylabel("Residuals") |
| 85 | + ax_res.grid(True, linestyle="--", alpha=0.5) |
| 86 | + |
| 87 | + st.pyplot(fig_res) |
| 88 | + |
| 89 | + except Exception as e: |
| 90 | + st.error(f"Fitting failed: {e}") |
| 91 | + |
| 92 | + except Exception as e: |
| 93 | + st.error(f"Error reading file: {e}") |
| 94 | +else: |
| 95 | + st.info("Please upload a CSV file to get started.") |
| 96 | + |
| 97 | + # Example data button |
| 98 | + if st.button("Load Example Data"): |
| 99 | + # Create a dummy dataframe for demonstration |
| 100 | + data = { |
| 101 | + "Substrate_mM": [0.1, 0.2, 0.5, 1.0, 2.0, 5.0, 10.0], |
| 102 | + "Rate_uM_s": [0.05, 0.09, 0.22, 0.38, 0.60, 0.88, 1.05] |
| 103 | + } |
| 104 | + df_example = pd.DataFrame(data) |
| 105 | + csv = df_example.to_csv(index=False).encode('utf-8') |
| 106 | + |
| 107 | + st.download_button( |
| 108 | + label="Download Example CSV", |
| 109 | + data=csv, |
| 110 | + file_name="example_kinetics_data.csv", |
| 111 | + mime="text/csv", |
| 112 | + ) |
0 commit comments