libsbmljs

A client-side WebAssembly / JavaScript interface to libSBML for use in Node.js and the Web.

View the demo.

Installation

Choose one of the following packages:

  • No SBML extensions (i.e. cannot read flux-balance models) but smallest size
    npm install --save libsbmljs_core@beta
    
  • Everything from the previous package plus accepted SBML extensions (CAN read flux-balance models)
    npm install --save libsbmljs_stable@beta
    
  • Everything from the previous package plus all SBML extensions (including proposed extensions)
    npm install --save libsbmljs_experimental@beta
    

These packages get larger but have more features as you go down the list. Your choice will depend on which SBML extensions you need, and how much bandwidth you want to conserve. If you are unsure, go with libsbmljs_stable. If you would like more info, keep reading.




Why Three NPM Packages?

libsbmljs comes in libsbmljs_core, libsbmljs_stable, and libsbmljs_experimental npm packages. The difference between them is the number of SBML extensions supported in each version. If you don’t know what extensions you need, go with the “stable” package. It can read all models in the BioModels and BiGG Models databases.

core stable experimental
(no extensions) layout layout
  render render
  fbc fbc
  multi multi
  qual qual
  comp comp
  groups groups
    distrib
    dyn
    arrays
    spatial

The libSBML C++ library maintains two separate branches: the “stable” branch for accepted extensions and the “experimental” branch for proposed extensions. The extensions in the experimental branch are not finalized yet - they are subject to change, and any code that relies on them will have to be changed as well. If you don’t require any SBML extensions at all, you can go with the “core” build, which has the added benefit of giving you the smallest npm package.

Package size (uncompressed):

core 3.0M
stable 6.4M
experimental 9.5M

Tutorial

This tutorial will teach you the basics of using libsbmljs in an empty project. Make sure you have Node.js 10.15.0 or later before starting. We’re going to use Node.js for this tutorial because setting up libsbmljs in the browser usually involves Webpack, which makes things a little more complicated. We have a separate example showing how to use libsbmljs with Webpack.

  • First, create and change to a new directory and run the following. The answers you provide to npm's questions don't matter for this tutorial. You can leave all of the fields blank

    npm init
    
  • You will need to install one of libsbmljs's npm packages. We will use the `libsbmljs_stable` package for this tutorial.

    npm install --save libsbmljs_stable@beta
    
  • Next you will need to install Babel:

    npm install --save-dev @babel/core @babel/cli @babel/node @babel/preset-env
    
  • Create the file “index.js” in the current directory. This file will hold all the JavaScript code for this tutorial. Open the “index.js” file.

  • Unlike most JavaScript libraries, libsbmljs is loaded asynchronously. This means that it cannot be used immediately after importing it because the browser or Node.js may still be downloading and compiling it. The libsbmljs module can be called using a "then()" method similar to a Promise. Any libsbmljs functions you want to use should go inside the "then()" callback. Add the following code to your `index.js` file:

    import libsbml from 'libsbmljs_stable'
    // the module isn't usable yet - wait for it to load
    libsbml().then((libsbml) => {
      // now it is safe to use the module
      const doc = new libsbml.SBMLDocument(3,2)
    })
    
  • To populate the SBML content to the document you just created in the previous step, let's add one SBML Comaprtment, one Species and one Reaction. You can paste this code into your `then()` callback or copy the full source code at the end of this tutorial into "index.js".

...
  // create a model and compartment
  const model = doc.createModel()
  const compartment = model.createCompartment()
  compartment.setId('C1')
  compartment.setSize(1)
  compartment.setConstant(true)

  // create a species
  const species = model.createSpecies()
  species.setId('S1')
  species.setConstant(false)
  species.setHasOnlySubstanceUnits(false)
  species.setBoundaryCondition(false)

  // create a reaction
  const reaction = model.createReaction()
  reaction.setId('J1')
  reaction.setReversible(false)

  const reactant = reaction.createReactant() // returns a SpeciesReference
  reactant.setSpecies('S1')
  reactant.setConstant(false)

  // create a parser for infix formulae
  // this is a libsbmljs helper class
  // it doesn't exist in the libsbml C++ library
  const parser = new libsbml.SBMLFormulaParser()
  reaction.createKineticLaw().setMath(parser.parseL3Formula('10*S1'))
