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 has no title attribute.
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
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.
That's it! Copy&paste this code and try it yourself! Please note that this tutorial is really basic. For more comprehensive examples, please see the examples/ folder in the libSBML source tree.