Skip to content

Commit 7e41492

Browse files
committed
fix unit ids and remove validation error
1 parent d1fa04e commit 7e41492

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

pyenzyme/sbml/serializer.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
from pathlib import Path
55
from typing import Callable, List
66

7-
import rich
87
import libsbml
98
import pandas as pd
109
from loguru import logger
@@ -18,8 +17,8 @@
1817
from pyenzyme.sbml.validation import validate_sbml_export
1918
from pyenzyme.sbml.versions import v2
2019
from pyenzyme.tabular import to_pandas
21-
from pyenzyme.units.units import UnitDefinition
2220
from pyenzyme.tools import to_dict_wo_json_ld
21+
from pyenzyme.units.units import UnitDefinition
2322

2423
MAPPINGS = tools.read_static_file("pyenzyme.sbml", "mappings.toml")
2524
NSMAP = {"enzymeml": "https://www.enzymeml.org/v2"}
@@ -156,7 +155,7 @@ def _add_vessel(vessel: pe.Vessel):
156155
compartment.setSize(vessel.volume)
157156
compartment.setAnnotation(rdf.to_rdf_xml(vessel))
158157

159-
if vessel.unit in units:
158+
if vessel.unit:
160159
compartment.setUnits(_get_unit_id(vessel.unit))
161160
model.setVolumeUnits(_get_unit_id(vessel.unit))
162161
else:
@@ -450,9 +449,9 @@ def _get_unit_id(unit: pe.UnitDefinition | None) -> str | None:
450449
if _same_unit(unit, unit2):
451450
return unit2.id
452451

453-
454452
raise ValueError(f"Unit {unit.name} not found in the list of units")
455453

454+
456455
def _same_unit(unit1: pe.UnitDefinition, unit2: pe.UnitDefinition) -> bool:
457456
"""Check if two units are the same."""
458457

@@ -464,6 +463,7 @@ def _same_unit(unit1: pe.UnitDefinition, unit2: pe.UnitDefinition) -> bool:
464463

465464
return unit1 == unit2
466465

466+
467467
def _validate_sbml(sbmldoc: libsbml.SBMLDocument) -> None:
468468
"""Validate the SBML document using the libSBML function."""
469469

@@ -488,19 +488,22 @@ def _validate_sbml(sbmldoc: libsbml.SBMLDocument) -> None:
488488
sbmldoc.getError(error).getMessage().strip().replace("\n", " ")
489489
)
490490

491-
if not valid:
492-
raise ValueError("SBML model is not valid")
493-
494491

495492
def _assign_ids_to_units(doc_units: List[UnitDefinition]) -> List[UnitDefinition]:
496493
ids = [unit.id for unit in doc_units if unit.id]
494+
unique_units = []
497495

498496
for unit in doc_units:
497+
if any(_same_unit(unit, u) for u in unique_units):
498+
continue
499+
499500
if unit.id is None:
500501
new_id = next(_id_generator(ids))
501502
unit.id = new_id
502503

503-
return doc_units
504+
unique_units.append(unit)
505+
506+
return unique_units
504507

505508

506509
def _id_generator(ids: list[str]):

0 commit comments

Comments
 (0)