Update app.py
Browse filestesting the addition of graphing functionality
app.py
CHANGED
@@ -2,53 +2,76 @@ import streamlit as st
|
|
2 |
from utils import validate_sequence, predict
|
3 |
from model import models
|
4 |
import pandas as pd
|
|
|
|
|
5 |
|
6 |
def main():
|
7 |
st.set_page_config(layout="wide") # Keep the wide layout for overall flexibility
|
8 |
st.title("AA Property Inference Demo", anchor=None)
|
9 |
|
10 |
-
#
|
11 |
st.markdown("""
|
12 |
<style>
|
13 |
.reportview-container {
|
14 |
font-family: 'Courier New', monospace;
|
15 |
}
|
16 |
-
.css-1d391kg {
|
17 |
-
padding: 0.25rem 1rem; # Adjust padding if necessary for better spacing
|
18 |
-
}
|
19 |
</style>
|
20 |
""", unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
21 |
|
22 |
-
#
|
23 |
-
|
24 |
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
if
|
30 |
-
|
31 |
-
|
32 |
-
df = pd.read_csv(uploaded_file)
|
33 |
-
sequences.extend(df['sequence'].tolist())
|
34 |
|
35 |
-
|
|
|
|
|
36 |
for seq in sequences:
|
37 |
if validate_sequence(seq):
|
38 |
model_results = {}
|
|
|
39 |
for model_name, model in models.items():
|
40 |
prediction, confidence = predict(model, seq)
|
41 |
model_results[f"{model_name}_prediction"] = prediction
|
42 |
model_results[f"{model_name}_confidence"] = round(confidence, 3)
|
|
|
43 |
results.append({"Sequence": seq, **model_results})
|
|
|
44 |
else:
|
45 |
-
st.error(f"Invalid sequence: {seq}")
|
46 |
|
47 |
if results:
|
48 |
-
st.write("### Results")
|
49 |
results_df = pd.DataFrame(results)
|
50 |
-
|
51 |
st.dataframe(results_df.style.format(precision=3), width=None, height=None)
|
52 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
if __name__ == "__main__":
|
54 |
main()
|
|
|
2 |
from utils import validate_sequence, predict
|
3 |
from model import models
|
4 |
import pandas as pd
|
5 |
+
import matplotlib.pyplot as plt
|
6 |
+
import seaborn as sns
|
7 |
|
8 |
def main():
|
9 |
st.set_page_config(layout="wide") # Keep the wide layout for overall flexibility
|
10 |
st.title("AA Property Inference Demo", anchor=None)
|
11 |
|
12 |
+
# Styling for the app to use monospace font
|
13 |
st.markdown("""
|
14 |
<style>
|
15 |
.reportview-container {
|
16 |
font-family: 'Courier New', monospace;
|
17 |
}
|
|
|
|
|
|
|
18 |
</style>
|
19 |
""", unsafe_allow_html=True)
|
20 |
+
|
21 |
+
# Input section in the sidebar
|
22 |
+
sequence = st.sidebar.text_input("Enter your amino acid sequence:")
|
23 |
+
uploaded_file = st.sidebar.file_uploader("Or upload a CSV file with amino acid sequences", type="csv")
|
24 |
|
25 |
+
# Button to analyze sequence
|
26 |
+
analyze_pressed = st.sidebar.button("Analyze Sequence")
|
27 |
|
28 |
+
# Show graphs toggle
|
29 |
+
show_graphs = st.sidebar.checkbox("Show Prediction Graphs")
|
30 |
+
|
31 |
+
sequences = [sequence] if sequence else []
|
32 |
+
if uploaded_file:
|
33 |
+
df = pd.read_csv(uploaded_file)
|
34 |
+
sequences.extend(df['sequence'].tolist())
|
|
|
|
|
35 |
|
36 |
+
results = []
|
37 |
+
all_data = {}
|
38 |
+
if analyze_pressed:
|
39 |
for seq in sequences:
|
40 |
if validate_sequence(seq):
|
41 |
model_results = {}
|
42 |
+
graph_data = {}
|
43 |
for model_name, model in models.items():
|
44 |
prediction, confidence = predict(model, seq)
|
45 |
model_results[f"{model_name}_prediction"] = prediction
|
46 |
model_results[f"{model_name}_confidence"] = round(confidence, 3)
|
47 |
+
graph_data[model_name] = (prediction, confidence)
|
48 |
results.append({"Sequence": seq, **model_results})
|
49 |
+
all_data[seq] = graph_data
|
50 |
else:
|
51 |
+
st.sidebar.error(f"Invalid sequence: {seq}")
|
52 |
|
53 |
if results:
|
|
|
54 |
results_df = pd.DataFrame(results)
|
55 |
+
st.write("### Results")
|
56 |
st.dataframe(results_df.style.format(precision=3), width=None, height=None)
|
57 |
|
58 |
+
# Display graphs in sidebar if toggle is active
|
59 |
+
if show_graphs and all_data:
|
60 |
+
plot_prediction_graphs(all_data)
|
61 |
+
|
62 |
+
def plot_prediction_graphs(data):
|
63 |
+
# Function to plot graphs for predictions in the sidebar
|
64 |
+
for model_name in models.keys():
|
65 |
+
plt.figure(figsize=(10, 4))
|
66 |
+
predictions = {seq: values[model_name][0] for seq, values in data.items()}
|
67 |
+
confidences = {seq: values[model_name][1] for seq, values in data.items()}
|
68 |
+
sequences = list(predictions.keys())
|
69 |
+
conf_values = list(confidences.values())
|
70 |
+
sns.barplot(x=sequences, y=conf_values, palette="viridis")
|
71 |
+
plt.title(f'Confidence Scores for {model_name.capitalize()} Model')
|
72 |
+
plt.xlabel('Sequences')
|
73 |
+
plt.ylabel('Confidence')
|
74 |
+
st.sidebar.pyplot(plt) # Display each plot in the sidebar
|
75 |
+
|
76 |
if __name__ == "__main__":
|
77 |
main()
|