4. Reading and Writing Models

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.

The package also ships with test models in various formats for testing purposes.

[1]:
from pathlib import Path
from cobra.io import load_json_model, save_json_model, load_matlab_model, save_matlab_model, read_sbml_model, write_sbml_model
import logging

data_dir = Path(".") / ".." / "src" / "cobra" / "data"
data_dir = data_dir.resolve()

print("mini test files: ")
print(", ".join(str(i) for i in data_dir.glob('mini*.*')))

textbook_model = load_model("textbook")
ecoli_model = load_model("iJO1366")
logging.getLogger("cobra.io.sbml").setLevel(logging.ERROR)salmonella_model = load_model("salmonella")
mini test files:
/Users/uridavidakavia/PycharmProjects/cobrapy/src/cobra/data/mini_fbc2.xml, /Users/uridavidakavia/PycharmProjects/cobrapy/src/cobra/data/mini.json, /Users/uridavidakavia/PycharmProjects/cobrapy/src/cobra/data/mini_fbc1.xml, /Users/uridavidakavia/PycharmProjects/cobrapy/src/cobra/data/mini_fbc2.xml.gz, /Users/uridavidakavia/PycharmProjects/cobrapy/src/cobra/data/mini.yml, /Users/uridavidakavia/PycharmProjects/cobrapy/src/cobra/data/mini_cobra.xml, /Users/uridavidakavia/PycharmProjects/cobrapy/src/cobra/data/mini.mat, /Users/uridavidakavia/PycharmProjects/cobrapy/src/cobra/data/mini_fbc2.xml.bz2, /Users/uridavidakavia/PycharmProjects/cobrapy/src/cobra/data/mini.pickle

4.1. SBML

The Systems Biology Markup Language is an XML-based standard format for distributing models which has support for COBRA models through the FBC extension version 2.

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.

[2]:
mini_fbc2_path = data_dir / "mini_fbc2.xml"
read_sbml_model(str(mini_fbc2_path.resolve()))
[2]:
Name mini_textbook
Memory address 0x07fbe29d69cf8
Number of metabolites 23
Number of reactions 18
Number of groups 0
Objective expression 1.0*ATPM - 1.0*ATPM_reverse_5b752 + 1.0*PFK - 1.0*PFK_reverse_d24a6
Compartments cytosol, extracellular
[3]:
write_sbml_model(textbook_model, "test_fbc2.xml")

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.

Cobrapy can use 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.

Consider having the lxml package installed as it can speed up parsing considerably.

[4]:
mini_cobra_path = data_dir / "mini_cobra.xml"
read_sbml_model(str(mini_cobra_path.resolve()))
[4]:
Name mini_textbook
Memory address 0x07fbe29d693c8
Number of metabolites 23
Number of reactions 18
Number of groups 0
Objective expression 1.0*ATPM - 1.0*ATPM_reverse_5b752 + 1.0*PFK - 1.0*PFK_reverse_d24a6
Compartments cytosol, extracellular
[5]:
write_sbml_model(textbook_model, "test_cobra.xml")

4.2. JSON

Cobrapy models have a JSON (JavaScript Object Notation) representation. This format was created for interoperability with escher.

[6]:
mini_json_path = data_dir / "mini.json"
load_json_model(str(mini_json_path.resolve()))
[6]:
Name mini_textbook
Memory address 0x07fbe29c6ac50
Number of metabolites 23
Number of reactions 18
Number of groups 0
Objective expression 1.0*ATPM - 1.0*ATPM_reverse_5b752 + 1.0*PFK - 1.0*PFK_reverse_d24a6
Compartments cytosol, extracellular
[7]:
save_json_model(textbook_model, "test.json")

4.3. YAML

Cobrapy models have a YAML (YAML Ain’t Markup Language) representation. This format was created for more human readable model representations and automatic diffs between models.

[8]:
mini_yml_path = data_dir / "mini.yml"
load_yaml_model(str(mini_yml_path.resolve()))
[8]:
Name mini_textbook
Memory address 0x07fbe29c6ad30
Number of metabolites 23
Number of reactions 18
Number of groups 0
Objective expression 1.0*ATPM - 1.0*ATPM_reverse_5b752 + 1.0*PFK - 1.0*PFK_reverse_d24a6
Compartments cytosol, extracellular
[9]:
save_yaml_model(textbook_model, "test.yml")

4.4. MATLAB

Often, models may be imported and exported solely for the purposes of working with the same models in cobrapy and the MATLAB cobra toolbox. MATLAB has its own “.mat” format for storing variables. Reading and writing to these mat files from python requires scipy.

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:

[10]:
mini_mat_path = data_dir / "mini.mat"
load_matlab_model(
    str(mini_mat_path.resolve()),
    variable_name="mini_textbook",
)
[10]:
Name mini_textbook
Memory address 0x07fbe29c5dd30
Number of metabolites 23
Number of reactions 18
Number of groups 0
Objective expression 1.0*ATPM - 1.0*ATPM_reverse_5b752 + 1.0*PFK - 1.0*PFK_reverse_d24a6
Compartments c, e

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.

[11]:
mini_mat_path = data_dir / "mini.mat"
load_matlab_model(str(mini_mat_path.resolve()))
[11]:
Name mini_textbook
Memory address 0x07fbe299af940
Number of metabolites 23
Number of reactions 18
Number of groups 0
Objective expression 1.0*ATPM - 1.0*ATPM_reverse_5b752 + 1.0*PFK - 1.0*PFK_reverse_d24a6
Compartments c, e

Saving models to mat files is also relatively straightforward

[12]:
save_matlab_model(textbook_model, "test.mat")

4.5. Pickle

Cobra models can be serialized using the python serialization format, pickle.

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.