diff --git a/petab/v1/models/pysb_model.py b/petab/v1/models/pysb_model.py index acf8405f..de1ce95c 100644 --- a/petab/v1/models/pysb_model.py +++ b/petab/v1/models/pysb_model.py @@ -2,10 +2,12 @@ from __future__ import annotations +import ast import itertools import re import sys -from collections.abc import Iterable +from collections import Counter +from collections.abc import Iterable, Iterator from pathlib import Path from typing import Any @@ -161,35 +163,47 @@ def is_valid(self) -> bool: return True def is_state_variable(self, id_: str) -> bool: + """Check whether the given ID is a plausible PySB species name. + + 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. + This does not check whether the species is actually generated by + the model's rules, or whether all monomers of a complex are + connected. + """ # If there is a component with that name, it's not a state variable # (there are no dynamically-sized compartments) if self.model.components.get(id_, None): return False - # Try parsing the ID try: - result = parse_species_name(id_) + pattern = pattern_from_string(id_, self.model) except ValueError: return False - else: - # check if the ID is plausible - for monomer, compartment, site_config in result: - pysb_monomer: pysb.Monomer = self.model.monomers.get(monomer) - if pysb_monomer is None: + + 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: return False - if compartment: - pysb_compartment = self.model.compartments.get(compartment) - if pysb_compartment is None: - return False - for site, state in site_config.items(): - if site not in pysb_monomer.sites: - return False - if state not in pysb_monomer.site_states[site]: - return False - if set(pysb_monomer.sites) - set(site_config.keys()): - # There are undefined sites + site_value = mp.site_conditions[site] + if ( + isinstance(site_value, pysb.MultiState) + and len(site_value) != count + ): return False - return True + + # each bond has to connect exactly two sites + bond_counts = Counter( + bond + for mp in pattern.monomer_patterns + for site_value in mp.site_conditions.values() + for bond in _iter_bonds(site_value) + ) + return all(count == 2 for count in bond_counts.values()) def get_compartment_ids(self) -> Iterable[str]: return (compartment.name for compartment in self.model.compartments) @@ -200,6 +214,10 @@ def parse_species_name( ) -> list[tuple[str, str | None, dict[str, Any]]]: """Parse a PySB species name + Site values in the form of ``(state, bond)`` tuples and + :class:`pysb.MultiState` are not supported. + See :func:`pattern_from_string` for a more complete parser. + :param name: Species name to parse :returns: List of species, representing complex constituents, each as a tuple of the monomer name, the compartment name, and a dict of sites @@ -251,16 +269,96 @@ def parse_species_name( def pattern_from_string(string: str, model: pysb.Model) -> pysb.ComplexPattern: - """Convert a pattern string to a Pattern instance""" - parts = parse_species_name(string) - patterns = [] - for part in parts: - patterns.append( - pysb.MonomerPattern( - monomer=model.monomers.get(part[0]), - compartment=model.compartments.get(part[1], None), - site_conditions=part[2], - ) - ) + """Convert a pattern string to a pattern of the given model. + + The string has to be in the format of ``str(pysb.ComplexPattern)``, + e.g., ``A(b=1, s=('u', 2)) ** c % B(a=1, c=2)``. + Site values may be ``None``, bond numbers, lists of bond numbers, + state strings, ``(state, bond)`` tuples, or ``MultiState(...)``. + Wildcards such as ``ANY`` or ``WILD`` are not supported. + + :param string: The pattern string. + :param model: The model that defines the monomers and compartments. + :returns: The pattern. + :raises ValueError: If the string is not a valid pattern for this model. + """ - return pysb.ComplexPattern(patterns, compartment=None) + class InvalidPattern(ValueError): + pass + + def invalid(node: ast.AST | None = None) -> InvalidPattern: + detail = f" ('{ast.unparse(node)}')" if node is not None else "" + return InvalidPattern(f"Invalid species name: '{string}'{detail}") + + def convert_site_value(node: ast.expr): + """Convert the AST of a site value to the PySB site condition.""" + if isinstance(node, ast.Constant) and ( + node.value is None or type(node.value) in (int, str) + ): + return node.value + if isinstance(node, ast.Tuple): + return tuple(convert_site_value(e) for e in node.elts) + if isinstance(node, ast.List): + return [convert_site_value(e) for e in node.elts] + if ( + isinstance(node, ast.Call) + and isinstance(node.func, ast.Name) + and node.func.id == "MultiState" + and not node.keywords + ): + return pysb.MultiState(*map(convert_site_value, node.args)) + raise invalid(node) + + def convert(node: ast.expr): + """Convert the AST of a (complex/monomer) pattern to a PySB pattern.""" + if isinstance(node, ast.Call) and isinstance(node.func, ast.Name): + monomer = model.monomers.get(node.func.id) + if monomer is None or node.args: + raise invalid(node) + sites = [kw.arg for kw in node.keywords] + if ( + None in sites + or len(set(sites)) != len(sites) + or set(sites) - set(monomer.sites) + ): + raise invalid(node) + site_conditions = { + kw.arg: convert_site_value(kw.value) for kw in node.keywords + } + # not `monomer(**site_conditions)`, which fails for a site + # named `conditions` + return pysb.MonomerPattern(monomer, site_conditions, None) + if isinstance(node, ast.BinOp) and isinstance(node.op, ast.Mod): + return convert(node.left) % convert(node.right) + if ( + isinstance(node, ast.BinOp) + and isinstance(node.op, ast.Pow) + and isinstance(node.right, ast.Name) + and (compartment := model.compartments.get(node.right.id)) + ): + return convert(node.left) ** compartment + raise invalid(node) + + try: + tree = ast.parse(string, mode="eval") + except (SyntaxError, RecursionError, MemoryError) as e: + # RecursionError / MemoryError for too deeply nested expressions + raise invalid() from e + + try: + return pysb.as_complex_pattern(convert(tree.body)) + except InvalidPattern: + raise + except Exception as e: + # PySB raises ValueError, KeyError, or bare Exception + # for invalid patterns + raise invalid() from e + + +def _iter_bonds(site_value) -> Iterator[int]: + """Iterate over the bond numbers in a site value.""" + if isinstance(site_value, int): + yield site_value + elif isinstance(site_value, tuple | list | pysb.MultiState): + for x in site_value: + yield from _iter_bonds(x) diff --git a/tests/v1/test_model_pysb.py b/tests/v1/test_model_pysb.py index 8abb7604..48aa4481 100644 --- a/tests/v1/test_model_pysb.py +++ b/tests/v1/test_model_pysb.py @@ -1,6 +1,7 @@ """Test related to petab.models.model_pysb""" import pysb +import pytest from petab.v1.models.pysb_model import ( PySBModel, @@ -66,6 +67,87 @@ def test_pysb_model(clean_pysb_exports): assert petab_model.is_state_variable("A(s='a')") is False +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"]}) + pysb.Monomer("D", ["x", "x", "y"]) + E = pysb.Monomer("E", ["b"]) + pysb.Monomer("F", ["conditions"], {"conditions": ["u", "p"]}) + petab_model = PySBModel(model=model, model_id="test_model") + + valid = [ + "D(x=MultiState(None, 1), y=1)", + "E(b=[1, 2]) % E(b=1) % E(b=2)", + "F(conditions='u')", + "A(b=None, s='u')", + "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), + ): + pattern = pysb.as_complex_pattern(pattern) + assert petab_model.is_state_variable(str(pattern)) is True + assert str(pattern_from_string(str(pattern), model)) == str(pattern) + + invalid = [ + # dangling bond + "A(b=1, s='u')", + # bond number used three times + "A(b=1, s='u') % B(a=1) % B(a=1)", + # state for a site without states + "A(b='u', s='u')", + # unknown state + "A(b=None, s='x')", + # missing state + "A(b=None, s=1)", + # unknown site + "A(b=None, s='u', z=None)", + # duplicate site + "A(b=None, b=None, s='u')", + # missing site + "A(b=None)", + # wildcards + "A(b=ANY, s='u')", + "A(b=WILD, s='u')", + # incomplete multi-state site + "C(x=MultiState('u'))", + "C(x=MultiState('u', 'p', 'u'))", + "C(x='u')", + # missing site of a monomer with duplicate sites + "D(x=MultiState(None, None))", + # missing bond partner in a list of bonds + "E(b=[1, 2]) % E(b=1)", + # unknown compartment + "A(b=None, s='u') ** d", + # not a species pattern + "A(b=True, s='u')", + "A(**kwargs)", + "__import__('os')", + "A(", + ] + for species in invalid: + assert petab_model.is_state_variable(species) is False, species + + # errors from PySB are reported with the offending string + with pytest.raises(ValueError, match=r"Invalid species name: 'A\(b='u'"): + pattern_from_string("A(b='u', s='u')", model) + + def test_pattern_parsing(clean_pysb_exports): model = pysb.Model() c1 = pysb.Compartment("c1") diff --git a/tests/v2/test_lint.py b/tests/v2/test_lint.py index 25d2440a..5577d68f 100644 --- a/tests/v2/test_lint.py +++ b/tests/v2/test_lint.py @@ -158,3 +158,30 @@ def test_check_mapping_table(clean_pysb_exports): "`A` is used in the mapping table and referenced directly" in error.message ) + + +def test_pysb_species_with_sites_as_condition_target( + clean_pysb_exports, +): + """PySB species with binding sites can be used as condition targets.""" + pysb_model = pysb.Model("test_model", _export=False) + pysb_model.add_component( + pysb.Monomer("A", ["b", "s"], {"s": ["u", "p"]}, _export=False) + ) + pysb_model.add_component(pysb.Monomer("B", ["a"], _export=False)) + problem = Problem() + problem.model = PySBModel(model=pysb_model, model_id="test_model") + problem.add_mapping("A_u", "A(b=None, s='u')") + problem.add_mapping("AB", "A(b=None, s=('p', 1)) % B(a=1)") + problem.add_condition("c1", A_u=1, AB=2) + + assert problem.model.is_state_variable("A(b=None, s='u')") + assert problem.model.is_state_variable("A(b=None, s=('p', 1)) % B(a=1)") + + check = CheckValidParameterInConditionOrParameterTable() + assert check.run(problem) is None + assert not [ + issue + for issue in problem.validate() + if issue.level >= ValidationIssueSeverity.ERROR + ]