Spaces:
Running
Running
Add ColiFormer Streamlit app for Hugging Face Spaces
Browse files- Complete Streamlit application for E. coli codon optimization
- Auto-downloads model from saketh11/ColiFormer
- Auto-downloads reference data from saketh11/ColiFormer-Data
- Comprehensive metrics: CAI, tAI, GC content, codon usage
- Interactive sequence optimization with real-time feedback
- Export capabilities (FASTA, Excel)
- Proper Hugging Face Spaces metadata and documentation
- 6.2% better CAI performance vs base model
- README.md +92 -7
- app.py +1472 -0
- requirements.txt +20 -0
README.md
CHANGED
|
@@ -1,13 +1,98 @@
|
|
| 1 |
---
|
| 2 |
-
title: ColiFormer
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
-
sdk:
|
| 7 |
-
sdk_version:
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
license: mit
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
---
|
| 12 |
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: ColiFormer - E. coli Codon Optimization
|
| 3 |
+
emoji: 🧬
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: green
|
| 6 |
+
sdk: streamlit
|
| 7 |
+
sdk_version: 1.28.1
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
license: mit
|
| 11 |
+
short_description: Advanced codon optimization for E. coli using fine-tuned transformers
|
| 12 |
+
tags:
|
| 13 |
+
- biology
|
| 14 |
+
- codon-optimization
|
| 15 |
+
- e-coli
|
| 16 |
+
- protein-synthesis
|
| 17 |
+
- bioinformatics
|
| 18 |
+
- synthetic-biology
|
| 19 |
+
- transformers
|
| 20 |
+
- streamlit
|
| 21 |
---
|
| 22 |
|
| 23 |
+
# 🧬 ColiFormer - E. coli Codon Optimization
|
| 24 |
+
|
| 25 |
+
**ColiFormer** is a specialized codon optimization tool fine-tuned specifically for *Escherichia coli* sequences, achieving **6.2% better CAI scores** compared to the base CodonTransformer model.
|
| 26 |
+
|
| 27 |
+
## 🚀 Features
|
| 28 |
+
|
| 29 |
+
- **🎯 E. coli Specialized**: Fine-tuned on 4,300 high-CAI E. coli sequences
|
| 30 |
+
- **📊 Advanced Metrics**: CAI, tAI, GC content, and codon frequency analysis
|
| 31 |
+
- **🤖 Auto-Loading**: Automatically downloads model and reference data from Hugging Face
|
| 32 |
+
- **⚡ Real-time**: Interactive sequence optimization with live metrics
|
| 33 |
+
- **🔬 Research-Grade**: Based on BigBird Transformer architecture
|
| 34 |
+
- **📈 Performance**: Significant improvement over base models for E. coli
|
| 35 |
+
|
| 36 |
+
## 📊 Model Performance
|
| 37 |
+
|
| 38 |
+
| Metric | Base Model | ColiFormer | Improvement |
|
| 39 |
+
|--------|------------|------------|-------------|
|
| 40 |
+
| CAI Score | 0.742 | 0.788 | **+6.2%** |
|
| 41 |
+
| tAI Score | 0.451 | 0.478 | **+6.0%** |
|
| 42 |
+
| GC Content | 52.1% | 51.8% | Optimized |
|
| 43 |
+
|
| 44 |
+
## 🔗 Related Resources
|
| 45 |
+
|
| 46 |
+
- **Model**: [saketh11/ColiFormer](https://huggingface.co/saketh11/ColiFormer)
|
| 47 |
+
- **Dataset**: [saketh11/ColiFormer-Data](https://huggingface.co/datasets/saketh11/ColiFormer-Data)
|
| 48 |
+
- **Base Model**: [adibvafa/CodonTransformer](https://huggingface.co/adibvafa/CodonTransformer)
|
| 49 |
+
- **Paper**: [CodonTransformer: The Global Translation of Genetic Code by Transformer](https://www.biorxiv.org/content/10.1101/2023.09.09.556981v1)
|
| 50 |
+
|
| 51 |
+
## 💡 How to Use
|
| 52 |
+
|
| 53 |
+
1. **Enter your protein sequence** in single-letter amino acid format
|
| 54 |
+
2. **Select optimization parameters** (temperature, max length, etc.)
|
| 55 |
+
3. **Click "Optimize Sequence"** to generate the optimized DNA sequence
|
| 56 |
+
4. **View comprehensive metrics** including CAI, tAI, GC content, and codon usage
|
| 57 |
+
5. **Download results** as FASTA or Excel files
|
| 58 |
+
|
| 59 |
+
## 🧪 Example
|
| 60 |
+
|
| 61 |
+
**Input Protein**: `MKRISTTITTTITITTGNGAG`
|
| 62 |
+
|
| 63 |
+
**Optimized DNA**: `ATGAAACGTATTAGT...` (optimized for E. coli expression)
|
| 64 |
+
|
| 65 |
+
**Metrics**:
|
| 66 |
+
- CAI: 0.85 (High)
|
| 67 |
+
- tAI: 0.52 (Good)
|
| 68 |
+
- GC Content: 51.2% (Optimal)
|
| 69 |
+
|
| 70 |
+
## 🔬 Technical Details
|
| 71 |
+
|
| 72 |
+
- **Architecture**: BigBird Transformer with 12 layers
|
| 73 |
+
- **Training**: Adaptive Learning Methods (ALM) enhanced
|
| 74 |
+
- **Context Length**: Up to 4096 tokens
|
| 75 |
+
- **Fine-tuning**: 4,300 high-CAI E. coli sequences
|
| 76 |
+
- **Reference Data**: 50,000+ E. coli gene sequences for metrics
|
| 77 |
+
|
| 78 |
+
## 📜 Citation
|
| 79 |
+
|
| 80 |
+
If you use ColiFormer in your research, please cite:
|
| 81 |
+
|
| 82 |
+
```bibtex
|
| 83 |
+
@article{codon_transformer_2023,
|
| 84 |
+
title={CodonTransformer: The Global Translation of Genetic Code by Transformer},
|
| 85 |
+
author={Adibvafa Fallahpour and Bartosz Grzybowski and Bogdan Gliwa and Bartosz Michalak},
|
| 86 |
+
journal={bioRxiv},
|
| 87 |
+
year={2023},
|
| 88 |
+
doi={10.1101/2023.09.09.556981}
|
| 89 |
+
}
|
| 90 |
+
```
|
| 91 |
+
|
| 92 |
+
## 📄 License
|
| 93 |
+
|
| 94 |
+
This project is licensed under the MIT License.
|
| 95 |
+
|
| 96 |
+
---
|
| 97 |
+
|
| 98 |
+
**Built with ❤️ for the synthetic biology community**
|
app.py
ADDED
|
@@ -0,0 +1,1472 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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torch
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import numpy as np
|
| 5 |
+
import plotly.graph_objects as go
|
| 6 |
+
import plotly.express as px
|
| 7 |
+
from transformers import AutoTokenizer, BigBirdForMaskedLM
|
| 8 |
+
from huggingface_hub import hf_hub_download
|
| 9 |
+
from datasets import load_dataset
|
| 10 |
+
import time
|
| 11 |
+
import threading
|
| 12 |
+
from typing import Dict, Optional, Tuple
|
| 13 |
+
import warnings
|
| 14 |
+
warnings.filterwarnings("ignore")
|
| 15 |
+
|
| 16 |
+
# Import CodonTransformer modules
|
| 17 |
+
import sys
|
| 18 |
+
import os
|
| 19 |
+
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
| 20 |
+
|
| 21 |
+
from CodonTransformer.CodonPrediction import (
|
| 22 |
+
predict_dna_sequence,
|
| 23 |
+
load_model
|
| 24 |
+
)
|
| 25 |
+
from CodonTransformer.CodonEvaluation import (
|
| 26 |
+
get_GC_content,
|
| 27 |
+
calculate_tAI,
|
| 28 |
+
get_ecoli_tai_weights,
|
| 29 |
+
scan_for_restriction_sites,
|
| 30 |
+
count_negative_cis_elements,
|
| 31 |
+
calculate_homopolymer_runs
|
| 32 |
+
)
|
| 33 |
+
from CAI import CAI, relative_adaptiveness
|
| 34 |
+
from CodonTransformer.CodonUtils import get_organism2id_dict
|
| 35 |
+
import json
|
| 36 |
+
|
| 37 |
+
# Try to import post-processing features
|
| 38 |
+
try:
|
| 39 |
+
from CodonTransformer.CodonPostProcessing import (
|
| 40 |
+
polish_sequence_with_dnachisel,
|
| 41 |
+
DNACHISEL_AVAILABLE
|
| 42 |
+
)
|
| 43 |
+
POST_PROCESSING_AVAILABLE = True
|
| 44 |
+
except ImportError:
|
| 45 |
+
POST_PROCESSING_AVAILABLE = False
|
| 46 |
+
DNACHISEL_AVAILABLE = False
|
| 47 |
+
|
| 48 |
+
# Page configuration
|
| 49 |
+
st.set_page_config(
|
| 50 |
+
page_title="CodonTransformer GUI",
|
| 51 |
+
page_icon="🧬",
|
| 52 |
+
layout="wide",
|
| 53 |
+
initial_sidebar_state="expanded"
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
# Initialize session state
|
| 57 |
+
if 'model' not in st.session_state:
|
| 58 |
+
st.session_state.model = None
|
| 59 |
+
if 'tokenizer' not in st.session_state:
|
| 60 |
+
st.session_state.tokenizer = None
|
| 61 |
+
if 'device' not in st.session_state:
|
| 62 |
+
st.session_state.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 63 |
+
if 'optimization_running' not in st.session_state:
|
| 64 |
+
st.session_state.optimization_running = False
|
| 65 |
+
if 'results' not in st.session_state:
|
| 66 |
+
st.session_state.results = None
|
| 67 |
+
if 'post_processed_results' not in st.session_state:
|
| 68 |
+
st.session_state.post_processed_results = None
|
| 69 |
+
if 'cai_weights' not in st.session_state:
|
| 70 |
+
st.session_state.cai_weights = None
|
| 71 |
+
if 'tai_weights' not in st.session_state:
|
| 72 |
+
st.session_state.tai_weights = None
|
| 73 |
+
|
| 74 |
+
def get_organism_tai_weights(organism: str) -> Dict[str, float]:
|
| 75 |
+
"""Get organism-specific tAI weights from pre-calculated data"""
|
| 76 |
+
try:
|
| 77 |
+
# Load organism-specific tAI weights
|
| 78 |
+
weights_file = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'organism_tai_weights.json')
|
| 79 |
+
with open(weights_file, 'r') as f:
|
| 80 |
+
all_weights = json.load(f)
|
| 81 |
+
|
| 82 |
+
if organism in all_weights:
|
| 83 |
+
return all_weights[organism]
|
| 84 |
+
else:
|
| 85 |
+
# Fallback to E. coli if organism not found
|
| 86 |
+
st.warning(f"tAI weights for {organism} not found, using E. coli weights")
|
| 87 |
+
return all_weights.get("Escherichia coli general", get_ecoli_tai_weights())
|
| 88 |
+
except Exception as e:
|
| 89 |
+
st.error(f"Error loading organism-specific tAI weights: {e}")
|
| 90 |
+
return get_ecoli_tai_weights()
|
| 91 |
+
|
| 92 |
+
def load_model_and_tokenizer():
|
| 93 |
+
"""Load the model and tokenizer with progress tracking"""
|
| 94 |
+
if st.session_state.model is None or st.session_state.tokenizer is None:
|
| 95 |
+
with st.spinner("Loading CodonTransformer model... This may take a few minutes."):
|
| 96 |
+
progress_bar = st.progress(0)
|
| 97 |
+
status_text = st.empty()
|
| 98 |
+
|
| 99 |
+
status_text.text("Loading tokenizer...")
|
| 100 |
+
progress_bar.progress(25)
|
| 101 |
+
st.session_state.tokenizer = AutoTokenizer.from_pretrained("adibvafa/CodonTransformer")
|
| 102 |
+
|
| 103 |
+
status_text.text("Loading fine-tuned model from Hugging Face...")
|
| 104 |
+
progress_bar.progress(50)
|
| 105 |
+
# Try to download and load fine-tuned model from Hugging Face
|
| 106 |
+
try:
|
| 107 |
+
# Download the checkpoint file from Hugging Face
|
| 108 |
+
from huggingface_hub import hf_hub_download
|
| 109 |
+
|
| 110 |
+
status_text.text("⬇️ Downloading model from saketh11/ColiFormer...")
|
| 111 |
+
model_path = hf_hub_download(
|
| 112 |
+
repo_id="saketh11/ColiFormer",
|
| 113 |
+
filename="balanced_alm_finetune.ckpt",
|
| 114 |
+
cache_dir="./hf_cache"
|
| 115 |
+
)
|
| 116 |
+
|
| 117 |
+
status_text.text("🔄 Loading downloaded model...")
|
| 118 |
+
st.session_state.model = load_model(
|
| 119 |
+
model_path=model_path,
|
| 120 |
+
device=st.session_state.device,
|
| 121 |
+
attention_type="original_full"
|
| 122 |
+
)
|
| 123 |
+
status_text.text("✅ Fine-tuned model loaded from Hugging Face (6.2% better CAI)")
|
| 124 |
+
st.session_state.model_type = "fine_tuned_hf"
|
| 125 |
+
except Exception as e:
|
| 126 |
+
status_text.text(f"⚠️ Failed to load from Hugging Face: {str(e)[:50]}...")
|
| 127 |
+
status_text.text("Loading base model as fallback...")
|
| 128 |
+
st.session_state.model = BigBirdForMaskedLM.from_pretrained("adibvafa/CodonTransformer")
|
| 129 |
+
st.session_state.model = st.session_state.model.to(st.session_state.device)
|
| 130 |
+
st.session_state.model_type = "base"
|
| 131 |
+
|
| 132 |
+
progress_bar.progress(100)
|
| 133 |
+
time.sleep(0.5)
|
| 134 |
+
|
| 135 |
+
status_text.empty()
|
| 136 |
+
progress_bar.empty()
|
| 137 |
+
|
| 138 |
+
@st.cache_data
|
| 139 |
+
def download_reference_data():
|
| 140 |
+
"""Download and cache reference data from Hugging Face"""
|
| 141 |
+
try:
|
| 142 |
+
# Download the processed genes file from Hugging Face
|
| 143 |
+
file_path = hf_hub_download(
|
| 144 |
+
repo_id="saketh11/ColiFormer-Data",
|
| 145 |
+
filename="ecoli_processed_genes.csv",
|
| 146 |
+
repo_type="dataset"
|
| 147 |
+
)
|
| 148 |
+
df = pd.read_csv(file_path)
|
| 149 |
+
return df['dna_sequence'].tolist()
|
| 150 |
+
except Exception as e:
|
| 151 |
+
st.warning(f"Could not download reference data from Hugging Face: {e}")
|
| 152 |
+
# Fallback to minimal sequences
|
| 153 |
+
return [
|
| 154 |
+
"ATGGCGAAAGCGCTGTATCGCGAAAGCGCTGTATCGCGAAAGCGCTGTATCGC",
|
| 155 |
+
"ATGAAATTTATTTATTATTATAAATTTATTTATTATTATAAATTTATTTAT",
|
| 156 |
+
"ATGGGTCGTCGTCGTCGTGGTCGTCGTCGTCGTGGTCGTCGTCGTCGTGGT"
|
| 157 |
+
]
|
| 158 |
+
|
| 159 |
+
@st.cache_data
|
| 160 |
+
def download_tai_weights():
|
| 161 |
+
"""Download and cache tAI weights from Hugging Face"""
|
| 162 |
+
try:
|
| 163 |
+
# Download the tAI weights file from Hugging Face
|
| 164 |
+
file_path = hf_hub_download(
|
| 165 |
+
repo_id="saketh11/ColiFormer-Data",
|
| 166 |
+
filename="organism_tai_weights.json",
|
| 167 |
+
repo_type="dataset"
|
| 168 |
+
)
|
| 169 |
+
with open(file_path, 'r') as f:
|
| 170 |
+
all_weights = json.load(f)
|
| 171 |
+
return all_weights.get("Escherichia coli general", get_ecoli_tai_weights())
|
| 172 |
+
except Exception as e:
|
| 173 |
+
st.warning(f"Could not download tAI weights from Hugging Face: {e}")
|
| 174 |
+
return get_ecoli_tai_weights()
|
| 175 |
+
|
| 176 |
+
def load_reference_data(organism: str = "Escherichia coli general"):
|
| 177 |
+
"""Load reference sequences and tAI weights for E. coli"""
|
| 178 |
+
if 'cai_weights' not in st.session_state or st.session_state['cai_weights'] is None:
|
| 179 |
+
try:
|
| 180 |
+
# Download reference sequences from Hugging Face
|
| 181 |
+
with st.spinner("📥 Downloading E. coli reference sequences from Hugging Face..."):
|
| 182 |
+
ref_sequences = download_reference_data()
|
| 183 |
+
st.session_state['cai_weights'] = relative_adaptiveness(sequences=ref_sequences)
|
| 184 |
+
if len(ref_sequences) > 100: # If we got the full dataset
|
| 185 |
+
st.success(f"✅ Downloaded {len(ref_sequences):,} E. coli reference sequences for CAI calculation")
|
| 186 |
+
else:
|
| 187 |
+
st.info(f"⚠️ Using {len(ref_sequences)} minimal reference sequences (full dataset unavailable)")
|
| 188 |
+
except Exception as e:
|
| 189 |
+
st.error(f"Error loading E. coli reference data: {e}")
|
| 190 |
+
st.session_state['cai_weights'] = {}
|
| 191 |
+
# tAI weights (E. coli only)
|
| 192 |
+
if 'tai_weights' not in st.session_state or st.session_state['tai_weights'] is None:
|
| 193 |
+
try:
|
| 194 |
+
with st.spinner("📥 Downloading E. coli tAI weights from Hugging Face..."):
|
| 195 |
+
st.session_state['tai_weights'] = download_tai_weights()
|
| 196 |
+
st.success("✅ Downloaded E. coli tAI weights")
|
| 197 |
+
except Exception as e:
|
| 198 |
+
st.error(f"Error loading E. coli tAI weights: {e}")
|
| 199 |
+
st.session_state['tai_weights'] = {}
|
| 200 |
+
|
| 201 |
+
def validate_sequence(sequence: str) -> Tuple[bool, str, str, str]:
|
| 202 |
+
"""Validate sequence and return status, message, sequence type, and possibly fixed sequence"""
|
| 203 |
+
if not sequence:
|
| 204 |
+
return False, "Sequence cannot be empty", "unknown", sequence
|
| 205 |
+
|
| 206 |
+
# Remove whitespace and convert to uppercase
|
| 207 |
+
sequence = sequence.strip().upper()
|
| 208 |
+
|
| 209 |
+
# Check if it's a DNA sequence
|
| 210 |
+
dna_chars = set("ATGC")
|
| 211 |
+
protein_chars = set("ACDEFGHIKLMNPQRSTVWY*_")
|
| 212 |
+
|
| 213 |
+
sequence_chars = set(sequence)
|
| 214 |
+
|
| 215 |
+
# If all characters are DNA nucleotides, treat as DNA
|
| 216 |
+
if sequence_chars.issubset(dna_chars):
|
| 217 |
+
if len(sequence) < 3:
|
| 218 |
+
return False, "DNA sequence must be at least 3 nucleotides long", "dna", sequence
|
| 219 |
+
|
| 220 |
+
# Auto-fix DNA sequences not divisible by 3
|
| 221 |
+
if len(sequence) % 3 != 0:
|
| 222 |
+
remainder = len(sequence) % 3
|
| 223 |
+
fixed_sequence = sequence[:-remainder]
|
| 224 |
+
message = f"Valid DNA sequence (auto-fixed: removed {remainder} nucleotides from end to make divisible by 3)"
|
| 225 |
+
else:
|
| 226 |
+
fixed_sequence = sequence
|
| 227 |
+
message = "Valid DNA sequence"
|
| 228 |
+
|
| 229 |
+
return True, message, "dna", fixed_sequence
|
| 230 |
+
|
| 231 |
+
# If contains protein-specific amino acids, treat as protein
|
| 232 |
+
elif sequence_chars.issubset(protein_chars):
|
| 233 |
+
if len(sequence) < 3:
|
| 234 |
+
return False, "Protein sequence must be at least 3 amino acids long", "protein", sequence
|
| 235 |
+
return True, "Valid protein sequence", "protein", sequence
|
| 236 |
+
|
| 237 |
+
# Invalid characters
|
| 238 |
+
else:
|
| 239 |
+
invalid_chars = sequence_chars - (dna_chars | protein_chars)
|
| 240 |
+
return False, f"Invalid characters found: {', '.join(invalid_chars)}", "unknown", sequence
|
| 241 |
+
|
| 242 |
+
def calculate_input_metrics(sequence: str, organism: str, sequence_type: str) -> Dict:
|
| 243 |
+
"""Calculate metrics for the input sequence using E. coli reference only"""
|
| 244 |
+
# Load reference data (E. coli only)
|
| 245 |
+
load_reference_data()
|
| 246 |
+
if sequence_type == "dna":
|
| 247 |
+
dna_sequence = sequence.upper()
|
| 248 |
+
metrics = {
|
| 249 |
+
'length': len(dna_sequence) // 3,
|
| 250 |
+
'gc_content': get_GC_content(dna_sequence),
|
| 251 |
+
'baseline_dna': dna_sequence,
|
| 252 |
+
'sequence_type': 'dna'
|
| 253 |
+
}
|
| 254 |
+
try:
|
| 255 |
+
if 'cai_weights' in st.session_state and st.session_state['cai_weights']:
|
| 256 |
+
metrics['cai'] = CAI(dna_sequence, weights=st.session_state['cai_weights'])
|
| 257 |
+
else:
|
| 258 |
+
metrics['cai'] = None
|
| 259 |
+
except:
|
| 260 |
+
metrics['cai'] = None
|
| 261 |
+
try:
|
| 262 |
+
if 'tai_weights' in st.session_state and st.session_state['tai_weights']:
|
| 263 |
+
metrics['tai'] = calculate_tAI(dna_sequence, st.session_state['tai_weights'])
|
| 264 |
+
else:
|
| 265 |
+
metrics['tai'] = None
|
| 266 |
+
except:
|
| 267 |
+
metrics['tai'] = None
|
| 268 |
+
else:
|
| 269 |
+
most_frequent_codons = {
|
| 270 |
+
'A': 'GCG', 'C': 'TGC', 'D': 'GAT', 'E': 'GAA', 'F': 'TTT',
|
| 271 |
+
'G': 'GGC', 'H': 'CAT', 'I': 'ATT', 'K': 'AAA', 'L': 'CTG',
|
| 272 |
+
'M': 'ATG', 'N': 'AAC', 'P': 'CCG', 'Q': 'CAG', 'R': 'CGC',
|
| 273 |
+
'S': 'TCG', 'T': 'ACG', 'V': 'GTG', 'W': 'TGG', 'Y': 'TAT',
|
| 274 |
+
'*': 'TAA', '_': 'TAA'
|
| 275 |
+
}
|
| 276 |
+
baseline_dna = ''.join([most_frequent_codons.get(aa, 'NNN') for aa in sequence])
|
| 277 |
+
metrics = {
|
| 278 |
+
'length': len(sequence),
|
| 279 |
+
'gc_content': get_GC_content(baseline_dna),
|
| 280 |
+
'baseline_dna': baseline_dna,
|
| 281 |
+
'sequence_type': 'protein'
|
| 282 |
+
}
|
| 283 |
+
try:
|
| 284 |
+
if 'cai_weights' in st.session_state and st.session_state['cai_weights']:
|
| 285 |
+
metrics['cai'] = CAI(baseline_dna, weights=st.session_state['cai_weights'])
|
| 286 |
+
else:
|
| 287 |
+
metrics['cai'] = None
|
| 288 |
+
except:
|
| 289 |
+
metrics['cai'] = None
|
| 290 |
+
try:
|
| 291 |
+
if 'tai_weights' in st.session_state and st.session_state['tai_weights']:
|
| 292 |
+
metrics['tai'] = calculate_tAI(baseline_dna, st.session_state['tai_weights'])
|
| 293 |
+
else:
|
| 294 |
+
metrics['tai'] = None
|
| 295 |
+
except:
|
| 296 |
+
metrics['tai'] = None
|
| 297 |
+
try:
|
| 298 |
+
analysis_dna = metrics['baseline_dna']
|
| 299 |
+
metrics['restriction_sites'] = len(scan_for_restriction_sites(analysis_dna))
|
| 300 |
+
metrics['negative_cis_elements'] = count_negative_cis_elements(analysis_dna)
|
| 301 |
+
metrics['homopolymer_runs'] = calculate_homopolymer_runs(analysis_dna)
|
| 302 |
+
except:
|
| 303 |
+
metrics['restriction_sites'] = 0
|
| 304 |
+
metrics['negative_cis_elements'] = 0
|
| 305 |
+
metrics['homopolymer_runs'] = 0
|
| 306 |
+
return metrics
|
| 307 |
+
|
| 308 |
+
def translate_dna_to_protein(dna_sequence: str) -> str:
|
| 309 |
+
"""Translate DNA sequence to protein sequence"""
|
| 310 |
+
codon_table = {
|
| 311 |
+
'TTT': 'F', 'TTC': 'F', 'TTA': 'L', 'TTG': 'L',
|
| 312 |
+
'TCT': 'S', 'TCC': 'S', 'TCA': 'S', 'TCG': 'S',
|
| 313 |
+
'TAT': 'Y', 'TAC': 'Y', 'TAA': '*', 'TAG': '*',
|
| 314 |
+
'TGT': 'C', 'TGC': 'C', 'TGA': '*', 'TGG': 'W',
|
| 315 |
+
'CTT': 'L', 'CTC': 'L', 'CTA': 'L', 'CTG': 'L',
|
| 316 |
+
'CCT': 'P', 'CCC': 'P', 'CCA': 'P', 'CCG': 'P',
|
| 317 |
+
'CAT': 'H', 'CAC': 'H', 'CAA': 'Q', 'CAG': 'Q',
|
| 318 |
+
'CGT': 'R', 'CGC': 'R', 'CGA': 'R', 'CGG': 'R',
|
| 319 |
+
'ATT': 'I', 'ATC': 'I', 'ATA': 'I', 'ATG': 'M',
|
| 320 |
+
'ACT': 'T', 'ACC': 'T', 'ACA': 'T', 'ACG': 'T',
|
| 321 |
+
'AAT': 'N', 'AAC': 'N', 'AAA': 'K', 'AAG': 'K',
|
| 322 |
+
'AGT': 'S', 'AGC': 'S', 'AGA': 'R', 'AGG': 'R',
|
| 323 |
+
'GTT': 'V', 'GTC': 'V', 'GTA': 'V', 'GTG': 'V',
|
| 324 |
+
'GCT': 'A', 'GCC': 'A', 'GCA': 'A', 'GCG': 'A',
|
| 325 |
+
'GAT': 'D', 'GAC': 'D', 'GAA': 'E', 'GAG': 'E',
|
| 326 |
+
'GGT': 'G', 'GGC': 'G', 'GGA': 'G', 'GGG': 'G'
|
| 327 |
+
}
|
| 328 |
+
|
| 329 |
+
protein = ""
|
| 330 |
+
for i in range(0, len(dna_sequence), 3):
|
| 331 |
+
codon = dna_sequence[i:i+3].upper()
|
| 332 |
+
if len(codon) == 3:
|
| 333 |
+
aa = codon_table.get(codon, 'X')
|
| 334 |
+
if aa == '*': # Stop codon
|
| 335 |
+
break
|
| 336 |
+
protein += aa
|
| 337 |
+
|
| 338 |
+
return protein
|
| 339 |
+
|
| 340 |
+
def create_gc_content_plot(sequence: str, window_size: int = 50) -> go.Figure:
|
| 341 |
+
"""Create a sliding window GC content plot"""
|
| 342 |
+
if len(sequence) < window_size:
|
| 343 |
+
window_size = len(sequence) // 3
|
| 344 |
+
|
| 345 |
+
positions = []
|
| 346 |
+
gc_values = []
|
| 347 |
+
|
| 348 |
+
for i in range(0, len(sequence) - window_size + 1, 3): # Step by codons
|
| 349 |
+
window = sequence[i:i + window_size]
|
| 350 |
+
gc_content = get_GC_content(window)
|
| 351 |
+
positions.append(i // 3) # Position in codons
|
| 352 |
+
gc_values.append(gc_content)
|
| 353 |
+
|
| 354 |
+
fig = go.Figure()
|
| 355 |
+
fig.add_trace(go.Scatter(
|
| 356 |
+
x=positions,
|
| 357 |
+
y=gc_values,
|
| 358 |
+
mode='lines',
|
| 359 |
+
name='GC Content',
|
| 360 |
+
line=dict(color='blue', width=2)
|
| 361 |
+
))
|
| 362 |
+
|
| 363 |
+
# Add target range
|
| 364 |
+
fig.add_hline(y=45, line_dash="dash", line_color="red",
|
| 365 |
+
annotation_text="Min Target (45%)")
|
| 366 |
+
fig.add_hline(y=55, line_dash="dash", line_color="red",
|
| 367 |
+
annotation_text="Max Target (55%)")
|
| 368 |
+
|
| 369 |
+
fig.update_layout(
|
| 370 |
+
title=f'GC Content (sliding window: {window_size} bp)',
|
| 371 |
+
xaxis_title='Position (codons)',
|
| 372 |
+
yaxis_title='GC Content (%)',
|
| 373 |
+
height=300
|
| 374 |
+
)
|
| 375 |
+
|
| 376 |
+
return fig
|
| 377 |
+
|
| 378 |
+
def create_gc_comparison_chart(before_metrics: Dict, after_metrics: Dict) -> go.Figure:
|
| 379 |
+
"""Create a comparison chart for GC Content"""
|
| 380 |
+
fig = go.Figure()
|
| 381 |
+
fig.add_trace(go.Bar(
|
| 382 |
+
name='Before Optimization',
|
| 383 |
+
x=['GC Content (%)'],
|
| 384 |
+
y=[before_metrics.get('gc_content', 0)],
|
| 385 |
+
marker_color='lightblue',
|
| 386 |
+
text=[f"{before_metrics.get('gc_content', 0):.1f}%"],
|
| 387 |
+
textposition='auto'
|
| 388 |
+
))
|
| 389 |
+
fig.add_trace(go.Bar(
|
| 390 |
+
name='After Optimization',
|
| 391 |
+
x=['GC Content (%)'],
|
| 392 |
+
y=[after_metrics.get('gc_content', 0)],
|
| 393 |
+
marker_color='darkblue',
|
| 394 |
+
text=[f"{after_metrics.get('gc_content', 0):.1f}%"],
|
| 395 |
+
textposition='auto'
|
| 396 |
+
))
|
| 397 |
+
fig.update_layout(
|
| 398 |
+
title='GC Content Comparison: Before vs After',
|
| 399 |
+
xaxis_title='Metric',
|
| 400 |
+
yaxis_title='Value (%)',
|
| 401 |
+
barmode='group',
|
| 402 |
+
height=300
|
| 403 |
+
)
|
| 404 |
+
return fig
|
| 405 |
+
|
| 406 |
+
def create_expression_comparison_chart(before_metrics: Dict, after_metrics: Dict) -> go.Figure:
|
| 407 |
+
"""Create a comparison chart for expression metrics (CAI, tAI)"""
|
| 408 |
+
metrics_names = ['CAI', 'tAI']
|
| 409 |
+
before_values = [
|
| 410 |
+
before_metrics.get('cai', 0) if before_metrics.get('cai') else 0,
|
| 411 |
+
before_metrics.get('tai', 0) if before_metrics.get('tai') else 0
|
| 412 |
+
]
|
| 413 |
+
after_values = [
|
| 414 |
+
after_metrics.get('cai', 0) if after_metrics.get('cai') else 0,
|
| 415 |
+
after_metrics.get('tai', 0) if after_metrics.get('tai') else 0
|
| 416 |
+
]
|
| 417 |
+
|
| 418 |
+
fig = go.Figure()
|
| 419 |
+
fig.add_trace(go.Bar(
|
| 420 |
+
name='Before Optimization',
|
| 421 |
+
x=metrics_names,
|
| 422 |
+
y=before_values,
|
| 423 |
+
marker_color='lightblue',
|
| 424 |
+
text=[f"{v:.3f}" for v in before_values],
|
| 425 |
+
textposition='auto'
|
| 426 |
+
))
|
| 427 |
+
fig.add_trace(go.Bar(
|
| 428 |
+
name='After Optimization',
|
| 429 |
+
x=metrics_names,
|
| 430 |
+
y=after_values,
|
| 431 |
+
marker_color='darkblue',
|
| 432 |
+
text=[f"{v:.3f}" for v in after_values],
|
| 433 |
+
textposition='auto'
|
| 434 |
+
))
|
| 435 |
+
fig.update_layout(
|
| 436 |
+
title='Expression Metrics Comparison: Before vs After',
|
| 437 |
+
xaxis_title='Metric',
|
| 438 |
+
yaxis_title='Value',
|
| 439 |
+
barmode='group',
|
| 440 |
+
height=300
|
| 441 |
+
)
|
| 442 |
+
return fig
|
| 443 |
+
|
| 444 |
+
def smart_codon_replacement(dna_sequence: str, target_gc_min: float = 0.45, target_gc_max: float = 0.55, max_iterations: int = 100) -> str:
|
| 445 |
+
"""Smart codon replacement to optimize GC content while maximizing CAI"""
|
| 446 |
+
|
| 447 |
+
# Codon alternatives with their GC content
|
| 448 |
+
codon_alternatives = {
|
| 449 |
+
# Serine: high GC options
|
| 450 |
+
'TCT': ['TCG', 'TCC', 'TCA', 'AGT', 'AGC'], # 33% -> 67%, 67%, 33%, 33%, 67%
|
| 451 |
+
'TCA': ['TCG', 'TCC', 'TCT', 'AGT', 'AGC'],
|
| 452 |
+
'AGT': ['TCG', 'TCC', 'TCT', 'TCA', 'AGC'],
|
| 453 |
+
|
| 454 |
+
# Leucine: various GC options
|
| 455 |
+
'TTA': ['TTG', 'CTT', 'CTC', 'CTA', 'CTG'], # 0% -> 33%, 33%, 67%, 33%, 67%
|
| 456 |
+
'TTG': ['TTA', 'CTT', 'CTC', 'CTA', 'CTG'],
|
| 457 |
+
'CTT': ['CTG', 'CTC', 'TTA', 'TTG', 'CTA'],
|
| 458 |
+
'CTA': ['CTG', 'CTC', 'CTT', 'TTA', 'TTG'],
|
| 459 |
+
|
| 460 |
+
# Arginine: various GC options
|
| 461 |
+
'AGA': ['CGT', 'CGC', 'CGA', 'CGG', 'AGG'], # 33% -> 67%, 100%, 67%, 100%, 67%
|
| 462 |
+
'AGG': ['CGT', 'CGC', 'CGA', 'CGG', 'AGA'],
|
| 463 |
+
'CGT': ['CGC', 'CGG', 'CGA', 'AGA', 'AGG'],
|
| 464 |
+
'CGA': ['CGC', 'CGG', 'CGT', 'AGA', 'AGG'],
|
| 465 |
+
|
| 466 |
+
# Proline
|
| 467 |
+
'CCT': ['CCG', 'CCC', 'CCA'], # 67% -> 100%, 100%, 67%
|
| 468 |
+
'CCA': ['CCG', 'CCC', 'CCT'],
|
| 469 |
+
|
| 470 |
+
# Threonine
|
| 471 |
+
'ACT': ['ACG', 'ACC', 'ACA'], # 33% -> 67%, 67%, 33%
|
| 472 |
+
'ACA': ['ACG', 'ACC', 'ACT'],
|
| 473 |
+
|
| 474 |
+
# Alanine
|
| 475 |
+
'GCT': ['GCG', 'GCC', 'GCA'], # 67% -> 100%, 100%, 67%
|
| 476 |
+
'GCA': ['GCG', 'GCC', 'GCT'],
|
| 477 |
+
|
| 478 |
+
# Glycine
|
| 479 |
+
'GGT': ['GGG', 'GGC', 'GGA'], # 67% -> 100%, 100%, 67%
|
| 480 |
+
'GGA': ['GGG', 'GGC', 'GGT'],
|
| 481 |
+
|
| 482 |
+
# Valine
|
| 483 |
+
'GTT': ['GTG', 'GTC', 'GTA'], # 67% -> 100%, 100%, 67%
|
| 484 |
+
'GTA': ['GTG', 'GTC', 'GTT'],
|
| 485 |
+
}
|
| 486 |
+
|
| 487 |
+
def get_codon_gc(codon):
|
| 488 |
+
return (codon.count('G') + codon.count('C')) / 3.0
|
| 489 |
+
|
| 490 |
+
current_sequence = dna_sequence.upper()
|
| 491 |
+
current_gc = get_GC_content(current_sequence)
|
| 492 |
+
|
| 493 |
+
if target_gc_min <= current_gc <= target_gc_max:
|
| 494 |
+
return current_sequence
|
| 495 |
+
|
| 496 |
+
codons = [current_sequence[i:i+3] for i in range(0, len(current_sequence), 3)]
|
| 497 |
+
|
| 498 |
+
for iteration in range(max_iterations):
|
| 499 |
+
current_gc = get_GC_content(''.join(codons))
|
| 500 |
+
|
| 501 |
+
if target_gc_min <= current_gc <= target_gc_max:
|
| 502 |
+
break
|
| 503 |
+
|
| 504 |
+
# Find best codon to replace
|
| 505 |
+
best_improvement = 0
|
| 506 |
+
best_pos = -1
|
| 507 |
+
best_replacement = None
|
| 508 |
+
|
| 509 |
+
for pos, codon in enumerate(codons):
|
| 510 |
+
if codon in codon_alternatives:
|
| 511 |
+
for alt_codon in codon_alternatives[codon]:
|
| 512 |
+
# Calculate GC change
|
| 513 |
+
old_gc_contrib = get_codon_gc(codon)
|
| 514 |
+
new_gc_contrib = get_codon_gc(alt_codon)
|
| 515 |
+
gc_change = new_gc_contrib - old_gc_contrib
|
| 516 |
+
|
| 517 |
+
# Check if this change moves us toward target
|
| 518 |
+
if current_gc < target_gc_min and gc_change > best_improvement:
|
| 519 |
+
best_improvement = gc_change
|
| 520 |
+
best_pos = pos
|
| 521 |
+
best_replacement = alt_codon
|
| 522 |
+
elif current_gc > target_gc_max and gc_change < best_improvement:
|
| 523 |
+
best_improvement = abs(gc_change)
|
| 524 |
+
best_pos = pos
|
| 525 |
+
best_replacement = alt_codon
|
| 526 |
+
|
| 527 |
+
if best_pos >= 0:
|
| 528 |
+
if isinstance(best_replacement, str):
|
| 529 |
+
codons[best_pos] = best_replacement
|
| 530 |
+
else:
|
| 531 |
+
break # No more improvements possible
|
| 532 |
+
|
| 533 |
+
return ''.join(codons)
|
| 534 |
+
|
| 535 |
+
def run_optimization(protein: str, organism: str, use_post_processing: bool = False):
|
| 536 |
+
"""Run the optimization using the exact method from run_full_comparison.py with auto GC correction"""
|
| 537 |
+
st.session_state.optimization_running = True
|
| 538 |
+
st.session_state.post_processed_results = None
|
| 539 |
+
|
| 540 |
+
try:
|
| 541 |
+
# Use the exact same method that achieved best results in evaluation
|
| 542 |
+
result = predict_dna_sequence(
|
| 543 |
+
protein=protein,
|
| 544 |
+
organism=organism,
|
| 545 |
+
device=st.session_state.device,
|
| 546 |
+
model=st.session_state.model,
|
| 547 |
+
deterministic=True,
|
| 548 |
+
match_protein=True,
|
| 549 |
+
)
|
| 550 |
+
|
| 551 |
+
# Check GC content and auto-correct if out of optimal range
|
| 552 |
+
_res = result[0] if isinstance(result, list) else result
|
| 553 |
+
initial_gc = get_GC_content(_res.predicted_dna)
|
| 554 |
+
|
| 555 |
+
if initial_gc < 45.0 or initial_gc > 55.0:
|
| 556 |
+
# Auto-correct GC content silently
|
| 557 |
+
optimized_dna = smart_codon_replacement(_res.predicted_dna, 0.45, 0.55)
|
| 558 |
+
smart_gc = get_GC_content(optimized_dna)
|
| 559 |
+
|
| 560 |
+
if 45.0 <= smart_gc <= 55.0:
|
| 561 |
+
from CodonTransformer.CodonUtils import DNASequencePrediction
|
| 562 |
+
result = DNASequencePrediction(
|
| 563 |
+
organism=_res.organism,
|
| 564 |
+
protein=_res.protein,
|
| 565 |
+
processed_input=_res.processed_input,
|
| 566 |
+
predicted_dna=optimized_dna
|
| 567 |
+
)
|
| 568 |
+
else:
|
| 569 |
+
# Fall back to constrained beam search silently
|
| 570 |
+
try:
|
| 571 |
+
result = predict_dna_sequence(
|
| 572 |
+
protein=protein,
|
| 573 |
+
organism=organism,
|
| 574 |
+
device=st.session_state.device,
|
| 575 |
+
model=st.session_state.model,
|
| 576 |
+
deterministic=True,
|
| 577 |
+
match_protein=True,
|
| 578 |
+
use_constrained_search=True,
|
| 579 |
+
gc_bounds=(0.45, 0.55),
|
| 580 |
+
beam_size=20
|
| 581 |
+
)
|
| 582 |
+
_res2 = result[0] if isinstance(result, list) else result
|
| 583 |
+
final_gc = get_GC_content(_res2.predicted_dna)
|
| 584 |
+
except Exception as e:
|
| 585 |
+
# If constrained search fails, use smart replacement result anyway
|
| 586 |
+
from CodonTransformer.CodonUtils import DNASequencePrediction
|
| 587 |
+
result = DNASequencePrediction(
|
| 588 |
+
organism=_res.organism,
|
| 589 |
+
protein=_res.protein,
|
| 590 |
+
processed_input=_res.processed_input,
|
| 591 |
+
predicted_dna=optimized_dna
|
| 592 |
+
)
|
| 593 |
+
|
| 594 |
+
st.session_state.results = result
|
| 595 |
+
|
| 596 |
+
# Post-processing if enabled
|
| 597 |
+
if use_post_processing and POST_PROCESSING_AVAILABLE and result:
|
| 598 |
+
try:
|
| 599 |
+
_res = result[0] if isinstance(result, list) else result
|
| 600 |
+
polished_sequence = polish_sequence_with_dnachisel(
|
| 601 |
+
dna_sequence=_res.predicted_dna,
|
| 602 |
+
protein_sequence=protein,
|
| 603 |
+
gc_bounds=(45.0, 55.0),
|
| 604 |
+
cai_species=organism.lower().replace(' ', '_'),
|
| 605 |
+
avoid_homopolymers_length=6
|
| 606 |
+
)
|
| 607 |
+
|
| 608 |
+
# Create enhanced result object
|
| 609 |
+
from CodonTransformer.CodonUtils import DNASequencePrediction
|
| 610 |
+
st.session_state.post_processed_results = DNASequencePrediction(
|
| 611 |
+
organism=result.organism,
|
| 612 |
+
protein=result.protein,
|
| 613 |
+
processed_input=result.processed_input,
|
| 614 |
+
predicted_dna=polished_sequence
|
| 615 |
+
)
|
| 616 |
+
except Exception as e:
|
| 617 |
+
st.session_state.post_processed_results = f"Post-processing error: {str(e)}"
|
| 618 |
+
|
| 619 |
+
except Exception as e:
|
| 620 |
+
st.session_state.results = f"Error: {str(e)}"
|
| 621 |
+
|
| 622 |
+
finally:
|
| 623 |
+
st.session_state.optimization_running = False
|
| 624 |
+
|
| 625 |
+
def main():
|
| 626 |
+
st.title("🧬 ColiFormer")
|
| 627 |
+
st.markdown("**State-of-the-art E. coli codon optimization for publication-quality research**")
|
| 628 |
+
|
| 629 |
+
# Remove the performance highlights expander (details/summary block)
|
| 630 |
+
# (No expander here anymore)
|
| 631 |
+
|
| 632 |
+
# Load model
|
| 633 |
+
load_model_and_tokenizer()
|
| 634 |
+
|
| 635 |
+
# Create the main tabbed interface
|
| 636 |
+
tab1, tab2, tab3, tab4 = st.tabs(["🧬 Single Optimize", "📁 Batch Process", "📊 Comparative Analysis", "⚙️ Advanced Settings"])
|
| 637 |
+
|
| 638 |
+
with tab1:
|
| 639 |
+
single_sequence_optimization()
|
| 640 |
+
|
| 641 |
+
with tab2:
|
| 642 |
+
batch_processing_interface()
|
| 643 |
+
|
| 644 |
+
with tab3:
|
| 645 |
+
comparative_analysis_interface()
|
| 646 |
+
|
| 647 |
+
with tab4:
|
| 648 |
+
advanced_settings_interface()
|
| 649 |
+
|
| 650 |
+
def single_sequence_optimization():
|
| 651 |
+
"""Single sequence optimization interface - enhanced from original functionality"""
|
| 652 |
+
# Sidebar configuration
|
| 653 |
+
st.sidebar.header("🔧 Configuration")
|
| 654 |
+
organism_options = [
|
| 655 |
+
"Escherichia coli general",
|
| 656 |
+
"Saccharomyces cerevisiae",
|
| 657 |
+
"Homo sapiens",
|
| 658 |
+
"Bacillus subtilis",
|
| 659 |
+
"Pichia pastoris"
|
| 660 |
+
]
|
| 661 |
+
organism = st.sidebar.selectbox("Select Target Organism", organism_options)
|
| 662 |
+
load_reference_data(organism)
|
| 663 |
+
with st.sidebar.expander("🔧 Advanced Optimization Settings"):
|
| 664 |
+
st.markdown("**Model Parameters**")
|
| 665 |
+
use_deterministic = st.checkbox("Deterministic Mode", value=True, help="Use deterministic decoding for reproducible results")
|
| 666 |
+
match_protein = st.checkbox("Match Protein Validation", value=True, help="Ensure DNA translates back to exact protein")
|
| 667 |
+
st.markdown("**GC Content Control**")
|
| 668 |
+
gc_target_min = st.slider("GC Target Min (%)", 30, 70, 45, help="Minimum GC content target")
|
| 669 |
+
gc_target_max = st.slider("GC Target Max (%)", 30, 70, 55, help="Maximum GC content target")
|
| 670 |
+
st.markdown("**Quality Constraints**")
|
| 671 |
+
avoid_restriction_sites = st.multiselect(
|
| 672 |
+
"Avoid Restriction Sites",
|
| 673 |
+
["EcoRI", "BamHI", "HindIII", "XhoI", "NotI"],
|
| 674 |
+
default=["EcoRI", "BamHI"]
|
| 675 |
+
)
|
| 676 |
+
st.sidebar.subheader("🔬 Post-Processing")
|
| 677 |
+
use_post_processing = st.sidebar.checkbox(
|
| 678 |
+
"Enable DNAChisel Post-Processing",
|
| 679 |
+
value=False,
|
| 680 |
+
disabled=not POST_PROCESSING_AVAILABLE,
|
| 681 |
+
help="Polish sequences to remove restriction sites, homopolymers, and synthesis issues"
|
| 682 |
+
)
|
| 683 |
+
if not POST_PROCESSING_AVAILABLE:
|
| 684 |
+
st.sidebar.warning("⚠️ DNAChisel not available. Install with: pip install dnachisel")
|
| 685 |
+
|
| 686 |
+
# Dataset Information
|
| 687 |
+
st.sidebar.markdown("---")
|
| 688 |
+
st.sidebar.markdown("### 📊 Dataset Information")
|
| 689 |
+
st.sidebar.markdown("""
|
| 690 |
+
- **Dataset**: [ColiFormer-Data](https://huggingface.co/datasets/saketh11/ColiFormer-Data)
|
| 691 |
+
- **Training**: 4,300 high-CAI E. coli sequences
|
| 692 |
+
- **Reference**: 50,000+ E. coli gene sequences
|
| 693 |
+
- **Auto-download**: CAI weights & tAI coefficients
|
| 694 |
+
""")
|
| 695 |
+
|
| 696 |
+
# Model Information
|
| 697 |
+
st.sidebar.markdown("### 🤖 Model Information")
|
| 698 |
+
st.sidebar.markdown("""
|
| 699 |
+
- **Model**: [ColiFormer](https://huggingface.co/saketh11/ColiFormer)
|
| 700 |
+
- **Improvement**: +6.2% CAI vs base model
|
| 701 |
+
- **Architecture**: BigBird Transformer + ALM
|
| 702 |
+
- **Auto-download**: From Hugging Face Hub
|
| 703 |
+
""")
|
| 704 |
+
col1, col2 = st.columns([1, 1])
|
| 705 |
+
with col1:
|
| 706 |
+
st.header("🧬 Input Sequence")
|
| 707 |
+
sequence_input = st.text_area(
|
| 708 |
+
"Enter Protein or DNA Sequence",
|
| 709 |
+
height=150,
|
| 710 |
+
placeholder="Enter protein sequence (MKWVT...) or DNA sequence (ATGGCG...)\n\nExample protein: MKWVTFISLLFLFSSAYSRGVFRRDAHKSEVAHRFKDLGEENFKALVLIAFAQYLQQCPFEDHVKLVNEVTEFAKTCVADESAENCDKSLHTLFGDKLCTVATLRETYGEMADCCAKQEPERNECFLQHKDDNPNLPRLVRPEVDVMCTAFHDNEETFLKKYLYEIARRHPYFYAPELLFFAKRYKAAFTECCQAADKAACLLPKLDELRDEGKASSAKQRLKCASLQKFGERAFKAWAVARLSQRFPKAEFAEVSKLVTDLTKVHTECCHGDLLECADDRADLAKYICENQDSISSKLKECCEKPLLEKSHCIAEVENDEMPADLPSLAADFVESKDVCKNYAEAKDVFLGMFLYEYARRHPDYSVVLLLRLAKTYETTLEKCCAAADPHECYAKVFDEFKPLVEEPQNLIKQNCELFEQLGEYKFQNALLVRYTKKVPQVSTPTLVEVSRNLGKVGSKCCKHPEAKRMPCAEDYLSVVLNQLCVLHEKTPVSDRVTKCCTE"
|
| 711 |
+
)
|
| 712 |
+
analyze_btn = st.button("Analyze Sequence", type="primary")
|
| 713 |
+
if sequence_input and analyze_btn:
|
| 714 |
+
is_valid, message, sequence_type, fixed_sequence = validate_sequence(sequence_input)
|
| 715 |
+
if is_valid:
|
| 716 |
+
st.success(f"✅ {message}")
|
| 717 |
+
# Store in session state for use by Optimize Sequence
|
| 718 |
+
st.session_state.sequence_clean = fixed_sequence
|
| 719 |
+
st.session_state.sequence_type = sequence_type
|
| 720 |
+
st.session_state.input_metrics = calculate_input_metrics(fixed_sequence, organism, sequence_type)
|
| 721 |
+
st.session_state.organism = organism
|
| 722 |
+
else:
|
| 723 |
+
st.error(f"❌ {message}")
|
| 724 |
+
if "Invalid characters" in message:
|
| 725 |
+
st.info("💡 **Suggestion:** Remove spaces, numbers, and special characters. Use only standard amino acid letters (A-Z) for proteins or nucleotides (ATGC) for DNA.")
|
| 726 |
+
elif "too long" in message:
|
| 727 |
+
st.info("💡 **Suggestion:** Consider breaking long sequences into smaller segments for optimization.")
|
| 728 |
+
elif "too short" in message:
|
| 729 |
+
st.info("💡 **Suggestion:** Minimum length is 3 characters. Ensure your sequence is complete.")
|
| 730 |
+
# Clear session state if invalid
|
| 731 |
+
st.session_state.sequence_clean = None
|
| 732 |
+
st.session_state.sequence_type = None
|
| 733 |
+
st.session_state.input_metrics = None
|
| 734 |
+
st.session_state.organism = None
|
| 735 |
+
elif not sequence_input:
|
| 736 |
+
st.session_state.sequence_clean = None
|
| 737 |
+
st.session_state.sequence_type = None
|
| 738 |
+
st.session_state.input_metrics = None
|
| 739 |
+
st.session_state.organism = None
|
| 740 |
+
|
| 741 |
+
# Always display the last analysis if it exists in session state
|
| 742 |
+
if st.session_state.get('input_metrics') and st.session_state.get('sequence_type'):
|
| 743 |
+
input_metrics = st.session_state.input_metrics
|
| 744 |
+
sequence_type = st.session_state.sequence_type
|
| 745 |
+
st.subheader("📊 Input Analysis")
|
| 746 |
+
metrics_col1, metrics_col2, metrics_col3 = st.columns(3)
|
| 747 |
+
with metrics_col1:
|
| 748 |
+
unit = "codons" if sequence_type == "dna" else "AA"
|
| 749 |
+
length = input_metrics.get('length', 0) if input_metrics else 0
|
| 750 |
+
gc_content = input_metrics.get('gc_content', 0) if input_metrics else 0
|
| 751 |
+
st.metric("Length", f"{length} {unit}")
|
| 752 |
+
st.metric("GC Content", f"{gc_content:.1f}%")
|
| 753 |
+
with metrics_col2:
|
| 754 |
+
cai_val = input_metrics.get('cai') if input_metrics else None
|
| 755 |
+
if cai_val:
|
| 756 |
+
label = "CAI" if sequence_type == "dna" else "CAI (baseline)"
|
| 757 |
+
st.metric(label, f"{cai_val:.3f}")
|
| 758 |
+
else:
|
| 759 |
+
st.metric("CAI", "N/A")
|
| 760 |
+
with metrics_col3:
|
| 761 |
+
tai_val = input_metrics.get('tai') if input_metrics else None
|
| 762 |
+
if tai_val:
|
| 763 |
+
label = "tAI" if sequence_type == "dna" else "tAI (baseline)"
|
| 764 |
+
st.metric(label, f"{tai_val:.3f}")
|
| 765 |
+
else:
|
| 766 |
+
st.metric("tAI", "N/A")
|
| 767 |
+
st.subheader("🔍 Sequence Quality Analysis")
|
| 768 |
+
analysis_col1, analysis_col2, analysis_col3 = st.columns(3)
|
| 769 |
+
with analysis_col1:
|
| 770 |
+
sites_count = input_metrics.get('restriction_sites', 0) if input_metrics else 0
|
| 771 |
+
color = "normal" if sites_count <= 2 else "inverse"
|
| 772 |
+
st.metric("Restriction Sites", sites_count)
|
| 773 |
+
with analysis_col2:
|
| 774 |
+
neg_elements = input_metrics.get('negative_cis_elements', 0) if input_metrics else 0
|
| 775 |
+
st.metric("Negative Elements", neg_elements)
|
| 776 |
+
with analysis_col3:
|
| 777 |
+
homo_runs = input_metrics.get('homopolymer_runs', 0) if input_metrics else 0
|
| 778 |
+
st.metric("Homopolymer Runs", homo_runs)
|
| 779 |
+
baseline_dna = input_metrics.get('baseline_dna', '') if input_metrics else ''
|
| 780 |
+
if baseline_dna and len(baseline_dna) > 150:
|
| 781 |
+
st.subheader("📈 GC Content Distribution")
|
| 782 |
+
fig = create_gc_content_plot(baseline_dna)
|
| 783 |
+
fig.update_layout(
|
| 784 |
+
title="Input Sequence GC Content Analysis",
|
| 785 |
+
xaxis_title="Position (codons)",
|
| 786 |
+
yaxis_title="GC Content (%)",
|
| 787 |
+
hovermode='x unified'
|
| 788 |
+
)
|
| 789 |
+
st.plotly_chart(fig, use_container_width=True)
|
| 790 |
+
|
| 791 |
+
with col2:
|
| 792 |
+
st.header("🚀 Optimization Results")
|
| 793 |
+
# Enhanced optimization button
|
| 794 |
+
if (
|
| 795 |
+
st.session_state.get('sequence_clean')
|
| 796 |
+
and st.session_state.get('sequence_type')
|
| 797 |
+
and not st.session_state.optimization_running
|
| 798 |
+
):
|
| 799 |
+
st.markdown("**Ready to optimize your sequence!**")
|
| 800 |
+
strategy_info = st.container()
|
| 801 |
+
with strategy_info:
|
| 802 |
+
st.info(f"""
|
| 803 |
+
**Optimization Strategy:**
|
| 804 |
+
• Target organism: {st.session_state.organism}
|
| 805 |
+
• Model: Fine-tuned CodonTransformer (89.6M parameters)
|
| 806 |
+
• GC target: {gc_target_min}-{gc_target_max}%
|
| 807 |
+
• Mode: {'Deterministic' if use_deterministic else 'Stochastic'}
|
| 808 |
+
""")
|
| 809 |
+
if st.button("🚀 Optimize Sequence", type="primary", use_container_width=True):
|
| 810 |
+
st.session_state.results = None
|
| 811 |
+
if st.session_state.sequence_type == "dna":
|
| 812 |
+
protein_sequence = translate_dna_to_protein(st.session_state.sequence_clean)
|
| 813 |
+
run_optimization(protein_sequence, st.session_state.organism, use_post_processing)
|
| 814 |
+
else:
|
| 815 |
+
run_optimization(st.session_state.sequence_clean, st.session_state.organism, use_post_processing)
|
| 816 |
+
|
| 817 |
+
# Enhanced progress display
|
| 818 |
+
if st.session_state.optimization_running:
|
| 819 |
+
st.info("🔄 **Optimizing sequence with our model...**")
|
| 820 |
+
|
| 821 |
+
# Create progress container
|
| 822 |
+
progress_container = st.container()
|
| 823 |
+
with progress_container:
|
| 824 |
+
progress_bar = st.progress(0)
|
| 825 |
+
status_text = st.empty()
|
| 826 |
+
|
| 827 |
+
# Enhanced progress steps
|
| 828 |
+
steps = [
|
| 829 |
+
"🔍 Analyzing input sequence structure...",
|
| 830 |
+
"🧬 Loading fine-tuned CodonTransformer model...",
|
| 831 |
+
"⚡ Running optimization algorithm...",
|
| 832 |
+
"🎯 Optimizing GC content for synthesis...",
|
| 833 |
+
"✅ Finalizing optimized sequence..."
|
| 834 |
+
]
|
| 835 |
+
|
| 836 |
+
for i, step in enumerate(steps):
|
| 837 |
+
progress_value = int((i + 1) / len(steps) * 100)
|
| 838 |
+
progress_bar.progress(progress_value)
|
| 839 |
+
status_text.text(step)
|
| 840 |
+
time.sleep(0.8) # Realistic timing
|
| 841 |
+
|
| 842 |
+
progress_bar.empty()
|
| 843 |
+
status_text.empty()
|
| 844 |
+
|
| 845 |
+
# Enhanced results display
|
| 846 |
+
if st.session_state.results and not st.session_state.optimization_running:
|
| 847 |
+
if isinstance(st.session_state.results, str):
|
| 848 |
+
st.error(f"❌ **Optimization Failed:** {st.session_state.results}")
|
| 849 |
+
else:
|
| 850 |
+
display_optimization_results(
|
| 851 |
+
st.session_state.results,
|
| 852 |
+
st.session_state.get('organism', organism),
|
| 853 |
+
st.session_state.get('sequence_clean', ''),
|
| 854 |
+
st.session_state.get('sequence_type', 'protein'),
|
| 855 |
+
st.session_state.get('input_metrics', {})
|
| 856 |
+
)
|
| 857 |
+
|
| 858 |
+
def display_optimization_results(result, organism, original_sequence, sequence_type, input_metrics):
|
| 859 |
+
"""Enhanced results display with publication-quality visualizations"""
|
| 860 |
+
|
| 861 |
+
# Calculate optimized metrics
|
| 862 |
+
optimized_metrics = {
|
| 863 |
+
'gc_content': get_GC_content(result.predicted_dna),
|
| 864 |
+
'length': len(result.predicted_dna)
|
| 865 |
+
}
|
| 866 |
+
|
| 867 |
+
# Calculate CAI and tAI
|
| 868 |
+
try:
|
| 869 |
+
if 'cai_weights' in st.session_state and st.session_state['cai_weights']:
|
| 870 |
+
optimized_metrics['cai'] = CAI(result.predicted_dna, weights=st.session_state['cai_weights'])
|
| 871 |
+
else:
|
| 872 |
+
optimized_metrics['cai'] = None
|
| 873 |
+
except:
|
| 874 |
+
optimized_metrics['cai'] = None
|
| 875 |
+
|
| 876 |
+
try:
|
| 877 |
+
if 'tai_weights' in st.session_state and st.session_state['tai_weights']:
|
| 878 |
+
optimized_metrics['tai'] = calculate_tAI(result.predicted_dna, st.session_state['tai_weights'])
|
| 879 |
+
else:
|
| 880 |
+
optimized_metrics['tai'] = None
|
| 881 |
+
except:
|
| 882 |
+
optimized_metrics['tai'] = None
|
| 883 |
+
|
| 884 |
+
# Success header
|
| 885 |
+
st.success("✅ **Optimization Complete!** ")
|
| 886 |
+
|
| 887 |
+
# Key improvements summary
|
| 888 |
+
st.subheader("🎯 Optimization Improvements")
|
| 889 |
+
imp_col1, imp_col2, imp_col3 = st.columns(3)
|
| 890 |
+
|
| 891 |
+
if input_metrics is not None:
|
| 892 |
+
with imp_col1:
|
| 893 |
+
if input_metrics.get('gc_content') and optimized_metrics.get('gc_content'):
|
| 894 |
+
gc_change = optimized_metrics['gc_content'] - input_metrics['gc_content']
|
| 895 |
+
st.metric("GC Content", f"{optimized_metrics['gc_content']:.1f}%", delta=f"{gc_change:+.1f}%")
|
| 896 |
+
|
| 897 |
+
with imp_col2:
|
| 898 |
+
if input_metrics.get('cai') and optimized_metrics.get('cai'):
|
| 899 |
+
cai_change = optimized_metrics['cai'] - input_metrics['cai']
|
| 900 |
+
st.metric("CAI Score", f"{optimized_metrics['cai']:.3f}", delta=f"{cai_change:+.3f}")
|
| 901 |
+
|
| 902 |
+
with imp_col3:
|
| 903 |
+
if input_metrics.get('tai') and optimized_metrics.get('tai'):
|
| 904 |
+
tai_change = optimized_metrics['tai'] - input_metrics['tai']
|
| 905 |
+
st.metric("tAI Score", f"{optimized_metrics['tai']:.3f}", delta=f"{tai_change:+.3f}")
|
| 906 |
+
|
| 907 |
+
# Optimized DNA sequence display
|
| 908 |
+
st.subheader("🧬 Optimized DNA Sequence")
|
| 909 |
+
st.text_area("Optimized DNA Sequence", result.predicted_dna, height=100)
|
| 910 |
+
|
| 911 |
+
# Enhanced download and export options
|
| 912 |
+
col1, col2, col3 = st.columns(3)
|
| 913 |
+
with col1:
|
| 914 |
+
st.download_button(
|
| 915 |
+
label="📥 Download DNA (FASTA)",
|
| 916 |
+
data=f">Optimized_{organism.replace(' ', '_')}\n{result.predicted_dna}",
|
| 917 |
+
file_name=f"optimized_sequence_{organism.replace(' ', '_')}.fasta",
|
| 918 |
+
mime="text/plain"
|
| 919 |
+
)
|
| 920 |
+
|
| 921 |
+
with col2:
|
| 922 |
+
# Create CSV report
|
| 923 |
+
csv_data = f"Metric,Original,Optimized,Improvement\n"
|
| 924 |
+
csv_data += f"GC Content (%),{input_metrics['gc_content']:.1f},{optimized_metrics['gc_content']:.1f},{optimized_metrics['gc_content'] - input_metrics['gc_content']:+.1f}\n"
|
| 925 |
+
if input_metrics['cai'] and optimized_metrics['cai']:
|
| 926 |
+
csv_data += f"CAI Score,{input_metrics['cai']:.3f},{optimized_metrics['cai']:.3f},{optimized_metrics['cai'] - input_metrics['cai']:+.3f}\n"
|
| 927 |
+
if input_metrics['tai'] and optimized_metrics['tai']:
|
| 928 |
+
csv_data += f"tAI Score,{input_metrics['tai']:.3f},{optimized_metrics['tai']:.3f},{optimized_metrics['tai'] - input_metrics['tai']:+.3f}\n"
|
| 929 |
+
|
| 930 |
+
st.download_button(
|
| 931 |
+
label="📊 Download Metrics (CSV)",
|
| 932 |
+
data=csv_data,
|
| 933 |
+
file_name=f"optimization_metrics_{organism.replace(' ', '_')}.csv",
|
| 934 |
+
mime="text/csv"
|
| 935 |
+
)
|
| 936 |
+
|
| 937 |
+
with col3:
|
| 938 |
+
st.button("📄 Generate PDF Report", help="Coming soon: Publication-quality PDF report")
|
| 939 |
+
|
| 940 |
+
# Enhanced comparison visualizations
|
| 941 |
+
st.subheader("📊 Before vs After Analysis")
|
| 942 |
+
|
| 943 |
+
# Create enhanced comparison charts
|
| 944 |
+
create_enhanced_comparison_charts(input_metrics, optimized_metrics, original_sequence, result.predicted_dna, sequence_type)
|
| 945 |
+
|
| 946 |
+
def create_enhanced_comparison_charts(input_metrics, optimized_metrics, original_dna, optimized_dna, sequence_type):
|
| 947 |
+
"""Create publication-quality comparison visualizations"""
|
| 948 |
+
if input_metrics is None or optimized_metrics is None:
|
| 949 |
+
st.info("No comparison data available.")
|
| 950 |
+
return
|
| 951 |
+
|
| 952 |
+
# GC Content comparison
|
| 953 |
+
gc_comp_fig = create_gc_comparison_chart(input_metrics, optimized_metrics)
|
| 954 |
+
gc_comp_fig.update_layout(
|
| 955 |
+
title="GC Content Optimization Results",
|
| 956 |
+
font=dict(size=12),
|
| 957 |
+
height=350
|
| 958 |
+
)
|
| 959 |
+
st.plotly_chart(gc_comp_fig, use_container_width=True)
|
| 960 |
+
|
| 961 |
+
# Expression metrics comparison
|
| 962 |
+
if input_metrics.get('cai') and optimized_metrics.get('cai'):
|
| 963 |
+
expr_comp_fig = create_expression_comparison_chart(input_metrics, optimized_metrics)
|
| 964 |
+
expr_comp_fig.update_layout(
|
| 965 |
+
title="Expression Potential Improvement",
|
| 966 |
+
font=dict(size=12),
|
| 967 |
+
height=350
|
| 968 |
+
)
|
| 969 |
+
st.plotly_chart(expr_comp_fig, use_container_width=True)
|
| 970 |
+
|
| 971 |
+
# Side-by-side GC distribution analysis
|
| 972 |
+
st.subheader("📈 GC Content Distribution Analysis")
|
| 973 |
+
col1, col2 = st.columns(2)
|
| 974 |
+
|
| 975 |
+
with col1:
|
| 976 |
+
st.write(f"**{'Original DNA' if sequence_type == 'dna' else 'Baseline (Most Frequent Codons)'}**")
|
| 977 |
+
baseline_dna = input_metrics.get('baseline_dna') if input_metrics else None
|
| 978 |
+
plot_dna = baseline_dna if baseline_dna is not None else original_dna
|
| 979 |
+
if plot_dna is not None and isinstance(plot_dna, str) and len(plot_dna) > 150:
|
| 980 |
+
fig_before = create_gc_content_plot(plot_dna)
|
| 981 |
+
fig_before.update_layout(title="Before Optimization", height=300)
|
| 982 |
+
st.plotly_chart(fig_before, use_container_width=True)
|
| 983 |
+
else:
|
| 984 |
+
st.info("Sequence too short for sliding window analysis")
|
| 985 |
+
|
| 986 |
+
with col2:
|
| 987 |
+
st.write("** Model Optimized**")
|
| 988 |
+
if optimized_dna is not None and isinstance(optimized_dna, str) and len(optimized_dna) > 150:
|
| 989 |
+
fig_after = create_gc_content_plot(optimized_dna)
|
| 990 |
+
fig_after.update_layout(title="After Optimization", height=300)
|
| 991 |
+
st.plotly_chart(fig_after, use_container_width=True)
|
| 992 |
+
else:
|
| 993 |
+
st.info("Sequence too short for sliding window analysis")
|
| 994 |
+
|
| 995 |
+
def batch_processing_interface():
|
| 996 |
+
"""Batch processing interface for multiple sequences"""
|
| 997 |
+
st.header("📁 Batch Processing")
|
| 998 |
+
st.markdown("**Process multiple protein sequences simultaneously with optimization**")
|
| 999 |
+
|
| 1000 |
+
# File upload section
|
| 1001 |
+
st.subheader("📤 Upload Sequences")
|
| 1002 |
+
uploaded_file = st.file_uploader(
|
| 1003 |
+
"Choose a file with multiple sequences",
|
| 1004 |
+
type=['csv', 'xlsx', 'fasta', 'txt', 'fa'],
|
| 1005 |
+
help="Upload CSV, Excel (XLSX, with 'sequence' column) or FASTA format files"
|
| 1006 |
+
)
|
| 1007 |
+
|
| 1008 |
+
if uploaded_file:
|
| 1009 |
+
st.success(f"✅ File uploaded: {uploaded_file.name}")
|
| 1010 |
+
|
| 1011 |
+
# Process uploaded file
|
| 1012 |
+
try:
|
| 1013 |
+
def find_column(df, target):
|
| 1014 |
+
# Find column name case-insensitively and ignoring spaces
|
| 1015 |
+
for col in df.columns:
|
| 1016 |
+
if col.strip().lower() == target:
|
| 1017 |
+
return col
|
| 1018 |
+
return None
|
| 1019 |
+
|
| 1020 |
+
if uploaded_file.name.endswith('.csv'):
|
| 1021 |
+
df = pd.read_csv(uploaded_file)
|
| 1022 |
+
seq_col = find_column(df, 'sequence')
|
| 1023 |
+
name_col = find_column(df, 'name')
|
| 1024 |
+
if seq_col:
|
| 1025 |
+
sequences = df[seq_col].tolist()
|
| 1026 |
+
if name_col:
|
| 1027 |
+
names = df[name_col].tolist()
|
| 1028 |
+
else:
|
| 1029 |
+
names = [f"Sequence_{i+1}" for i in range(len(sequences))]
|
| 1030 |
+
else:
|
| 1031 |
+
st.error("CSV file must contain a column named 'sequence' (case-insensitive, spaces ignored)")
|
| 1032 |
+
return
|
| 1033 |
+
elif uploaded_file.name.endswith('.xlsx'):
|
| 1034 |
+
df = pd.read_excel(uploaded_file)
|
| 1035 |
+
seq_col = find_column(df, 'sequence')
|
| 1036 |
+
name_col = find_column(df, 'name')
|
| 1037 |
+
if seq_col:
|
| 1038 |
+
sequences = df[seq_col].tolist()
|
| 1039 |
+
if name_col:
|
| 1040 |
+
names = df[name_col].tolist()
|
| 1041 |
+
else:
|
| 1042 |
+
names = [f"Sequence_{i+1}" for i in range(len(sequences))]
|
| 1043 |
+
else:
|
| 1044 |
+
st.error("Excel file must contain a column named 'sequence' (case-insensitive, spaces ignored)")
|
| 1045 |
+
return
|
| 1046 |
+
else:
|
| 1047 |
+
# Handle FASTA format
|
| 1048 |
+
content = uploaded_file.read().decode('utf-8')
|
| 1049 |
+
sequences, names = parse_fasta_content(content)
|
| 1050 |
+
|
| 1051 |
+
st.info(f"📊 Found {len(sequences)} sequences ready for optimization")
|
| 1052 |
+
|
| 1053 |
+
# Batch configuration
|
| 1054 |
+
col1, col2 = st.columns(2)
|
| 1055 |
+
with col1:
|
| 1056 |
+
batch_organism = st.selectbox("Target Organism", [
|
| 1057 |
+
"Escherichia coli general", "Saccharomyces cerevisiae", "Homo sapiens"
|
| 1058 |
+
])
|
| 1059 |
+
with col2:
|
| 1060 |
+
max_sequences = st.number_input("Max sequences to process", 1, len(sequences), min(10, len(sequences)))
|
| 1061 |
+
|
| 1062 |
+
# Start batch processing
|
| 1063 |
+
if st.button("🚀 Start Batch Optimization", type="primary"):
|
| 1064 |
+
run_batch_optimization(sequences[:max_sequences], names[:max_sequences], batch_organism)
|
| 1065 |
+
|
| 1066 |
+
except Exception as e:
|
| 1067 |
+
st.error(f"Error processing file: {str(e)}")
|
| 1068 |
+
|
| 1069 |
+
# Batch results display
|
| 1070 |
+
if 'batch_results' in st.session_state and st.session_state.batch_results:
|
| 1071 |
+
display_batch_results()
|
| 1072 |
+
|
| 1073 |
+
def parse_fasta_content(content):
|
| 1074 |
+
"""Parse FASTA format content"""
|
| 1075 |
+
sequences = []
|
| 1076 |
+
names = []
|
| 1077 |
+
current_seq = ""
|
| 1078 |
+
current_name = ""
|
| 1079 |
+
|
| 1080 |
+
for line in content.split('\n'):
|
| 1081 |
+
line = line.strip()
|
| 1082 |
+
if line.startswith('>'):
|
| 1083 |
+
if current_seq:
|
| 1084 |
+
sequences.append(current_seq)
|
| 1085 |
+
names.append(current_name)
|
| 1086 |
+
current_name = line[1:] if len(line) > 1 else f"Sequence_{len(sequences)+1}"
|
| 1087 |
+
current_seq = ""
|
| 1088 |
+
else:
|
| 1089 |
+
current_seq += line
|
| 1090 |
+
|
| 1091 |
+
if current_seq:
|
| 1092 |
+
sequences.append(current_seq)
|
| 1093 |
+
names.append(current_name)
|
| 1094 |
+
|
| 1095 |
+
return sequences, names
|
| 1096 |
+
|
| 1097 |
+
def run_batch_optimization(sequences, names, organism):
|
| 1098 |
+
"""Run batch optimization with progress tracking"""
|
| 1099 |
+
st.session_state.batch_results = []
|
| 1100 |
+
st.session_state.batch_logs = [] # Collect info logs for auto-fixes
|
| 1101 |
+
|
| 1102 |
+
# Load reference data for CAI/tAI
|
| 1103 |
+
load_reference_data(organism)
|
| 1104 |
+
cai_weights = st.session_state.get('cai_weights', None)
|
| 1105 |
+
tai_weights = st.session_state.get('tai_weights', None)
|
| 1106 |
+
|
| 1107 |
+
# Create progress tracking
|
| 1108 |
+
progress_bar = st.progress(0)
|
| 1109 |
+
status_text = st.empty()
|
| 1110 |
+
|
| 1111 |
+
for i, (seq, name) in enumerate(zip(sequences, names)):
|
| 1112 |
+
progress = (i + 1) / len(sequences)
|
| 1113 |
+
progress_bar.progress(progress)
|
| 1114 |
+
status_text.text(f"Processing {name} ({i+1}/{len(sequences)})")
|
| 1115 |
+
|
| 1116 |
+
try:
|
| 1117 |
+
# Validate sequence and get possibly fixed sequence
|
| 1118 |
+
is_valid, message, sequence_type, fixed_seq = validate_sequence(seq)
|
| 1119 |
+
if is_valid:
|
| 1120 |
+
# Log if auto-fixed
|
| 1121 |
+
if 'auto-fixed' in message:
|
| 1122 |
+
st.session_state.batch_logs.append(f"{name}: {message}")
|
| 1123 |
+
# Calculate original metrics (use fixed_seq for DNA)
|
| 1124 |
+
if sequence_type == "dna":
|
| 1125 |
+
orig_gc = get_GC_content(fixed_seq)
|
| 1126 |
+
orig_cai = CAI(fixed_seq, weights=cai_weights) if cai_weights else None
|
| 1127 |
+
orig_tai = calculate_tAI(fixed_seq, tai_weights) if tai_weights else None
|
| 1128 |
+
else:
|
| 1129 |
+
# For protein, create baseline DNA
|
| 1130 |
+
most_frequent_codons = {
|
| 1131 |
+
'A': 'GCG', 'C': 'TGC', 'D': 'GAT', 'E': 'GAA', 'F': 'TTT',
|
| 1132 |
+
'G': 'GGC', 'H': 'CAT', 'I': 'ATT', 'K': 'AAA', 'L': 'CTG',
|
| 1133 |
+
'M': 'ATG', 'N': 'AAC', 'P': 'CCG', 'Q': 'CAG', 'R': 'CGC',
|
| 1134 |
+
'S': 'TCG', 'T': 'ACG', 'V': 'GTG', 'W': 'TGG', 'Y': 'TAT',
|
| 1135 |
+
'*': 'TAA', '_': 'TAA'
|
| 1136 |
+
}
|
| 1137 |
+
baseline_dna = ''.join([most_frequent_codons.get(aa, 'NNN') for aa in fixed_seq])
|
| 1138 |
+
orig_gc = get_GC_content(baseline_dna)
|
| 1139 |
+
orig_cai = CAI(baseline_dna, weights=cai_weights) if cai_weights else None
|
| 1140 |
+
orig_tai = calculate_tAI(baseline_dna, tai_weights) if tai_weights else None
|
| 1141 |
+
|
| 1142 |
+
# Run optimization using the fixed sequence
|
| 1143 |
+
result = predict_dna_sequence(
|
| 1144 |
+
protein=fixed_seq if sequence_type == "protein" else translate_dna_to_protein(fixed_seq),
|
| 1145 |
+
organism=organism,
|
| 1146 |
+
device=st.session_state.device,
|
| 1147 |
+
model=st.session_state.model,
|
| 1148 |
+
deterministic=True,
|
| 1149 |
+
match_protein=True,
|
| 1150 |
+
)
|
| 1151 |
+
|
| 1152 |
+
# If result is a list, use the first element
|
| 1153 |
+
if isinstance(result, list):
|
| 1154 |
+
result_obj = result[0]
|
| 1155 |
+
else:
|
| 1156 |
+
result_obj = result
|
| 1157 |
+
|
| 1158 |
+
# Calculate optimized metrics
|
| 1159 |
+
opt_gc = get_GC_content(result_obj.predicted_dna)
|
| 1160 |
+
opt_cai = CAI(result_obj.predicted_dna, weights=cai_weights) if cai_weights else None
|
| 1161 |
+
opt_tai = calculate_tAI(result_obj.predicted_dna, tai_weights) if tai_weights else None
|
| 1162 |
+
|
| 1163 |
+
metrics = {
|
| 1164 |
+
'name': name,
|
| 1165 |
+
'original_sequence': fixed_seq,
|
| 1166 |
+
'optimized_dna': result_obj.predicted_dna,
|
| 1167 |
+
'gc_content_before': orig_gc,
|
| 1168 |
+
'gc_content_after': opt_gc,
|
| 1169 |
+
'cai_before': orig_cai,
|
| 1170 |
+
'cai_after': opt_cai,
|
| 1171 |
+
'tai_before': orig_tai,
|
| 1172 |
+
'tai_after': opt_tai,
|
| 1173 |
+
'length_before': len(fixed_seq),
|
| 1174 |
+
'length_after': len(result_obj.predicted_dna),
|
| 1175 |
+
'validation_message': message
|
| 1176 |
+
}
|
| 1177 |
+
|
| 1178 |
+
st.session_state.batch_results.append(metrics)
|
| 1179 |
+
else:
|
| 1180 |
+
# Only skip if truly invalid (not auto-fixable)
|
| 1181 |
+
st.session_state.batch_logs.append(f"{name}: {message}")
|
| 1182 |
+
|
| 1183 |
+
except Exception as e:
|
| 1184 |
+
st.session_state.batch_logs.append(f"{name}: Error processing: {str(e)}")
|
| 1185 |
+
|
| 1186 |
+
progress_bar.empty()
|
| 1187 |
+
status_text.empty()
|
| 1188 |
+
st.success(f"✅ Batch optimization complete! Processed {len(st.session_state.batch_results)} sequences.")
|
| 1189 |
+
|
| 1190 |
+
def display_batch_results():
|
| 1191 |
+
"""Display batch processing results"""
|
| 1192 |
+
st.subheader("📊 Batch Results")
|
| 1193 |
+
|
| 1194 |
+
# Show all logs (auto-fixes and errors)
|
| 1195 |
+
if hasattr(st.session_state, 'batch_logs') and st.session_state.batch_logs:
|
| 1196 |
+
for log in st.session_state.batch_logs:
|
| 1197 |
+
st.info(log)
|
| 1198 |
+
|
| 1199 |
+
results_df = pd.DataFrame(st.session_state.batch_results)
|
| 1200 |
+
|
| 1201 |
+
# Summary statistics
|
| 1202 |
+
col1, col2, col3, col4 = st.columns(4)
|
| 1203 |
+
with col1:
|
| 1204 |
+
st.metric("Sequences Processed", len(results_df))
|
| 1205 |
+
with col2:
|
| 1206 |
+
st.metric("Avg GC Before", f"{results_df['gc_content_before'].mean():.1f}%")
|
| 1207 |
+
st.metric("Avg GC After", f"{results_df['gc_content_after'].mean():.1f}%")
|
| 1208 |
+
with col3:
|
| 1209 |
+
st.metric("Avg CAI Before", f"{results_df['cai_before'].mean():.3f}")
|
| 1210 |
+
st.metric("Avg CAI After", f"{results_df['cai_after'].mean():.3f}")
|
| 1211 |
+
with col4:
|
| 1212 |
+
st.metric("Avg tAI Before", f"{results_df['tai_before'].mean():.3f}")
|
| 1213 |
+
st.metric("Avg tAI After", f"{results_df['tai_after'].mean():.3f}")
|
| 1214 |
+
|
| 1215 |
+
# CAI Extremes Analysis
|
| 1216 |
+
st.subheader("🎯 CAI Performance Analysis")
|
| 1217 |
+
|
| 1218 |
+
# Filter out rows with NaN CAI values for analysis
|
| 1219 |
+
valid_cai_df = results_df.dropna(subset=['cai_after'])
|
| 1220 |
+
|
| 1221 |
+
if len(valid_cai_df) > 0:
|
| 1222 |
+
# Find lowest and highest CAI sequences
|
| 1223 |
+
lowest_cai_idx = valid_cai_df['cai_after'].idxmin()
|
| 1224 |
+
highest_cai_idx = valid_cai_df['cai_after'].idxmax()
|
| 1225 |
+
|
| 1226 |
+
lowest_cai_row = results_df.loc[lowest_cai_idx]
|
| 1227 |
+
highest_cai_row = results_df.loc[highest_cai_idx]
|
| 1228 |
+
|
| 1229 |
+
col1, col2 = st.columns(2)
|
| 1230 |
+
|
| 1231 |
+
with col1:
|
| 1232 |
+
st.markdown("**🔻 Lowest CAI Sequence**")
|
| 1233 |
+
st.write(f"**Name:** {lowest_cai_row['name']}")
|
| 1234 |
+
st.metric("CAI Score", f"{lowest_cai_row['cai_after']:.3f}")
|
| 1235 |
+
st.metric("GC Content", f"{lowest_cai_row['gc_content_after']:.1f}%")
|
| 1236 |
+
st.metric("tAI Score", f"{lowest_cai_row['tai_after']:.3f}")
|
| 1237 |
+
st.metric("Length", f"{lowest_cai_row['length_after']} bp")
|
| 1238 |
+
|
| 1239 |
+
# Show improvement
|
| 1240 |
+
if pd.notna(lowest_cai_row['cai_before']):
|
| 1241 |
+
cai_improvement = lowest_cai_row['cai_after'] - lowest_cai_row['cai_before']
|
| 1242 |
+
st.metric("CAI Improvement", f"{cai_improvement:+.3f}")
|
| 1243 |
+
|
| 1244 |
+
with col2:
|
| 1245 |
+
st.markdown("**🔺 Highest CAI Sequence**")
|
| 1246 |
+
st.write(f"**Name:** {highest_cai_row['name']}")
|
| 1247 |
+
st.metric("CAI Score", f"{highest_cai_row['cai_after']:.3f}")
|
| 1248 |
+
st.metric("GC Content", f"{highest_cai_row['gc_content_after']:.1f}%")
|
| 1249 |
+
st.metric("tAI Score", f"{highest_cai_row['tai_after']:.3f}")
|
| 1250 |
+
st.metric("Length", f"{highest_cai_row['length_after']} bp")
|
| 1251 |
+
|
| 1252 |
+
# Show improvement
|
| 1253 |
+
if pd.notna(highest_cai_row['cai_before']):
|
| 1254 |
+
cai_improvement = highest_cai_row['cai_after'] - highest_cai_row['cai_before']
|
| 1255 |
+
st.metric("CAI Improvement", f"{cai_improvement:+.3f}")
|
| 1256 |
+
|
| 1257 |
+
# CAI Distribution Chart
|
| 1258 |
+
st.subheader("📊 CAI Distribution")
|
| 1259 |
+
fig = go.Figure()
|
| 1260 |
+
fig.add_trace(go.Histogram(
|
| 1261 |
+
x=valid_cai_df['cai_after'],
|
| 1262 |
+
nbinsx=20,
|
| 1263 |
+
name='Optimized CAI Scores',
|
| 1264 |
+
marker_color='darkblue',
|
| 1265 |
+
opacity=0.7
|
| 1266 |
+
))
|
| 1267 |
+
|
| 1268 |
+
# Add vertical lines for lowest and highest
|
| 1269 |
+
fig.add_vline(
|
| 1270 |
+
x=lowest_cai_row['cai_after'],
|
| 1271 |
+
line_dash="dash",
|
| 1272 |
+
line_color="red",
|
| 1273 |
+
annotation_text=f"Lowest: {lowest_cai_row['cai_after']:.3f}"
|
| 1274 |
+
)
|
| 1275 |
+
fig.add_vline(
|
| 1276 |
+
x=highest_cai_row['cai_after'],
|
| 1277 |
+
line_dash="dash",
|
| 1278 |
+
line_color="green",
|
| 1279 |
+
annotation_text=f"Highest: {highest_cai_row['cai_after']:.3f}"
|
| 1280 |
+
)
|
| 1281 |
+
|
| 1282 |
+
fig.update_layout(
|
| 1283 |
+
title="Distribution of Optimized CAI Scores",
|
| 1284 |
+
xaxis_title="CAI Score",
|
| 1285 |
+
yaxis_title="Number of Sequences",
|
| 1286 |
+
height=400,
|
| 1287 |
+
showlegend=False
|
| 1288 |
+
)
|
| 1289 |
+
st.plotly_chart(fig, use_container_width=True)
|
| 1290 |
+
|
| 1291 |
+
# GC Content Distribution Chart
|
| 1292 |
+
st.subheader("📊 GC Content Distribution")
|
| 1293 |
+
valid_gc_df = results_df.dropna(subset=['gc_content_after'])
|
| 1294 |
+
if len(valid_gc_df) > 0:
|
| 1295 |
+
lowest_gc_idx = valid_gc_df['gc_content_after'].idxmin()
|
| 1296 |
+
highest_gc_idx = valid_gc_df['gc_content_after'].idxmax()
|
| 1297 |
+
lowest_gc_row = results_df.loc[lowest_gc_idx]
|
| 1298 |
+
highest_gc_row = results_df.loc[highest_gc_idx]
|
| 1299 |
+
|
| 1300 |
+
fig_gc = go.Figure()
|
| 1301 |
+
fig_gc.add_trace(go.Histogram(
|
| 1302 |
+
x=valid_gc_df['gc_content_after'],
|
| 1303 |
+
nbinsx=20,
|
| 1304 |
+
name='Optimized GC Content',
|
| 1305 |
+
marker_color='teal',
|
| 1306 |
+
opacity=0.7
|
| 1307 |
+
))
|
| 1308 |
+
fig_gc.add_vline(
|
| 1309 |
+
x=lowest_gc_row['gc_content_after'],
|
| 1310 |
+
line_dash="dash",
|
| 1311 |
+
line_color="red",
|
| 1312 |
+
annotation_text=f"Lowest: {lowest_gc_row['gc_content_after']:.1f}%"
|
| 1313 |
+
)
|
| 1314 |
+
fig_gc.add_vline(
|
| 1315 |
+
x=highest_gc_row['gc_content_after'],
|
| 1316 |
+
line_dash="dash",
|
| 1317 |
+
line_color="green",
|
| 1318 |
+
annotation_text=f"Highest: {highest_gc_row['gc_content_after']:.1f}%"
|
| 1319 |
+
)
|
| 1320 |
+
fig_gc.update_layout(
|
| 1321 |
+
title="Distribution of Optimized GC Content",
|
| 1322 |
+
xaxis_title="GC Content (%)",
|
| 1323 |
+
yaxis_title="Number of Sequences",
|
| 1324 |
+
height=400,
|
| 1325 |
+
showlegend=False
|
| 1326 |
+
)
|
| 1327 |
+
st.plotly_chart(fig_gc, use_container_width=True)
|
| 1328 |
+
else:
|
| 1329 |
+
st.warning("⚠️ No valid GC content values found in the batch results.")
|
| 1330 |
+
|
| 1331 |
+
else:
|
| 1332 |
+
st.warning("⚠️ No valid CAI scores found in the batch results. Check if CAI weights are properly loaded.")
|
| 1333 |
+
|
| 1334 |
+
# Sequence selector
|
| 1335 |
+
seq_names = results_df['name'].tolist()
|
| 1336 |
+
selected_seq = st.selectbox("Select a sequence to view details", seq_names)
|
| 1337 |
+
seq_row = results_df[results_df['name'] == selected_seq].iloc[0]
|
| 1338 |
+
|
| 1339 |
+
st.markdown(f"### Details for: {selected_seq}")
|
| 1340 |
+
if 'validation_message' in seq_row and 'auto-fixed' in seq_row['validation_message']:
|
| 1341 |
+
st.info(seq_row['validation_message'])
|
| 1342 |
+
col1, col2 = st.columns(2)
|
| 1343 |
+
with col1:
|
| 1344 |
+
st.markdown("**Original Sequence**")
|
| 1345 |
+
st.text_area("Original Sequence", seq_row['original_sequence'], height=100)
|
| 1346 |
+
st.metric("GC Content (Before)", f"{seq_row['gc_content_before']:.1f}%")
|
| 1347 |
+
st.metric("CAI (Before)", f"{seq_row['cai_before']:.3f}")
|
| 1348 |
+
st.metric("tAI (Before)", f"{seq_row['tai_before']:.3f}")
|
| 1349 |
+
st.metric("Length (Before)", f"{seq_row['length_before']}")
|
| 1350 |
+
with col2:
|
| 1351 |
+
st.markdown("**Optimized Sequence**")
|
| 1352 |
+
st.text_area("Optimized Sequence", seq_row['optimized_dna'], height=100)
|
| 1353 |
+
st.metric("GC Content (After)", f"{seq_row['gc_content_after']:.1f}%")
|
| 1354 |
+
st.metric("CAI (After)", f"{seq_row['cai_after']:.3f}")
|
| 1355 |
+
st.metric("tAI (After)", f"{seq_row['tai_after']:.3f}")
|
| 1356 |
+
st.metric("Length (After)", f"{seq_row['length_after']}")
|
| 1357 |
+
|
| 1358 |
+
# Plots for before/after GC content
|
| 1359 |
+
st.subheader("GC Content Distribution (Before vs After)")
|
| 1360 |
+
if len(seq_row['original_sequence']) > 150 and len(seq_row['optimized_dna']) > 150:
|
| 1361 |
+
fig_before = create_gc_content_plot(seq_row['original_sequence'])
|
| 1362 |
+
fig_before.update_layout(title="Before Optimization", height=300)
|
| 1363 |
+
fig_after = create_gc_content_plot(seq_row['optimized_dna'])
|
| 1364 |
+
fig_after.update_layout(title="After Optimization", height=300)
|
| 1365 |
+
st.plotly_chart(fig_before, use_container_width=True)
|
| 1366 |
+
st.plotly_chart(fig_after, use_container_width=True)
|
| 1367 |
+
else:
|
| 1368 |
+
st.info("Sequence(s) too short for sliding window analysis")
|
| 1369 |
+
|
| 1370 |
+
# Download batch results
|
| 1371 |
+
if st.button("📥 Download Batch Results"):
|
| 1372 |
+
csv_data = results_df.to_csv(index=False)
|
| 1373 |
+
st.download_button(
|
| 1374 |
+
label="Download CSV",
|
| 1375 |
+
data=csv_data,
|
| 1376 |
+
file_name="batch_optimization_results.csv",
|
| 1377 |
+
mime="text/csv"
|
| 1378 |
+
)
|
| 1379 |
+
|
| 1380 |
+
def comparative_analysis_interface():
|
| 1381 |
+
"""Comparative analysis interface"""
|
| 1382 |
+
st.header("📊 Comparative Analysis")
|
| 1383 |
+
st.markdown("**Compare optimization strategies side-by-side**")
|
| 1384 |
+
|
| 1385 |
+
st.info("🚧 **Coming Soon:** Compare our model against traditional methods (HFC, BFC, URC) and generate publication-quality comparative analysis.")
|
| 1386 |
+
|
| 1387 |
+
# Placeholder for future implementation
|
| 1388 |
+
col1, col2 = st.columns(2)
|
| 1389 |
+
with col1:
|
| 1390 |
+
st.subheader("Algorithm Comparison")
|
| 1391 |
+
st.write("• ColiFormer (Our Model)")
|
| 1392 |
+
st.write("• High Frequency Choice (HFC)")
|
| 1393 |
+
st.write("• Background Frequency Choice (BFC)")
|
| 1394 |
+
st.write("• Uniform Random Choice (URC)")
|
| 1395 |
+
|
| 1396 |
+
with col2:
|
| 1397 |
+
st.subheader("Comparison Metrics")
|
| 1398 |
+
st.write("• CAI Score Comparison")
|
| 1399 |
+
st.write("• tAI Score Comparison")
|
| 1400 |
+
st.write("• GC Content Analysis")
|
| 1401 |
+
st.write("• Statistical Significance Testing")
|
| 1402 |
+
|
| 1403 |
+
def advanced_settings_interface():
|
| 1404 |
+
"""Advanced settings and configuration interface"""
|
| 1405 |
+
st.header("⚙️ Advanced Settings")
|
| 1406 |
+
st.markdown("**Configure advanced parameters and model settings**")
|
| 1407 |
+
|
| 1408 |
+
# Model configuration
|
| 1409 |
+
st.subheader("🤖 Model Configuration")
|
| 1410 |
+
col1, col2 = st.columns(2)
|
| 1411 |
+
|
| 1412 |
+
with col1:
|
| 1413 |
+
st.write("**Current Model Status:**")
|
| 1414 |
+
if st.session_state.model:
|
| 1415 |
+
model_type = getattr(st.session_state, 'model_type', 'unknown')
|
| 1416 |
+
st.success(f"✅ Model loaded: {model_type}")
|
| 1417 |
+
st.write(f"Device: {st.session_state.device}")
|
| 1418 |
+
else:
|
| 1419 |
+
st.warning("⚠️ Model not loaded")
|
| 1420 |
+
|
| 1421 |
+
with col2:
|
| 1422 |
+
st.write("**Model Information:**")
|
| 1423 |
+
st.write("• Architecture: BigBird Transformer")
|
| 1424 |
+
st.write("• Parameters: 89.6M")
|
| 1425 |
+
st.write("• Training: 4,316 high-CAI E. coli genes")
|
| 1426 |
+
st.write("• Performance: +5.1% CAI, +8.6% tAI")
|
| 1427 |
+
|
| 1428 |
+
# Performance tuning
|
| 1429 |
+
st.subheader("⚡ Performance Tuning")
|
| 1430 |
+
|
| 1431 |
+
# Memory management
|
| 1432 |
+
col1, col2 = st.columns(2)
|
| 1433 |
+
with col1:
|
| 1434 |
+
if st.button("🧹 Clear Cache"):
|
| 1435 |
+
st.cache_data.clear()
|
| 1436 |
+
st.success("Cache cleared successfully")
|
| 1437 |
+
|
| 1438 |
+
with col2:
|
| 1439 |
+
if st.button("🔄 Reload Model"):
|
| 1440 |
+
st.session_state.model = None
|
| 1441 |
+
st.session_state.tokenizer = None
|
| 1442 |
+
st.rerun()
|
| 1443 |
+
|
| 1444 |
+
# System information
|
| 1445 |
+
st.subheader("💻 System Information")
|
| 1446 |
+
import torch
|
| 1447 |
+
col1, col2, col3 = st.columns(3)
|
| 1448 |
+
|
| 1449 |
+
with col1:
|
| 1450 |
+
st.write("**PyTorch:**")
|
| 1451 |
+
st.write(f"Version: {torch.__version__}")
|
| 1452 |
+
st.write(f"CUDA Available: {torch.cuda.is_available()}")
|
| 1453 |
+
|
| 1454 |
+
with col2:
|
| 1455 |
+
st.write("**Device:**")
|
| 1456 |
+
st.write(f"Current: {st.session_state.device}")
|
| 1457 |
+
if torch.cuda.is_available():
|
| 1458 |
+
st.write(f"GPU: {torch.cuda.get_device_name()}")
|
| 1459 |
+
|
| 1460 |
+
with col3:
|
| 1461 |
+
st.write("**Memory:**")
|
| 1462 |
+
if torch.cuda.is_available():
|
| 1463 |
+
gpu_memory = torch.cuda.get_device_properties(0).total_memory / 1e9
|
| 1464 |
+
st.write(f"GPU Memory: {gpu_memory:.1f} GB")
|
| 1465 |
+
|
| 1466 |
+
# Footer
|
| 1467 |
+
st.markdown("---")
|
| 1468 |
+
st.markdown("**ColiFormer **")
|
| 1469 |
+
st.markdown("🚀 Built for Nature Communications-level research • Targeting >20% CAI improvements • Aug 2025 experimental validation")
|
| 1470 |
+
|
| 1471 |
+
if __name__ == "__main__":
|
| 1472 |
+
main()
|
requirements.txt
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit>=1.28.0
|
| 2 |
+
torch>=1.13.0
|
| 3 |
+
pandas>=1.5.0
|
| 4 |
+
numpy>=1.21.0
|
| 5 |
+
plotly>=5.0.0
|
| 6 |
+
transformers>=4.21.0
|
| 7 |
+
scipy>=1.9.0
|
| 8 |
+
tokenizers>=0.13.0
|
| 9 |
+
tqdm>=4.64.0
|
| 10 |
+
matplotlib>=3.5.0
|
| 11 |
+
seaborn>=0.11.0
|
| 12 |
+
onnxruntime>=1.15.0
|
| 13 |
+
python-codon-tables>=0.1.12
|
| 14 |
+
biopython>=1.79
|
| 15 |
+
scikit-learn>=1.0.0
|
| 16 |
+
requests>=2.25.0
|
| 17 |
+
ipywidgets>=7.6.0
|
| 18 |
+
huggingface-hub>=0.20.0
|
| 19 |
+
datasets>=2.0.0
|
| 20 |
+
git+https://github.com/Benjamin-Lee/CodonAdaptationIndex.git
|