Reading and writing SBML content from your software

This section summarizes how to read and write SBML content using the facilities provided by the libSBML Python API.

Getting started: the 1-minute introduction

In LibSBML, the class libsbml.SBMLDocument is used as a top-level container for storing SBML content and data associated with it (such as warnings and error messages). Here is a simple example to start this discussion, using Python in interactive mode:
>>> 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.

Reading SBML

As shown in the example above, SBML may be read from a file or an in-memory character string into a libsbml.SBMLDocument object. LibSBML defines two basic, global functions for reading SBML:

The model may be either in SBML Level 1 or SBML Level 2 format. LibSBML implements an unified object model for SBML that encompasses both Level 1 and Level 2, so applications generally do not need to worry about differences in syntax between these definitions of SBML when reading and writing models. (However, applications still need to be concerned about the constructs used and how they are interpreted, since there are substantial differences between SBML Level 1 and Level 2!)

The libsbml::SBMLDocument container

As might be deduced from the examples so far, an libsbml.SBMLDocument object in libSBML represents a whole SBML model and its associated data. The libsbml.SBMLDocument class corresponds roughly to the class Sbml defined in the SBML Level 2 specification, but it does not have a direct correspondence in SBML Level 1. (But, it is created by libSBML no matter whether the model is Level 1 or Level 2.)

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:

Of course, the whole point of reading an SBML file or data stream is to get at the SBML model it contains. The following method allows access to the Model object within an SBML document:

Here is an example of using this:
>>> 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:

Finally, another set of libsbml.SBMLDocument methods worth mentioning in the context of reading SBML are those for running consistency-checking and validation rules on the SBML content. These methods assess whether the SBML is legal according to basic rules listed in the SBML Level 2 Version 2 through 4 specification documents. Note that they are mostly structural checks, in the sense that they can indicate whether the SBML is properly constructed; they cannot tell if a model is nonsense. (But at least they can assess whether it's syntactically correct nonsense!).

This release of libSBML supports Levels/Versions of SBML up through Level 2 Version 4.

Writing SBML

Writing SBML is, in the end, a very simple matter in libSBML. The library provides the following two global functions for this purposes:




HTML documentation generated on Thu Jan 21 16:56:12 2010 using Doxygen 1.5.8.