|
a |
|
b/aiagents4pharma/talk2biomodels/tools/load_biomodel.py |
|
|
1 |
#!/usr/bin/env python3 |
|
|
2 |
|
|
|
3 |
""" |
|
|
4 |
Function for loading the BioModel. |
|
|
5 |
""" |
|
|
6 |
|
|
|
7 |
from typing import Annotated, Any, Union |
|
|
8 |
from pydantic import BaseModel, BeforeValidator |
|
|
9 |
from ..models.basico_model import BasicoModel |
|
|
10 |
|
|
|
11 |
def ensure_biomodel_id(value: Any) -> Any: |
|
|
12 |
""" |
|
|
13 |
Ensure that the biomodel_id is an integer or a string starting with 'BIOMD' or 'MODEL'. |
|
|
14 |
""" |
|
|
15 |
if isinstance(value, int): |
|
|
16 |
return value |
|
|
17 |
if isinstance(value, str) and (value.startswith("BIOMD") or value.startswith("MODEL")): |
|
|
18 |
return value |
|
|
19 |
raise ValueError("biomodel_id must be an integer or a string starting with 'BIOMD' or 'MODEL'.") |
|
|
20 |
|
|
|
21 |
class ModelData(BaseModel): |
|
|
22 |
""" |
|
|
23 |
Base model for the model data. |
|
|
24 |
""" |
|
|
25 |
biomodel_id: Annotated[Union[int, str], BeforeValidator(ensure_biomodel_id)] = None |
|
|
26 |
# sbml_file_path: Optional[str] = None |
|
|
27 |
use_uploaded_sbml_file: bool = False |
|
|
28 |
|
|
|
29 |
def load_biomodel(sys_bio_model, sbml_file_path=None): |
|
|
30 |
""" |
|
|
31 |
Load the BioModel. |
|
|
32 |
""" |
|
|
33 |
model_object = None |
|
|
34 |
if sys_bio_model.biomodel_id: |
|
|
35 |
model_object = BasicoModel(biomodel_id=sys_bio_model.biomodel_id) |
|
|
36 |
elif sbml_file_path: |
|
|
37 |
model_object = BasicoModel(sbml_file_path=sbml_file_path) |
|
|
38 |
return model_object |