Skip to content

GPR rule is not recognized by the evaluation function #2

@michaelpierrelee

Description

@michaelpierrelee

Hello

with Python 3.10 and cobra 0.29, the GPR rule is not recognized by the evaluation function (unsupported operation cobra.core.gene.GPR).

The solution is to reimplement it using only the AST python module:

    def __evaluate_gpr(self, node, conf_genes):

        if isinstance(node, ast.Module) or isinstance(node, ast.Expr):
            for child in ast.iter_child_nodes(node):
                return self.__evaluate_gpr(child, conf_genes)
        elif isinstance(node, ast.BoolOp):
            if isinstance(node.op, ast.Or):
                x = [self.__evaluate_gpr(child, conf_genes) for child in node.values]
                if None not in x:
                    return max(x)
            elif isinstance(node.op, ast.And):
                x = [self.__evaluate_gpr(child, conf_genes) for child in node.values]
                if None not in x:
                    return min(x)
            else:
                raise TypeError("unsupported operation " + node.op.__class__.__name__)
        elif isinstance(node, ast.Name):
            if node.id not in conf_genes:
                return 1
            return conf_genes[node.id]
        elif node is None:
            return 1
        else:
            raise TypeError("unsupported operation  " + repr(node))

and

    def __gene_set_expression(self, rule, conf_genes):

        if rule == '':
            return 1
        else:
            ast_tree = ast.parse(rule)
            return self.__evaluate_gpr(ast_tree, conf_genes)

I believe this implementation should be supported for previous versions of cobrapy, as it is only based on the property cobra.core.reaction.Reaction.gene_reaction_rule that has been set on its early versions.

Best

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions