Spaces:
Runtime error
Runtime error
Delete app.py.py
Browse files
app.py.py
DELETED
|
@@ -1,334 +0,0 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import sqlite3
|
| 3 |
-
import pandas as pd
|
| 4 |
-
import plotly.express as px
|
| 5 |
-
from datetime import datetime
|
| 6 |
-
|
| 7 |
-
# Page configuration
|
| 8 |
-
st.set_page_config(
|
| 9 |
-
page_title="MicroID - Bacterial Identification System",
|
| 10 |
-
page_icon="🔬",
|
| 11 |
-
layout="wide"
|
| 12 |
-
)
|
| 13 |
-
|
| 14 |
-
# Initialize database
|
| 15 |
-
@st.cache_resource
|
| 16 |
-
def init_db():
|
| 17 |
-
conn = sqlite3.connect('microid.db', check_same_thread=False)
|
| 18 |
-
return conn
|
| 19 |
-
|
| 20 |
-
def load_tests():
|
| 21 |
-
conn = init_db()
|
| 22 |
-
df = pd.read_sql_query("SELECT id, test_code, test_name, position FROM biochemical_tests ORDER BY position", conn)
|
| 23 |
-
return df
|
| 24 |
-
|
| 25 |
-
def load_species():
|
| 26 |
-
conn = init_db()
|
| 27 |
-
df = pd.read_sql_query("SELECT id, full_name, gram_stain FROM species ORDER BY full_name", conn)
|
| 28 |
-
return df
|
| 29 |
-
|
| 30 |
-
def identify_organism(reactions):
|
| 31 |
-
conn = init_db()
|
| 32 |
-
|
| 33 |
-
# Get all species profiles
|
| 34 |
-
query = """
|
| 35 |
-
SELECT
|
| 36 |
-
s.id, s.full_name, s.gram_stain, s.morphology,
|
| 37 |
-
bt.position, sp.reaction as expected_reaction
|
| 38 |
-
FROM species s
|
| 39 |
-
JOIN species_profiles sp ON s.id = sp.species_id
|
| 40 |
-
JOIN biochemical_tests bt ON sp.test_id = bt.id
|
| 41 |
-
ORDER BY s.id, bt.position
|
| 42 |
-
"""
|
| 43 |
-
|
| 44 |
-
df = pd.read_sql_query(query, conn)
|
| 45 |
-
|
| 46 |
-
# Calculate matches for each species
|
| 47 |
-
results = []
|
| 48 |
-
for species_id in df['id'].unique():
|
| 49 |
-
species_data = df[df['id'] == species_id]
|
| 50 |
-
species_name = species_data['full_name'].iloc[0]
|
| 51 |
-
gram_stain = species_data['gram_stain'].iloc[0]
|
| 52 |
-
morphology = species_data['morphology'].iloc[0]
|
| 53 |
-
|
| 54 |
-
matches = 0
|
| 55 |
-
total = 0
|
| 56 |
-
variable_count = 0
|
| 57 |
-
|
| 58 |
-
for idx, row in species_data.iterrows():
|
| 59 |
-
pos = row['position'] - 1
|
| 60 |
-
if pos < len(reactions):
|
| 61 |
-
expected = row['expected_reaction']
|
| 62 |
-
actual = reactions[pos]
|
| 63 |
-
|
| 64 |
-
if expected != 'V':
|
| 65 |
-
total += 1
|
| 66 |
-
if expected == actual:
|
| 67 |
-
matches += 1
|
| 68 |
-
else:
|
| 69 |
-
total += 1
|
| 70 |
-
matches += 1
|
| 71 |
-
variable_count += 1
|
| 72 |
-
|
| 73 |
-
match_percent = (matches / total * 100) if total > 0 else 0
|
| 74 |
-
|
| 75 |
-
results.append({
|
| 76 |
-
'species': species_name,
|
| 77 |
-
'gram_stain': gram_stain,
|
| 78 |
-
'morphology': morphology,
|
| 79 |
-
'match_score': match_percent,
|
| 80 |
-
'matches': matches,
|
| 81 |
-
'total_tests': total,
|
| 82 |
-
'variable_tests': variable_count
|
| 83 |
-
})
|
| 84 |
-
|
| 85 |
-
# Sort by match score
|
| 86 |
-
results.sort(key=lambda x: x['match_score'], reverse=True)
|
| 87 |
-
|
| 88 |
-
# Calculate confidence (normalized)
|
| 89 |
-
top_matches = results[:5]
|
| 90 |
-
total_score = sum(m['match_score'] for m in top_matches)
|
| 91 |
-
for m in top_matches:
|
| 92 |
-
m['confidence'] = (m['match_score'] / total_score * 100) if total_score > 0 else 0
|
| 93 |
-
|
| 94 |
-
return top_matches
|
| 95 |
-
|
| 96 |
-
# Custom CSS
|
| 97 |
-
st.markdown("""
|
| 98 |
-
<style>
|
| 99 |
-
.main-header {
|
| 100 |
-
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 101 |
-
padding: 2rem;
|
| 102 |
-
border-radius: 10px;
|
| 103 |
-
margin-bottom: 2rem;
|
| 104 |
-
color: white;
|
| 105 |
-
text-align: center;
|
| 106 |
-
}
|
| 107 |
-
.test-card {
|
| 108 |
-
background: #f8f9fa;
|
| 109 |
-
padding: 10px;
|
| 110 |
-
border-radius: 8px;
|
| 111 |
-
text-align: center;
|
| 112 |
-
margin: 5px;
|
| 113 |
-
border: 1px solid #e0e0e0;
|
| 114 |
-
}
|
| 115 |
-
.positive {
|
| 116 |
-
background-color: #d4edda;
|
| 117 |
-
border-color: #28a745;
|
| 118 |
-
}
|
| 119 |
-
.negative {
|
| 120 |
-
background-color: #f8d7da;
|
| 121 |
-
border-color: #dc3545;
|
| 122 |
-
}
|
| 123 |
-
.result-card {
|
| 124 |
-
background: white;
|
| 125 |
-
padding: 1rem;
|
| 126 |
-
border-radius: 10px;
|
| 127 |
-
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
| 128 |
-
margin-bottom: 1rem;
|
| 129 |
-
border-left: 4px solid #667eea;
|
| 130 |
-
}
|
| 131 |
-
.confidence-high {
|
| 132 |
-
color: #28a745;
|
| 133 |
-
font-weight: bold;
|
| 134 |
-
}
|
| 135 |
-
.confidence-medium {
|
| 136 |
-
color: #ffc107;
|
| 137 |
-
font-weight: bold;
|
| 138 |
-
}
|
| 139 |
-
.confidence-low {
|
| 140 |
-
color: #dc3545;
|
| 141 |
-
font-weight: bold;
|
| 142 |
-
}
|
| 143 |
-
</style>
|
| 144 |
-
""", unsafe_allow_html=True)
|
| 145 |
-
|
| 146 |
-
# Header
|
| 147 |
-
st.markdown("""
|
| 148 |
-
<div class="main-header">
|
| 149 |
-
<h1>🔬 MicroID System</h1>
|
| 150 |
-
<p>API®-style Bacterial Identification using Biochemical Profiles</p>
|
| 151 |
-
<p style="font-size: 0.9rem; opacity: 0.9;">Based on API® 20E strip methodology</p>
|
| 152 |
-
</div>
|
| 153 |
-
""", unsafe_allow_html=True)
|
| 154 |
-
|
| 155 |
-
# Sidebar
|
| 156 |
-
with st.sidebar:
|
| 157 |
-
st.markdown("## 📊 About")
|
| 158 |
-
st.info("""
|
| 159 |
-
**MicroID** simulates the identification process used in clinical microbiology labs.
|
| 160 |
-
|
| 161 |
-
- Enter 20 biochemical reactions
|
| 162 |
-
- System matches against database
|
| 163 |
-
- Returns top species matches with confidence scores
|
| 164 |
-
|
| 165 |
-
*Educational purpose - Not for clinical use*
|
| 166 |
-
""")
|
| 167 |
-
|
| 168 |
-
st.markdown("## 🧪 Quick Examples")
|
| 169 |
-
col1, col2 = st.columns(2)
|
| 170 |
-
with col1:
|
| 171 |
-
if st.button("🦠 E. coli", use_container_width=True):
|
| 172 |
-
st.session_state['example'] = 'E. coli'
|
| 173 |
-
with col2:
|
| 174 |
-
if st.button("🧫 P. aeruginosa", use_container_width=True):
|
| 175 |
-
st.session_state['example'] = 'P. aeruginosa'
|
| 176 |
-
|
| 177 |
-
col3, col4 = st.columns(2)
|
| 178 |
-
with col3:
|
| 179 |
-
if st.button("🟡 S. aureus", use_container_width=True):
|
| 180 |
-
st.session_state['example'] = 'S. aureus'
|
| 181 |
-
with col4:
|
| 182 |
-
if st.button("🔄 Reset All", use_container_width=True):
|
| 183 |
-
st.session_state['example'] = 'reset'
|
| 184 |
-
|
| 185 |
-
st.markdown("---")
|
| 186 |
-
st.markdown("### 📈 Database Stats")
|
| 187 |
-
|
| 188 |
-
conn = init_db()
|
| 189 |
-
species_count = pd.read_sql_query("SELECT COUNT(*) as count FROM species", conn)['count'][0]
|
| 190 |
-
test_count = pd.read_sql_query("SELECT COUNT(*) as count FROM biochemical_tests", conn)['count'][0]
|
| 191 |
-
|
| 192 |
-
st.metric("Species in Database", species_count)
|
| 193 |
-
st.metric("Biochemical Tests", test_count)
|
| 194 |
-
|
| 195 |
-
# Main content
|
| 196 |
-
col1, col2 = st.columns([2, 1])
|
| 197 |
-
|
| 198 |
-
with col1:
|
| 199 |
-
st.markdown("## 🧬 Biochemical Test Reactions")
|
| 200 |
-
st.markdown("Select the reaction results for each test (20 positions)")
|
| 201 |
-
|
| 202 |
-
tests_df = load_tests()
|
| 203 |
-
|
| 204 |
-
# Initialize session state for reactions
|
| 205 |
-
if 'reactions' not in st.session_state:
|
| 206 |
-
st.session_state.reactions = ['-'] * 20
|
| 207 |
-
|
| 208 |
-
# Handle example loading
|
| 209 |
-
examples = {
|
| 210 |
-
'E. coli': ['+','-','+','+','-','-','-','-','+','-','-','+','+','-','+','+','+','+','-','+'],
|
| 211 |
-
'P. aeruginosa': ['-','+','-','-','+','-','-','-','-','-','+','+','-','-','-','-','-','-','-','-'],
|
| 212 |
-
'S. aureus': ['+','-','-','-','V','-','+','-','-','+','V','+','+','-','+','V','+','-','V','-']
|
| 213 |
-
}
|
| 214 |
-
|
| 215 |
-
if 'example' in st.session_state:
|
| 216 |
-
if st.session_state.example == 'reset':
|
| 217 |
-
st.session_state.reactions = ['-'] * 20
|
| 218 |
-
elif st.session_state.example in examples:
|
| 219 |
-
st.session_state.reactions = examples[st.session_state.example]
|
| 220 |
-
del st.session_state.example
|
| 221 |
-
st.rerun()
|
| 222 |
-
|
| 223 |
-
# Create grid of test buttons
|
| 224 |
-
cols_per_row = 5
|
| 225 |
-
for i in range(0, 20, cols_per_row):
|
| 226 |
-
cols = st.columns(cols_per_row)
|
| 227 |
-
for j in range(cols_per_row):
|
| 228 |
-
idx = i + j
|
| 229 |
-
if idx < 20:
|
| 230 |
-
test = tests_df.iloc[idx]
|
| 231 |
-
with cols[j]:
|
| 232 |
-
with st.container():
|
| 233 |
-
st.markdown(f"**{test['test_code']}**")
|
| 234 |
-
st.caption(test['test_name'])
|
| 235 |
-
|
| 236 |
-
# Reaction buttons
|
| 237 |
-
col_a, col_b = st.columns(2)
|
| 238 |
-
with col_a:
|
| 239 |
-
if st.button(f"+", key=f"pos_{idx}",
|
| 240 |
-
use_container_width=True,
|
| 241 |
-
type="primary" if st.session_state.reactions[idx] == '+' else "secondary"):
|
| 242 |
-
st.session_state.reactions[idx] = '+'
|
| 243 |
-
st.rerun()
|
| 244 |
-
with col_b:
|
| 245 |
-
if st.button(f"-", key=f"neg_{idx}",
|
| 246 |
-
use_container_width=True,
|
| 247 |
-
type="primary" if st.session_state.reactions[idx] == '-' else "secondary"):
|
| 248 |
-
st.session_state.reactions[idx] = '-'
|
| 249 |
-
st.rerun()
|
| 250 |
-
|
| 251 |
-
# Current profile display
|
| 252 |
-
st.markdown("---")
|
| 253 |
-
current_profile = ''.join(st.session_state.reactions)
|
| 254 |
-
st.markdown(f"**Current Profile Code:** `{current_profile}`")
|
| 255 |
-
|
| 256 |
-
# Identify button
|
| 257 |
-
if st.button("🔍 IDENTIFY ORGANISM", type="primary", use_container_width=True):
|
| 258 |
-
with st.spinner("Analyzing biochemical profile..."):
|
| 259 |
-
results = identify_organism(st.session_state.reactions)
|
| 260 |
-
st.session_state.results = results
|
| 261 |
-
|
| 262 |
-
with col2:
|
| 263 |
-
st.markdown("## 🎯 Identification Results")
|
| 264 |
-
|
| 265 |
-
if 'results' in st.session_state and st.session_state.results:
|
| 266 |
-
results = st.session_state.results
|
| 267 |
-
|
| 268 |
-
# Main identification
|
| 269 |
-
top_result = results[0]
|
| 270 |
-
|
| 271 |
-
if top_result['confidence'] > 70:
|
| 272 |
-
confidence_class = "confidence-high"
|
| 273 |
-
confidence_text = "High Confidence"
|
| 274 |
-
elif top_result['confidence'] > 40:
|
| 275 |
-
confidence_class = "confidence-medium"
|
| 276 |
-
confidence_text = "Medium Confidence"
|
| 277 |
-
else:
|
| 278 |
-
confidence_class = "confidence-low"
|
| 279 |
-
confidence_text = "Low Confidence"
|
| 280 |
-
|
| 281 |
-
st.markdown(f"""
|
| 282 |
-
<div class="result-card">
|
| 283 |
-
<h3>🏆 Top Match</h3>
|
| 284 |
-
<h2>{top_result['species']}</h2>
|
| 285 |
-
<p><strong>Gram Stain:</strong> {top_result['gram_stain'] or 'N/A'}</p>
|
| 286 |
-
<p><strong>Morphology:</strong> {top_result['morphology'] or 'N/A'}</p>
|
| 287 |
-
<p><strong>Match Score:</strong> {top_result['match_score']:.1f}%</p>
|
| 288 |
-
<p><strong>Confidence:</strong> <span class="{confidence_class}">{top_result['confidence']:.1f}% ({confidence_text})</span></p>
|
| 289 |
-
<p><strong>Tests Matched:</strong> {top_result['matches']}/{top_result['total_tests']}</p>
|
| 290 |
-
{f'<p>⚠️ Variable reactions: {top_result["variable_tests"]}</p>' if top_result['variable_tests'] > 0 else ''}
|
| 291 |
-
</div>
|
| 292 |
-
""", unsafe_allow_html=True)
|
| 293 |
-
|
| 294 |
-
# Other matches
|
| 295 |
-
st.markdown("### 📋 Other Possible Matches")
|
| 296 |
-
for result in results[1:4]:
|
| 297 |
-
st.markdown(f"""
|
| 298 |
-
<div class="result-card">
|
| 299 |
-
<strong>{result['species']}</strong><br>
|
| 300 |
-
Match: {result['match_score']:.1f}% | Confidence: {result['confidence']:.1f}%
|
| 301 |
-
</div>
|
| 302 |
-
""", unsafe_allow_html=True)
|
| 303 |
-
|
| 304 |
-
# Create match chart
|
| 305 |
-
match_data = pd.DataFrame(results[:5])
|
| 306 |
-
fig = px.bar(match_data, x='species', y='match_score',
|
| 307 |
-
title='Match Scores by Species',
|
| 308 |
-
labels={'match_score': 'Match Percentage', 'species': 'Species'},
|
| 309 |
-
color='match_score',
|
| 310 |
-
color_continuous_scale='Viridis')
|
| 311 |
-
fig.update_layout(height=300, showlegend=False)
|
| 312 |
-
st.plotly_chart(fig, use_container_width=True)
|
| 313 |
-
|
| 314 |
-
else:
|
| 315 |
-
st.info("👈 Enter biochemical reactions and click 'IDENTIFY ORGANISM' to see results")
|
| 316 |
-
|
| 317 |
-
# Show example of what results look like
|
| 318 |
-
st.markdown("""
|
| 319 |
-
### 📖 How it works:
|
| 320 |
-
1. Select + or - for each of the 20 tests
|
| 321 |
-
2. Click "IDENTIFY ORGANISM"
|
| 322 |
-
3. System compares against database
|
| 323 |
-
4. Returns ranked matches with confidence scores
|
| 324 |
-
|
| 325 |
-
**Try the example buttons in the sidebar!**
|
| 326 |
-
""")
|
| 327 |
-
|
| 328 |
-
# Footer
|
| 329 |
-
st.markdown("---")
|
| 330 |
-
st.markdown("""
|
| 331 |
-
<div style="text-align: center; color: #666; font-size: 0.8rem;">
|
| 332 |
-
🔬 MicroID - Educational Bacterial Identification System | Based on API® 20E methodology
|
| 333 |
-
</div>
|
| 334 |
-
""", unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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