libSBML Python API  5.18.0
fbc_example1.py

Simple example of writing a model that uses the SBML Level 3 Flux Balance Constraints package.

1 #!/usr/bin/env python
2 #
3 # @file fbc_example1.py
4 # @brief SBML FBC example
5 # @author Frank Bergmann
6 #
7 # <!--------------------------------------------------------------------------
8 # This sample program is distributed under a different license than the rest
9 # of libSBML. This program uses the open-source MIT license, as follows:
10 #
11 # Copyright (c) 2013-2018 by the California Institute of Technology
12 # (California, USA), the European Bioinformatics Institute (EMBL-EBI, UK)
13 # and the University of Heidelberg (Germany), with support from the National
14 # Institutes of Health (USA) under grant R01GM070923. All rights reserved.
15 #
16 # Permission is hereby granted, free of charge, to any person obtaining a
17 # copy of this software and associated documentation files (the "Software"),
18 # to deal in the Software without restriction, including without limitation
19 # the rights to use, copy, modify, merge, publish, distribute, sublicense,
20 # and/or sell copies of the Software, and to permit persons to whom the
21 # Software is furnished to do so, subject to the following conditions:
22 #
23 # The above copyright notice and this permission notice shall be included in
24 # all copies or substantial portions of the Software.
25 #
26 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
29 # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
31 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
32 # DEALINGS IN THE SOFTWARE.
33 #
34 # Neither the name of the California Institute of Technology (Caltech), nor
35 # of the European Bioinformatics Institute (EMBL-EBI), nor of the University
36 # of Heidelberg, nor the names of any contributors, may be used to endorse
37 # or promote products derived from this software without specific prior
38 # written permission.
39 # ------------------------------------------------------------------------ -->
40 
41 from libsbml import *
42 
43 sbmlns = SBMLNamespaces(3,1,"fbc",1)
44 
45 # create the document
46 
47 document = SBMLDocument(sbmlns)
48 
49 # mark the fbc package as not required
50 
51 document.setPackageRequired("fbc", False)
52 
53 # create the Model
54 
55 model= document.createModel()
56 
57 # create the Compartment
58 
59 compartment = model.createCompartment()
60 compartment.setId("compartment")
61 compartment.setConstant(True)
62 compartment.setSize(1)
63 
64 # create the Species
65 
66 species = model.createSpecies()
67 species.setId("Node1")
68 species.setCompartment("compartment")
69 species.setBoundaryCondition(False)
70 species.setConstant(False)
71 species.setHasOnlySubstanceUnits(False)
72 
73 # just as an example for setting charge and chemicalFormula
74 
75 splugin = species.getPlugin("fbc")
76 if splugin is not None:
77  splugin.setCharge(-1)
78  # chemical formula should follow the hill system
79  splugin.setChemicalFormula("C2H5Br")
80 
81 
82 species = model.createSpecies()
83 species.setId("Node2")
84 species.setCompartment("compartment")
85 species.setBoundaryCondition(False)
86 species.setConstant(False)
87 species.setHasOnlySubstanceUnits(False)
88 
89 species = model.createSpecies()
90 species.setId("Node3")
91 species.setCompartment("compartment")
92 species.setBoundaryCondition(False)
93 species.setConstant(False)
94 species.setHasOnlySubstanceUnits(False)
95 
96 species = model.createSpecies()
97 species.setId("Node4")
98 species.setCompartment("compartment")
99 species.setBoundaryCondition(False)
100 species.setConstant(False)
101 species.setHasOnlySubstanceUnits(False)
102 
103 species = model.createSpecies()
104 species.setId("Node5")
105 species.setCompartment("compartment")
106 species.setBoundaryCondition(False)
107 species.setConstant(False)
108 species.setHasOnlySubstanceUnits(False)
109 
110 species = model.createSpecies()
111 species.setId("Node6")
112 species.setCompartment("compartment")
113 species.setBoundaryCondition(False)
114 species.setConstant(False)
115 species.setHasOnlySubstanceUnits(False)
116 
117 species = model.createSpecies()
118 species.setId("Node7")
119 species.setCompartment("compartment")
120 species.setBoundaryCondition(False)
121 species.setConstant(False)
122 species.setHasOnlySubstanceUnits(False)
123 
124 species = model.createSpecies()
125 species.setId("Node8")
126 species.setCompartment("compartment")
127 species.setBoundaryCondition(False)
128 species.setConstant(False)
129 species.setHasOnlySubstanceUnits(False)
130 
131 species = model.createSpecies()
132 species.setId("Node0")
133 species.setCompartment("compartment")
134 species.setBoundaryCondition(True)
135 species.setConstant(False)
136 species.setHasOnlySubstanceUnits(False)
137 
138 species = model.createSpecies()
139 species.setId("Node9")
140 species.setCompartment("compartment")
141 species.setBoundaryCondition(True)
142 species.setConstant(False)
143 species.setHasOnlySubstanceUnits(False)
144 
145 # create reactions
146 
147 reaction = model.createReaction()
148 reaction.setId("J0")
149 reaction.setReversible(False)
150 reaction.setFast(False)
151 reactant = reaction.createReactant()
152 reactant.setSpecies("Node0")
153 reactant.setStoichiometry(1)
154 reactant.setConstant(True)
155 product = reaction.createProduct()
156 product.setSpecies("Node1")
157 product.setStoichiometry(1)
158 product.setConstant(True)
159 
160 reaction = model.createReaction()
161 reaction.setId("J1")
162 reaction.setReversible(False)
163 reaction.setFast(False)
164 reactant = reaction.createReactant()
165 reactant.setSpecies("Node1")
166 reactant.setStoichiometry(1)
167 reactant.setConstant(True)
168 product = reaction.createProduct()
169 product.setSpecies("Node2")
170 product.setStoichiometry(1)
171 product.setConstant(True)
172 
173 reaction = model.createReaction()
174 reaction.setId("J2")
175 reaction.setReversible(False)
176 reaction.setFast(False)
177 reactant = reaction.createReactant()
178 reactant.setSpecies("Node2")
179 reactant.setStoichiometry(1)
180 reactant.setConstant(True)
181 product = reaction.createProduct()
182 product.setSpecies("Node3")
183 product.setStoichiometry(1)
184 product.setConstant(True)
185 
186 reaction = model.createReaction()
187 reaction.setId("J3")
188 reaction.setReversible(False)
189 reaction.setFast(False)
190 reactant = reaction.createReactant()
191 reactant.setSpecies("Node1")
192 reactant.setStoichiometry(1)
193 reactant.setConstant(True)
194 product = reaction.createProduct()
195 product.setSpecies("Node4")
196 product.setStoichiometry(1)
197 product.setConstant(True)
198 
199 reaction = model.createReaction()
200 reaction.setId("J4")
201 reaction.setReversible(False)
202 reaction.setFast(False)
203 reactant = reaction.createReactant()
204 reactant.setSpecies("Node4")
205 reactant.setStoichiometry(1)
206 reactant.setConstant(True)
207 product = reaction.createProduct()
208 product.setSpecies("Node3")
209 product.setStoichiometry(1)
210 product.setConstant(True)
211 
212 reaction = model.createReaction()
213 reaction.setId("J5")
214 reaction.setReversible(False)
215 reaction.setFast(False)
216 reactant = reaction.createReactant()
217 reactant.setSpecies("Node3")
218 reactant.setStoichiometry(1)
219 reactant.setConstant(True)
220 product = reaction.createProduct()
221 product.setSpecies("Node5")
222 product.setStoichiometry(1)
223 product.setConstant(True)
224 
225 reaction = model.createReaction()
226 reaction.setId("J6")
227 reaction.setReversible(False)
228 reaction.setFast(False)
229 reactant = reaction.createReactant()
230 reactant.setSpecies("Node5")
231 reactant.setStoichiometry(1)
232 reactant.setConstant(True)
233 product = reaction.createProduct()
234 product.setSpecies("Node6")
235 product.setStoichiometry(1)
236 product.setConstant(True)
237 
238 reaction = model.createReaction()
239 reaction.setId("J7")
240 reaction.setReversible(False)
241 reaction.setFast(False)
242 reactant = reaction.createReactant()
243 reactant.setSpecies("Node6")
244 reactant.setStoichiometry(1)
245 reactant.setConstant(True)
246 product = reaction.createProduct()
247 product.setSpecies("Node7")
248 product.setStoichiometry(1)
249 product.setConstant(True)
250 
251 reaction = model.createReaction()
252 reaction.setId("J8")
253 reaction.setReversible(False)
254 reaction.setFast(False)
255 reactant = reaction.createReactant()
256 reactant.setSpecies("Node5")
257 reactant.setStoichiometry(1)
258 reactant.setConstant(True)
259 product = reaction.createProduct()
260 product.setSpecies("Node8")
261 product.setStoichiometry(1)
262 product.setConstant(True)
263 
264 reaction = model.createReaction()
265 reaction.setId("J9")
266 reaction.setReversible(False)
267 reaction.setFast(False)
268 reactant = reaction.createReactant()
269 reactant.setSpecies("Node8")
270 reactant.setStoichiometry(1)
271 reactant.setConstant(True)
272 product = reaction.createProduct()
273 product.setSpecies("Node7")
274 product.setStoichiometry(1)
275 product.setConstant(True)
276 
277 reaction = model.createReaction()
278 reaction.setId("J10")
279 reaction.setReversible(False)
280 reaction.setFast(False)
281 reactant = reaction.createReactant()
282 reactant.setSpecies("Node7")
283 reactant.setStoichiometry(1)
284 reactant.setConstant(True)
285 product = reaction.createProduct()
286 product.setSpecies("Node9")
287 product.setStoichiometry(1)
288 product.setConstant(True)
289 
290 # add FBC constraints
291 
292 mplugin = model.getPlugin("fbc")
293 
294 bound= mplugin.createFluxBound()
295 bound.setId("bound1")
296 bound.setReaction("J0")
297 bound.setOperation("equal")
298 bound.setValue(10)
299 
300 objective = mplugin.createObjective()
301 objective.setId("obj1")
302 objective.setType("maximize")
303 
304 mplugin.setActiveObjectiveId("obj1")
305 
306 fluxObjective = objective.createFluxObjective()
307 fluxObjective.setReaction("J8")
308 fluxObjective.setCoefficient(1)
309 
310 writeSBMLToFile(document,"fbc_example1.xml")