Spaces:
No application file
No application file
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,407 +8,293 @@ import os
|
|
| 8 |
import re
|
| 9 |
import logging
|
| 10 |
import numpy as np
|
|
|
|
|
|
|
|
|
|
| 11 |
import tempfile
|
| 12 |
import shutil
|
| 13 |
import sys
|
| 14 |
from pathlib import Path
|
| 15 |
|
| 16 |
# --- Global Variables ---
|
| 17 |
-
MAFFT_PATH = "mafft/mafftdir/bin/mafft"
|
| 18 |
-
IQTREE_PATH = "iqtree/bin/iqtree2"
|
| 19 |
|
| 20 |
# --- Logging ---
|
| 21 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
| 22 |
|
| 23 |
-
# ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
boundary_model = None
|
| 25 |
keras_model = None
|
| 26 |
kmer_to_index = None
|
| 27 |
-
csv_data = None
|
| 28 |
|
| 29 |
-
#
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
# Look for start codons (ATG) and stop codons
|
| 44 |
-
start_codons = ['ATG']
|
| 45 |
-
stop_codons = ['TAA', 'TAG', 'TGA']
|
| 46 |
-
|
| 47 |
-
for i in range(len(sequence) - 2):
|
| 48 |
-
codon = sequence[i:i+3]
|
| 49 |
-
if codon in start_codons:
|
| 50 |
-
predictions.append(1) # Start
|
| 51 |
-
probabilities.append(0.8)
|
| 52 |
-
elif codon in stop_codons:
|
| 53 |
-
predictions.append(2) # Stop
|
| 54 |
-
probabilities.append(0.7)
|
| 55 |
-
else:
|
| 56 |
-
predictions.append(0) # Non-coding
|
| 57 |
-
probabilities.append(0.3)
|
| 58 |
-
|
| 59 |
-
confidence = 0.6
|
| 60 |
-
return predictions, probabilities, confidence
|
| 61 |
-
|
| 62 |
-
def extract_gene_regions(self, predictions, sequence):
|
| 63 |
-
"""Extract potential gene regions"""
|
| 64 |
-
regions = []
|
| 65 |
-
start_pos = None
|
| 66 |
-
|
| 67 |
-
for i, pred in enumerate(predictions):
|
| 68 |
-
if pred == 1 and start_pos is None: # Start codon
|
| 69 |
-
start_pos = i
|
| 70 |
-
elif pred == 2 and start_pos is not None: # Stop codon
|
| 71 |
-
if i - start_pos > 150: # Minimum gene length
|
| 72 |
-
regions.append({
|
| 73 |
-
'start': start_pos,
|
| 74 |
-
'end': i + 3,
|
| 75 |
-
'sequence': sequence[start_pos:i+3],
|
| 76 |
-
'confidence': 0.6
|
| 77 |
-
})
|
| 78 |
-
start_pos = None
|
| 79 |
-
|
| 80 |
-
return regions
|
| 81 |
|
| 82 |
-
# Try to load
|
| 83 |
try:
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
)
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
logging.info("Using simple boundary model (fallback)")
|
| 99 |
-
except Exception as e:
|
| 100 |
-
logging.warning(f"Could not load HF model: {e}")
|
| 101 |
-
boundary_model = SimpleGenePredictor()
|
| 102 |
-
logging.info("Using simple boundary model (fallback)")
|
| 103 |
-
|
| 104 |
-
# Try to load Keras model
|
| 105 |
-
try:
|
| 106 |
-
from tensorflow.keras.models import load_model
|
| 107 |
-
keras_path = hf_hub_download(
|
| 108 |
-
repo_id=model_repo,
|
| 109 |
-
filename="best_model.keras",
|
| 110 |
-
token=hf_token
|
| 111 |
-
)
|
| 112 |
-
kmer_path = hf_hub_download(
|
| 113 |
-
repo_id=model_repo,
|
| 114 |
-
filename="kmer_to_index.pkl",
|
| 115 |
-
token=hf_token
|
| 116 |
-
)
|
| 117 |
-
|
| 118 |
-
if os.path.exists(keras_path) and os.path.exists(kmer_path):
|
| 119 |
-
keras_model = load_model(keras_path)
|
| 120 |
-
with open(kmer_path, "rb") as f:
|
| 121 |
-
kmer_to_index = pickle.load(f)
|
| 122 |
-
logging.info("Keras model loaded successfully")
|
| 123 |
-
else:
|
| 124 |
-
logging.warning("Keras model files not found")
|
| 125 |
-
except Exception as e:
|
| 126 |
-
logging.warning(f"Could not load Keras model: {e}")
|
| 127 |
-
|
| 128 |
-
except ImportError:
|
| 129 |
-
logging.warning("huggingface_hub not available, using fallback models")
|
| 130 |
-
boundary_model = SimpleGenePredictor()
|
| 131 |
|
| 132 |
-
#
|
|
|
|
| 133 |
try:
|
| 134 |
-
|
| 135 |
if os.path.exists(csv_path):
|
| 136 |
-
|
| 137 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
else:
|
| 139 |
-
logging.
|
|
|
|
| 140 |
except Exception as e:
|
| 141 |
-
logging.
|
|
|
|
| 142 |
|
| 143 |
-
# ---
|
| 144 |
-
def
|
| 145 |
-
"""
|
| 146 |
-
mafft_available = shutil.which('mafft') is not None or os.path.exists(MAFFT_PATH)
|
| 147 |
-
iqtree_available = shutil.which('iqtree2') is not None or shutil.which('iqtree') is not None or os.path.exists(IQTREE_PATH)
|
| 148 |
-
|
| 149 |
-
mafft_cmd = 'mafft' if shutil.which('mafft') else MAFFT_PATH if os.path.exists(MAFFT_PATH) else None
|
| 150 |
-
iqtree_cmd = 'iqtree2' if shutil.which('iqtree2') else 'iqtree' if shutil.which('iqtree') else IQTREE_PATH if os.path.exists(IQTREE_PATH) else None
|
| 151 |
-
|
| 152 |
-
return mafft_available, iqtree_available, mafft_cmd, iqtree_cmd
|
| 153 |
-
|
| 154 |
-
# --- Prediction Functions ---
|
| 155 |
-
def predict_gene_sequence(sequence):
|
| 156 |
-
"""Main gene prediction function"""
|
| 157 |
try:
|
| 158 |
-
if not
|
| 159 |
-
return "
|
| 160 |
|
| 161 |
# Clean sequence
|
| 162 |
sequence = re.sub(r'[^ATCG]', '', sequence.upper())
|
| 163 |
-
|
| 164 |
if len(sequence) < 10:
|
| 165 |
-
return "Sequence too short
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
|
| 167 |
-
|
| 168 |
-
results.append(f"🧬 GENE SEQUENCE ANALYSIS")
|
| 169 |
-
results.append(f"Input sequence length: {len(sequence)} bp")
|
| 170 |
-
results.append("=" * 50)
|
| 171 |
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
if regions:
|
| 183 |
-
for i, region in enumerate(regions[:3]):
|
| 184 |
-
results.append(f"\nRegion {i+1}:")
|
| 185 |
-
results.append(f" - Start: {region['start']}")
|
| 186 |
-
results.append(f" - End: {region['end']}")
|
| 187 |
-
results.append(f" - Length: {len(region['sequence'])} bp")
|
| 188 |
-
results.append(f" - Confidence: {region.get('confidence', 0):.4f}")
|
| 189 |
-
|
| 190 |
-
except Exception as e:
|
| 191 |
-
results.append(f"❌ Boundary prediction failed: {str(e)}")
|
| 192 |
-
else:
|
| 193 |
-
results.append("\n❌ Boundary model not available")
|
| 194 |
|
| 195 |
-
#
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 221 |
else:
|
| 222 |
-
|
| 223 |
|
| 224 |
-
|
| 225 |
-
results.append("\n📊 SEQUENCE STATISTICS:")
|
| 226 |
-
gc_content = (sequence.count('G') + sequence.count('C')) / len(sequence) * 100
|
| 227 |
-
results.append(f"- GC Content: {gc_content:.2f}%")
|
| 228 |
-
results.append(f"- A: {sequence.count('A')} ({sequence.count('A')/len(sequence)*100:.1f}%)")
|
| 229 |
-
results.append(f"- T: {sequence.count('T')} ({sequence.count('T')/len(sequence)*100:.1f}%)")
|
| 230 |
-
results.append(f"- G: {sequence.count('G')} ({sequence.count('G')/len(sequence)*100:.1f}%)")
|
| 231 |
-
results.append(f"- C: {sequence.count('C')} ({sequence.count('C')/len(sequence)*100:.1f}%)")
|
| 232 |
-
|
| 233 |
-
return "\n".join(results)
|
| 234 |
|
| 235 |
except Exception as e:
|
| 236 |
-
logging.error(f"
|
| 237 |
-
return f"
|
| 238 |
|
| 239 |
-
def
|
| 240 |
-
"""
|
|
|
|
|
|
|
| 241 |
try:
|
| 242 |
-
if
|
| 243 |
-
return "
|
| 244 |
-
|
| 245 |
-
# Read file content
|
| 246 |
-
with open(file.name, 'r') as f:
|
| 247 |
-
content = f.read()
|
| 248 |
-
|
| 249 |
-
# Parse FASTA
|
| 250 |
-
sequences = {}
|
| 251 |
-
current_seq = ""
|
| 252 |
-
current_name = ""
|
| 253 |
-
|
| 254 |
-
lines = content.strip().split('\n')
|
| 255 |
-
for line in lines:
|
| 256 |
-
line = line.strip()
|
| 257 |
-
if line.startswith('>'):
|
| 258 |
-
if current_name and current_seq:
|
| 259 |
-
sequences[current_name] = current_seq
|
| 260 |
-
current_name = line[1:]
|
| 261 |
-
current_seq = ""
|
| 262 |
-
else:
|
| 263 |
-
current_seq += line.upper()
|
| 264 |
-
|
| 265 |
-
if current_name and current_seq:
|
| 266 |
-
sequences[current_name] = current_seq
|
| 267 |
-
|
| 268 |
-
if not sequences:
|
| 269 |
-
return "No valid sequences found in FASTA file."
|
| 270 |
-
|
| 271 |
-
# Process sequences
|
| 272 |
-
results = []
|
| 273 |
-
results.append(f"📁 FASTA FILE ANALYSIS")
|
| 274 |
-
results.append(f"Found {len(sequences)} sequences")
|
| 275 |
-
results.append("=" * 60)
|
| 276 |
-
|
| 277 |
-
for i, (name, seq) in enumerate(sequences.items()):
|
| 278 |
-
if i >= 5:
|
| 279 |
-
results.append(f"\n... and {len(sequences) - 5} more sequences")
|
| 280 |
-
break
|
| 281 |
-
|
| 282 |
-
results.append(f"\n🧬 Sequence: {name}")
|
| 283 |
-
results.append(f"Length: {len(seq)} bp")
|
| 284 |
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
prediction = predict_gene_sequence(clean_seq)
|
| 288 |
-
results.append(prediction)
|
| 289 |
-
else:
|
| 290 |
-
results.append("❌ Sequence too short or invalid")
|
| 291 |
|
| 292 |
-
|
|
|
|
|
|
|
|
|
|
| 293 |
|
| 294 |
-
|
|
|
|
|
|
|
| 295 |
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
return f"FASTA processing failed: {str(e)}"
|
| 299 |
-
|
| 300 |
-
def build_phylogenetic_tree(sequence):
|
| 301 |
-
"""Build phylogenetic tree"""
|
| 302 |
-
try:
|
| 303 |
-
if not sequence or len(sequence.strip()) == 0:
|
| 304 |
-
return "Please provide a DNA sequence."
|
| 305 |
|
| 306 |
-
|
|
|
|
| 307 |
|
| 308 |
-
if
|
| 309 |
-
return
|
| 310 |
|
| 311 |
-
|
| 312 |
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
|
|
|
| 316 |
|
| 317 |
-
#
|
| 318 |
-
|
| 319 |
-
if
|
| 320 |
-
|
| 321 |
-
else:
|
| 322 |
-
result += "❌ MAFFT: Not available\n"
|
| 323 |
-
|
| 324 |
-
if iqtree_available:
|
| 325 |
-
result += f"✅ IQ-TREE: {iqtree_cmd}\n"
|
| 326 |
-
else:
|
| 327 |
-
result += "❌ IQ-TREE: Not available\n"
|
| 328 |
-
|
| 329 |
-
if not mafft_available or not iqtree_available:
|
| 330 |
-
result += "\n⚠️ External tools required for phylogenetic analysis.\n"
|
| 331 |
-
result += "Please install MAFFT and IQ-TREE:\n"
|
| 332 |
-
result += "- Ubuntu/Debian: sudo apt-get install mafft iqtree\n"
|
| 333 |
-
result += "- macOS: brew install mafft iqtree\n"
|
| 334 |
-
result += "- conda: conda install -c bioconda mafft iqtree\n"
|
| 335 |
-
return result
|
| 336 |
-
|
| 337 |
-
# Simple analysis if CSV data is available
|
| 338 |
-
if csv_data is not None:
|
| 339 |
-
result += f"\n📊 Dataset analysis:\n"
|
| 340 |
-
result += f"- Available sequences: {len(csv_data)}\n"
|
| 341 |
-
|
| 342 |
-
# Simple similarity search
|
| 343 |
-
if 'sequence' in csv_data.columns:
|
| 344 |
-
similarities = []
|
| 345 |
-
query_len = len(clean_seq)
|
| 346 |
-
|
| 347 |
-
for idx, row in csv_data.head(100).iterrows(): # Check first 100
|
| 348 |
-
ref_seq = str(row.get('sequence', ''))
|
| 349 |
-
if len(ref_seq) > 10:
|
| 350 |
-
# Simple similarity calculation
|
| 351 |
-
ref_clean = re.sub(r'[^ATCG]', '', ref_seq.upper())
|
| 352 |
-
if len(ref_clean) > 0:
|
| 353 |
-
min_len = min(len(clean_seq), len(ref_clean))
|
| 354 |
-
matches = sum(1 for i in range(min_len) if clean_seq[i] == ref_clean[i])
|
| 355 |
-
similarity = matches / min_len * 100
|
| 356 |
-
if similarity > 70:
|
| 357 |
-
similarities.append((idx, similarity, len(ref_clean)))
|
| 358 |
-
|
| 359 |
-
result += f"- Similar sequences found: {len(similarities)}\n"
|
| 360 |
-
|
| 361 |
-
if similarities:
|
| 362 |
-
similarities.sort(key=lambda x: x[1], reverse=True)
|
| 363 |
-
result += "\nTop matches:\n"
|
| 364 |
-
for i, (idx, sim, length) in enumerate(similarities[:5]):
|
| 365 |
-
result += f" {i+1}. Index {idx}: {sim:.1f}% similarity ({length} bp)\n"
|
| 366 |
|
| 367 |
-
|
| 368 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 369 |
|
| 370 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 371 |
|
| 372 |
except Exception as e:
|
| 373 |
-
|
| 374 |
-
|
|
|
|
| 375 |
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
|
| 395 |
-
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
|
| 402 |
-
if iqtree_available:
|
| 403 |
-
status.append("✅ IQ-TREE: Available")
|
| 404 |
-
else:
|
| 405 |
-
status.append("❌ IQ-TREE: Not Available")
|
| 406 |
-
|
| 407 |
-
return "\n".join(status)
|
| 408 |
|
| 409 |
# --- Gradio Interface ---
|
| 410 |
-
def
|
| 411 |
-
"""Create
|
| 412 |
|
| 413 |
css = """
|
| 414 |
.gradio-container {
|
|
@@ -419,98 +305,178 @@ def create_interface():
|
|
| 419 |
font-size: 12px;
|
| 420 |
line-height: 1.4;
|
| 421 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 422 |
"""
|
| 423 |
|
| 424 |
with gr.Blocks(css=css, title="Gene Analysis Tool") as interface:
|
| 425 |
gr.Markdown("""
|
| 426 |
# 🧬 Gene Analysis Tool
|
| 427 |
-
|
| 428 |
-
Comprehensive gene sequence analysis with machine learning models and phylogenetic analysis.
|
| 429 |
""")
|
| 430 |
|
| 431 |
with gr.Tabs():
|
| 432 |
-
#
|
| 433 |
-
with gr.Tab("🔬
|
|
|
|
|
|
|
| 434 |
with gr.Row():
|
| 435 |
-
with gr.Column(
|
| 436 |
-
gr.
|
| 437 |
-
seq_input = gr.Textbox(
|
| 438 |
label="DNA Sequence",
|
| 439 |
placeholder="Enter DNA sequence (A, T, C, G only)...",
|
| 440 |
lines=4
|
| 441 |
)
|
| 442 |
-
predict_btn = gr.Button("🚀 Analyze Sequence", variant="primary")
|
| 443 |
|
| 444 |
-
gr.
|
| 445 |
-
|
| 446 |
-
|
| 447 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 448 |
)
|
| 449 |
-
process_btn = gr.Button("📊 Process FASTA", variant="primary")
|
| 450 |
|
| 451 |
-
with gr.Column(
|
| 452 |
-
|
| 453 |
-
label="
|
| 454 |
-
lines=
|
| 455 |
elem_classes=["output-text"]
|
| 456 |
)
|
| 457 |
|
| 458 |
-
|
| 459 |
-
|
| 460 |
-
|
| 461 |
-
|
|
|
|
| 462 |
)
|
| 463 |
|
| 464 |
-
|
| 465 |
-
fn=
|
| 466 |
-
inputs=[
|
| 467 |
-
outputs=[
|
| 468 |
)
|
| 469 |
|
| 470 |
-
#
|
| 471 |
-
with gr.Tab("🌳
|
|
|
|
|
|
|
| 472 |
with gr.Row():
|
| 473 |
with gr.Column(scale=1):
|
| 474 |
-
gr.
|
| 475 |
-
|
| 476 |
-
|
| 477 |
-
placeholder="Enter sequence for phylogenetic analysis...",
|
| 478 |
lines=4
|
| 479 |
)
|
| 480 |
-
tree_btn = gr.Button("🌳 Build Tree", variant="primary")
|
| 481 |
|
| 482 |
-
gr.
|
| 483 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 484 |
|
| 485 |
with gr.Column(scale=2):
|
| 486 |
-
|
| 487 |
-
label="
|
| 488 |
-
lines=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 489 |
elem_classes=["output-text"]
|
| 490 |
)
|
| 491 |
|
| 492 |
-
|
| 493 |
-
|
| 494 |
-
|
| 495 |
-
|
|
|
|
| 496 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 497 |
|
| 498 |
-
|
| 499 |
-
|
| 500 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 501 |
)
|
| 502 |
|
| 503 |
-
#
|
| 504 |
-
gr.Markdown("""
|
| 505 |
---
|
| 506 |
-
|
| 507 |
-
|
| 508 |
-
|
| 509 |
-
|
| 510 |
-
- **Models**: Uses trained ML models for gene boundary detection and validation
|
| 511 |
""")
|
| 512 |
-
|
| 513 |
-
|
| 514 |
|
| 515 |
# --- Main Application ---
|
| 516 |
if __name__ == "__main__":
|
|
@@ -519,19 +485,22 @@ if __name__ == "__main__":
|
|
| 519 |
|
| 520 |
# Log startup information
|
| 521 |
logging.info("Starting Gene Analysis Tool")
|
| 522 |
-
logging.info(f"Boundary model
|
| 523 |
-
logging.info(f"Keras model
|
| 524 |
-
logging.info(f"
|
| 525 |
|
| 526 |
# Create and launch interface
|
| 527 |
try:
|
| 528 |
-
interface =
|
|
|
|
|
|
|
| 529 |
interface.launch(
|
| 530 |
share=False,
|
| 531 |
server_name="0.0.0.0",
|
| 532 |
server_port=7860,
|
| 533 |
show_error=True
|
| 534 |
)
|
|
|
|
| 535 |
except Exception as e:
|
| 536 |
logging.error(f"Failed to launch interface: {e}")
|
| 537 |
sys.exit(1)
|
|
|
|
| 8 |
import re
|
| 9 |
import logging
|
| 10 |
import numpy as np
|
| 11 |
+
from predictor import GenePredictor
|
| 12 |
+
from tensorflow.keras.models import load_model
|
| 13 |
+
import ml_simplified_tree
|
| 14 |
import tempfile
|
| 15 |
import shutil
|
| 16 |
import sys
|
| 17 |
from pathlib import Path
|
| 18 |
|
| 19 |
# --- Global Variables ---
|
| 20 |
+
MAFFT_PATH = "mafft/mafftdir/bin/mafft" # Update this path as needed
|
| 21 |
+
IQTREE_PATH = "iqtree/bin/iqtree2" # Update this path as needed
|
| 22 |
|
| 23 |
# --- Logging ---
|
| 24 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
| 25 |
|
| 26 |
+
# --- Paths ---
|
| 27 |
+
from huggingface_hub import hf_hub_download
|
| 28 |
+
|
| 29 |
+
# Model repository and file paths
|
| 30 |
+
model_repo = "GGproject10/best_boundary_aware_model"
|
| 31 |
+
csv_path = "f cleaned.csv"
|
| 32 |
+
|
| 33 |
+
# Get HF token from environment (if available)
|
| 34 |
+
hf_token = os.getenv("HF_TOKEN")
|
| 35 |
+
|
| 36 |
+
# --- Load Models ---
|
| 37 |
boundary_model = None
|
| 38 |
keras_model = None
|
| 39 |
kmer_to_index = None
|
|
|
|
| 40 |
|
| 41 |
+
# Try to load boundary model from Hugging Face Hub
|
| 42 |
+
try:
|
| 43 |
+
boundary_path = hf_hub_download(
|
| 44 |
+
repo_id=model_repo,
|
| 45 |
+
filename="best_boundary_aware_model.pth",
|
| 46 |
+
token=hf_token
|
| 47 |
+
)
|
| 48 |
+
if os.path.exists(boundary_path):
|
| 49 |
+
boundary_model = GenePredictor(boundary_path)
|
| 50 |
+
logging.info("Boundary model loaded successfully from Hugging Face Hub.")
|
| 51 |
+
else:
|
| 52 |
+
logging.warning(f"Boundary model file not found after download")
|
| 53 |
+
except Exception as e:
|
| 54 |
+
logging.error(f"Failed to load boundary model from HF Hub: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
+
# Try to load Keras model from Hugging Face Hub
|
| 57 |
try:
|
| 58 |
+
keras_path = hf_hub_download(
|
| 59 |
+
repo_id=model_repo,
|
| 60 |
+
filename="best_model.keras",
|
| 61 |
+
token=hf_token
|
| 62 |
+
)
|
| 63 |
+
kmer_path = hf_hub_download(
|
| 64 |
+
repo_id=model_repo,
|
| 65 |
+
filename="kmer_to_index.pkl",
|
| 66 |
+
token=hf_token
|
| 67 |
+
)
|
| 68 |
|
| 69 |
+
if os.path.exists(keras_path) and os.path.exists(kmer_path):
|
| 70 |
+
keras_model = load_model(keras_path)
|
| 71 |
+
with open(kmer_path, "rb") as f:
|
| 72 |
+
kmer_to_index = pickle.load(f)
|
| 73 |
+
logging.info("Keras model and k-mer index loaded successfully from Hugging Face Hub.")
|
| 74 |
+
else:
|
| 75 |
+
logging.warning(f"Keras model or kmer files not found after download")
|
| 76 |
+
except Exception as e:
|
| 77 |
+
logging.error(f"Failed to load Keras model from HF Hub: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
+
# --- Initialize Tree Analyzer ---
|
| 80 |
+
analyzer = None
|
| 81 |
try:
|
| 82 |
+
analyzer = ml_simplified_tree.PhylogeneticTreeAnalyzer()
|
| 83 |
if os.path.exists(csv_path):
|
| 84 |
+
if analyzer.load_data(csv_path):
|
| 85 |
+
logging.info("Tree analyzer initialized successfully")
|
| 86 |
+
# Try to train AI model (optional)
|
| 87 |
+
try:
|
| 88 |
+
if not analyzer.train_ai_model():
|
| 89 |
+
logging.warning("AI model training failed; proceeding with basic analysis.")
|
| 90 |
+
except Exception as e:
|
| 91 |
+
logging.warning(f"AI model training failed: {e}")
|
| 92 |
+
else:
|
| 93 |
+
logging.error("Failed to load CSV data for tree analyzer")
|
| 94 |
+
analyzer = None
|
| 95 |
else:
|
| 96 |
+
logging.error(f"CSV file not found: {csv_path}")
|
| 97 |
+
analyzer = None
|
| 98 |
except Exception as e:
|
| 99 |
+
logging.error(f"Failed to initialize tree analyzer: {e}")
|
| 100 |
+
analyzer = None
|
| 101 |
|
| 102 |
+
# --- Individual Model Functions (Separated) ---
|
| 103 |
+
def predict_with_boundary_only(sequence):
|
| 104 |
+
"""Boundary model prediction only"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
try:
|
| 106 |
+
if not boundary_model:
|
| 107 |
+
return "❌ Boundary model not available", None
|
| 108 |
|
| 109 |
# Clean sequence
|
| 110 |
sequence = re.sub(r'[^ATCG]', '', sequence.upper())
|
|
|
|
| 111 |
if len(sequence) < 10:
|
| 112 |
+
return "❌ Sequence too short (minimum 10 bp required)", None
|
| 113 |
+
|
| 114 |
+
# Get predictions from boundary model
|
| 115 |
+
predictions, probabilities, confidence = boundary_model.predict(sequence)
|
| 116 |
+
|
| 117 |
+
# Extract gene regions
|
| 118 |
+
regions = boundary_model.extract_gene_regions(predictions, sequence)
|
| 119 |
+
|
| 120 |
+
result = f"🎯 BOUNDARY DETECTION MODEL RESULTS:\n"
|
| 121 |
+
result += f"Input Length: {len(sequence)} bp\n"
|
| 122 |
+
result += f"Overall Confidence: {confidence:.4f}\n"
|
| 123 |
+
result += f"Regions Detected: {len(regions) if regions else 0}\n"
|
| 124 |
+
|
| 125 |
+
extracted_sequence = None
|
| 126 |
+
if regions:
|
| 127 |
+
for i, region in enumerate(regions[:3]): # Show first 3 regions
|
| 128 |
+
result += f"\n--- Region {i+1} ---\n"
|
| 129 |
+
result += f"Start: {region['start']}\n"
|
| 130 |
+
result += f"End: {region['end']}\n"
|
| 131 |
+
result += f"Length: {len(region['sequence'])} bp\n"
|
| 132 |
+
result += f"Confidence: {region.get('confidence', 'N/A'):.4f}\n"
|
| 133 |
+
result += f"Sequence: {region['sequence'][:50]}{'...' if len(region['sequence']) > 50 else ''}\n"
|
| 134 |
+
|
| 135 |
+
# Use the first region as extracted sequence
|
| 136 |
+
if i == 0:
|
| 137 |
+
extracted_sequence = region['sequence']
|
| 138 |
|
| 139 |
+
return result, extracted_sequence
|
|
|
|
|
|
|
|
|
|
| 140 |
|
| 141 |
+
except Exception as e:
|
| 142 |
+
logging.error(f"Boundary prediction error: {e}")
|
| 143 |
+
return f"❌ Boundary prediction failed: {str(e)}", None
|
| 144 |
+
|
| 145 |
+
def predict_with_keras_only(sequence):
|
| 146 |
+
"""Keras model prediction only"""
|
| 147 |
+
try:
|
| 148 |
+
if not keras_model or not kmer_to_index:
|
| 149 |
+
return "❌ Keras validation model not available"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
|
| 151 |
+
# Clean sequence
|
| 152 |
+
sequence = re.sub(r'[^ATCG]', '', sequence.upper())
|
| 153 |
+
if len(sequence) < 6:
|
| 154 |
+
return "❌ Sequence too short for k-mer analysis (minimum 6 bp required)"
|
| 155 |
+
|
| 156 |
+
# Generate k-mers
|
| 157 |
+
kmers = [sequence[i:i+6] for i in range(len(sequence)-5)]
|
| 158 |
+
indices = [kmer_to_index.get(kmer, 0) for kmer in kmers]
|
| 159 |
+
|
| 160 |
+
# Prepare input
|
| 161 |
+
input_arr = np.array([indices])
|
| 162 |
+
prediction = keras_model.predict(input_arr, verbose=0)[0]
|
| 163 |
+
|
| 164 |
+
# Format prediction as probabilities/scores
|
| 165 |
+
mean_score = np.mean(prediction)
|
| 166 |
+
max_score = np.max(prediction)
|
| 167 |
+
min_score = np.min(prediction)
|
| 168 |
+
|
| 169 |
+
result = f"🔍 KERAS VALIDATION MODEL RESULTS:\n"
|
| 170 |
+
result += f"Input Length: {len(sequence)} bp\n"
|
| 171 |
+
result += f"K-mers Generated: {len(kmers)}\n"
|
| 172 |
+
result += f"Mean Score: {mean_score:.4f}\n"
|
| 173 |
+
result += f"Max Score: {max_score:.4f}\n"
|
| 174 |
+
result += f"Min Score: {min_score:.4f}\n"
|
| 175 |
+
|
| 176 |
+
# Interpretation
|
| 177 |
+
if mean_score > 0.7:
|
| 178 |
+
result += f"Interpretation: ✅ High confidence gene sequence\n"
|
| 179 |
+
elif mean_score > 0.4:
|
| 180 |
+
result += f"Interpretation: ⚠️ Moderate confidence\n"
|
| 181 |
else:
|
| 182 |
+
result += f"Interpretation: ❌ Low confidence gene sequence\n"
|
| 183 |
|
| 184 |
+
return result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 185 |
|
| 186 |
except Exception as e:
|
| 187 |
+
logging.error(f"Keras prediction error: {e}")
|
| 188 |
+
return f"❌ Keras prediction failed: {str(e)}"
|
| 189 |
|
| 190 |
+
def analyze_sequence_for_tree_display(sequence, matching_percentage=80):
|
| 191 |
+
"""
|
| 192 |
+
Create phylogenetic tree and return both HTML content and file path
|
| 193 |
+
"""
|
| 194 |
try:
|
| 195 |
+
if not analyzer:
|
| 196 |
+
return None, None, "❌ Tree analyzer not initialized"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 197 |
|
| 198 |
+
if not sequence:
|
| 199 |
+
return None, None, "❌ Please provide a sequence"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 200 |
|
| 201 |
+
# Clean sequence
|
| 202 |
+
sequence = re.sub(r'[^ATCG]', '', sequence.upper())
|
| 203 |
+
if len(sequence) < 20:
|
| 204 |
+
return None, None, "❌ Sequence too short for tree analysis (minimum 20 bp)"
|
| 205 |
|
| 206 |
+
# Find query sequence
|
| 207 |
+
if not analyzer.find_query_sequence(sequence):
|
| 208 |
+
return None, None, "❌ Sequence not found in reference dataset"
|
| 209 |
|
| 210 |
+
# Set matching percentage
|
| 211 |
+
analyzer.matching_percentage = matching_percentage
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 212 |
|
| 213 |
+
# Find similar sequences
|
| 214 |
+
matched_ids, actual_percentage = analyzer.find_similar_sequences(matching_percentage)
|
| 215 |
|
| 216 |
+
if not matched_ids:
|
| 217 |
+
return None, None, f"❌ No similar sequences found at {matching_percentage}% similarity"
|
| 218 |
|
| 219 |
+
logging.info(f"Found {len(matched_ids)} similar sequences at {actual_percentage:.1f}% similarity")
|
| 220 |
|
| 221 |
+
# Build tree structure
|
| 222 |
+
tree_structure = analyzer.build_tree_structure(matched_ids)
|
| 223 |
+
if not tree_structure:
|
| 224 |
+
return None, None, "❌ Failed to build tree structure"
|
| 225 |
|
| 226 |
+
# Create interactive tree
|
| 227 |
+
fig = analyzer.create_interactive_tree(matched_ids, actual_percentage)
|
| 228 |
+
if not fig:
|
| 229 |
+
return None, None, "❌ Failed to create tree visualization"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 230 |
|
| 231 |
+
# Generate HTML content with custom height
|
| 232 |
+
html_content = fig.to_html(
|
| 233 |
+
full_html=True,
|
| 234 |
+
include_plotlyjs='cdn',
|
| 235 |
+
config={'displayModeBar': True, 'responsive': True}
|
| 236 |
+
)
|
| 237 |
|
| 238 |
+
# Modify HTML to set a specific height
|
| 239 |
+
html_content = html_content.replace(
|
| 240 |
+
'<div id="',
|
| 241 |
+
'<div style="height: 600px; width: 100%;" id="'
|
| 242 |
+
)
|
| 243 |
+
|
| 244 |
+
# Save to output folder
|
| 245 |
+
output_dir = "output"
|
| 246 |
+
os.makedirs(output_dir, exist_ok=True)
|
| 247 |
+
|
| 248 |
+
# Create a safe filename
|
| 249 |
+
safe_seq_name = re.sub(r'[^a-zA-Z0-9]', '_', sequence[:20])
|
| 250 |
+
html_filename = os.path.join(output_dir, f"tree_{safe_seq_name}_{matching_percentage}.html")
|
| 251 |
+
|
| 252 |
+
with open(html_filename, "w", encoding='utf-8') as f:
|
| 253 |
+
f.write(html_content)
|
| 254 |
+
|
| 255 |
+
logging.info(f"Tree HTML saved to {html_filename}")
|
| 256 |
+
|
| 257 |
+
success_msg = f"✅ Phylogenetic tree generated!\n"
|
| 258 |
+
success_msg += f"Sequences analyzed: {len(matched_ids)}\n"
|
| 259 |
+
success_msg += f"Similarity threshold: {actual_percentage:.1f}%"
|
| 260 |
+
|
| 261 |
+
return html_content, html_filename, success_msg
|
| 262 |
|
| 263 |
except Exception as e:
|
| 264 |
+
error_msg = f"❌ Tree analysis error: {str(e)}"
|
| 265 |
+
logging.error(error_msg)
|
| 266 |
+
return None, None, error_msg
|
| 267 |
|
| 268 |
+
# --- Combined Analysis Function ---
|
| 269 |
+
def analyze_with_boundary_and_tree(sequence, similarity_threshold=80):
|
| 270 |
+
"""
|
| 271 |
+
Analyze sequence with boundary model and create tree from extracted sequence
|
| 272 |
+
"""
|
| 273 |
+
try:
|
| 274 |
+
# Step 1: Boundary analysis
|
| 275 |
+
boundary_result, extracted_seq = predict_with_boundary_only(sequence)
|
| 276 |
+
|
| 277 |
+
if not extracted_seq:
|
| 278 |
+
return boundary_result, None, None, "❌ No sequence extracted from boundary model"
|
| 279 |
+
|
| 280 |
+
# Step 2: Tree analysis using extracted sequence
|
| 281 |
+
html_content, html_file, tree_msg = analyze_sequence_for_tree_display(
|
| 282 |
+
extracted_seq, similarity_threshold
|
| 283 |
+
)
|
| 284 |
+
|
| 285 |
+
# Combine results
|
| 286 |
+
combined_result = boundary_result + "\n\n🌳 PHYLOGENETIC TREE ANALYSIS:\n" + tree_msg
|
| 287 |
+
|
| 288 |
+
return combined_result, html_content, html_file, tree_msg
|
| 289 |
+
|
| 290 |
+
except Exception as e:
|
| 291 |
+
error_msg = f"❌ Combined analysis failed: {str(e)}"
|
| 292 |
+
logging.error(error_msg)
|
| 293 |
+
return error_msg, None, None, error_msg
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 294 |
|
| 295 |
# --- Gradio Interface ---
|
| 296 |
+
def create_gradio_interface():
|
| 297 |
+
"""Create simplified Gradio interface"""
|
| 298 |
|
| 299 |
css = """
|
| 300 |
.gradio-container {
|
|
|
|
| 305 |
font-size: 12px;
|
| 306 |
line-height: 1.4;
|
| 307 |
}
|
| 308 |
+
.custom-html {
|
| 309 |
+
height: 600px !important;
|
| 310 |
+
overflow: auto;
|
| 311 |
+
}
|
| 312 |
"""
|
| 313 |
|
| 314 |
with gr.Blocks(css=css, title="Gene Analysis Tool") as interface:
|
| 315 |
gr.Markdown("""
|
| 316 |
# 🧬 Gene Analysis Tool
|
| 317 |
+
Analyze gene sequences with boundary detection, validation, and phylogenetic trees.
|
|
|
|
| 318 |
""")
|
| 319 |
|
| 320 |
with gr.Tabs():
|
| 321 |
+
# Tab 1: Individual Model Analysis
|
| 322 |
+
with gr.Tab("🔬 Individual Models"):
|
| 323 |
+
gr.Markdown("### Analyze with individual models separately")
|
| 324 |
+
|
| 325 |
with gr.Row():
|
| 326 |
+
with gr.Column():
|
| 327 |
+
individual_seq_input = gr.Textbox(
|
|
|
|
| 328 |
label="DNA Sequence",
|
| 329 |
placeholder="Enter DNA sequence (A, T, C, G only)...",
|
| 330 |
lines=4
|
| 331 |
)
|
|
|
|
| 332 |
|
| 333 |
+
with gr.Row():
|
| 334 |
+
boundary_btn = gr.Button("🎯 Boundary Detection", variant="primary")
|
| 335 |
+
keras_btn = gr.Button("🔍 Keras Validation", variant="secondary")
|
| 336 |
+
|
| 337 |
+
with gr.Row():
|
| 338 |
+
with gr.Column():
|
| 339 |
+
boundary_output = gr.Textbox(
|
| 340 |
+
label="Boundary Model Results",
|
| 341 |
+
lines=12,
|
| 342 |
+
elem_classes=["output-text"]
|
| 343 |
)
|
|
|
|
| 344 |
|
| 345 |
+
with gr.Column():
|
| 346 |
+
keras_output = gr.Textbox(
|
| 347 |
+
label="Keras Model Results",
|
| 348 |
+
lines=12,
|
| 349 |
elem_classes=["output-text"]
|
| 350 |
)
|
| 351 |
|
| 352 |
+
# Button actions
|
| 353 |
+
boundary_btn.click(
|
| 354 |
+
fn=lambda seq: predict_with_boundary_only(seq)[0],
|
| 355 |
+
inputs=[individual_seq_input],
|
| 356 |
+
outputs=[boundary_output]
|
| 357 |
)
|
| 358 |
|
| 359 |
+
keras_btn.click(
|
| 360 |
+
fn=predict_with_keras_only,
|
| 361 |
+
inputs=[individual_seq_input],
|
| 362 |
+
outputs=[keras_output]
|
| 363 |
)
|
| 364 |
|
| 365 |
+
# Tab 2: Integrated Analysis
|
| 366 |
+
with gr.Tab("🌳 Boundary + Tree Analysis"):
|
| 367 |
+
gr.Markdown("### Boundary detection with automatic phylogenetic tree generation")
|
| 368 |
+
|
| 369 |
with gr.Row():
|
| 370 |
with gr.Column(scale=1):
|
| 371 |
+
integrated_seq_input = gr.Textbox(
|
| 372 |
+
label="DNA Sequence",
|
| 373 |
+
placeholder="Enter DNA sequence for boundary detection and tree analysis...",
|
|
|
|
| 374 |
lines=4
|
| 375 |
)
|
|
|
|
| 376 |
|
| 377 |
+
similarity_slider = gr.Slider(
|
| 378 |
+
minimum=1,
|
| 379 |
+
maximum=99,
|
| 380 |
+
value=80,
|
| 381 |
+
step=1,
|
| 382 |
+
label="Tree Similarity Threshold (%)"
|
| 383 |
+
)
|
| 384 |
+
|
| 385 |
+
integrated_btn = gr.Button("🚀 Analyze & Build Tree", variant="primary")
|
| 386 |
|
| 387 |
with gr.Column(scale=2):
|
| 388 |
+
integrated_output = gr.Textbox(
|
| 389 |
+
label="Analysis Results",
|
| 390 |
+
lines=15,
|
| 391 |
+
elem_classes=["output-text"]
|
| 392 |
+
)
|
| 393 |
+
|
| 394 |
+
# Tree display section
|
| 395 |
+
gr.Markdown("### 🌳 Interactive Phylogenetic Tree")
|
| 396 |
+
|
| 397 |
+
with gr.Row():
|
| 398 |
+
with gr.Column():
|
| 399 |
+
tree_html = gr.HTML(
|
| 400 |
+
label="Phylogenetic Tree",
|
| 401 |
+
elem_classes=["custom-html"]
|
| 402 |
+
)
|
| 403 |
+
|
| 404 |
+
with gr.Column(scale=0.3):
|
| 405 |
+
tree_download = gr.File(
|
| 406 |
+
label="Download Tree (HTML)",
|
| 407 |
+
file_count="single"
|
| 408 |
+
)
|
| 409 |
+
|
| 410 |
+
tree_status = gr.Textbox(
|
| 411 |
+
label="Tree Status",
|
| 412 |
+
lines=3,
|
| 413 |
elem_classes=["output-text"]
|
| 414 |
)
|
| 415 |
|
| 416 |
+
# Integrated analysis action
|
| 417 |
+
integrated_btn.click(
|
| 418 |
+
fn=analyze_with_boundary_and_tree,
|
| 419 |
+
inputs=[integrated_seq_input, similarity_slider],
|
| 420 |
+
outputs=[integrated_output, tree_html, tree_download, tree_status]
|
| 421 |
)
|
| 422 |
+
|
| 423 |
+
# Tab 3: Quick Tree Analysis
|
| 424 |
+
with gr.Tab("🌲 Direct Tree Analysis"):
|
| 425 |
+
gr.Markdown("### Create phylogenetic tree from any sequence")
|
| 426 |
|
| 427 |
+
with gr.Row():
|
| 428 |
+
with gr.Column():
|
| 429 |
+
direct_seq_input = gr.Textbox(
|
| 430 |
+
label="DNA Sequence",
|
| 431 |
+
placeholder="Enter sequence for direct tree analysis...",
|
| 432 |
+
lines=4
|
| 433 |
+
)
|
| 434 |
+
|
| 435 |
+
direct_similarity_slider = gr.Slider(
|
| 436 |
+
minimum=1,
|
| 437 |
+
maximum=99,
|
| 438 |
+
value=80,
|
| 439 |
+
step=1,
|
| 440 |
+
label="Similarity Threshold (%)"
|
| 441 |
+
)
|
| 442 |
+
|
| 443 |
+
direct_tree_btn = gr.Button("🌳 Generate Tree", variant="primary")
|
| 444 |
+
|
| 445 |
+
with gr.Row():
|
| 446 |
+
with gr.Column():
|
| 447 |
+
direct_tree_html = gr.HTML(
|
| 448 |
+
label="Phylogenetic Tree",
|
| 449 |
+
elem_classes=["custom-html"]
|
| 450 |
+
)
|
| 451 |
+
|
| 452 |
+
with gr.Column(scale=0.3):
|
| 453 |
+
direct_tree_download = gr.File(
|
| 454 |
+
label="Download Tree (HTML)"
|
| 455 |
+
)
|
| 456 |
+
|
| 457 |
+
direct_tree_status = gr.Textbox(
|
| 458 |
+
label="Tree Status",
|
| 459 |
+
lines=4,
|
| 460 |
+
elem_classes=["output-text"]
|
| 461 |
+
)
|
| 462 |
+
|
| 463 |
+
# Direct tree analysis action
|
| 464 |
+
direct_tree_btn.click(
|
| 465 |
+
fn=analyze_sequence_for_tree_display,
|
| 466 |
+
inputs=[direct_seq_input, direct_similarity_slider],
|
| 467 |
+
outputs=[direct_tree_html, direct_tree_download, direct_tree_status]
|
| 468 |
)
|
| 469 |
|
| 470 |
+
# Footer with model status
|
| 471 |
+
gr.Markdown(f"""
|
| 472 |
---
|
| 473 |
+
**Model Status:**
|
| 474 |
+
Boundary Model: {"✅" if boundary_model else "❌"} |
|
| 475 |
+
Keras Model: {"✅" if keras_model else "❌"} |
|
| 476 |
+
Tree Analyzer: {"✅" if analyzer else "❌"}
|
|
|
|
| 477 |
""")
|
| 478 |
+
|
| 479 |
+
return interface
|
| 480 |
|
| 481 |
# --- Main Application ---
|
| 482 |
if __name__ == "__main__":
|
|
|
|
| 485 |
|
| 486 |
# Log startup information
|
| 487 |
logging.info("Starting Gene Analysis Tool")
|
| 488 |
+
logging.info(f"Boundary model loaded: {boundary_model is not None}")
|
| 489 |
+
logging.info(f"Keras model loaded: {keras_model is not None}")
|
| 490 |
+
logging.info(f"Tree analyzer initialized: {analyzer is not None}")
|
| 491 |
|
| 492 |
# Create and launch interface
|
| 493 |
try:
|
| 494 |
+
interface = create_gradio_interface()
|
| 495 |
+
|
| 496 |
+
# Launch with appropriate settings
|
| 497 |
interface.launch(
|
| 498 |
share=False,
|
| 499 |
server_name="0.0.0.0",
|
| 500 |
server_port=7860,
|
| 501 |
show_error=True
|
| 502 |
)
|
| 503 |
+
|
| 504 |
except Exception as e:
|
| 505 |
logging.error(f"Failed to launch interface: {e}")
|
| 506 |
sys.exit(1)
|