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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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)
|
init_db.py
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sqlite3
|
| 2 |
+
import os
|
| 3 |
+
|
| 4 |
+
def init_database():
|
| 5 |
+
"""Initialize SQLite database with species and biochemical profiles"""
|
| 6 |
+
|
| 7 |
+
conn = sqlite3.connect('microid.db')
|
| 8 |
+
cursor = conn.cursor()
|
| 9 |
+
|
| 10 |
+
# Create tables
|
| 11 |
+
cursor.execute('''
|
| 12 |
+
CREATE TABLE IF NOT EXISTS species (
|
| 13 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 14 |
+
genus TEXT NOT NULL,
|
| 15 |
+
species TEXT NOT NULL,
|
| 16 |
+
full_name TEXT UNIQUE NOT NULL,
|
| 17 |
+
gram_stain TEXT,
|
| 18 |
+
morphology TEXT,
|
| 19 |
+
notes TEXT
|
| 20 |
+
)
|
| 21 |
+
''')
|
| 22 |
+
|
| 23 |
+
cursor.execute('''
|
| 24 |
+
CREATE TABLE IF NOT EXISTS biochemical_tests (
|
| 25 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 26 |
+
test_code TEXT UNIQUE NOT NULL,
|
| 27 |
+
test_name TEXT NOT NULL,
|
| 28 |
+
description TEXT,
|
| 29 |
+
position INTEGER
|
| 30 |
+
)
|
| 31 |
+
''')
|
| 32 |
+
|
| 33 |
+
cursor.execute('''
|
| 34 |
+
CREATE TABLE IF NOT EXISTS species_profiles (
|
| 35 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 36 |
+
species_id INTEGER,
|
| 37 |
+
test_id INTEGER,
|
| 38 |
+
reaction TEXT NOT NULL,
|
| 39 |
+
probability REAL DEFAULT 0.95,
|
| 40 |
+
FOREIGN KEY (species_id) REFERENCES species(id),
|
| 41 |
+
FOREIGN KEY (test_id) REFERENCES biochemical_tests(id)
|
| 42 |
+
)
|
| 43 |
+
''')
|
| 44 |
+
|
| 45 |
+
# Insert biochemical tests
|
| 46 |
+
tests = [
|
| 47 |
+
(1, 'ONPG', 'Beta-galactosidase', 1),
|
| 48 |
+
(2, 'ADH', 'Arginine dihydrolase', 2),
|
| 49 |
+
(3, 'LDC', 'Lysine decarboxylase', 3),
|
| 50 |
+
(4, 'ODC', 'Ornithine decarboxylase', 4),
|
| 51 |
+
(5, 'CIT', 'Citrate utilization', 5),
|
| 52 |
+
(6, 'H2S', 'Hydrogen sulfide', 6),
|
| 53 |
+
(7, 'URE', 'Urease', 7),
|
| 54 |
+
(8, 'TDA', 'Tryptophan deaminase', 8),
|
| 55 |
+
(9, 'IND', 'Indole production', 9),
|
| 56 |
+
(10, 'VP', 'Voges-Proskauer', 10),
|
| 57 |
+
(11, 'GEL', 'Gelatinase', 11),
|
| 58 |
+
(12, 'GLU', 'Glucose fermentation', 12),
|
| 59 |
+
(13, 'MAN', 'Mannitol', 13),
|
| 60 |
+
(14, 'INO', 'Inositol', 14),
|
| 61 |
+
(15, 'SOR', 'Sorbitol', 15),
|
| 62 |
+
(16, 'RHA', 'Rhamnose', 16),
|
| 63 |
+
(17, 'SAC', 'Sucrose', 17),
|
| 64 |
+
(18, 'MEL', 'Melibiose', 18),
|
| 65 |
+
(19, 'AMY', 'Amygdalin', 19),
|
| 66 |
+
(20, 'ARA', 'Arabinose', 20)
|
| 67 |
+
]
|
| 68 |
+
|
| 69 |
+
for test in tests:
|
| 70 |
+
cursor.execute('''
|
| 71 |
+
INSERT OR IGNORE INTO biochemical_tests (id, test_code, test_name, position)
|
| 72 |
+
VALUES (?, ?, ?, ?)
|
| 73 |
+
''', test)
|
| 74 |
+
|
| 75 |
+
# Insert species
|
| 76 |
+
species_list = [
|
| 77 |
+
('Escherichia', 'coli', 'Escherichia coli', 'negative', 'rod'),
|
| 78 |
+
('Pseudomonas', 'aeruginosa', 'Pseudomonas aeruginosa', 'negative', 'rod'),
|
| 79 |
+
('Staphylococcus', 'aureus', 'Staphylococcus aureus', 'positive', 'coccus'),
|
| 80 |
+
('Streptococcus', 'pneumoniae', 'Streptococcus pneumoniae', 'positive', 'coccus'),
|
| 81 |
+
('Klebsiella', 'pneumoniae', 'Klebsiella pneumoniae', 'negative', 'rod'),
|
| 82 |
+
('Proteus', 'mirabilis', 'Proteus mirabilis', 'negative', 'rod'),
|
| 83 |
+
('Enterococcus', 'faecalis', 'Enterococcus faecalis', 'positive', 'coccus'),
|
| 84 |
+
('Neisseria', 'gonorrhoeae', 'Neisseria gonorrhoeae', 'negative', 'coccus'),
|
| 85 |
+
('Haemophilus', 'influenzae', 'Haemophilus influenzae', 'negative', 'rod'),
|
| 86 |
+
('Candida', 'albicans', 'Candida albicans', 'yeast', 'yeast'),
|
| 87 |
+
('Salmonella', 'typhimurium', 'Salmonella typhimurium', 'negative', 'rod'),
|
| 88 |
+
('Shigella', 'flexneri', 'Shigella flexneri', 'negative', 'rod'),
|
| 89 |
+
('Bacillus', 'subtilis', 'Bacillus subtilis', 'positive', 'rod'),
|
| 90 |
+
('Clostridium', 'perfringens', 'Clostridium perfringens', 'positive', 'rod'),
|
| 91 |
+
('Listeria', 'monocytogenes', 'Listeria monocytogenes', 'positive', 'rod'),
|
| 92 |
+
('Acinetobacter', 'baumannii', 'Acinetobacter baumannii', 'negative', 'coccus'),
|
| 93 |
+
('Enterobacter', 'cloacae', 'Enterobacter cloacae', 'negative', 'rod'),
|
| 94 |
+
('Serratia', 'marcescens', 'Serratia marcescens', 'negative', 'rod'),
|
| 95 |
+
('Citrobacter', 'freundii', 'Citrobacter freundii', 'negative', 'rod'),
|
| 96 |
+
('Morganella', 'morganii', 'Morganella morganii', 'negative', 'rod'),
|
| 97 |
+
('Yersinia', 'enterocolitica', 'Yersinia enterocolitica', 'negative', 'rod'),
|
| 98 |
+
('Vibrio', 'cholerae', 'Vibrio cholerae', 'negative', 'rod'),
|
| 99 |
+
('Campylobacter', 'jejuni', 'Campylobacter jejuni', 'negative', 'curved rod'),
|
| 100 |
+
('Bacteroides', 'fragilis', 'Bacteroides fragilis', 'negative', 'rod'),
|
| 101 |
+
('Lactobacillus', 'acidophilus', 'Lactobacillus acidophilus', 'positive', 'rod'),
|
| 102 |
+
('Streptococcus', 'pyogenes', 'Streptococcus pyogenes', 'positive', 'coccus'),
|
| 103 |
+
('Staphylococcus', 'epidermidis', 'Staphylococcus epidermidis', 'positive', 'coccus'),
|
| 104 |
+
('Candida', 'glabrata', 'Candida glabrata', 'yeast', 'yeast'),
|
| 105 |
+
('Candida', 'parapsilosis', 'Candida parapsilosis', 'yeast', 'yeast'),
|
| 106 |
+
('Candida', 'tropicalis', 'Candida tropicalis', 'yeast', 'yeast'),
|
| 107 |
+
('Cryptococcus', 'neoformans', 'Cryptococcus neoformans', 'yeast', 'yeast')
|
| 108 |
+
]
|
| 109 |
+
|
| 110 |
+
for species in species_list:
|
| 111 |
+
cursor.execute('''
|
| 112 |
+
INSERT OR IGNORE INTO species (genus, species, full_name, gram_stain, morphology)
|
| 113 |
+
VALUES (?, ?, ?, ?, ?)
|
| 114 |
+
''', species)
|
| 115 |
+
|
| 116 |
+
# Get species IDs
|
| 117 |
+
cursor.execute('SELECT id, full_name FROM species')
|
| 118 |
+
species_map = {row[1]: row[0] for row in cursor.fetchall()}
|
| 119 |
+
|
| 120 |
+
# Get test IDs
|
| 121 |
+
cursor.execute('SELECT id, test_code FROM biochemical_tests')
|
| 122 |
+
test_map = {row[1]: row[0] for row in cursor.fetchall()}
|
| 123 |
+
|
| 124 |
+
# Insert profiles (simplified - add more for full database)
|
| 125 |
+
profiles = {
|
| 126 |
+
'Escherichia coli': {
|
| 127 |
+
'ONPG':'+', 'ADH':'-', 'LDC':'+', 'ODC':'+', 'CIT':'-',
|
| 128 |
+
'H2S':'-', 'URE':'-', 'TDA':'-', 'IND':'+', 'VP':'-',
|
| 129 |
+
'GEL':'-', 'GLU':'+', 'MAN':'+', 'INO':'-', 'SOR':'+',
|
| 130 |
+
'RHA':'+', 'SAC':'+', 'MEL':'+', 'AMY':'-', 'ARA':'+'
|
| 131 |
+
},
|
| 132 |
+
'Pseudomonas aeruginosa': {
|
| 133 |
+
'ONPG':'-', 'ADH':'+', 'LDC':'-', 'ODC':'-', 'CIT':'+',
|
| 134 |
+
'H2S':'-', 'URE':'-', 'TDA':'-', 'IND':'-', 'VP':'-',
|
| 135 |
+
'GEL':'+', 'GLU':'+', 'MAN':'-', 'INO':'-', 'SOR':'-',
|
| 136 |
+
'RHA':'-', 'SAC':'-', 'MEL':'-', 'AMY':'-', 'ARA':'-'
|
| 137 |
+
},
|
| 138 |
+
'Staphylococcus aureus': {
|
| 139 |
+
'ONPG':'+', 'ADH':'-', 'LDC':'-', 'ODC':'-', 'CIT':'V',
|
| 140 |
+
'H2S':'-', 'URE':'+', 'TDA':'-', 'IND':'-', 'VP':'+',
|
| 141 |
+
'GEL':'V', 'GLU':'+', 'MAN':'+', 'INO':'-', 'SOR':'+',
|
| 142 |
+
'RHA':'V', 'SAC':'+', 'MEL':'-', 'AMY':'V', 'ARA':'-'
|
| 143 |
+
}
|
| 144 |
+
}
|
| 145 |
+
|
| 146 |
+
# Add more species profiles (I'll add more in the complete version)
|
| 147 |
+
for species_name, reactions in profiles.items():
|
| 148 |
+
if species_name in species_map:
|
| 149 |
+
species_id = species_map[species_name]
|
| 150 |
+
for test_code, reaction in reactions.items():
|
| 151 |
+
test_id = test_map[test_code]
|
| 152 |
+
cursor.execute('''
|
| 153 |
+
INSERT OR IGNORE INTO species_profiles (species_id, test_id, reaction)
|
| 154 |
+
VALUES (?, ?, ?)
|
| 155 |
+
''', (species_id, test_id, reaction))
|
| 156 |
+
|
| 157 |
+
conn.commit()
|
| 158 |
+
conn.close()
|
| 159 |
+
print("Database initialized successfully!")
|
| 160 |
+
|
| 161 |
+
if __name__ == "__main__":
|
| 162 |
+
init_database()
|