Skip to content

Commit

Permalink
add Download section to FLASHTagger
Browse files Browse the repository at this point in the history
  • Loading branch information
t0mdavid-m committed May 23, 2024
1 parent f0c66ce commit e9d24c7
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 19 deletions.
1 change: 1 addition & 0 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def flashtagPages():
Page("pages/FileUploadTagger.py", "File Upload", "📁"),
Page("pages/LayoutManagerTagger.py", "Layout Manager", "📝️"),
Page("pages/FLASHTaggerViewer.py", "Viewer", "👀"),
Page("pages/FLASHTaggerDownload.py", "Download", "⬇️"),
])


Expand Down
61 changes: 61 additions & 0 deletions pages/FLASHTaggerDownload.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import streamlit as st

from os.path import join, exists, dirname, isfile, basename
from os import listdir

from src.common import page_setup
from io import StringIO, BytesIO
from zipfile import ZipFile, ZIP_DEFLATED


def content():
page_setup("TaggerViewer")

dirpath = join(st.session_state["workspace"], 'FLASHTaggerOutput')

if exists(dirpath):
directories = sorted(
[entry for entry in listdir(dirpath) if not isfile(entry)]
)
else:
directories = []

if len(directories) == 0:
st.error('No results to show yet. Please run a workflow first!')
return

# Table Header
columns = st.columns((0.4, 0.6))
columns[0].write('**Run**')
columns[1].write('**Download**')

# Table Body
for i, directory in enumerate(directories):
st.divider()
columns = st.columns((0.4, 0.6))
columns[0].empty().write(directory)

with columns[1]:
button_placeholder = st.empty()

clicked = button_placeholder.button('Prepare Download', key=i)
if clicked:
button_placeholder.empty()
with st.spinner():
out_zip = join(dirpath, directory, 'output.zip')
if not exists(out_zip):
with ZipFile(out_zip, 'w', ZIP_DEFLATED) as zip_file:
for output in listdir(join(dirpath, directory)):
try:
with open(join(dirpath, directory, output), 'r') as f:
zip_file.writestr(basename(output), f.read())
except:
continue
with open(out_zip, 'rb') as f:
button_placeholder.download_button(
"Download ⬇️", f,
file_name = f'{directory}.zip'
)

if __name__ == "__main__":
content()
38 changes: 19 additions & 19 deletions pages/FLASHTaggerViewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,26 +232,26 @@ def content():
sendDataToJS(selected_exp, layout_info, 'flash_viewer_grid_%d' % exp_index)


selected_tags = selected_exp0.iloc[0]['Tag Files']
selected_proteins = selected_exp0.iloc[0]['Protein Files']
tag_df = st.session_state['tag_dfs_tagger'][selected_tags]
protein_df = st.session_state['protein_dfs_tagger'][selected_proteins]

tag_buffer = StringIO()
tag_df.to_csv(tag_buffer, sep='\t', index=False)
tag_buffer.seek(0)

protein_buffer = StringIO()
protein_df.to_csv(protein_buffer, sep='\t', index=False)
protein_buffer.seek(0)

zip_buffer = BytesIO()
with ZipFile(zip_buffer, 'w', ZIP_DEFLATED) as zip_file:
zip_file.writestr('tags.tsv', tag_buffer.getvalue())
zip_file.writestr('proteins.tsv', protein_buffer.getvalue())
zip_buffer.seek(0)
# selected_tags = selected_exp0.iloc[0]['Tag Files']
# selected_proteins = selected_exp0.iloc[0]['Protein Files']
# tag_df = st.session_state['tag_dfs_tagger'][selected_tags]
# protein_df = st.session_state['protein_dfs_tagger'][selected_proteins]

# tag_buffer = StringIO()
# tag_df.to_csv(tag_buffer, sep='\t', index=False)
# tag_buffer.seek(0)

# protein_buffer = StringIO()
# protein_df.to_csv(protein_buffer, sep='\t', index=False)
# protein_buffer.seek(0)

# zip_buffer = BytesIO()
# with ZipFile(zip_buffer, 'w', ZIP_DEFLATED) as zip_file:
# zip_file.writestr('tags.tsv', tag_buffer.getvalue())
# zip_file.writestr('proteins.tsv', protein_buffer.getvalue())
# zip_buffer.seek(0)

st.download_button("Download ⬇️", zip_buffer, file_name=f'{st.session_state.selected_experiment0}.zip')
# st.download_button("Download ⬇️", zip_buffer, file_name=f'{st.session_state.selected_experiment0}.zip')


if __name__ == "__main__":
Expand Down
14 changes: 14 additions & 0 deletions src/Workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,14 @@ def execution(self) -> None:
out_tag = join(base_path, 'tags-tsv', f'{current_base}_{current_time}_tagged.tsv')
out_protein = join(base_path, 'proteins-tsv', f'{current_base}_{current_time}_protein.tsv')
#decoy_db = join(temp_path, f'{current_base}_db.fasta')

# Get folder name
folder_path = join(base_path, 'FLASHTaggerOutput', '%s_%s'%(current_base, current_time))

if exists(folder_path):
rmtree(folder_path)
makedirs(folder_path)


if self.executor.parameter_manager.get_parameters_from_json()['generate_decoys']:
if self.executor.parameter_manager.get_parameters_from_json()['few_proteins']:
Expand Down Expand Up @@ -279,6 +287,12 @@ def execution(self) -> None:
uploaded_files.append(out_tag)
uploaded_files.append(out_protein)

copyfile(out_db, join(folder_path, 'database.fasta'))
copyfile(out_anno, join(folder_path, 'annotated.mzML'))
copyfile(out_deconv, join(folder_path, 'out.mzML'))
copyfile(out_tag, join(folder_path, 'tags.tsv'))
copyfile(out_protein, join(folder_path, 'proteins.tsv'))


# make directory to store deconv and anno mzML files & initialize data storage
# input_types = ["deconv-mzMLs", "anno-mzMLs", "tags-tsv", "db-fasta"]
Expand Down

0 comments on commit e9d24c7

Please sign in to comment.