Ejemplo n.º 1
0
 def test_two(self):
     text = "((1, 2), 3);"
     enewick_graph = enewick_to_digraph(text)
     clusters = [(1, 2), (3,)]
     gold = construct(clusters)
     GM = GraphMatcher(enewick_graph, gold)
     self.assertTrue(GM.is_isomorphic())
Ejemplo n.º 2
0
 def test_one(self):
     text = "(1, 2);"
     enewick_graph = enewick_to_digraph(text)
     clusters = [('1', '2'),]
     gold = construct(clusters)
     GM = GraphMatcher(enewick_graph, gold)
     self.assertTrue(GM.is_isomorphic())
Ejemplo n.º 3
0
 def test_five(self):
     text = "((1, (2)h#H1)x,(h#H1,3)y)r;"
     clusters = [(1, 2), (2, 3)]
     enewick_graph = enewick_to_digraph(text)
     self.assertEqual(len(enewick_graph.nodes()), 7)
     gold = construct(clusters)
     GM = GraphMatcher(enewick_graph, gold)
     self.assertTrue(GM.is_isomorphic())
Ejemplo n.º 4
0
    def test_three(self):
        text = "((4, 5#1)2, (#1, 6)3);"
        enewick_graph = enewick_to_digraph(text)
        self.assertEqual(len(enewick_graph.nodes()), 6)
        enewick_graph = calc_hybrid(enewick_graph)
        leafs = get_leaf_nodes(enewick_graph)
        gold_leafs = ['1', '4', '6', ]
        self.assertItemsEqual(leafs, gold_leafs)

        clusters = ["1,2", "2,3"]
        gold = construct(clusters)
        g = enewick_to_phylonet(text)
        GM = GraphMatcher(g, gold)
        self.assertTrue(GM.is_isomorphic())
Ejemplo n.º 5
0
    def test_two(self):
        text = "((4, 5#1)2, (#1, 6)3);"
        enewick_graph = enewick_to_digraph(text)
        self.assertEqual(len(enewick_graph.nodes()), 6)

        enewick_phylo = enewick_to_phylonet(text)
        gold_hard = "1,4 1,4,6 1,6 4 6".split()
        hard = cluster_hard(enewick_graph)
        self.assertItemsEqual(hard, gold_hard)
        self.assertEqual(len(enewick_phylo.nodes()), 7)

        clusters = [(1, 2), (2, 3),]
        gold = construct(clusters)
        GM = GraphMatcher(enewick_phylo, gold)
        self.assertTrue(GM.is_isomorphic())
Ejemplo n.º 6
0
 def test_one(self):
     text = "((MOUSE,(HUMAN,RAT)),CIOIN);"
     enewick_graph = enewick_to_digraph(text)
     self.assertEqual(len(enewick_graph.nodes()), 7)
     enewick_graph = enewick_to_phylonet(text)
     self.assertEqual(len(enewick_graph.nodes()), 7)
     clusters = [(1, 2, 3), (2, 3), (4,)]
     gold = construct(clusters)
     GM = GraphMatcher(enewick_graph, gold)
     self.assertTrue(GM.is_isomorphic())
     gold_hard = [
          'CIOIN,HUMAN,MOUSE,RAT',
          'CIOIN', 'HUMAN', 'MOUSE', 'RAT',
          'HUMAN,MOUSE,RAT',
          'HUMAN,RAT',
         ]
     hard = cluster_hard(enewick_graph)
     self.assertEqual(len(hard), 7)
     self.assertItemsEqual(hard, gold_hard)
Ejemplo n.º 7
0
def hypergraphs_are_equivalent(graph1, graph2, isomorphy=True):
    if graph1 is None or graph2 is None:
        return None
    graph1 = put_parent_node_first(graph1)
    graph2 = put_parent_node_first(graph2)
    if len(graph1) != len(graph2):
        return None

    # index_cache_1 = {}
    # index_cache_2 = {}
    if not isomorphy:
        # node type must match as well as node ordering, nx is just needed to check that the edges also align
        edge2nodes1 = graph1.node_ids_by_edge_id()
        edge2nodes2 = graph2.node_ids_by_edge_id()

        def other_node1(node_id, edge_id):
            nodes = edge2nodes1[edge_id]
            tmp = [n for n in nodes if n != node_id]
            assert len(tmp) == 1, "This node is not connected to this edge!"
            return tmp[0]

        def other_node2(node_id, edge_id):
            nodes = edge2nodes2[edge_id]
            tmp = [n for n in nodes if n != node_id]
            assert len(tmp) == 1, "This node is not connected to this edge!"
            return tmp[0]

        mapping = {
            id1: id2
            for id1, id2 in zip(graph1.node.keys(), graph2.node.keys())
        }

        for i, (node1_id, node1), (node2_id,
                                   node2) in zip(range(len(graph1)),
                                                 graph1.node.items(),
                                                 graph2.node.items()):
            if not (graph1.is_parent_node(node1)
                    == graph2.is_parent_node(node2)):
                return None
            elif str(node1) != str(node2) or len(node1.edges) != len(
                    node2.edges):
                return None
            else:
                for edge1_id, edge2_id in zip(node1.edge_ids, node2.edge_ids):
                    edge1 = graph1.edges[edge1_id]
                    edge2 = graph2.edges[edge2_id]
                    if edge1.type != edge2.type or edge1.data != edge2.data:
                        return None
                    # now test that matching edges lead to matching nodes
                    candidate_node_id1 = other_node1(node1_id, edge1_id)
                    candidate_node_id2 = other_node2(node2_id, edge2_id)
                    if mapping[candidate_node_id1] != candidate_node_id2:
                        return None

        return mapping

    else:

        def nodes_match(node1, node2):
            # parent nodes must be aligned
            if not graph1.is_parent_node(
                    node1['node']) == graph2.is_parent_node(node2['node']):
                return False
            # and for all nodes the content must match as well as the ordering
            return str(node1['node']) == str(node2['node'])

        def edges_match(edge1, edge2):
            return edge1['data'].type == edge2['data'].type and edge1[
                'data'].data == edge2['data'].data

        from networkx.algorithms.isomorphism import GraphMatcher
        graph1_nx = graph1.to_nx()
        graph2_nx = graph2.to_nx()
        GM = GraphMatcher(graph1_nx,
                          graph2_nx,
                          edge_match=edges_match,
                          node_match=nodes_match)

        if GM.is_isomorphic():
            # assert str(graph1) == str(graph2)
            # for id1, id2
            # test = {id1: id2 for id1, id2 in zip(graph1.node.keys(), graph2.node.keys())}
            # for id1 in test:
            #     if test[id1] != GM.mapping[id1]:
            #         print("what?")
            assert graph1.parent_node_id is None or GM.mapping[
                graph1.parent_node_id] == graph2.parent_node_id
            return GM.mapping
        else:
            return None