Example #1
0
    def __init__(self):
        self.graph_ = TypedDiGraph()
        self.graph_.add_node(1, 'agent', {'name': 'EGFR', 'state': 'p'})
        self.graph_.add_node(2, 'action', attrs={'name': 'BND'})
        self.graph_.add_node(3, 'agent', {
            'name': 'Grb2',
            'aa': 'S',
            'loc': 90
        })
        self.graph_.add_node(4, 'region', attrs={'name': 'SH2'})
        self.graph_.add_node(5, 'agent', attrs={'name': 'EGFR'})
        self.graph_.add_node(6, 'action', attrs={'name': 'BND'})
        self.graph_.add_node(7, 'agent', attrs={'name': 'Grb2'})

        self.graph_.add_node(8, 'agent', attrs={'name': 'WAF1'})
        self.graph_.add_node(9, 'action', {'name': 'BND'})
        self.graph_.add_node(10, 'agent', {'name': 'G1-S/CDK', 'state': 'p'})

        self.graph_.add_node(11, 'agent')
        self.graph_.add_node(12, 'agent')
        self.graph_.add_node(13, 'agent')

        edges = [(1, 2), (4, 2), (4, 3), (5, 6), (7, 6), (8, 9), (10, 9),
                 (11, 12), (12, 11), (12, 13), (13, 12), (11, 13), (13, 11),
                 (5, 2)]

        self.graph_.add_edges_from(edges)

        # later you can add some attributes to the edge

        self.graph_.set_edge(1, 2, {'s': 'p'})
        self.graph_.set_edge(4, 2, {'s': 'u'})
        self.graph_.set_edge(5, 6, {'s': 'p'})
        self.graph_.set_edge(7, 6, {'s': 'u'})
        self.graph_.set_edge(5, 2, {'s': 'u'})

        self.LHS_ = TypedDiGraph()

        self.LHS_.add_node(1, 'agent', {'name': 'EGFR'})
        self.LHS_.add_node(2, 'action', {'name': 'BND'})
        self.LHS_.add_node(3, 'region')
        self.LHS_.add_node(4, 'agent', {'name': 'Grb2'})
        self.LHS_.add_node(5, 'agent', {'name': 'EGFR'})
        self.LHS_.add_node(6, 'action', {'name': 'BND'})
        self.LHS_.add_node(7, 'agent', {'name': 'Grb2'})

        self.LHS_.add_edges_from([(1, 2), (3, 2), (3, 4), (5, 6), (7, 6)])

        self.LHS_.set_edge(1, 2, {'s': 'p'})
        self.LHS_.set_edge(5, 6, {'s': 'p'})
    def test_load_graph_dir(self):
        __location__ = os.path.realpath(
            os.path.join(os.getcwd(), os.path.dirname(__file__)))
        filename = os.path.join(__location__, "graph_example.json")
        a = TypedDiGraph()
        a.load(filename)

        assert_equals(a.nodes(), [1, 2, 3])
        assert_equals(a.edges(), [(1, 2), (2, 3), (3, 1)])
        assert_equals(a.node[1].type_, "agent")
        assert_equals(a.node[2].type_, "agent")
        assert_equals(a.node[3].type_, "action")
        assert_equals(a.node[1].attrs_, {"u": {1}, "k": {33}})
        assert_equals(a.node[2].attrs_, None)
        assert_equals(a.node[3].attrs_, {"x": {33, 55, 66}})
Example #3
0
 def test_homomorphism(self):
     new_pattern = TypedDiGraph()
     new_pattern.add_node(34, "agent")
     new_pattern.add_node(35, "agent")
     new_pattern.add_node(36, "action")
     new_pattern.add_edges_from([(34, 36), (35, 36)])
     mapping = {34: 5, 35: 5, 36: 6}
     h = Homomorphism(new_pattern, self.graph_, mapping)
     assert_equals(h.is_monic(), False)
Example #4
0
def pullback(h1, h2):
    """ Given h1 : B -> D; h2 : C -> D returns A, rh1, rh2
        with rh1 : A -> B; rh2 : A -> C """
    if h1.target_ != h2.target_:
        raise ValueError(
            "Homomorphisms don't have the same codomain, can't do pullback")
    if type(h1.target_) == TypedGraph:
        res_graph = TypedGraph()
    else:
        res_graph = TypedDiGraph()
    hom1 = {}
    hom2 = {}
    for n1 in h1.source_.nodes():
        for n2 in h2.source_.nodes():
            if not h1.mapping_[n1] in res_graph.nodes():
                if h1.mapping_[n1] == h2.mapping_[n2]:
                    res_graph.add_node(
                        h1.mapping_[n1],
                        h1.target_.node[h1.mapping_[n1]].type_,
                        merge_attributes(h1.source_.node[n1].attrs_,
                                         h2.source_.node[n2].attrs_,
                                         'intersection'))

                    hom1[h1.mapping_[n1]] = n1
                    hom2[h2.mapping_[n2]] = n2

    for n1 in res_graph.nodes():
        for n2 in res_graph.nodes():
            if res_graph.is_directed():
                if (hom1[n1], hom1[n2]) in h1.source_.edges():
                    if (hom2[n1], hom2[n2]) in h2.source_.edges():
                        res_graph.add_edge(n1, n2)
                        res_graph.set_edge(
                            n1, n2,
                            merge_attributes(
                                h1.source_.get_edge(hom1[n1], hom1[n2]),
                                h2.source_.get_edge(hom2[n1], hom2[n2]),
                                'intersection'))
            else:
                if (hom1[n1], hom1[n2]) in h1.source_.edges() or (
                        hom1[n2], hom1[n1]) in h1.source_.edges():
                    if (hom2[n1], hom2[n2]) in h2.source_.edges() or (
                            hom2[n2], hom2[n1]) in h2.source_.edges():
                        res_graph.add_edge(n1, n2)
                        res_graph.set_edge(
                            n1, n2,
                            merge_attributes(
                                h1.source_.get_edge(hom1[n1], hom1[n2]),
                                h2.source_.get_edge(hom2[n1], hom2[n2]),
                                'intersection'))

    res_h1 = Homomorphism(res_graph, h1.source_, hom1)
    res_h2 = Homomorphism(res_graph, h2.source_, hom2)

    return res_graph, res_h1, res_h2
 def test_homomorphism(self):
     new_pattern = TypedDiGraph()
     new_pattern.add_node(34, "agent")
     new_pattern.add_node(35, "agent")
     new_pattern.add_node(36, "action")
     new_pattern.add_edges_from([(34, 36), (35, 36)])
     mapping = {34: 5,
                35: 5,
                36: 6}
     h = Homomorphism(new_pattern, self.graph_, mapping)
     assert_equals(h.is_monic(), False)
Example #6
0
    def test_find_matching(self):
        assert_equals(self.instances_, [{
            1: 1,
            2: 2,
            3: 4,
            4: 3,
            5: 5,
            6: 6,
            7: 7
        }])

        new_pattern = TypedDiGraph()
        new_pattern.add_node("a", "agent")
        new_pattern.add_node("b", "agent")
        new_pattern.add_node("c", "agent")

        new_pattern.add_edges_from([("a", "b"), ("a", "c")])
        instances = self.rw_.find_matching(new_pattern)
        assert_equals(6, len(instances))
Example #7
0
    def test_load_graph_dir(self):
        __location__ = os.path.realpath(
            os.path.join(os.getcwd(), os.path.dirname(__file__)))
        filename = os.path.join(__location__, "graph_example.json")
        a = TypedDiGraph()
        a.load(filename)

        assert_equals(a.nodes(), [1, 2, 3])
        assert_equals(a.edges(), [(1, 2), (2, 3), (3, 1)])
        assert_equals(a.node[1].type_, "agent")
        assert_equals(a.node[2].type_, "agent")
        assert_equals(a.node[3].type_, "action")
        assert_equals(a.node[1].attrs_, {"u": {1}, "k": {33}})
        assert_equals(a.node[2].attrs_, None)
        assert_equals(a.node[3].attrs_, {"x": {33, 55, 66}})
