%load_ext pymatbridge

import cobra.test
m = cobra.test.create_test_model("textbook")

"""
Explanation: Using the COBRA toolbox with cobrapy
This example demonstrates using COBRA toolbox commands in MATLAB from python through pymatbridge.
End of explanation
"""


from cobra.io.mat import model_to_pymatbridge
model_to_pymatbridge(m, variable_name="model")

"""
Explanation: The model_to_pymatbridge function will send the model to the workspace with the given variable name.
End of explanation
"""


%%matlab
model

"""
Explanation: Now in the MATLAB workspace, the variable name 'model' holds a COBRA toolbox struct encoding the model.
End of explanation
"""


%%matlab --silent
warning('off'); % this works around a pymatbridge bug
addpath(genpath('~/cobratoolbox/'));
initCobraToolbox();

"""
Explanation: First, we have to initialize the COBRA toolbox in MATLAB.
End of explanation
"""


%%matlab
optimizeCbModel(model)

"""
Explanation: Commands from the COBRA toolbox can now be run on the model
End of explanation
"""


%time
m.optimize().f

"""
Explanation: FBA in the COBRA toolbox should give the same result as cobrapy (but maybe just a little bit slower :))
End of explanation
"""


