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
3 changes: 3 additions & 0 deletions changelog.d/+pysb-species-compartments.fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Require a compartment for the complex or for every monomer when checking
species names in PySB models that define compartments
([#616](https://github.com/PEtab-dev/libpetab-python/pull/616)).
8 changes: 5 additions & 3 deletions petab/v1/models/pysb_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@ def is_state_variable(self, id_: str) -> bool:

The ID has to be a species pattern (e.g., ``A(b=1, s='u') % B(a=1)``)
with all sites of all monomers specified and all bonds paired.
Compartments are optional, even if the model has compartments.
For models with compartments, the pattern must specify either a
compartment for the whole complex or one for every monomer.
This does not check whether the species is actually generated by
the model's rules, or whether all monomers of a complex are
connected.
Expand All @@ -182,9 +183,10 @@ def is_state_variable(self, id_: str) -> bool:
except ValueError:
return False

if not pattern.is_concrete():
return False

for mp in pattern.monomer_patterns:
if not mp.is_site_concrete():
return False
# is_site_concrete() doesn't check this for duplicate sites
for site, count in Counter(mp.monomer.sites).items():
if site not in mp.site_conditions:
Expand Down
24 changes: 17 additions & 7 deletions tests/v1/test_model_pysb.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@ def test_pysb_model(clean_pysb_exports):
pysb.Monomer("B", ["s"], {"s": ["a", "b"]})
petab_model = PySBModel(model=model, model_id="test_model")

assert petab_model.is_state_variable("A()") is True
assert petab_model.is_state_variable("B(s='a')") is True
assert petab_model.is_state_variable("A()") is False
assert petab_model.is_state_variable("B(s='a')") is False
assert petab_model.is_state_variable("A() ** c1") is True
assert petab_model.is_state_variable("B(s='a') ** c1") is True

# a compartment
assert petab_model.is_state_variable("c1") is False
Expand All @@ -67,10 +69,22 @@ def test_pysb_model(clean_pysb_exports):
assert petab_model.is_state_variable("A(s='a')") is False


def test_is_state_variable_compartment_for_complex(clean_pysb_exports):
"""A complex can specify a shared compartment or one per monomer."""
model = pysb.Model()
pysb.Compartment("c")
pysb.Monomer("A", ["b"])
pysb.Monomer("B", ["a"])
petab_model = PySBModel(model=model, model_id="test_model")

assert petab_model.is_state_variable("(A(b=1) % B(a=1)) ** c")
assert petab_model.is_state_variable("A(b=1) ** c % B(a=1) ** c")
assert not petab_model.is_state_variable("A(b=1) ** c % B(a=1)")


def test_is_state_variable_sites(clean_pysb_exports):
"""Species with binding sites, bonds, and multi-state sites."""
model = pysb.Model()
pysb.Compartment("c")
A = pysb.Monomer("A", ["b", "s"], {"s": ["u", "p"]})
B = pysb.Monomer("B", ["a"])
C = pysb.Monomer("C", ["x", "x"], {"x": ["u", "p"]})
Expand All @@ -87,16 +101,12 @@ def test_is_state_variable_sites(clean_pysb_exports):
"A(b=1, s='u') % B(a=1)",
"A(b=None, s=('u', 1)) % B(a=1)",
"C(x=MultiState('u', 'p'))",
"A(b=None, s='u') ** c",
"A(b=1, s='u') ** c % B(a=1) ** c",
"(A(b=1, s='u') % B(a=1)) ** c",
]
for species in valid:
assert petab_model.is_state_variable(species) is True, species
# str(pattern) round trip
for pattern in (
A(b=1, s=("p", 2)) % B(a=1) % B(a=2),
(A(b=1, s="u") % B(a=1)) ** model.compartments["c"],
C(x=pysb.MultiState("u", "p")),
E(b=[1, 2]) % E(b=1) % E(b=2),
):
Expand Down