예제 #1
0
    def test_find_matching(self):
        pattern = NXGraph()
        pattern.add_nodes_from([1, (2, {"a": 1}), 3])
        pattern.add_edges_from([(1, 2), (2, 3)])
        pattern_typing = {1: "circle", 2: "square", 3: "triangle"}

        nxinstances = self.nx_hierarchy.find_matching(graph_id="g1",
                                                      pattern=pattern,
                                                      pattern_typing={
                                                          "g0": pattern_typing,
                                                          "g00": {
                                                              1: "white",
                                                              2: "white",
                                                              3: "black"
                                                          }
                                                      })
        if self.neo4j_hierarchy:
            n4instances = self.neo4j_hierarchy.find_matching(
                graph_id="g1",
                pattern=pattern,
                pattern_typing={
                    "g0": pattern_typing,
                    "g00": {
                        1: "white",
                        2: "white",
                        3: "black"
                    }
                })

        assert (len(nxinstances) == 1)
        if self.neo4j_hierarchy:
            assert (n4instances == nxinstances)
예제 #2
0
    def test_rewrite(self):
        pattern = NXGraph()
        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 = NXGraph()
        add_nodes_from(p, [(1, {'state': 'p'}), (2, {'name': 'BND'}), 3, 4])
        p.add_edges_from([(1, 2), (3, 4)])

        rhs = NXGraph()
        add_nodes_from(rhs, [(1, {
            'state': 'p'
        }), (2, {
            'name': 'BND'
        }), (3, {
            'merged': 'yes'
        }), (4, {
            'new': 'yes'
        })])

        add_edges_from(rhs, [(1, 2, {
            's': 'u'
        }), (2, 4), (3, 3), (3, 4, {
            'from': 'merged'
        })])

        p_lhs = {1: 1, 2: 2, 3: 3, 4: 4}
        p_rhs = {1: 1, 2: 2, 3: 3, 4: 3}

        rule = Rule(p, pattern, rhs, p_lhs, p_rhs)
        # instances = find_matching_with_types(self.graph, rule.lhs, {}, {}, {})
        instances = self.graph.find_matching(rule.lhs)
        self.graph.rewrite(rule, instances[0])
예제 #3
0
    def test_pushout_symmetry_directed(self):

        A = NXGraph()
        A.add_nodes_from(["a", "b"])
        A.add_edges_from([("a", "b")])

        B = NXGraph()
        B.add_nodes_from([1, 2, 3])
        B.add_edges_from([(2, 3), (3, 2), (1, 3)])

        C = NXGraph()
        C.add_nodes_from(["x", "y"])
        C.add_edges_from([("x", "x"), ("x", "y")])

        homAB = {"a": 2, "b": 3}
        homAC = {"a": "x", "b": "x"}

        D, homBD, homCD = pushout(
            A, B, C, homAB, homAC
        )
        D_inv, homCD_inv, homBD_inv = pushout(
            A, C, B, homAC, homAB
        )
        assert_equals(len(D.nodes()), len(D_inv.nodes()))
        assert_equals(len(D.edges()), len(D_inv.edges()))
