# Import necessary libraries import streamlit as st from io import StringIO, BytesIO from Bio import SeqIO from kitikiplot.genomics import grid # Page configuration st.set_page_config( page_title="Genome Grid Plot", page_icon="🧬", layout="wide", initial_sidebar_state="collapsed" ) # Professional styling with blue navigation bar st.markdown(""" """, unsafe_allow_html=True) import base64 with open("src/logo.png", "rb") as img_file: encoded = base64.b64encode(img_file.read()).decode() # Display using HTML st.markdown(f""" """, unsafe_allow_html=True) # st.markdown(""" # # """, unsafe_allow_html=True) # st.image("banner.png", width=60) # Initialize session state if 'figure' not in st.session_state: st.session_state.figure = None if 'data' not in st.session_state: st.session_state.data = "" if 'plot_generated' not in st.session_state: st.session_state.plot_generated = False # Main layout col1, col2 = st.columns([30, 70]) # Column 1: Upload and Controls with col1: st.markdown('

📁 Upload Genome File

', unsafe_allow_html=True) uploaded_file = st.file_uploader( "Choose a FASTA file", type=['fasta', 'fa', 'fas'], help="Upload your genome sequence in FASTA format" ) if uploaded_file is not None: # Process file try: data = str(list(SeqIO.parse(StringIO(uploaded_file.getvalue().decode("utf-8")), "fasta"))[0].seq) st.session_state.data = data # Show file info st.success(f"✅ Sequence length: {len(data):,} nucleotides") # Window length input st.markdown('

⚙️ Parameters

', unsafe_allow_html=True) window_length = st.number_input( label="Window/Chunk Length", min_value=1, max_value=200, value=50, key="window_length", help="Size of each analysis window for the grid plot" ) st.markdown('

Recommended: sequence length <1000 for detailed analysis

', unsafe_allow_html=True) # Submit button submit = st.button("🔬 Generate Plot", type="primary", key="submit_btn") if submit: with st.spinner("🧬 Generating genome grid plot..."): st.session_state.figure = grid.plot( nucleotide_sequence=data, window_length=window_length ) st.session_state.plot_generated = True except Exception as e: st.error(f"❌ Error processing file: {str(e)}") else: st.info("👆 Please upload a FASTA file to begin visualization") # # Banner image at bottom with error handling # try: # st.image("src/banner.png", use_container_width=True) # except: # pass # Silently ignore if banner not found st.markdown('', unsafe_allow_html=True) # Column 2: Plot Display with col2: if st.session_state.figure is not None and st.session_state.plot_generated: st.markdown('

Visualization Results

', unsafe_allow_html=True) # Display plot st.pyplot(st.session_state.figure, use_container_width=True) # Download section st.markdown("---") # Prepare download img_bytes = BytesIO() st.session_state.figure.savefig(img_bytes, format="png", dpi=300, bbox_inches='tight', facecolor='white', edgecolor='none') img_bytes.seek(0) # Download button filename = uploaded_file.name.replace('.fasta', '').replace('.fa', '').replace('.fas', '') if uploaded_file else "genome" st.download_button( label="📥 Download Plot as PNG", data=img_bytes.getvalue(), file_name=f"{filename}_GridPlot.png", mime="image/png" ) else: # Empty state with instructions st.markdown("""

🧬 Ready for Analysis ?

Upload a FASTA file and click "Generate Plot" to visualize your genome sequence.


About
""", unsafe_allow_html=True) st.markdown('', unsafe_allow_html=True) # Footer st.markdown("---") st.markdown("""

Genome Grid Plot • Intuitive genomics visualization tool • Built with Streamlit

""", unsafe_allow_html=True)