Example #8
0
def final_PBC(h1, h2):
    if h1.target_ != h2.source_:
        raise ValueError(
            "Codomain of homomorphism 1 and domain of homomorphism 2 " +
            "don't match, can't do pullback complement")

    if not h2.is_monic():
        raise ValueError(
            "Second homomorphism is not monic, cannot find final pullback complement"
        )

    if type(h1.target_) == TypedGraph:
        res_graph = TypedGraph()
    else:
        res_graph = TypedDiGraph()

    hom1 = {}
    hom2 = {}

    for node in h2.target_.nodes():
        B_node = keys_by_value(h2.mapping_, node)
        if len(B_node) > 0:
            mapped_A_nodes = keys_by_value(h1.mapping_, B_node[0])
            print(mapped_A_nodes)
            for A_node in mapped_A_nodes:
                res_graph.add_node(
                    str(A_node) + "_" + str(node),
                    h2.target_.node[h2.mapping_[h1.mapping_[A_node]]].type_,
                    merge_attributes(
                        h1.source_.node[A_node].attrs_, h2.target_.node[
                            h2.mapping_[h1.mapping_[A_node]]].attrs_,
                        "intersection"))
                hom1[A_node] = str(A_node) + "_" + str(node)
                hom2[str(A_node) + "_" +
                     str(node)] = h2.mapping_[h1.mapping_[A_node]]
        else:
            res_graph.add_node(
                str(node) + "_", h2.target_.node[node].type_,
                h2.target_.node[node].attrs_)
            hom2[str(node) + "_"] = node
    for s, t in h2.target_.edges():
        B_s = keys_by_value(h2.mapping_, s)
        B_t = keys_by_value(h2.mapping_, t)
        if len(B_s) > 0 and len(B_t) > 0:
            mapped_A_ss = keys_by_value(h1.mapping_, B_s[0])
            mapped_A_ts = keys_by_value(h1.mapping_, B_t[0])
            for A_s in mapped_A_ss:
                for A_t in mapped_A_ts:
                    if res_graph.is_directed():
                        if hom1[A_s] == hom1[A_t] and (
                                A_s, A_t) not in h1.source_.edges():
                            res_graph.add_edge(
                                hom1[A_s], hom1[A_t],
                                h2.target_.get_edge(
                                    h2.mapping_[h1.mapping_[A_s]],
                                    h2.mapping_[h1.mapping_[A_t]]))
                        else:
                            res_graph.add_edge(
                                hom1[A_s], hom1[A_t],
                                merge_attributes(
                                    h1.source_.get_edge(A_s, A_t),
                                    h2.target_.get_edge(
                                        h2.mapping_[h1.mapping_[A_s]],
                                        h2.mapping_[h1.mapping_[A_t]]),
                                    "intersection"))
                    else:
                        if hom1[A_s] == hom1[A_t] and (
                                A_s, A_t) not in h1.source_.edges() and (
                                    A_t, A_s) not in h1.source_.edges():
                            res_graph.add_edge(
                                hom1[A_s], hom1[A_t],
                                h2.target_.get_edge(
                                    h2.mapping_[h1.mapping_[A_s]],
                                    h2.mapping_[h1.mapping_[A_t]]))
                            pass
                        else:
                            res_graph.add_edge(
                                hom1[A_s], hom1[A_t],
                                merge_attributes(
                                    h1.source_.get_edge(A_s, A_t),
                                    h2.target_.get_edge(
                                        h2.mapping_[h1.mapping_[A_s]],
                                        h2.mapping_[h1.mapping_[A_t]]),
                                    "intersection"))
        else:
            if len(B_s) == 0:
                sources_to_add = [str(s) + "_"]
            else:
                mapped_A_ss = keys_by_value(h1.mapping_, B_s[0])
                sources_to_add = [hom1[A_s] for A_s in mapped_A_ss]
            if len(B_t) == 0:
                targets_to_add = [str(t) + "_"]
            else:
                mapped_A_ts = keys_by_value(h1.mapping_, B_t[0])
                targets_to_add = [hom1[A_t] for A_t in mapped_A_ts]
            for new_s in sources_to_add:
                for new_t in targets_to_add:
                    res_graph.add_edge(new_s, new_t, h2.target_.edge[s][t])
    res_h1 = Homomorphism(h1.source_, res_graph, hom1)
    res_h2 = Homomorphism(res_graph, h2.target_, hom2)

    return (res_graph, res_h1, res_h2)
Example #9
0
from regraph.library.data_structures import TypedDiGraph


base_metamodel = TypedDiGraph()

base_metamodel.add_nodes_from(
    [
        ("agent", "node"),
        ("action", "node")
    ]
)
base_metamodel.add_edges_from(
    [
        ("agent", "agent"),
        ("agent", "action"),
        ("action", "action"),
        ("action", "agent"),
    ]
)

metamodel_AG = TypedDiGraph(base_metamodel)

