Skip to content

Commit

Permalink
fix local
Browse files Browse the repository at this point in the history
  • Loading branch information
Arslan-Siraj committed Sep 21, 2023
1 parent 46d14b3 commit e1d0cec
Show file tree
Hide file tree
Showing 3 changed files with 205 additions and 209 deletions.
112 changes: 56 additions & 56 deletions pages/0_📁_File_Upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,71 +15,71 @@
st.session_state["controllo"] = True

#if controllo is false means not captcha applied
if 'controllo' not in st.session_state or params["controllo"] == False or st.session_state.location != "local":
if 'controllo' not in st.session_state or params["controllo"] == False:
#apply captcha
captcha_control()

else:
### main content of page

# Make sure "selected-mzML-files" is in session state
if "selected-mzML-files" not in st.session_state:
st.session_state["selected-mzML-files"] = params["selected-mzML-files"]
### main content of page

st.title("File Upload")
# Make sure "selected-mzML-files" is in session state
if "selected-mzML-files" not in st.session_state:
st.session_state["selected-mzML-files"] = params["selected-mzML-files"]

tabs = ["File Upload", "Example Data"]
if st.session_state.location == "local":
tabs.append("Files from local folder")
st.title("File Upload")

tabs = st.tabs(tabs)
tabs = ["File Upload", "Example Data"]
if st.session_state.location == "local":
tabs.append("Files from local folder")

with tabs[0]:
with st.form("mzML-upload", clear_on_submit=True):
files = st.file_uploader(
"mzML files", accept_multiple_files=(st.session_state.location == "local"))
cols = st.columns(3)
if cols[1].form_submit_button("Add files to workspace", type="primary"):
save_uploaded_mzML(files)
tabs = st.tabs(tabs)

# Example mzML files
with tabs[1]:
st.markdown("Short information text about the example data.")
with tabs[0]:
with st.form("mzML-upload", clear_on_submit=True):
files = st.file_uploader(
"mzML files", accept_multiple_files=(st.session_state.location == "local"))
cols = st.columns(3)
if cols[1].button("Load Example Data", type="primary"):
load_example_mzML_files()

# Local file upload option: via directory path
if st.session_state.location == "local":
with tabs[2]:
# with st.form("local-file-upload"):
local_mzML_dir = st.text_input(
"path to folder with mzML files")
# raw string for file paths
local_mzML_dir = r"{}".format(local_mzML_dir)
cols = st.columns(3)
if cols[1].button("Copy files to workspace", type="primary", disabled=(local_mzML_dir == "")):
copy_local_mzML_files_from_directory(local_mzML_dir)

if any(Path(mzML_dir).iterdir()):
v_space(2)
# Display all mzML files currently in workspace
df = pd.DataFrame(
{"file name": [f.name for f in Path(mzML_dir).iterdir()]})
st.markdown("##### mzML files in current workspace:")
show_table(df)
v_space(1)
# Remove files
with st.expander("🗑️ Remove mzML files"):
to_remove = st.multiselect("select mzML files",
options=[f.stem for f in sorted(mzML_dir.iterdir())])
c1, c2 = st.columns(2)
if c2.button("Remove **selected**", type="primary", disabled=not any(to_remove)):
remove_selected_mzML_files(to_remove)
st.experimental_rerun()

if c1.button("⚠️ Remove **all**", disabled=not any(mzML_dir.iterdir())):
remove_all_mzML_files()
st.experimental_rerun()
if cols[1].form_submit_button("Add files to workspace", type="primary"):
save_uploaded_mzML(files)

# Example mzML files
with tabs[1]:
st.markdown("Short information text about the example data.")
cols = st.columns(3)
if cols[1].button("Load Example Data", type="primary"):
load_example_mzML_files()

# Local file upload option: via directory path
if st.session_state.location == "local":
with tabs[2]:
# with st.form("local-file-upload"):
local_mzML_dir = st.text_input(
"path to folder with mzML files")
# raw string for file paths
local_mzML_dir = r"{}".format(local_mzML_dir)
cols = st.columns(3)
if cols[1].button("Copy files to workspace", type="primary", disabled=(local_mzML_dir == "")):
copy_local_mzML_files_from_directory(local_mzML_dir)

if any(Path(mzML_dir).iterdir()):
v_space(2)
# Display all mzML files currently in workspace
df = pd.DataFrame(
{"file name": [f.name for f in Path(mzML_dir).iterdir()]})
st.markdown("##### mzML files in current workspace:")
show_table(df)
v_space(1)
# Remove files
with st.expander("🗑️ Remove mzML files"):
to_remove = st.multiselect("select mzML files",
options=[f.stem for f in sorted(mzML_dir.iterdir())])
c1, c2 = st.columns(2)
if c2.button("Remove **selected**", type="primary", disabled=not any(to_remove)):
remove_selected_mzML_files(to_remove)
st.experimental_rerun()

if c1.button("⚠️ Remove **all**", disabled=not any(mzML_dir.iterdir())):
remove_all_mzML_files()
st.experimental_rerun()

save_params(params)
150 changes: 74 additions & 76 deletions pages/1_👀_View_Raw_Data.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,93 +13,91 @@
st.session_state["controllo"] = True

#if controllo is false means not captcha applied
if 'controllo' not in st.session_state or params["controllo"] == False or st.session_state.location != "local":
if 'controllo' not in st.session_state or params["controllo"] == False:
#apply captcha
captcha_control()

