-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOrthoHPI_Home.py
More file actions
160 lines (123 loc) · 5.5 KB
/
OrthoHPI_Home.py
File metadata and controls
160 lines (123 loc) · 5.5 KB
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
import utils
import web_utils
import streamlit as st
from streamlit_extras.switch_page_button import switch_page
import pandas as pd
import plotly.express as px
import holoviews as hv
from css import style
from holoviews import opts, dim
hv.extension('bokeh')
st.set_page_config(layout="wide", page_title="OrthoHPI 2.0", menu_items={})
st.session_state.sidebar_state = 'collapsed'
style.load_css()
page = web_utils.show_pages_menu(index=0)
if page == "Predicted Host-parasite PPIs":
switch_page("predicted host-parasite ppis")
elif page == "Predicted PPI structures":
switch_page('interaction structures')
elif page == "About":
switch_page('about')
# Read dataset
config = utils.read_config('config.yml')
predictions = utils.read_parquet_file(input_file='data/predictions.parquet')
predictions['weight'] = predictions['weight'].astype(float)
tissues = utils.read_parquet_file(input_file='data/tissues_cell_types.parquet')
pred_tissues = pd.merge(predictions, tissues.rename({'Gene': 'target'}, axis=1), on='target', how='left')
tissues = None
ontology = utils.read_parquet_file(input_file='data/go_ontology.parquet')
#Initialize variables
df_select = None
net = None
selected_rows = []
selected_terms = []
enrichment_table = None
enrichment = None
def filter_tissues(config, df):
tissue_df = []
mapped_tissues = config['tissues']
for ident in df['taxid1'].unique():
tissues = [mapped_tissues[t].lower() for t in config['parasites'][int(ident)]['tissues']]
aux = df[(df['taxid1']==ident) & (df['Tissue'].isin(tissues))]
tissue_df.append(aux)
tissue_df = pd.concat(tissue_df)
return tissue_df
@st.cache_data
def generate_tissue_cell_type_box(df, config):
aux = df.copy()
aux['Cell type'] = aux['Cell type'].fillna("Not available")
aux = filter_tissues(config, aux)
counts_tissues = aux.groupby(['taxid1', 'Tissue']).count()['taxid2'].reset_index()
counts_tissues = counts_tissues.rename({'taxid2':'edges_tissue'}, axis=1)
counts_cells = aux.groupby(['taxid1', 'Tissue', 'Cell type']).count()['taxid2'].reset_index()
counts_cells = counts_cells.rename({'taxid2':'edges_cell_type'}, axis=1)
aux = pd.merge(aux, counts_tissues, on=['taxid1', 'Tissue'], how='left')
aux = pd.merge(aux, counts_cells, on=['taxid1', 'Tissue', 'Cell type'], how='left')
fig = px.icicle(aux, path=[px.Constant("Parasites"), 'taxid1_label', 'Tissue', 'Cell type'], values='edges_cell_type',
color='edges_cell_type', hover_data=['edges_tissue', 'edges_cell_type', 'taxid1', 'taxid1_label', 'pTPM'],
color_continuous_scale='Burgyl', height=900, width=1200, maxdepth=-1)
return fig
def generate_circos_plot(df_pred):
nodes = set()
links = []
seen = set()
i = 0
for g1, df1 in df_pred.groupby('taxid1_label'):
j = i + 1
for g2, df2 in df_pred.groupby('taxid1_label'):
if g1 != g2 and (g1, g2) not in seen:
nodes.update([(i, g1[0]+'. '+g1.split(' ')[1]), (j, g2[0]+'. '+g2.split(' ')[1])])
links.append((i, j, len(set(df1['target'].tolist()).intersection(df2['target'].tolist()))))
seen.update([(g1, g2), (g2, g1)])
j += 1
i += 1
links = pd.DataFrame(links, columns=['source', 'target', 'value'])
nodes = hv.Dataset(pd.DataFrame(list(nodes), columns = ['index', 'name']), 'index')
chord = hv.Chord((links, nodes)).select(value=(1, None))
chord.opts(
opts.Chord(width=500, height=700, cmap='Category20', edge_cmap='Category20', edge_color=dim('source').str(),
labels='name', node_color=dim('index').str()))
return chord
def generate_boxplot_score_stats(df):
fig = px.box(df.sort_values("taxid1"), x="taxid1_label", y="weight", color='taxid1', labels={"weight":"score", "taxid1_label": "parasites"})
fig.update_traces(showlegend=False)
return fig
def generate_barplot_stats(df):
fig = px.bar(df.groupby(["taxid1_label"]).count().reset_index().sort_values("taxid2"), x="taxid1_label", y="weight", color='taxid1_label', labels={"weight":"count", "taxid1_label": "parasites"})
fig.update_traces(showlegend=False)
return fig
def generate_stats_plots(df):
stats_figures = []
fig = generate_barplot_stats(df)
stats_figures.append((fig, "Number of Interactions"))
fig = generate_boxplot_score_stats(df)
stats_figures.append((fig, "Boxplot of Confidence scores"))
return stats_figures
st.markdown("<h1 style='text-align: center; color: #023858;'>OrthoHPI 2.0</h1>", unsafe_allow_html=True)
st.markdown("<h3 style='text-align: center; color: #2b8cbe;'>Orthology Prediction of Host-Parasite PPI</h3>", unsafe_allow_html=True)
st.text(" ")
st.text(" ")
st.markdown("---")
chart1, chart2 = st.columns(2)
with chart1:
st.subheader("Circos Plot of Common Host Interactors")
circos_plot = generate_circos_plot(predictions)
st.bokeh_chart(hv.render(circos_plot), use_container_width=True)
stats_figs = generate_stats_plots(predictions)
predictions = None
stats_cols = st.columns(len(stats_figs))
i = 0
for stats_fig, title in stats_figs:
with stats_cols[i]:
st.subheader(title)
st.plotly_chart(stats_fig, use_container_width=True)
i += 1
fig = generate_tissue_cell_type_box(pred_tissues, config)
with chart2:
st.subheader("Summary of Interactions per Tissue and Cell type")
st.plotly_chart(fig, use_container_width=True)
pred_tissues = None
st.markdown("---")
# Footer
with st.container():
web_utils.footer()