From f715ced1fc2d5c77d693e5470e38bea101786f3d Mon Sep 17 00:00:00 2001 From: Arslan Date: Mon, 16 Oct 2023 11:21:56 +0200 Subject: [PATCH] added changes from template --- "pages/1_\342\232\231\357\270\217_Analyze.py" | 92 +++++++++---------- src/captcha_.py | 68 ++++++-------- src/common.py | 2 +- 3 files changed, 77 insertions(+), 85 deletions(-) diff --git "a/pages/1_\342\232\231\357\270\217_Analyze.py" "b/pages/1_\342\232\231\357\270\217_Analyze.py" index 235510b7..bc138cf5 100755 --- "a/pages/1_\342\232\231\357\270\217_Analyze.py" +++ "b/pages/1_\342\232\231\357\270\217_Analyze.py" @@ -20,24 +20,24 @@ ### main content of page -#title of page +# title of page st.title("⚙️ Run Analysis") -#make sure "selected-mzML-files" is in session state +# 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.get("selected-mzML-files", []) -#make sure "selected-fasta-files" is in session state +# make sure "selected-fasta-files" is in session state if "selected-fasta-files" not in st.session_state: st.session_state["selected-fasta-files"] = params.get("selected-fasta-files", []) -#make sure mzML example files in current session state +# make sure mzML example files in current session state load_example_mzML_files() -#take mzML files from current session file +# take mzML files from current session file mzML_files_ = [f.name for f in Path(st.session_state.workspace, "mzML-files").iterdir()] -#selecte mzML file from mzML files list +# selected mzML file from mzML files list selected_mzML_file = st.selectbox( "choose mzML file", [item for item in mzML_files_ if not item.endswith(".csv")] @@ -45,13 +45,13 @@ help="If file not here, please upload at File Upload" ) -#make sure fasta example files in current session state +# make sure fasta example files in current session state load_example_fasta_files() -#take fasta files from current session file +# take fasta files from current session file fasta_files = [f.name for f in Path(st.session_state.workspace,"fasta-files").iterdir()] -#select fasta file from mzML files list +# select fasta file from mzML files list selected_fasta_file = st.selectbox( "choose fasta file", [f.name for f in Path(st.session_state.workspace, @@ -59,25 +59,25 @@ help="If file not here, please upload at File Upload" ) -#take full path of mzML file +# take full path of mzML file if selected_mzML_file: mzML_file_path = str(Path(st.session_state.workspace, "mzML-files", selected_mzML_file)) -#take full path of fasta file +# take full path of fasta file if selected_fasta_file: database_file_path = str(Path(st.session_state.workspace, "fasta-files", selected_fasta_file)) -#out file path +# out file path result_dir: Path = Path(st.session_state.workspace, "result-files") -#create same output file path name as input file path +# create same output file path name as input file path mzML_file_name = os.path.basename(mzML_file_path) protocol_name = os.path.splitext(mzML_file_name)[0] result_path = os.path.join(result_dir, protocol_name + ".idXML") ######################## Take NuXL configurations ini read ################################# # Define the sections you want to extract -#will capture automaticaly if add new section as decoy_factor +# will capture automaticaly if add new section as decoy_factor sections = [ "fixed", "variable", @@ -93,11 +93,11 @@ "missed_cleavages" ] -#current directory +# current directory current_dir = os.getcwd() -#take .ini config path +# take .ini config path config_path = os.path.join(current_dir, 'assets', 'OpenMS_NuXL.ini') -#take NuXL config dictionary +# take NuXL config dictionary # (will give every section as 1 entry: # entry = { #"name": node_name, @@ -107,7 +107,7 @@ # }) NuXL_config=ini2dict(config_path, sections) -#take all variables settings from config dictionary/ take all user configuration +# take all variables settings from config dictionary/ take all user configuration cols=st.columns(2) with cols[0]: cols_=st.columns(2) @@ -180,16 +180,16 @@ ##################################### NuXL command (subprocess) ############################ -#result dictionary to capture ooutput of subprocess +# result dictionary to capture ooutput of subprocess result_dict = {} result_dict["success"] = False result_dict["log"] = " " -#create terminate flag from even function +# create terminate flag from even function terminate_flag = threading.Event() terminate_flag.set() -#terminate subprocess by terminate flag +# terminate subprocess by terminate flag def terminate_subprocess(): global terminate_flag terminate_flag.set() @@ -205,12 +205,12 @@ def terminate_subprocess(): #clear form st.experimental_rerun() - #with st.spinner("Running analysis... Please wait until analysis done 😑"): #without status/ just spinner button + # with st.spinner("Running analysis... Please wait until analysis done 😑"): #without status/ just spinner button with st.status("Running analysis... Please wait until analysis done 😑"): - #If session state is local + # If session state is local if st.session_state.location == "local": - #If local in current directory of app like bin and percolator folder + # If local in current directory of app like bin and percolator folder OpenNuXL_exec = os.path.join(os.getcwd(),'bin', 'OpenNuXL') perc_exec = os.path.join(os.getcwd(), 'Percolator', 'percolator.exe') @@ -223,10 +223,10 @@ def terminate_subprocess(): args.extend(["-percolator_executable", perc_exec]) - #If session state is online/docker + # If session state is online/docker else: - #In docker it executable on path + # In docker it executable on path args = ["OpenNuXL", "-in", mzML_file_path, "-database", database_file_path, "-out", result_path, "-NuXL:presets", preset, "-NuXL:length", length, "-NuXL:scoring", scoring, "-precursor:mass_tolerance", Precursor_MT, "-precursor:mass_tolerance_unit", Precursor_MT_unit, "-fragment:mass_tolerance", Fragment_MT, "-fragment:mass_tolerance_unit", Fragment_MT_unit, @@ -234,12 +234,12 @@ def terminate_subprocess(): "-modifications:variable_max_per_peptide", Variable_max_per_peptide ] - #If variable modification provided + # If variable modification provided if variable_modification: args.extend(["-modifications:variable"]) args.extend(variable_modification) - #If fixed modification provided + # If fixed modification provided if fixed_modification: args.extend(["-modifications:fixed"]) args.extend(fixed_modification) @@ -247,27 +247,27 @@ def terminate_subprocess(): # Add any additional variables needed for the subprocess (if any) variables = [] - #want to see the command values and argues - #message = f"Running '{' '.join(args)}'" - #st.code(message) + # want to see the command values and argues + # message = f"Running '{' '.join(args)}'" + # st.code(message) - #run subprocess command + # run subprocess command run_subprocess(args, variables, result_dict) # Use st.experimental_thread to run the subprocess asynchronously - #terminate_flag = threading.Event() - #thread = threading.Thread(target=run_subprocess, args=(args, variables, result_dict)) - #thread.start() - #thread.join() + # terminate_flag = threading.Event() + # thread = threading.Thread(target=run_subprocess, args=(args, variables, result_dict)) + # thread.start() + # thread.join() - #if run_subprocess success (no need if not success because error will show/display in run_subprocess command) + # if run_subprocess success (no need if not success because error will show/display in run_subprocess command) if result_dict["success"]: - #add .mzML.ambigious_masses.csv in result directory + # add .mzML.ambigious_masses.csv in result directory add_this_result_file(f"{protocol_name}.mzML.ambigious_masses.csv", Path(st.session_state.workspace, "mzML-files")) - #remove .mzML.ambigious_masses.csv from mzML directory + # remove .mzML.ambigious_masses.csv from mzML directory remove_this_mzML_file(f"{protocol_name}.mzML.ambigious_masses.csv") # Save the log to a text file in the result_dir @@ -275,28 +275,28 @@ def terminate_subprocess(): with open(log_file_path, "w") as log_file: log_file.write(result_dict["log"]) - #all result files in result-dir + # all result files in result-dir All_files = [f.name for f in sorted(result_dir.iterdir())] - #filtered out all current run file from all resul-dir files + # filtered out all current run file from all resul-dir files current_analysis_files = [s for s in All_files if protocol_name in s] - #add list of files to dataframe + # add list of files to dataframe df = pd.DataFrame({"output files ": current_analysis_files}) - #show table of all list files of current protocol + # show table of all list files of current protocol show_table(df) - #check if perc files availabe in some cases could not run percolator e-g if identification hits are so less + # check if perc files availabe in some cases could not run percolator e-g if identification hits are so less perc_exec = any("_perc_" in string for string in current_analysis_files) - #just show and download the identification_files of XLs PSMs/PRTs if perc_XLs available otherwise without the percolator identification file + # just show and download the identification_files of XLs PSMs/PRTs if perc_XLs available otherwise without the percolator identification file if perc_exec : identification_files = [string for string in current_analysis_files if "_perc_0.0100_XLs" in string or "_perc_0.1000_XLs" in string or "_perc_1.0000_XLs" in string or "_perc_proteins" in string] else: identification_files = [string for string in current_analysis_files if "_XLs" in string or "_proteins" in string] - #then download link for identification file of above criteria + # then download link for identification file of above criteria download_selected_result_files(identification_files, f":arrow_down: {protocol_name}_XL_identification_files") save_params(params) \ No newline at end of file diff --git a/src/captcha_.py b/src/captcha_.py index 5e7d0bc9..329e091e 100644 --- a/src/captcha_.py +++ b/src/captcha_.py @@ -1,4 +1,3 @@ -import os from pathlib import Path import streamlit as st from streamlit.source_util import ( @@ -7,9 +6,11 @@ get_pages, _on_pages_changed ) +import os from captcha.image import ImageCaptcha import random, string +from src.common import * def delete_all_pages(main_script_path_str: str) -> None: """ @@ -172,51 +173,42 @@ def add_page(main_script_path_str: str, page_name: str) -> None: height = 180 # define the function for the captcha control -def captcha_control() -> None: - """ - Captcha control function to verify if the user is not a robot. - - This function displays a captcha image and allows the user to enter the captcha text. - It verifies whether the entered captcha text matches the generated captcha. - - Returns: - None - """ +def captcha_control(): #control if the captcha is correct if 'controllo' not in st.session_state or st.session_state['controllo'] == False: - st.title("Makesure you are not a robot🤖") + st.title("Make sure you are not a robot🤖") # define the session state for control if the captcha is correct st.session_state['controllo'] = False - col1, col2 = st.columns(2) # define the session state for the captcha text because it doesn't change during refreshes if 'Captcha' not in st.session_state: st.session_state['Captcha'] = ''.join(random.choices(string.ascii_uppercase + string.digits, k=length_captcha)) - #setup the captcha widget - image = ImageCaptcha(width=width, height=height) - data = image.generate(st.session_state['Captcha']) - col1.image(data) - capta2_text = col2.text_area('Enter captcha text', height=20) - - - if st.button("Verify the code"): - capta2_text = capta2_text.replace(" ", "") - # if the captcha is correct, the controllo session state is set to True - if st.session_state['Captcha'].lower() == capta2_text.lower().strip(): - del st.session_state['Captcha'] - col1.empty() - col2.empty() - st.session_state['controllo'] = True - st.rerun() + col1, _ = st.columns(2) + with col1.form("captcha-form"): + #setup the captcha widget + st.info("Please enter the captcha as text. Note: If your captcha is not accepted, you might need to disable your ad blocker.") + image = ImageCaptcha(width=width, height=height) + data = image.generate(st.session_state['Captcha']) + st.image(data) + c1, c2 = st.columns([70, 30]) + capta2_text = c1.text_input('Enter captcha text', max_chars=5) + c2.markdown("##") + if c2.form_submit_button("Verify the code", type="primary"): + capta2_text = capta2_text.replace(" ", "") + # if the captcha is correct, the controllo session state is set to True + if st.session_state['Captcha'].lower() == capta2_text.lower().strip(): + del st.session_state['Captcha'] + col1.empty() + st.session_state['controllo'] = True + st.rerun() + else: + # if the captcha is wrong, the controllo session state is set to False and the captcha is regenerated + st.error("🚨 Captch is wrong") + del st.session_state['Captcha'] + del st.session_state['controllo'] + st.rerun() else: - # if the captcha is wrong, the controllo session state is set to False and the captcha is regenerated - st.error("🚨 Captch is wrong") - del st.session_state['Captcha'] - del st.session_state['controllo'] - st.rerun() - else: - #wait for the button click - st.stop() - \ No newline at end of file + #wait for the button click + st.stop() \ No newline at end of file diff --git a/src/common.py b/src/common.py index d21724fb..94bece6e 100644 --- a/src/common.py +++ b/src/common.py @@ -10,7 +10,7 @@ import pandas as pd # set these variables according to your project -APP_NAME = "OpenMS App Template" +APP_NAME = "OpenMS NuXL Streamlit App" REPOSITORY_NAME = "streamlit-template"