17.1.1.3.2.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
for new annotation format; https://github.com/opencobra/cobrapy/issues/684)
- write compartment annotations and notes (depends on updated first-class
compartments; see https://github.com/opencobra/cobrapy/issues/760)
- support compression on file handles (depends on solution for
17.1.1.3.2.4.1. Module Contents¶
17.1.1.3.2.4.1.1. Functions¶
|
converts a non alphanumeric character to a string representation of |
|
converts an ascii number to a character |
|
Clips a prefix from the beginning of a string if it exists. |
|
Clips gene prefix from id. |
|
Adds gene prefix to id. |
|
Clips specie/metabolite prefix from id. |
|
Adds specie/metabolite prefix to id. |
|
Clips reaction prefix from id. |
|
Adds reaction prefix to id. |
|
Clips group prefix from id. |
|
Adds group prefix to id. |
|
Reads SBML model from given filename. |
|
Get SBMLDocument from given filename. |
|
Creates cobra model from SBMLDocument. |
|
Writes cobra model to filename. |
|
Convert Cobra model to SBMLDocument. |
|
Creates bound in model for given reaction. |
|
Create parameter in SBML model. |
|
Get required attribute from SBase. |
|
Checks the libsbml return value and logs error messages. |
|
Creates dictionary of COBRA notes. |
|
Set SBase notes based on dictionary. |
|
Parses cobra annotations from a given SBase object. |
Parses provider and term from given identifiers annotation uri. |
|
|
Set SBase annotations based on cobra annotations. |
|
Validate SBML model and returns the model along with a list of errors. |
|
String representation of SBMLError. |
-
cobra.io.sbml.
_escape_non_alphanum
(nonASCII)[source]¶ converts a non alphanumeric character to a string representation of its ascii number
-
cobra.io.sbml.
_clip
(sid, prefix)[source]¶ Clips a prefix from the beginning of a string if it exists.
-
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
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.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.
_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
dict (annotation dictionary)
FIXME (annotation format must be updated (this is a big collection of) – fixes) - see: https://github.com/opencobra/cobrapy/issues/684)
-
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
sbase (libsbml.SBase) – SBML object to annotate
annotation (cobra annotation structure) – cobra object with annotation information
FIXME (annotation format must be updated) – (https://github.com/opencobra/cobrapy/issues/684)
-
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