libSBML Python API  5.11.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-2014 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 # create the Model
50 
51 model= document.createModel();
52 
53 # create the Compartment
54 
55 compartment = model.createCompartment();
56 compartment.setId("compartment");
57 compartment.setConstant(True);
58 compartment.setSize(1);
59 
60 # create the Species
61 
62 species = model.createSpecies();
63 species.setId("Node1");
64 species.setCompartment("compartment");
65 species.setBoundaryCondition(False);
66 species.setConstant(False);
67 species.setHasOnlySubstanceUnits(False);
68 
69 species = model.createSpecies();
70 species.setId("Node2");
71 species.setCompartment("compartment");
72 species.setBoundaryCondition(False);
73 species.setConstant(False);
74 species.setHasOnlySubstanceUnits(False);
75 
76 species = model.createSpecies();
77 species.setId("Node3");
78 species.setCompartment("compartment");
79 species.setBoundaryCondition(False);
80 species.setConstant(False);
81 species.setHasOnlySubstanceUnits(False);
82 
83 species = model.createSpecies();
84 species.setId("Node4");
85 species.setCompartment("compartment");
86 species.setBoundaryCondition(False);
87 species.setConstant(False);
88 species.setHasOnlySubstanceUnits(False);
89 
90 species = model.createSpecies();
91 species.setId("Node5");
92 species.setCompartment("compartment");
93 species.setBoundaryCondition(False);
94 species.setConstant(False);
95 species.setHasOnlySubstanceUnits(False);
96 
97 species = model.createSpecies();
98 species.setId("Node6");
99 species.setCompartment("compartment");
100 species.setBoundaryCondition(False);
101 species.setConstant(False);
102 species.setHasOnlySubstanceUnits(False);
103 
104 species = model.createSpecies();
105 species.setId("Node7");
106 species.setCompartment("compartment");
107 species.setBoundaryCondition(False);
108 species.setConstant(False);
109 species.setHasOnlySubstanceUnits(False);
110 
111 species = model.createSpecies();
112 species.setId("Node8");
113 species.setCompartment("compartment");
114 species.setBoundaryCondition(False);
115 species.setConstant(False);
116 species.setHasOnlySubstanceUnits(False);
117 
118 species = model.createSpecies();
119 species.setId("Node0");
120 species.setCompartment("compartment");
121 species.setBoundaryCondition(True);
122 species.setConstant(False);
123 species.setHasOnlySubstanceUnits(False);
124 
125 species = model.createSpecies();
126 species.setId("Node9");
127 species.setCompartment("compartment");
128 species.setBoundaryCondition(True);
129 species.setConstant(False);
130 species.setHasOnlySubstanceUnits(False);
131 
132 # create reactions
133 
134 reaction = model.createReaction();
135 reaction.setId("J0");
136 reaction.setReversible(False);
137 reaction.setFast(False);
138 reactant = reaction.createReactant();
139 reactant.setSpecies("Node0");
140 reactant.setStoichiometry(1);
141 reactant.setConstant(True);
142 product = reaction.createProduct();
143 product.setSpecies("Node1");
144 product.setStoichiometry(1);
145 product.setConstant(True);
146 
147 reaction = model.createReaction();
148 reaction.setId("J1");
149 reaction.setReversible(False);
150 reaction.setFast(False);
151 reactant = reaction.createReactant();
152 reactant.setSpecies("Node1");
153 reactant.setStoichiometry(1);
154 reactant.setConstant(True);
155 product = reaction.createProduct();
156 product.setSpecies("Node2");
157 product.setStoichiometry(1);
158 product.setConstant(True);
159 
160 reaction = model.createReaction();
161 reaction.setId("J2");
162 reaction.setReversible(False);
163 reaction.setFast(False);
164 reactant = reaction.createReactant();
165 reactant.setSpecies("Node2");
166 reactant.setStoichiometry(1);
167 reactant.setConstant(True);
168 product = reaction.createProduct();
169 product.setSpecies("Node3");
170 product.setStoichiometry(1);
171 product.setConstant(True);
172 
173 reaction = model.createReaction();
174 reaction.setId("J3");
175 reaction.setReversible(False);
176 reaction.setFast(False);
177 reactant = reaction.createReactant();
178 reactant.setSpecies("Node1");
179 reactant.setStoichiometry(1);
180 reactant.setConstant(True);
181 product = reaction.createProduct();
182 product.setSpecies("Node4");
183 product.setStoichiometry(1);
184 product.setConstant(True);
185 
186 reaction = model.createReaction();
187 reaction.setId("J4");
188 reaction.setReversible(False);
189 reaction.setFast(False);
190 reactant = reaction.createReactant();
191 reactant.setSpecies("Node4");
192 reactant.setStoichiometry(1);
193 reactant.setConstant(True);
194 product = reaction.createProduct();
195 product.setSpecies("Node3");
196 product.setStoichiometry(1);
197 product.setConstant(True);
198 
199 reaction = model.createReaction();
200 reaction.setId("J5");
201 reaction.setReversible(False);
202 reaction.setFast(False);
203 reactant = reaction.createReactant();
204 reactant.setSpecies("Node3");
205 reactant.setStoichiometry(1);
206 reactant.setConstant(True);
207 product = reaction.createProduct();
208 product.setSpecies("Node5");
209 product.setStoichiometry(1);
210 product.setConstant(True);
211 
212 reaction = model.createReaction();
213 reaction.setId("J6");
214 reaction.setReversible(False);
215 reaction.setFast(False);
216 reactant = reaction.createReactant();
217 reactant.setSpecies("Node5");
218 reactant.setStoichiometry(1);
219 reactant.setConstant(True);
220 product = reaction.createProduct();
221 product.setSpecies("Node6");
222 product.setStoichiometry(1);
223 product.setConstant(True);
224 
225 reaction = model.createReaction();
226 reaction.setId("J7");
227 reaction.setReversible(False);
228 reaction.setFast(False);
229 reactant = reaction.createReactant();
230 reactant.setSpecies("Node6");
231 reactant.setStoichiometry(1);
232 reactant.setConstant(True);
233 product = reaction.createProduct();
234 product.setSpecies("Node7");
235 product.setStoichiometry(1);
236 product.setConstant(True);
237 
238 reaction = model.createReaction();
239 reaction.setId("J8");
240 reaction.setReversible(False);
241 reaction.setFast(False);
242 reactant = reaction.createReactant();
243 reactant.setSpecies("Node5");
244 reactant.setStoichiometry(1);
245 reactant.setConstant(True);
246 product = reaction.createProduct();
247 product.setSpecies("Node8");
248 product.setStoichiometry(1);
249 product.setConstant(True);
250 
251 reaction = model.createReaction();
252 reaction.setId("J9");
253 reaction.setReversible(False);
254 reaction.setFast(False);
255 reactant = reaction.createReactant();
256 reactant.setSpecies("Node8");
257 reactant.setStoichiometry(1);
258 reactant.setConstant(True);
259 product = reaction.createProduct();
260 product.setSpecies("Node7");
261 product.setStoichiometry(1);
262 product.setConstant(True);
263 
264 reaction = model.createReaction();
265 reaction.setId("J10");
266 reaction.setReversible(False);
267 reaction.setFast(False);
268 reactant = reaction.createReactant();
269 reactant.setSpecies("Node7");
270 reactant.setStoichiometry(1);
271 reactant.setConstant(True);
272 product = reaction.createProduct();
273 product.setSpecies("Node9");
274 product.setStoichiometry(1);
275 product.setConstant(True);
276 
277 # add FBC constraints
278 
279 mplugin = model.getPlugin("fbc");
280 
281 
282 bound= mplugin.createFluxBound();
283 bound.setId("bound1");
284 bound.setReaction("J0");
285 bound.setOperation("equal");
286 bound.setValue(10);
287 
288 objective = mplugin.createObjective();
289 objective.setId("obj1");
290 objective.setType("maximize");
291 
292 mplugin.setActiveObjectiveId("obj1");
293 
294 fluxObjective = objective.createFluxObjective();
295 fluxObjective.setReaction("J8");
296 fluxObjective.setCoefficient(1);
297 
298 writeSBMLToFile(document,"fbc_example1.xml");