Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test streamlit #21

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 6 additions & 11 deletions streamlit_app/FAIR_MS_Library_Editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,15 @@
st.markdown("## Datasets")
if 'datasets' not in st.session_state or st.session_state['datasets'] == {}:
st.warning("Please upload a file to begin!")
if 'selected_sheets' not in st.session_state or st.session_state['selected_sheets'] == {}:
st.warning("Please select a dataset to begin!")
#if 'selected_sheets' not in st.session_state or st.session_state['selected_sheets'] == {}:
#st.warning("Please select a dataset to begin!")
# with st.spinner("Loading..."):
# time.sleep(5)
# st.success("Done!")
if 'datasets' in st.session_state and st.session_state['datasets'] != {}:
for key in st.session_state['selected_sheets']:
with st.expander(key):
datasets = st.session_state['datasets']
rowsMetricColumn, columnsMetricColumn = st.columns(2)
with rowsMetricColumn:
st.metric('Rows', datasets[key].shape[0])
with columnsMetricColumn:
st.metric('Columns', datasets[key].shape[1])
if 'df_spectra' in st.session_state and st.session_state['df_spectra'] != {}:

df_spectra = st.session_state['df_spectra']
st.metric('Detected how many spectra', len(df_spectra))
# if st.button("Edit", key=key):
# selected_sheet = key
# if key in datasets_metadata:
Expand Down
76 changes: 40 additions & 36 deletions streamlit_app/pages/1_File_Import.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import streamlit as st
import pandas as pd
from matchms.importing import load_from_mgf
from tempfile import NamedTemporaryFile


st.set_page_config(
layout="wide",
page_title="File Import - FAIR MS Library Curation Editor",
page_title="File Import (.mgf) - FAIR MS Library Curation Editor",
#page_icon="assets/favicon.ico",
menu_items={
'Get Help': 'https://github.com/mzmine/biohack23_p15',
Expand All @@ -12,52 +15,53 @@
}
)

st.markdown("## File Import")
st.markdown("Please select an Excel file to upload. The file should contain one or more sheets. Each sheet should contain sample columns, detailing factors of each individual sample (rows). Lipid identities are the column headers of the non-sample columns, quantities should be reported in the cells.")
st.markdown("## File Import (.mgf)")
st.markdown("Please select an mgf to upload.")

uploaded_file = st.file_uploader("Choose a file", type = ".mgf")
st.set_option('deprecation.showfileUploaderEncoding', False)



uploaded_file = st.file_uploader("Choose a file", )
if uploaded_file is not None:
print(uploaded_file)
st.session_state['uploaded_file'] = uploaded_file

if 'uploaded_file' in st.session_state and st.session_state['uploaded_file'] is not None:
print("Uploaded file:", st.session_state['uploaded_file'])
uploaded_file = st.session_state['uploaded_file']
#uploaded_file = st.session_state['uploaded_file']
with st.spinner('Loading data...'):
datasets = {}
if 'datasets' in st.session_state:
datasets = st.session_state['datasets']
else:
st.session_state['datasets'] = datasets

with NamedTemporaryFile(dir='.', suffix='.mgf') as f:
f.write(uploaded_file.getbuffer())
spectra_temp = load_from_mgf(f.name)
#spectra_temp = load_from_mgf(uploaded_file, "wb")
spectra = list(spectra_temp)
df_spectra = pd.DataFrame({"spectrum": spectra})


xl = pd.ExcelFile(uploaded_file)
sheets = xl.sheet_names
for sheet in sheets:
if sheet not in datasets:
df = pd.read_excel(uploaded_file, sheet_name=sheet)
datasets[sheet] = df

st.markdown("## Preview Sheets")
sheet_selector = st.selectbox(
"Select a sheet",
sheets
)
if sheet_selector is not None and sheet_selector in datasets:
rowsMetricColumn, columnsMetricColumn = st.columns(2)
with rowsMetricColumn:
st.metric('Rows', datasets[sheet_selector].shape[0])
with columnsMetricColumn:
st.metric('Columns', datasets[sheet_selector].shape[1])
st.write(datasets[sheet_selector])

st.markdown("## Select Sheets as Datasets")
selected_sheets = st.multiselect(
'Each selected sheet will be converted to a dataset',
sheets,
sheets
)
st.session_state['datasets'] = datasets
st.session_state['selected_sheets'] = selected_sheets

if 'datasets' not in st.session_state:
st.session_state['datasets'] = []
# make dataframe for metadata
def extract_metadata(df, keys):
for key in keys:
df[key] = df["spectrum"].apply(lambda x: x.get(key))


extract_metadata(df_spectra, df_spectra["spectrum"][0].metadata.keys())

st.markdown("## Preview Information")

st.metric('Detected how many spectra', len(df_spectra))

st.write(df_spectra)


st.session_state['df_spectra'] = df_spectra
st.session_state['len_spectra'] = len(df_spectra)

if 'df_spectra' not in st.session_state:
st.session_state['df_spectra'] = []