flatten¶
Helpers for model flattening.
flatten_sbml
¶
Flatten given SBML file.
A relative path is relative to the working directory, which is never
changed. libsbml resolves the comp:source of an external model definition
against the location of the document which names it, i.e. relative to the
directory of sbml_path, and of the external files in turn.
libsbml opens the file of an external model definition itself, and on
Windows it does so with the narrow (ANSI) file API, which cannot open a
path with a non-ASCII character: a model with external model definitions
in such a directory cannot be flattened on Windows. sbml_path and
sbml_flat_path themselves may be any path, a path libsbml cannot open
is read and written by python.
:param sbml_path: input path to SBML file to flatten (should be a comp model)
:param sbml_flat_path: output path for flat SBML
:param leave_ports: boolean flag to leave ports in flattened model.
:param validate: validate the comp model before flattening it, see
flatten_sbml_doc
:return: flattened SBMLDocument
:raises ValueError: if the file cannot be read, has no model, or libsbml
cannot flatten the document, see flatten_sbml_doc
flatten_sbml_doc
¶
Flatten SBMLDocument.
The document is flattened in place. The comp model is not validated by
default: the validation is the full consistency check of the document and
most of the time of the flattening, and a model is usually validated when
it is written (create_model). Validate an unvalidated comp model first or
pass validate=True: without the validation an invalid comp model can be
flattened into a wrong flat model which is itself valid, e.g. a replacement
of a species by a parameter drops the species, a duplicate id in a model
definition keeps one of the two elements, and a conversion factor with an
unknown id is dropped.
A document with errors from reading it is not flattened. A submodel, an external model definition or a replaced element which cannot be resolved fails the flattening either way; without the validation libsbml finds such a reference during the conversion, so the document is then left partly flattened.
If an output path is provided the file is written to the output path.
:param doc: SBMLDocument to flatten. :param sbml_flat_path: Path to write flattended SBMLDocument to :param leave_ports: flag to leave ports :param validate: validate the comp model before flattening it, so that an invalid model is not flattened and the document is left as it is
:return: SBMLDocument
:raises ValueError: if the document has no model, e.g. a library of comp model definitions, which has nothing to flatten them into