psamm.datasource.sbml – SBML model parser

Parser for SBML model files.

exception psamm.datasource.sbml.ParseError

Error parsing SBML file

class psamm.datasource.sbml.SBMLSpeciesEntry(reader, root, filemark=None)

Species entry in the SBML file

name

Species name

compartment

Species compartment

charge

Species charge

formula

Species formula

boundary

Whether this compound is a boundary condition

properties

All species properties as a dict

filemark

Position of entry in the source file (or None).

class psamm.datasource.sbml.SBMLReactionEntry(reader, root, filemark=None)

Reaction entry in SBML file

id

Reaction ID

name

Reaction name

reversible

Whether the reaction is reversible

equation

Reaction equation is a Reaction object

kinetic_law_reaction_parameters

Iterator over the values of kinetic law reaction parameters

properties

All reaction properties as a dict

filemark

Position of entry in the source file (or None).

class psamm.datasource.sbml.SBMLCompartmentEntry(reader, root, filemark=None)

Compartment entry in the SBML file

properties

All compartment properties as a dict.

filemark

Position of entry in the source file (or None).

class psamm.datasource.sbml.SBMLObjectiveEntry(reader, namespace, root)

Flux objective defined with FBC

class psamm.datasource.sbml.SBMLFluxBoundEntry(reader, namespace, root)

Flux bound defined with FBC V1.

Flux bounds defined with FBC V2 are instead encoded as upper_flux and lower_flux properties on the ReactionEntry objects.

id

Return ID of flux bound.

name

Return name of flux bound.

reaction

Return reaction ID that the flux bound pertains to.

operation

Return the operation of the flux bound.

Returns one of LESS_EQUAL, GREATER_EQUAL or EQUAL.

value

Return the flux bound value.

class psamm.datasource.sbml.SBMLReader(file, strict=False, ignore_boundary=True, context=None)

Reader of SBML model files

The constructor takes a file-like object which will be parsed as XML and then as SBML according to the specification. If the strict parameter is set to False, the parser will revert to a more lenient parsing which is required for many older models. This tries to mimic the inconsistencies employed by COBRA when parsing models.

If ignore_boundary is True, the species that are marked as boundary conditions will simply be dropped from the species list and from the reaction equations, and any boundary compartment will be dropped too. Otherwise the boundary species will be retained. Retaining these is only useful to extract specific information from those species objects.

Parameters:
  • file – File-like object to parse XML SBML content from.
  • strict – Indicating whether strict parsing is enabled.
  • ignore_boundary – Indicating whether boundary species are dropped.
  • context – Optional file parsing context from psamm.datasource.context.
get_compartment(compartment_id)

Return CompartmentEntry corresponding to id.

get_reaction(reaction_id)

Return SBMLReactionEntry corresponding to reaction_id

get_species(species_id)

Return SBMLSpeciesEntry corresponding to species_id

get_objective(objective_id)

Return SBMLObjectiveEntry corresponding to objective_id

compartments

Iterator over SBMLCompartmentEntry entries.

reactions

Iterator over ReactionEntries

species

Iterator over SpeciesEntries

This will not yield boundary condition species if those are ignored.

objectives

Iterator over SBMLObjectiveEntry

flux_bounds

Iterator over SBMLFluxBoundEntry

id

Model ID

name

Model name

create_model()

Create model from reader.

Returns:psamm.datasource.native.NativeModel.
class psamm.datasource.sbml.SBMLWriter(cobra_flux_bounds=False)

Writer of SBML files.

write_model(file, model, pretty=False)

Write a given model to file.

Parameters:
  • file – File-like object open for writing.
  • model – Instance of NativeModel to write.
  • pretty – Whether to format the XML output for readability.
psamm.datasource.sbml.convert_sbml_model(model)

Convert raw SBML model to extended model.

Parameters:modelNativeModel obtained from SBMLReader.
psamm.datasource.sbml.entry_id_from_cobra_encoding(cobra_id)

Convert COBRA-encoded ID string to decoded ID string.

psamm.datasource.sbml.create_convert_sbml_id_function(compartment_prefix='C_', reaction_prefix='R_', compound_prefix='M_', decode_id=<function entry_id_from_cobra_encoding>)

