#!/usr/bin/env python # coding: utf-8 # # Using the COBRA toolbox with cobrapy # # This example demonstrates using COBRA toolbox commands in MATLAB from python through [pymatbridge](http://arokem.github.io/python-matlab-bridge/). # In[1]: get_ipython().run_line_magic('load_ext', 'pymatbridge') # In[2]: import cobra.test m = cobra.test.create_test_model("textbook") # The model_to_pymatbridge function will send the model to the workspace with the given variable name. # In[3]: from cobra.io.mat import model_to_pymatbridge model_to_pymatbridge(m, variable_name="model") # Now in the MATLAB workspace, the variable name 'model' holds a COBRA toolbox struct encoding the model. # In[4]: get_ipython().run_cell_magic('matlab', '', 'model\n') # First, we have to initialize the COBRA toolbox in MATLAB. # In[5]: get_ipython().run_cell_magic('matlab', '--silent', "warning('off'); % this works around a pymatbridge bug\naddpath(genpath('~/cobratoolbox/'));\ninitCobraToolbox();\n") # Commands from the COBRA toolbox can now be run on the model # In[6]: get_ipython().run_cell_magic('matlab', '', 'optimizeCbModel(model)\n') # FBA in the COBRA toolbox should give the same result as cobrapy (but maybe just a little bit slower :)) # In[7]: get_ipython().run_line_magic('time', '') m.optimize().f