3. Reading and Writing Models
In this notebook example, the import and export capabilities of MASSpy are demonstrated.
MASSpy supports reading and writing models in the SBML and JSON formats. The preferred format for general use is the SBML with the FBC (Version 2) extension and the Groups (Version 1) extension.
The JSON format may be more useful for MASSpy specific functionality and for visualizing networks via Escher. See the Network Visualization section for additional details.
The MASSpy package also comes with models in various formats for testing purposes.
[1]:
from os.path import join
import mass
import mass.example_data
# To view the list of available models, remove the semicolon
mass.example_data.view_example_models();
3.1. SBML
[2]:
from mass.io import sbml
The Systems Biology Markup Language is an XML-based standard format for distributing models.
MASSpy supports the reading and writing of SBML Level 3. MASSpy attempts to convert SBML Level 1 and Level 2 models to Level 3 before loading.
[3]:
model = sbml.read_sbml_model(join(mass.example_data.MODELS_DIR, "textbook.xml"))
model
Set parameter Username
[3]:
Name | RBC_PFK |
Memory address | 0x07fd789a94e80 |
Stoichiometric Matrix | 68x76 |
Matrix Rank | 63 |
Number of metabolites | 68 |
Initial conditions defined | 68/68 |
Number of reactions | 76 |
Number of genes | 0 |
Number of enzyme modules | 1 |
Number of groups | 16 |
Objective expression | 0 |
Compartments | Cytosol |
[4]:
sbml.write_sbml_model(model, "test_textbook.xml")
MASSpy utilizes the libSBML package to read and write SBML files, supporting both the [FBC](https://synonym.caltech.edu/software/libsbml/5.18.0/docs/formatted/python-api/group__fbc.html (Version 2) and the Groups (Version 1) extensions. When reading in a model, MASSpy automatically detects whether the FBC and/or Groups extensions were used.
To preserve information specific to EnzymeModule
objects, the SBML Groups extension is used along with the notes section for SBML objects. The use_groups_package
argument can be utilized to indicate whether to write cobra.Group
objects to the SBML file, including EnzymeModule
information. Disabling this extension may result in a loss of some enzyme specific information (e.g., categorized groups), but it does not prevent species and reactions of the enzyme module from being
written.
When writing a model, the use_fbc_package
argument can be used to indicate whether to write additional model information (e.g., metabolite formula and charge, genes, reaction bounds) via the FBC extension.
3.2. JSON
[5]:
from mass.io import json
MASSpy models have a JSON representation, allowing for interoperability with the Escher.
See the Network Visualization section for additional details on working with Escher.
[6]:
model = json.load_json_model(join(mass.example_data.MODELS_DIR, "textbook.json"))
model
[6]:
Name | RBC_PFK |
Memory address | 0x07fd77891a880 |
Stoichiometric Matrix | 68x76 |
Matrix Rank | 63 |
Number of metabolites | 68 |
Initial conditions defined | 68/68 |
Number of reactions | 76 |
Number of genes | 0 |
Number of enzyme modules | 1 |
Number of groups | 16 |
Objective expression | 0 |
Compartments | Cytosol |
[7]:
json.save_json_model(model, "test_textbook.json")
Consider having the simplejson package to speed up reading/writing of JSON models.
3.2.1. JSON schema
The JSON schema for MASSpy models is stored in mass.io.json as the JSON_SCHEMA
variable. It can be accessed via the following:
[8]:
# To view the JSON schema, remove the semicolon
json.JSON_SCHEMA;
3.3. Converting between file formats
Often there are times where one program or package is used to execute a specific task, yet a downstream task requires the use of a different program or package. Consequently, the ability to import a model written using one format and subsequently export it to another is essential in these scenarios. The submodules of mass.io
can be used to facilitate the import/export of models in different formats for this purpose.
One possible scenario in which the conversion between file formats is necessary involves the visualizion of an SBML model on a network map using the Escher [KDragerE+15] visualization tool.
See Visualizing SBML models with Escher in Python for an example demonstrating how MASSpy facilitates file conversion for model exchangability.