{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Reading and Writing Models\n", "\n", "Cobrapy supports reading and writing models in SBML (with and without FBC), JSON, MAT, and pickle formats. Generally, SBML with FBC version 2 is the preferred format for general use. The JSON format may be more useful for cobrapy-specific functionality.\n", "\n", "The package also ships with test models in various formats for testing purposes." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "mini test files: \n", "mini.mat, mini_cobra.xml, mini.json, mini_fbc2.xml.gz, mini_fbc2.xml.bz2, mini_fbc2.xml, mini_fbc1.xml, mini.pickle\n" ] } ], "source": [ "import cobra.test\n", "import os\n", "from os.path import join\n", "\n", "data_dir = cobra.test.data_directory\n", "\n", "print(\"mini test files: \")\n", "print(\", \".join(i for i in os.listdir(data_dir)\n", " if i.startswith(\"mini\")))\n", "\n", "\n", "\n", "textbook_model = cobra.test.create_test_model(\"textbook\")\n", "ecoli_model = cobra.test.create_test_model(\"ecoli\")\n", "salmonella_model = cobra.test.create_test_model(\"salmonella\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## SBML" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The [Systems Biology Markup Language](http://sbml.org) is an XML-based standard format for distributing models which has support for COBRA models through the [FBC extension](http://sbml.org/Documents/Specifications/SBML_Level_3/Packages/Flux_Balance_Constraints_%28flux%29) version 2.\n", "\n", "Cobrapy has native support for reading and writing SBML with FBCv2. Please note that all id's in the model must conform to the SBML SID requirements in order to generate a valid SBML file." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cobra.io.read_sbml_model(join(data_dir, \"mini_fbc2.xml\"))" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [], "source": [ "cobra.io.write_sbml_model(textbook_model, \"test_fbc2.xml\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There are other dialects of SBML prior to FBC 2 which have previously been use to encode COBRA models. The primary ones is the \"COBRA\" dialect which used the \"notes\" fields in SBML files.\n", "\n", "Cobrapy can use [libsbml](http://sbml.org/Software/libSBML), which must be installed separately (see installation instructions) to read and write these files. When reading in a model, it will automatically detect whether fbc was used or not. When writing a model, the use_fbc_package flag can be used can be used to write files in this legacy \"cobra\" format." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cobra.io.read_sbml_model(join(data_dir, \"mini_cobra.xml\"))" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [], "source": [ "cobra.io.write_sbml_model(textbook_model, \"test_cobra.xml\",\n", " use_fbc_package=False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## JSON\n", "\n", "cobrapy models have a [JSON](https://en.wikipedia.org/wiki/JSON) (JavaScript Object Notation) representation. This format was crated for interoperability with [escher](https://escher.github.io)." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cobra.io.load_json_model(join(data_dir, \"mini.json\"))" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": true }, "outputs": [], "source": [ "cobra.io.save_json_model(textbook_model, \"test.json\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## MATLAB" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Often, models may be imported and exported soley for the purposes of working with the same models in cobrapy and the [MATLAB cobra toolbox](http://opencobra.github.io/cobratoolbox/). MATLAB has its own \".mat\" format for storing variables. Reading and writing to these mat files from python requires scipy.\n", "\n", "A mat file can contain multiple MATLAB variables. Therefore, the variable name of the model in the MATLAB file can be passed into the reading function:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cobra.io.load_matlab_model(join(data_dir, \"mini.mat\"),\n", " variable_name=\"mini_textbook\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If the mat file contains only a single model, cobra can figure out which variable to read from, and the variable_name paramter is unnecessary." ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cobra.io.load_matlab_model(join(data_dir, \"mini.mat\"))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Saving models to mat files is also relatively straightforward" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [], "source": [ "cobra.io.save_matlab_model(textbook_model, \"test.mat\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Pickle" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Cobra models can be serialized using the python serialization format, [pickle](https://docs.python.org/2/library/pickle.html).\n", "\n", "Please note that use of the pickle format is generally not recommended for most use cases. JSON, SBML, and MAT are generally the preferred formats." ] } ], "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 }