...
  • Finally, we will write out the SBML document we create to XML:

    ...
      // print out the serialized XML for the SBML model we created
      const writer = new libsbml.SBMLWriter()
      console.log(writer.writeSBMLToString(doc))
    ...
    
  • You can run this example from the directory containing "index.js" as follows:

    npx babel-node --presets @babel/env index.js
    

You should see the following output printed to the terminal:

<?xml version="1.0" encoding="UTF-8"?>
<sbml xmlns="http://www.sbml.org/sbml/level3/version2/core" level="3" version="2">
  <model>
    <listOfCompartments>
      <compartment id="C1" size="1" constant="true"/>
    </listOfCompartments>
    <listOfSpecies>
      <species id="S1" hasOnlySubstanceUnits="false" boundaryCondition="false" constant="false"/>
    </listOfSpecies>
    <listOfReactions>
      <reaction id="J1" reversible="false">
        <listOfReactants>
          <speciesReference species="S1"/>
        </listOfReactants>
        <kineticLaw>
          <math xmlns="http://www.w3.org/1998/Math/MathML">
            <apply>
              <times/>
              <cn type="integer"> 10 </cn>
              <ci> S1 </ci>
            </apply>
          </math>
        </kineticLaw>
      </reaction>
    </listOfReactions>
  </model>
</sbml>
  • Here is the full source code of the “index.js” file we created in this tutorial:
import libsbml from 'libsbmljs_stable'
// the module isn't usable yet - wait for it to load
libsbml().then((libsbml) => {
  // now it is safe to use the module
  const doc = new libsbml.SBMLDocument(3,2)

  // create a model and compartment
  const model = doc.createModel()
  const compartment = model.createCompartment()
  compartment.setId('C1')
  compartment.setSize(1)
  compartment.setConstant(true)

  // create a species
  const species = model.createSpecies()
  species.setId('S1')
  species.setConstant(false)
  species.setHasOnlySubstanceUnits(false)
  species.setBoundaryCondition(false)

  // create a reaction
  const reaction = model.createReaction()
  reaction.setId('J1')
  reaction.setReversible(false)

  const reactant = reaction.createReactant() // returns a SpeciesReference
  reactant.setSpecies('S1')
  reactant.setConstant(false)

  // create a parser for infix formulae
  // this is a helper class of libsbmljs
  // it doesn't exist in the libsbml C++ library
  const parser = new libsbml.SBMLFormulaParser()
  reaction.createKineticLaw().setMath(parser.parseL3Formula('10*S1'))

  // print out the serialized XML for the SBML model we created
  const writer = new libsbml.SBMLWriter()
  console.log(writer.writeSBMLToString(doc))
})


API Documentation:

Examples

We provide a number of Node.js examples showing how to use libsbmljs and the various packages contained in the stable and experimental builds. We recommend you try the Node.js examples first, since they are easy to get started with and allow for a lot of experimentation.

If you are designing web apps, you are probably using Webpack. We also provide an example showing how to use libsbmljs with Webpack to bundle the JavaScript assets for a web app.

Demo

We provide a simple demo which allows the user to search the BioModels and BiGG Models databases and read individual models using libsbmljs.

After selecting a model, you have the option of running SBML consistency checks (a.k.a. validation) on the model. You may be familiar with the SBML Online Validator, but libsbmljs does not use the Online Validator. The demo runs entirely in the browser using JavaScript and WebAssembly. It does not rely on any external web services.

View the demo.

Source Code

The source code of the libsbmljs wrapper per se is actually a set of build scripts and WebIDL interface files which can be used to generate a WebAssembly / JavaScript wrapper from a given checkout of the libSBML C++ source code. The libsbmljs wrapper uses Emscripten to build WebAssembly / JavaScript bindings.

The build scripts generating the libsbmljs wrapper along with instructions for using them are available at GitHub.

If you’re looking for the source code of libSBML itself, please visit the libSBML site.