14.1.4. cobra.io package Submodules cobra.io.json module


Load a cobra model from a JSON document.

Parameters:document (str) – The JSON document representation of a cobra model.
Returns:The cobra model as represented in the JSON document.
Return type:cobra.Model

See also

Load directly from a file.

Load a cobra model from a file in JSON format.

Parameters:filename (str or file-like) – File path or descriptor that contains the JSON document describing the cobra model.
Returns:The cobra model as represented in the JSON document.
Return type:cobra.Model

See also

Load from a string.
cobra.io.json.save_json_model(model, filename, pretty=False, **kwargs)[source]

Write the cobra model to a file in JSON format.

kwargs are passed on to json.dump.

  • model (cobra.Model) – The cobra model to represent.
  • filename (str or file-like) – File path or descriptor that the JSON representation should be written to.
  • pretty (bool, optional) – Whether to format the JSON more compactly (default) or in a more verbose but easier to read fashion. Can be partially overwritten by the kwargs.

See also

Return a string representation.
Base function.
cobra.io.json.to_json(model, **kwargs)[source]

Return the model as a JSON document.

kwargs are passed on to json.dumps.

Parameters:model (cobra.Model) – The cobra model to represent.
Returns:String representation of the cobra model as a JSON document.
Return type:str

See also

Write directly to a file.
Base function. cobra.io.mat module


create a dict mapping model attributes to arrays

cobra.io.mat.from_mat_struct(mat_struct, model_id=None, inf=<Mock object>)[source]

create a model from the COBRA toolbox struct

The struct will be a dict read in by scipy.io.loadmat

cobra.io.mat.load_matlab_model(infile_path, variable_name=None, inf=<Mock object>)[source]

Load a cobra model stored as a .mat file

  • infile_path (str) – path to the file to to read
  • variable_name (str, optional) – The variable name of the model in the .mat file. If this is not specified, then the first MATLAB variable which looks like a COBRA model will be used
  • inf (value) – The value to use for infinite bounds. Some solvers do not handle infinite values so for using those, set this to a high numeric value.

The resulting cobra model

Return type:


cobra.io.mat.model_to_pymatbridge(model, variable_name='model', matlab=None)[source]

send the model to a MATLAB workspace through pymatbridge

This model can then be manipulated through the COBRA toolbox

  • variable_name (str) – The variable name to which the model will be assigned in the MATLAB workspace
  • matlab (None or pymatbridge.Matlab instance) – The MATLAB workspace to which the variable will be sent. If this is None, then this will be sent to the same environment used in IPython magics.
cobra.io.mat.save_matlab_model(model, file_name, varname=None)[source]

Save the cobra model as a .mat file.

This .mat file can be used directly in the MATLAB version of COBRA.

  • model (cobra.core.Model.Model object) – The model to save
  • file_name (str or file-like object) – The file to save to
  • varname (string) – The name of the variable within the workspace cobra.io.sbml module

cobra.io.sbml.add_sbml_species(sbml_model, cobra_metabolite, note_start_tag, note_end_tag, boundary_metabolite=False)[source]

A helper function for adding cobra metabolites to an sbml model.

  • sbml_model (sbml_model object) –
  • cobra_metabolite (a cobra.Metabolite object) –
  • note_start_tag (string) – the start tag for parsing cobra notes. this will eventually be supplanted when COBRA is worked into sbml.
  • note_end_tag (string) – the end tag for parsing cobra notes. this will eventually be supplanted when COBRA is worked into sbml.
  • boundary_metabolite (bool) – if metabolite boundary condition should be set or not


Return type:

the created metabolite identifier

cobra.io.sbml.create_cobra_model_from_sbml_file(sbml_filename, old_sbml=False, legacy_metabolite=False, print_time=False, use_hyphens=False)[source]

convert an SBML XML file into a cobra.Model object.

