-
Notifications
You must be signed in to change notification settings - Fork 0
/
sbml32spec.py
379 lines (298 loc) · 12 KB
/
sbml32spec.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#!/usr/bin/env python3
'''
initial attempt at creating an SBML API using modelspec
https://github.com/combine-org/compbiolibs/issues/28
based on sbml.level-3.version-2.core.release-2.pdf
'''
import modelspec
from modelspec import field, instance_of, optional
from modelspec.base_types import Base
from typing import List
from sbml_validators import *
@modelspec.define
class Notes(Base):
'''
XHTML field of SBase
Args:
xmlns: str fixed "http://www.w3.org/1999/xhtml"
content: str valid XHTML
'''
xmlns: str = field(default="http://www.w3.org/1999/xhtml",validator=[instance_of(str),xmlns_notes])
content: str = field(default=None,validator=optional([instance_of(str),valid_xhtml]))
@modelspec.define
class Math(Base):
'''
Subset of MathML 2.0 used to define all formulae in SBML
'''
xmlns: str = field(default="http://www.w3.org/1998/Math/MathML",validator=[instance_of(str),xmlns_math])
content: str = field(default=None,validator=optional([instance_of(str),valid_mathml]))
@modelspec.define
class SBase(Base):
"""
Abstract base class for all SBML objects
Args:
sid: SId optional
name: string optional
metaid: XML ID optional
sboTerm: SBOTerm optional
notes: XHTML 1.0 optional
annotation: XML content optional
"""
sid: str = field(default=None,validator=optional([instance_of(str),valid_sid]))
name: str = field(default=None,validator=optional(instance_of(str)))
metaid: str = field(default=None,validator=optional([instance_of(str),valid_xml_id]))
sboTerm: str = field(default=None,validator=optional([instance_of(str),valid_sbo]))
notes: Notes = field(default=None,validator=optional(instance_of(Notes)))
annotation: str = field(default=None,validator=optional([instance_of(str),valid_xml_content]))
@modelspec.define
class Trigger(SBase):
initialValue: bool = field(default=None,validator=instance_of(bool))
persistent: bool = field(default=None,validator=instance_of(bool))
math: str = field(default=None,validator=optional(instance_of(str)))
@modelspec.define
class Priority(SBase):
math: str = field(default=None,validator=optional(instance_of(str)))
@modelspec.define
class Delay(SBase):
math: str = field(default=None,validator=optional(instance_of(str)))
@modelspec.define
class EventAssignment(SBase):
'''
Args:
variable: SIdRef
'''
math: str = field(default=None,validator=optional(instance_of(str)))
variable: str = field(default=None,validator=optional(instance_of(str)))
@modelspec.define
class Event(SBase):
useValuesFromTriggerTime: bool = field(default=None,validator=instance_of(bool))
trigger: Trigger = field(default=None, validator=optional(instance_of(Trigger)))
priority: Priority = field(default=None, validator=optional(instance_of(Priority)))
delay: Delay = field(default=None, validator=optional(instance_of(Delay)))
listOfEventAssignments: List[EventAssignment] = field(factory=list)
@modelspec.define
class SimpleSpeciesReference(SBase):
"""
Base class used by SpeciesReference and ModifierSpeciesReference
Args:
species: SIdRef
"""
species: str = field(default=None,validator=instance_of(str))
@modelspec.define
class ModifierSpeciesReference(SimpleSpeciesReference):
''
@modelspec.define
class SpeciesReference(SimpleSpeciesReference):
"""
Args:
stoichiometry: double optional
constant: boolean
"""
stoichiometry: float = field(default=None,validator=optional(instance_of(float)))
constant: bool = field(default=None,validator=instance_of(bool))
@modelspec.define
class LocalParameter(SBase):
"""
Args:
units: UnitSIdRef optional
"""
value: float = field(default=None,validator=optional(instance_of(float)))
units: str = field(default=None,validator=optional(instance_of(str)))
@modelspec.define
class KineticLaw(SBase):
"""
"""
math: str = field(default=None,validator=optional(instance_of(str)))
listOfLocalParameters: List[LocalParameter] = field(factory=list)
@modelspec.define
class Reaction(SBase):
"""
A model reaction
Args:
reversible: boolean
compartment: SIdRef optional
"""
reversible: bool = field(default=None,validator=instance_of(bool))
compartment: str = field(default=None,validator=optional(instance_of(str)))
listOfReactants: List[SpeciesReference] = field(factory=list)
listOfProducts: List[SpeciesReference] = field(factory=list)
listOfModifiers: List[ModifierSpeciesReference] = field(factory=list)
kineticLaw: KineticLaw = field(default=None, validator=optional(instance_of(KineticLaw)))
@modelspec.define
class Constraint(SBase):
"""
A model constraint
Args:
math: MathML optional
message: XHTML 1.0 optional
"""
math: str = field(default=None,validator=optional(instance_of(str)))
message: str = field(default=None,validator=optional(instance_of(str)))
@modelspec.define
class Rule(SBase):
"""
A rule, either algebraic, assignment or rate
Args:
math: MathML optional
"""
math: str = field(default=None,validator=optional(instance_of(str)))
@modelspec.define
class AlgebraicRule(Rule):
"""
An algebraic rule
"""
@modelspec.define
class AssignmentRule(Rule):
"""
An assignment rule
Args:
variable: SIdRef required
"""
variable: str = field(default=None,validator=instance_of(str))
@modelspec.define
class RateRule(Rule):
"""
A rate rule
Args:
variable: SIdRef required
"""
variable: str = field(default=None,validator=instance_of(str))
@modelspec.define
class InitialAssignment(SBase):
"""
An initial assignment
Args:
symbol: SIdRef required
math: MathML optional
"""
symbol: str = field(default=None,validator=instance_of(str))
math: str = field(default=None,validator=optional(instance_of(str)))
@modelspec.define
class Parameter(SBase):
"""
A parameter
Args:
value: double optional
units: UnitSIdRef optional
constant: boolean
"""
constant: bool = field(default=None,validator=instance_of(bool))
value: float = field(default=None,validator=optional(instance_of(float)))
units: str = field(default=None,validator=optional(instance_of(str)))
@modelspec.define
class Species(SBase):
"""
A species: entities of the same kind participating in reactions within a specific compartment
Args:
compartment: SIdRef
initialAmount: double optional
initialConcentration: double optional
substanceUnits: UnitSIdRef optional
hasOnlySubstanceUnits: boolean
boundaryCondition: boolean
constant: boolean
conversionFactor: SIdRef optional
"""
compartment: str = field(default=None,validator=instance_of(str))
hasOnlySubstanceUnits: bool = field(default=None,validator=instance_of(bool))
boundaryCondition: bool = field(default=None,validator=instance_of(bool))
constant: bool = field(default=None,validator=instance_of(bool))
initialAmount: float = field(default=None, validator=optional(instance_of(float)))
initialConcentration: float = field(default=None, validator=optional(instance_of(float)))
substanceUnits: str = field(default=None, validator=optional(instance_of(str)))
conversionFactor: str = field(default=None, validator=optional(instance_of(str)))
@modelspec.define
class Compartment(SBase):
"""
A compartment
Args:
spatialDimensions: eg 3 for three dimensional space etc
size: initial size of compartment
units: units being used to define the compartment's size
constant: whether size is fixed
"""
constant: bool = field(default=None,validator=instance_of(bool))
spatialDimensions: float = field(default=None,validator=optional(instance_of(float)))
size: float = field(default=None,validator=optional(instance_of(float)))
units: str = field(default=None,validator=optional(instance_of(str)))
@modelspec.define
class Unit(SBase):
"""
A unit used to compose a unit definition.
unit = (multiplier x 10^scale x kind)^exponent
Args:
kind: base unit (base or derived SI units only, see Table 2 of the SBML spec)
exponent: double
scale: integer
multiplier: double
"""
kind: str = field(default=None,validator=[instance_of(str),valid_kind])
exponent: str = field(default=1.0, validator=instance_of(float))
scale: str = field(default=0, validator=instance_of(int))
multiplier: str = field(default=1.0, validator=instance_of(float))
@modelspec.define
class UnitDefinition(SBase):
"""
A unit definition
Args:
sid: UnitSid required (overrides SBase sid)
listOfUnits: List of units used to compose the definition
"""
sid: str = field(default=None,validator=[instance_of(str),valid_unitsid])
listOfUnits: List[Unit] = field(factory=list)
@modelspec.define
class FunctionDefinition(SBase):
"""
A function definition using MathML
Args:
sid: SId required
math: MathML function definition optional
"""
sid: str = field(default=None,validator=[instance_of(str),valid_sid])
math: Math = field(default=None, validator=optional(instance_of(Math)))
@modelspec.define
class Model(SBase):
"""
The model
Args:
substanceUnits: UnitSIdRef optional
timeUnits: UnitSIdRef optional
volumeUnits: UnitSIdRef optional
areaUnits: UnitSIdRef optional
lengthUnits: UnitSIdRef optional
extentUnits: UnitSIdRef optional
conversionFactor: SIdRef optional
"""
substanceUnits: str = field(default=None, validator=optional([instance_of(str),valid_unitsid]))
timeUnits: str = field(default=None, validator=optional([instance_of(str),valid_unitsid]))
volumeUnits: str = field(default=None, validator=optional([instance_of(str),valid_unitsid]))
areaUnits: str = field(default=None, validator=optional([instance_of(str),valid_unitsid]))
lengthUnits: str = field(default=None, validator=optional([instance_of(str),valid_unitsid]))
extentUnits: str = field(default=None, validator=optional([instance_of(str),valid_unitsid]))
conversionFactor: str = field(default=None, validator=optional([instance_of(str),valid_unitsid]))
listOfFunctionDefinitions: List[FunctionDefinition] = field(factory=list)
listOfUnitDefinitions: List[UnitDefinition] = field(factory=list)
listOfCompartments: List[Compartment] = field(factory=list)
listOfSpecies: List[Species] = field(factory=list)
listOfParameters: List[Parameter] = field(factory=list)
listOfInitialAssignments: List[InitialAssignment] = field(factory=list)
listOfRules: List[Rule] = field(factory=list)
listOfConstraints: List[Constraint] = field(factory=list)
listOfReactions: List[Reaction] = field(factory=list)
listOfEvents: List[Event] = field(factory=list)
@modelspec.define
class SBML(SBase):
"""
The top-level SBML container implementing SBML 3.2.
See sbml.level-3.version-2.core.release-2.pdf section 4.
http://www.sbml.org/sbml/level3/version2/core
Args:
xmlns: string, fixed to "http://www.sbml.org/sbml/level3/version2/core"
level: SBML level, fixed to 3
version: SBML version, fixed to 2
model: Optional model
"""
xmlns: str = field(default="http://www.sbml.org/sbml/level3/version2/core",validator=[instance_of(str),xmlns_sbml])
level: str = field(default="3",validator=[instance_of(str),fixed_level])
version: str = field(default="2",validator=[instance_of(str),fixed_version])
model: Model = field(default=None, validator=optional(instance_of(Model)))