src/pybel/io/jgif.py
def to_jgif(graph):
    """Build a JGIF dictionary from a BEL graph.

    :param pybel.BELGraph graph: A BEL graph
    :return: A JGIF dictionary
    :rtype: dict

    .. warning::

        Untested! This format is not general purpose and is therefore time is not heavily invested. If you want to
        use Cytoscape.js, we suggest using :func:`pybel.to_cx` instead.

    The example below shows how to output a BEL graph as a JGIF dictionary.

    .. code-block:: python

        import os
        from pybel.examples import sialic_acid_graph
        graph_jgif_json = pybel.to_jgif(sialic_acid_graph)

    If you want to write the graph directly to a file as JGIF, see func:`to_jgif_file`.
    """
    u_v_r_bel = {}

    nodes_entry = []
    edges_entry = []

    for node in sorted(graph, key=methodcaller('as_bel')):
        nodes_entry.append({
            'id': node.md5,
            'label': node.as_bel(),
            'bel_function_type': node.function,
        })

    for u, v in graph.edges():
        relation_evidences = defaultdict(list)

        for key, data in graph[u][v].items():
            if (u, v, data[RELATION]) not in u_v_r_bel:
                u_v_r_bel[u, v, data[RELATION]] = graph.edge_to_bel(u, v, edge_data=data)

            bel = u_v_r_bel[u, v, data[RELATION]]

            evidence_dict = {
                'bel_statement': bel,
                'key': key,
            }

            if ANNOTATIONS in data:
                evidence_dict['experiment_context'] = data[ANNOTATIONS]

            if EVIDENCE in data:
                evidence_dict['summary_text'] = data[EVIDENCE]

            if CITATION in data:
                evidence_dict['citation'] = data[CITATION]

            relation_evidences[data[RELATION]].append(evidence_dict)

        for relation, evidences in relation_evidences.items():
            edges_entry.append({
                'source': u.md5,
                'target': v.md5,
                'relation': relation,
                'label': u_v_r_bel[u, v, relation],
                'metadata': {
                    'evidences': evidences,
                },
            })

    return {
        'graph': {
            'metadata': dict(
                origin=dict(name='pybel', version=get_version()),
                **graph.document,
            ),
            'nodes': nodes_entry,
            'edges': edges_entry,
        },
    }
Build a JGIF dictionary from a BEL graph.

    :param pybel.BELGraph graph: A BEL graph
    :return: A JGIF dictionary
    :rtype: dict

    .. warning::

        Untested! This format is not general purpose and is therefore time is not heavily invested. If you want to
        use Cytoscape.js, we suggest using :func:`pybel.to_cx` instead.

    The example below shows how to output a BEL graph as a JGIF dictionary.

    .. code-block:: python

        import os
        from pybel.examples import sialic_acid_graph
        graph_jgif_json = pybel.to_jgif(sialic_acid_graph)

    If you want to write the graph directly to a file as JGIF, see func:`to_jgif_file`.
    
Build a JGIF dictionary from a BEL graph.
Build a JGIF dictionary from a BEL graph .
def to_jgif(graph):
    u_v_r_bel = {}
    nodes_entry = []
    edges_entry = []
    for node in sorted(graph, key=methodcaller('as_bel')):
        nodes_entry.append({
            'id': node.md5,
            'label': node.as_bel(),
            'bel_function_type': node.function,
        })
    for u, v in graph.edges():
        relation_evidences = defaultdict(list)
        for key, data in graph[u][v].items():
            if (u, v, data[RELATION]) not in u_v_r_bel:
                u_v_r_bel[u, v, data[RELATION]] = graph.edge_to_bel(u, v, edge_data=data)
            bel = u_v_r_bel[u, v, data[RELATION]]
            evidence_dict = {
                'bel_statement': bel,
                'key': key,
            }
            if ANNOTATIONS in data:
                evidence_dict['experiment_context'] = data[ANNOTATIONS]
            if EVIDENCE in data:
                evidence_dict['summary_text'] = data[EVIDENCE]
            if CITATION in data:
                evidence_dict['citation'] = data[CITATION]
            relation_evidences[data[RELATION]].append(evidence_dict)
        for relation, evidences in relation_evidences.items():
            edges_entry.append({
                'source': u.md5,
                'target': v.md5,
                'relation': relation,
                'label': u_v_r_bel[u, v, relation],
                'metadata': {
                    'evidences': evidences,
                },
            })
    return {
        'graph': {
            'metadata': dict(
                origin=dict(name='pybel', version=get_version()),
                **graph.document,
            ),
            'nodes': nodes_entry,
            'edges': edges_entry,
        },
    }
def to_jgif ( graph ) : u_v_r_bel = { } nodes_entry = [ ] edges_entry = [ ] for node in sorted ( graph , key = methodcaller ( 'as_bel' ) ) : nodes_entry . append ( { 'id' : node . md5 , 'label' : node . as_bel ( ) , 'bel_function_type' : node . function , } ) for u , v in graph . edges ( ) : relation_evidences = defaultdict ( list ) for key , data in graph [ u ] [ v ] . items ( ) : if ( u , v , data [ RELATION ] ) not in u_v_r_bel : u_v_r_bel [ u , v , data [ RELATION ] ] = graph . edge_to_bel ( u , v , edge_data = data ) bel = u_v_r_bel [ u , v , data [ RELATION ] ] evidence_dict = { 'bel_statement' : bel , 'key' : key , } if ANNOTATIONS in data : evidence_dict [ 'experiment_context' ] = data [ ANNOTATIONS ] if EVIDENCE in data : evidence_dict [ 'summary_text' ] = data [ EVIDENCE ] if CITATION in data : evidence_dict [ 'citation' ] = data [ CITATION ] relation_evidences [ data [ RELATION ] ] . append ( evidence_dict ) for relation , evidences in relation_evidences . items ( ) : edges_entry . append ( { 'source' : u . md5 , 'target' : v . md5 , 'relation' : relation , 'label' : u_v_r_bel [ u , v , relation ] , 'metadata' : { 'evidences' : evidences , } , } ) return { 'graph' : { 'metadata' : dict ( origin = dict ( name = 'pybel' , version = get_version ( ) ) , ** graph . document , ) , 'nodes' : nodes_entry , 'edges' : edges_entry , } , }
Build a JGIF dictionary from a BEL graph.
Build a JGIF dictionary from a BEL graph .
"""Build a JGIF dictionary from a BEL graph.

    :param pybel.BELGraph graph: A BEL graph
    :return: A JGIF dictionary
    :rtype: dict

    .. warning::

        Untested! This format is not general purpose and is therefore time is not heavily invested. If you want to
        use Cytoscape.js, we suggest using :func:`pybel.to_cx` instead.

    The example below shows how to output a BEL graph as a JGIF dictionary.

    .. code-block:: python

        import os
        from pybel.examples import sialic_acid_graph
        graph_jgif_json = pybel.to_jgif(sialic_acid_graph)

    If you want to write the graph directly to a file as JGIF, see func:`to_jgif_file`.
    """
A JGIF dictionary
A JGIF dictionary
dict
.. warning::

    Untested! This format is not general purpose and is therefore time is not heavily invested. If you want to
    use Cytoscape.js, we suggest using :func:`pybel.to_cx` instead.

The example below shows how to output a BEL graph as a JGIF dictionary.

.. code-block:: python

    import os
    from pybel.examples import sialic_acid_graph
    graph_jgif_json = pybel.to_jgif(sialic_acid_graph)

If you want to write the graph directly to a file as JGIF, see func:`to_jgif_file`.
graph
A BEL graph
A BEL graph