-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
142 lines (124 loc) · 5.57 KB
/
Copy pathmodel.py
File metadata and controls
142 lines (124 loc) · 5.57 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
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
"""Represents the static (specification-derived) properties of a model."""
import src.constants as cn # type: ignore
import os
import tellurium as te # type: ignore
from typing import List
class Model(object):
"""Static properties of a model derived from its specification, not its execution.
Accepts an SBML or Antimony string. Antimony is converted to SBML on
construction. RoadRunner is used transiently and not stored.
"""
def __init__(self, model_str: str, model_name: str = "") -> None:
"""
Parameters
----------
model_str : str
SBML XML string or Antimony model string.
model_name : str
Optional identifier for the model (e.g. 'BIOMD0000000001').
"""
if not isinstance(model_str, str):
raise ValueError("model_str must be a string.")
self.model_name = model_name
self.sbml_str = self._toSBML(model_str)
if self.sbml_str == "":
raise ValueError("this is not a model.")
self._species_names: List[str] = []
#
rr = te.loadSBMLModel(self.sbml_str)
self.species_names = rr.getFloatingSpeciesIds() + rr.getBoundarySpeciesIds()
self.initial_value_dct = {n: float(rr.model[f"init({n})"])
for n in self.species_names}
self.num_reaction = rr.getNumReactions()
self.num_species = len(self.species_names)
self.num_assignment_rule = len(rr.getAssignmentRuleIds())
def __eq__(self, other: object) -> bool:
if not isinstance(other, Model):
return NotImplemented
return self.sbml_str == other.sbml_str and self.model_name == other.model_name
def getModifableSpecies(self) -> List[str]:
"""
Finds the species whose initial values can be modified.
"""
rr = te.loadSBMLModel(self.sbml_str)
modifable_species_names = []
for species_name in self.species_names:
try:
rr.model[f"init({species_name})"] = rr.model[f"init({species_name})"]
modifable_species_names.append(species_name)
except Exception as e:
pass
#
return modifable_species_names
def checkModifableSpecies(self) -> None:
"""Check that the model has modifiable species."""
modifable_species_names = self.getModifableSpecies()
if (len(self.species_names) == 0) or len(modifable_species_names) != len(self.species_names):
raise ValueError(f"model {self.model_name} has non-modifiable species: "
f"{set(self.species_names) - set(modifable_species_names)}")
@staticmethod
def getBiomodelNumberFromName(model_name: str) -> int:
"""Return the BioModels number if the model name is a BioModels identifier."""
if model_name.startswith("BIOMD"):
return int(model_name[5:])
else:
raise ValueError(f"Model name '{model_name}' is not a BioModels identifier.")
def getBiomodelNumber(self) -> int:
"""Return the BioModels number if the model name is a BioModels identifier."""
return self.getBiomodelNumberFromName(self.model_name)
@staticmethod
def getBiomodelName(model_num: int) -> str:
return f"BIOMD{model_num:010d}"
# ------------------------------------------------------------------
# Class methods
# ------------------------------------------------------------------
@classmethod
def makeBiomodel(cls, model_name: str="", model_num: int = -1) -> "Model":
"""
Create a Model from a BioModels SBML file in cn.BIOMODELS_DIR.
Parameters
----------
model_name : str
BioModel identifier (e.g. 'BIOMD0000000001'). Must start with 'BIOMD'.
model_num : int, optional
The index of the model to load (default is -1, which loads the first one).
If the model_num is present, model_name is ignored and constructed as 'BIOMD{model_num:010d}'.
Returns
-------
Model
"""
if model_num > 0:
model_name = f"BIOMD{model_num:010d}"
if not model_name.startswith("BIOMD"):
raise ValueError(f"model_name must start with 'BIOMD', got '{model_name}'.")
model_dir = os.path.join(cn.BIOMODELS_DIR, model_name)
if not os.path.isdir(model_dir):
raise FileNotFoundError(f"Model directory not found: {model_dir}")
xml_files = [
f for f in os.listdir(model_dir)
if f.endswith(".xml") and f != "manifest.xml"
]
if not xml_files:
raise FileNotFoundError(f"No SBML file found in {model_dir}")
path = os.path.join(model_dir, sorted(xml_files)[0])
with open(path) as fh:
sbml_str = fh.read()
return cls(sbml_str, model_name=model_name)
# ------------------------------------------------------------------
# Private helpers
# ------------------------------------------------------------------
def _toSBML(self, model_str: str) -> str:
"""Return an SBML string, converting from Antimony if necessary."""
stripped = model_str.strip()
try:
if "<?xml" in stripped or "<sbml" in stripped:
te.loadSBMLModel(model_str) # validate
return model_str
try:
rr = te.loada(model_str)
return rr.getSBML()
except Exception as e:
raise ValueError(f"Could not load model: {e}")
except Exception as e:
print(f"{self.model_name} failed to load: {e}")
return ""