libSBML Python API  5.18.0
example1-L3.py

A version of example1.py for SBML Level 3.

1 #!/usr/bin/env python
2 
3 #
4 # \file example1-L3.py
5 # \brief SBML Layout example
6 # \author Ralph Gauges
7 # \author Akiya Jouraku
8 # \author Frank Bergmann (adapted to use libsbml-5 package version)
9 #
10 
11 # Copyright 2004 European Media Laboratories Research gGmbH
12 #
13 # This library is free software; you can redistribute it and/or modify it
14 # under the terms of the GNU Lesser General Public License as published
15 # by the Free Software Foundation; either version 2.1 of the License, or
16 # any later version.
17 #
18 # This library is distributed in the hope that it will be useful, but
19 # WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF
20 # MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. The software and
21 # documentation provided hereunder is on an "as is" basis, and the
22 # European Media Laboratories Research gGmbH have no obligations to
23 # provide maintenance, support, updates, enhancements or modifications.
24 # In no event shall the European Media Laboratories Research gGmbH be
25 # liable to any party for direct, indirect, special, incidental or
26 # consequential damages, including lost profits, arising out of the use of
27 # this software and its documentation, even if the European Media
28 # Laboratories Research gGmbH have been advised of the possibility of such
29 # damage. See the GNU Lesser General Public License for more details.
30 #
31 # You should have received a copy of the GNU Lesser General Public License
32 # along with this library; if not, write to the Free Software Foundation,
33 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
34 #
35 # The original code contained here was initially developed by:
36 #
37 # Ralph Gaugess
38 # Bioinformatics Group
39 # European Media Laboratories Research gGmbH
40 # Schloss-Wolfsbrunnenweg 31c
41 # 69118 Heidelberg
42 # Germany
43 #
44 # http://www.eml-research.de/english/Research/BCB/
45 # mailto:ralph.gauges@eml-r.villa-bosch.de
46 #
47 # Contributor(s):
48 #
49 
50 
51 from libsbml import *
52 
53 # Creates an SBMLNamespaces object with the given SBML level, version
54 # package name, package version.
55 #
56 # (NOTE) By default, the name of package (i.e. "layout") will be used
57 # if the argument for the prefix is missing or empty. Thus the argument
58 # for the prefix can be added as follows:
59 #
60 # SBMLNamespaces sbmlns(3,1,"layout",1,"LAYOUT")
61 #
62 sbmlns = SBMLNamespaces(3, 1, "layout", 1)
63 # create the document
64 document = SBMLDocument(sbmlns)
65 # set the "required" attribute of layout package to "true"
66 document.setPkgRequired("layout", True)
67 
68 # create the Model
69 
70 model = document.createModel()
71 model.setId("TestModel")
72 document.setModel(model)
73 
74 # create the Compartment
75 
76 compartment = model.createCompartment()
77 compartment.setId("Compartment_1")
78 compartment.setConstant(True)
79 # create the Species
80 
81 species1 = model.createSpecies()
82 species1.setId("Species_1")
83 species1.setCompartment(compartment.getId())
84 species1.setHasOnlySubstanceUnits(False)
85 species1.setBoundaryCondition(False)
86 species1.setConstant(False)
87 
88 species2 = model.createSpecies()
89 species2.setId("Species_2")
90 species2.setCompartment(compartment.getId())
91 species2.setCompartment(compartment.getId())
92 species2.setHasOnlySubstanceUnits(False)
93 species2.setBoundaryCondition(False)
94 species2.setConstant(False)
95 
96 # create the Reactions
97 
98 reaction1 = model.createReaction()
99 reaction1.setId("Reaction_1")
100 reaction1.setReversible(False)
101 reaction1.setFast(False)
102 
103 reference1 = reaction1.createReactant()
104 reference1.setSpecies(species1.getId())
105 reference1.setId("SpeciesReference_1")
106 reference1.setConstant(False)
107 
108 reference2 = reaction1.createProduct()
109 reference2.setSpecies(species2.getId())
110 reference2.setId("SpeciesReference_2")
111 reference2.setConstant(False)
112 
113 reaction2 = model.createReaction()
114 reaction2.setId("Reaction_2")
115 reaction2.setReversible(False)
116 reaction2.setFast(False)
117 
118 reference3 = reaction2.createReactant()
119 reference3.setSpecies(species2.getId())
120 reference3.setId("SpeciesReference_3")
121 reference3.setConstant(False)
122 
123 reference4 = reaction2.createProduct()
124 reference4.setSpecies(species1.getId())
125 reference4.setId("SpeciesReference_4")
126 reference4.setConstant(False)
127 
128 # create the Layout
129 
130 #
131 # set the LayoutPkgNamespaces for Level 3 Version1 Layout Version 1
132 #
133 layoutns = LayoutPkgNamespaces(3, 1, 1)
134 
135 #
136 # Get a LayoutModelPlugin object plugged in the model object.
137 #
138 # The type of the returned value of SBase::getPlugin() function is SBasePlugin, and
139 # thus the value needs to be casted for the corresponding derived class.
140 #
141 
142 mplugin = (model.getPlugin("layout"))
143 
144 if mplugin is None:
145  print(
146  "[Fatal Error] Layout Extension Level " + layoutns.getLevel() + " Version " + layoutns.getVersion() + " package version " + layoutns.getPackageVersion() + " is not registered.")
147  sys.exit(1)
148 
149 #
150 # Creates a Layout object via LayoutModelPlugin object.
151 #
152 layout = mplugin.createLayout()
153 layout.setId("Layout_1")
154 layout.setDimensions(Dimensions(layoutns, 400.0, 220.0))
155 
156 # create the CompartmentGlyph
157 
158 compartmentGlyph = layout.createCompartmentGlyph()
159 compartmentGlyph.setId("CompartmentGlyph_1")
160 compartmentGlyph.setCompartmentId(compartment.getId())
161 compartmentGlyph.setBoundingBox(BoundingBox(layoutns, "bb1", 5, 5, 390, 210))
162 
163 # create the SpeciesGlyphs
164 
165 speciesGlyph1 = layout.createSpeciesGlyph()
166 speciesGlyph1.setId("SpeciesGlyph_1")
167 speciesGlyph1.setSpeciesId(species1.getId())
168 speciesGlyph1.setBoundingBox(BoundingBox(layoutns, "bb2", 80, 26, 240, 24))
169 
170 textGlyph1 = layout.createTextGlyph()
171 textGlyph1.setId("TextGlyph_01")
172 textGlyph1.setBoundingBox(BoundingBox(layoutns, "bbA", 92, 26, 228, 24))
173 textGlyph1.setOriginOfTextId(speciesGlyph1.getId())
174 textGlyph1.setGraphicalObjectId(speciesGlyph1.getId())
175 
176 speciesGlyph2 = layout.createSpeciesGlyph()
177 speciesGlyph2.setId("SpeciesGlyph_2")
178 speciesGlyph2.setSpeciesId(species2.getId())
179 speciesGlyph2.setBoundingBox(BoundingBox(layoutns, "bb3", 80, 170, 240, 24))
180 
181 textGlyph2 = layout.createTextGlyph()
182 textGlyph2.setId("TextGlyph_02")
183 textGlyph2.setBoundingBox(BoundingBox(layoutns, "bbB", 92, 170, 228, 24))
184 textGlyph2.setOriginOfTextId(speciesGlyph2.getId())
185 textGlyph2.setGraphicalObjectId(speciesGlyph2.getId())
186 
187 # create the ReactionGlyphs
188 
189 reactionGlyph1 = layout.createReactionGlyph()
190 reactionGlyph1.setId("ReactionGlyph_1")
191 reactionGlyph1.setReactionId(reaction1.getId())
192 
193 reactionCurve1 = reactionGlyph1.getCurve()
194 ls = reactionCurve1.createLineSegment()
195 ls.setStart(Point(layoutns, 165, 105))
196 ls.setEnd(Point(layoutns, 165, 115))
197 
198 reactionGlyph2 = layout.createReactionGlyph()
199 reactionGlyph2.setId("ReactionGlyph_1")
200 reactionGlyph2.setReactionId(reaction2.getId())
201 
202 reactionCurve2 = reactionGlyph2.getCurve()
203 ls = reactionCurve2.createLineSegment()
204 ls.setStart(Point(layoutns, 235, 105))
205 ls.setEnd(Point(layoutns, 235, 115))
206 
207 # add the SpeciesReferenceGlyphs
208 
209 speciesReferenceGlyph1 = reactionGlyph1.createSpeciesReferenceGlyph()
210 speciesReferenceGlyph1.setId("SpeciesReferenceGlyph_1")
211 speciesReferenceGlyph1.setSpeciesGlyphId(speciesGlyph1.getId())
212 speciesReferenceGlyph1.setSpeciesReferenceId(reference1.getId())
213 speciesReferenceGlyph1.setRole(SPECIES_ROLE_SUBSTRATE)
214 
215 speciesReferenceCurve1 = speciesReferenceGlyph1.getCurve()
216 cb = speciesReferenceCurve1.createCubicBezier()
217 cb.setStart(Point(layoutns, 165, 105))
218 cb.setBasePoint1(Point(layoutns, 165, 90))
219 cb.setBasePoint2(Point(layoutns, 165, 90))
220 cb.setEnd(Point(layoutns, 195, 60))
221 
222 speciesReferenceGlyph2 = reactionGlyph1.createSpeciesReferenceGlyph()
223 speciesReferenceGlyph2.setId("SpeciesReferenceGlyph_2")
224 speciesReferenceGlyph2.setSpeciesGlyphId(speciesGlyph2.getId())
225 speciesReferenceGlyph2.setSpeciesReferenceId(reference2.getId())
226 speciesReferenceGlyph2.setRole(SPECIES_ROLE_PRODUCT)
227 
228 speciesReferenceCurve2 = speciesReferenceGlyph2.getCurve()
229 cb = speciesReferenceCurve2.createCubicBezier()
230 cb.setStart(Point(layoutns, 165, 115))
231 cb.setBasePoint1(Point(layoutns, 165, 130))
232 cb.setBasePoint2(Point(layoutns, 165, 130))
233 cb.setEnd(Point(layoutns, 195, 160))
234 
235 speciesReferenceGlyph3 = reactionGlyph2.createSpeciesReferenceGlyph()
236 speciesReferenceGlyph3.setId("SpeciesReferenceGlyph_3")
237 speciesReferenceGlyph3.setSpeciesGlyphId(speciesGlyph2.getId())
238 speciesReferenceGlyph3.setSpeciesReferenceId(reference3.getId())
239 speciesReferenceGlyph3.setRole(SPECIES_ROLE_SUBSTRATE)
240 
241 speciesReferenceCurve3 = speciesReferenceGlyph3.getCurve()
242 cb = speciesReferenceCurve3.createCubicBezier()
243 cb.setStart(Point(layoutns, 235, 115))
244 cb.setBasePoint1(Point(layoutns, 235, 130))
245 cb.setBasePoint2(Point(layoutns, 235, 130))
246 cb.setEnd(Point(layoutns, 205, 160))
247 
248 speciesReferenceGlyph4 = reactionGlyph2.createSpeciesReferenceGlyph()
249 speciesReferenceGlyph4.setId("SpeciesReferenceGlyph_4")
250 speciesReferenceGlyph4.setSpeciesGlyphId(speciesGlyph1.getId())
251 speciesReferenceGlyph4.setSpeciesReferenceId(reference4.getId())
252 speciesReferenceGlyph4.setRole(SPECIES_ROLE_PRODUCT)
253 
254 speciesReferenceCurve4 = speciesReferenceGlyph4.getCurve()
255 cb = speciesReferenceCurve4.createCubicBezier()
256 cb.setStart(Point(layoutns, 235, 105))
257 cb.setBasePoint1(Point(layoutns, 235, 90))
258 cb.setBasePoint2(Point(layoutns, 235, 90))
259 cb.setEnd(Point(layoutns, 205, 60))
260 
261 writeSBML(document, "layout_example1_L3-python.xml")