libSBML 5.8.0 in Fedora 19 with Python bindings

How to install libSBML 5.8.0 in Fedora 19 with Python bindings

Install compiler
yum install gcc-c++
yum groupinstall "Development Tools"

Install some additional packages if needed
yum install python-devel
yum install libxml2-devel
yum install swig

Compile and install libSBML
Go to the directory where you have exctracted libsbml source and run these 3 commands
./configure --with-python
make
make install

Make libsbml accessible to Python
In Fedora 18 you would export full path to libsbml:
export PYTHONPATH=/usr/local/lib64/python2.7/site-packages/libsbmlIn Fedora 18 you would import _libsbml not just import libsbml.

In Fedora 19 you have to export path like this:
export PYTHONPATH=/usr/local/lib64/python2.7/site-packages/

The export command expires when user leaves his/her session.

Use the libSBML in your code
import libsbml

How to install libSBML in Fedora 18 with Python bindings

Install compiler
yum install gcc-c++
yum groupinstall "Development Tools"

Install some additional packages if needed
yum install python-devel
yum install libxml2-devel
yum install swig

Compile and install libSBML
Go to the directory where you have exctracted libsbml source and run these 3 commands
./configure --with-python
make
make install

Make libsbml accessible to Python
export PYTHONPATH=/usr/local/lib64/python2.7/site-packages/libsbml
Or fix the path right from your python script
import sys
sys.path.append('/usr/local/lib64/python2.7/site-packages/libsbml')

You can get and set environment variables via os.environ:
import os
os.environ['PYTHONPATH'] = '/usr/local/lib/python2.7/dist-packages/libsbml'

But since your interpreter already runs, this will have no effect.

For whatever reason, in Fedora the library file is _libsbml, not libsml. To use libsbml in Fedora import it like this:
import _libsbml

LibSBML does not support chemical formulas for species

Yes, this is true. You can not store metabolites chemical formulas directly in SBML. I have seen various approaches, like, adding chemical formula to the name of the metabolite or storing formula in the htmlNotes tags.
I have been struggling to find an appropriate way to read&write chemical formulas in SBML files until my recent correspondance with authors of libSBML.

Quoting Sarah Keating:

The most robust way to currently store this information is to use a miriam compliant annotation to point to a database/resource that specifies the formula. Section 6 of the SBML Level 3 Version 1 Core specification details of this type of annotation and libSBML provides an API that allows users to interact with the annotation via a class CVTerms (see the addCVTerms example).
Directly adding chemicalFormula to the Species element is being facilitated by the SBML L3 Flux Balance Constraints package (http://sbml.org/Documents/Specifications/SBML_Level_3/Packages/Flux_Balance_Constraints_%28flux%29). LibSBML does provide “experimental” support for this package.

How to create an SBML file using libSBML in Python

This tutorial is intended as a quick introduction on how to create an SBML file using libSBML. For a full developer’s guide, please see libSBML Python API.

from libsbml import *
document = SBMLDocument()
model = document.createModel(2, 4) # SBML level and version

##First you have to create compartments
c1 = model.createCompartment()
c1.setName('Forest')
c1.setId('comp_FOREST')

## Set the volume of compartments
c1.setVolume(1000)

## Continue with other compartments
c2 = model.createCompartment()
c2.setName('Meadow')
c2.setId('comp_Meadow')
c2.setVolume(1000)

## When you have finished with compartments, proceed with species.
s1 = model.createSpecies()
s1.setName('Foxes')
s1.setId('spec_FOXES')
s1.setCompartment('comp_FOREST')
s1.setInitialAmount(10)

s2 = model.createSpecies()
s2.setName('Rabbits')
s2.setId('spec_RABBITS')
s2.setCompartment('comp_FOREST')
s2.setInitialAmount(50)

s3 = model.createSpecies()
s3.setName('Snakes')
s3.setId('spec_Snakes')
s3.setCompartment('comp_Meadow')
s3.setInitialAmount(100)

## When you are done with compartments and species, create reactions.
r1 = model.createReaction()
r1.setName('Foxes eat rabbits')
r1.setId('R1')

## Create reactants and products
reac1 = r1.createReactant()
reac1.setSpecies('spec_RABBITS')
prod1 = r1.createProduct()
prod1.setSpecies('spec_FOXES')

## if necessary, set Stoichiometry
reac1.setStoichiometry(2) # 2 rabbits
prod1.setStoichiometry(1) # 1 fox

Notice that we create seemingly independent objects for comprartments, reaction, reactant and product. We set some values to the properties of these objects and never actually add these elements to the model. Thats because they are automatically added. You can try this inside a loop and it will still work.

##Now when you have done whith your model you have to add it to the document.
document.setModel(model)

## If you wish you can add optional information.
model.setName('My test model')
model.setId('MyModelID')

##Save to file.
writeSBMLToFile(document,'name_of_your_model.xml')

That’s it! Copy&paste this code and try it yourself!
Please note that this tutorial is realy basic. For more comprehensive examples, please see the examples/ folder in the libSBML source tree.