|
| 1 | +"""GPR (gene-protein-reaction rule) linting. |
| 2 | +
|
| 3 | +Flag GPRs that are *not* in disjunctive normal form ("OR of AND-complexes"), via cobra's |
| 4 | +GPR AST. GPR syntax *normalisation* is already done by cobra on assignment, so it isn't |
| 5 | +re-implemented here. |
| 6 | +
|
| 7 | +Part (2) has no cobrapy equivalent and is ported here, reworked onto cobra's |
| 8 | +GPR AST instead of RAVEN's brittle substring search. The relevant property is |
| 9 | +**disjunctive normal form (DNF)**: an OR of AND-clauses of single genes, e.g. |
| 10 | +``(G1 and G2) or G3``. Rules where an AND contains an OR — e.g. |
| 11 | +``(G1 or G2) and (G3 or G4)`` — are *valid* for cobra but ambiguous for the |
| 12 | +isoenzyme/complex reasoning used across RAVEN/GECKO, and ``expand_model`` |
| 13 | +(see :mod:`raven_python.manipulation.expand`) only does something for DNF rules. |
| 14 | +:func:`find_non_dnf_grrules` surfaces them as structured data rather than, as |
| 15 | +RAVEN did, only printing a warning. |
| 16 | +""" |
| 17 | +from __future__ import annotations |
| 18 | + |
| 19 | +import ast |
| 20 | +from dataclasses import dataclass |
| 21 | + |
| 22 | +import cobra |
| 23 | +from cobra.core.gene import GPR |
| 24 | + |
| 25 | + |
| 26 | +def _contains_or(node: ast.AST | None) -> bool: |
| 27 | + """True if ``node``'s subtree contains an OR operator anywhere.""" |
| 28 | + if isinstance(node, ast.BoolOp): |
| 29 | + if isinstance(node.op, ast.Or): |
| 30 | + return True |
| 31 | + return any(_contains_or(value) for value in node.values) |
| 32 | + return False |
| 33 | + |
| 34 | + |
| 35 | +def _is_dnf_node(node: ast.AST | None) -> bool: |
| 36 | + """True if the AST rooted at ``node`` is in disjunctive normal form. |
| 37 | +
|
| 38 | + DNF here means no AND operator has an OR anywhere beneath it, i.e. the |
| 39 | + rule is a single gene, a pure AND-complex, or an OR of those. |
| 40 | + """ |
| 41 | + if node is None or isinstance(node, ast.Name): |
| 42 | + return True |
| 43 | + if isinstance(node, ast.BoolOp): |
| 44 | + if isinstance(node.op, ast.And): |
| 45 | + return not any(_contains_or(value) for value in node.values) |
| 46 | + # OR: every disjunct must itself be DNF |
| 47 | + return all(_is_dnf_node(value) for value in node.values) |
| 48 | + # Unknown node type: don't flag it as a problem. |
| 49 | + return True |
| 50 | + |
| 51 | + |
| 52 | +def is_dnf(gpr: GPR | str | None) -> bool: |
| 53 | + """Return whether a GPR is in disjunctive normal form (OR of AND-complexes). |
| 54 | +
|
| 55 | + Parameters |
| 56 | + ---------- |
| 57 | + gpr |
| 58 | + A cobra :class:`~cobra.core.gene.GPR`, a grRule string, or ``None``. |
| 59 | + An empty/``None`` rule is trivially DNF. |
| 60 | +
|
| 61 | + Examples |
| 62 | + -------- |
| 63 | + >>> is_dnf("(G1 and G2) or G3") |
| 64 | + True |
| 65 | + >>> is_dnf("(G1 or G2) and G3") |
| 66 | + False |
| 67 | + """ |
| 68 | + if isinstance(gpr, str): |
| 69 | + gpr = GPR.from_string(gpr) |
| 70 | + if gpr is None: |
| 71 | + return True |
| 72 | + return _is_dnf_node(gpr.body) |
| 73 | + |
| 74 | + |
| 75 | +@dataclass(frozen=True) |
| 76 | +class GPRIssue: |
| 77 | + """A reaction whose GPR is flagged by the linter. |
| 78 | +
|
| 79 | + Attributes |
| 80 | + ---------- |
| 81 | + reaction_id |
| 82 | + ID of the reaction. |
| 83 | + gpr |
| 84 | + The (already cobra-normalised) grRule string. |
| 85 | + reason |
| 86 | + Human-readable explanation of why it was flagged. |
| 87 | + """ |
| 88 | + |
| 89 | + reaction_id: str |
| 90 | + gpr: str |
| 91 | + reason: str |
| 92 | + |
| 93 | + |
| 94 | +_NON_DNF_REASON = ( |
| 95 | + "GPR is not in disjunctive normal form (an AND clause contains an OR). " |
| 96 | + "Isoenzyme/complex reasoning and expand_model assume an OR of AND-complexes, " |
| 97 | + 'e.g. rewrite "(G1 or G2) and (G3 or G4)" as ' |
| 98 | + '"(G1 and G3) or (G1 and G4) or (G2 and G3) or (G2 and G4)".' |
| 99 | +) |
| 100 | + |
| 101 | + |
| 102 | +def find_non_dnf_grrules(model: cobra.Model) -> list[GPRIssue]: |
| 103 | + """Find reactions whose GPR is not in disjunctive normal form ("OR of AND-complexes"). |
| 104 | +
|
| 105 | + Uses cobra's GPR AST. Reactions with no GPR are skipped. |
| 106 | +
|
| 107 | + Returns |
| 108 | + ------- |
| 109 | + list of GPRIssue |
| 110 | + One entry per flagged reaction, in model reaction order. Empty if all |
| 111 | + GPRs are simple OR-of-AND-complexes. |
| 112 | + """ |
| 113 | + issues: list[GPRIssue] = [] |
| 114 | + for rxn in model.reactions: |
| 115 | + if not rxn.gene_reaction_rule: |
| 116 | + continue |
| 117 | + if not is_dnf(rxn.gpr): |
| 118 | + issues.append(GPRIssue(rxn.id, rxn.gene_reaction_rule, _NON_DNF_REASON)) |
| 119 | + return issues |
0 commit comments