-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgene_exp_viewer_dbms.py
More file actions
354 lines (242 loc) · 11.3 KB
/
gene_exp_viewer_dbms.py
File metadata and controls
354 lines (242 loc) · 11.3 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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
import pandas as pd
import plotly.graph_objects as go
from sqlalchemy import create_engine, MetaData, Table, select,inspect,column,text
import streamlit as st
import math
import numpy as np
from authenticator import authenticate
#layout
st.set_page_config(layout="wide")
# Custom CSS to hide "Made with Streamlit" and "About" in the hamburger menu
hide_streamlit_style = """
<style>
.stDeployButton {
visibility: hidden;
}
[data-testid="stHeader"] {
visibility: hidden;
}
footer {visibility: hidden;}
.stApp { margin-top: -70px; } /* Adjust the top margin */
</style>
"""
st.markdown(hide_streamlit_style, unsafe_allow_html=True)
# Define your database connection details
db_url = 'mysql+pymysql://wilhelm2024:XctyzMr3hr2>@mysql-lab.iric.ca/wilhelm2024'
engine = create_engine(db_url)
metadata = MetaData()
big_summary_groups = Table('big_summary_groups', metadata, autoload_with=engine)
big_summary_gene = Table('big_summary_gene', metadata, autoload_with=engine)
target_AML_groups = Table('target_AML_groups', metadata, autoload_with=engine)
target_AML_genes = Table('target_AML_genes', metadata, autoload_with=engine)
connection = engine.connect()
# Function to calculate custom median handling non-numeric values
def custom_median(series):
numeric_series = pd.to_numeric(series, errors='coerce')
numeric_series = numeric_series.dropna()
return numeric_series.median()
# Streamlit cache decorator for data fetching
@st.cache_data()
def fetch_gene_data(gene,group_name,data_file):
global result2
if(data_file=='big_summary_updated_June_2024'):
# Construct the select query to get the sample names for the given group
query = select(big_summary_groups.c['Sample']).where(big_summary_groups.c[group_name] != '')
result = connection.execute(query)
selected_samples = [row[0] for row in result]
#query = select(big_summary_groups.c['Sample']).where(big_summary_groups.c[group_name] IS NOT NULL)
# Construct the select query to get the values for the selected samples and gene of interest
selected_columns = [big_summary_gene.c[sample] for sample in selected_samples]
query2 = select(*selected_columns).where(big_summary_gene.c['Gene'] == gene)
result2 = connection.execute(query2)
elif(data_file=='Target AML'):
query = select(target_AML_groups.c['Sample']).where(target_AML_groups.c['Type'] == group_name)
result = connection.execute(query)
#st.write(result.fetchall())
selected_samples = [row[0] for row in result]
#st.write(selected_samples)
selected_columns = [target_AML_genes.c[sample] for sample in selected_samples]
query2 = select(*selected_columns).where(target_AML_genes.c['Sample_Id'] == gene)
result2 = connection.execute(query2)
# Execute the query and fetch results
df = pd.DataFrame(result2.fetchall())
# Close the connection
return df
def gene_input(options,data_file, check=None):
fig=go.Figure()
for option in options:
if option == 'Mutation':
continue
else:
global result_gene
if data_file=='big_summary_updated_June_2024':
query_gene=big_summary_gene.select().where(big_summary_gene.c['Gene']==option)
result_gene=connection.execute(query_gene)
stat=pd.DataFrame(result_gene.fetchall())
stat=stat.iloc[:,2:]
elif data_file=='Target AML':
query_gene=target_AML_genes.select().where(target_AML_genes.c['Sample_Id']==option)
result_gene=connection.execute(query_gene)
stat=pd.DataFrame(result_gene.fetchall())
stat=stat.iloc[:,1:]
#st.write(stat)
if stat.empty:
st.warning(f"No data found for gene:{option}")
continue
#stat = stat.select_dtypes(include=[np.number])
if not stat.empty:
stat_data = stat.T.reset_index()
stat_data.columns = ['sample', 'FPKM']
stat_data['FPKM'] = pd.to_numeric(stat_data['FPKM'], errors='coerce').fillna(0.0)
#st.write(stat_data['FPKM'].dtypes)
hover_y = 'FPKM'
if check:
stat_data['FPKM'] = np.log2(stat_data['FPKM'] + 1)
hover_y = 'log2(FPKM+0.001)'
box_trace = go.Box(y=stat_data['FPKM'], boxpoints='all', jitter=0.2, pointpos=0, hoverinfo='text',
hovertext=[f"Sample: {idx}<br>{hover_y}: {val}" for idx, val in
zip(stat_data['sample'], stat_data['FPKM'])],
boxmean=True, name=option, marker=dict(size=10))
fig.add_trace(box_trace)
median = custom_median(stat_data['FPKM'])
fig.add_annotation(x=option, y=median, text=f"Median: {median:.2f}", showarrow=False, xshift=5,
font=dict(color='Black'))
if check:
title_y = 'log2(FPKM+1)'
else:
title_y = 'FPKM'
fig.update_layout(
yaxis_title=dict(text=title_y,font=dict(size=18, family='Bold Lexand')),
xaxis=dict(
title=dict(text='Genes',font=dict(size=18, family='Bold Lexand')),
tickfont=dict(size=16, family='Bold Lexand')
),
boxmode='group',
width=2500,
height=1000,
font=dict(size=16),
boxgap=0,
boxgroupgap=0,
title='Boxplot -Gene Expression'
)
#print("Gene input took", time.time() - start_time, "seconds")
return fig
# Streamlit function to create box plots
def gene_input_single(gene, group_names,data_file,log_scale=None):
fig = go.Figure()
if(gene=='Fusion'):
st.warning("Please select a gene from the dropdown menu.")
return fig
for group_name in group_names:
# Fetch data using the cached function
data_file=data_file
df = fetch_gene_data(gene, group_name,data_file)
size_group = len(df.columns)
df=df.T.reset_index()
df.columns=['sample', 'FPKM']
if df.empty:
st.warning(f"No data found for gene '{gene}' in the database.")
stat_data = df
stat_data.columns = ['sample', 'FPKM']
hover_y = 'FPKM'
if log_scale:
hover_y = 'log2(FPKM)'
stat_data['FPKM'] = stat_data['FPKM'].apply(lambda x: math.log2(float(x) + 1) )
box_trace = go.Box(
y=stat_data['FPKM'],
name=f"{group_name}<br>count={size_group}",
boxpoints='all',
jitter=0.3,
pointpos=0,
hoverinfo='text',
hovertext=[f"Sample: {sample}<br>{hover_y}: {fpkm}" for sample, fpkm in zip(stat_data['sample'], stat_data['FPKM'])],
boxmean=True,
marker=dict(size=10)
)
fig.add_trace(box_trace)
median = custom_median(stat_data['FPKM'])
fig.add_annotation(x=f"{group_name}<br>count={size_group}", y=median, text=f"Median: {median:.2f}", showarrow=False, xshift=5,
font=dict(color='Black'))
# Update layout for the plot
if log_scale:
title_y = 'log2(FPKM+1)'
else:
title_y = 'FPKM'
fig.update_layout(
yaxis_title=title_y,
xaxis=dict(
title=dict(text='Groups',font=dict(size=18, family='Bold Lexand')),
tickfont=dict(size=16, family='Bold Lexand')
),
yaxis=dict(
title=dict(text=title_y,font=dict(size=18, family='Bold Lexand')),
tickfont=dict(size=18, family='Bold Lexand')
),
boxmode='group',
width=1200,
height=800,
font=dict(size=16),
boxgap=0.5,
boxgroupgap=0.5,
title=f'Boxplot - Gene Expression for {gene}'
)
return fig
# Streamlit app
def main():
# Streamlit UI
st.sidebar.title("**Gene Expression viewer**")
#st.sidebar.subheader("**Select Data File**")
data_files = ["big_summary_updated_June_2024", "Target AML"]
Data_selected = st.sidebar.radio('**Select data file**', data_files)
if not Data_selected:
st.error("Please select a data file.")
return
inspector = inspect(engine)
columns = inspector.get_columns('big_summary_groups')
if "big_summary_updated_June_2024" in Data_selected:
res=select(big_summary_gene.c['Gene']).distinct()
res2=select(big_summary_groups.c).distinct()
resu=connection.execute(res)
resu2=connection.execute(res2)
gene_list = [row[0] for row in resu if (row[0]!='Fusion' and row[0]!='MLL-AF6' and row[0]!='MLL-AF9' and row[0]!='MLL-AF10')]
column_names = [column['name'] for column in columns if column['name']!='Sample']
column_names=column_names[:28]
default_genes = ["DPM1", "SCYL3"]
default_groups = ["CD34", "CD34+MA9", "Model AML"]
options = st.sidebar.multiselect('**Select genes**', gene_list, default=default_genes)
elif "Target AML" in Data_selected:
res=select(target_AML_genes.c['Sample_Id']).distinct()
res2=select(target_AML_groups.c['Type']).distinct()
resu=connection.execute(res)
resu2=connection.execute(res2)
gene_list = [row[0] for row in resu if row[0]!='Fusion']
column_names = [ro[0] for ro in resu2]
default_genes = ["DPM1", "SCYL3"]
default_groups = ["Other", "BM"]
options = st.sidebar.multiselect('**Select genes**', gene_list, default=default_genes)
tab1, tab2 = st.tabs(['**Group Viewer**', '**Gene Viewer**'])
check = st.sidebar.checkbox('Log2 transform Axis', key='log',value=False)
with tab1:
groups = st.multiselect("**Select Groups**", column_names, default=default_groups)
for option in options:
col1 = st.container()
with col1:
if groups:
figs = gene_input_single(option,groups,Data_selected, check)
st.plotly_chart(figs, use_container_width=True)
else:
st.title("Please select one or more groups to display the boxplot.")
break
with tab2:
col2 = st.container()
with col2:
if options:
fig = gene_input(options,Data_selected,check)
st.plotly_chart(fig, use_container_width=True)
else:
st.title("Please select one or more genes to display the boxplot.")
if __name__ == '__main__':
if authenticate():
main()
else:
st.error("Please login to the application.")