Skip to content

Commit 1064af3

Browse files
committed
Generates same example sbml; restructured handling of listOf... classes
Can certainly be made more succinct, but generating valid SBML
1 parent 5b2a2f7 commit 1064af3

File tree

7 files changed

+88
-19
lines changed

7 files changed

+88
-19
lines changed

examples/sbml/example_sbml_minimal.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<sbml xmlns="http://www.sbml.org/sbml/level3/version2/core" level="3" version="2">
3+
4+
<!-- An example of a simple SBML file known to be valid -->
5+
36
<model substanceUnits="mole" timeUnits="second" extentUnits="mole">
47
<listOfUnitDefinitions>
58
<unitDefinition id="per_second">

examples/sbml/sbml32spec.py

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ class Compartment(SBase):
333333

334334

335335
@modelspec.define
336-
class Unit(SBase):
336+
class unit(SBase):
337337
"""
338338
A unit used to compose a unit definition.
339339
unit = (multiplier x 10^scale x kind)^exponent
@@ -352,17 +352,41 @@ class Unit(SBase):
352352

353353

354354
@modelspec.define
355-
class unitDefinition(SBase):
355+
class ListOfUnits(SBase):
356+
"""
357+
A listOfUnits
358+
359+
Args:
360+
listOfUnits: the actual list Of Units
361+
"""
362+
363+
listOfUnits: List[unit] = field(factory=list)
364+
365+
366+
@modelspec.define
367+
class unitDefinition(SBaseWithId):
356368
"""
357369
A unit definition
358370
359371
Args:
360-
sid: UnitSid required (overrides SBase sid)
361372
listOfUnits: List of units used to compose the definition
362373
"""
363374

364-
sid: str = field(default=None, validator=[instance_of(str), valid_unitsid])
365-
listOfUnits: List[Unit] = field(factory=list)
375+
listOfUnits: ListOfUnits = field(
376+
default=None, validator=optional(instance_of(ListOfUnits))
377+
)
378+
379+
380+
@modelspec.define
381+
class ListOfUnitDefinitions(SBase):
382+
"""
383+
A listOfUnitDefinitions
384+
385+
Args:
386+
listOfUnitDefinitions: the actual list Of Unit Definitions
387+
"""
388+
389+
listOfUnitDefinitions: List[unitDefinition] = field(factory=list)
366390

367391

368392
@modelspec.define
@@ -419,7 +443,11 @@ class Model(SBase):
419443
)
420444

421445
listOfFunctionDefinitions: List[FunctionDefinition] = field(factory=list)
422-
listOfUnitDefinitions: List[unitDefinition] = field(factory=list)
446+
447+
listOfUnitDefinitions: ListOfUnitDefinitions = field(
448+
default=None, validator=optional(instance_of(ListOfUnitDefinitions))
449+
)
450+
423451
listOfCompartments: List[Compartment] = field(factory=list)
424452
listOfSpecies: List[Species] = field(factory=list)
425453
listOfParameters: List[Parameter] = field(factory=list)
@@ -435,7 +463,7 @@ class sbml(SBaseWithId):
435463
"""
436464
The top-level SBML container implementing SBML 3.2.
437465
See sbml.level-3.version-2.core.release-2.pdf section 4.
438-
http://www.sbml.org/sbml/level3/version2/core
466+
http://www.sbml.org/sbml/level3/version2/
439467
440468
Args:
441469
xmlns: string, fixed to "http://www.sbml.org/sbml/level3/version2/core"

examples/sbml/sbml_validators.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,11 @@ def fixed_version(instance, attribute, value):
5757

5858

5959
def valid_sid(instance, attribute, value):
60-
if not re.fullmatch("[_A-Za-z][_A-Za-z0-9]*", value):
61-
raise ValueError(
62-
"an SId must match the regular expression: [_A-Za-z][_A-Za-z0-9]*"
63-
)
60+
if value is not None:
61+
if not re.fullmatch("[_A-Za-z][_A-Za-z0-9]*", value):
62+
raise ValueError(
63+
"an SId must match the regular expression: [_A-Za-z][_A-Za-z0-9]*"
64+
)
6465

6566

6667
def valid_unitsid(instance, attribute, value):
@@ -163,6 +164,8 @@ def validate_sbml(doc, units_consistency: bool = False) -> None:
163164
)
164165
print("-" * 80)
165166

167+
return len(warnings) + len(errors)
168+
166169

167170
if __name__ == "__main__":
168171

@@ -174,4 +177,6 @@ def validate_sbml(doc, units_consistency: bool = False) -> None:
174177
reader = libsbml.SBMLReader()
175178
document = reader.readSBML(sbml_file)
176179

177-
validate_sbml(document, units_consistency=False)
180+
issues = validate_sbml(document, units_consistency=False)
181+
182+
exit(issues)

examples/sbml/test_minimal_example.json

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,21 @@
55
"model": {
66
"substanceUnits": "mole",
77
"timeUnits": "second",
8-
"extentUnits": "mole"
8+
"extentUnits": "mole",
9+
"listOfUnitDefinitions": {
10+
"listOfUnitDefinitions": {
11+
"per_second": {
12+
"listOfUnits": {
13+
"listOfUnits": [
14+
{
15+
"kind": "second",
16+
"exponent": -1.0
17+
}
18+
]
19+
}
20+
}
21+
}
22+
}
923
}
1024
}
1125
}
Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<sbml xmlns="http://www.sbml.org/sbml/level3/version2/core" id="test_minimal_example" level="3" version="2">
3-
<model substanceUnits="mole" timeUnits="second" extentUnits="mole"/>
3+
<model substanceUnits="mole" timeUnits="second" extentUnits="mole">
4+
<listOfUnitDefinitions>
5+
<unitDefinition id="per_second">
6+
<listOfUnits>
7+
<unit kind="second" exponent="-1.0" scale="0" multiplier="1.0"/>
8+
</listOfUnits>
9+
</unitDefinition>
10+
</listOfUnitDefinitions>
11+
</model>
412
</sbml>

examples/sbml/test_minimal_example.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,10 @@ test_minimal_example:
55
substanceUnits: mole
66
timeUnits: second
77
extentUnits: mole
8+
listOfUnitDefinitions:
9+
listOfUnitDefinitions:
10+
per_second:
11+
listOfUnits:
12+
listOfUnits:
13+
- kind: second
14+
exponent: -1.0

examples/sbml/test_sbml3.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,15 @@ def test_example_sbml_minimal():
2323
model = Model(substanceUnits="mole", timeUnits="second", extentUnits="mole")
2424
sbml_doc.model = model
2525

26-
unit_def = unitDefinition(sid="per_second")
27-
"""
28-
model.listOfUnitDefinitions.append(unit_def)
29-
unit = Unit(kind="second", exponent=-1.0)
30-
unit_def.listOfUnits.append(unit)"""
26+
unit_def = unitDefinition(id="per_second")
27+
28+
model.listOfUnitDefinitions = ListOfUnitDefinitions()
29+
30+
model.listOfUnitDefinitions.listOfUnitDefinitions.append(unit_def)
31+
32+
unit_s = unit(kind="second", exponent=-1.0)
33+
unit_def.listOfUnits = ListOfUnits()
34+
unit_def.listOfUnits.listOfUnits.append(unit_s)
3135

3236
print("------------------------------------------------")
3337
print(sbml_doc)

0 commit comments

Comments
 (0)