예제 #4
0
    def test_rewrite(self):
        pattern = NXGraph()
        pattern.add_nodes_from([1, (2, {"a": {1, 2}}), 3])
        pattern.add_edges_from([(1, 2), (2, 3)])
        lhs_typing = {
            "g0": {
                1: "circle",
                2: "square",
                3: "triangle"
            },
            "g00": {
                1: "white",
                2: "white",
                3: "black"
            }
        }

        p = NXGraph()
        p.add_nodes_from([1, 2, 3])
        p.add_edges_from([(2, 3)])

        rhs = NXGraph()
        rhs.add_nodes_from([1, (2, {"a": {3, 5}}), (3, {"new_attrs": {1}}), 4])
        rhs.add_edges_from([(2, 1, {
            "new_attrs": {2}
        }), (2, 4, {
            "new_attrs": {3}
        }), (2, 3, {
            "new_attrs": {4}
        })])
        p_lhs = {1: 1, 2: 2, 3: 3}
        p_rhs = {1: 1, 2: 2, 3: 3}

        rule = Rule(p, pattern, rhs, p_lhs, p_rhs)
        rhs_typing = {
            "g0": {
                1: "circle",
                2: "square",
                3: "triangle",
                4: "triangle"
            },
            "g00": {
                1: "white",
                2: "white",
                3: "black",
                4: "black"
            }
        }

        instances = self.nx_hierarchy.find_matching("g1",
                                                    pattern,
                                                    pattern_typing=lhs_typing)

        old_g0_nodes = self.nx_hierarchy.get_graph("g0").nodes(data=True)
        old_g0_edges = self.nx_hierarchy.get_graph("g0").edges(data=True)
        old_g00_nodes = self.nx_hierarchy.get_graph("g00").nodes(data=True)
        old_g00_edges = self.nx_hierarchy.get_graph("g00").edges(data=True)

        self.nx_hierarchy.rewrite("g1",
                                  rule,
                                  instances[0],
                                  rhs_typing=rhs_typing)

        assert (set([n[0] for n in old_g0_nodes
                     ]) == set(self.nx_hierarchy.get_graph("g0").nodes()))
        assert (set([(n[0], n[1]) for n in old_g0_edges
                     ]) == set(self.nx_hierarchy.get_graph("g0").edges()))
        assert (set([n[0] for n in old_g00_nodes
                     ]) == set(self.nx_hierarchy.get_graph("g00").nodes()))
        assert (set([(n[0], n[1]) for n in old_g00_edges
                     ]) == set(self.nx_hierarchy.get_graph("g00").edges()))

        for n, attrs in old_g0_nodes:
            assert (self.nx_hierarchy.get_graph("g0").get_node(n) == attrs)

        for s, t, attrs in old_g0_edges:
            assert (self.nx_hierarchy.get_graph("g0").get_edge(s, t) == attrs)

        for n, attrs in old_g00_nodes:
            assert (self.nx_hierarchy.get_graph("g00").get_node(n) == attrs)

        for s, t, attrs in old_g00_edges:
            assert (self.nx_hierarchy.get_graph("g00").get_edge(s, t) == attrs)

        if self.neo4j_hierarchy:
            instances = self.neo4j_hierarchy.find_matching(
                "g1", pattern, pattern_typing=lhs_typing)

        if self.neo4j_hierarchy:
            old_g0_nodes = self.neo4j_hierarchy.get_graph("g0").nodes(
                data=True)
            old_g0_edges = self.neo4j_hierarchy.get_graph("g0").edges(
                data=True)
            old_g00_nodes = self.neo4j_hierarchy.get_graph("g00").nodes(
                data=True)
            old_g00_edges = self.neo4j_hierarchy.get_graph("g00").edges(
                data=True)
            self.neo4j_hierarchy.rewrite("g1",
                                         rule,
                                         instances[0],
                                         rhs_typing=rhs_typing)

            assert (set([n[0] for n in old_g0_nodes]) == set(
                self.neo4j_hierarchy.get_graph("g0").nodes()))
            assert (set([
                (n[0], n[1]) for n in old_g0_edges
            ]) == set(self.neo4j_hierarchy.get_graph("g0").edges()))
            assert (set([n[0] for n in old_g00_nodes]) == set(
                self.neo4j_hierarchy.get_graph("g00").nodes()))
            assert (set([
                (n[0], n[1]) for n in old_g00_edges
            ]) == set(self.neo4j_hierarchy.get_graph("g00").edges()))

            for n, attrs in old_g0_nodes:
                assert (
                    self.neo4j_hierarchy.get_graph("g0").get_node(n) == attrs)

            for s, t, attrs in old_g0_edges:
                assert (self.neo4j_hierarchy.get_graph("g0").get_edge(
                    s, t) == attrs)

            for n, attrs in old_g00_nodes:
                assert (
                    self.neo4j_hierarchy.get_graph("g00").get_node(n) == attrs)

            for s, t, attrs in old_g00_edges:
                assert (self.neo4j_hierarchy.get_graph("g00").get_edge(
                    s, t) == attrs)
