{ "cells": [ { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "# Using the COBRA toolbox with cobrapy\n", "\n", "This example demonstrates using COBRA toolbox commands in MATLAB from python through [pymatbridge](http://arokem.github.io/python-matlab-bridge/)." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Starting MATLAB on ZMQ socket ipc:///tmp/pymatbridge-57ff5429-02d9-4e1a-8ed0-44e391fb0df7\n", "Send 'exit' command to kill the server\n", "....MATLAB started and connected!\n" ] } ], "source": [ "%load_ext pymatbridge" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import cobra.test\n", "m = cobra.test.create_test_model(\"textbook\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The model_to_pymatbridge function will send the model to the workspace with the given variable name." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from cobra.io.mat import model_to_pymatbridge\n", "model_to_pymatbridge(m, variable_name=\"model\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now in the MATLAB workspace, the variable name 'model' holds a COBRA toolbox struct encoding the model." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "\n", "model = \n", "\n", " rev: [95x1 double]\n", " metNames: {72x1 cell}\n", " b: [72x1 double]\n", " metCharge: [72x1 double]\n", " c: [95x1 double]\n", " csense: [72x1 char]\n", " genes: {137x1 cell}\n", " metFormulas: {72x1 cell}\n", " rxns: {95x1 cell}\n", " grRules: {95x1 cell}\n", " rxnNames: {95x1 cell}\n", " description: [11x1 char]\n", " S: [72x95 double]\n", " ub: [95x1 double]\n", " lb: [95x1 double]\n", " mets: {72x1 cell}\n", " subSystems: {95x1 cell}\n", "\n" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%%matlab\n", "model" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "First, we have to initialize the COBRA toolbox in MATLAB." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true }, "outputs": [], "source": [ "%%matlab --silent\n", "warning('off'); % this works around a pymatbridge bug\n", "addpath(genpath('~/cobratoolbox/'));\n", "initCobraToolbox();" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Commands from the COBRA toolbox can now be run on the model" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "\n", "ans = \n", "\n", " x: [95x1 double]\n", " f: 0.8739\n", " y: [71x1 double]\n", " w: [95x1 double]\n", " stat: 1\n", " origStat: 5\n", " solver: 'glpk'\n", " time: 3.2911\n", "\n" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%%matlab\n", "optimizeCbModel(model)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "FBA in the COBRA toolbox should give the same result as cobrapy (but maybe just a little bit slower :))" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 0 ns, sys: 0 ns, total: 0 ns\n", "Wall time: 5.48 µs\n" ] }, { "data": { "text/plain": [ "0.8739215069684909" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%time\n", "m.optimize().f" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.4.3" } }, "nbformat": 4, "nbformat_minor": 0 }