metamodel_AG.add_nodes_from(
    [
        ("protein", "agent"),
        ("region", "agent"),
        ("residue", "agent"),
        ("family", "agent"),
        ("complex", "agent"),
        ("small_molecule", "agent"),
        ("state", "agent"),
Example #10
0
def pushout(h1, h2):
    if h1.source_ != h2.source_:
        raise ValueError(
            "Domain of homomorphism 1 and domain of homomorphism 2 " +
            "don't match, can't do pushout")

    hom1 = {}
    hom2 = {}

    if type(h1.target_) == TypedGraph:
        res_graph = TypedGraph()
    else:
        res_graph = TypedDiGraph()

    for node in h1.source_.nodes():
        res_graph.add_node(
            str(h1.mapping_[node]) + "_" + str(h2.mapping_[node]),
            h1.source_.node[node].type_,
            merge_attributes(h1.target_.node[h1.mapping_[node]].attrs_,
                             h2.target_.node[h2.mapping_[node]].attrs_,
                             "union"))
        hom1[h1.mapping_[node]] =\
            str(h1.mapping_[node]) + "_" + str(h2.mapping_[node])
        hom2[h2.mapping_[node]] =\
            str(h1.mapping_[node]) + "_" + str(h2.mapping_[node])

    for s, t in h1.source_.edges():
        res_graph.add_edge(
            str(h1.mapping_[s]) + "_" + str(h2.mapping_[s]),
            str(h1.mapping_[t]) + "_" + str(h2.mapping_[t]),
            merge_attributes(
                h1.target_.get_edge(h1.mapping_[s], h1.mapping_[t]),
                h2.target_.get_edge(h2.mapping_[s], h2.mapping_[t]), "union"))

    for node in h1.target_.nodes():
        if node not in h1.mapping_.values():
            res_graph.add_node(
                str(node) + "_", h1.target_.node[node].type_,
                h1.target_.node[node].attrs_)
            hom1[node] = str(node) + "_"

    for node in h2.target_.nodes():
        if node not in h2.mapping_.values():
            res_graph.add_node(
                str(node) + "_", h2.target_.node[node].type_,
                h2.target_.node[node].attrs_)
            hom2[node] = str(node) + "_"

    for s, t in h1.target_.edges():
        if s not in h1.mapping_.values() or t not in h1.mapping_.values():
            res_graph.add_edge(hom1[s], hom1[t], h1.target_.get_edge(s, t))
        if res_graph.is_directed():
            if (hom1[s], hom1[t]) not in res_graph.edges():
                res_graph.add_edge(hom1[s], hom1[t], h1.target_.get_edge(s, t))
        else:
            if (hom1[s], hom1[t]) not in res_graph.edges() and (
                    hom1[t], hom1[s]) not in res_graph.edges():
                res_graph.add_edge(hom1[s], hom1[t], h1.target_.get_edge(s, t))

    for s, t in h2.target_.edges():
        if s not in h2.mapping_.values() or t not in h2.mapping_.values():
            res_graph.add_edge(hom2[s], hom2[t], h2.target_.get_edge(s, t))
        if res_graph.is_directed():
            if (hom2[s], hom2[t]) not in res_graph.edges():
                res_graph.add_edge(hom2[s], hom2[t], h2.target_.get_edge(s, t))
        else:
            if (hom2[s], hom2[t]) not in res_graph.edges() and (
                    hom2[t], hom2[s]) not in res_graph.edges():
                res_graph.add_edge(hom2[s], hom2[t], h2.target_.get_edge(s, t))

    res_h1 = Homomorphism(h1.target_, res_graph, hom1)
    res_h2 = Homomorphism(h2.target_, res_graph, hom2)

    return (res_graph, res_h1, res_h2)
Example #11
0
class TestDataStructures(object):
    """Class for testing data structures with Python nose tests."""
    def __init__(self):
        self.graph_ = TypedDiGraph()
        self.graph_.add_node(1, 'agent', {'name': 'EGFR', 'state': 'p'})
        self.graph_.add_node(2, 'action', attrs={'name': 'BND'})
        self.graph_.add_node(3, 'agent', {
            'name': 'Grb2',
            'aa': 'S',
            'loc': 90
        })
        self.graph_.add_node(4, 'region', attrs={'name': 'SH2'})
        self.graph_.add_node(5, 'agent', attrs={'name': 'EGFR'})
        self.graph_.add_node(6, 'action', attrs={'name': 'BND'})
        self.graph_.add_node(7, 'agent', attrs={'name': 'Grb2'})

        self.graph_.add_node(8, 'agent', attrs={'name': 'WAF1'})
        self.graph_.add_node(9, 'action', {'name': 'BND'})
        self.graph_.add_node(10, 'agent', {'name': 'G1-S/CDK', 'state': 'p'})

        self.graph_.add_node(11, 'agent')
        self.graph_.add_node(12, 'agent')
        self.graph_.add_node(13, 'agent')

        edges = [(1, 2), (4, 2), (4, 3), (5, 6), (7, 6), (8, 9), (10, 9),
                 (11, 12), (12, 11), (12, 13), (13, 12), (11, 13), (13, 11),
                 (5, 2)]

        self.graph_.add_edges_from(edges)

        # later you can add some attributes to the edge

        self.graph_.set_edge(1, 2, {'s': 'p'})
        self.graph_.set_edge(4, 2, {'s': 'u'})
        self.graph_.set_edge(5, 6, {'s': 'p'})
        self.graph_.set_edge(7, 6, {'s': 'u'})
        self.graph_.set_edge(5, 2, {'s': 'u'})

        self.LHS_ = TypedDiGraph()

        self.LHS_.add_node(1, 'agent', {'name': 'EGFR'})
        self.LHS_.add_node(2, 'action', {'name': 'BND'})
        self.LHS_.add_node(3, 'region')
        self.LHS_.add_node(4, 'agent', {'name': 'Grb2'})
        self.LHS_.add_node(5, 'agent', {'name': 'EGFR'})
        self.LHS_.add_node(6, 'action', {'name': 'BND'})
        self.LHS_.add_node(7, 'agent', {'name': 'Grb2'})

        self.LHS_.add_edges_from([(1, 2), (3, 2), (3, 4), (5, 6), (7, 6)])

        self.LHS_.set_edge(1, 2, {'s': 'p'})
        self.LHS_.set_edge(5, 6, {'s': 'p'})

    def test_homorphism_init(self):
        # Test homomorphisms functionality
        mapping = {1: 1, 2: 2, 3: 4, 4: 3, 5: 5, 6: 6, 7: 7}
        Homomorphism(self.LHS_, self.graph_, mapping)

    @raises(ValueError)
    def test_homomorphism_not_covered(self):
        mapping = {1: 1, 2: 2, 3: 4, 4: 3, 5: 5, 6: 6}
        Homomorphism(self.LHS_, self.graph_, mapping)

    @raises(ValueError)
    def test_homomorphism_type_mismatch(self):
        mapping = {1: 1, 2: 2, 3: 4, 4: 3, 5: 5, 6: 6, 7: 7}
        cast_node(self.LHS_, 1, 'other_type')
        Homomorphism(self.LHS_, self.graph_, mapping)

    @raises(ValueError)
    def test_homomorphism_attributes_mismatch(self):
        mapping = {1: 1, 2: 2, 3: 4, 4: 3, 5: 5, 6: 6, 7: 7}
        self.LHS_.node[1].attrs_.update({'new_attr': 0})
        Homomorphism(self.LHS_, self.graph_, mapping)

    @raises(ValueError)
    def test_homomorphism_connectivity_fails(self):
        mapping = {1: 1, 2: 2, 3: 4, 4: 3, 5: 5, 6: 6, 7: 7}
        remove_edge(self.graph_, 4, 5)
        Homomorphism(self.LHS_, self.graph_, mapping)

    @raises(ValueError)
    def test_homomorphism_edge_attributes_mismatch(self):
        mapping = {1: 1, 2: 2, 3: 4, 4: 3, 5: 5, 6: 6, 7: 7}
        self.LHS_.edge[5][6].update({'new_attr': 0})
        Homomorphism(self.LHS_, self.graph_, mapping)

    def test_homomorphism(self):
        new_pattern = TypedDiGraph()
        new_pattern.add_node(34, "agent")
        new_pattern.add_node(35, "agent")
        new_pattern.add_node(36, "action")
        new_pattern.add_edges_from([(34, 36), (35, 36)])
        mapping = {34: 5, 35: 5, 36: 6}
        h = Homomorphism(new_pattern, self.graph_, mapping)
        assert_equals(h.is_monic(), False)

    def test_init_with_metamodel_directed(self):
        meta_meta = TypedDiGraph()
        meta_meta.add_node("agent", "node")
        meta_meta.add_node("action", "node")
        meta_meta.add_edges_from([("agent", "agent"), ("action", "action"),
                                  ("action", "agent"), ("agent", "action")])

        meta = TypedDiGraph(meta_meta)
        meta.add_node("protein", "agent")
        meta.add_node("region", "agent")
        meta.add_node("action", "agent")
        meta.add_edges_from([
            ("protein", "protein"),
            ("region", "region"),
            ("action", "action"),
            ("region", "protein"),
            ("region", "action"),
            ("action", "region"),
        ])

        graph = TypedDiGraph(meta)
        graph.add_nodes_from([(1, "protein"), (2, "region"), (3, "action"),
                              (4, "region"), (5, "protein"), (6, "region"),
                              (7, "protein")])
        graph.add_edge(2, 1)
        graph.add_edge(2, 3)
        graph.add_edge(4, 3)
        graph.add_edge(4, 5)
        graph.add_edge(6, 3)
        graph.add_edge(6, 7)

    def test_init_with_metamodel_undirected(self):
        meta_meta = TypedGraph()
        meta_meta.add_node("agent", "node")
        meta_meta.add_node("action", "node")
        meta_meta.add_edges_from([("agent", "agent"), ("action", "action"),
                                  ("action", "agent")])

        meta = TypedGraph(meta_meta)
        meta.add_node("protein", "agent")
        meta.add_node("region", "agent")
        meta.add_node("action", "agent")
        meta.add_edges_from([
            ("protein", "protein", {
                'a': 1
            }),
            ("region", "region"),
            ("action", "action"),
            ("protein", "region", {
                'a': 2
            }),
            ("action", "region"),
        ])
        assert_equals(meta.edge["protein"]["region"],
                      meta.edge["region"]["protein"])

        graph = TypedGraph(meta)
        graph.add_nodes_from([(1, "protein"), (2, "region"), (3, "action"),
                              (4, "region"), (5, "protein"), (6, "region"),
                              (7, "protein")])
        graph.add_edge(2, 1, {'x': 1})
        graph.add_edge(2, 3, {'x': 2})
        graph.add_edge(4, 3, {'x': 3})
        graph.add_edge(4, 5, {'x': 4})
        graph.add_edge(6, 3, {'x': 5})
        graph.add_edge(6, 7, {'x': 6})
        assert_equals(graph.edge[1][2], graph.edge[2][1])

    def test_load_graph_dir(self):
        __location__ = os.path.realpath(
            os.path.join(os.getcwd(), os.path.dirname(__file__)))
        filename = os.path.join(__location__, "graph_example.json")
        a = TypedDiGraph()
        a.load(filename)

        assert_equals(a.nodes(), [1, 2, 3])
        assert_equals(a.edges(), [(1, 2), (2, 3), (3, 1)])
        assert_equals(a.node[1].type_, "agent")
        assert_equals(a.node[2].type_, "agent")
        assert_equals(a.node[3].type_, "action")
        assert_equals(a.node[1].attrs_, {"u": {1}, "k": {33}})
        assert_equals(a.node[2].attrs_, None)
        assert_equals(a.node[3].attrs_, {"x": {33, 55, 66}})

    def test_load_graph_undir(self):
        __location__ = os.path.realpath(
            os.path.join(os.getcwd(), os.path.dirname(__file__)))
        filename = os.path.join(__location__, "graph_example.json")
        a = TypedGraph()
        a.load(filename)

        assert_equals(a.nodes(), [1, 2, 3])
        assert_edges_undir(a.edges(), [(1, 2), (2, 3), (3, 1)])
        assert_equals(a.node[1].type_, "agent")
        assert_equals(a.node[2].type_, "agent")
        assert_equals(a.node[3].type_, "action")
        assert_equals(a.node[1].attrs_, {"u": {1}, "k": {33}})
        assert_equals(a.node[2].attrs_, None)
        assert_equals(a.node[3].attrs_, {"x": {33, 55, 66}})

    def test_load_export(self):
        __location__ = os.path.realpath(
            os.path.join(os.getcwd(), os.path.dirname(__file__)))
        filename = os.path.join(__location__, "graph_example.json")
        a = TypedGraph()
        a.load(filename)
        out_filename = os.path.join(__location__, "output_graph.json")
        a.export(out_filename)
        b = TypedGraph()
        b.load(out_filename)

        assert_equals(a.nodes(), b.nodes())
        assert_edges_undir(a.edges(), b.edges())
        assert_equals(a.node[3].attrs_, b.node[3].attrs_)
    def __init__(self):
        self.graph_ = TypedDiGraph()
        self.graph_.add_node(1, 'agent',
                             {'name': 'EGFR', 'state': 'p'})
        self.graph_.add_node(2, 'action', attrs={'name': 'BND'})
        self.graph_.add_node(3, 'agent',
                             {'name': 'Grb2', 'aa': 'S', 'loc': 90})
        self.graph_.add_node(4, 'region', attrs={'name': 'SH2'})
        self.graph_.add_node(5, 'agent', attrs={'name': 'EGFR'})
        self.graph_.add_node(6, 'action', attrs={'name': 'BND'})
        self.graph_.add_node(7, 'agent', attrs={'name': 'Grb2'})

        self.graph_.add_node(8, 'agent', attrs={'name': 'WAF1'})
        self.graph_.add_node(9, 'action', {'name': 'BND'})
        self.graph_.add_node(10, 'agent', {'name': 'G1-S/CDK', 'state': 'p'})

        self.graph_.add_node(11, 'agent')
        self.graph_.add_node(12, 'agent')
        self.graph_.add_node(13, 'agent')

        edges = [
            (1, 2),
            (4, 2),
            (4, 3),
            (5, 6),
            (7, 6),
            (8, 9),
            (10, 9),
            (11, 12),
            (12, 11),
            (12, 13),
            (13, 12),
            (11, 13),
            (13, 11),
            (5, 2)
        ]

        self.graph_.add_edges_from(edges)

        # later you can add some attributes to the edge

        self.graph_.set_edge(1, 2, {'s': 'p'})
        self.graph_.set_edge(4, 2, {'s': 'u'})
        self.graph_.set_edge(5, 6, {'s': 'p'})
        self.graph_.set_edge(7, 6, {'s': 'u'})
        self.graph_.set_edge(5, 2, {'s': 'u'})

        self.LHS_ = TypedDiGraph()

        self.LHS_.add_node(1, 'agent', {'name': 'EGFR'})
        self.LHS_.add_node(2, 'action', {'name': 'BND'})
        self.LHS_.add_node(3, 'region')
        self.LHS_.add_node(4, 'agent', {'name': 'Grb2'})
        self.LHS_.add_node(5, 'agent', {'name': 'EGFR'})
        self.LHS_.add_node(6, 'action', {'name': 'BND'})
        self.LHS_.add_node(7, 'agent', {'name': 'Grb2'})

        self.LHS_.add_edges_from([(1, 2), (3, 2), (3, 4), (5, 6), (7, 6)])

        self.LHS_.set_edge(1, 2, {'s': 'p'})
        self.LHS_.set_edge(5, 6, {'s': 'p'})
    def test_init_with_metamodel_directed(self):
        meta_meta = TypedDiGraph()
        meta_meta.add_node("agent", "node")
        meta_meta.add_node("action", "node")
        meta_meta.add_edges_from([
            ("agent", "agent"),
            ("action", "action"),
            ("action", "agent"),
            ("agent", "action")])

        meta = TypedDiGraph(meta_meta)
        meta.add_node("protein", "agent")
        meta.add_node("region", "agent")
        meta.add_node("action", "agent")
        meta.add_edges_from([
            ("protein", "protein"),
            ("region", "region"),
            ("action", "action"),
            ("region", "protein"),
            ("region", "action"),
            ("action", "region"),
        ])

        graph = TypedDiGraph(meta)
        graph.add_nodes_from([
            (1, "protein"),
            (2, "region"),
            (3, "action"),
            (4, "region"),
            (5, "protein"),
            (6, "region"),
            (7, "protein")])
        graph.add_edge(2, 1)
        graph.add_edge(2, 3)
        graph.add_edge(4, 3)
        graph.add_edge(4, 5)
        graph.add_edge(6, 3)
        graph.add_edge(6, 7)
class TestDataStructures(object):
    """Class for testing data structures with Python nose tests."""

    def __init__(self):
        self.graph_ = TypedDiGraph()
        self.graph_.add_node(1, 'agent',
                             {'name': 'EGFR', 'state': 'p'})
        self.graph_.add_node(2, 'action', attrs={'name': 'BND'})
        self.graph_.add_node(3, 'agent',
                             {'name': 'Grb2', 'aa': 'S', 'loc': 90})
        self.graph_.add_node(4, 'region', attrs={'name': 'SH2'})
        self.graph_.add_node(5, 'agent', attrs={'name': 'EGFR'})
        self.graph_.add_node(6, 'action', attrs={'name': 'BND'})
        self.graph_.add_node(7, 'agent', attrs={'name': 'Grb2'})

        self.graph_.add_node(8, 'agent', attrs={'name': 'WAF1'})
        self.graph_.add_node(9, 'action', {'name': 'BND'})
        self.graph_.add_node(10, 'agent', {'name': 'G1-S/CDK', 'state': 'p'})

        self.graph_.add_node(11, 'agent')
        self.graph_.add_node(12, 'agent')
        self.graph_.add_node(13, 'agent')

        edges = [
            (1, 2),
            (4, 2),
            (4, 3),
            (5, 6),
            (7, 6),
            (8, 9),
            (10, 9),
            (11, 12),
            (12, 11),
            (12, 13),
            (13, 12),
            (11, 13),
            (13, 11),
            (5, 2)
        ]

        self.graph_.add_edges_from(edges)

        # later you can add some attributes to the edge

        self.graph_.set_edge(1, 2, {'s': 'p'})
        self.graph_.set_edge(4, 2, {'s': 'u'})
        self.graph_.set_edge(5, 6, {'s': 'p'})
        self.graph_.set_edge(7, 6, {'s': 'u'})
        self.graph_.set_edge(5, 2, {'s': 'u'})

        self.LHS_ = TypedDiGraph()

        self.LHS_.add_node(1, 'agent', {'name': 'EGFR'})
        self.LHS_.add_node(2, 'action', {'name': 'BND'})
        self.LHS_.add_node(3, 'region')
        self.LHS_.add_node(4, 'agent', {'name': 'Grb2'})
        self.LHS_.add_node(5, 'agent', {'name': 'EGFR'})
        self.LHS_.add_node(6, 'action', {'name': 'BND'})
        self.LHS_.add_node(7, 'agent', {'name': 'Grb2'})

        self.LHS_.add_edges_from([(1, 2), (3, 2), (3, 4), (5, 6), (7, 6)])

        self.LHS_.set_edge(1, 2, {'s': 'p'})
        self.LHS_.set_edge(5, 6, {'s': 'p'})

    def test_homorphism_init(self):
        # Test homomorphisms functionality
        mapping = {1: 1,
                   2: 2,
                   3: 4,
                   4: 3,
                   5: 5,
                   6: 6,
                   7: 7}
        Homomorphism(self.LHS_, self.graph_, mapping)

    @raises(ValueError)
    def test_homomorphism_not_covered(self):
        mapping = {1: 1,
                   2: 2,
                   3: 4,
                   4: 3,
                   5: 5,
                   6: 6}
        Homomorphism(self.LHS_, self.graph_, mapping)

    @raises(ValueError)
    def test_homomorphism_type_mismatch(self):
        mapping = {1: 1,
                   2: 2,
                   3: 4,
                   4: 3,
                   5: 5,
                   6: 6,
                   7: 7}
        cast_node(self.LHS_, 1, 'other_type')
        Homomorphism(self.LHS_, self.graph_, mapping)

    @raises(ValueError)
    def test_homomorphism_attributes_mismatch(self):
        mapping = {1: 1,
                   2: 2,
                   3: 4,
                   4: 3,
                   5: 5,
                   6: 6,
                   7: 7}
        self.LHS_.node[1].attrs_.update({'new_attr': 0})
        Homomorphism(self.LHS_, self.graph_, mapping)

    @raises(ValueError)
    def test_homomorphism_connectivity_fails(self):
        mapping = {1: 1,
                   2: 2,
                   3: 4,
                   4: 3,
                   5: 5,
                   6: 6,
                   7: 7}
        remove_edge(self.graph_, 4, 5)
        Homomorphism(self.LHS_, self.graph_, mapping)

    @raises(ValueError)
    def test_homomorphism_edge_attributes_mismatch(self):
        mapping = {1: 1,
                   2: 2,
                   3: 4,
                   4: 3,
                   5: 5,
                   6: 6,
                   7: 7}
        self.LHS_.edge[5][6].update({'new_attr': 0})
        Homomorphism(self.LHS_, self.graph_, mapping)

    def test_homomorphism(self):
        new_pattern = TypedDiGraph()
        new_pattern.add_node(34, "agent")
        new_pattern.add_node(35, "agent")
        new_pattern.add_node(36, "action")
        new_pattern.add_edges_from([(34, 36), (35, 36)])
        mapping = {34: 5,
                   35: 5,
                   36: 6}
        h = Homomorphism(new_pattern, self.graph_, mapping)
        assert_equals(h.is_monic(), False)

    def test_init_with_metamodel_directed(self):
        meta_meta = TypedDiGraph()
        meta_meta.add_node("agent", "node")
        meta_meta.add_node("action", "node")
        meta_meta.add_edges_from([
            ("agent", "agent"),
            ("action", "action"),
            ("action", "agent"),
            ("agent", "action")])

        meta = TypedDiGraph(meta_meta)
        meta.add_node("protein", "agent")
        meta.add_node("region", "agent")
        meta.add_node("action", "agent")
        meta.add_edges_from([
            ("protein", "protein"),
            ("region", "region"),
            ("action", "action"),
            ("region", "protein"),
            ("region", "action"),
            ("action", "region"),
        ])

        graph = TypedDiGraph(meta)
        graph.add_nodes_from([
            (1, "protein"),
            (2, "region"),
            (3, "action"),
            (4, "region"),
            (5, "protein"),
            (6, "region"),
            (7, "protein")])
        graph.add_edge(2, 1)
        graph.add_edge(2, 3)
        graph.add_edge(4, 3)
        graph.add_edge(4, 5)
        graph.add_edge(6, 3)
        graph.add_edge(6, 7)

    def test_init_with_metamodel_undirected(self):
        meta_meta = TypedGraph()
        meta_meta.add_node("agent", "node")
        meta_meta.add_node("action", "node")
        meta_meta.add_edges_from([
            ("agent", "agent"),
            ("action", "action"),
            ("action", "agent")])

        meta = TypedGraph(meta_meta)
        meta.add_node("protein", "agent")
        meta.add_node("region", "agent")
        meta.add_node("action", "agent")
        meta.add_edges_from([
            ("protein", "protein", {'a': 1}),
            ("region", "region"),
            ("action", "action"),
            ("protein", "region", {'a': 2}),
            ("action", "region"),
        ])
        assert_equals(
            meta.edge["protein"]["region"],
            meta.edge["region"]["protein"])

        graph = TypedGraph(meta)
        graph.add_nodes_from([
            (1, "protein"),
            (2, "region"),
            (3, "action"),
            (4, "region"),
            (5, "protein"),
            (6, "region"),
            (7, "protein")])
        graph.add_edge(2, 1, {'x': 1})
        graph.add_edge(2, 3, {'x': 2})
        graph.add_edge(4, 3, {'x': 3})
        graph.add_edge(4, 5, {'x': 4})
        graph.add_edge(6, 3, {'x': 5})
        graph.add_edge(6, 7, {'x': 6})
        assert_equals(graph.edge[1][2], graph.edge[2][1])

    def test_load_graph_dir(self):
        __location__ = os.path.realpath(
            os.path.join(os.getcwd(), os.path.dirname(__file__)))
        filename = os.path.join(__location__, "graph_example.json")
        a = TypedDiGraph()
        a.load(filename)

        assert_equals(a.nodes(), [1, 2, 3])
        assert_equals(a.edges(), [(1, 2), (2, 3), (3, 1)])
        assert_equals(a.node[1].type_, "agent")
        assert_equals(a.node[2].type_, "agent")
        assert_equals(a.node[3].type_, "action")
        assert_equals(a.node[1].attrs_, {"u": {1}, "k": {33}})
        assert_equals(a.node[2].attrs_, None)
        assert_equals(a.node[3].attrs_, {"x": {33, 55, 66}})

    def test_load_graph_undir(self):
        __location__ = os.path.realpath(
            os.path.join(os.getcwd(), os.path.dirname(__file__)))
        filename = os.path.join(__location__, "graph_example.json")
        a = TypedGraph()
        a.load(filename)

        assert_equals(a.nodes(), [1, 2, 3])
        assert_edges_undir(a.edges(), [(1, 2), (2, 3), (3, 1)])
        assert_equals(a.node[1].type_, "agent")
        assert_equals(a.node[2].type_, "agent")
        assert_equals(a.node[3].type_, "action")
        assert_equals(a.node[1].attrs_, {"u": {1}, "k": {33}})
        assert_equals(a.node[2].attrs_, None)
        assert_equals(a.node[3].attrs_, {"x": {33, 55, 66}})

    def test_load_export(self):
        __location__ = os.path.realpath(
            os.path.join(os.getcwd(), os.path.dirname(__file__)))
        filename = os.path.join(__location__, "graph_example.json")
        a = TypedGraph()
        a.load(filename)
        out_filename = os.path.join(__location__, "output_graph.json")
        a.export(out_filename)
        b = TypedGraph()
        b.load(out_filename)

        assert_equals(a.nodes(), b.nodes())
        assert_edges_undir(a.edges(), b.edges())
        assert_equals(a.node[3].attrs_, b.node[3].attrs_)
Example #15
0
    def test_clonning_merging_sequence(self):
        __location__ = os.path.realpath(
            os.path.join(os.getcwd(), os.path.dirname(__file__)))

        g = TypedDiGraph()

        g.add_node(1, "agent", {"name": "John"})
        g.add_node(2, "action", {})

        g.add_edges_from([(1, 2), (2, 1)])

        plot_graph(g, filename=os.path.join(__location__, "cms_test_init.png"))

        LHS = TypedDiGraph()
        LHS.add_node('entity', 'agent')
        LHS.add_node('media', 'action')

        LHS.add_edges_from([('entity', 'media'), ('media', 'entity')])

        rewriter = Rewriter(g)
        instances = rewriter.find_matching(LHS)

        for i, instance in enumerate(instances):
            plot_instance(
                rewriter.graph_, LHS, instance,
                os.path.join(__location__, "cms_instance_%d.png" % i))
        for instance in instances:
            new_name = rewriter.clone(instance, 'media')
            rewriter.add_edge(instance, 'media', new_name)
            # rewriter.merge(instance, ['media', new_name])
        plot_graph(rewriter.graph_,
                   filename=os.path.join(__location__, "cms_test_clone1.png"))
Example #16
0
class TestRewrites(object):
    """."""
    def __init__(self):
        graph = TypedDiGraph()
        graph.add_node(1, 'agent', {'name': 'EGFR', 'state': 'p'})
        graph.add_node(2, 'action', attrs={'name': 'BND'})
        graph.add_node(3, 'agent', {'name': 'Grb2', 'aa': 'S', 'loc': 90})
        graph.add_node(4, 'region', attrs={'name': 'SH2'})
        graph.add_node(5, 'agent', attrs={'name': 'EGFR'})
        graph.add_node(6, 'action', attrs={'name': 'BND'})
        graph.add_node(7, 'agent', attrs={'name': 'Grb2'})

        graph.add_node(8, 'agent', attrs={'name': 'WAF1'})
        graph.add_node(9, 'action', {'name': 'BND'})
        graph.add_node(10, 'agent', {'name': 'G1-S/CDK', 'state': 'p'})

        graph.add_node(11, 'agent')
        graph.add_node(12, 'agent')
        graph.add_node(13, 'agent')

        edges = [(1, 2), (4, 2), (4, 3), (5, 6), (7, 6), (8, 9), (10, 9),
                 (11, 12), (12, 11), (12, 13), (13, 12), (11, 13), (13, 11),
                 (5, 2)]

        graph.add_edges_from(edges)

        graph.set_edge(1, 2, {'s': 'p'})
        graph.set_edge(4, 2, {'s': 'u'})
        graph.set_edge(5, 6, {'s': 'p'})
        graph.set_edge(7, 6, {'s': 'u'})
        graph.set_edge(5, 2, {'s': 'u'})

        self.LHS_ = TypedDiGraph()

        self.LHS_.add_node(1, 'agent', {'name': 'EGFR'})
        self.LHS_.add_node(2, 'action', {'name': 'BND'})
        self.LHS_.add_node(3, 'region')
        self.LHS_.add_node(4, 'agent', {'name': 'Grb2'})
        self.LHS_.add_node(5, 'agent', {'name': 'EGFR'})
        self.LHS_.add_node(6, 'action', {'name': 'BND'})
        self.LHS_.add_node(7, 'agent', {'name': 'Grb2'})

        self.LHS_.add_edges_from([(1, 2), (3, 2), (3, 4), (5, 6), (7, 6)])

        self.LHS_.set_edge(1, 2, {'s': 'p'})
        self.LHS_.set_edge(5, 6, {'s': 'p'})

        self.rw_ = Rewriter(graph)
        self.instances_ = self.rw_.find_matching(self.LHS_)

    def test_find_matching(self):
        assert_equals(self.instances_, [{
            1: 1,
            2: 2,
            3: 4,
            4: 3,
            5: 5,
            6: 6,
            7: 7
        }])

        new_pattern = TypedDiGraph()
        new_pattern.add_node("a", "agent")
        new_pattern.add_node("b", "agent")
        new_pattern.add_node("c", "agent")

        new_pattern.add_edges_from([("a", "b"), ("a", "c")])
        instances = self.rw_.find_matching(new_pattern)
        assert_equals(6, len(instances))

    def test_transform_instance(self):
        self.rw_.transform_instance(
            self.instances_[0], """delete_node 6.\n"""
            """merge [1, 5] method union as 'merge_1'.\n"""
            """merge [4, 7] as 'merge_2'.\n"""
            """add_edge 'merge_1' 'merge_2'.\n"""
            """clone 'merge_1' as 'clone_1'.\n"""
            """clone 3 as 'clone_2'.""")

    def test_clonning_merging_sequence(self):
        __location__ = os.path.realpath(
            os.path.join(os.getcwd(), os.path.dirname(__file__)))

        g = TypedDiGraph()

        g.add_node(1, "agent", {"name": "John"})
        g.add_node(2, "action", {})

        g.add_edges_from([(1, 2), (2, 1)])

        plot_graph(g, filename=os.path.join(__location__, "cms_test_init.png"))

        LHS = TypedDiGraph()
        LHS.add_node('entity', 'agent')
        LHS.add_node('media', 'action')

        LHS.add_edges_from([('entity', 'media'), ('media', 'entity')])

        rewriter = Rewriter(g)
        instances = rewriter.find_matching(LHS)

        for i, instance in enumerate(instances):
            plot_instance(
                rewriter.graph_, LHS, instance,
                os.path.join(__location__, "cms_instance_%d.png" % i))
        for instance in instances:
            new_name = rewriter.clone(instance, 'media')
            rewriter.add_edge(instance, 'media', new_name)
            # rewriter.merge(instance, ['media', new_name])
        plot_graph(rewriter.graph_,
                   filename=os.path.join(__location__, "cms_test_clone1.png"))

    def test_undirected_imp_init(self):
        __location__ = os.path.realpath(
            os.path.join(os.getcwd(), os.path.dirname(__file__)))

        g = TypedGraph()
        g.add_node(1, "agent", {"name": "John"})
        g.add_node(2, "action")
        g.add_node(3, "agent", {"name": "Paul"})

        g.add_edges_from([(1, 2), (3, 2)])
        g.set_edge(1, 2, {"a": 0})

        rw = Rewriter(g)
        LHS = TypedGraph()
        LHS.add_nodes_from([(1, "agent"), (2, "action")])
        LHS.add_edges_from([(1, 2)])

        instances = rw.find_matching(LHS)
        for i, instance in enumerate(instances):
            plot_instance(
                rw.graph_, LHS, instance,
                os.path.join(__location__, "undir_instance_%d.png" % i))
        rw.add_node(instances[0], 'region', 'Europe', {"a": 44})
        rw.delete_node(instances[0], 1)
        rw.delete_edge(instances[0], 2, 3)
        rw.clone(instances[0], 2)
        cast_node(rw.graph_, "Europe", "action")
        rw.merge(instances[0], ["Europe", 2])
        plot_graph(rw.graph_,
                   filename=os.path.join(__location__, "undir_cloned.png"))

    def test_undirected_dec_init(self):
        __location__ = os.path.realpath(
            os.path.join(os.getcwd(), os.path.dirname(__file__)))

        g = TypedGraph()
        g.add_node(1, "action")
        g.add_node(2, "agent", {"u": {0, 1}})
        g.add_node(3, "agent", {"u": {4}, "name": "Paul"})
        g.add_node(4, "action")
        g.add_node(5, "agent", {"u": {0}})
        g.add_node(6, "agent", {"u": {7}})
        g.add_node(7, "agent", {"u": {4}})

        g.add_edges_from([(1, 2), (3, 2), (1, 5), (5, 4), (5, 6)])
        g.set_edge(1, 2, {"a": {0}})
        g.set_edge(2, 3, {"k": {1, 2, 3}})

        rw = Rewriter(g)

        LHS = TypedGraph()
        LHS.add_nodes_from([(10, "action"), (20, "agent"), (30, "agent")])
        LHS.node[20].attrs_ = {"u": {0}}

        LHS.add_edges_from([(10, 20), (20, 30)])
        LHS.set_edge(20, 30, {"k": {1, 2}})

        P = TypedGraph()
        P.add_node(100, "agent")
        P.add_node(200, "agent", {"u": {0}})
        P.add_node(300, "agent")
        P.add_edges_from([(300, 100), (200, 300)])
        P.set_edge(100, 300, {"k": {1, 2}})
        P.set_edge(200, 300, {"k": set()})

        RHS = TypedGraph()
        RHS.add_node(1000, "region")
        RHS.add_node(2000, "agent", {"u": {3}})
        RHS.add_node(3000, "agent", {"u": {0, 2}})

        RHS.add_edges_from([(1000, 3000), (2000, 3000), (3000, 3000)])
        RHS.set_edge(3000, 3000, {"k": {5, 6}})
        RHS.set_edge(2000, 3000, {"k": {1, 2, 10}})
        RHS.set_edge(1000, 3000, {"a": {12}})

        instances = rw.find_matching(LHS)
        for i, instance in enumerate(instances):
            plot_instance(
                rw.graph_, LHS, instance,
                os.path.join(__location__, "undir_dec_instance_%d.png" % i))
        left_h = Homomorphism(P, LHS, {100: 20, 200: 20, 300: 30})
        righ_h = Homomorphism(P, RHS, {100: 2000, 200: 3000, 300: 3000})
        RHS_instance = rw.apply_rule(instances[0], left_h, righ_h)
        plot_instance(rw.graph_,
                      RHS,
                      RHS_instance,
                      filename=os.path.join(__location__, "undir_dec_RHS.png"))

    def test_rule_to_homomorphism(self):
        __location__ = os.path.realpath(
            os.path.join(os.getcwd(), os.path.dirname(__file__)))

        g = TypedGraph()
        g.add_node(1, "action")
        g.add_node(2, "agent", {"u": {0, 1}})
        g.add_node(3, "agent", {"u": {4}, "name": "Paul"})
        g.add_node(4, "action")
        g.add_node(5, "agent", {"u": {0}})
        g.add_node(6, "agent", {"u": {7}})
        g.add_node(7, "agent", {"u": {4}})

        g.add_edges_from([(1, 2), (3, 2), (1, 5), (5, 4), (5, 6)])
        g.set_edge(1, 2, {"a": {0}})
        g.set_edge(2, 3, {"k": {1, 2, 3}})

        rw = Rewriter(g)

        LHS = TypedGraph()
        LHS.add_nodes_from([(1, "action"), (2, "agent"), (3, "agent")])
        LHS.node[2].set_attrs({"u": 0})

        LHS.add_edges_from([(1, 2), (2, 3)])
        LHS.set_edge(2, 3, {"k": {1, 2}})

        instances = rw.find_matching(LHS)

        rw.add_node_attrs(instances[0], 1, {"u": 55, "x": 0})
        h1, h2 = rw.generate_rule(
            LHS, """delete_node 1.
            clone 2 as 'clone'.
            delete_node_attrs 'clone' {'u': 0}.
            delete_edge 2 3.
            delete_edge_attrs 'clone' 3 {'k': {1}}.
            update_edge_attrs 'clone' 3 {'t': 333}.
            update_node_attrs 2 {'u': {12, 13}}.
            merge ['clone', 3] as 'merged'.
            add_node_attrs 'merged' {'m': 1}.
            add_node 'new_node' type 'region'.
            add_node_attrs 'new_node' {'x': 1}.
            add_edge 'new_node' 'merged'.
            add_edge_attrs 'merged' 'new_node' {'j': 33}.""")

        RHS_instance = rw.apply_rule(instances[0], h1, h2)
        plot_instance(rw.graph_,
                      h2.target_,
                      RHS_instance,
                      filename=os.path.join(__location__,
                                            "undir_rule_to_hom_RHS.png"))

    def test_cloning_cases(self):
        g = TypedDiGraph()
        g.add_node(10, "agent", {"a": 0})
        g.add_node(20, "agent", {"b": 0})
        g.add_node(30, "action", {"c": 0})
        g.add_edges_from([(10, 30), (20, 30)])
        g.set_edge(10, 30, {"x": 0})
        g.set_edge(20, 30, {"y": 0})

        LHS = TypedDiGraph()
        LHS.add_node(1, "agent")

        rw = Rewriter(g)
        instances = rw.find_matching(LHS)

        # simple clone
        P1 = TypedDiGraph()
        P1.add_node("a", "agent")
        P1.add_node("b", "agent")

        RHS1 = TypedDiGraph()
        RHS1.add_node("x", "agent")
        RHS1.add_node("y", "agent")

        h1 = Homomorphism(P1, LHS, {"a": 1, "b": 1})
        h2 = Homomorphism(P1, RHS1, {"a": "x", "b": "y"})
        RHS_instance = rw.apply_rule(instances[0], h1, h2)

        # clone merge on the same nodes
        RHS2 = TypedGraph()
        RHS2.add_node("x", "agent")
        h2 = Homomorphism(P1, RHS2, {"a": "x", "b": "x"})
        RHS_instance = rw.apply_rule(instances[0], h1, h2)

        # clone and merge one with other node
        LHS.add_node(2, "agent")
        # update matching
        instances = rw.find_matching(LHS)

        P3 = TypedDiGraph()
        P3.add_node("a", "agent")
        P3.add_node("b", "agent")
        P3.add_node("c", "agent")

        RHS3 = TypedDiGraph()
        RHS3.add_node("x", "agent")
        RHS3.add_node("y", "agent")

        h1 = Homomorphism(P3, LHS, {"a": 1, "b": 1, "c": 2})
        h2 = Homomorphism(P3, RHS3, {"a": "x", "b": "y", "c": "y"})
        RHS_instance = rw.apply_rule(instances[0], h1, h2)

    def test_merging(self):
        g = TypedDiGraph()
        g.add_node(10, "agent", {"a": 0})
        g.add_node(20, "agent", {"b": 0})
        g.add_node(30, "action", {"c": 0})
        g.add_edges_from([(10, 30), (20, 30)])
        g.set_edge(10, 30, {"x": 0})
        g.set_edge(20, 30, {"y": 0})

        LHS = TypedDiGraph()
        LHS.add_node(1, "agent")
        LHS.add_node(2, "agent")

        P = TypedDiGraph()
        P.add_node("a", "agent")
        P.add_node("b", "agent")

        RHS = TypedDiGraph()
        RHS.add_node("x", "agent")

        h1 = Homomorphism(P, LHS, {"a": 1, "b": 2})
        h2 = Homomorphism(P, RHS, {"a": "x", "b": "x"})

        rw = Rewriter(g)
        instances = rw.find_matching(LHS)
        RHS_instance = rw.apply_rule(instances[0], h1, h2)

    def test_rewriting_with_metamodel(self):
        meta_meta = TypedDiGraph()
        meta_meta.add_node("agent", "node")
        meta_meta.add_node("action", "node")
        meta_meta.add_edges_from([("agent", "agent"), ("action", "action"),
                                  ("action", "agent"), ("agent", "action")])

        meta = TypedDiGraph(meta_meta)
        meta.add_node("protein", "agent")
        meta.add_node("region", "agent")
        meta.add_node("action", "agent")
        meta.add_edges_from([
            ("protein", "protein"),
            ("region", "region"),
            ("action", "action"),
            ("region", "protein"),
            ("region", "action"),
            ("action", "region"),
        ])

        graph = TypedDiGraph(meta)
        graph.add_nodes_from([(1, "protein"), (2, "region"), (3, "action"),
                              (4, "region"), (5, "protein"), (6, "region"),
                              (7, "protein")])
        graph.add_edge(2, 1)
        graph.add_edge(2, 3)
        graph.add_edge(4, 3)
        graph.add_edge(4, 5)
        graph.add_edge(6, 3)
        graph.add_edge(6, 7)

        rw = Rewriter(graph)

        LHS = TypedDiGraph()
        LHS.add_node("a", "protein")
        LHS.add_node("b", "region")
        LHS.add_node(33, "action")
        LHS.add_edges_from([("b", "a"), ("b", 33)])
        instances = rw.find_matching(LHS)
        try:
            rw.add_node(instances[0], "new_type", 2)
            assert False
        except:
            assert True
        try:
            rw.add_edge(instances[0], "a", 33)
            assert False
        except:
            assert True
Example #17
0
    def test_init_with_metamodel_directed(self):
        meta_meta = TypedDiGraph()
        meta_meta.add_node("agent", "node")
        meta_meta.add_node("action", "node")
        meta_meta.add_edges_from([("agent", "agent"), ("action", "action"),
                                  ("action", "agent"), ("agent", "action")])

        meta = TypedDiGraph(meta_meta)
        meta.add_node("protein", "agent")
        meta.add_node("region", "agent")
        meta.add_node("action", "agent")
        meta.add_edges_from([
            ("protein", "protein"),
            ("region", "region"),
            ("action", "action"),
            ("region", "protein"),
            ("region", "action"),
            ("action", "region"),
        ])

        graph = TypedDiGraph(meta)
        graph.add_nodes_from([(1, "protein"), (2, "region"), (3, "action"),
                              (4, "region"), (5, "protein"), (6, "region"),
                              (7, "protein")])
        graph.add_edge(2, 1)
        graph.add_edge(2, 3)
        graph.add_edge(4, 3)
        graph.add_edge(4, 5)
        graph.add_edge(6, 3)
        graph.add_edge(6, 7)
Example #18
0
    def test_rewriting_with_metamodel(self):
        meta_meta = TypedDiGraph()
        meta_meta.add_node("agent", "node")
        meta_meta.add_node("action", "node")
        meta_meta.add_edges_from([("agent", "agent"), ("action", "action"),
                                  ("action", "agent"), ("agent", "action")])

        meta = TypedDiGraph(meta_meta)
        meta.add_node("protein", "agent")
        meta.add_node("region", "agent")
        meta.add_node("action", "agent")
        meta.add_edges_from([
            ("protein", "protein"),
            ("region", "region"),
            ("action", "action"),
            ("region", "protein"),
            ("region", "action"),
            ("action", "region"),
        ])

        graph = TypedDiGraph(meta)
        graph.add_nodes_from([(1, "protein"), (2, "region"), (3, "action"),
                              (4, "region"), (5, "protein"), (6, "region"),
                              (7, "protein")])
        graph.add_edge(2, 1)
        graph.add_edge(2, 3)
        graph.add_edge(4, 3)
        graph.add_edge(4, 5)
        graph.add_edge(6, 3)
        graph.add_edge(6, 7)

        rw = Rewriter(graph)

        LHS = TypedDiGraph()
        LHS.add_node("a", "protein")
        LHS.add_node("b", "region")
        LHS.add_node(33, "action")
        LHS.add_edges_from([("b", "a"), ("b", 33)])
        instances = rw.find_matching(LHS)
        try:
            rw.add_node(instances[0], "new_type", 2)
            assert False
        except:
            assert True
        try:
            rw.add_edge(instances[0], "a", 33)
            assert False
        except:
            assert True
Example #19
0
    def test_merging(self):
        g = TypedDiGraph()
        g.add_node(10, "agent", {"a": 0})
        g.add_node(20, "agent", {"b": 0})
        g.add_node(30, "action", {"c": 0})
        g.add_edges_from([(10, 30), (20, 30)])
        g.set_edge(10, 30, {"x": 0})
        g.set_edge(20, 30, {"y": 0})

        LHS = TypedDiGraph()
        LHS.add_node(1, "agent")
        LHS.add_node(2, "agent")

        P = TypedDiGraph()
        P.add_node("a", "agent")
        P.add_node("b", "agent")

        RHS = TypedDiGraph()
        RHS.add_node("x", "agent")

        h1 = Homomorphism(P, LHS, {"a": 1, "b": 2})
        h2 = Homomorphism(P, RHS, {"a": "x", "b": "x"})

        rw = Rewriter(g)
        instances = rw.find_matching(LHS)
        RHS_instance = rw.apply_rule(instances[0], h1, h2)
Example #20
0
    def test_cloning_cases(self):
        g = TypedDiGraph()
        g.add_node(10, "agent", {"a": 0})
        g.add_node(20, "agent", {"b": 0})
        g.add_node(30, "action", {"c": 0})
        g.add_edges_from([(10, 30), (20, 30)])
        g.set_edge(10, 30, {"x": 0})
        g.set_edge(20, 30, {"y": 0})

        LHS = TypedDiGraph()
        LHS.add_node(1, "agent")

        rw = Rewriter(g)
        instances = rw.find_matching(LHS)

        # simple clone
        P1 = TypedDiGraph()
        P1.add_node("a", "agent")
        P1.add_node("b", "agent")

        RHS1 = TypedDiGraph()
        RHS1.add_node("x", "agent")
        RHS1.add_node("y", "agent")

        h1 = Homomorphism(P1, LHS, {"a": 1, "b": 1})
        h2 = Homomorphism(P1, RHS1, {"a": "x", "b": "y"})
        RHS_instance = rw.apply_rule(instances[0], h1, h2)

        # clone merge on the same nodes
        RHS2 = TypedGraph()
        RHS2.add_node("x", "agent")
        h2 = Homomorphism(P1, RHS2, {"a": "x", "b": "x"})
        RHS_instance = rw.apply_rule(instances[0], h1, h2)

        # clone and merge one with other node
        LHS.add_node(2, "agent")
        # update matching
        instances = rw.find_matching(LHS)

        P3 = TypedDiGraph()
        P3.add_node("a", "agent")
        P3.add_node("b", "agent")
        P3.add_node("c", "agent")

        RHS3 = TypedDiGraph()
        RHS3.add_node("x", "agent")
        RHS3.add_node("y", "agent")

        h1 = Homomorphism(P3, LHS, {"a": 1, "b": 1, "c": 2})
        h2 = Homomorphism(P3, RHS3, {"a": "x", "b": "y", "c": "y"})
        RHS_instance = rw.apply_rule(instances[0], h1, h2)
Example #21
0
    def __init__(self):
        graph = TypedDiGraph()
        graph.add_node(1, 'agent', {'name': 'EGFR', 'state': 'p'})
        graph.add_node(2, 'action', attrs={'name': 'BND'})
        graph.add_node(3, 'agent', {'name': 'Grb2', 'aa': 'S', 'loc': 90})
        graph.add_node(4, 'region', attrs={'name': 'SH2'})
        graph.add_node(5, 'agent', attrs={'name': 'EGFR'})
        graph.add_node(6, 'action', attrs={'name': 'BND'})
        graph.add_node(7, 'agent', attrs={'name': 'Grb2'})

        graph.add_node(8, 'agent', attrs={'name': 'WAF1'})
        graph.add_node(9, 'action', {'name': 'BND'})
        graph.add_node(10, 'agent', {'name': 'G1-S/CDK', 'state': 'p'})

        graph.add_node(11, 'agent')
        graph.add_node(12, 'agent')
        graph.add_node(13, 'agent')

        edges = [(1, 2), (4, 2), (4, 3), (5, 6), (7, 6), (8, 9), (10, 9),
                 (11, 12), (12, 11), (12, 13), (13, 12), (11, 13), (13, 11),
                 (5, 2)]

        graph.add_edges_from(edges)

        graph.set_edge(1, 2, {'s': 'p'})
        graph.set_edge(4, 2, {'s': 'u'})
        graph.set_edge(5, 6, {'s': 'p'})
        graph.set_edge(7, 6, {'s': 'u'})
        graph.set_edge(5, 2, {'s': 'u'})

        self.LHS_ = TypedDiGraph()

        self.LHS_.add_node(1, 'agent', {'name': 'EGFR'})
        self.LHS_.add_node(2, 'action', {'name': 'BND'})
        self.LHS_.add_node(3, 'region')
        self.LHS_.add_node(4, 'agent', {'name': 'Grb2'})
        self.LHS_.add_node(5, 'agent', {'name': 'EGFR'})
        self.LHS_.add_node(6, 'action', {'name': 'BND'})
        self.LHS_.add_node(7, 'agent', {'name': 'Grb2'})

        self.LHS_.add_edges_from([(1, 2), (3, 2), (3, 4), (5, 6), (7, 6)])

        self.LHS_.set_edge(1, 2, {'s': 'p'})
        self.LHS_.set_edge(5, 6, {'s': 'p'})

        self.rw_ = Rewriter(graph)
        self.instances_ = self.rw_.find_matching(self.LHS_)