Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inside cow interface #12

Merged
merged 6 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions models/inside-cattle/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,24 @@ This model aims to determine the impact on both methane production and the compl
Two models are integrated:
`build_metabolic_network.py`: focused on exploring the metabolic pathways and flux distribution towards production of methane in M.Ruminantium.
`fermentation_model.m`: focused on exploring the rumen-wide consumption and production of key metabolites by amino acid-utilizing bacteria, hydrogen-utilizing bacteria (methanogens), and sugar-utilizing bacteria, and their microbial growth in relation to nutrient availability.
`app.py`: a streamlit app that allows users to interact with the fermentation model and observe the impact of different parameters on the rumen microbiome and methane production.

### To Run:
`build_metabolic_network.py`: ensure that CobraPy is installed to a current version of python. Download `/data/M_Methanobrevibacter_ruminantium_M1__44____32__AGORA__32__version__32__1__46__03.sbml` and ensure it is placed in the same local directory, and run using an IDE of choice.
`fermentation_model.m`: ensure that MATLAB and numeric toolboxes are installed. Download the script, and run.

To run the interface:
1. Download Matlab Engine
`python -m pip install matlabengine`
Make sure to install the correct version of Matlab Engine for your [Matlab version](https://www.mathworks.com/support/requirements/python-compatibility.html)
2. Install streamlit
`pip install streamlit`
3. cd into the directory containing the app.py file
`cd models/inside-cattle/scripts`
4. Run the app
`streamlit run app.py`

### Assets:
`fermentation_model.m` outputs a plot of fifteen major state variables, consisting of feed components, soluble metabolite concentrations, and bacterial concentrations in the cow rumen with time. It outputs a plot of all of these variables, under a typical state and a reduced methanogenic state, where hydrogen-utilizer concentration has been reduced to simulate interference with M. ruminantium microbial growth.

`fermentation_model_for_app.m` is the wrapper version of the model for the app interface
115 changes: 115 additions & 0 deletions models/inside-cattle/scripts/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import streamlit as st
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from scipy.io import loadmat
import matlab.engine
import seaborn as sns
import os

eng = matlab.engine.start_matlab()
eng.cd(os.path.dirname(__file__))
eng.addpath(os.path.abspath(os.path.dirname(__file__)))
print("Rerun")
print(eng.pwd())
result = eng.eval("which('fermentation_model')")
print(eng.ls())
# eng.eval("run('fermentation_model.m')")


st.title('Inside Cattle ODE using MATLAB Rumen Model')
with st.expander("Initial Conditions", expanded=True):
st.sidebar.write("Initial conditions for the ODE system:")
z_NDF_0 = st.sidebar.number_input('z_NDF_0 (Cell wall carbohydrates)', value=2.0, format="%.5f")
z_NSC_0 = st.sidebar.number_input('z_NSC_0 (Non-fiber carbohydrates)', value=5.0, format="%.5f")
z_pro_0 = st.sidebar.number_input('z_pro_0 (Proteins)', value=1.25, format="%.5f")
s_su_0 = st.sidebar.number_input('s_su_0 (Soluble sugars)', value=0.00067, format="%.5f")
s_aa_0 = st.sidebar.number_input('s_aa_0 (Soluble amino acids)', value=0.0, format="%.5f")
s_H2_0 = st.sidebar.number_input('s_H2_0 (Soluble hydrogen)', value=0.000002, format="%.5f")
s_ac_0 = st.sidebar.number_input('s_ac_0 (Soluble acetate)', value=0.006, format="%.5f")
s_bu_0 = st.sidebar.number_input('s_bu_0 (Soluble butyrate)', value=0.01, format="%.5f")
s_pr_0 = st.sidebar.number_input('s_pr_0 (Soluble proline)', value=0.00005, format="%.5f")
s_IN_0 = st.sidebar.number_input('s_IN_0 (Inorganic nitrogen)', value=0.025, format="%.5f")
s_IC_0 = st.sidebar.number_input('s_IC_0 (Inorganic carbon)', value=0.14, format="%.5f")
s_CH4_0 = st.sidebar.number_input('s_CH4_0 (Soluble methane)', value=0.0007, format="%.5f")
x_su_0 = st.sidebar.number_input('x_su_0 (Sugar utilizers)', value=0.01, format="%.5f")
x_aa_0 = st.sidebar.number_input('x_aa_0 (Amino acid utilizers)', value=0.005, format="%.5f")
x_H2_0 = st.sidebar.number_input('x_H2_0 (Hydrogen utilizers)', value=0.00075, format="%.5f")
ng_H2_0 = st.sidebar.number_input('ng_H2_0 (Gas phase hydrogen)', value=0.00006, format="%.5f")
ng_CO2_0 = st.sidebar.number_input('ng_CO2_0 (Gas phase CO2)', value=0.76, format="%.5f")
ng_CH4_0 = st.sidebar.number_input('ng_CH4_0 (Gas phase methane)', value=0.35, format="%.5f")


y0 = [
z_NDF_0, z_NSC_0, z_pro_0, s_su_0, s_aa_0, s_H2_0,
s_ac_0, s_bu_0, s_pr_0, s_CH4_0, s_IC_0, s_IN_0,
x_su_0, x_aa_0, x_H2_0, ng_H2_0, ng_CO2_0, ng_CH4_0
]

x_H2_1 = x_H2_0 / 1 * 0.25
x_su_1 = x_su_0 / 94 * (94 + 0.95 * 0.75)
x_aa_1 = x_aa_0 / 5 * (5 + 0.05 * 0.75)

reduced_y0 = [
z_NDF_0, z_NSC_0, z_pro_0, s_su_0, s_aa_0, s_H2_0,
s_ac_0, s_bu_0, s_pr_0, s_CH4_0, s_IC_0, s_IN_0,
x_su_1, x_aa_1, x_H2_1, ng_H2_0, ng_CO2_0, ng_CH4_0
]

tspan = [0, 30]
sns.set_palette("husl")
if st.button('Run Simulation'):
initial_conditions_mat = matlab.double(y0)
reduced_initial_conditions_mat = matlab.double(reduced_y0)

t, q, y, z = eng.fermentation_model_for_app(initial_conditions_mat, reduced_initial_conditions_mat, nargout=4)

st.header("ODE Results")

t = np.array(t)
y = np.array(y)
q = np.array(q)
z = np.array(z)

print(y)

titles = ['Zndf', 'Znsc', 'Zpro', 'Ssu', 'Saa', 'Sh2', 'Sac', 'Sbu', 'Spr', 'Sch4', 'SIC', 'SIN', 'Xsu', 'Xaa', 'Xh2']
scales = [[0, 3], [0, 6], [0, 2], [0, 6], [0, 2.5], [0, 20], [0, 100], [0, 25], [0, 20], [0, 5], [0, 200], [0, 26], [0, 40], [0, 15], [0, 2]]
ylabels = ["g/L", "g/L", "g/L", "mM", "mM", "uM", "mM", "mM", "mM", "mM", "mM", "mM", "mM", "mM", "mM"]
plot_scale_factor = [1, 1, 1, 1e3, 1e3, 1e3, 1e3, 1e3, 1e3, 1e3, 1e3, 1e3, 1e3, 1e3, 1e3]

print(t.shape, y.shape, q.shape, z.shape)
print(t[:, 0].shape, y[:, 0].shape)

fig, axs = plt.subplots(3, 5, figsize=(20, 12))

for i, ax in enumerate(axs.flatten()):
sns.lineplot(x=t[:, 0], y=y[:, i] * plot_scale_factor[i], ax=ax, label=titles[i])
ax.set_title(titles[i])
ax.set_xlabel('Time (h)')
ax.set_ylabel(ylabels[i])
ax.set_ylim(scales[i])
ax.legend()

plt.tight_layout()
st.pyplot(fig)

st.header("ODE Results (Reduced System)")

fig, axs = plt.subplots(3, 5, figsize=(20, 12))

for i, ax in enumerate(axs.flatten()):
sns.lineplot(x=q[:, 0], y=z[:, i] * plot_scale_factor[i], ax=ax, label=titles[i])
ax.set_title(titles[i])
ax.set_xlabel('Time (h)')
ax.set_ylabel(ylabels[i])
ax.set_ylim(scales[i])
ax.legend()

plt.tight_layout()
st.pyplot(fig)





219 changes: 219 additions & 0 deletions models/inside-cattle/scripts/fermentation_model_for_app.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
% initialize the ode system as a function of time, where y is the
% concentration of the state.
function [t, q, y, z] = fermentation_model_for_app(initial_conditions, reduced_initial_conditions)
y0 = initial_conditions;
y0_reduced_methanogen = reduced_initial_conditions;

tspan = [0 30];

% Solving the system
[t, y] = ode45(@(t, y) system_fct(t, y), tspan, y0);
[q, z] = ode45(@(q, z) reduced_methanogen_system_fct(q, z), tspan, y0_reduced_methanogen);
size(t)
size(y)
size(q)
size(z)
end

function dydt = system_fct(t,y)

% constants
KH_CO2=2.46e-2;
KH_CH4=1.1e-3;
KH_H2=7.23e-4;
w_aa=134;
w_mb=113;
w_su=180.16;
Vl = 0.03;

% estimated parameters (Tamayo et al.)
kL_a=1.07;
f_ch_x=0.2;
f_pro_x=0.55;
k_d=8.33e-4;
k_hyd_NDF=0.05;
k_hyd_NSC=0.2;
k_hyd_pro=0.22;
k_s_su=9e-3;
k_m_su=0.99;
k_s_IN=2e-4;
Y_su=2e-4;
lambda1=0.43;
lambda2=0.29;
lambda3=0.28;
k_s_aa=6.4e-3;
k_m_aa=1.98;
Y_aa=0.31;
sigma_ac_aa=0.67;
sigma_bu_aa=0.24;
sigma_pr_aa=0.062;
sigma_H2_aa=0.82;
sigma_IC2_aa=0.88;
k_s_H2=5.84e-6;
k_m_H2=13.93;
Y_H2=0.0016;

% Kinetic Rates of each state variable.
I_IN = y(12)/(y(12)+k_s_IN);
P_NDF = k_hyd_NDF * y(1);
P_NSC = k_hyd_NSC * y(2);
P_pro = k_hyd_pro * y(3);
P_su = k_m_su * (y(4) / (k_s_su + y(4))) * y(13) * I_IN; % Needs I_IN (y(10))
P_aa = k_m_aa * (y(5) / (k_s_aa + y(5))) * y(14);
P_H2 = k_m_H2 * (y(6) / (k_s_H2 + y(6))) * y(15) * I_IN; % Needs I_IN (y(10))
P_x_su = k_d * y(13);
P_x_aa = k_d * y(14);
P_x_H2 = k_d * y(15);

% Gas conversion
P = 1.01325;
pg_H2 = y(16) / ( y(16) + y(17) + y(18) ) * P;
pg_CO2 = y(17) / ( y(16) + y(17) + y(18) ) * P;
pg_CH4 = y(18) / ( y(16) + y(17) + y(18) ) * P;
P_T_H2 = kL_a*(y(6)-KH_H2*pg_H2);
P_T_CO2 = kL_a*(y(7)-KH_CO2*pg_CO2);
P_T_CH4 = kL_a*(y(10)-KH_CH4*pg_CH4);


% Yield functions
f_su = 1 - (5/6) * Y_su;
Y_ac_su = f_su * (2 * lambda1 + (2/3) * lambda2);
Y_bu_su = f_su * lambda3;
Y_pr_su = f_su * (4/3) * lambda2;
Y_H2_su = f_su * (4 * lambda1 + 2 * lambda3);
Y_IN_su = -Y_su;
Y_IC_su = f_su * (2 * lambda1 + (2/3) * lambda2 + 2 * lambda3);
f_H2 = 1 - 10 * Y_H2;
Y_CH4_H2 = f_H2 * 0.25;
Y_IC_H2 = -((0.25 * f_H2) + 0.5 * (1 - f_H2));
Y_IN_H2 = -Y_H2;
Y_H2_aa = (1 - Y_H2) * sigma_H2_aa;
Y_ac_aa = (1 - Y_ac_su) * sigma_ac_aa;
Y_bu_aa = (1 - Y_bu_su) * sigma_bu_aa;
Y_pr_aa = (1 - Y_pr_su) * sigma_pr_aa;
Y_IN_aa = 0.067 - Y_aa*0.059;
Y_IC_aa = (1 - Y_IC_su) * sigma_IC2_aa;

% System of DEs
dydt = zeros(18, 1);
dydt(1) = -P_NDF;
dydt(2) = -P_NSC+(f_ch_x*w_mb)*(P_x_su+P_x_aa+P_x_H2);
dydt(3) = -P_pro+(f_pro_x*w_mb)*(P_x_su+P_x_aa+P_x_H2);
dydt(4) = (P_NDF/w_su)+(P_NSC/w_su)-P_su;
dydt(5) = (P_pro/w_aa)-P_aa;
dydt(6) = Y_H2_aa*P_aa+Y_H2_su*P_su-P_T_H2;
dydt(7) = Y_ac_su*P_su+Y_ac_aa*P_aa;
dydt(8) = Y_bu_su*P_su+Y_bu_aa*P_aa;
dydt(9) = Y_pr_su*P_su+Y_pr_aa*P_aa;
dydt(10) = Y_CH4_H2*P_H2-P_T_CH4;
dydt(11) = Y_IC_aa*P_aa+Y_IC_su*P_su+Y_IC_H2*P_H2-P_T_CO2;
dydt(12) = Y_IN_aa*P_aa+Y_IN_su*P_su+Y_IN_H2*P_H2;
dydt(13) = Y_su*P_su-P_x_su;
dydt(14) = Y_aa*P_aa-P_x_aa;
dydt(15) = Y_H2*P_H2-P_x_H2;
dydt(16) = Vl * P_T_H2;
dydt(17) = Vl * P_T_CO2;
dydt(18) = Vl * P_T_CH4;

end

% Reduced methanogenic state

function dzdt = reduced_methanogen_system_fct(q,z)
% constants
KH_CO2=2.46e-2;
KH_CH4=1.1e-3;
KH_H2=7.23e-4;
w_aa=134;
w_mb=113;
w_su=180.16;
Vl = 0.03;
P = 1.01325;

% estimated parameters (Tamayo et al.)
kL_a=1.07;
f_ch_x=0.2;
f_pro_x=0.55;
k_d=8.33e-4;
k_hyd_NDF=0.05;
k_hyd_NSC=0.2;
k_hyd_pro=0.22;
k_s_su=9e-3;
k_m_su=0.99;
k_s_IN=2e-4;
Y_su=2e-4;
lambda1=0.43;
lambda2=0.29;
lambda3=0.28;
k_s_aa=6.4e-3;
k_m_aa=1.98;
Y_aa=0.31;
sigma_ac_aa=0.67;
sigma_bu_aa=0.24;
sigma_pr_aa=0.062;
sigma_H2_aa=0.82;
sigma_IC2_aa=0.88;
k_s_H2=5.84e-6;
k_m_H2=13.93;
Y_H2=0.0016;

% Kinetic Rates of each state variable.
I_IN = z(12)/(z(12)+k_s_IN);
P_NDF = k_hyd_NDF * z(1);
P_NSC = k_hyd_NSC * z(2);
P_pro = k_hyd_pro * z(3);
P_su = k_m_su * (z(4) / (k_s_su + z(4))) * z(13) * I_IN
P_aa = k_m_aa * (z(5) / (k_s_aa + z(5))) * z(14);
P_H2 = k_m_H2 * (z(6) / (k_s_H2 + z(6))) * z(15) * I_IN;
P_x_su = k_d * z(13);
P_x_aa = k_d * z(14);
P_x_H2 = k_d * z(15);
pg_H2 = z(16) / ( z(16) + z(17) + z(18) ) * P;
pg_CO2 = z(17) / ( z(16) + z(17) + z(18) ) * P;
pg_CH4 = z(18) / ( z(16) + z(17) + z(18) ) * P;
P_T_H2 = kL_a*(z(6)-KH_H2*pg_H2);
P_T_CO2 = kL_a*(z(7)-KH_CO2*pg_CO2);
P_T_CH4 = kL_a*(z(10)-KH_CH4*pg_CH4);

% Yield equations
f_su = 1 - (5/6) * Y_su;
Y_ac_su = f_su * (2 * lambda1 + (2/3) * lambda2);
Y_bu_su = f_su * lambda3;
Y_pr_su = f_su * (4/3) * lambda2;
Y_H2_su = f_su * (4 * lambda1 + 2 * lambda3);
Y_IN_su = -Y_su;
Y_IC_su = f_su * (2 * lambda1 + (2/3) * lambda2 + 2 * lambda3);
f_H2 = 1 - 10 * Y_H2;
Y_CH4_H2 = f_H2 * 0.25;
Y_IC_H2 = -((0.25 * f_H2) + 0.5 * (1 - f_H2));
Y_IN_H2 = -Y_H2;
Y_H2_aa = (1 - Y_H2) * sigma_H2_aa;
Y_ac_aa = (1 - Y_ac_su) * sigma_ac_aa;
Y_bu_aa = (1 - Y_bu_su) * sigma_bu_aa;
Y_pr_aa = (1 - Y_pr_su) * sigma_pr_aa;
Y_IN_aa = 0.067 - Y_aa*0.059;
Y_IC_aa = (1 - Y_IC_su) * sigma_IC2_aa;

% system of DEs
dzdt = zeros(18, 1);
dzdt(1) = -P_NDF;
dzdt(2) = -P_NSC+(f_ch_x*w_mb)*(P_x_su+P_x_aa+P_x_H2);
dzdt(3) = -P_pro+(f_pro_x*w_mb)*(P_x_su+P_x_aa+P_x_H2);
dzdt(4) = (P_NDF/w_su)+(P_NSC/w_su)-P_su;
dzdt(5) = (P_pro/w_aa)-P_aa;
dzdt(6) = Y_H2_aa*P_aa+Y_H2_su*P_su-P_T_H2;
dzdt(7) = Y_ac_su*P_su+Y_ac_aa*P_aa;
dzdt(8) = Y_bu_su*P_su+Y_bu_aa*P_aa;
dzdt(9) = Y_pr_su*P_su+Y_pr_aa*P_aa;
dzdt(10) = Y_CH4_H2*P_H2-P_T_CH4;
dzdt(11) = Y_IC_aa*P_aa+Y_IC_su*P_su+Y_IC_H2*P_H2-P_T_CO2;
dzdt(12) = Y_IN_aa*P_aa+Y_IN_su*P_su+Y_IN_H2*P_H2;
dzdt(13) = Y_su*P_su-P_x_su;
dzdt(14) = Y_aa*P_aa-P_x_aa;
dzdt(15) = Y_H2*P_H2-P_x_H2;
dzdt(16) = Vl * P_T_H2;
dzdt(17) = Vl * P_T_CO2;
dzdt(18) = Vl * P_T_CH4;
end


Loading
Loading