{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Reading and Writing Models" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Cobrapy supports reading and writing models in SBML (with and without FBC), JSON, YAML, 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": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "mini test files: \n", "mini.json, mini.mat, mini.pickle, mini.yml, mini_cobra.xml, mini_fbc1.xml, mini_fbc2.xml, mini_fbc2.xml.bz2, mini_fbc2.xml.gz\n" ] } ], "source": [ "import cobra.test\n", "import os\n", "from os.path import join\n", "\n", "data_dir = cobra.test.data_dir\n", "\n", "print(\"mini test files: \")\n", "print(\", \".join(i for i in os.listdir(data_dir) if i.startswith(\"mini\")))\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": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Namemini_textbook
Memory address0x01074fd080
Number of metabolites23
Number of reactions18
Objective expression-1.0*ATPM_reverse_5b752 - 1.0*PFK_reverse_d24a6 + 1.0*PFK + 1.0*ATPM
Compartmentscytosol, extracellular
" ], "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": true }, "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.\n", "\n", "Consider having the [lxml](http://lxml.de/) package installed as it can speed up parsing considerably." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Namemini_textbook
Memory address0x0112fa6b38
Number of metabolites23
Number of reactions18
Objective expression-1.0*ATPM_reverse_5b752 - 1.0*PFK_reverse_d24a6 + 1.0*PFK + 1.0*ATPM
Compartmentscytosol, extracellular
" ], "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": true }, "outputs": [], "source": [ "cobra.io.write_sbml_model(\n", " textbook_model, \"test_cobra.xml\", use_fbc_package=False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## JSON" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Cobrapy models have a [JSON](https://en.wikipedia.org/wiki/JSON) (JavaScript Object Notation) representation. This format was created for interoperability with [escher](https://escher.github.io)." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Namemini_textbook
Memory address0x0113061080
Number of metabolites23
Number of reactions18
Objective expression-1.0*ATPM_reverse_5b752 - 1.0*PFK_reverse_d24a6 + 1.0*PFK + 1.0*ATPM
Compartmentscytosol, extracellular
" ], "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": [ "## YAML" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Cobrapy models have a [YAML](https://en.wikipedia.org/wiki/YAML) (YAML Ain't Markup Language) representation. This format was created for more human readable model representations and automatic diffs between models." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Namemini_textbook
Memory address0x0113013390
Number of metabolites23
Number of reactions18
Objective expression-1.0*ATPM_reverse_5b752 - 1.0*PFK_reverse_d24a6 + 1.0*PFK + 1.0*ATPM
Compartmentsextracellular, cytosol
" ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cobra.io.load_yaml_model(join(data_dir, \"mini.yml\"))" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": true }, "outputs": [], "source": [ "cobra.io.save_yaml_model(textbook_model, \"test.yml\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## MATLAB" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Often, models may be imported and exported solely 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": 10, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Namemini_textbook
Memory address0x0113000b70
Number of metabolites23
Number of reactions18
Objective expression-1.0*ATPM_reverse_5b752 - 1.0*PFK_reverse_d24a6 + 1.0*PFK + 1.0*ATPM
Compartmentsc, e
" ], "text/plain": [ "" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cobra.io.load_matlab_model(\n", " join(data_dir, \"mini.mat\"), 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 parameter is unnecessary." ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Namemini_textbook
Memory address0x0113758438
Number of metabolites23
Number of reactions18
Objective expression-1.0*ATPM_reverse_5b752 - 1.0*PFK_reverse_d24a6 + 1.0*PFK + 1.0*ATPM
Compartmentsc, e
" ], "text/plain": [ "" ] }, "execution_count": 11, "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": 12, "metadata": { "collapsed": true }, "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.6.0" } }, "nbformat": 4, "nbformat_minor": 1 }