Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError in expression_profile.py #19

Open
jotech opened this issue Mar 21, 2019 · 4 comments
Open

TypeError in expression_profile.py #19

jotech opened this issue Mar 21, 2019 · 4 comments

Comments

@jotech
Copy link

jotech commented Mar 21, 2019

I encountered an error when calling the imat function:

imat(recon, exprof, (0, 30), condition="cond1")
[...]
TypeError: Call constructor takes at most 3 positional arguments

I can track it down to a sympy call by parse_expr:

./driven/data_sets/expression_profile.py in _map_gene_to_rxn(self, reaction, gene_values, by)
    333         rule = reaction.gene_reaction_rule.replace("and", "*").replace("or",
    334                                                                        "+")
--> 335         expression = parse_expr(rule, local_dict)
    336         if by == "or2max_and2min":
    337             expression = expression.replace(Mul, Min).replace(Add, Max)

A minimal example to reproduce the error would be:

from sympy import Symbol
from sympy.parsing.ast_parser import parse_expr
local_dict = {'5161.0': 5161.0,
 '1738.0': 1738.0,
 '5160.0': 5160.0,
 '1737.0': 1737.0,
 '5162.0': 5162.0,
 '8050.0': 8050.0}
rule = '(1738.0 * 8050.0) * (5161.0 * 5162.0) * (1737.0) + (1738.0 * 8050.0) * (5160.0 * 5162.0) * (1737.0)'
expression = parse_expr(rule, local_dict)
[...]
TypeError: Call constructor takes at most 3 positional arguments

Do you have any clue what's going on here?

extended log file
python 3.7.2+
sympy 1.3

@arabidopsis
Copy link

Yes. sympy is using a 5 argument version of ast.Call which was
removed in Py3.5. see https://greentreesnakes.readthedocs.io/en/latest/nodes.html#Call

@arabidopsis
Copy link

Here's a monkey patch (might create a pull request if I have time)

from cobra.core.gene import parse_gpr
from sympy import Mul, Add, Symbol, Max, Min
from ast import And, Or, NodeTransformer

class Transform(NodeTransformer):

    def visit_Name(self, node):
        return Symbol(node.id)
    def visit_BoolOp(self, node):
        values = [self.visit(v) for v in node.values]
        if isinstance(node.op, And):
            return Mul(*values)
        if isinstance(node.op, Or):
            return Add(*values)
        return node

def parse_expr(s):
    a, genes = parse_gpr(s.strip())
    a = Transform().visit(a)
    return a.body

def _map_gene_to_rxn(self, reaction, gene_values, by):
    rule = reaction.gene_reaction_rule
    expression = parse_expr(rule)
    if by == "or2max_and2min":
        expression = expression.replace(Mul, Min).replace(Add, Max)
    elif by == "or2sum_and2min":
        expression = expression.replace(Mul, Min)
    return expression.subs(gene_values).evalf()

from driven.data_sets.expression_profile import ExpressionProfile
ExpressionProfile._map_gene_to_rxn = _map_gene_to_rxn

@arabidopsis
Copy link

Ok... I created a pull request. Let's see what happens :)

@jotech
Copy link
Author

jotech commented Mar 29, 2019

thank you very much it helped indeed :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants