In [1]:
import myvariant, mygene, os, plotly, json, textwrap, re, glob, pickle
from IPython.display import IFrame
#from wand.image import Image as WImage
from networkx.readwrite import json_graph
import plotly
plotly.offline.init_notebook_mode()
import plotly.graph_objs as go
from plotly.offline import download_plotlyjs, init_notebook_mode, iplot
from IPython.core.display import display, HTML
from ipywidgets import interact, interactive, fixed,Layout, Button, Box
import ipywidgets as widgets
from collections import OrderedDict
#def out_sad_fig(G):
#plotly plotting function for networkx graphs
def in_json_out_plotly_fig(json_name, make_3D, global_in):
#Load JSON graph
with open(json_name,"r") as json_comm:
json_in = json.load(json_comm)
G = json_graph.node_link_graph(json_in)
width=500
height=500
axis=dict(showbackground=False,
showline=False,
zeroline=False,
showgrid=False,
showticklabels=False,
title="")
if global_in:
layout = go.Layout(
width=240,
height=240,
xaxis=go.XAxis(axis),
yaxis=go.YAxis(axis),
showlegend=False,
scene=go.Scene(
xaxis=go.XAxis(axis),
yaxis=go.YAxis(axis),
zaxis=go.ZAxis(axis)),
margin=go.Margin(l=0,
r=0,
b=0,
t=50),
hovermode="closest")
layout["title"] = "Whole Nucleus"
else:
layout = go.Layout(
width=800,
height=400,
xaxis=go.XAxis(axis),
yaxis=go.YAxis(axis),
showlegend=False,
scene=go.Scene(
xaxis=go.XAxis(axis),
yaxis=go.YAxis(axis),
zaxis=go.ZAxis(axis)),
margin=go.Margin(l=0,
r=0,
b=0,
t=0),
hovermode="closest")
layout["title"] = "<br> <a href=\"" + G.graph["ucsc_session"] + "\"> UCSC Browser View </a>"
#make node trace
#make 3d?
traceN = go.Scatter(x=[], y=[], mode="markers", text=[],marker=go.Marker(color=[],size=[],opacity=[]))
if make_3D:
traceN = go.Scatter3d(x=[], y=[], z=[], mode="markers", text=[],marker=go.Marker(color=[],size=[],opacity=[]))
traceN["name"] = ""
traceN["hoverinfo"] = "text"
for node in G.nodes(data=True):
text_node = ""
if "rsid" in node[1]:
text_node = "rsid: " + node[1]["rsid"] + "\n" + node[1]["name"]
elif "name" in node[1] and "fpkm" in node[1]:
text_node += node[0] + " " + node[1]["name"]
elif "name" in node[1]:
text_node += node[1]["name"]
else:
text_node += node[0]
if "fpkm" in node[1]:
text_node += "\nFPKM: " + str(node[1]["fpkm"])
if "tfbs" in node[1]:
text_node += "\n" + textwrap.fill("TFBS: " + node[1]["tfbs"])
if "cadd" in node[1]:
text_node += "\n" + textwrap.fill("CADD Score: " + str(node[1]["cadd"]))
if "deep_score" in node[1]:
text_node += "\n" + textwrap.fill("DeepBind Delta: " + str(node[1]["deep_score"]))
if "deepbind_tf" in node[1]:
text_node += "\n" + textwrap.fill("DeepBind Top TFs: " + str(node[1]["deepbind_tf"]))
if "grasp_pheno" in node[1]:
text_node += "\n" + textwrap.fill("GWAS Phenotype (GRASP): " + str(node[1]["grasp_pheno"]))
if "grasp_pmid" in node[1]:
text_node += "\n" + textwrap.fill("GWAS PMID (GRASP): " + str(node[1]["grasp_pmid"]))
if "gtex_eqtl_pval" in node[1]:
text_node += "\n" + textwrap.fill("GTEX eQTL P-value: " + str(node[1]["gtex_eqtl_pval"]))
if "sad_abs_sum" in node[1]:
text_node += "\n" + textwrap.fill("Sum of SAD across tissues: " + str(node[1]["sad_abs_sum"]))
traceN["text"].append(text_node.replace("\n","<br>"))
traceN["x"].append(node[1]["x"])
traceN["y"].append(node[1]["y"])
if make_3D:
traceN["z"].append(node[1]["z"])
if "color" in node[1]:
traceN["marker"]["color"].append(node[1]["color"])
else:
traceN["marker"]["color"].append("white")
if "size" in node[1]:
traceN["marker"]["size"].append(node[1]["size"])
else:
traceN["marker"]["size"].append(10)
if "opacity" in node[1]:
traceN["marker"]["opacity"].append(node[1]["opacity"])
else:
traceN["marker"]["opacity"].append(1)
#make edge trace and annotations
traceE = go.Scatter(x=[], y=[], mode="lines", hoverinfo = "none")
traceE_annot = go.Scatter(x=[], y=[], name="", mode="markers", text=[],marker=go.Marker(color=[],size=[],opacity=[]),hoverinfo="text")
if make_3D:
traceE = go.Scatter3d(x=[], y=[], z=[], mode="lines", hoverinfo = "none")
traceE_annot = go.Scatter3d(x=[], y=[], z=[], name="", mode="markers", text=[],marker=go.Marker(color=[],size=[],opacity=[]),hoverinfo="text")
traceE["name"] = ""
traceE["line"]["width"] = 1
for edge in G.edges(data=True):
traceE["x"] += [G.node[edge[0]]["x"],G.node[edge[1]]["x"], None]
traceE["y"] += [G.node[edge[0]]["y"],G.node[edge[1]]["y"], None]
if make_3D:
traceE["z"] += [G.node[edge[0]]["z"],G.node[edge[1]]["z"], None]
text_edge = ""
if "weight" in G.get_edge_data(*edge):
text_edge += "Weight: " + str(G.get_edge_data(*edge)["weight"])
traceE_annot["x"].append((float(G.node[edge[0]]["x"])+float(G.node[edge[1]]["x"]))/2.0)
traceE_annot["y"].append((float(G.node[edge[0]]["y"])+float(G.node[edge[1]]["y"]))/2.0)
if make_3D:
traceE_annot["z"].append((float(G.node[edge[0]]["z"])+float(G.node[edge[1]]["z"]))/2.0)
traceE_annot["marker"]["color"].append("black")
traceE_annot["marker"]["size"].append(5)
if "overlapped_tf" in G.get_edge_data(*edge):
text_edge += "<br>" + textwrap.fill("Deepbind TF overlap: " + str(G.get_edge_data(*edge)["overlapped_tf"]))
if "overlapped_tf_enc" in G.get_edge_data(*edge):
text_edge += "<br>" + textwrap.fill("Encode TF overlap: " + str(G.get_edge_data(*edge)["overlapped_tf_enc"]))
if text_edge:
traceE_annot["text"].append(text_edge)
data = go.Data([traceE, traceN, traceE_annot])
fig = go.Figure(data=data, layout=layout)
plotly.offline.iplot(fig)