else:
### main content of page

### main content of page

st.title("View raw MS data")
selected_file = st.selectbox(
"choose file",
[f.name for f in Path(st.session_state.workspace,
"mzML-files").iterdir()],
st.title("View raw MS data")
selected_file = st.selectbox(
"choose file",
[f.name for f in Path(st.session_state.workspace,
"mzML-files").iterdir()],
)
if selected_file:
df = get_df(
Path(st.session_state.workspace, "mzML-files", selected_file))
df_MS1, df_MS2 = (
df[df["mslevel"] == 1],
df[df["mslevel"] == 2],
)
if selected_file:
df = get_df(
Path(st.session_state.workspace, "mzML-files", selected_file))
df_MS1, df_MS2 = (
df[df["mslevel"] == 1],
df[df["mslevel"] == 2],
)

if not df_MS1.empty:
tabs = st.tabs(["📈 Base peak chromatogram and MS1 spectra",
"📈 Peak map and MS2 spectra"])
with tabs[0]:
# BPC and MS1 spec
st.markdown("💡 Click a point in the BPC to show the MS1 spectrum.")
bpc_fig = plot_bpc(df_MS1)
if not df_MS1.empty:
tabs = st.tabs(["📈 Base peak chromatogram and MS1 spectra",
"📈 Peak map and MS2 spectra"])
with tabs[0]:
# BPC and MS1 spec
st.markdown("💡 Click a point in the BPC to show the MS1 spectrum.")
bpc_fig = plot_bpc(df_MS1)

# Determine RT positions from clicks in BPC to show MS1 at this position
bpc_points = plotly_events(bpc_fig)
if bpc_points:
ms1_rt = bpc_points[0]["x"]
else:
ms1_rt = df_MS1.loc[0, "RT"]
# Determine RT positions from clicks in BPC to show MS1 at this position
bpc_points = plotly_events(bpc_fig)
if bpc_points:
ms1_rt = bpc_points[0]["x"]
else:
ms1_rt = df_MS1.loc[0, "RT"]

spec = df_MS1.loc[df_MS1["RT"] == ms1_rt].squeeze()

spec = df_MS1.loc[df_MS1["RT"] == ms1_rt].squeeze()
title = f"MS1 spectrum @RT {spec['RT']}"
fig = plot_ms_spectrum(
spec,
title,
"#EF553B",
)
show_fig(fig, title.replace(" ", "_"))

with tabs[1]:
c1, c2 = st.columns(2)
c1.number_input(
"2D map intensity cutoff",
1000,
1000000000,
params["2D-map-intensity-cutoff"],
1000,
key="2D-map-intensity-cutoff"
)
v_space(1, c2)
c2.markdown("💡 Click anywhere to show the closest MS2 spectrum.")
map2D = plot_2D_map(
df_MS1,
df_MS2,
st.session_state["2D-map-intensity-cutoff"],
)
map_points = plotly_events(map2D)
# Determine RT and mz positions from clicks in the map to get closest MS2 spectrum
if not df_MS2.empty:
if map_points:
rt = map_points[0]["x"]
prec_mz = map_points[0]["y"]
else:
rt = df_MS2.iloc[0, 2]
prec_mz = df_MS2.iloc[0, 0]
spec = df_MS2.loc[
(
abs(df_MS2["RT"] - rt) +
abs(df_MS2["precursormz"] - prec_mz)
).idxmin(),
:,
]
title = f"MS2 spectrum @precursor m/z {round(spec['precursormz'], 4)} @RT {round(spec['RT'], 2)}"

title = f"MS1 spectrum @RT {spec['RT']}"
fig = plot_ms_spectrum(
ms2_fig = plot_ms_spectrum(
spec,
title,
"#EF553B",
"#00CC96"
)
show_fig(fig, title.replace(" ", "_"))

with tabs[1]:
c1, c2 = st.columns(2)
c1.number_input(
"2D map intensity cutoff",
1000,
1000000000,
params["2D-map-intensity-cutoff"],
1000,
key="2D-map-intensity-cutoff"
)
v_space(1, c2)
c2.markdown("💡 Click anywhere to show the closest MS2 spectrum.")
map2D = plot_2D_map(
df_MS1,
df_MS2,
st.session_state["2D-map-intensity-cutoff"],
)
map_points = plotly_events(map2D)
# Determine RT and mz positions from clicks in the map to get closest MS2 spectrum
if not df_MS2.empty:
if map_points:
rt = map_points[0]["x"]
prec_mz = map_points[0]["y"]
else:
rt = df_MS2.iloc[0, 2]
prec_mz = df_MS2.iloc[0, 0]
spec = df_MS2.loc[
(
abs(df_MS2["RT"] - rt) +
abs(df_MS2["precursormz"] - prec_mz)
).idxmin(),
:,
]
title = f"MS2 spectrum @precursor m/z {round(spec['precursormz'], 4)} @RT {round(spec['RT'], 2)}"

ms2_fig = plot_ms_spectrum(
spec,
title,
"#00CC96"
)
show_fig(ms2_fig, title.replace(" ", "_"))
show_fig(ms2_fig, title.replace(" ", "_"))

save_params(params)
Loading

0 comments on commit e1d0cec

Please sign in to comment.