Supports SBML Level 2 Versions 1 and 4. The function will detect if the SBML fbc package is used in the file and run the converter if the fbc package is used.

  • sbml_filename (string) –
  • old_sbml (bool) – Set to True if the XML file has metabolite formula appended to metabolite names. This was a poorly designed artifact that persists in some models.
  • legacy_metabolite (bool) –
    If True then assume that the metabolite id has the compartment id
    appended after an underscore (e.g. _c for cytosol). This has not been implemented but will be soon.
  • print_time (bool) – deprecated
  • use_hyphens (bool) – If True, double underscores (__) in an SBML ID will be converted to hyphens


Return type:

The parsed cobra model

cobra.io.sbml.fix_legacy_id(id, use_hyphens=False, fix_compartments=False)[source]
cobra.io.sbml.get_libsbml_document(cobra_model, sbml_level=2, sbml_version=1, print_time=False, use_fbc_package=True)[source]

Return a libsbml document object for writing to a file. This function is used by write_cobra_model_to_sbml_file().

cobra.io.sbml.parse_legacy_id(the_id, the_compartment=None, the_type='metabolite', use_hyphens=False)[source]

Deals with a bunch of problems due to bigg.ucsd.edu not following SBML standards

  • the_id (String.) –
  • the_compartment (String) –
  • the_type (String) – Currently only ‘metabolite’ is supported
  • use_hyphens (Boolean) – If True, double underscores (__) in an SBML ID will be converted to hyphens


Return type:

the identifier

cobra.io.sbml.parse_legacy_sbml_notes(note_string, note_delimiter=':')[source]

Deal with various legacy SBML format issues.

cobra.io.sbml.read_legacy_sbml(filename, use_hyphens=False)[source]

read in an sbml file and fix the sbml id’s

cobra.io.sbml.write_cobra_model_to_sbml_file(cobra_model, sbml_filename, sbml_level=2, sbml_version=1, print_time=False, use_fbc_package=True)[source]

Write a cobra.Model object to an SBML XML file.



TODO: Update the NOTES to match the SBML standard and provide support for Level 2 Version 4 cobra.io.sbml3 module

exception cobra.io.sbml3.CobraSBMLError[source]

Bases: Exception

cobra.io.sbml3.annotate_cobra_from_sbml(cobra_element, sbml_element)[source]
cobra.io.sbml3.annotate_sbml_from_cobra(sbml_element, cobra_element)[source]
cobra.io.sbml3.clip(string, prefix)[source]

clips a prefix from the beginning of a string if it exists

>>> clip("R_pgi", "R_")
cobra.io.sbml3.construct_gpr_xml(parent, expression)[source]

create gpr xml under parent node

cobra.io.sbml3.extract_rdf_annotation(sbml_element, metaid)[source]
cobra.io.sbml3.get_attrib(tag, attribute, type=<function <lambda>>, require=False)[source]
cobra.io.sbml3.indent_xml(elem, level=0)[source]

indent xml for pretty printing

cobra.io.sbml3.model_to_xml(cobra_model, units=True)[source]

replace prefixes with namespace


parses filename or compressed stream to xml

cobra.io.sbml3.parse_xml_into_model(xml, number=<class 'float'>)[source]
cobra.io.sbml3.read_sbml_model(filename, number=<class 'float'>, **kwargs)[source]
cobra.io.sbml3.set_attrib(xml, attribute_name, value)[source]

Utility function to convert a number to a string

cobra.io.sbml3.validate_sbml_model(filename, check_model=True)[source]

Returns the model along with a list of errors.

  • filename (str) – The filename of the SBML model to be validated.
  • check_model (bool, optional) – Whether to also check some basic model properties such as reaction boundaries and compartment formulas.

  • model (Model object) – The cobra model if the file could be read succesfully or None otherwise.
  • errors (dict) – Warnings and errors grouped by their respective types.


CobraSBMLError – If the file is not a valid SBML Level 3 file with FBC.

cobra.io.sbml3.write_sbml_model(cobra_model, filename, use_fbc_package=True, **kwargs)[source] Module contents