In [1]:
import copy
import networkx as nx
from regraph.hierarchy import Hierarchy
from regraph.rules import Rule
from regraph.plotting import plot_graph, plot_instance, plot_rule
from regraph.primitives import find_matching, rewrite, print_graph, equal, add_nodes_from, add_edges_from
from regraph.utils import keys_by_value
import matplotlib.pyplot as plt
In [2]:
hierarchy = Hierarchy()
graph = nx.DiGraph()
add_nodes_from(graph,
[
('1', {'name': 'EGFR', 'state': 'p'}),
('2', {'name': 'BND'}),
('3', {'name': 'Grb2', 'aa': 'S', 'loc': 90}),
('4', {'name': 'SH2'}),
('5', {'name': 'EGFR'}),
('6', {'name': 'BND'}),
('7', {'name': 'Grb2'}),
('8', {'name': 'WAF1'}),
('9', {'name': 'BND'}),
('10', {'name': 'G1-S/CDK', 'state': 'p'}),
])
edges = [
('1', '2', {'s': 'p'}),
('4', '2', {'s': 'u'}),
('4', '3'),
('5', '6', {'s': 'p'}),
('7', '6', {'s': 'u'}),
('8', '9'),
('9', '8'),
('10', '8', {"a": {1}}),
('10', '9', {"a": {2}}),
('5', '2', {'s': 'u'})
]
add_edges_from(graph, edges)
hierarchy.add_graph("g", graph)
In [3]:
positioning = plot_graph(graph)
In [4]:
pattern = nx.DiGraph()
add_nodes_from(
pattern,
[(1, {'state': 'p'}),
(2, {'name': 'BND'}),
3,
4]
)
add_edges_from(
pattern,
[(1, 2, {'s': 'p'}),
(3, 2, {'s': 'u'}),
(3, 4)]
)
p = nx.DiGraph()
add_nodes_from(p,
[(1, {'state': 'p'}),
'1_clone',
(2, {'name': 'BND'}),
3,
4
])
add_edges_from(
p,
[(1, 2),
('1_clone', 2),
(3, 4)
])
rhs = nx.DiGraph()
add_nodes_from(
rhs,
[(1, {'state': 'p'}),
'1_clone',
(2, {'name': 'BND'}),
3,
4,
5
])
add_edges_from(
rhs,
[(1, 2, {'s': 'u'}),
('1_clone', 2),
(2, 4),
(3, 4),
(5, 3)
])
p_lhs = {1: 1, '1_clone': 1, 2: 2, 3: 3, 4: 4}
p_rhs = {1: 1, '1_clone': '1_clone', 2: 2, 3: 3, 4: 4}
rule = Rule(p, pattern, rhs, p_lhs, p_rhs)
plot_rule(rule)
In [5]:
instances = hierarchy.find_matching("g", rule.lhs)
print(instances)
In [6]:
hierarchy.rewrite("g", rule, instances[0])
Out[6]:
In [7]:
hierarchy.node["g"].graph.nodes()
Out[7]:
In [8]:
plot_graph(hierarchy.node["g"].graph, parent_pos=positioning)
Out[8]: