-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_sbml32.py
executable file
·163 lines (122 loc) · 5.04 KB
/
test_sbml32.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
#!/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 json
import yaml
import os
from sbml32spec import *
def test_example_sbml_minimal():
'aiming to eventually match the xml file example_sbml_minimal.xml'
path = "test_minimal_example"
sbml_doc = SBML()
#open(f"{path}.docs.json", "w").write(json.dumps(sbml_doc.generate_documentation(format="dict"), indent=4))
model = Model(substanceUnits="mole",timeUnits="second",extentUnits="mole")
sbml_doc.model = model
unit_def = UnitDefinition(sid="per_second")
model.listOfUnitDefinitions.append(unit_def)
unit = Unit(kind="second",exponent=-1.0)
unit_def.listOfUnits.append(unit)
sbml_doc.to_json_file(f"{path}.json")
sbml_doc.to_xml_file(f"{path}.xml")
def test_minimal_example():
path = "untracked/test_minimal_example"
sbml_doc = SBML()
open(f"{path}.docs.json", "w").write(json.dumps(sbml_doc.generate_documentation(format="dict"), indent=4))
model = Model()
sbml_doc.model = model
unit_def = UnitDefinition(sid="unit_def")
model.listOfUnitDefinitions.append(unit_def)
unit1 = Unit(kind="ampere")
unit2 = Unit(kind="watt")
unit_def.listOfUnits.append(unit1)
unit_def.listOfUnits.append(unit2)
sbml_doc.to_json_file(f"{path}.json")
def test_sbml_empty():
'empty sbml with only defaults'
sbml_doc = SBML()
assert sbml_doc.sid == None
assert sbml_doc.name == None
assert sbml_doc.metaid == None
assert sbml_doc.sboTerm == None
assert sbml_doc.notes == None
assert sbml_doc.annotation == None
assert sbml_doc.xmlns == "http://www.sbml.org/sbml/level3/version2/core"
assert sbml_doc.level == "3"
assert sbml_doc.version == "2"
assert sbml_doc.model == None
def test_sbml_sid():
'test validation of SBML.sid values'
#valid values should be accepted
for value in ["abc_123","A1B2C3_","_A_BC321"]:
SBML(sid=value)
#invalid values should throw a ValueError
for value in ["123","A.1","z:"]:
try:
SBML(sid=value)
raise Exception(f'invalid value was accepted: {value}')
except ValueError:
pass
def test_complete_example():
'''
aiming to have at least one of every item type
not fully implemented yet
'''
#a minimal exmaple xhtml document
content='''
<html xmlns="http://www.w3.org/TR/xhtml1/strict">
<head>
<title>A Small Document</title>
</head>
<body>
<p>A simple page!</p>
</body>
</html>'''
notes = Notes(content=content)
annotation = "<thing>minimum xml</thing>"
sbml_doc = SBML(sid="sbml1",name="my name is sbml doc",metaid="zuckerberg",sboTerm="SBO:1234567",notes=notes,annotation=annotation)
model = Model(sid="model1")
sbml_doc.model = model
#function definition list
content = '<math> <lambda> <bvar><ci> x </ci></bvar> <apply> <power/> <ci> x </ci> <cn sbml:units="dimensionless"> 3 </cn> </apply> </lambda> </math>'
math = Math(content=content)
funcDef = FunctionDefinition(sid="funcdef1",math=math)
model.listOfFunctionDefinitions.append(funcDef)
#unit definition list
unitDef = UnitDefinition(sid="metres_per_second")
unit1 = Unit(kind="metre")
unit2 = Unit(kind="second",exponent=-1.0)
unitDef.listOfUnits.append(unit1)
unitDef.listOfUnits.append(unit2)
model.listOfUnitDefinitions.append(unitDef)
#compartment list
compartment = Compartment(sid="compartment1",spatialDimensions=3.0,size=2.798,constant=True)
model.listOfCompartments.append(compartment)
#species list
species = Species(sid="species1",compartment="compartment1",hasOnlySubstanceUnits=False,boundaryCondition=False,constant=False)
model.listOfSpecies.append(species)
#parameter list
parameter = Parameter(sid="parameter1",value=1.2345,units="meters_per_second",constant=False)
model.listOfParameters.append(parameter)
#initial assignment
initialAssignment = InitialAssignment(sid="initialAssignment1",symbol="parameter1")
model.listOfInitialAssignments.append(initialAssignment)
path = "untracked/complete_example"
os.makedirs("untracked", exist_ok=True)
sbml_doc.to_json_file(f"{path}.json")
sbml_doc.to_yaml_file(f"{path}.yaml")
sbml_doc.to_bson_file(f"{path}.bson")
#sbml_doc.to_xml_file(f"{path}.xml")
open(f"{path}.docs.md", "w").write(sbml_doc.generate_documentation(format="markdown"))
open(f"{path}.docs.rst", "w").write(sbml_doc.generate_documentation(format="rst"))
doc_dict = sbml_doc.generate_documentation(format="dict")
open(f"{path}.docs.json", "w").write(json.dumps(doc_dict, indent=4))
open(f"{path}.docs.yaml", "w").write(yaml.dump(doc_dict, indent=4, sort_keys=False))
if __name__ == "__main__":
test_example_sbml_minimal()
#test_minimal_example()
#test_sbml_empty()
#test_sbml_sid()
#test_complete_example()