Exemplo n.º 1
0
def render_graph(graph, add_parent=False, default_color='white'):
    g_copy = graph.copy()

    _scale_edges(g_copy)

    if add_parent:
        g_copy = _add_parent_term(g_copy)
    _set_node_color(g_copy, default_color)
    d = nx_to_json(g_copy)
    u_name = "cy{}".format(uuid.uuid4())
    d['uuid'] = u_name
    d['style_json'] = json.dumps(styles['default'])

    edge_types = set()
    for i, j, data in graph.edges(data=True):
        if 'interactionType' in data:
            for e in data['interactionType'].split('|'):
                edge_types.add(e)

    d['edge_list'] = list(edge_types)

    fname_temp = '{}.html'.format(u_name)
    subgraph_html = env.get_template('subgraph.html')
    template = env.get_template('main_view.html')

    with open(fname_temp, 'w') as f:
        f.write(subgraph_html.render(d))

    display(HTML(template.render(name=u_name, filename=fname_temp)))
Exemplo n.º 2
0
def draw_cyjs(graph, add_parent=False, layout='cose-bilkent',
              bg_color='white', default_node_color='white', **layout_args):
    """ Renders a graph using cytoscape.js

    Parameters
    ----------
    graph : nx.DiGraph
    add_parent : bool
        Group together nodes that share 'termName'.
    layout : str
        Layout
    bg_color
    default_node_color : str
        Will only be used if color is not already set for node
    layout_args : dict
        Any additional arguments for use in cytoscape.js. Examples can be found
        in cyjs_options.py

    Returns
    -------

    """
    # Dont think users need these
    height = 700
    width = 100
    g_copy = graph.copy()
    if add_parent:
        g_copy = _add_parent_term(g_copy)

    _scale_edges(g_copy)
    _set_node_color(g_copy, default_node_color)

    d = nx_to_json(g_copy)
    d['background'] = bg_color
    d['uuid'] = "cy" + str(uuid.uuid4())
    d['widget_width'] = str(width)
    d['widget_height'] = str(height)

    if layout not in layouts:
        raise Exception(
            "layout {} is not in {}".format(layout, layouts.keys())
        )
    else:
        layout_opts = layouts[layout].copy()
        layout_opts.update(layout_args)

    d['layout_json'] = json.dumps(layout_opts)
    d['style_json'] = json.dumps(styles['default'])
    d['fitbutton'] = "fit{}".format(uuid.uuid4())

    template = env.get_template('subgraph_2.html')
    display(HTML(template.render(d)))
Exemplo n.º 3
0
def test_nx_to_json():
    g = nx.DiGraph()
    g.add_edge('a', 'b')
    json_g = exporters.nx_to_json(g)
    answer = {'edges': '[{"data": {"source": "a", "target": "b"}}]',
              'nodes': '[{"data": {"id": "a", "name": "a"}},'
                       ' {"data": {"id": "b", "name": "b"}}]'}
    a_edges = json.loads(json_g['edges'])
    b_edges = json.loads(answer['edges'])
    a_nodes = json.loads(json_g['nodes'])
    b_nodes = json.loads(answer['nodes'])

    ok_(ordered(a_edges) == ordered(b_edges))
    ok_(ordered(a_nodes) == ordered(b_nodes))
Exemplo n.º 4
0
def test_nx_to_json():
    g = nx.DiGraph()
    g.add_edge('a', 'b')
    json_g = exporters.nx_to_json(g)
    answer = {
        'edges':
        '[{"data": {"source": "a", "target": "b"}}]',
        'nodes':
        '[{"data": {"id": "a", "name": "a"}},'
        ' {"data": {"id": "b", "name": "b"}}]'
    }
    a_edges = json.loads(json_g['edges'])
    b_edges = json.loads(answer['edges'])
    a_nodes = json.loads(json_g['nodes'])
    b_nodes = json.loads(answer['nodes'])

    assert ordered(a_edges) == ordered(b_edges)
    assert ordered(a_nodes) == ordered(b_nodes)
Exemplo n.º 5
0
def display_graph(graph,
                  add_parent=False,
                  layout_algorithm='cose-bilkent',
                  background='#FFFFFF',
                  height=700,
                  width=100):
    g_copy = graph.copy()
    if add_parent:
        g_copy = _add_parent_term(g_copy)

    d = nx_to_json(g_copy)
    d['background'] = background
    d['uuid'] = "cy" + str(uuid.uuid4())
    d['widget_width'] = str(width)
    d['widget_height'] = str(height)
    d['layout'] = layout_algorithm
    d['style_json'] = json.dumps(styles['default'])
    d['fitbutton'] = "fit" + str(uuid.uuid4())

    template = env.get_template('subgraph_2.html')
    widget = template.render(d)
    display(HTML(widget))
Exemplo n.º 6
0
def prep_g(sg):
    data = nx_to_json(sg)
    data['edge_list'] = _get_edge_types(sg)
    data['style_json'] = styles['default']
    return data