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()
## Set the volume of compartments
## Continue with other compartments
c2 = model.createCompartment()
## When you have finished with compartments, proceed with species.
s1 = model.createSpecies()
s2 = model.createSpecies()
s3 = model.createSpecies()
## When you are done with compartments and species, create reactions.
r1 = model.createReaction()
r1.setName('Foxes eat rabbits')
## Create reactants and products
reac1 = r1.createReactant()
prod1 = r1.createProduct()
## 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.
## If you wish you can add optional information.
model.setName('My test model')
##Save to file.
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.