Skip to content

ode2sbml.validators.sbml_validator

sbml_validator

SBML validator: wraps libsbml consistency checks.

SBMLValidationError

Bases: Exception

Raised when SBML consistency checks find errors or fatal issues.

Source code in ode2sbml/validators/sbml_validator.py
class SBMLValidationError(Exception):
    """Raised when SBML consistency checks find errors or fatal issues."""

validate_sbml_doc(doc, raise_on_error=True)

Run libsbml consistency checks on doc.

Parameters:

Name Type Description Default
doc SBMLDocument

The SBMLDocument to validate.

required
raise_on_error bool

If True (default), raise :class:SBMLValidationError when errors or fatal errors are found.

True

Returns:

Type Description
list of str

All diagnostic messages (warnings + errors).

Source code in ode2sbml/validators/sbml_validator.py
def validate_sbml_doc(doc: libsbml.SBMLDocument, raise_on_error: bool = True) -> list:
    """
    Run libsbml consistency checks on *doc*.

    Parameters
    ----------
    doc:
        The SBMLDocument to validate.
    raise_on_error:
        If True (default), raise :class:`SBMLValidationError` when errors
        or fatal errors are found.

    Returns
    -------
    list of str
        All diagnostic messages (warnings + errors).
    """
    doc.checkConsistency()
    doc.checkInternalConsistency()

    n_errors = doc.getNumErrors()
    messages: list = []
    has_error = False

    for i in range(n_errors):
        err = doc.getError(i)
        severity = err.getSeverityAsString()
        msg = f"[{severity}] (line {err.getLine()}) {err.getMessage()}"
        messages.append(msg)
        if severity in ("Error", "Fatal"):
            has_error = True
        elif severity == "Warning":
            warnings.warn(f"SBML validation warning: {msg}", stacklevel=2)

    if has_error and raise_on_error:
        full_msg = "\n".join(messages)
        raise SBMLValidationError(
            f"SBML validation failed with {n_errors} diagnostic(s):\n{full_msg}"
        )

    return messages

validate_sbml_file(filepath, raise_on_error=True)

Validate an SBML file on disk.

Source code in ode2sbml/validators/sbml_validator.py
def validate_sbml_file(filepath: str, raise_on_error: bool = True) -> list:
    """Validate an SBML file on disk."""
    reader = libsbml.SBMLReader()
    doc = reader.readSBMLFromFile(filepath)
    if doc is None:
        raise SBMLValidationError(f"Could not read SBML file: {filepath}")
    return validate_sbml_doc(doc, raise_on_error=raise_on_error)

validate_sbml_string(xml_string, raise_on_error=True)

Validate an SBML XML string.

Source code in ode2sbml/validators/sbml_validator.py
def validate_sbml_string(xml_string: str, raise_on_error: bool = True) -> list:
    """Validate an SBML XML string."""
    reader = libsbml.SBMLReader()
    doc = reader.readSBMLFromString(xml_string)
    if doc is None:
        raise SBMLValidationError("Could not parse SBML XML string.")
    return validate_sbml_doc(doc, raise_on_error=raise_on_error)