-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsbml_model.py
More file actions
72 lines (58 loc) · 2.22 KB
/
sbml_model.py
File metadata and controls
72 lines (58 loc) · 2.22 KB
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
import re
import libsbml
import petab.v1 as petab
from petab.v1.models.sbml_model import SbmlModel
from petab.v1.sbml import load_sbml_from_string
from PySide6.QtCore import QObject, QSignalBlocker, Signal
from ..C import DEFAULT_ANTIMONY_TEXT
from .sbml_utils import antimony_to_sbml, sbml_to_antimony
class SbmlViewerModel(QObject):
"""Model for the SBML viewer.
Attributes
----------
sbml_text: str
The SBML text.
antimony_text: str
The SBML model converted to Antimony.
"""
something_changed = Signal(bool)
def __init__(self, sbml_model: petab.models.Model, parent=None):
super().__init__(parent)
self._sbml_model_original = sbml_model
if sbml_model:
self.sbml_text = libsbml.writeSBMLToString(
self._sbml_model_original.sbml_model.getSBMLDocument()
)
self.antimony_text = sbml_to_antimony(self.sbml_text)
else:
self.antimony_text = DEFAULT_ANTIMONY_TEXT
with QSignalBlocker(self):
self.convert_antimony_to_sbml()
self.model_id = self._get_model_id()
def convert_sbml_to_antimony(self):
self.antimony_text = sbml_to_antimony(self.sbml_text)
self.model_id = self._get_model_id()
self.something_changed.emit(True)
def convert_antimony_to_sbml(self):
self.sbml_text = antimony_to_sbml(self.antimony_text)
self.model_id = self._get_model_id()
self.something_changed.emit(True)
def get_current_sbml_model(self):
"""Temporary write SBML to file and turn into petab.models.Model."""
if self.sbml_text == "":
return None
sbml_reader, sbml_document, sbml_model = load_sbml_from_string(
self.sbml_text
)
model_id = sbml_model.getIdAttribute()
return SbmlModel(
sbml_model=sbml_model,
sbml_reader=sbml_reader,
sbml_document=sbml_document,
model_id=model_id,
)
def _get_model_id(self):
"""Extract the model ID from the SBML text."""
document = libsbml.readSBMLFromString(self.sbml_text)
model = document.getModel()
return model.getIdAttribute() or "New_File"