-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp_plotly.py
179 lines (157 loc) · 6.89 KB
/
app_plotly.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import json
import itertools # itertools.combinations may be useful
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
import scipy
import pandas as pd
import streamlit as st
import pickle
import plotly.graph_objects as go
import plotly.express as px
from st_aggrid import GridOptionsBuilder, AgGrid, GridUpdateMode, DataReturnMode
# Cache the dataframe so it's only loaded once
@st.experimental_memo
def load_data():
G = pickle.load(open('saved_variables/graph.txt','rb'))
lg = pickle.load(open('saved_variables/longest_chain.txt','rb'))
df = pickle.load(open('saved_variables/dataframe.txt','rb')) # big dataframe
df = df.sort_values(by=['Source'])
return G,lg,df
def plotter(G,df,title):
edge_x = []
edge_y = []
for edge in G.edges():
x0, y0 = G.nodes[edge[0]]['pos']
x1, y1 = G.nodes[edge[1]]['pos']
edge_x.append(x0)
edge_x.append(x1)
edge_x.append(None)
edge_y.append(y0)
edge_y.append(y1)
edge_y.append(None)
edge_trace = go.Scatter(
x=edge_x, y=edge_y,
line=dict(width=0.5, color='#888'),
hoverinfo='none',
mode='lines')
node_x = [] # position
node_y = [] # copy number
node_id = [] # node id
node_chr = []
for node in G.nodes():
x, y = G.nodes[node]['pos']
node_x.append(x)
node_y.append(y)
node_id.append(node)
if df[df['Source']==node].Chromosome.values[0] == 'X':
node_chr.append(23)
elif df[df['Source']==node].Chromosome.values[0] == 'Y':
node_chr.append(24)
elif df[df['Source']==node].Chromosome.values[0] == 'M':
node_chr.append(25)
else:
node_chr.append(int(df[df['Source']==node].Chromosome.values[0])) # item() returns numbers as str
unique_values = len(set(df.Chromosome.to_list()))
unique_values = len(set(node_chr))
color_bar_values = [val for val in np.linspace(0, 1, unique_values+1) for _ in range(2)]
discrete_colors = [val for val in px.colors.qualitative.Alphabet for _ in range(2)]
colorscale = [[value, color] for value, color in zip(color_bar_values, discrete_colors[1:])]
colorscale.pop(0)
colorscale.pop(-1)
### Compile hover text for each node
node_text = []
for n,node in enumerate(G.nodes()):
next_node='None' if n==len(G.nodes())-1 else ','.join(str(i) for i in df[df['Source']==node]['Sink'].to_list() if i!='loose')
prev_node='None' if n==0 else ','.join(str(i) for i in df[df['Sink']==node]['Source'].to_list())
if node_chr[n]==23:
node_text.append(f'Prev: {prev_node}<br><b>-Node:{node}-</b><br>Next: {next_node}<br>Chromosome: X<br>CN: {node_y[n]}' )
elif node_chr[n]==24:
node_text.append(f'Prev: {prev_node}<br><b>-Node:{node}-</b><br>Next: {next_node}<br>Chromosome: Y<br>CN: {node_y[n]}')
elif node_chr[n]==25:
node_text.append(f'Prev: {prev_node}<br><b>-Node:{node}-</b><br>Next: {next_node}<br>Chromosome: M<br>CN: {node_y[n]}')
else:
node_text.append(f'Prev: {prev_node}<br><b>-Node:{node}-</b><br>Next: {next_node}<br>Chromosome: {node_chr[n]}<br>CN: {node_y[n]}')
node_trace = go.Scatter(
x=node_x, y=node_y,
mode='markers',
hoverinfo='text',
text=node_text,
marker=dict(
showscale=True,
# colorscale options
#'Greys' | 'YlGnBu' | 'Greens' | 'YlOrRd' | 'Bluered' | 'RdBu' |
#'Reds' | 'Blues' | 'Picnic' | 'Rainbow' | 'Portland' | 'Jet' |
#'Hot' | 'Blackbody' | 'Earth' | 'Electric' | 'Viridis' |
# colorscale=px.colors.qualitative.Light24,
colorscale=colorscale,
reversescale=True,
color=node_chr,
size=10,
symbol=0,
colorbar=dict(
thickness=15,
title='Chromosome Number',
xanchor='left',
titleside='right'
),
line_width=2))
# Can also be assigned later like below
# node_trace.text = node_id
# node_trace.marker.color = node_chr
fig = go.Figure(data=[edge_trace, node_trace],
layout=go.Layout(
title='Network graph of the longest chain',
titlefont_size=16,
showlegend=False,
hovermode='closest',
margin=dict(b=20,l=15,r=5,t=40),
annotations=[ dict(
text=f"Number of nodes: {len(node_y)}",
showarrow=False,
xref="paper", yref="paper",
x=0.1, y=1 ) ],
xaxis=dict(showgrid=True, zeroline=True, showticklabels=True,showline=True,title_text = "Start Point"),
yaxis=dict(showgrid=True, zeroline=True, showticklabels=True,showline=True,title_text = "Copy Number"))
)
return fig
def main():
G,lg,df = load_data()
fig_all = plotter(G,df,'Network graph of all the paths')
fig_longest = plotter(lg,df,'Network graph of the longest chain')
st.plotly_chart(fig_all, use_container_width=True)
st.plotly_chart(fig_longest, use_container_width=True)
with st.sidebar:
# Style and Printing
st.subheader('SV Graph Network Connections')
st.write('M.Unal, MD Anderson Cancer Center')
st.write('January 2023')
st.write('This plot shows the longest stractural variants chain found from a JaBba output file. Networkx is used for the network.')
st.write('Hover through the nodes with your mouse to see the details of each node. Full screen and zooming is also possible on the graph. Moreover, the entire dataset is summarized in the table below.')
st.subheader("All Nodes")
# Prepare data
# AgGrid(df)
gb = GridOptionsBuilder.from_dataframe(df)
gb.configure_pagination(paginationAutoPageSize=True) #Add pagination
gb.configure_side_bar() #Add a sidebar
gb.configure_selection('multiple', use_checkbox=True, groupSelectsChildren="Group checkbox select children") #Enable multi-row selection
gridOptions = gb.build()
grid_response = AgGrid(
df,
gridOptions=gridOptions,
data_return_mode='AS_INPUT',
update_mode='MODEL_CHANGED',
fit_columns_on_grid_load=False,
#theme='blue', #Add theme color to the table
enable_enterprise_modules=True,
height=350,
width='100%',
reload_data=True,
)
df = grid_response['data']
selected = grid_response['selected_rows']
df_f = pd.DataFrame(selected) #Pass the selected rows to a new dataframe df
# Boolean to resize the dataframe, stored as a session state variable
st.dataframe(df_f, use_container_width=False)
if __name__ == '__main__':
main()