17.1.1.3.1.4. cobra.io.sbml

SBML import and export using python-libsbml.

  • The SBML importer supports all versions of SBML and the fbc package.

  • The SBML exporter writes SBML L3 models.

  • Annotation information is stored on the cobrapy objects

  • Information from the group package is read

Parsing of fbc models was implemented as efficient as possible, whereas (discouraged) fallback solutions are not optimized for efficiency.

Notes are only supported in a minimal way relevant for constraint-based models. I.e., structured information from notes in the form

<p>key: value</p>

is read into the Object.notes dictionary when reading SBML files. On writing the Object.notes dictionary is serialized to the SBML notes information.

Annotations are read in the Object.annotation fields.

Some SBML related issues are still open, please refer to the respective issue: - update annotation format and support qualifiers (depends on decision

17.1.1.3.1.4.1. Module Contents

17.1.1.3.1.4.1.1. Functions

_escape_non_alphanum(nonASCII)

converts a non alphanumeric character to a string representation of

_number_to_chr(numberStr)

converts an ascii number to a character

_clip(sid, prefix)

Clips a prefix from the beginning of a string if it exists.

_f_gene(sid, prefix=’G_’)

Clips gene prefix from id.

_f_gene_rev(sid, prefix=’G_’)

Adds gene prefix to id.

_f_specie(sid, prefix=’M_’)

Clips specie/metabolite prefix from id.

_f_specie_rev(sid, prefix=’M_’)

Adds specie/metabolite prefix to id.

_f_reaction(sid, prefix=’R_’)

Clips reaction prefix from id.

_f_reaction_rev(sid, prefix=’R_’)

Adds reaction prefix to id.

_f_group(sid, prefix=’G_’)

Clips group prefix from id.

_f_group_rev(sid, prefix=’G_’)

Adds group prefix to id.

read_sbml_model(filename, number=float, f_replace=F_REPLACE, **kwargs)

Reads SBML model from given filename.

_get_doc_from_filename(filename)

Get SBMLDocument from given filename.

_sbml_to_model(doc, number=float, f_replace=F_REPLACE, set_missing_bounds=False, **kwargs)

Creates cobra model from SBMLDocument.

write_sbml_model(cobra_model, filename, f_replace=F_REPLACE, **kwargs)

Writes cobra model to filename.

_model_to_sbml(cobra_model, f_replace=None, units=True)

Convert Cobra model to SBMLDocument.

_create_bound(model, reaction, bound_type, f_replace, units=None, flux_udef=None)

Creates bound in model for given reaction.

_create_parameter(model, pid, value, sbo=None, constant=True, units=None, flux_udef=None)

Create parameter in SBML model.

_check_required(sbase, value, attribute)

Get required attribute from SBase.

_check(value, message)

Checks the libsbml return value and logs error messages.

_parse_notes_dict(sbase)

Creates dictionary of COBRA notes.

_sbase_notes_dict(sbase, notes)

Set SBase notes based on dictionary.

_parse_annotations(sbase)

Parses cobra annotations from a given SBase object.

_parse_annotation_info(uri)

Parses provider and term from given identifiers annotation uri.

_sbase_annotations(sbase, annotation)

Set SBase annotations based on cobra annotations.

validate_sbml_model(filename, check_model=True, internal_consistency=True, check_units_consistency=False, check_modeling_practice=False, **kwargs)

Validate SBML model and returns the model along with a list of errors.

_error_string(error, k=None)

String representation of SBMLError.

exception cobra.io.sbml.CobraSBMLError[source]

Bases: Exception

SBML error class.

cobra.io.sbml.LOGGER[source]
cobra.io.sbml.config[source]
cobra.io.sbml.LOWER_BOUND_ID = cobra_default_lb[source]
cobra.io.sbml.UPPER_BOUND_ID = cobra_default_ub[source]
cobra.io.sbml.ZERO_BOUND_ID = cobra_0_bound[source]
cobra.io.sbml.BOUND_MINUS_INF = minus_inf[source]
cobra.io.sbml.BOUND_PLUS_INF = plus_inf[source]
cobra.io.sbml.SBO_FBA_FRAMEWORK = SBO:0000624[source]
cobra.io.sbml.SBO_DEFAULT_FLUX_BOUND = SBO:0000626[source]
cobra.io.sbml.SBO_FLUX_BOUND = SBO:0000625[source]
cobra.io.sbml.SBO_EXCHANGE_REACTION = SBO:0000627[source]
cobra.io.sbml.LONG_SHORT_DIRECTION[source]
cobra.io.sbml.SHORT_LONG_DIRECTION[source]
cobra.io.sbml.Unit[source]
cobra.io.sbml.UNITS_FLUX = ['mmol_per_gDW_per_hr', None][source]
cobra.io.sbml.SBML_DOT = __SBML_DOT__[source]
cobra.io.sbml.pattern_notes[source]
cobra.io.sbml.pattern_to_sbml[source]
cobra.io.sbml.pattern_from_sbml[source]
cobra.io.sbml._escape_non_alphanum(nonASCII)[source]

converts a non alphanumeric character to a string representation of its ascii number

cobra.io.sbml._number_to_chr(numberStr)[source]

converts an ascii number to a character

cobra.io.sbml._clip(sid, prefix)[source]

Clips a prefix from the beginning of a string if it exists.

cobra.io.sbml._f_gene(sid, prefix='G_')[source]

Clips gene prefix from id.

cobra.io.sbml._f_gene_rev(sid, prefix='G_')[source]

Adds gene prefix to id.

cobra.io.sbml._f_specie(sid, prefix='M_')[source]

Clips specie/metabolite prefix from id.

cobra.io.sbml._f_specie_rev(sid, prefix='M_')[source]

Adds specie/metabolite prefix to id.

cobra.io.sbml._f_reaction(sid, prefix='R_')[source]

Clips reaction prefix from id.

cobra.io.sbml._f_reaction_rev(sid, prefix='R_')[source]

Adds reaction prefix to id.

cobra.io.sbml._f_group(sid, prefix='G_')[source]

Clips group prefix from id.

cobra.io.sbml._f_group_rev(sid, prefix='G_')[source]

Adds group prefix to id.

cobra.io.sbml.F_GENE = F_GENE[source]
cobra.io.sbml.F_GENE_REV = F_GENE_REV[source]
cobra.io.sbml.F_SPECIE = F_SPECIE[source]
cobra.io.sbml.F_SPECIE_REV = F_SPECIE_REV[source]
cobra.io.sbml.F_REACTION = F_REACTION[source]
cobra.io.sbml.F_REACTION_REV = F_REACTION_REV[source]
cobra.io.sbml.F_GROUP = F_GROUP[source]
cobra.io.sbml.F_GROUP_REV = F_GROUP_REV[source]
cobra.io.sbml.F_REPLACE[source]
cobra.io.sbml.read_sbml_model(filename, number=float, f_replace=F_REPLACE, **kwargs)[source]

Reads SBML model from given filename.

If the given filename ends with the suffix ‘’.gz’’ (for example, ‘’myfile.xml.gz’),’ the file is assumed to be compressed in gzip format and will be automatically decompressed upon reading. Similarly, if the given filename ends with ‘’.zip’’ or ‘’.bz2’,’ the file is assumed to be compressed in zip or bzip2 format (respectively). Files whose names lack these suffixes will be read uncompressed. Note that if the file is in zip format but the archive contains more than one file, only the first file in the archive will be read and the rest ignored.

To read a gzip/zip file, libSBML needs to be configured and linked with the zlib library at compile time. It also needs to be linked with the bzip2 library to read files in bzip2 format. (Both of these are the default configurations for libSBML.)

This function supports SBML with FBC-v1 and FBC-v2. FBC-v1 models are converted to FBC-v2 models before reading.

The parser tries to fall back to information in notes dictionaries if information is not available in the FBC packages, e.g., CHARGE, FORMULA on species, or GENE_ASSOCIATION, SUBSYSTEM on reactions.

Parameters
  • filename (path to SBML file, or SBML string, or SBML file handle) – SBML which is read into cobra model

  • number (data type of stoichiometry: {float, int}) – In which data type should the stoichiometry be parsed.

  • f_replace (dict of replacement functions for id replacement) – Dictionary of replacement functions for gene, specie, and reaction. By default the following id changes are performed on import: clip G_ from genes, clip M_ from species, clip R_ from reactions If no replacements should be performed, set f_replace={}, None

Returns

Return type

cobra.core.Model

Notes

Provided file handles cannot be opened in binary mode, i.e., use
with open(path, “r” as f):

read_sbml_model(f)

File handles to compressed files are not supported yet.

cobra.io.sbml._get_doc_from_filename(filename)[source]

Get SBMLDocument from given filename.

Parameters

filename (path to SBML, or SBML string, or filehandle) –

Returns

Return type

libsbml.SBMLDocument

cobra.io.sbml._sbml_to_model(doc, number=float, f_replace=F_REPLACE, set_missing_bounds=False, **kwargs)[source]

Creates cobra model from SBMLDocument.

Parameters
  • doc (libsbml.SBMLDocument) –

  • number (data type of stoichiometry: {float, int}) – In which data type should the stoichiometry be parsed.

  • f_replace (dict of replacement functions for id replacement) –

  • set_missing_bounds (flag to set missing bounds) –

Returns

Return type

cobra.core.Model

cobra.io.sbml.write_sbml_model(cobra_model, filename, f_replace=F_REPLACE, **kwargs)[source]

Writes cobra model to filename.

The created model is SBML level 3 version 1 (L1V3) with fbc package v2 (fbc-v2).

If the given filename ends with the suffix “.gz” (for example, “myfile.xml.gz”), libSBML assumes the caller wants the file to be written compressed in gzip format. Similarly, if the given filename ends with “.zip” or “.bz2”, libSBML assumes the caller wants the file to be compressed in zip or bzip2 format (respectively). Files whose names lack these suffixes will be written uncompressed. Special considerations for the zip format: If the given filename ends with “.zip”, the file placed in the zip archive will have the suffix “.xml” or “.sbml”. For example, the file in the zip archive will be named “test.xml” if the given filename is “test.xml.zip” or “test.zip”. Similarly, the filename in the archive will be “test.sbml” if the given filename is “test.sbml.zip”.

Parameters
  • cobra_model (cobra.core.Model) – Model instance which is written to SBML

  • filename (string) – path to which the model is written

  • f_replace (dict of replacement functions for id replacement) –

cobra.io.sbml._model_to_sbml(cobra_model, f_replace=None, units=True)[source]

Convert Cobra model to SBMLDocument.

Parameters
  • cobra_model (cobra.core.Model) – Cobra model instance

  • f_replace (dict of replacement functions) – Replacement to apply on identifiers.

  • units (boolean) – Should the FLUX_UNITS be written in the SBMLDocument.

Returns

Return type

libsbml.SBMLDocument

cobra.io.sbml._create_bound(model, reaction, bound_type, f_replace, units=None, flux_udef=None)[source]

Creates bound in model for given reaction.

Adds the parameters for the bounds to the SBML model.

Parameters
  • model (libsbml.Model) – SBML model instance

  • reaction (cobra.core.Reaction) – Cobra reaction instance from which the bounds are read.

  • bound_type ({LOWER_BOUND, UPPER_BOUND}) – Type of bound

  • f_replace (dict of id replacement functions) –

  • units (flux units) –

Returns

Return type

Id of bound parameter.

cobra.io.sbml._create_parameter(model, pid, value, sbo=None, constant=True, units=None, flux_udef=None)[source]

Create parameter in SBML model.

cobra.io.sbml._check_required(sbase, value, attribute)[source]

Get required attribute from SBase.

Parameters
  • sbase (libsbml.SBase) –

  • value (existing value) –

  • attribute (name of attribute) –

Returns

Return type

attribute value (or value if already set)

cobra.io.sbml._check(value, message)[source]

Checks the libsbml return value and logs error messages.

If ‘value’ is None, logs an error message constructed using

‘message’ and then exits with status code 1. If ‘value’ is an integer, it assumes it is a libSBML return status code. If the code value is LIBSBML_OPERATION_SUCCESS, returns without further action; if it is not, prints an error message constructed using ‘message’ along with text from libSBML explaining the meaning of the code, and exits with status code 1.

cobra.io.sbml._parse_notes_dict(sbase)[source]

Creates dictionary of COBRA notes.

Parameters

sbase (libsbml.SBase) –

Returns

Return type

dict of notes

cobra.io.sbml._sbase_notes_dict(sbase, notes)[source]

Set SBase notes based on dictionary.

Parameters
  • sbase (libsbml.SBase) – SBML object to set notes on

  • notes (notes object) – notes information from cobra object

cobra.io.sbml.URL_IDENTIFIERS_PATTERN[source]
cobra.io.sbml.URL_IDENTIFIERS_PREFIX = https://identifiers.org[source]
cobra.io.sbml.QUALIFIER_TYPES[source]
cobra.io.sbml._parse_annotations(sbase)[source]

Parses cobra annotations from a given SBase object.

Annotations are dictionaries with the providers as keys.

Parameters

sbase (libsbml.SBase) – SBase from which the SBML annotations are read

Returns

cobra.io.sbml._parse_annotation_info(uri)[source]

Parses provider and term from given identifiers annotation uri.

Parameters

uri (str) – uri (identifiers.org url)

Returns

Return type

(provider, identifier) if resolvable, None otherwise

cobra.io.sbml._sbase_annotations(sbase, annotation)[source]

Set SBase annotations based on cobra annotations.

Parameters
cobra.io.sbml.validate_sbml_model(filename, check_model=True, internal_consistency=True, check_units_consistency=False, check_modeling_practice=False, **kwargs)[source]

Validate SBML model and returns the model along with a list of errors.

Parameters
  • filename (str) – The filename (or SBML string) of the SBML model to be validated.

  • internal_consistency (boolean {True, False}) – Check internal consistency.

  • check_units_consistency (boolean {True, False}) – Check consistency of units.

  • check_modeling_practice (boolean {True, False}) – Check modeling practise.

  • check_model (boolean {True, False}) – Whether to also check some basic model properties such as reaction boundaries and compartment formulas.

Returns

  • (model, errors)

  • model (Model object) – The cobra model if the file could be read successfully or None otherwise.

  • errors (dict) – Warnings and errors grouped by their respective types.

Raises

CobraSBMLError

cobra.io.sbml._error_string(error, k=None)[source]

String representation of SBMLError.

Parameters
  • error (libsbml.SBMLError) –

  • k (index of error) –

Returns

Return type

string representation of error