Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.gitignore
/venv
/.pytest_cache
/.pytest_cache
__pycache__/
*.pyc
4 changes: 4 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2024-05-23 - [Regex Pre-compilation in Loops]
**Learning:** Pre-compiling regular expressions (`re.compile`) at the module level provides a significant performance boost (measured ~1.8x speedup) when the regex is used inside a tight loop or a pandas `apply` function, compared to compiling it repeatedly or implicitly inside the loop. Vectorized string operations in Pandas are usually faster, but in complex logic cases (multiple prioritized regex groups + fallback logic), a simple pre-compiled regex with `apply` can sometimes be cleaner and sufficiently fast, or even faster if the vectorized approach requires multiple passes or expensive intermediate structures.
**Action:** Always check for regex usage in loops or `apply` calls. If found, refactor to use module-level pre-compiled patterns. When considering vectorization, benchmark against the optimized loop version, as the overhead of complex vectorization might outweigh the benefits for moderate dataset sizes.

## 2024-10-24 - [Streamlit File Streaming Optimization]
**Learning:** Loading large files in Streamlit using `file.read().decode()` creates a massive memory spike (approx 3x file size) because it loads the full bytes, converts to full string, and then wraps in StringIO. Wrapping the `UploadedFile` (which is `BytesIO`-like) in `io.TextIOWrapper` allows for streaming decoding, significantly reducing memory footprint.
**Action:** Use `io.TextIOWrapper(uploaded_file, encoding='utf-8')` instead of `io.StringIO(uploaded_file.read().decode('utf-8'))` for large text files, provided the downstream library (like `pyteomics.mgf`) supports non-seekable or wrapped streams (note: `mztab` parser does NOT support this currently).
Binary file added __pycache__/data_loading.cpython-312.pyc
Binary file not shown.
Binary file added __pycache__/processing.cpython-312.pyc
Binary file not shown.
10 changes: 8 additions & 2 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,14 @@ def run_streamlit_app():
# Process files only when both are uploaded
if mgf_file and mztab_file:
# Decode uploaded file contents (Streamlit files are bytes by default)
# Use StringIO to create file-like objects for pyteomics parsers
spectra = load_mgf(io.StringIO(mgf_file.read().decode('utf-8')))

# ⚑ OPTIMIZATION: Use TextIOWrapper for MGF to stream-decode bytes.
# This prevents loading the entire file into memory as a decoded string (3x memory savings).
# mgf_file (UploadedFile) is seekable, which works with pyteomics.mgf.read.
spectra = load_mgf(io.TextIOWrapper(mgf_file, encoding='utf-8'))

# Note: mzTab parser has known issues with TextIOWrapper, so we keep the
# read().decode() -> StringIO pattern for stability.
psm_df = load_mztab(io.StringIO(mztab_file.read().decode('utf-8')))

# Create mappings between PSMs and spectra
Expand Down
Binary file added tests/__pycache__/__init__.cpython-312.pyc
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.