4
4
from pathlib import Path
5
5
from typing import Callable , List
6
6
7
- import rich
8
7
import libsbml
9
8
import pandas as pd
10
9
from loguru import logger
18
17
from pyenzyme .sbml .validation import validate_sbml_export
19
18
from pyenzyme .sbml .versions import v2
20
19
from pyenzyme .tabular import to_pandas
21
- from pyenzyme .units .units import UnitDefinition
22
20
from pyenzyme .tools import to_dict_wo_json_ld
21
+ from pyenzyme .units .units import UnitDefinition
23
22
24
23
MAPPINGS = tools .read_static_file ("pyenzyme.sbml" , "mappings.toml" )
25
24
NSMAP = {"enzymeml" : "https://www.enzymeml.org/v2" }
@@ -156,7 +155,7 @@ def _add_vessel(vessel: pe.Vessel):
156
155
compartment .setSize (vessel .volume )
157
156
compartment .setAnnotation (rdf .to_rdf_xml (vessel ))
158
157
159
- if vessel .unit in units :
158
+ if vessel .unit :
160
159
compartment .setUnits (_get_unit_id (vessel .unit ))
161
160
model .setVolumeUnits (_get_unit_id (vessel .unit ))
162
161
else :
@@ -450,9 +449,9 @@ def _get_unit_id(unit: pe.UnitDefinition | None) -> str | None:
450
449
if _same_unit (unit , unit2 ):
451
450
return unit2 .id
452
451
453
-
454
452
raise ValueError (f"Unit { unit .name } not found in the list of units" )
455
453
454
+
456
455
def _same_unit (unit1 : pe .UnitDefinition , unit2 : pe .UnitDefinition ) -> bool :
457
456
"""Check if two units are the same."""
458
457
@@ -464,6 +463,7 @@ def _same_unit(unit1: pe.UnitDefinition, unit2: pe.UnitDefinition) -> bool:
464
463
465
464
return unit1 == unit2
466
465
466
+
467
467
def _validate_sbml (sbmldoc : libsbml .SBMLDocument ) -> None :
468
468
"""Validate the SBML document using the libSBML function."""
469
469
@@ -488,19 +488,22 @@ def _validate_sbml(sbmldoc: libsbml.SBMLDocument) -> None:
488
488
sbmldoc .getError (error ).getMessage ().strip ().replace ("\n " , " " )
489
489
)
490
490
491
- if not valid :
492
- raise ValueError ("SBML model is not valid" )
493
-
494
491
495
492
def _assign_ids_to_units (doc_units : List [UnitDefinition ]) -> List [UnitDefinition ]:
496
493
ids = [unit .id for unit in doc_units if unit .id ]
494
+ unique_units = []
497
495
498
496
for unit in doc_units :
497
+ if any (_same_unit (unit , u ) for u in unique_units ):
498
+ continue
499
+
499
500
if unit .id is None :
500
501
new_id = next (_id_generator (ids ))
501
502
unit .id = new_id
502
503
503
- return doc_units
504
+ unique_units .append (unit )
505
+
506
+ return unique_units
504
507
505
508
506
509
def _id_generator (ids : list [str ]):
0 commit comments