Create function for converting SBML IDs.

The returned function will strip prefixes, decode the ID using the provided function. These prefixes are common on IDs in SBML models because the IDs live in a global namespace.

psamm.datasource.sbml.translate_sbml_compartment(entry, new_id)

Translate SBML compartment entry.

psamm.datasource.sbml.translate_sbml_reaction(entry, new_id, compartment_map, compound_map)

Translate SBML reaction entry.

psamm.datasource.sbml.translate_sbml_compound(entry, new_id, compartment_map)

Translate SBML compound entry.

psamm.datasource.sbml.convert_model_entries(model, convert_id=<function create_convert_sbml_id_function.<locals>.convert_sbml_id>, create_unique_id=None, translate_compartment=<function translate_sbml_compartment>, translate_reaction=<function translate_sbml_reaction>, translate_compound=<function translate_sbml_compound>)

Convert and decode model entries.

Model entries are converted to new entries using the translate functions and IDs are converted using the given coversion function. If ID conversion would create a clash of IDs, the create_unique_id function is called with a container of current IDs and the base ID to generate a unique ID from. The translation functions take an existing entry and the new ID.

All references within the model are updated to use new IDs: compartment boundaries, limits, exchange, model, biomass reaction, etc.

Parameters:modelNativeModel.
psamm.datasource.sbml.parse_xhtml_notes(entry)

Yield key, value pairs parsed from the XHTML notes section.

Each key, value pair must be defined in its own text block, e.g. <p>key: value</p><p>key2: value2</p>. The key and value must be separated by a colon. Whitespace is stripped from both key and value, and quotes are removed from values if present. The key is normalized by conversion to lower case and spaces replaced with underscores.

Parameters:entry_SBMLEntry.
psamm.datasource.sbml.parse_xhtml_species_notes(entry)

Return species properties defined in the XHTML notes.

Older SBML models often define additional properties in the XHTML notes section because structured methods for defining properties had not been developed. This will try to parse the following properties: PUBCHEM ID, CHEBI ID, FORMULA, KEGG ID, CHARGE.

Parameters:entrySBMLSpeciesEntry.
psamm.datasource.sbml.parse_xhtml_reaction_notes(entry)

Return reaction properties defined in the XHTML notes.

Older SBML models often define additional properties in the XHTML notes section because structured methods for defining properties had not been developed. This will try to parse the following properties: SUBSYSTEM, GENE ASSOCIATION, EC NUMBER, AUTHORS, CONFIDENCE.

Parameters:entrySBMLReactionEntry.
psamm.datasource.sbml.parse_objective_coefficient(entry)

Return objective value for reaction entry.

Detect objectives that are specified using the non-standardized kinetic law parameters which are used by many pre-FBC SBML models. The objective coefficient is returned for the given reaction, or None if undefined.

Parameters:entrySBMLReactionEntry.
psamm.datasource.sbml.parse_flux_bounds(entry)

Return flux bounds for reaction entry.

Detect flux bounds that are specified using the non-standardized kinetic law parameters which are used by many pre-FBC SBML models. The flux bounds are returned as a pair of lower, upper bounds. The returned bound is None if undefined.

Parameters:entrySBMLReactionEntry.
psamm.datasource.sbml.detect_extracellular_compartment(model)

Detect the identifier for equations with extracellular compartments.

Parameters:modelNativeModel.
psamm.datasource.sbml.convert_exchange_to_compounds(model)

Convert exchange reactions in model to exchange compounds.

Only exchange reactions in the extracellular compartment are converted. The extracelluar compartment must be defined for the model.

Parameters:modelNativeModel.
psamm.datasource.sbml.merge_equivalent_compounds(model)

Merge equivalent compounds in various compartments.

Tries to detect and merge compound entries that represent the same compound in different compartments. The entries are only merged if all properties are equivalent. Compound entries must have an ID with a suffix of an underscore followed by the compartment ID. This suffix will be stripped and compounds with identical IDs are merged if the properties are identical.

Parameters:modelNativeModel.