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