Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -24,32 +24,14 @@ logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(
|
|
| 24 |
# Core Classes and Functions
|
| 25 |
# ---------------------------
|
| 26 |
class GenBankParser:
|
| 27 |
-
def __init__(self,
|
| 28 |
-
self.
|
| 29 |
self.records = []
|
| 30 |
|
| 31 |
def load_records(self):
|
| 32 |
-
#
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
text_io = io.StringIO(self.fileobj.read().decode("utf-8"))
|
| 36 |
-
elif isinstance(self.fileobj, bytes):
|
| 37 |
-
text_io = io.StringIO(self.fileobj.decode("utf-8"))
|
| 38 |
-
elif isinstance(self.fileobj, str):
|
| 39 |
-
text_io = io.StringIO(self.fileobj)
|
| 40 |
-
else:
|
| 41 |
-
# Might be a StringIO or already a text-mode file
|
| 42 |
-
try:
|
| 43 |
-
self.fileobj.seek(0)
|
| 44 |
-
_peek = self.fileobj.read(200)
|
| 45 |
-
self.fileobj.seek(0)
|
| 46 |
-
if isinstance(_peek, bytes):
|
| 47 |
-
text_io = io.StringIO(self.fileobj.read().decode("utf-8"))
|
| 48 |
-
else:
|
| 49 |
-
text_io = self.fileobj
|
| 50 |
-
except Exception:
|
| 51 |
-
text_io = self.fileobj
|
| 52 |
-
self.records = list(SeqIO.parse(text_io, "genbank"))
|
| 53 |
return self.records
|
| 54 |
|
| 55 |
class AnalysisEngine:
|
|
@@ -127,12 +109,22 @@ with st.expander("ℹ️ About GeneBank Genie"):
|
|
| 127 |
st.sidebar.header("Step 1. Upload GenBank File")
|
| 128 |
gb_file = st.sidebar.file_uploader("Upload a GenBank file (.gb, .gbk)", type=["gb", "gbk"])
|
| 129 |
if gb_file is not None:
|
| 130 |
-
#
|
| 131 |
gb_file.seek(0)
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
else:
|
| 137 |
records = []
|
| 138 |
st.warning("Upload a GenBank file to get started.")
|
|
|
|
| 24 |
# Core Classes and Functions
|
| 25 |
# ---------------------------
|
| 26 |
class GenBankParser:
|
| 27 |
+
def __init__(self, text_io):
|
| 28 |
+
self.text_io = text_io
|
| 29 |
self.records = []
|
| 30 |
|
| 31 |
def load_records(self):
|
| 32 |
+
# Always seek to start (for re-reads)
|
| 33 |
+
self.text_io.seek(0)
|
| 34 |
+
self.records = list(SeqIO.parse(self.text_io, "genbank"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
return self.records
|
| 36 |
|
| 37 |
class AnalysisEngine:
|
|
|
|
| 109 |
st.sidebar.header("Step 1. Upload GenBank File")
|
| 110 |
gb_file = st.sidebar.file_uploader("Upload a GenBank file (.gb, .gbk)", type=["gb", "gbk"])
|
| 111 |
if gb_file is not None:
|
| 112 |
+
# Always wrap the file as text for Biopython
|
| 113 |
gb_file.seek(0)
|
| 114 |
+
gb_bytes = gb_file.read()
|
| 115 |
+
if isinstance(gb_bytes, bytes):
|
| 116 |
+
gb_text = gb_bytes.decode("utf-8")
|
| 117 |
+
else:
|
| 118 |
+
gb_text = gb_bytes
|
| 119 |
+
gb_text_io = io.StringIO(gb_text)
|
| 120 |
+
parser = GenBankParser(gb_text_io)
|
| 121 |
+
try:
|
| 122 |
+
records = parser.load_records()
|
| 123 |
+
except Exception as e:
|
| 124 |
+
st.error(f"Error parsing GenBank file: {e}")
|
| 125 |
+
records = []
|
| 126 |
+
else:
|
| 127 |
+
st.sidebar.success(f"Loaded {len(records)} GenBank records.")
|
| 128 |
else:
|
| 129 |
records = []
|
| 130 |
st.warning("Upload a GenBank file to get started.")
|