-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstreamlit_utils.py
More file actions
257 lines (205 loc) · 9.18 KB
/
streamlit_utils.py
File metadata and controls
257 lines (205 loc) · 9.18 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# MAVISp - various utilities for MAVISp web server
# Copyright (C) 2022 Matteo Tiberti, Danish Cancer Society
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import streamlit as st
import base64
import os
import pandas as pd
from fsspec.implementations.dirfs import DirFileSystem
from fsspec.implementations.zip import ZipFileSystem
from pathlib import Path
from dot_plot import plot as do_dotplots
from dot_plot import process_input as process_input_for_dotplot
from dot_plot import generate_summary, filter_vep_summary
from lolliplot import process_input as process_input_for_lolliplot
from lolliplot import plot as do_lolliplot
@st.cache_data
def get_base64_of_bin_file(png_file):
with open(png_file, "rb") as f:
data = f.read()
return base64.b64encode(data).decode()
def build_markup_for_logo(
png_file,
background_position="center top",
margin_top="0%",
margin_bottom="10%",
image_width="60%",
image_height="",
):
binary_string = get_base64_of_bin_file(png_file)
return """
<style>
[data-testid="stSidebarNav"] {
background-image: url("data:image/png;base64,%s");
background-repeat: no-repeat;
background-position: %s;
margin-top: %s;
margin-bottom: %s;
background-size: %s %s;
}
[data-testid="stSidebarNav"]::before {
content: " ";
margin-left: 20px;
margin-top: 50px;
margin-bottom: 50px;
font-size: 30px;
position: relative;
top: 100px;
}
</style>
""" % (
binary_string,
background_position,
margin_top,
margin_bottom,
image_width,
image_height,
)
def add_mavisp_logo(png_file, *args, **kwargs):
logo_markup = build_markup_for_logo(png_file, *args, **kwargs)
st.markdown(
logo_markup,
unsafe_allow_html=True,
)
@st.cache_data
def get_database_dir(dir_var_name='MAVISP_DATABASE_PATH', default_dir_name='.'):
dir_name = os.getenv(dir_var_name)
if dir_name is None:
dir_name = default_dir_name
return dir_name
@st.cache_data
def get_database_name(db_var_name='MAVISP_DATABASE_NAME', default_db_name='database'):
db_name = os.getenv(db_var_name)
if db_name is None:
db_name = default_db_name
return db_name
@st.cache_data
def find_database_files(dir):
dfs = []
current_db_name = str(Path(dir) / Path(get_database_name()))
files = map(str, list(Path(dir).glob('*.zip')))
for f in files:
zfs = ZipFileSystem(f)
try:
with zfs.open('dataset_info.csv') as fh:
df = pd.read_csv(fh)
df ['File name'] = f
except KeyError:
continue
print(f, current_db_name)
if f == current_db_name:
df['Date of run'] = f"{df.loc[0, 'Date of run']} (current)"
dfs.append(df)
if len(dfs) > 0:
return pd.concat(dfs)
else:
return None
@st.cache_data
def get_database_filesystem(dir_var_name='MAVISP_DATABASE_PATH',
db_var_name='MAVISP_DATABASE_NAME',
default_dir_name='.',
default_db_name='database'):
dir_name = os.getenv(dir_var_name)
db_name = os.getenv(db_var_name)
if dir_name is None:
dir_name = default_dir_name
if db_name is None:
db_name = default_db_name
db_path = Path(dir_name) / Path(db_name)
if not db_path.exists():
raise FileNotFoundError(f"provided database path {db_path} does not exist")
if db_path.is_file() and db_path.suffix == ".zip":
fs = ZipFileSystem(db_path)
elif db_path.is_dir():
fs = DirFileSystem(db_path)
else:
raise TypeError(f"database must be either a directory or a zip file with .zip extensions. Current database is: {db_path}")
return fs
def add_affiliation_logo():
columns = st.sidebar.columns(2)
with columns[0]:
st.write("""<div style="width:100%;text-align:center;"><a href="https://www.cancer.dk" style="float:center"><img src="app/static/dcs_logo.png" width="60px"></img></a></div>""", unsafe_allow_html=True)
with columns[1]:
st.write("""<div style="width:100%;text-align:center;"><a href="https://www.dtu.dk" style="float:center"><img src="app/static/dtu_logo.png" width="60px"></img></a></div>""", unsafe_allow_html=True)
@st.cache_data
def load_dataset(_data_fs, protein, mode):
with _data_fs.open(os.path.join(mode, 'dataset_tables', f'{protein}-{mode}.csv')) as fh:
return pd.read_csv(fh)
@st.cache_data
def load_main_table(_data_fs, mode):
with _data_fs.open(os.path.join(mode, 'index.csv')) as fh:
return pd.read_csv(fh).sort_values('Protein')
@st.cache_data
def load_clinvar_dict(tsv_file):
clinvar_dict = pd.read_csv(tsv_file,
sep='\t',
header=None,
names=['clinvar', 'internal_category'])
return clinvar_dict.set_index('clinvar')['internal_category'].to_dict()
@st.cache_data
def plot_dotplot(df, demask_co, revel_co, gemme_co, fig_width=14, fig_height=4, n_muts=50, do_revel=False, do_demask=True):
df = df.copy()
if 'ClinVar Interpretation' not in df.columns:
df['ClinVar Interpretation'] = None
clinvar_dict = load_clinvar_dict('mavisp/data/clinvar_interpretation_internal_dictionary.txt')
plot_df, processed_df, full_df, clinvar_mapped_df = process_input_for_dotplot(df,
d_cutoff=demask_co,
r_cutoff=revel_co,
g_cutoff=gemme_co,
residues=None,
mutations=None,
clinvar_dict=clinvar_dict,
plot_Revel=True,
plot_Demask=True,
plot_Source=None,
plot_Clinvar=None,
color_Clinvar=True)
if not do_revel:
processed_df = processed_df.drop(columns=['REVEL'])
my_plots = do_dotplots(plot_df, clinvar_mapped_df, fig_width, fig_height, n_muts, False, True)
return my_plots
@st.cache_data
def process_df_for_lolliplot(df):
df = df.copy()
clinvar_dict = load_clinvar_dict('mavisp/data/clinvar_interpretation_internal_dictionary.txt')
plotting_df, processed_df, full_df, clinvar_mapped_df = process_input_for_dotplot(df,
r_cutoff=0.5,
d_cutoff=0.25,
g_cutoff=3.0,
residues=None,
mutations=None,
clinvar_dict=clinvar_dict,
plot_Revel=False,
plot_Demask=True,
plot_Source=None,
plot_Clinvar=None,
color_Clinvar=False)
text, summary_df = generate_summary(full_df, d_cutoff=0.25, r_cutoff=0.5)
filtered_summary_df = filter_vep_summary(summary_df, processed_df, 'alphamissense', True)
return process_input_for_lolliplot(filtered_summary_df)
@st.cache_data
def plot_lolliplots(df, muts_per_plot=50):
return do_lolliplot(df, muts_per_plot)
@st.cache_data
def get_compact_dataset(this_dataset_table):
default_cols = ['Mutation', 'HGVSp', 'HGVSg', 'Mutation sources']
selected_cols = [c for c in this_dataset_table.columns if "classification" in c ]
return this_dataset_table[default_cols + selected_cols + ['References']]
def replace_boolean_col(df, col, dictionary={True : 'Yes', False : 'No'}):
df[col] = df[col].astype(str)
for k,v in dictionary.items():
k, v = str(k), str(v)
df[col] = df[col].replace(to_replace=k, value=v)
return df