Skip to content

fbc-v3: xmlns="http://sbml.org/fbc/keyvaluepair" in <listOfKeyValuePairs> element is an invalid namespace. #256

@matthiaskoenig

Description

@matthiaskoenig

When writing a SBML file with fbc-v3 KeyValuePairs and reading it again one gets the following error

An SBML XML document must conform to the XML Schema for the corresponding SBML Level, Version and Release. The XML Schema for SBML defines the basic SBML object structure, the data types used by those objects, and the order in which the objects may appear in an SBML document.
 xmlns="http://sbml.org/fbc/keyvaluepair" in <listOfKeyValuePairs> element is an invalid namespace.

Below a full example

"""
Test bed for FBC version 3.
For latest SBML fbc v3 specification see
https://github.com/bgoli/sbml-fbc-spec/blob/main/sf_svn/spec/main.pdf
"""
from logging import getLogger
from pathlib import Path

import libsbml

# from sbmlutils.console import console
logger = getLogger(__name__)


def check(value: int, message: str) -> bool:
    """Check the libsbml return value and prints message if something happened.

    If 'value' is None, prints 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.
    """
    valid = True
    if value is None:
        logger.error(f"Error: LibSBML returned a null value trying to <{message}>.")
        valid = False
    elif isinstance(value, int):
        if value != libsbml.LIBSBML_OPERATION_SUCCESS:
            logger.error(f"Error encountered trying to '{message}'.")
            logger.error(
                f"LibSBML returned error code {str(value)}: "
                f"{libsbml.OperationReturnValue_toString(value).strip()}"
            )
            valid = False

    return valid


# create document with new fbc version 3 namespace
sbmlns: libsbml.SBMLNamespaces = libsbml.SBMLNamespaces(3, 1)
sbmlns.addPkgNamespace("fbc", 3)

doc: libsbml.SBMLDocument = libsbml.SBMLDocument(sbmlns)
doc_fbc: libsbml.FbcSBMLDocumentPlugin = doc.getPlugin("fbc")
doc_fbc.setRequired(False)

model: libsbml.Model = doc.createModel()
model_fbc: libsbml.FbcModelPlugin = model.getPlugin("fbc")
model_fbc.setStrict(True)

p1: libsbml.Parameter = model.createParameter()
p1.setId("lb")
p1.setValue(-100)
p1.setConstant(True)
# KeyValue Pair on parameter
p1_fbc: libsbml.FbcSpeciesPlugin = p1.getPlugin("fbc")
kvp_list: libsbml.ListOfKeyValuePairs = p1_fbc.getListOfKeyValuePairs()
# kvp_list.setXmlns("http://sbml.org/fbc/keyvaluepair")
kvp: libsbml.KeyValuePair = kvp_list.createKeyValuePair()
check(kvp.setKey("pdata"), "Set Key on KeyValuePair")
check(kvp.setValue("10.0"), "Set Value on KeyValuePair")


sbml_str: str = libsbml.writeSBMLToString(doc)
print("-" * 80)
# console.log(sbml_str)
print(sbml_str)
print("-" * 80)


# checking that SBML string can be read again
doc2 = libsbml.readSBMLFromString(sbml_str)
if doc2.getNumErrors() > 0:
    if doc2.getError(0).getErrorId() == libsbml.XMLFileUnreadable:
        err_message = "Unreadable SBML file"
    elif doc2.getError(0).getErrorId() == libsbml.XMLFileOperationError:
        err_message = "Problems reading SBML file: XMLFileOperationError"
    else:
        err_message = "SBMLDocumentErrors encountered while reading the SBML file."

    # log_sbml_errors_for_doc(doc)
    logger.error(f"`read_sbml` error `{err_message}`")
    for k in range(doc2.getNumErrors()):
        error: libsbml.SBMLError = doc2.getError(k)
        print(f"Error {k}")
        print(error.getMessage())

Results in

<?xml version="1.0" encoding="UTF-8"?>
<sbml xmlns="http://www.sbml.org/sbml/level3/version1/core" xmlns:fbc="http://www.sbml.org/sbml/level3/version1/fbc/version3" level="3" version="1" fbc:required="false">
  <model fbc:strict="true">
    <listOfParameters>
      <parameter id="lb" value="-100" constant="true">
        <annotation>
          <listOfKeyValuePairs xmlns="http://sbml.org/fbc/keyvaluepair">
            <keyValuePair key="pdata" value="10.0"/>
          </listOfKeyValuePairs>
        </annotation>
      </parameter>
    </listOfParameters>
  </model>
</sbml>

--------------------------------------------------------------------------------
`read_sbml` error `SBMLDocumentErrors encountered while reading the SBML file.`
Error 0
An SBML XML document must conform to the XML Schema for the corresponding SBML Level, Version and Release. The XML Schema for SBML defines the basic SBML object structure, the data types used by those objects, and the order in which the objects may appear in an SBML document.
 xmlns="http://sbml.org/fbc/keyvaluepair" in <listOfKeyValuePairs> element is an invalid namespace.

The written SBML looks as expected, but SBMLDocumentErrors exist during reading due to the invalid namespace.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions