|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +"""Callback definitions to be used by streamlit app""" |
| 5 | + |
| 6 | +import shutil |
| 7 | +from pathlib import Path |
| 8 | +import streamlit as st |
| 9 | + |
| 10 | +import pynteny.app.filemanager as filemanager |
| 11 | +from pynteny.subcommands import synteny_search, build_database, download_hmms |
| 12 | +from pynteny.utils import ConfigParser |
| 13 | + |
| 14 | + |
| 15 | +parent_dir = Path(__file__).parent |
| 16 | + |
| 17 | + |
| 18 | +def select_log_path(): |
| 19 | + if st.session_state.log == "Yes": |
| 20 | + logfile = Path(st.session_state.outdir) / "pynteny.log" |
| 21 | + else: |
| 22 | + logfile = None |
| 23 | + st.session_state.pynteny_log = logfile |
| 24 | + |
| 25 | + |
| 26 | +def update_log(): |
| 27 | + """ |
| 28 | + Update Pynteny log with Streamlit log info |
| 29 | + """ |
| 30 | + config = ConfigParser.get_default_config() |
| 31 | + streamlit_log = config.get_field("streamlit_log") |
| 32 | + select_log_path() |
| 33 | + if st.session_state.pynteny_log is not None: |
| 34 | + shutil.copy(streamlit_log, st.session_state.pynteny_log) |
| 35 | + |
| 36 | + |
| 37 | +def search(): |
| 38 | + config = ConfigParser.get_default_config() |
| 39 | + if (st.session_state.search_state.hmm_dir is None) and ( |
| 40 | + not config.get_field("data_downloaded") |
| 41 | + ): |
| 42 | + with st.spinner("Downloading HMM database, please wait..."): |
| 43 | + download_hmms(st.session_state.download_state) |
| 44 | + st.success("HMM database downloaded!") |
| 45 | + st.session_state.search_state.outdir = st.session_state.outdir |
| 46 | + if ( |
| 47 | + st.session_state.search_state.data is not None |
| 48 | + and st.session_state.search_state.data.exists() |
| 49 | + ): |
| 50 | + synhits = synteny_search(st.session_state.search_state).getSyntenyHits() |
| 51 | + st.session_state.search_state.synteny_hits = synhits[ |
| 52 | + [c for c in synhits.columns if c != "full_label"] |
| 53 | + ] |
| 54 | + update_log() |
| 55 | + else: |
| 56 | + st.warning("Please, first upload a sequence database file") |
| 57 | + |
| 58 | + |
| 59 | +def build(): |
| 60 | + if not st.session_state.sequence_data_uploaded: |
| 61 | + st.warning("Please, first upload assembly data file") |
| 62 | + else: |
| 63 | + st.session_state.build_state.data = st.session_state.search_state.data |
| 64 | + st.session_state.build_state.outdir = st.session_state.search_state.outdir |
| 65 | + st.session_state.build_state.outfile = ( |
| 66 | + Path(st.session_state.search_state.data.parent) |
| 67 | + / f"{st.session_state.search_state.data.stem}_labelled.faa" |
| 68 | + ) |
| 69 | + st.session_state.search_state.data = st.session_state.build_state.outfile |
| 70 | + build_database(st.session_state.build_state) |
| 71 | + update_log() |
| 72 | + |
| 73 | + |
| 74 | +def upload_data(): |
| 75 | + selected_path = filemanager.open_file_explorer() |
| 76 | + st.session_state.search_state.data = selected_path |
| 77 | + st.session_state.sequence_data_uploaded = True |
| 78 | + |
| 79 | + |
| 80 | +def update_output_dir(): |
| 81 | + selected_outdir = filemanager.open_directory_explorer() |
| 82 | + if selected_outdir is not None: |
| 83 | + st.session_state.outdir = selected_outdir |
| 84 | + |
| 85 | + |
| 86 | +def update_output_prefix(): |
| 87 | + st.session_state.search_state.prefix = st.session_state.prefix |
| 88 | + |
| 89 | + |
| 90 | +def update_output_subdirectory(): |
| 91 | + subdir = Path(st.session_state.outdir) / st.session_state.subdirectory |
| 92 | + if not subdir.exists(): |
| 93 | + subdir.mkdir(parents=True, exist_ok=False) |
| 94 | + st.session_state.outdir = subdir |
| 95 | + |
| 96 | + |
| 97 | +def select_HMM_dir(): |
| 98 | + selected_dir = filemanager.open_directory_explorer() |
| 99 | + st.session_state.search_state.hmm_dir = selected_dir |
| 100 | + st.success(f"Selected HMM database: {selected_dir}") |
| 101 | + |
| 102 | + |
| 103 | +def select_HMM_meta(): |
| 104 | + selected_file = filemanager.open_file_explorer() |
| 105 | + st.session_state.search_state.hmm_meta = selected_file |
| 106 | + st.success(f"Selected HMM metadata: {selected_file}") |
| 107 | + |
| 108 | + |
| 109 | +def set_number_of_processes(): |
| 110 | + st.session_state.build_state.processes = st.session_state.processes |
| 111 | + st.session_state.search_state.processes = st.session_state.processes |
| 112 | + |
| 113 | + |
| 114 | +def close_session(): |
| 115 | + st.text(" ") |
| 116 | + st.text(" ") |
| 117 | + st.markdown("# Thanks for using") |
| 118 | + st.text(" ") |
| 119 | + st.image(st.session_state.sidebar_icon) |
| 120 | + st.text(" ") |
| 121 | + st.markdown("### Please stop the server by pressing control + c in terminal") |
| 122 | + st.stop() |
0 commit comments