Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,27 +1,32 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import zipfile
|
| 3 |
import sqlite3
|
| 4 |
-
import
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
#
|
| 7 |
def get_connection_from_zip(zip_path: str, db_filename: str) -> sqlite3.Connection:
|
| 8 |
-
with zipfile.ZipFile(zip_path) as
|
| 9 |
-
with
|
| 10 |
-
|
| 11 |
-
|
|
|
|
| 12 |
|
| 13 |
-
|
|
|
|
|
|
|
| 14 |
conn = get_connection_from_zip("proteins.db.zip", "proteins.db")
|
| 15 |
cursor = conn.cursor()
|
| 16 |
|
| 17 |
-
# Query
|
| 18 |
def search_by_sequence(seq: str) -> str:
|
| 19 |
seq = seq.strip().upper()
|
| 20 |
cursor.execute("SELECT * FROM proteins WHERE sequence = ?", (seq,))
|
| 21 |
row = cursor.fetchone()
|
| 22 |
|
| 23 |
if not row:
|
| 24 |
-
return
|
| 25 |
|
| 26 |
col_names = [desc[0] for desc in cursor.description]
|
| 27 |
return "\n".join(f"{k}: {v}" for k, v in zip(col_names, row))
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import zipfile
|
| 3 |
import sqlite3
|
| 4 |
+
import tempfile
|
| 5 |
+
import shutil
|
| 6 |
+
import os
|
| 7 |
|
| 8 |
+
# Extract DB from ZIP to a temporary file
|
| 9 |
def get_connection_from_zip(zip_path: str, db_filename: str) -> sqlite3.Connection:
|
| 10 |
+
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
|
| 11 |
+
with zip_ref.open(db_filename) as db_file:
|
| 12 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".db") as tmp_file:
|
| 13 |
+
shutil.copyfileobj(db_file, tmp_file)
|
| 14 |
+
tmp_db_path = tmp_file.name
|
| 15 |
|
| 16 |
+
return sqlite3.connect(tmp_db_path)
|
| 17 |
+
|
| 18 |
+
# Open connection at startup
|
| 19 |
conn = get_connection_from_zip("proteins.db.zip", "proteins.db")
|
| 20 |
cursor = conn.cursor()
|
| 21 |
|
| 22 |
+
# Query by protein sequence
|
| 23 |
def search_by_sequence(seq: str) -> str:
|
| 24 |
seq = seq.strip().upper()
|
| 25 |
cursor.execute("SELECT * FROM proteins WHERE sequence = ?", (seq,))
|
| 26 |
row = cursor.fetchone()
|
| 27 |
|
| 28 |
if not row:
|
| 29 |
+
return "No protein found for the given sequence."
|
| 30 |
|
| 31 |
col_names = [desc[0] for desc in cursor.description]
|
| 32 |
return "\n".join(f"{k}: {v}" for k, v in zip(col_names, row))
|