>>> from libsbml import *
>>> reader = SBMLReader()
>>> document = reader.readSBML("examples/sample-models/from-spec/level-2/enzymekinetics.xml")
>>> document.getNumErrors()
0
>>>
The code above illustrates probably the simplest possible use of libsbml: reading a model and printing any errors encountered. The code begins with a Python import command to load the libSBML API into the running Python interpreter. Next, it instantiates an libsbml.SBMLReader object and stores it in a variable called reader. Then, it uses this object to read an SBML model stored in a file, creating an libsbml.SBMLDocument object in the process and storing it in the variable document. Finally, it calls on the libsbml.SBMLDocument.getNumErrors() method to check if any errors were encountered.
libsbml.SBMLDocument libsbml.readSBML(string filename). This function takes a file name, attempts to read an SBML document from the file, and returns a libsbml.SBMLDocument object if successful.libsbml.SBMLDocument libsbml.readSBMLFromString(string xml). This function takes a string assumed to contain XML content, attempts to read an SBML document from the string, and returns a libsbml.SBMLDocument object if successful.libsbml.SBMLDocument is derived from libsbml.SBase, so that it contains the usual libsbml.SBase attributes (in SBML Level 2 Version 3) of "metaid" and "sboTerm", as well as the subelements "notes" and "annotation". It also contains the attributes "level" and "version" indicating the Level and Version of the SBML read. libsbml.SBase (and thus its subclasses such as libsbml.SBMLDocument) provides methods for querying this information:
int libsbml.SBMLDocument.getLevel() returns the SBML Level of the model.int libsbml.SBMLDocument.getVersion() returns the SBML Version within the Level of the model.
libsbml.SBMLDocument.getModel() returns a libsbml.Model object for the SBML model contained in the libsbml.SBMLDocument.
>>> from libsbml import *
>>> reader = SBMLReader()
>>> document = reader.readSBML("examples/sample-models/from-spec/level-2/enzymekinetics.xml")
>>> model = document.getModel()
>>> model.getNumSpecies()
4
>>>
libsbml.SBMLDocument also acts to log any problems encountered while reading the model from the file or data stream. Whether the problems are warnings or errors, they are reported through a single common interface involving the object class SBMLError. The example earlier on this page already showed some of the methods available for accessing errors and warnings; here is a slightly more complete list:
int SBMLDocument.getNumErrors() returns a count of the diagnostic messages logged during while attempting to read an SBML model using either libsbml.readSBML(filename) or libsbml.readFromString(string).libsbml.SBMLError libsbml.SBMLDocument.getError(int n) returns the error indexed by integer n in the error log. The libsbml.SBMLError object class provides methods for displaying an error message, assessing the severity of the problem encountered, and for finding out the line and column number of where the problem occurred in the SBML input.libsbml.SBMLDocument.printErrors() prints to standard output all of the errors and diagnostics logged with the given libsbml.SBMLDocument().libsbml.SBMLDocument.printErrors(ostream stream) is identical to the method above, but prints all of the diagnostics to the given output stream instead of the terminal.
int libsbml.SBMLDocument.checkConsistency() performs a set of structural and mathematical checks on the SBML content and reports the number of failed checks (errors) encountered. Use the libsbml.SBMLDocument.getNumErrors() and libsbml.SBMLDocument.getError(int n) interfaces to examine the individual errors.int libsbml.SBMLDocument.checkL1Compatibility() peforms a set of semantic consistency checks on the document to establish whether it can be converted to SBML Level 1, and returns the number of failures. If all the checks succeed, it returns 0.int libsbml.SBMLDocument.checkL2v1Compatibility() peforms a set of semantic consistency checks on the document to establish whether it can be converted to SBML Level 2 Version 1, and returns the number of failures. If all the checks succeed, it returns 0.int libsbml.SBMLDocument.checkL2v2Compatibility() peforms a set of semantic consistency checks on the document to establish whether it can be converted to SBML Level 2 Version 2, and returns the number of failures. If all the checks succeed, it returns 0.int libsbml.SBMLDocument.checkL2v3Compatibility() peforms a set of semantic consistency checks on the document to establish whether it can be converted to SBML Level 2 Version 3, and returns the number of failures. If all the checks succeed, it returns 0.int libsbml.SBMLDocument.checkL2v4Compatibility() peforms a set of semantic consistency checks on the document to establish whether it can be converted to SBML Level 2 Version 4, and returns the number of failures. If all the checks succeed, it returns 0.
int libsbml.writeSBML(SBMLDocument d, string filename) writes the given SBML document to a file, and returns either 1 on success or 0 on failure. Reasons for failure can be, for example, that the named file could not be opened for writing.int libsbml.writeSBML(SBMLDocument d, ostream stream) writes the given SBML document to an ostream output stream, and returns either 1 on success or 0 on failure.string libsbml.writeSBMLToString(SBMLDocument d) returns the given SBML document as a character string, or returns an empty string if a failure occurred.