libSBML Python API  5.11.0
createExampleSBML.py

Lengthy example of creating SBML models presented in the SBML specification.

1 #!/usr/bin/env python
2 ##
3 ## @file createExampleSBML.py
4 ## @brief Creates example SBML models presented in the SBML specification.
5 ## @author Akiya Jouraku
6 ## @author Michael Hucka
7 ## @author Sarah Keating
8 ##
9 ## <!--------------------------------------------------------------------------
10 ## This sample program is distributed under a different license than the rest
11 ## of libSBML. This program uses the open-source MIT license, as follows:
12 ##
13 ## Copyright (c) 2013-2014 by the California Institute of Technology
14 ## (California, USA), the European Bioinformatics Institute (EMBL-EBI, UK)
15 ## and the University of Heidelberg (Germany), with support from the National
16 ## Institutes of Health (USA) under grant R01GM070923. All rights reserved.
17 ##
18 ## Permission is hereby granted, free of charge, to any person obtaining a
19 ## copy of this software and associated documentation files (the "Software"),
20 ## to deal in the Software without restriction, including without limitation
21 ## the rights to use, copy, modify, merge, publish, distribute, sublicense,
22 ## and/or sell copies of the Software, and to permit persons to whom the
23 ## Software is furnished to do so, subject to the following conditions:
24 ##
25 ## The above copyright notice and this permission notice shall be included in
26 ## all copies or substantial portions of the Software.
27 ##
28 ## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
29 ## IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
30 ## FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
31 ## THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
32 ## LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
33 ## FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
34 ## DEALINGS IN THE SOFTWARE.
35 ##
36 ## Neither the name of the California Institute of Technology (Caltech), nor
37 ## of the European Bioinformatics Institute (EMBL-EBI), nor of the University
38 ## of Heidelberg, nor the names of any contributors, may be used to endorse
39 ## or promote products derived from this software without specific prior
40 ## written permission.
41 ## ------------------------------------------------------------------------ -->
42 ##
43 
44 
45 import sys
46 import os.path
47 from libsbml import *
48 
49 #
50 # These variables are used in writeExampleSBML when writing an SBML
51 # document. They are handed to libSBML functions in order to include
52 # the program information into comments within the SBML file.
53 #
54 ProgramName = "createExampleModels";
55 ProgramVersion = "1.0.0";
56 
57 #
58 # The SBML Level and Version of the example SBML models.
59 #
60 Level = 2;
61 Version = 4;
62 
63 
64 
65 
66 #===============================================================================
67 #
68 #
69 # Functions for creating the Example SBML documents.
70 #
71 #
72 #===============================================================================
73 
74 
75 #
76 #
77 # Creates an SBML model represented in "7.1 A Simple example application of SBML"
78 # in the SBML Level 2 Version 4 Specification.
79 #
80 #
81 
82 def createExampleEnzymaticReaction():
83  level = Level;
84  version = Version;
85 
86  #---------------------------------------------------------------------------
87  #
88  # Creates an SBMLDocument object
89  #
90  #---------------------------------------------------------------------------
91 
92  sbmlDoc = SBMLDocument(level, version);
93 
94  #---------------------------------------------------------------------------
95  #
96  # Creates a Model object inside the SBMLDocument object.
97  #
98  #---------------------------------------------------------------------------
99 
100  model = sbmlDoc.createModel();
101  model.setId("EnzymaticReaction");
102 
103  #---------------------------------------------------------------------------
104  #
105  # Creates UnitDefinition objects inside the Model object.
106  #
107  #---------------------------------------------------------------------------
108 
109  #---------------------------------------------------------------------------
110  # (UnitDefinition1) Creates an UnitDefinition object ("per_second")
111  #---------------------------------------------------------------------------
112 
113  unitdef = model.createUnitDefinition();
114  unitdef.setId("per_second");
115 
116  # Creates an Unit inside the UnitDefinition object
117 
118  unit = unitdef.createUnit();
119  unit.setKind(UNIT_KIND_SECOND);
120  unit.setExponent(-1);
121 
122  #--------------------------------------------------------------------------------
123  # (UnitDefinition2) Creates an UnitDefinition object ("litre_per_mole_per_second")
124  #--------------------------------------------------------------------------------
125 
126  # Note that we can reuse the pointers 'unitdef' and 'unit' because the
127  # actual UnitDefinition object (along with the Unit objects within it)
128  # is already attached to the Model object.
129 
130  unitdef = model.createUnitDefinition();
131  unitdef.setId("litre_per_mole_per_second");
132 
133  # Creates an Unit inside the UnitDefinition object ("litre_per_mole_per_second")
134 
135  unit = unitdef.createUnit();
136  unit.setKind(UNIT_KIND_MOLE);
137  unit.setExponent(-1);
138 
139  # Creates an Unit inside the UnitDefinition object ("litre_per_mole_per_second")
140 
141  unit = unitdef.createUnit();
142  unit.setKind(UNIT_KIND_LITRE);
143  unit.setExponent(1);
144 
145  # Creates an Unit inside the UnitDefinition object ("litre_per_mole_per_second")
146 
147  unit = unitdef.createUnit();
148  unit.setKind(UNIT_KIND_SECOND);
149  unit.setExponent(-1);
150 
151 
152  #---------------------------------------------------------------------------
153  #
154  # Creates a Compartment object inside the Model object.
155  #
156  #---------------------------------------------------------------------------
157 
158  compName = "cytosol";
159 
160  # Creates a Compartment object ("cytosol")
161 
162  comp = model.createCompartment();
163  comp.setId(compName);
164 
165  # Sets the "size" attribute of the Compartment object.
166  #
167  # We are not setting the units on the compartment size explicitly, so
168  # the units of this Compartment object will be the default SBML units of
169  # volume, which are liters.
170  #
171  comp.setSize(1e-14);
172 
173 
174  #---------------------------------------------------------------------------
175  #
176  # Creates Species objects inside the Model object.
177  #
178  #---------------------------------------------------------------------------
179 
180  #---------------------------------------------------------------------------
181  # (Species1) Creates a Species object ("ES")
182  #---------------------------------------------------------------------------
183 
184  # Create the Species objects inside the Model object.
185 
186  sp = model.createSpecies();
187  sp.setId("ES");
188  sp.setName("ES");
189 
190  # Sets the "compartment" attribute of the Species object to identify the
191  # compartment in which the Species object is located.
192 
193  sp.setCompartment(compName);
194 
195  # Sets the "initialAmount" attribute of the Species object.
196  #
197  # In SBML, the units of a Species object's initial quantity are
198  # determined by two attributes, "substanceUnits" and
199  # "hasOnlySubstanceUnits", and the "spatialDimensions" attribute
200  # of the Compartment object ("cytosol") in which the species
201  # object is located. Here, we are using the default values for
202  # "substanceUnits" (which is "mole") and "hasOnlySubstanceUnits"
203  # (which is "False"). The compartment in which the species is
204  # located uses volume units of liters, so the units of these
205  # species (when the species appear in numerical formulas in the
206  # model) will be moles/liters.
207  #
208  sp.setInitialAmount(0);
209 
210  #---------------------------------------------------------------------------
211  # (Species2) Creates a Species object ("P")
212  #---------------------------------------------------------------------------
213 
214  sp = model.createSpecies();
215  sp.setCompartment(compName);
216  sp.setId("P");
217  sp.setName("P");
218  sp.setInitialAmount(0);
219 
220  #---------------------------------------------------------------------------
221  # (Species3) Creates a Species object ("S")
222  #---------------------------------------------------------------------------
223 
224  sp = model.createSpecies();
225  sp.setCompartment(compName);
226  sp.setId("S");
227  sp.setName("S");
228  sp.setInitialAmount(1e-20);
229 
230  #---------------------------------------------------------------------------
231  # (Species4) Creates a Species object ("E")
232  #---------------------------------------------------------------------------
233 
234  sp = model.createSpecies();
235  sp.setCompartment(compName);
236  sp.setId("E");
237  sp.setName("E");
238  sp.setInitialAmount(5e-21);
239 
240 
241  #---------------------------------------------------------------------------
242  #
243  # Creates Reaction objects inside the Model object.
244  #
245  #---------------------------------------------------------------------------
246 
247  #---------------------------------------------------------------------------
248  # (Reaction1) Creates a Reaction object ("veq").
249  #---------------------------------------------------------------------------
250 
251  reaction = model.createReaction();
252  reaction.setId("veq");
253 
254  # (Reactant1) Creates a Reactant object that references Species "E"
255  # in the model. The object will be created within the reaction in the
256  # SBML <listOfReactants>.
257 
258  spr = reaction.createReactant();
259  spr.setSpecies("E");
260 
261  # (Reactant2) Creates a Reactant object that references Species "S"
262  # in the model.
263 
264  spr = reaction.createReactant();
265  spr.setSpecies("S");
266 
267  #---------------------------------------------------------------------------
268  # (Product1) Creates a Product object that references Species "ES" in
269  # the model.
270  #---------------------------------------------------------------------------
271 
272  spr = reaction.createProduct();
273  spr.setSpecies("ES");
274 
275  #---------------------------------------------------------------------------
276  # Creates a KineticLaw object inside the Reaction object ("veq").
277  #---------------------------------------------------------------------------
278 
279  kl = reaction.createKineticLaw();
280 
281  #---------------------------------------------------------------------------
282  # Creates an object which represents the following math of the
283  # KineticLaw.
284  #
285  # <math xmlns="http://www.w3.org/1998/Math/MathML">
286  # <apply>
287  # <times/>
288  # <ci> cytosol </ci>
289  # <apply>
290  # <minus/>
291  # <apply>
292  # <times/>
293  # <ci> kon </ci>
294  # <ci> E </ci>
295  # <ci> S </ci>
296  # </apply>
297  # <apply>
298  # <times/>
299  # <ci> koff </ci>
300  # <ci> ES </ci>
301  # </apply>
302  # </apply>
303  # </apply>
304  # </math>
305  #
306  #---------------------------------------------------------------------------
307 
308  #------------------------------------------
309  #
310  # create nodes representing the variables
311  #
312  #------------------------------------------
313 
314  astCytosol = ASTNode(AST_NAME);
315  astCytosol.setName("cytosol");
316 
317  astKon = ASTNode(AST_NAME);
318  astKon.setName("kon");
319 
320  astKoff = ASTNode(AST_NAME);
321  astKoff.setName("koff");
322 
323  astE = ASTNode(AST_NAME);
324  astE.setName("E");
325 
326  astS = ASTNode(AST_NAME);
327  astS.setName("S");
328 
329  astES = ASTNode(AST_NAME);
330  astES.setName("ES");
331 
332 
333  #--------------------------------------------
334  #
335  # create node representing
336  # <apply>
337  # <times/>
338  # <ci> koff </ci>
339  # <ci> ES </ci>
340  # </apply>
341  #
342  #--------------------------------------------
343 
344  astTimes1 = ASTNode(AST_TIMES);
345  astTimes1.addChild(astKoff);
346  astTimes1.addChild(astES);
347 
348  #--------------------------------------------
349  #
350  # create node representing
351  # <apply>
352  # <times/>
353  # <ci> kon </ci>
354  # <ci> E </ci>
355  # <ci> S </ci>
356  # </apply>
357  #
358  #
359  # (NOTES)
360  #
361  # Since there is a restriction with an of "<times/>" operation
362  # such that the is a binary class and thus only two operands can
363  # be directly added, the following code in this comment block is invalid
364  # because the code directly adds three <ci> ASTNodes to <times/> ASTNode.
365  #
366  # *astTimes = ASTNode(AST_TIMES);
367  # astTimes.addChild(astKon);
368  # astTimes.addChild(astE);
369  # astTimes.addChild(astS);
370  #
371  # The following valid code after this comment block creates the ASTNode
372  # as a binary tree.
373  #
374  # Please see "Converting between ASTs and text strings" described
375  # at http://sbml.org/Software/libSBML/docs/cpp-api/class_a_s_t_node.html
376  # for the detailed information.
377  #
378  #--------------------------------------------
379 
380  astTimes2 = ASTNode(AST_TIMES);
381  astTimes2.addChild(astE);
382  astTimes2.addChild(astS);
383 
384  astTimes = ASTNode(AST_TIMES);
385  astTimes.addChild(astKon);
386  astTimes.addChild(astTimes2);
387 
388  #--------------------------------------------
389  #
390  # create node representing
391  # <apply>
392  # <minus/>
393  # <apply>
394  # <times/>
395  # <ci> kon </ci>
396  # <ci> E </ci>
397  # <ci> S </ci>
398  # </apply>
399  # <apply>
400  # <times/>
401  # <ci> koff </ci>
402  # <ci> ES </ci>
403  # </apply>
404  # </apply>
405  #
406  #--------------------------------------------
407 
408  astMinus = ASTNode(AST_MINUS);
409  astMinus.addChild(astTimes);
410  astMinus.addChild(astTimes1);
411 
412 
413  #--------------------------------------------
414  #
415  # create node representing
416  # <apply>
417  # <times/>
418  # <ci> cytosol </ci>
419  # <apply>
420  # <minus/>
421  # <apply>
422  # <times/>
423  # <ci> kon </ci>
424  # <ci> E </ci>
425  # <ci> S </ci>
426  # </apply>
427  # <apply>
428  # <times/>
429  # <ci> koff </ci>
430  # <ci> ES </ci>
431  # </apply>
432  # </apply>
433  # </apply>
434  #
435  #--------------------------------------------
436 
437  astMath = ASTNode(AST_TIMES);
438  astMath.addChild(astCytosol);
439  astMath.addChild(astMinus);
440 
441  #---------------------------------------------
442  #
443  # set the Math element
444  #
445  #------------------------------------------------
446 
447  kl.setMath(astMath);
448 
449 
450  #---------------------------------------------------------------------------
451  # Creates local Parameter objects inside the KineticLaw object.
452  #---------------------------------------------------------------------------
453 
454  # Creates a Parameter ("kon")
455 
456  para = kl.createParameter();
457  para.setId("kon");
458  para.setValue(1000000);
459  para.setUnits("litre_per_mole_per_second");
460 
461  # Creates a Parameter ("koff")
462 
463  para = kl.createParameter();
464  para.setId("koff");
465  para.setValue(0.2);
466  para.setUnits("per_second");
467 
468 
469  #---------------------------------------------------------------------------
470  # (Reaction2) Creates a Reaction object ("vcat") .
471  #---------------------------------------------------------------------------
472 
473  reaction = model.createReaction();
474  reaction.setId("vcat");
475  reaction.setReversible(False);
476 
477  #---------------------------------------------------------------------------
478  # Creates Reactant objects inside the Reaction object ("vcat").
479  #---------------------------------------------------------------------------
480 
481  # (Reactant1) Creates a Reactant object that references Species "ES" in the
482  # model.
483 
484  spr = reaction.createReactant();
485  spr.setSpecies("ES");
486 
487  #---------------------------------------------------------------------------
488  # Creates a Product object inside the Reaction object ("vcat").
489  #---------------------------------------------------------------------------
490 
491  # (Product1) Creates a Product object that references Species "E" in the model.
492 
493  spr = reaction.createProduct();
494  spr.setSpecies("E");
495 
496  # (Product2) Creates a Product object that references Species "P" in the model.
497 
498  spr = reaction.createProduct();
499  spr.setSpecies("P");
500 
501  #---------------------------------------------------------------------------
502  # Creates a KineticLaw object inside the Reaction object ("vcat").
503  #---------------------------------------------------------------------------
504 
505  kl = reaction.createKineticLaw();
506 
507  #---------------------------------------------------------------------------
508  # Sets a math (object) to the KineticLaw object.
509  #---------------------------------------------------------------------------
510 
511  # To create mathematical expressions, one would typically construct
512  # an tree as the above example code which creates a math of another
513  # KineticLaw object. Here, to save some space and illustrate another approach
514  # of doing it, we will write out the formula in MathML form and then use a
515  # libSBML convenience function to create the tree for us.
516  # (This is a bit dangerous; it's very easy to make mistakes when writing MathML
517  # by hand, so in a real program, we would not really want to do it this way.)
518 
519  mathXMLString = """<math xmlns="http://www.w3.org/1998/Math/MathML">
520  <apply>
521  <times/>
522  <ci> cytosol </ci>
523  <ci> kcat </ci>
524  <ci> ES </ci>
525  </apply>
526  </math>
527  """
528 
529  astMath = readMathMLFromString(mathXMLString);
530  kl.setMath(astMath);
531 
532 
533  #---------------------------------------------------------------------------
534  # Creates local Parameter objects inside the KineticLaw object.
535  #---------------------------------------------------------------------------
536 
537  # Creates a Parameter ("kcat")
538 
539  para = kl.createParameter();
540  para.setId("kcat");
541  para.setValue(0.1);
542  para.setUnits("per_second");
543 
544 
545  # Returns the created SBMLDocument object.
546  # The returned object must be explicitly deleted by the caller,
547  # otherwise a memory leak will happen.
548 
549  return sbmlDoc;
550 
551 #
552 #
553 # Creates an SBML model represented in "7.2 Example involving units"
554 # in the SBML Level 2 Version 4 Specification.
555 #
556 #
557 
558 def createExampleInvolvingUnits():
559  level = Level;
560  version = Version;
561 
562  #---------------------------------------------------------------------------
563  #
564  # Creates an SBMLDocument object
565  #
566  #---------------------------------------------------------------------------
567 
568  sbmlDoc = SBMLDocument(level, version);
569 
570  # Adds the namespace for XHTML to the SBMLDocument object. We need this
571  # because we will add notes to the model. (By default, the SBML document
572  # created by SBMLDocument only declares the SBML XML namespace.)
573 
574  sbmlDoc.getNamespaces().add("http://www.w3.org/1999/xhtml", "xhtml");
575 
576  #---------------------------------------------------------------------------
577  #
578  # Creates a Model object inside the SBMLDocument object.
579  #
580  #---------------------------------------------------------------------------
581 
582  model = sbmlDoc.createModel();
583  model.setId("unitsExample");
584 
585  #---------------------------------------------------------------------------
586  #
587  # Creates UnitDefinition objects inside the Model object.
588  #
589  #---------------------------------------------------------------------------
590 
591  #---------------------------------------------------------------------------
592  # (UnitDefinition1) Creates an UnitDefinition object ("substance").
593  #
594  # This has the effect of redefining the default unit of subtance for the
595  # whole model.
596  #---------------------------------------------------------------------------
597 
598  unitdef = model.createUnitDefinition();
599  unitdef.setId("substance");
600 
601  # Creates an Unit inside the UnitDefinition object
602 
603  unit = unitdef.createUnit();
604  unit.setKind(UNIT_KIND_MOLE);
605  unit.setScale(-3);
606 
607  #--------------------------------------------------------------------------------
608  # (UnitDefinition2) Creates an UnitDefinition object ("mmls")
609  #--------------------------------------------------------------------------------
610 
611  # Note that we can reuse the pointers 'unitdef' and 'unit' because the
612  # actual UnitDefinition object (along with the Unit objects within it)
613  # is already attached to the Model object.
614 
615  unitdef = model.createUnitDefinition();
616  unitdef.setId("mmls");
617 
618  # Creates an Unit inside the UnitDefinition object ("mmls")
619 
620  unit = unitdef.createUnit();
621  unit.setKind(UNIT_KIND_MOLE);
622  unit.setScale(-3);
623 
624  # Creates an Unit inside the UnitDefinition object ("mmls")
625 
626  unit = unitdef.createUnit();
627  unit.setKind(UNIT_KIND_LITRE);
628  unit.setExponent(-1);
629 
630  # Creates an Unit inside the UnitDefinition object ("mmls")
631 
632  unit = unitdef.createUnit();
633  unit.setKind(UNIT_KIND_SECOND);
634  unit.setExponent(-1);
635 
636  #--------------------------------------------------------------------------------
637  # (UnitDefinition3) Creates an UnitDefinition object ("mml")
638  #--------------------------------------------------------------------------------
639 
640  unitdef = model.createUnitDefinition();
641  unitdef.setId("mml");
642 
643  # Creates an Unit inside the UnitDefinition object ("mml")
644 
645  unit = unitdef.createUnit();
646  unit.setKind(UNIT_KIND_MOLE);
647  unit.setScale(-3);
648 
649  # Creates an Unit inside the UnitDefinition object ("mml")
650 
651  unit = unitdef.createUnit();
652  unit.setKind(UNIT_KIND_LITRE);
653  unit.setExponent(-1);
654 
655 
656  #---------------------------------------------------------------------------
657  #
658  # Creates a Compartment object inside the Model object.
659  #
660  #---------------------------------------------------------------------------
661 
662  compName = "cell";
663 
664  # Creates a Compartment object ("cell")
665 
666  comp = model.createCompartment();
667  comp.setId(compName);
668 
669  # Sets the "size" attribute of the Compartment object.
670  #
671  # The units of this Compartment object is the default SBML
672  # units of volume (litre), and thus we don't have to explicitly invoke
673  # setUnits("litre") function to set the default units.
674  #
675  comp.setSize(1);
676 
677 
678  #---------------------------------------------------------------------------
679  #
680  # Creates Species objects inside the Model object.
681  #
682  #---------------------------------------------------------------------------
683 
684 
685  #---------------------------------------------------------------------------
686  # (Species1) Creates a Species object ("x0")
687  #---------------------------------------------------------------------------
688 
689  sp = model.createSpecies();
690  sp.setId("x0");
691 
692  # Sets the "compartment" attribute of the Species object to identify the
693  # compartnet in which the Species object located.
694 
695  sp.setCompartment(compName);
696 
697  # Sets the "initialConcentration" attribute of the Species object.
698  #
699  # The units of this Species object is determined by two attributes of this
700  # Species object ("substanceUnits" and "hasOnlySubstanceUnits") and the
701  # "spatialDimensions" attribute of the Compartment object ("cytosol") in which
702  # this species object is located.
703  # Since the default values are used for "substanceUnits" (substance (mole))
704  # and "hasOnlySubstanceUnits" (False) and the value of "spatialDimension" (3)
705  # is greater than 0, the units of this Species object is moles/liters .
706  #
707  sp.setInitialConcentration(1);
708 
709  #---------------------------------------------------------------------------
710  # (Species2) Creates a Species object ("x1")
711  #---------------------------------------------------------------------------
712 
713  sp = model.createSpecies();
714  sp.setId("x1");
715  sp.setCompartment(compName);
716  sp.setInitialConcentration(1);
717 
718  #---------------------------------------------------------------------------
719  # (Species3) Creates a Species object ("s1")
720  #---------------------------------------------------------------------------
721 
722  sp = model.createSpecies();
723  sp.setCompartment(compName);
724  sp.setId("s1");
725  sp.setInitialConcentration(1);
726 
727  #---------------------------------------------------------------------------
728  # (Species4) Creates a Species object ("s2")
729  #---------------------------------------------------------------------------
730 
731  sp = model.createSpecies();
732  sp.setCompartment(compName);
733  sp.setId("s2");
734  sp.setInitialConcentration(1);
735 
736  #---------------------------------------------------------------------------
737  #
738  # Creates global Parameter objects inside the Model object.
739  #
740  #---------------------------------------------------------------------------
741 
742  # Creates a Parameter ("vm")
743 
744  para = model.createParameter();
745  para.setId("vm");
746  para.setValue(2);
747  para.setUnits("mmls");
748 
749  # Creates a Parameter ("km")
750 
751  para = model.createParameter();
752  para.setId("km");
753  para.setValue(2);
754  para.setUnits("mml");
755 
756 
757  #---------------------------------------------------------------------------
758  #
759  # Creates Reaction objects inside the Model object.
760  #
761  #---------------------------------------------------------------------------
762 
763  #---------------------------------------------------------------------------
764  # (Reaction1) Creates a Reaction object ("v1").
765  #---------------------------------------------------------------------------
766 
767  reaction = model.createReaction();
768  reaction.setId("v1");
769 
770  #---------------------------------------------------------------------------
771  # Creates Reactant objects inside the Reaction object ("v1").
772  #---------------------------------------------------------------------------
773 
774  # (Reactant1) Creates a Reactant object that references Species "x0"
775  # in the model.
776 
777  spr = reaction.createReactant();
778  spr.setSpecies("x0");
779 
780  #---------------------------------------------------------------------------
781  # Creates a Product object inside the Reaction object ("v1").
782  #---------------------------------------------------------------------------
783 
784  # Creates a Product object that references Species "s1" in the model.
785 
786  spr = reaction.createProduct();
787  spr.setSpecies("s1");
788 
789  #---------------------------------------------------------------------------
790  # Creates a KineticLaw object inside the Reaction object ("v1").
791  #---------------------------------------------------------------------------
792 
793  kl = reaction.createKineticLaw();
794 
795  # Creates a <notes> element in the KineticLaw object.
796  # Here we illustrate how to do it using a literal string. This requires
797  # known the required syntax of XHTML and the requirements for SBML <notes>
798  # elements. Later below, we show how to create notes using objects instead
799  # of strings.
800 
801  notesString = "<xhtml:p> ((vm * s1)/(km + s1)) * cell </xhtml:p>";
802  kl.setNotes(notesString);
803 
804  #---------------------------------------------------------------------------
805  # Creates an object which represents the following KineticLaw object.
806  #
807  # <math xmlns="http://www.w3.org/1998/Math/MathML">
808  # <apply>
809  # <times/>
810  # <apply>
811  # <divide/>
812  # <apply>
813  # <times/>
814  # <ci> vm </ci>
815  # <ci> s1 </ci>
816  # </apply>
817  # <apply>
818  # <plus/>
819  # <ci> km </ci>
820  # <ci> s1 </ci>
821  # </apply>
822  # </apply>
823  # <ci> cell </ci>
824  # </apply>
825  # </math>
826  #---------------------------------------------------------------------------
827 
828  #
829  # In the following code, objects, which construct an tree
830  # of the above math, are created and added in the order of preorder traversal
831  # of the tree (i.e. the order corresponds to the nested structure of the above
832  # MathML elements), and thus the following code maybe a bit more efficient but
833  # maybe a bit difficult to read.
834  #
835 
836  astMath = ASTNode(AST_TIMES);
837 
838  astMath.addChild(ASTNode(AST_DIVIDE));
839  astDivide = astMath.getLeftChild();
840 
841  astDivide.addChild(ASTNode(AST_TIMES));
842  astTimes = astDivide.getLeftChild();
843 
844  astTimes.addChild(ASTNode(AST_NAME));
845  astTimes.getLeftChild().setName("vm");
846 
847  astTimes.addChild(ASTNode(AST_NAME));
848  astTimes.getRightChild().setName("s1");
849 
850  astDivide.addChild(ASTNode(AST_PLUS));
851  astPlus = astDivide.getRightChild();
852 
853  astPlus.addChild(ASTNode(AST_NAME));
854  astPlus.getLeftChild().setName("km");
855 
856  astPlus.addChild(ASTNode(AST_NAME));
857  astPlus.getRightChild().setName("s1");
858 
859 
860  astMath.addChild(ASTNode(AST_NAME));
861  astMath.getRightChild().setName("cell");
862 
863  #---------------------------------------------
864  #
865  # set the Math element
866  #
867  #------------------------------------------------
868 
869  kl.setMath(astMath);
870 
871  #---------------------------------------------------------------------------
872  # (Reaction2) Creates a Reaction object ("v2").
873  #---------------------------------------------------------------------------
874 
875  reaction = model.createReaction();
876  reaction.setId("v2");
877 
878  #---------------------------------------------------------------------------
879  # Creates Reactant objects inside the Reaction object ("v2").
880  #---------------------------------------------------------------------------
881 
882  # (Reactant2) Creates a Reactant object that references Species "s1"
883  # in the model.
884 
885  spr = reaction.createReactant();
886  spr.setSpecies("s1");
887 
888  #---------------------------------------------------------------------------
889  # Creates a Product object inside the Reaction object ("v2").
890  #---------------------------------------------------------------------------
891 
892  # Creates a Product object that references Species "s2" in the model.
893 
894  spr = reaction.createProduct();
895  spr.setSpecies("s2");
896 
897  #---------------------------------------------------------------------------
898  # Creates a KineticLaw object inside the Reaction object ("v2").
899  #---------------------------------------------------------------------------
900 
901  kl = reaction.createKineticLaw();
902 
903  # Sets a notes (by XMLNode) to the KineticLaw object.
904  #
905  # The following code is an alternative to using setNotes(const string&).
906  # The equivalent code would be like this:
907  #
908  # notesString = "<xhtml:p>((vm * s2)/(km + s2))*cell</xhtml:p>";
909  # kl.setNotes(notesString);
910 
911  # Creates an of start element (<xhtml:p>) without attributes.
912 
913  notesXMLNode = XMLNode(
914  XMLTriple("p", "", "xhtml"),
915  XMLAttributes());
916 
917  # Adds a text element to the start element.
918 
919  notesXMLNode.addChild(XMLNode(" ((vm * s2)/(km + s2)) * cell "));
920 
921  # Adds it to the kineticLaw object.
922 
923  kl.setNotes(notesXMLNode);
924 
925  #---------------------------------------------------------------------------
926  # Sets a math (object) to the KineticLaw object.
927  #---------------------------------------------------------------------------
928 
929  # To create mathematical expressions, one would typically construct
930  # an tree as the above example code which creates a math of another
931  # KineticLaw object. Here, to save some space and illustrate another approach
932  # of doing it, we will write out the formula in MathML form and then use a
933  # libSBML convenience function to create the tree for us.
934  # (This is a bit dangerous; it's very easy to make mistakes when writing MathML
935  # by hand, so in a real program, we would not really want to do it this way.)
936 
937  mathXMLString = """<math xmlns="http://www.w3.org/1998/Math/MathML">
938  <apply>
939  <times/>
940  <apply>
941  <divide/>
942  <apply>
943  <times/>
944  <ci> vm </ci>
945  <ci> s2 </ci>
946  </apply>
947  <apply>
948  <plus/>
949  <ci> km </ci>
950  <ci> s2 </ci>
951  </apply>
952  </apply>
953  <ci> cell </ci>
954  </apply>
955  </math>
956  """
957 
958  astMath = readMathMLFromString(mathXMLString);
959  kl.setMath(astMath);
960 
961 
962 
963  #---------------------------------------------------------------------------
964  # (Reaction3) Creates a Reaction object ("v3").
965  #---------------------------------------------------------------------------
966 
967  reaction = model.createReaction();
968  reaction.setId("v3");
969 
970  #---------------------------------------------------------------------------
971  # Creates Reactant objects inside the Reaction object ("v3").
972  #---------------------------------------------------------------------------
973 
974  # (Reactant2) Creates a Reactant object that references Species "s2"
975  # in the model.
976 
977  spr = reaction.createReactant();
978  spr.setSpecies("s2");
979 
980  #---------------------------------------------------------------------------
981  # Creates a Product object inside the Reaction object ("v3").
982  #---------------------------------------------------------------------------
983 
984  # Creates a Product object that references Species "x1" in the model.
985 
986  spr = reaction.createProduct();
987  spr.setSpecies("x1");
988 
989 
990  #---------------------------------------------------------------------------
991  # Creates a KineticLaw object inside the Reaction object ("v3").
992  #---------------------------------------------------------------------------
993 
994  kl = reaction.createKineticLaw();
995 
996  # Sets a notes (by string) to the KineticLaw object.
997 
998  notesString = "<xhtml:p> ((vm * x1)/(km + x1)) * cell </xhtml:p>";
999  kl.setNotes(notesString);
1000 
1001  #---------------------------------------------------------------------------
1002  # Sets a math (object) to the KineticLaw object.
1003  #---------------------------------------------------------------------------
1004 
1005  mathXMLString = """<math xmlns="http://www.w3.org/1998/Math/MathML">
1006  <apply>
1007  <times/>
1008  <apply>
1009  <divide/>
1010  <apply>
1011  <times/>
1012  <ci> vm </ci>
1013  <ci> x1 </ci>
1014  </apply>
1015  <apply>
1016  <plus/>
1017  <ci> km </ci>
1018  <ci> x1 </ci>
1019  </apply>
1020  </apply>
1021  <ci> cell </ci>
1022  </apply>
1023  </math>
1024  """
1025 
1026  astMath = readMathMLFromString(mathXMLString);
1027  kl.setMath(astMath);
1028 
1029  # Returns the created SBMLDocument object.
1030  # The returned object must be explicitly deleted by the caller,
1031  # otherwise memory leak will happen.
1032 
1033  return sbmlDoc;
1034 
1035 
1036 
1037 #
1038 #
1039 # Creates an SBML model represented in "7.8 Example involving function definitions"
1040 # in the SBML Level 2 Version 4 Specification.
1041 #
1042 #
1043 
1044 def createExampleInvolvingFunctionDefinitions():
1045  level = Level;
1046  version = Version;
1047 
1048  #---------------------------------------------------------------------------
1049  #
1050  # Creates an SBMLDocument object
1051  #
1052  #---------------------------------------------------------------------------
1053 
1054  sbmlDoc = SBMLDocument(level, version);
1055 
1056  #---------------------------------------------------------------------------
1057  #
1058  # Creates a Model object inside the SBMLDocument object.
1059  #
1060  #---------------------------------------------------------------------------
1061 
1062  model = sbmlDoc.createModel();
1063  model.setId("functionExample");
1064 
1065  #---------------------------------------------------------------------------
1066  #
1067  # Creates a FunctionDefinition object inside the Model object.
1068  #
1069  #---------------------------------------------------------------------------
1070 
1071  fdef = model.createFunctionDefinition();
1072  fdef.setId("f");
1073 
1074  # Sets a math (object) to the FunctionDefinition object.
1075 
1076  mathXMLString = """<math xmlns="http://www.w3.org/1998/Math/MathML">
1077  <lambda>
1078  <bvar>
1079  <ci> x </ci>
1080  </bvar>
1081  <apply>
1082  <times/>
1083  <ci> x </ci>
1084  <cn> 2 </cn>
1085  </apply>
1086  </lambda>
1087  </math>
1088  """
1089 
1090  astMath = readMathMLFromString(mathXMLString);
1091  fdef.setMath(astMath);
1092 
1093  #---------------------------------------------------------------------------
1094  #
1095  # Creates a Compartment object inside the Model object.
1096  #
1097  #---------------------------------------------------------------------------
1098 
1099  compName = "compartmentOne";
1100 
1101  # Creates a Compartment object ("compartmentOne")
1102 
1103  comp = model.createCompartment();
1104  comp.setId(compName);
1105 
1106  # Sets the "size" attribute of the Compartment object.
1107  #
1108  # The units of this Compartment object is the default SBML
1109  # units of volume (litre), and thus we don't have to explicitly invoke
1110  # setUnits("litre") function to set the default units.
1111  #
1112  comp.setSize(1);
1113 
1114 
1115  #---------------------------------------------------------------------------
1116  #
1117  # Creates Species objects inside the Model object.
1118  #
1119  #---------------------------------------------------------------------------
1120 
1121  #---------------------------------------------------------------------------
1122  # (Species1) Creates a Species object ("S1")
1123  #---------------------------------------------------------------------------
1124 
1125  sp = model.createSpecies();
1126  sp.setId("S1");
1127 
1128  # Sets the "compartment" attribute of the Species object to identify the
1129  # compartnet in which the Species object located.
1130 
1131  sp.setCompartment(compName);
1132 
1133  # Sets the "initialConcentration" attribute of the Species object.
1134  #
1135  # The units of this Species object is determined by two attributes of this
1136  # Species object ("substanceUnits" and "hasOnlySubstanceUnits") and the
1137  # "spatialDimension" attribute of the Compartment object ("cytosol") in which
1138  # this species object located.
1139  # Since the default values are used for "substanceUnits" (substance (mole))
1140  # and "hasOnlySubstanceUnits" (False) and the value of "spatialDimension" (3)
1141  # is greater than 0, the units of this Species object is mole/litre .
1142  #
1143 
1144  sp.setInitialConcentration(1);
1145 
1146  #---------------------------------------------------------------------------
1147  # (Species2) Creates a Species object ("S2")
1148  #---------------------------------------------------------------------------
1149 
1150  sp = model.createSpecies();
1151  sp.setId("S2");
1152  sp.setCompartment(compName);
1153  sp.setInitialConcentration(0);
1154 
1155 
1156  #---------------------------------------------------------------------------
1157  #
1158  # Creates a global Parameter object inside the Model object.
1159  #
1160  #---------------------------------------------------------------------------
1161 
1162  # Creates a Parameter ("t")
1163 
1164  para = model.createParameter();
1165  para.setId("t");
1166  para.setValue(1);
1167  para.setUnits("second");
1168 
1169 
1170  #---------------------------------------------------------------------------
1171  #
1172  # Creates Reaction objects inside the Model object.
1173  #
1174  #---------------------------------------------------------------------------
1175 
1176  #---------------------------------------------------------------------------
1177  # (Reaction1) Creates a Reaction object ("reaction_1").
1178  #---------------------------------------------------------------------------
1179 
1180  reaction = model.createReaction();
1181  reaction.setId("reaction_1");
1182  reaction.setReversible(False);
1183 
1184  #---------------------------------------------------------------------------
1185  # Creates Reactant objects inside the Reaction object ("reaction_1").
1186  #---------------------------------------------------------------------------
1187 
1188  # (Reactant1) Creates a Reactant object that references Species "S1"
1189  # in the model.
1190 
1191  spr = reaction.createReactant();
1192  spr.setSpecies("S1");
1193 
1194  #---------------------------------------------------------------------------
1195  # Creates a Product object inside the Reaction object ("reaction_1").
1196  #---------------------------------------------------------------------------
1197 
1198  # Creates a Product object that references Species "S2" in the model.
1199 
1200  spr = reaction.createProduct();
1201  spr.setSpecies("S2");
1202 
1203 
1204  #---------------------------------------------------------------------------
1205  # Creates a KineticLaw object inside the Reaction object ("reaction_1").
1206  #---------------------------------------------------------------------------
1207 
1208  kl = reaction.createKineticLaw();
1209 
1210  #---------------------------------------------------------------------------
1211  # Sets a math (object) to the KineticLaw object.
1212  #---------------------------------------------------------------------------
1213 
1214  mathXMLString = """<math xmlns="http://www.w3.org/1998/Math/MathML">
1215  <apply>
1216  <divide/>
1217  <apply>
1218  <times/>
1219  <apply>
1220  <ci> f </ci>
1221  <ci> S1 </ci>
1222  </apply>
1223  <ci> compartmentOne </ci>
1224  </apply>
1225  <ci> t </ci>
1226  </apply>
1227  </math>
1228  """
1229 
1230  astMath = readMathMLFromString(mathXMLString);
1231  kl.setMath(astMath);
1232 
1233  # Returns the created SBMLDocument object.
1234  # The returned object must be explicitly deleted by the caller,
1235  # otherwise memory leak will happen.
1236 
1237  return sbmlDoc;
1238 
1239 #===============================================================================
1240 #
1241 #
1242 # Helper functions for writing/validating the given SBML documents.
1243 #
1244 #
1245 #===============================================================================
1246 
1247 #
1248 #
1249 # Validates the given SBMLDocument.
1250 #
1251 # This function is based on validateSBML.py implemented by
1252 # Sarah Keating, Ben Bornstein, and Michael Hucka.
1253 #
1254 #
1255 
1256 def validateExampleSBML(sbmlDoc):
1257  if (sbmlDoc == None):
1258  print("validateExampleSBML: given a None SBML Document");
1259  return False;
1260 
1261  consistencyMessages = "";
1262  validationMessages = "";
1263  noProblems = True;
1264  numCheckFailures = 0;
1265  numConsistencyErrors = 0;
1266  numConsistencyWarnings = 0;
1267  numValidationErrors = 0;
1268  numValidationWarnings = 0;
1269 
1270  # LibSBML 3.3 is lenient when generating models from scratch using the
1271  # API for creating objects. Once the whole model is done and before it
1272  # gets written out, it's important to check that the whole model is in
1273  # fact complete, consistent and valid.
1274 
1275  numCheckFailures = sbmlDoc.checkInternalConsistency();
1276  if (numCheckFailures > 0):
1277  noProblems = False;
1278  for i in range(0, numCheckFailures):
1279  sbmlErr = sbmlDoc.getError(i);
1280  if (sbmlErr.isFatal() or sbmlErr.isError()):
1281  numConsistencyErrors = 1 + numConsistencyErrors;
1282  else:
1283  numConsistencyWarnings = 1+numConsistencyWarnings;
1284  sbmlDoc.printErrors();
1285 
1286  # If the internal checks fail, it makes little sense to attempt
1287  # further validation, because the model may be too compromised to
1288  # be properly interpreted.
1289 
1290  if (numConsistencyErrors > 0):
1291  consistencyMessages = consistencyMessages + "Further validation aborted.";
1292  else:
1293  numCheckFailures = sbmlDoc.checkConsistency();
1294  if (numCheckFailures > 0):
1295  noProblems = False;
1296  for i in range(0, numCheckFailures):
1297  sbmlErr = sbmlDoc.getError(i);
1298  if (sbmlErr.isFatal() or sbmlErr.isError()):
1299  numValidationErrors = 1+numValidationErrors;
1300  else:
1301  numValidationWarnings = 1+numValidationWarnings;
1302  sbmlDoc.printErrors();
1303 
1304  if (noProblems):
1305  return True;
1306  else:
1307  if (numConsistencyErrors > 0):
1308  tmp = ""
1309  if (numConsistencyErrors > 1):
1310  tmp = "s";
1311  print("ERROR: encountered " + str(numConsistencyErrors) + " consistency error" + tmp + " in model '" + sbmlDoc.getModel().getId() + "'.");
1312 
1313  if (numConsistencyWarnings > 0):
1314  tmp = ""
1315  if (numConsistencyWarnings > 1):
1316  tmp = "s"
1317  print("Notice: encountered " + str(numConsistencyWarnings)
1318  + " consistency warning" + tmp
1319  + " in model '" + sbmlDoc.getModel().getId() + "'.");
1320  print(consistencyMessages);
1321 
1322  if (numValidationErrors > 0):
1323  tmp = ""
1324  if (numValidationErrors > 1):
1325  tmp = "s"
1326  print("ERROR: encountered " + str(numValidationErrors) + " validation error" + (tmp)
1327  + " in model '" + sbmlDoc.getModel().getId() + "'.");
1328  if (numValidationWarnings > 0):
1329  tmp = ""
1330  if (numValidationWarnings > 1):
1331  tmp = "s"
1332  print("Notice: encountered " + str(numValidationWarnings)
1333  + " validation warning" + (tmp)
1334  + " in model '" + sbmlDoc.getModel().getId() + "'.");
1335  print(validationMessages);
1336 
1337  return (numConsistencyErrors == 0 and numValidationErrors == 0);
1338 
1339 
1340 #
1341 #
1342 # Writes the given SBMLDocument to the given file.
1343 #
1344 #
1345 def writeExampleSBML(sbmlDoc, filename):
1346  result = writeSBML(sbmlDoc, filename);
1347  if (result == 1):
1348  print("Wrote file '" + filename + "'");
1349  return True;
1350  else:
1351  print("Failed to write '" + filename + "'");
1352  return False;
1353 
1354 
1355 #===============================================================================
1356 #
1357 # Main routine
1358 #
1359 # Creates SBML models represented in "Example models expressed in XML using
1360 # SBML" in Section 7 of the SBML Level 2 Version 4 specification(*).
1361 #
1362 # (*) The specification document is available at the following URL:
1363 # http://sbml.org/Documents/Specifications
1364 #
1365 #===============================================================================
1366 #
1367 def main (args):
1368  sbmlDoc = None;
1369  SBMLok = False;
1370 
1371  try:
1372  #-------------------------------------------------
1373  # 7.1 A Simple example application of SBML
1374  #-------------------------------------------------
1375 
1376  sbmlDoc = createExampleEnzymaticReaction();
1377  SBMLok = validateExampleSBML(sbmlDoc);
1378  if (SBMLok):
1379  writeExampleSBML(sbmlDoc, "enzymaticreaction.xml");
1380  if (not SBMLok):
1381  return 1;
1382 
1383  #-------------------------------------------------
1384  # 7.2 Example involving units
1385  #-------------------------------------------------
1386 
1387  sbmlDoc = createExampleInvolvingUnits();
1388  SBMLok = validateExampleSBML(sbmlDoc);
1389  if (SBMLok):
1390  writeExampleSBML(sbmlDoc, "units.xml");
1391  if (not SBMLok):
1392  return 1;
1393 
1394  #-------------------------------------------------
1395  # 7.8 Example involving function definitions
1396  #-------------------------------------------------
1397 
1398  sbmlDoc = createExampleInvolvingFunctionDefinitions();
1399  SBMLok = validateExampleSBML(sbmlDoc);
1400  if (SBMLok):
1401  writeExampleSBML(sbmlDoc, "functiondef.xml");
1402  if (not SBMLok):
1403  return 1;
1404  except:
1405  print("Unexpected exceptional condition encountered.");
1406  return 1;
1407 
1408  # A 0 return status is the standard Unix/Linux way to say "all ok".
1409  return 0;
1410 
1411 
1412 if __name__ == '__main__':
1413  main(sys.argv)