예제 #5
0
    def __init__(self):
        """Initialize hierarchies."""
        self.nx_hierarchy = NXHierarchy()
        try:
            self.neo4j_hierarchy = Neo4jHierarchy(uri="bolt://localhost:7687",
                                                  user="******",
                                                  password="******")
            self.neo4j_hierarchy._clear()
        except:
            warnings.warn("Neo4j is down, skipping Neo4j-related tests")
            self.neo4j_hierarchy = None

        g0 = NXGraph()
        g0.add_node("circle", {"a": {1, 2, 3}})
        g0.add_node("square", {"a": {1, 2, 3, 5}})
        g0.add_node("triangle", {"new_attrs": {1}})
        g0.add_edges_from([
            ("circle", "circle"),  # , {"b": {1, 2, 3, 4}}),
            ("circle", "square"),
            ("square", "circle", {
                "new_attrs": {2}
            }),
            ("square", "triangle", {
                "new_attrs": {3, 4}
            })
        ])
        self.nx_hierarchy.add_graph("g0", g0, {"name": "Shapes"})
        if self.neo4j_hierarchy:
            self.neo4j_hierarchy.add_graph("g0", g0, {"name": "Shapes"})

        g00 = NXGraph()
        g00.add_node('black', {"a": {1, 2, 3}, "new_attrs": {1}})
        g00.add_node('white', {"a": {1, 2, 3, 5}})
        g00.add_edges_from([('white', 'white', {
            "new_attrs": 2
        }), ('white', 'black', {
            "new_attrs": {4, 3}
        }), ('black', 'black'), ('black', 'white')])
        self.nx_hierarchy.add_graph("g00", g00, {"name": "Colors"})
        if self.neo4j_hierarchy:
            self.neo4j_hierarchy.add_graph("g00", g00, {"name": "Colors"})

        g1 = NXGraph()
        g1.add_nodes_from([("black_circle", {
            "a": {1, 2, 3}
        }), "white_circle", "black_square", ("white_square", {
            "a": {1, 2}
        }), "black_triangle", "white_triangle"])

        g1.add_edges_from([
            ("black_circle", "black_circle"),  # {"b": {1, 2, 3, 4}}),
            ("black_circle", "white_circle"),
            ("black_circle", "black_square"),
            ("white_circle", "black_circle"),
            ("white_circle", "white_square"),
            ("black_square", "black_circle"),
            ("black_square", "black_triangle"),
            ("black_square", "white_triangle"),
            ("white_square", "white_circle"),
            ("white_square", "black_triangle"),
            ("white_square", "white_triangle")
        ])

        self.nx_hierarchy.add_graph("g1", g1)
        if self.neo4j_hierarchy:
            self.neo4j_hierarchy.add_graph("g1", g1)
        self.nx_hierarchy.add_typing(
            "g1", "g0", {
                "black_circle": "circle",
                "white_circle": "circle",
                "black_square": "square",
                "white_square": "square",
                "black_triangle": "triangle",
                "white_triangle": "triangle"
            })
        if self.neo4j_hierarchy:
            self.neo4j_hierarchy.add_typing(
                "g1", "g0", {
                    "black_circle": "circle",
                    "white_circle": "circle",
                    "black_square": "square",
                    "white_square": "square",
                    "black_triangle": "triangle",
                    "white_triangle": "triangle"
                })

        self.nx_hierarchy.add_typing(
            "g1", "g00", {
                "black_square": "black",
                "black_circle": "black",
                "black_triangle": "black",
                "white_square": "white",
                "white_circle": "white",
                "white_triangle": "white"
            })
        if self.neo4j_hierarchy:
            self.neo4j_hierarchy.add_typing(
                "g1", "g00", {
                    "black_square": "black",
                    "black_circle": "black",
                    "black_triangle": "black",
                    "white_square": "white",
                    "white_circle": "white",
                    "white_triangle": "white"
                })

        g2 = NXGraph()
        g2.add_nodes_from([
            (1, {
                "a": {1, 2}
            }),
            2,
            3,
            4,
            (5, {
                "a": {1}
            }),
            6,
            7,
        ])

        g2.add_edges_from([
            (1, 2),  # {"b": {1, 2, 3}}),
            (2, 3),
            (3, 6),
            (3, 7),
            (4, 2),
            (4, 5),
            (5, 7)
        ])
        self.nx_hierarchy.add_graph("g2", g2)
        if self.neo4j_hierarchy:
            self.neo4j_hierarchy.add_graph("g2", g2)
        self.nx_hierarchy.add_typing(
            "g2", "g1", {
                1: "black_circle",
                2: "black_circle",
                3: "black_square",
                4: "white_circle",
                5: "white_square",
                6: "white_triangle",
                7: "black_triangle"
            })
        if self.neo4j_hierarchy:
            self.neo4j_hierarchy.add_typing(
                "g2", "g1", {
                    1: "black_circle",
                    2: "black_circle",
                    3: "black_square",
                    4: "white_circle",
                    5: "white_square",
                    6: "white_triangle",
                    7: "black_triangle"
                })

        g3 = NXGraph()
        g3.add_nodes_from([
            (1),  # {"a": {1, 2}}),
            2,
            3,
            5,
            (4),  # {"a": {1}}),
            6,
            7,
        ])

        g3.add_edges_from([
            (1, 1),  # , {"b": {1, 2, 3}}),
            (1, 2),
            (1, 3),
            (1, 5),
            (2, 1),
            (3, 4),
            (4, 7),
            (4, 6),
            (5, 6),
            (5, 7)
        ])
        self.nx_hierarchy.add_graph("g3", g3)
        if self.neo4j_hierarchy:
            self.neo4j_hierarchy.add_graph("g3", g3)
        self.nx_hierarchy.add_typing(
            "g3", "g1", {
                1: "black_circle",
                2: "white_circle",
                3: "white_circle",
                5: "black_square",
                4: "white_square",
                6: "white_triangle",
                7: "black_triangle"
            })
        if self.neo4j_hierarchy:
            self.neo4j_hierarchy.add_typing(
                "g3", "g1", {
                    1: "black_circle",
                    2: "white_circle",
                    3: "white_circle",
                    5: "black_square",
                    4: "white_square",
                    6: "white_triangle",
                    7: "black_triangle"
                })

        g4 = NXGraph()
        g4.add_nodes_from([1, 2, 3])
        g4.add_edges_from([(1, 2), (2, 3)])

        self.nx_hierarchy.add_graph("g4", g4)
        self.nx_hierarchy.add_typing("g4", "g2", {1: 2, 2: 3, 3: 6})
        self.nx_hierarchy.add_typing("g4", "g3", {1: 1, 2: 5, 3: 6})
        if self.neo4j_hierarchy:
            self.neo4j_hierarchy.add_graph("g4", g4)
            self.neo4j_hierarchy.add_typing("g4", "g2", {1: 2, 2: 3, 3: 6})
            self.neo4j_hierarchy.add_typing("g4", "g3", {1: 1, 2: 5, 3: 6})

        g5 = NXGraph()
        g5.add_nodes_from([
            ("black_circle"),  # {"a": {255}}),
            ("black_square"),  # {"a": {256}}),
            ("white_triangle"),  # {"a": {257}}),
            ("star")  # , {"a": {258}})
        ])
        g5.add_edges_from([
            ("black_circle", "black_square"),
            ("black_square", "white_triangle"),  # , {"b": {11}}),
            ("star", "black_square"),
            ("star", "white_triangle")
        ])

        self.nx_hierarchy.add_graph("g5", g5)
        if self.neo4j_hierarchy:
            self.neo4j_hierarchy.add_graph("g5", g5)
