forked from Unsaif/mars-pipeline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
384 lines (324 loc) · 15.4 KB
/
app.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
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
# Import the necessary libraries
import streamlit as st
import pandas as pd
from ANT.ant import ant
from io import BytesIO
st.set_page_config(layout="wide")
# streamlit_app.py
from MARS.utils import read_file_as_dataframe, merge_files, normalize_dataframes, combine_metrics
from MARS.operations import split_taxonomic_groups, rename_taxa, calculate_metrics, check_presence_in_agora2
def convert_df(df, file_format, index=False):
if file_format == "xlsx":
output = BytesIO()
with pd.ExcelWriter(output, engine="openpyxl") as writer:
df.to_excel(writer, index=index)
data = output.getvalue()
return data
elif file_format == "txt":
return df.to_csv(index=index, sep="\t").encode("utf-8")
else:
return df.to_csv(index=index).encode("utf-8")
def file_to_list(uploaded_file):
# Reads a file and returns the data as a list
if uploaded_file is not None:
file_details = {
"FileName": uploaded_file.name,
"FileType": uploaded_file.type,
"FileSize": uploaded_file.size,
}
st.write(file_details)
if uploaded_file.type == "text/plain" or uploaded_file.type == "text/tab-separated-values":
# For txt files, we simply read each line into a list
bytes_data = uploaded_file.getvalue()
str_data = bytes_data.decode("utf-8")
lines = str_data.split("\n")
return [line for line in lines if line]
elif uploaded_file.type == "text/csv":
try:
df = pd.read_csv(uploaded_file)
# Assuming the species names are in the first column
species_list = df.iloc[:, 0].tolist()
return species_list
except Exception as e:
st.write("Could not read file: ", e)
return None
elif "spreadsheet" in uploaded_file.type:
try:
df = pd.read_excel(uploaded_file)
# Assuming the species names are in the first column
species_list = df.iloc[:, 0].tolist()
return species_list
except Exception as e:
st.write("Could not read file: ", e)
return None
else:
st.write("Unsupported file type: ", uploaded_file.type)
return None
else:
return None
st.image("images/logo2.png", use_column_width=True)
st.title("MARS")
st.write("""
""")
col1, col2, col3, col4 = st.columns(4)
uploaded_file1 = col1.file_uploader("Upload Taxonomy Table", type=['csv', 'tsv', 'txt', 'xlsx'])
uploaded_file2 = col2.file_uploader("Upload Feature Table", type=['csv', 'tsv', 'txt', 'xlsx'])
uploaded_file3 = col3.file_uploader("Upload Combined Table", type=['csv', 'tsv', 'txt', 'xlsx'])
uploaded_resource_file = col4.file_uploader(
"(Optional) Upload your own resource file",
type=["txt", "csv", "xlsx"],
)
# Sidebar for optional parameters
st.sidebar.header("Optional Parameters")
cutoff = st.sidebar.slider("Cutoff", 0.0, 100.0, 0.0)
output_format = st.sidebar.radio("Output Format", ["csv", "txt", "xlsx"])
stratification_file = st.sidebar.file_uploader("Stratification File", type=['csv', 'txt', 'xlsx'])
skip_ant = st.sidebar.checkbox('Skip ANT')
flagLoneSpecies = st.sidebar.checkbox('Genus name not present in the s__ taxonomic identifier?')
taxaSplit = st.sidebar.text_input('Delimiter used to seperate taxonomic levels')
if (uploaded_file1 and uploaded_file2) or uploaded_file3:
# When the button is clicked, the function is executed
if st.button("Process Files"):
with st.spinner("Merging files..."):
if uploaded_file3:
merged_dataframe = read_file_as_dataframe(uploaded_file3, 0)
st.success("Files already merged!")
else:
merged_dataframe = merge_files(uploaded_file1, uploaded_file2)
st.success("Merging files: Success!")
with st.spinner("Splitting taxonomic groups..."):
st.success(flagLoneSpecies)
taxonomic_dataframes = split_taxonomic_groups(merged_dataframe, flagLoneSpecies=flagLoneSpecies, taxaSplit=taxaSplit)
st.success("Splitting taxonomic groups: Success!")
with st.spinner("Renaming taxa..."):
renamed_dataframes = rename_taxa(taxonomic_dataframes)
st.success("Renaming taxa: Success!")
# ANT
if not skip_ant:
# Convert the uploaded file to a list
resource = file_to_list(uploaded_resource_file)
# If the species list is not empty, find homosynonyms
st.divider()
species = list(renamed_dataframes['Species'].index)
species = [sub.replace('_', ' ') for sub in species]
species_in_resource, homosynonyms, ncbi_tax_id, all_found_homosynoyms = ant(
species, resource
)
st.success("Done!")
st.divider()
# Create two columns
col1, col2 = st.columns(2)
col1.metric("Number of species found in resource", len(species_in_resource))
col2.metric("Number of homosynonyms found in resource", len(homosynonyms))
st.divider()
species_df = pd.DataFrame(species_in_resource, columns=["Species in Resource"])
homosynonyms_df = pd.DataFrame(
list(homosynonyms.items()),
columns=["Original Name", "Homosynonym in Resource"],
)
ncbi_tax_id_df = pd.DataFrame(
list(ncbi_tax_id.items()), columns=["Species", "NCBI Tax ID"]
)
all_found_homosynoyms_df = pd.DataFrame(
list(all_found_homosynoyms.items()),
columns=["Species", "All Found Homosynonymns"],
)
tab1, tab2, tab3, tab4 = st.tabs(
[
"Species in Resource",
"Homosynonyms in Resource",
"NCBI Taxonomic IDs",
"All Found Homosynonyms",
]
)
with tab1:
col1, col2 = st.columns(2)
col1.dataframe(species_df, use_container_width=True)
# Convert the DataFrame to downloadable
species = convert_df(species_df, output_format)
# Create the download button
col2.download_button(
label="Download Species",
data=species,
file_name=f"species.{output_format}",
mime=f"text/{output_format}",
disabled=species_df.empty,
)
with tab2:
col1, col2 = st.columns(2)
col1.dataframe(
homosynonyms_df,
hide_index=True,
use_container_width=True,
)
# Convert the DataFrame to downloadable
homosynonyms_conv = convert_df(homosynonyms_df, output_format)
# Create the download button
col2.download_button(
label="Download Homosynonyms",
data=homosynonyms_conv,
file_name=f"homosynonyms.{output_format}",
mime=f"text/{output_format}",
disabled=homosynonyms_df.empty,
)
with tab3:
col1, col2 = st.columns(2)
col1.dataframe(
ncbi_tax_id_df,
hide_index=False,
use_container_width=True,
)
# Convert the DataFrame to downloadable
ncbi_tax_ids = convert_df(ncbi_tax_id_df, output_format)
# Create the download button
col2.download_button(
label="Download NCBI Tax IDs",
data=ncbi_tax_ids,
file_name=f"ncbi_tax_ids.{output_format}",
mime=f"text/{output_format}",
disabled=ncbi_tax_id_df.empty,
)
with tab4:
col1, col2 = st.columns(2)
col1.dataframe(
all_found_homosynoyms_df,
hide_index=True,
use_container_width=True,
)
# Convert the DataFrame to downloadable
all_found_homosynoyms = convert_df(all_found_homosynoyms_df, output_format)
# Create the download button
col2.download_button(
label="Download All Found Homosynonyms",
data=all_found_homosynoyms,
file_name=f"all_found_homosynonyms.{output_format}",
mime=f"text/{output_format}",
disabled=all_found_homosynoyms_df.empty,
)
# rename any new found homosynonyms
if homosynonyms:
with st.spinner("Renaming found homosynonyms..."):
homosynonyms = {key.replace(' ', '_'): value.replace(' ', '_') for key, value in homosynonyms.items()}
renamed_dataframes["Species"].rename(index=homosynonyms)
st.success("Renaming found homosynonyms: Success!")
with st.spinner("Checking presence in AGORA2..."):
present_dataframes, absent_dataframes = check_presence_in_agora2(renamed_dataframes)
st.success("Checking presence in AGORA2: Success!")
with st.spinner("Normalizing dataframes..."):
normalized_dataframes = normalize_dataframes(renamed_dataframes, cutoff=cutoff)
normalized_present_dataframes, normalized_absent_dataframes = normalize_dataframes(present_dataframes, cutoff=cutoff), normalize_dataframes(absent_dataframes, cutoff=cutoff)
st.success("Normalizing dataframes: Success!")
with st.spinner("Calculating metrics..."):
pre_agora2_check_metrics = calculate_metrics(renamed_dataframes)
post_agora2_check_metrics = calculate_metrics(present_dataframes)
combined_metrics = combine_metrics(pre_agora2_check_metrics, post_agora2_check_metrics)
st.success("Calculating metrics: Success!")
stratification_groups = {}
if stratification_file is not None:
with st.spinner("Processing stratification groups..."):
stratification = pd.read_csv(stratification_file)
for group in stratification["group"].unique():
group_columns = list(stratification[stratification["group"] == group]["samples"])
pre_group_metrics = calculate_metrics(renamed_dataframes, group=group_columns)
post_group_metrics = calculate_metrics(present_dataframes, group=group_columns)
combined_group_metrics = combine_metrics(pre_group_metrics, post_group_metrics)
group_name = f"{group.lower()}_metrics"
stratification_groups[group_name] = combined_group_metrics
st.success("Processing stratification groups: Success!")
dataframe_groups = {'normalized': normalized_dataframes,
'present': normalized_present_dataframes,
'absent': normalized_absent_dataframes,
'metrics': combined_metrics
}
dataframe_groups.update(stratification_groups)
endtab1, endtab2, endtab3, endtab4 = st.tabs(
[
"Normalized",
"Present",
"Absent",
"Metrics",
]
)
levels = ['Kingdom', 'Phylum', 'Class', 'Order', 'Family', 'Genus', 'Species']
with endtab1:
tabs = st.tabs(levels)
for i, (group, df) in enumerate(dataframe_groups['normalized'].items()):
# Convert the DataFrame to downloadable
df_conv = convert_df(df, output_format, index=True)
with tabs[i]:
col1, col2 = st.columns(2)
col1.dataframe(
df,
hide_index=False,
use_container_width=True,
)
col2.download_button(
label=f"Download {group}",
data=df_conv,
file_name=f"{group}_normalized.{output_format}",
mime=f"text/{output_format}",
disabled=df.empty,
key=f'{group}_normalized'
)
with endtab2:
tabs = st.tabs(levels)
for i, (group, df) in enumerate(dataframe_groups['present'].items()):
# Convert the DataFrame to downloadable
df_conv = convert_df(df, output_format)
with tabs[i]:
col1, col2 = st.columns(2)
col1.dataframe(
df,
hide_index=False,
use_container_width=True,
)
col2.download_button(
label=f"Download {group}",
data=df_conv,
file_name=f"{group}_present.{output_format}",
mime=f"text/{output_format}",
disabled=df.empty,
key=f'{group}_present'
)
with endtab3:
tabs = st.tabs(levels)
for i, (group, df) in enumerate(dataframe_groups['absent'].items()):
# Convert the DataFrame to downloadable
df_conv = convert_df(df, output_format)
with tabs[i]:
col1, col2 = st.columns(2)
col1.dataframe(
df,
hide_index=False,
use_container_width=True,
)
col2.download_button(
label=f"Download {group}",
data=df_conv,
file_name=f"{group}_absent.{output_format}",
mime=f"text/{output_format}",
disabled=df.empty,
key=f'{group}_absent'
)
with endtab4:
metric_tabs = st.tabs(['Read Counts', 'Shannon Index', 'Firmicutes/Bacteroidetes Ratio'])
for i, (group, metrics) in enumerate(dataframe_groups['metrics'].items()):
for j, (metric, df) in enumerate(metrics.items()):
with metric_tabs[j]:
metric_tabs[j].subheader(group)
# Convert the DataFrame to downloadable
df_conv = convert_df(df, output_format)
col1, col2 = st.columns(2)
col1.dataframe(
df,
hide_index=False,
use_container_width=True,
)
col2.download_button(
label=f"Download {metric}",
data=df_conv,
file_name=f"{group}_{metric}.{output_format}",
mime=f"text/{output_format}",
disabled=df.empty,
key=f'{group}_{metric}'
)