29 DNA Analyzer with Streamlit
Today we will build a functioning Streamlit application that takes a DNA sequence as input and outputs basic biological metrics and a visualization.
Starter code
Complete the following starter code:
########## import correct python libraries here ##########
# Page config
st.set_page_config(page_title="DNA Sequence Analyzer", layout="wide")
########## Add title and description for the app ##########
########## Add a side bar for data input ##########
# Clean the sequence
sequence = sequence_input.upper().replace(" ", "").replace("\n", "")
if sequence:
st.subheader("Sequence Statistics")
# Calculations
seq_length = len(sequence)
a_count = sequence.count('A')
t_count = sequence.count('T')
g_count = sequence.count('G')
c_count = sequence.count('C')
# Calculate GC content safely
if seq_length > 0:
gc_content = ((g_count + c_count) / seq_length) * 100
else:
gc_content = 0.0
######## Add metrics for sequence length and GC content statistics in two equally spaced columns ##########
st.divider()
# Create a DataFrame for the counts
st.subheader("Nucleotide Frequencies")
nucleotide_dict = {
'Nucleotide': ['A', 'T', 'G', 'C'],
'Count': [a_count, t_count, g_count, c_count]
}
df = pd.DataFrame(nucleotide_dict)
# Display table and chart side-by-side
col3, col4 = st.columns(2)
with col3:
########## Display DataFrame as table ########
with col4:
# Streamlit's native bar chart requires the index to be the categorical variable
chart_data = df.set_index('Nucleotide')
st.bar_chart(chart_data)
else:
st.warning("Please enter a DNA sequence to begin analysis.")Assignment
After testing and completing your app, submit the following on Blackboard: a copy of your completed script and a screenshot of the app running in your browser.