-
Notifications
You must be signed in to change notification settings - Fork 12
/
gpr_eval.py
111 lines (95 loc) · 3.88 KB
/
gpr_eval.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# Copyright (C) 2019- Centre of Biological Engineering,
# University of Minho, Portugal
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
Author: Vitor Pereira
Simple examples/tests for GPR evaluation
"""
from mewpy.util.parsing import build_tree, BooleanEvaluator, Boolean, GeneEvaluator, Arithmetic, ArithmeticEvaluator
def test_0():
"""
Example on how to evaluate aritmetic expressions
"""
t = build_tree(" 1 + 2 + 3 + ( 2 * 2 )", Arithmetic)
res = t.evaluate(ArithmeticEvaluator.f_operand, ArithmeticEvaluator.f_operator)
print(t, ' = ', res)
def test_1():
"""
Example on how to evaluate Boolean expressions containing conditions
"""
# boolean example with conditions
expression = "( (Lrp AND NOT (leu_L_e_>0)) OR NOT(((GlnG AND GlnB AND GlnD) AND RpoN) AND ((glu_L_e_>0) OR \
(arg_L_e_>0) OR (asp_L_e_>0) OR (his_L_e_>0) OR (pro_L_e_>0) )))"
# expression = " (x > 0 or C or B) and not ph == 5 "
t = build_tree(expression, Boolean)
print(expression, ' <<==>> ', t)
t.print_node()
# list of true expressions
true_list = ['GlnG']
# dict of variables values
v = {'leu_L_e_': 1, 'glu_L_e_': 7, "arg_L_e_": 0.5, "asp_L_e_": 2, "his_L_e_": 0, "pro_L_e_": 0}
# propositions in the list are evaluated as True and the remaining are False
evaluator = BooleanEvaluator(true_list, v)
res = t.evaluate(evaluator.f_operand, evaluator.f_operator)
print('evaluation: ', res)
print('True list:', true_list)
print('variables:', v)
print('operands: ', t.get_operands())
print('conditions: ', t.get_conditions())
def test_2():
"""Example of evaluation of RECON1 GPRs
"""
from urllib.request import urlretrieve
from cobra.io import read_sbml_model
import random
path, _ = urlretrieve('http://bigg.ucsd.edu/static/models/RECON1.xml')
model = read_sbml_model(path)
ogpr = model.reactions.ATPS4m.gene_name_reaction_rule
gpr = ogpr
print(gpr)
t = build_tree(gpr, Boolean)
print(t)
genes = list(t.get_operands())
print("GENES:\n", genes)
print("Evaluations:")
evaluator = BooleanEvaluator(genes)
res = t.evaluate(evaluator.f_operand, evaluator.f_operator)
print(evaluator.true_list, " ==> ", res)
for _ in range(20):
g = []
n = random.randint(1, len(genes))
for _ in range(n):
i = random.randint(0, len(genes) - 1)
g.append(genes[i])
evaluator.set_true_list(g)
res = t.evaluate(evaluator.f_operand, evaluator.f_operator)
print(evaluator.true_list, " ==> ", res)
def test_3():
"""
Example on how to evaluate the over/under expression of genes using a GPR
"""
# Gene OU example
gpr = "((G_YIL043C and G_YMR015C and G_YNL111C) or (G_YKL150W and G_YMR015C and G_YNL111C))"
genes = {'G_YER091C': 0, 'G_YMR105C': 0.03125, 'G_YNL117W': 0.5, 'G_YNL111C': 0.125, 'G_YJR158W': 0.0625,
'G_YLR355C': 0.5}
operators = (lambda x, y: min(x, y), lambda x, y: max(x, y))
evaluator = GeneEvaluator(genes, operators[0], operators[1])
tree = build_tree(gpr, Boolean)
tree.print_node()
lv = tree.evaluate(evaluator.f_operand, evaluator.f_operator)
print(f"{gpr}\n{genes}\nlevel:{lv}\n")
if __name__ == '__main__':
test_2()
print()
test_2()
print()
test_3()