libSBML Python API  5.11.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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 defualt, the name of package (i.e. "layout") will be used
57 # if the arugment 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 
109 reference2= reaction1.createProduct ()
110 reference2.setSpecies(species2.getId())
111 reference2.setId("SpeciesReference_2")
112 reference2.setConstant (False)
113 
114 reaction2=model.createReaction()
115 reaction2.setId("Reaction_2")
116 reaction2.setReversible(False)
117 reaction2.setFast (False)
118 
119 reference3=reaction2.createReactant ()
120 reference3.setSpecies(species2.getId())
121 reference3.setId("SpeciesReference_3")
122 reference3.setConstant (False)
123 
124 reference4=reaction2.createProduct ()
125 reference4.setSpecies(species1.getId())
126 reference4.setId("SpeciesReference_4")
127 reference4.setConstant (False)
128 
129 
130 # create the Layout
131 
132 #
133 # set the LayoutPkgNamespaces for Level 3 Version1 Layout Version 1
134 #
135 layoutns = LayoutPkgNamespaces (3, 1, 1)
136 
137 
138 #
139 # Get a LayoutModelPlugin object plugged in the model object.
140 #
141 # The type of the returned value of SBase::getPlugin() function is SBasePlugin, and
142 # thus the value needs to be casted for the corresponding derived class.
143 #
144 
145 mplugin = (model.getPlugin ("layout"))
146 
147 
148 if (mplugin == None):
149  print ("[Fatal Error] Layout Extension Level " + layoutns.getLevel () + " Version " + layoutns.getVersion () + " package version " + layoutns.getPackageVersion () + " is not registered.")
150  sys.exit(1)
151 
152 #
153 # Creates a Layout object via LayoutModelPlugin object.
154 #
155 layout = mplugin.createLayout ()
156 layout.setId("Layout_1")
157 layout.setDimensions(Dimensions(layoutns,400.0,220.0))
158 
159 # create the CompartmentGlyph
160 
161 compartmentGlyph=layout.createCompartmentGlyph()
162 compartmentGlyph.setId("CompartmentGlyph_1")
163 compartmentGlyph.setCompartmentId(compartment.getId())
164 compartmentGlyph.setBoundingBox(BoundingBox(layoutns,"bb1",5,5,390,210))
165 
166 
167 # create the SpeciesGlyphs
168 
169 speciesGlyph1=layout.createSpeciesGlyph()
170 speciesGlyph1.setId("SpeciesGlyph_1")
171 speciesGlyph1.setSpeciesId(species1.getId())
172 speciesGlyph1.setBoundingBox(BoundingBox(layoutns,"bb2",80,26,240,24))
173 
174 textGlyph1=layout.createTextGlyph()
175 textGlyph1.setId("TextGlyph_01")
176 textGlyph1.setBoundingBox(BoundingBox(layoutns,"bbA",92,26,228,24))
177 textGlyph1.setOriginOfTextId(speciesGlyph1.getId())
178 textGlyph1.setGraphicalObjectId(speciesGlyph1.getId())
179 
180 
181 speciesGlyph2=layout.createSpeciesGlyph()
182 speciesGlyph2.setId("SpeciesGlyph_2")
183 speciesGlyph2.setSpeciesId(species2.getId())
184 speciesGlyph2.setBoundingBox(BoundingBox(layoutns,"bb3",80,170,240,24))
185 
186 textGlyph2=layout.createTextGlyph()
187 textGlyph2.setId("TextGlyph_02")
188 textGlyph2.setBoundingBox(BoundingBox(layoutns,"bbB",92,170,228,24))
189 textGlyph2.setOriginOfTextId(speciesGlyph2.getId())
190 textGlyph2.setGraphicalObjectId(speciesGlyph2.getId())
191 
192 # create the ReactionGlyphs
193 
194 reactionGlyph1=layout.createReactionGlyph()
195 reactionGlyph1.setId("ReactionGlyph_1")
196 reactionGlyph1.setReactionId(reaction1.getId())
197 
198 reactionCurve1=reactionGlyph1.getCurve()
199 ls=reactionCurve1.createLineSegment()
200 ls.setStart(Point(layoutns,165,105))
201 ls.setEnd(Point(layoutns,165,115))
202 
203 reactionGlyph2=layout.createReactionGlyph()
204 reactionGlyph2.setId("ReactionGlyph_1")
205 reactionGlyph2.setReactionId(reaction2.getId())
206 
207 reactionCurve2=reactionGlyph2.getCurve()
208 ls=reactionCurve2.createLineSegment()
209 ls.setStart(Point(layoutns,235,105))
210 ls.setEnd(Point(layoutns,235,115))
211 
212 # add the SpeciesReferenceGlyphs
213 
214 speciesReferenceGlyph1=reactionGlyph1.createSpeciesReferenceGlyph()
215 speciesReferenceGlyph1.setId("SpeciesReferenceGlyph_1")
216 speciesReferenceGlyph1.setSpeciesGlyphId(speciesGlyph1.getId())
217 speciesReferenceGlyph1.setSpeciesReferenceId(reference1.getId())
218 speciesReferenceGlyph1.setRole(SPECIES_ROLE_SUBSTRATE)
219 
220 speciesReferenceCurve1=speciesReferenceGlyph1.getCurve()
221 cb=speciesReferenceCurve1.createCubicBezier()
222 cb.setStart(Point(layoutns,165,105))
223 cb.setBasePoint1(Point(layoutns,165,90))
224 cb.setBasePoint2(Point(layoutns,165,90))
225 cb.setEnd(Point(layoutns,195,60))
226 
227 speciesReferenceGlyph2=reactionGlyph1.createSpeciesReferenceGlyph()
228 speciesReferenceGlyph2.setId("SpeciesReferenceGlyph_2")
229 speciesReferenceGlyph2.setSpeciesGlyphId(speciesGlyph2.getId())
230 speciesReferenceGlyph2.setSpeciesReferenceId(reference2.getId())
231 speciesReferenceGlyph2.setRole(SPECIES_ROLE_PRODUCT)
232 
233 speciesReferenceCurve2=speciesReferenceGlyph2.getCurve()
234 cb=speciesReferenceCurve2.createCubicBezier()
235 cb.setStart(Point(layoutns,165,115))
236 cb.setBasePoint1(Point(layoutns,165,130))
237 cb.setBasePoint2(Point(layoutns,165,130))
238 cb.setEnd(Point(layoutns,195,160))
239 
240 
241 speciesReferenceGlyph3=reactionGlyph2.createSpeciesReferenceGlyph()
242 speciesReferenceGlyph3.setId("SpeciesReferenceGlyph_3")
243 speciesReferenceGlyph3.setSpeciesGlyphId(speciesGlyph2.getId())
244 speciesReferenceGlyph3.setSpeciesReferenceId(reference3.getId())
245 speciesReferenceGlyph3.setRole(SPECIES_ROLE_SUBSTRATE)
246 
247 speciesReferenceCurve3=speciesReferenceGlyph3.getCurve()
248 cb=speciesReferenceCurve3.createCubicBezier()
249 cb.setStart(Point(layoutns,235,115))
250 cb.setBasePoint1(Point(layoutns,235,130))
251 cb.setBasePoint2(Point(layoutns,235,130))
252 cb.setEnd(Point(layoutns,205,160))
253 
254 speciesReferenceGlyph4=reactionGlyph2.createSpeciesReferenceGlyph()
255 speciesReferenceGlyph4.setId("SpeciesReferenceGlyph_4")
256 speciesReferenceGlyph4.setSpeciesGlyphId(speciesGlyph1.getId())
257 speciesReferenceGlyph4.setSpeciesReferenceId(reference4.getId())
258 speciesReferenceGlyph4.setRole(SPECIES_ROLE_PRODUCT)
259 
260 speciesReferenceCurve4=speciesReferenceGlyph4.getCurve()
261 cb=speciesReferenceCurve4.createCubicBezier()
262 cb.setStart(Point(layoutns,235,105))
263 cb.setBasePoint1(Point(layoutns,235,90))
264 cb.setBasePoint2(Point(layoutns,235,90))
265 cb.setEnd(Point(layoutns,205,60))
266 
267 writeSBML(document,"layout_example1_L3-python.xml")