-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathantimony.py
40 lines (29 loc) · 1.01 KB
/
antimony.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
"""Antimony -> SBML"""
import antimony as ant
from pathlib import Path
__all__ = ["antimony_to_sbml_str"]
def antimony_to_sbml_str(ant_model: str | Path) -> str:
"""Convert Antimony string to SBML model.
Arguments:
ant_str:
Antimony model as string (model, not filename), or Path to file.
Returns:
SBML model as string.
"""
# Unload everything / free memory
ant.clearPreviousLoads()
ant.freeAll()
if isinstance(ant_model, Path):
status = ant.loadAntimonyFile(str(ant_model))
else:
status = ant.loadAntimonyString(ant_model)
if status < 0:
raise RuntimeError(
f"Antimony model could not be loaded: {ant.getLastError()}"
)
if (main_module_name := ant.getMainModuleName()) is None:
raise AssertionError("There is no Antimony module.")
sbml_str = ant.getSBMLString(main_module_name)
if not sbml_str:
raise ValueError("Antimony model could not be converted to SBML.")
return sbml_str