Exemplo n.º 1
0
def assert_has_edge(self: unittest.TestCase,
                    u: BaseEntity,
                    v: BaseEntity,
                    graph: BELGraph,
                    permissive=True,
                    **kwargs):
    """A helper function for checking if an edge with the given properties is contained within a graph."""
    self.assertIsInstance(u, BaseEntity)
    self.assertIsInstance(v, BaseEntity)

    self.assertTrue(
        graph.has_edge(u, v),
        msg='Edge ({}, {}) not in graph. Other edges:\n{}'.format(
            u, v,
            '\n'.join('{} {} {}'.format(u.as_bel(), d[RELATION], v.as_bel())
                      for u, v, d in graph.edges(data=True))))

    if not kwargs:
        return

    if permissive:
        matches = any_subdict_matches(graph[u][v], kwargs)
    else:
        matches = any_dict_matches(graph[u][v], kwargs)

    msg = 'No edge ({}, {}) with correct properties. expected:\n {}\nbut got:\n{}'.format(
        u, v, dumps(kwargs, indent=2, sort_keys=True), str(graph[u][v]))
    self.assertTrue(matches, msg=msg)
Exemplo n.º 2
0
def assert_has_node(self: unittest.TestCase, node: BaseEntity, graph: BELGraph, **kwargs):
    """Check if a node with the given properties is contained within a graph."""
    self.assertIsInstance(node, BaseEntity)

    self.assertIn(
        node,
        graph,
        msg='{} not found in graph. Other nodes:\n{}'.format(node.as_bel(), '\n'.join(
            n.as_bel()
            for n in graph
        )),
    )

    if kwargs:
        missing = set(kwargs) - set(graph.nodes[node])
        self.assertFalse(missing, msg="Missing {} in node data".format(', '.join(sorted(missing))))
        self.assertTrue(all(kwarg in graph.nodes[node] for kwarg in kwargs),
                        msg="Missing kwarg in node data")
        self.assertEqual(kwargs, {k: graph.nodes[node][k] for k in kwargs},
                         msg="Wrong values in node data")
Exemplo n.º 3
0
def calculate_canonical_cx_identifier(node: BaseEntity) -> str:
    """Calculate the canonical name for a given node.

    If it is a simple node, uses the namespace:name combination. Otherwise, it uses the BEL string.
    """
    if node[FUNCTION] == COMPLEX and NAMESPACE in node:
        return '{}:{}'.format(node[NAMESPACE], node[NAME])

    if VARIANTS in node or FUSION in node or node[FUNCTION] in {
            REACTION, COMPOSITE, COMPLEX
    }:
        return node.as_bel()

    namespace = node[NAMESPACE]
    name = node.get(NAME)
    identifier = node.get(IDENTIFIER)

    if VARIANTS not in node and FUSION not in node:  # this is should be a simple node
        if name:
            return name
        if identifier:
            return '{}:{}'.format(namespace, identifier)

    raise ValueError('Unexpected node data: {}'.format(node))
Exemplo n.º 4
0
def get_triplet_tuple(a: BaseEntity, b: BaseEntity,
                      c: BaseEntity) -> BELTripleTuple:
    """Get the triple as a tuple of BEL/hashes."""
    return a.as_bel(), a.sha512, b.as_bel(), b.sha512, c.as_bel(), c.sha512
Exemplo n.º 5
0
def get_pair_tuple(a: BaseEntity, b: BaseEntity) -> BELPairTuple:
    """Get the pair as a tuple of BEL/hashes."""
    return a.as_bel(), a.sha512, b.as_bel(), b.sha512