LibSBML is an application programming interface (API) library for reading, writing and manipulating files and data streams containing SBML content. Developers can embed the library in their applications, saving themselves the work of implementing their own parsing, manipulation and validation software. At the API level, the library provides the same interface to data structures independently of whether the model originated in SBML Level 1 or 2. The library currently also offers the ability to translate SBML Level 1 models to SBML Level 2 and (to some extent) from Level 2 to Level 1 as well.

LibSBML is written in ISO standard C++ and C and is highly portable. It is currently supported on the Linux, Solaris, MacOS X, and Microsoft Windows operating systems. The library provides language bindings for C, C++, C#, Java, Python, Perl, MATLAB, Octave, and Ruby, with support for other languages planned for the future. The package is distributed in both source-code form and as precompiled dynamic libraries for the Microsoft Windows, Linux and Apple MacOS X operating systems; they are available from the `sbml` project on SourceForge.net.

## Advantages of a dedicated library for SBML

Why not simply use a generic XML parsing library? After all, SBML is usually expressed in XML, and there exist plenty of XML parsers, so why not simply tell people to use one of them, rather than develop a specialized library? The answer is: while it is true that developers can use general-purpose XML libraries, there are many reasons why using a system such as libSBML is a vastly better choice.

One of the features of libSBML is its facilities for manipulating mathematical formulas supporting differences in representation between SBML Level 1 and SBML Level 2. LibSBML provides an API that allows working with formulas in both text-string and MathML form, and to interconvert mathematical expressions between these forms. The utility of this facility extends beyond converting between SBML Level 1 and 2. Many software packages provide users with the ability to express formulas for such things as reaction rate expressions, and these packages' interfaces often let users type in the formulas directly as text strings. LibSBML saves application programmers the work of developing formula manipulation and translation functionality. It makes it possible to translate those formula strings directly into Abstract Syntax Trees (ASTs), manipulate them using AST operations, and write them out in the MathML format of SBML Level 2.

Another feature of libSBML is the validation it performs on SBML inputs at the time of parsing files and data streams. This helps verify the correctness of models in a way that goes beyond simple syntactic validation. Still another invaluable feature of libSBML is the domain-specific operations it provides beyond simple SBML-specific accessor facilities. Examples of such operations include obtaining a count of the number of boundary condition species, determining the modifier species of a reaction (assuming the reaction provides kinetics), and constructing the stoichiometric matrix for all reactions in a model.

Some of the other features of libSBML include the following:

- Full SBML Support. All constructs in SBML Level 1 (Versions 1 and 2) and SBML Level 2 are supported. For compatibility with some technically incorrect but popular Level 1 applications and models, the parser recognizes and stores notes and annotations defined for the top-level `<sbml>` element (logging a warning).
- Unified SBML Level 2 and Level 1 object models. All objects have `getSBMLDocument()`, `getModel()`, `getLevel()`, and `getVersion()` methods, among other things. Also, the interface to SBML's Rules abstracts away some of the individual rule type (assignment, rate, algebraic) differences.
- Full XML and SBML Validation. All XML and Schema warning, error and fatal error messages are logged with line and column number information and may be retrieved and manipulated programmatically.
- Dimensional analysis and unit checking. LibSBML implements a thorough system for dimensional analysis and checking units of quantities in a model. The validation rules for units that are specified in SBML Level 2 Version 2 and Version 3 are fully implemented, including checking units in mathematical formulas.
- Access to SBML annotations and notes as XML objects. Annotations and notes are read and manipulated as XML structures instead of text strings. Further, in order to facilitate the support of MIRIAM compatible annotations, there are new object classes ModelHistory and CVTerm. These classes facilitate the creation and addition of RDF annotations inside `<annotation>` elements by providing parsing and manipulation functions that treat the annotations in terms of XMLNode objects implemented by the new XML layer. Both ModelHistory and CVTerm follow the general libSBML format of providing getters and setters for each variable stored within the class.
- Support for compressed SBML files. If an SBML file name ends in `.gz`, `.zip` or `.bz2`, libSBML will automatically uncompress the file upon reading it. Similarly, if the file to be written has one of those extensions, libSBML will write it out in compressed form. (The compression library incorporated by libSBML is MiniZip 1.01e, written by Gilles Vollant and made freely available for all uses including commercial applications.)
- Parser abstraction layer. LibSBML relies on third-party XML parser libraries, but thanks to its implementation of an abstraction layer, libSBML can use any of three different popular XML parser libraries: Expat, Apache Xerces-C++, and Libxml2. LibSBML provides identical functionality and checking of XML syntax is now available no matter which one is used. SBML Documents are parsed and manipulated in the Unicode codepage for efficiency; however, strings are transcoded to the local code page for SBML structures.
- Small memory footprint and fast runtime. The parser is event-based (SAX2) and loads SBML data into C++ structures that mirror the SBML specification. As an example of libSBML's performance, a sample file generated by Gepasi known as the 100 Yeast file (2 MB in size, 2000 reactions; see http://www.gepasi.org/gep3sbml.html) loads in 1.18 s on a 1 GHz AMD Athlon XP and uses only 1.4 MB of memory.
- Interfaces for C, C++, C#, Java, Python, Perl, MATLAB, Octave, and Ruby. The C and C++ interfaces are implemented natively; the Java, Perl, Python, and Ruby interfaces are implemented using SWIG, the Simplified Wrapper Interface Generator; and the rest are implemented through custom hand-written interfaces.
- Well tested: version 3.0.0 has over 1280 unit tests and 5800 individual assertions. The entire library was written using the test-first approach popularized by Kent Beck and eXtreme Programming, where it's one of the 12 principles.
- Written in portable, pure ISO C and C++. The build system uses GNU tools (Autoconf, GNU Make) to build shared and static libraries.
- Complete user manual. Documentation is available in the "docs" subdirectory in both pre-formatted and source form.

## Manipulating mathematical formulas

In SBML Level 1, mathematical formulas are represented as text strings using a C-like syntax. This representation was chosen because of its simplicity, widespread familiarity and use in applications such as Gepasi and Jarnac, whose authors contributed to the initial design of SBML. For SBML Level 2, there was a need to expand the mathematical vocabulary of Level 1 to include additional functions (both built-in and user-defined), mathematical constants, logical operators, relational operators and a special symbol to represent time. Rather than growing the simple C-like syntax into something more complicated and esoteric in order to support these features, and consequently having to manage two standards in two different formats (XML and text string formulas), SBML Level 2 leverages an existing standard for expressing mathematical formulas, namely the content portion of MathML.

LibSBML provides an abstraction for working with mathematical expressions in both text-string and MathML form: Abstract Syntax Trees (ASTs). Abstract Syntax Trees are well-known in the computer science community; they are simple recursive data structures useful for representing the syntactic structure of sentences in certain kinds of languages (mathematical or otherwise). Much as libSBML allows programmers to manipulate SBML at the level of domain-specific objects, regardless of SBML level or version, it also allows programmers to work with mathematical formula at the level of ASTs regardless of whether the original format was C-like infix or MathML. LibSBML goes one step further by allowing programmers to work exclusively with infix formula strings and instantly convert them to the appropriate MathML whenever needed.