Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
`ExperimentsToSbmlConverter` no longer creates event assignments for species
that are assignment rule targets when a compartment is resized after the
first period, which resulted in invalid SBML
([#651](https://github.com/PEtab-dev/libpetab-python/pull/651))
20 changes: 17 additions & 3 deletions petab/v2/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -784,14 +784,28 @@ def _create_event_assignments_for_period(
Therefore, to correctly implement a PEtab change of a compartment size
in SBML, we need to compensate for the automatic update of species
concentrations by adding event assignments for all contained
concentration-based species.
concentration-based species, except for assignment rule targets.

:param event: The SBML event to which the assignments should be added.
:param changes: The PEtab condition changes that are to be applied
at the start of the period.
"""
_add_assignment = ExperimentsToSbmlConverter._add_assignment
sbml_model = event.getModel()
assignment_rule_targets = {
rule.getVariable()
for rule in sbml_model.getListOfRules()
if rule.isAssignment()
}

def needs_volume_compensation(species: libsbml.Species) -> bool:
"""Whether the event must keep the species' concentration
when its compartment is resized."""
return (
not species.getHasOnlySubstanceUnits()
and species.getId() not in assignment_rule_targets
)

# collect IDs of compartments that are changed in this period
changed_compartments = {
change.target_id
Expand Down Expand Up @@ -831,7 +845,7 @@ def _create_event_assignments_for_period(
species.getId()
for species in sbml_model.getListOfSpecies()
if species.getCompartment() == change.target_id
and not species.getHasOnlySubstanceUnits()
and needs_volume_compensation(species)
]
for species_id in conc_species:
if species_change := next(
Expand All @@ -855,7 +869,7 @@ def _create_event_assignments_for_period(
elif (
target_type != libsbml.SBML_SPECIES
or sbml_target.getCompartment() not in changed_compartments
or sbml_target.getHasOnlySubstanceUnits() is True
or not needs_volume_compensation(sbml_target)
):
# Handle any changes other than compartments and
# concentration-based species inside resized compartments
Expand Down
29 changes: 29 additions & 0 deletions tests/v2/test_converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -717,3 +717,32 @@ def test_time_in_target_value():
assert {
s.name for s in ia_expr.free_symbols if not isinstance(s, TimeSymbol)
} == {"_petab_experiment_indicator_e1"}


def test_resize_compartment_skips_assignment_rule_species():
"""Resizing a compartment in an event doesn't create event assignments
for assignment rule targets."""
problem = Problem()
problem.model = SbmlModel.from_antimony("""
compartment C = 1;
species B in C = 1;
species S_rule in C;
S_rule := 2 * B;
J: B -> ; 0.1 * B;
""")
problem.add_observable("obs_B", "B", noise_formula="1")
problem.add_condition("c_resize", C=2)
problem.add_experiment("e1", 0, "", 5, "c_resize")
problem.add_measurement(
"obs_B", experiment_id="e1", time=10, measurement=1
)

converted = ExperimentsToSbmlConverter(problem).convert()
sbml_model = converted.model.sbml_model

ev = sbml_model.getEvent("_petab_event_e1_1")
assert {ea.getVariable() for ea in ev.getListOfEventAssignments()} == {
"C",
"B",
}
assert converted.model.is_valid()
Loading