예제 #6
0
import itertools
import copy
import warnings

from regraph import NXGraph

from kamiql.parser import parse_query

from kami.resources.metamodels import meta_model_base_typing, meta_model
from kami.exceptions import KamiQLError, KamiQLWarning


# Global variables used to query nuggets and the action graph
META_MODEL = NXGraph()
META_MODEL.add_nodes_from(meta_model["nodes"])
META_MODEL.add_edges_from(meta_model["edges"])
META_TYPES = list(meta_model_base_typing.keys())
GENERIC_TYPES = list(set(meta_model_base_typing.values()))
GENERIC_TYPE_EXTENSION = {
    "component": ["protoform", "region", "site", "residue"],
    "interaction": ["bnd", "mod"],
    "state": ["state"]
}


def isinteraction(types):
    for t in types:
        if t in ["mod", "bnd"]:
            return True
    return False
예제 #7
0
    def __init__(self):
        """Initialize test object."""
        self.nx_graph = NXGraph()
        try:
            self.neo4j_graph = Neo4jGraph(uri="bolt://localhost:7687",
                                          user="******",
                                          password="******")
            self.neo4j_graph._clear()
        except:
            warnings.warn("Neo4j is down, skipping Neo4j-related tests")
            self.neo4j_graph = None

        node_list = [("b", {
            "name": "Bob",
            "age": 20
        }), ("a", {
            "name": "Alice",
            "age": 35
        }), ("d", {
            "name": "dummy"
        })]

        edge_list = [("a", "b", {
            "type": "friends",
            "since": 1999
        }), ("d", "a"), ("b", "d")]

        self.nx_graph.add_nodes_from(node_list)
        self.nx_graph.add_edges_from(edge_list)

        if self.neo4j_graph:
            self.neo4j_graph.add_nodes_from(node_list)
            self.neo4j_graph.add_edges_from(edge_list)

        node = "c"
        attrs = {"name": "Claire", "age": 66}
        self.nx_graph.add_node(node, attrs)
        if self.neo4j_graph:
            self.neo4j_graph.add_node(node, attrs)
        edge_attrs = {"type": "parent"}
        self.nx_graph.add_edge("c", "b", edge_attrs)
        if self.neo4j_graph:
            self.neo4j_graph.add_edge("c", "b", edge_attrs)

        self.nx_graph.remove_edge("d", "a")
        if self.neo4j_graph:
            self.neo4j_graph.remove_edge("d", "a")
        self.nx_graph.remove_node("d")
        if self.neo4j_graph:
            self.neo4j_graph.remove_node("d")

        self.nx_graph.update_node_attrs("a", {"name": "Alison"})
        if self.neo4j_graph:
            self.neo4j_graph.update_node_attrs("a", {"name": "Alison"})
        self.nx_graph.update_edge_attrs("a", "b", {"type": "enemies"})
        if self.neo4j_graph:
            self.neo4j_graph.update_edge_attrs("a", "b", {"type": "enemies"})

        self.nx_graph.set_node_attrs("a", {"age": 19}, update=False)
        if self.neo4j_graph:
            self.neo4j_graph.set_node_attrs("a", {"age": 19}, update=False)
        self.nx_graph.set_edge_attrs("a", "b", {"since": 1945}, update=False)
        if self.neo4j_graph:
            self.neo4j_graph.set_edge_attrs("a",
                                            "b", {"since": 1945},
                                            update=False)

        self.nx_graph.add_node_attrs("a", {"gender": {"M", "F"}})
        if self.neo4j_graph:
            self.neo4j_graph.add_node_attrs("a", {"gender": {"M", "F"}})
        self.nx_graph.add_edge_attrs("a", "b", {"probability": 0.5})
        if self.neo4j_graph:
            self.neo4j_graph.add_edge_attrs("a", "b", {"probability": 0.5})

        self.nx_graph.remove_node_attrs("a", {"gender": "F"})
        if self.neo4j_graph:
            self.neo4j_graph.remove_node_attrs("a", {"gender": "F"})
        self.nx_graph.remove_edge_attrs("a", "b", {"probability": 0.5})
        if self.neo4j_graph:
            self.neo4j_graph.remove_edge_attrs("a", "b", {"probability": 0.5})

        clone_id = self.nx_graph.clone_node("b", "b_clone")
        if self.neo4j_graph:
            self.neo4j_graph.clone_node("b", "b_clone")

        # Test relabeling
        self.nx_graph.relabel_node("b", "baba")
        if self.neo4j_graph:
            self.neo4j_graph.relabel_node("b", "baba")
        self.nx_graph.relabel_node("baba", "b")
        if self.neo4j_graph:
            self.neo4j_graph.relabel_node("baba", "b")
        self.nx_graph.relabel_nodes({
            clone_id: "lala",
            "b": "b1",
            "a": "a1",
            "c": "c1"
        })
        if self.neo4j_graph:
            self.neo4j_graph.relabel_nodes({
                clone_id: "lala",
                "b": "b1",
                "a": "a1",
                "c": "c1"
            })
        self.nx_graph.relabel_nodes({
            "b1": "b",
            "a1": "a",
            "c1": "c",
            "lala": clone_id
        })
        if self.neo4j_graph:
            self.neo4j_graph.relabel_nodes({
                "b1": "b",
                "a1": "a",
                "c1": "c",
                "lala": clone_id
            })

        self.nx_graph.merge_nodes(["b", "c"])
        if self.neo4j_graph:
            self.neo4j_graph.merge_nodes(["b", "c"])

        self.nx_graph.copy_node("a", "a_copy")
        if self.neo4j_graph:
            self.neo4j_graph.copy_node("a", "a_copy")

        # Test find matching
        pattern = NXGraph()
        pattern.add_nodes_from(["x", ("y", {"name": "Claire"}), "z"])
        pattern.add_edges_from([("x", "y"), ("y", "y"), ("y", "z")])
        instances1 = self.nx_graph.find_matching(pattern)
        if self.neo4j_graph:
            instances2 = self.neo4j_graph.find_matching(pattern)
            assert (instances1 == instances2)

        rule = Rule.from_transform(pattern)
        p_n, r_n = rule.inject_clone_node("y")
        rule.inject_remove_edge(p_n, "y")
        rule.inject_remove_edge("y", "y")
        rule.inject_add_node("w", {"name": "Frank"})
        rule.inject_add_edge("w", r_n, {"type": "parent"})
        rhs_g1 = self.nx_graph.rewrite(rule, instances1[0])
        if self.neo4j_graph:
            rhs_g2 = self.neo4j_graph.rewrite(rule, instances1[0])

        self.nx_graph.relabel_node(rhs_g1[r_n], "b")
        if self.neo4j_graph:
            self.neo4j_graph.relabel_node(rhs_g2[r_n], "b")
        self.nx_graph.relabel_node(rhs_g1["y"], "c")
        if self.neo4j_graph:
            self.neo4j_graph.relabel_node(rhs_g2["y"], "c")
            # Test the two obtained graphs are the same
            assert (self.nx_graph == self.neo4j_graph)
            assert (set(self.nx_graph.predecessors("b")) == set(
                self.neo4j_graph.predecessors("b")))
            assert (set(self.nx_graph.successors("a")) == set(
                self.neo4j_graph.successors("a")))
            assert (
                self.nx_graph.get_node("c") == self.neo4j_graph.get_node("c"))
            assert (self.nx_graph.get_edge("c",
                                           "b") == self.neo4j_graph.get_edge(
                                               "c", "b"))