diff --git a/changelog.d/+experiments-to-sbml-assignment-rule-species.fix.md b/changelog.d/+experiments-to-sbml-assignment-rule-species.fix.md new file mode 100644 index 00000000..145fb14a --- /dev/null +++ b/changelog.d/+experiments-to-sbml-assignment-rule-species.fix.md @@ -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)) diff --git a/petab/v2/converters.py b/petab/v2/converters.py index 5fd578ee..f4ac0847 100644 --- a/petab/v2/converters.py +++ b/petab/v2/converters.py @@ -784,7 +784,7 @@ 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 @@ -792,6 +792,20 @@ def _create_event_assignments_for_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 @@ -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( @@ -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 diff --git a/tests/v2/test_converters.py b/tests/v2/test_converters.py index 48339620..025a4669 100644 --- a/tests/v2/test_converters.py +++ b/tests/v2/test_converters.py @@ -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()