An example of creating a simple SBML Level 3 model.
46 def check(value, message):
47 """If 'value' is None, prints an error message constructed using 48 'message' and then exits with status code 1. If 'value' is an integer, 49 it assumes it is a libSBML return status code. If the code value is 50 LIBSBML_OPERATION_SUCCESS, returns without further action; if it is not, 51 prints an error message constructed using 'message' along with text from 52 libSBML explaining the meaning of the code, and exits with status code 1. 55 raise SystemExit(
'LibSBML returned a null value trying to ' + message +
'.')
56 elif type(value)
is int:
57 if value == LIBSBML_OPERATION_SUCCESS:
60 err_msg =
'Error encountered trying to ' + message +
'.' \
61 +
'LibSBML returned error code ' + str(value) +
': "' \
63 raise SystemExit(err_msg)
69 """Returns a simple but complete SBML Level 3 model for illustration.""" 77 document = SBMLDocument(3, 1)
79 raise SystemExit(
'Could not create SBMLDocument object')
88 model = document.createModel()
89 check(model,
'create model')
90 check(model.setTimeUnits(
"second"),
'set model-wide time units')
91 check(model.setExtentUnits(
"mole"),
'set model units of extent')
92 check(model.setSubstanceUnits(
'mole'),
'set model substance units')
98 per_second = model.createUnitDefinition()
99 check(per_second,
'create unit definition')
100 check(per_second.setId(
'per_second'),
'set unit definition id')
101 unit = per_second.createUnit()
102 check(unit,
'create unit on per_second')
103 check(unit.setKind(UNIT_KIND_SECOND),
'set unit kind')
104 check(unit.setExponent(-1),
'set unit exponent')
105 check(unit.setScale(0),
'set unit scale')
106 check(unit.setMultiplier(1),
'set unit multiplier')
111 c1 = model.createCompartment()
112 check(c1,
'create compartment')
113 check(c1.setId(
'c1'),
'set compartment id')
114 check(c1.setConstant(
True),
'set compartment "constant"')
115 check(c1.setSize(1),
'set compartment "size"')
116 check(c1.setSpatialDimensions(3),
'set compartment dimensions')
117 check(c1.setUnits(
'litre'),
'set compartment size units')
125 s1 = model.createSpecies()
126 check(s1,
'create species s1')
127 check(s1.setId(
's1'),
'set species s1 id')
128 check(s1.setCompartment(
'c1'),
'set species s1 compartment')
129 check(s1.setConstant(
False),
'set "constant" attribute on s1')
130 check(s1.setInitialAmount(5),
'set initial amount for s1')
131 check(s1.setSubstanceUnits(
'mole'),
'set substance units for s1')
132 check(s1.setBoundaryCondition(
False),
'set "boundaryCondition" on s1')
133 check(s1.setHasOnlySubstanceUnits(
False),
'set "hasOnlySubstanceUnits" on s1')
135 s2 = model.createSpecies()
136 check(s2,
'create species s2')
137 check(s2.setId(
's2'),
'set species s2 id')
138 check(s2.setCompartment(
'c1'),
'set species s2 compartment')
139 check(s2.setConstant(
False),
'set "constant" attribute on s2')
140 check(s2.setInitialAmount(0),
'set initial amount for s2')
141 check(s2.setSubstanceUnits(
'mole'),
'set substance units for s2')
142 check(s2.setBoundaryCondition(
False),
'set "boundaryCondition" on s2')
143 check(s2.setHasOnlySubstanceUnits(
False),
'set "hasOnlySubstanceUnits" on s2')
149 k = model.createParameter()
150 check(k,
'create parameter k')
151 check(k.setId(
'k'),
'set parameter k id')
152 check(k.setConstant(
True),
'set parameter k "constant"')
153 check(k.setValue(1),
'set parameter k value')
154 check(k.setUnits(
'per_second'),
'set parameter k units')
162 r1 = model.createReaction()
163 check(r1,
'create reaction')
164 check(r1.setId(
'r1'),
'set reaction id')
165 check(r1.setReversible(
False),
'set reaction reversibility flag')
166 check(r1.setFast(
False),
'set reaction "fast" attribute')
168 species_ref1 = r1.createReactant()
169 check(species_ref1,
'create reactant')
170 check(species_ref1.setSpecies(
's1'),
'assign reactant species')
171 check(species_ref1.setConstant(
True),
'set "constant" on species ref 1')
173 species_ref2 = r1.createProduct()
174 check(species_ref2,
'create product')
175 check(species_ref2.setSpecies(
's2'),
'assign product species')
176 check(species_ref2.setConstant(
True),
'set "constant" on species ref 2')
179 check(math_ast,
'create AST for rate expression')
181 kinetic_law = r1.createKineticLaw()
182 check(kinetic_law,
'create kinetic law')
183 check(kinetic_law.setMath(math_ast),
'set math on kinetic law')
191 if __name__ ==
'__main__':
192 print(create_model())