libSBML Python API  5.18.0
example1.py

An example layout.

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