Exemplo n.º 1
0
def test_belgraph_to_signed_graph():
    braf_no_act = Agent('BRAF', db_refs={'HGNC': '1097', 'UP': 'P15056'})
    mek = Agent('MAP2K1', db_refs={'HGNC': '6840', 'UP': 'Q02750'})
    stmt = Activation(braf_no_act, mek)
    hsh = stmt.get_hash(refresh=True)

    pba = pa.PybelAssembler([stmt])
    belgraph = pba.make_model()
    pb_seg = pa.belgraph_to_signed_graph(belgraph, propagate_annotations=True)

    assert len(pb_seg.edges) == 1

    edge = (braf_dsl, map2k1_dsl, 0)
    assert edge in pb_seg.edges

    edge_dict = pb_seg.edges.get(edge)
    assert edge_dict

    assert 'stmt_hash' in edge_dict
    assert isinstance(edge_dict['stmt_hash'], int)
    assert hsh == edge_dict['stmt_hash']

    assert 'uuid' in edge_dict
    assert isinstance(edge_dict['uuid'], str)
    assert stmt.uuid == edge_dict['uuid']

    assert 'belief' in edge_dict
    assert isinstance(edge_dict['belief'], (float, int))
    assert stmt.belief == edge_dict['belief']
Exemplo n.º 2
0
 def get_graph(self):
     """Convert a PyBELGraph to a graph with signed nodes."""
     # This import is done here rather than at the top level to avoid
     # making pybel an implicit dependency of the model checker
     from indra.assemblers.pybel.assembler import belgraph_to_signed_graph
     if self.graph:
         return self.graph
     signed_edges = belgraph_to_signed_graph(self.model)
     self.graph = self.signed_edges_to_signed_nodes(signed_edges)
     return self.graph
Exemplo n.º 3
0
 def get_graph(self,
               include_variants=False,
               symmetric_variant_links=False,
               include_components=True,
               symmetric_component_links=True):
     """Convert a PyBELGraph to a graph with signed nodes."""
     # This import is done here rather than at the top level to avoid
     # making pybel an implicit dependency of the model checker
     from indra.assemblers.pybel.assembler import belgraph_to_signed_graph
     if self.graph:
         return self.graph
     signed_edges = belgraph_to_signed_graph(
         self.model,
         include_variants=include_variants,
         symmetric_variant_links=symmetric_variant_links,
         include_components=include_components,
         symmetric_component_links=symmetric_component_links)
     self.graph = signed_edges_to_signed_nodes(signed_edges)
     return self.graph
Exemplo n.º 4
0
 def get_graph(self,
               include_variants=False,
               symmetric_variant_links=False,
               include_components=True,
               symmetric_component_links=True,
               edge_filter_func=None):
     """Convert a PyBELGraph to a graph with signed nodes."""
     # This import is done here rather than at the top level to avoid
     # making pybel an implicit dependency of the model checker
     from indra.assemblers.pybel.assembler import belgraph_to_signed_graph
     if self.graph:
         return self.graph
     # NOTE edge_filter_func is not currently used in PyBEL
     signed_edges = belgraph_to_signed_graph(
         self.model,
         include_variants=include_variants,
         symmetric_variant_links=symmetric_variant_links,
         include_components=include_components,
         symmetric_component_links=symmetric_component_links,
         propagate_annotations=True)
     self.graph = signed_edges_to_signed_nodes(signed_edges,
                                               copy_edge_data={'belief'})
     self.get_nodes_to_agents()
     return self.graph
Exemplo n.º 5
0
def db_dump_to_pybel_sg(stmts_list=None,
                        pybel_model=None,
                        belief_dump=None,
                        default_belief=0.1,
                        sign_in_edges=False):
    """Create a signed pybel graph from an evidenceless dump from the db

    Parameters
    ----------
    stmts_list : list[indra.statements.Statement]
        Provide a list of statements if they are already loaded. By default
        the latest available pa statements dump is downloaded from s3.
        Default: None.
    pybel_model : pybel.BELGraph
        If provided, skip generating a new pybel model from scratch
    belief_dump : dict
        If provided, reset the belief scores associated with the statements
        supporting the edges.
    default_belief : float
        Only used if belief_dump is provided. When no belief score is
        available, reset to this belief score. Default: 0.1.
    sign_in_edges : bool
        If True, check that all edges are stored with an index corresponding
        to the sign of the edge. Default: False.

    Returns
    -------
    tuple(DiGraph, MultiDiGraph)
    """
    # Get statement dump:
    # Look for latest file on S3 and pickle.loads it
    if pybel_model is None:
        pb_model = _custom_pb_assembly(stmts_list)
    else:
        logger.info('Pybel model provided')
        pb_model = pybel_model

    # If belief dump is provided, reset beliefs to the entries in it
    if belief_dump:
        logger.info('Belief dump provided, resetting belief scores')
        missing_hash = 0
        changed_belief = 0
        no_hash = 0
        logger.info(f'Looking for belief scores among {len(pb_model.edges)} '
                    f'edges')
        for edge in pb_model.edges:
            ed = pb_model.edges[edge]
            if ed and ed.get('stmt_hash'):
                h = ed['stmt_hash']
                if h in belief_dump:
                    ed['belief'] = belief_dump[h]
                    changed_belief += 1
                else:
                    logger.warning(f'No belief found for {h}')
                    ed['belief'] = default_belief
                    missing_hash += 1
            else:
                no_hash += 1
        logger.info(f'{no_hash} edges did not have hashes')
        logger.info(f'{changed_belief} belief scores were changed')
        logger.info(f'{missing_hash} edges did not have a belief entry')

    # Get a signed edge graph
    logger.info('Getting a PyBEL signed edge graph')
    pb_signed_edge_graph = belgraph_to_signed_graph(
        pb_model,
        symmetric_variant_links=True,
        symmetric_component_links=True,
        propagate_annotations=True)

    if sign_in_edges:
        for u, v, ix in pb_signed_edge_graph.edges:
            ed = pb_signed_edge_graph.edges[(u, v, ix)]
            if 'sign' in ed and ix != ed['sign']:
                pb_signed_edge_graph.add_edge(u, v, ed['sign'], **ed)
                pb_signed_edge_graph.remove_edge(u, v, ix)

    # Map hashes to edges
    logger.info('Getting hash to signed edge mapping')
    seg_hash_edge_dict = {}
    for edge in pb_signed_edge_graph.edges:
        if pb_signed_edge_graph.edges[edge].get('stmt_hash'):
            seg_hash_edge_dict[pb_signed_edge_graph.edges[edge]
                               ['stmt_hash']] = edge
    pb_signed_edge_graph.graph['edge_by_hash'] = seg_hash_edge_dict

    # Get the signed node graph
    logger.info('Getting a signed node graph from signed edge graph')
    pb_signed_node_graph = signed_edges_to_signed_nodes(pb_signed_edge_graph,
                                                        copy_edge_data=True)

    # Map hashes to edges for signed nodes
    logger.info('Getting hash to edge mapping')
    sng_hash_edge_dict = {}
    for edge in pb_signed_node_graph.edges:
        if pb_signed_node_graph.edges[edge].get('stmt_hash'):
            sng_hash_edge_dict[pb_signed_node_graph.edges[edge]
                               ['stmt_hash']] = edge
    pb_signed_node_graph.graph['edge_by_hash'] = sng_hash_edge_dict

    logger.info('Done assembling signed edge and signed node PyBEL graphs')
    return pb_signed_edge_graph, pb_signed_node_graph