Spaces:
No application file
No application file
Update app.py
Browse files
app.py
CHANGED
|
@@ -15,23 +15,11 @@ import tempfile
|
|
| 15 |
import shutil
|
| 16 |
import sys
|
| 17 |
from pathlib import Path
|
| 18 |
-
import uuid
|
| 19 |
-
from Bio import SeqIO
|
| 20 |
-
from Bio.Seq import Seq
|
| 21 |
-
from Bio.SeqRecord import SeqRecord
|
| 22 |
|
| 23 |
# --- Global Variables ---
|
| 24 |
MAFFT_PATH = "mafft/mafftdir/bin/mafft" # Update this path as needed
|
| 25 |
IQTREE_PATH = "iqtree/bin/iqtree2" # Update this path as needed
|
| 26 |
|
| 27 |
-
# Reference files for phylogenetic placement
|
| 28 |
-
REFERENCE_ALIGNMENT_PATH = "f_gene_sequences_aligned.fasta"
|
| 29 |
-
REFERENCE_TREE_PATH = "f_gene_sequences.phy.treefile"
|
| 30 |
-
QUERY_OUTPUT_DIR = "queries"
|
| 31 |
-
|
| 32 |
-
# Create output directory
|
| 33 |
-
os.makedirs(QUERY_OUTPUT_DIR, exist_ok=True)
|
| 34 |
-
|
| 35 |
# --- Logging ---
|
| 36 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
| 37 |
|
|
@@ -126,7 +114,6 @@ def check_tool_availability():
|
|
| 126 |
'/usr/bin/mafft',
|
| 127 |
'/usr/local/bin/mafft',
|
| 128 |
'mafft.bat', # Windows
|
| 129 |
-
os.path.join("binaries", "mafft", "mafft.bat"), # Local binaries
|
| 130 |
]
|
| 131 |
|
| 132 |
for candidate in mafft_candidates:
|
|
@@ -145,16 +132,12 @@ def check_tool_availability():
|
|
| 145 |
IQTREE_PATH,
|
| 146 |
'iqtree2',
|
| 147 |
'iqtree',
|
| 148 |
-
'
|
| 149 |
-
'/usr/bin/
|
| 150 |
-
'/usr/
|
| 151 |
-
'/usr/bin/iqtree3_intel',
|
| 152 |
'/usr/local/bin/iqtree',
|
| 153 |
'iqtree2.exe', # Windows
|
| 154 |
'iqtree.exe', # Windows
|
| 155 |
-
'iqtree3.exe', # Windows
|
| 156 |
-
os.path.join("binaries", "iqtree", "bin", "iqtree2"), # Local binaries
|
| 157 |
-
os.path.join("binaries", "iqtree", "bin", "iqtree3"), # Local binaries
|
| 158 |
]
|
| 159 |
|
| 160 |
for candidate in iqtree_candidates:
|
|
@@ -166,142 +149,6 @@ def check_tool_availability():
|
|
| 166 |
|
| 167 |
return mafft_available, iqtree_available, mafft_cmd, iqtree_cmd
|
| 168 |
|
| 169 |
-
# --- Phylogenetic Placement Functions ---
|
| 170 |
-
def place_sequence_in_tree(sequence: str):
|
| 171 |
-
"""
|
| 172 |
-
Place a new sequence into the existing reference tree using MAFFT + IQ-TREE
|
| 173 |
-
"""
|
| 174 |
-
try:
|
| 175 |
-
# Input validation
|
| 176 |
-
if not sequence or len(sequence.strip()) < 100:
|
| 177 |
-
return None, None, "Error: Sequence is too short for phylogenetic placement (minimum 100 bp required)."
|
| 178 |
-
|
| 179 |
-
# Clean sequence
|
| 180 |
-
sequence = sequence.upper().strip()
|
| 181 |
-
sequence = ''.join(c if c in 'ACTGN' else 'N' for c in sequence)
|
| 182 |
-
|
| 183 |
-
# Check if reference files exist
|
| 184 |
-
if not os.path.exists(REFERENCE_ALIGNMENT_PATH):
|
| 185 |
-
return None, None, f"Error: Reference alignment file not found: {REFERENCE_ALIGNMENT_PATH}"
|
| 186 |
-
|
| 187 |
-
if not os.path.exists(REFERENCE_TREE_PATH):
|
| 188 |
-
return None, None, f"Error: Reference tree file not found: {REFERENCE_TREE_PATH}"
|
| 189 |
-
|
| 190 |
-
# Check tool availability
|
| 191 |
-
mafft_available, iqtree_available, mafft_cmd, iqtree_cmd = check_tool_availability()
|
| 192 |
-
|
| 193 |
-
if not mafft_available:
|
| 194 |
-
return None, None, "Error: MAFFT not found. Please install MAFFT for phylogenetic placement."
|
| 195 |
-
|
| 196 |
-
if not iqtree_available:
|
| 197 |
-
return None, None, "Error: IQ-TREE not found. Please install IQ-TREE for phylogenetic placement."
|
| 198 |
-
|
| 199 |
-
# Generate unique ID for this query
|
| 200 |
-
query_id = f"QUERY_{uuid.uuid4().hex[:8]}"
|
| 201 |
-
query_fasta = os.path.join(QUERY_OUTPUT_DIR, f"{query_id}.fa")
|
| 202 |
-
aligned_with_query = os.path.join(QUERY_OUTPUT_DIR, f"{query_id}_aligned.fa")
|
| 203 |
-
output_prefix = os.path.join(QUERY_OUTPUT_DIR, f"{query_id}_placed_tree")
|
| 204 |
-
|
| 205 |
-
# Save query sequence to FASTA file
|
| 206 |
-
try:
|
| 207 |
-
query_record = SeqRecord(Seq(sequence), id=query_id, description="Query sequence for phylogenetic placement")
|
| 208 |
-
SeqIO.write([query_record], query_fasta, "fasta")
|
| 209 |
-
logging.info(f"Query sequence saved: {query_fasta}")
|
| 210 |
-
except Exception as e:
|
| 211 |
-
return None, None, f"Error: Failed to save query sequence: {str(e)}"
|
| 212 |
-
|
| 213 |
-
# Step 1: Add query sequence to reference alignment using MAFFT
|
| 214 |
-
logging.info("Adding query sequence to reference alignment...")
|
| 215 |
-
try:
|
| 216 |
-
with open(aligned_with_query, "w") as output_file:
|
| 217 |
-
mafft_cmd_list = [
|
| 218 |
-
mafft_cmd,
|
| 219 |
-
"--add", query_fasta,
|
| 220 |
-
"--reorder",
|
| 221 |
-
REFERENCE_ALIGNMENT_PATH
|
| 222 |
-
]
|
| 223 |
-
|
| 224 |
-
result = subprocess.run(
|
| 225 |
-
mafft_cmd_list,
|
| 226 |
-
stdout=output_file,
|
| 227 |
-
stderr=subprocess.PIPE,
|
| 228 |
-
text=True,
|
| 229 |
-
timeout=300, # 5 minute timeout
|
| 230 |
-
check=True
|
| 231 |
-
)
|
| 232 |
-
|
| 233 |
-
logging.info(f"MAFFT alignment completed: {aligned_with_query}")
|
| 234 |
-
|
| 235 |
-
except subprocess.TimeoutExpired:
|
| 236 |
-
return None, None, "Error: MAFFT alignment timed out (>5 minutes)."
|
| 237 |
-
except subprocess.CalledProcessError as e:
|
| 238 |
-
error_msg = e.stderr.decode() if e.stderr else "Unknown MAFFT error"
|
| 239 |
-
logging.error(f"MAFFT failed: {error_msg}")
|
| 240 |
-
return None, None, f"Error: MAFFT alignment failed: {error_msg}"
|
| 241 |
-
except Exception as e:
|
| 242 |
-
return None, None, f"Error: MAFFT execution failed: {str(e)}"
|
| 243 |
-
|
| 244 |
-
# Verify alignment file was created
|
| 245 |
-
if not os.path.exists(aligned_with_query) or os.path.getsize(aligned_with_query) == 0:
|
| 246 |
-
return None, None, "Error: MAFFT alignment file was not created or is empty."
|
| 247 |
-
|
| 248 |
-
# Step 2: Place sequence in tree using IQ-TREE with constraint tree
|
| 249 |
-
logging.info("Placing sequence in phylogenetic tree...")
|
| 250 |
-
try:
|
| 251 |
-
iqtree_cmd_list = [
|
| 252 |
-
iqtree_cmd,
|
| 253 |
-
"-s", aligned_with_query,
|
| 254 |
-
"-g", REFERENCE_TREE_PATH, # Constraint tree
|
| 255 |
-
"-m", "GTR+G", # Substitution model
|
| 256 |
-
"-pre", output_prefix, # Output prefix
|
| 257 |
-
"-redo", # Overwrite existing files
|
| 258 |
-
"--quiet" # Reduce verbosity
|
| 259 |
-
]
|
| 260 |
-
|
| 261 |
-
result = subprocess.run(
|
| 262 |
-
iqtree_cmd_list,
|
| 263 |
-
capture_output=True,
|
| 264 |
-
text=True,
|
| 265 |
-
timeout=600, # 10 minute timeout
|
| 266 |
-
check=True
|
| 267 |
-
)
|
| 268 |
-
|
| 269 |
-
logging.info("IQ-TREE placement completed")
|
| 270 |
-
|
| 271 |
-
except subprocess.TimeoutExpired:
|
| 272 |
-
return None, None, "Error: IQ-TREE placement timed out (>10 minutes)."
|
| 273 |
-
except subprocess.CalledProcessError as e:
|
| 274 |
-
error_msg = e.stderr if e.stderr else "Unknown IQ-TREE error"
|
| 275 |
-
logging.error(f"IQ-TREE failed: {error_msg}")
|
| 276 |
-
return None, None, f"Error: IQ-TREE placement failed: {error_msg}"
|
| 277 |
-
except Exception as e:
|
| 278 |
-
return None, None, f"Error: IQ-TREE execution failed: {str(e)}"
|
| 279 |
-
|
| 280 |
-
# Check if tree file was generated
|
| 281 |
-
treefile = f"{output_prefix}.treefile"
|
| 282 |
-
if not os.path.exists(treefile):
|
| 283 |
-
return None, None, "Error: Tree file was not generated by IQ-TREE."
|
| 284 |
-
|
| 285 |
-
if os.path.getsize(treefile) == 0:
|
| 286 |
-
return None, None, "Error: Generated tree file is empty."
|
| 287 |
-
|
| 288 |
-
# Success! Return the tree file and alignment
|
| 289 |
-
success_msg = f"✅ Success: Phylogenetic placement completed!\n"
|
| 290 |
-
success_msg += f"- Query ID: {query_id}\n"
|
| 291 |
-
success_msg += f"- Sequence length: {len(sequence)} bp\n"
|
| 292 |
-
success_msg += f"- Placed in tree with constraint from reference\n"
|
| 293 |
-
success_msg += f"- Tree file: {os.path.basename(treefile)}\n"
|
| 294 |
-
success_msg += f"- Alignment file: {os.path.basename(aligned_with_query)}"
|
| 295 |
-
|
| 296 |
-
logging.info(f"Phylogenetic placement successful: {treefile}")
|
| 297 |
-
|
| 298 |
-
return treefile, aligned_with_query, success_msg
|
| 299 |
-
|
| 300 |
-
except Exception as e:
|
| 301 |
-
error_msg = f"Phylogenetic placement failed: {str(e)}"
|
| 302 |
-
logging.error(error_msg)
|
| 303 |
-
return None, None, error_msg
|
| 304 |
-
|
| 305 |
def install_dependencies_guide():
|
| 306 |
"""Provide installation guidance for missing dependencies"""
|
| 307 |
guide = """
|
|
@@ -476,26 +323,27 @@ def create_multi_fasta_with_query(query_sequence, query_id="Query_F_Gene"):
|
|
| 476 |
for idx, row in analyzer.data.iterrows():
|
| 477 |
if 'sequence' in row and len(str(row['sequence'])) > 50:
|
| 478 |
seq_id = row.get('id', f"Ref_{count}")
|
| 479 |
-
sequence = str(row['sequence']).
|
| 480 |
temp_fasta.write(f">{seq_id}\n{sequence}\n")
|
| 481 |
count += 1
|
| 482 |
if count >= 20: # Limit to prevent too large datasets
|
| 483 |
break
|
| 484 |
-
logging.info(f"Added {count} sequences from
|
|
|
|
| 485 |
temp_fasta.close()
|
| 486 |
return temp_fasta.name
|
| 487 |
-
|
| 488 |
except Exception as e:
|
| 489 |
-
logging.error(f"
|
| 490 |
return None
|
| 491 |
|
| 492 |
-
def build_maximum_likelihood_tree(
|
| 493 |
-
"""Build
|
| 494 |
try:
|
| 495 |
-
# Check tool availability
|
| 496 |
mafft_available, iqtree_available, mafft_cmd, iqtree_cmd = check_tool_availability()
|
| 497 |
|
| 498 |
-
#
|
| 499 |
status_msg = "🔍 Checking dependencies...\n"
|
| 500 |
|
| 501 |
if not mafft_available:
|
|
@@ -508,20 +356,20 @@ def build_maximum_likelihood_tree(f_sequence):
|
|
| 508 |
else:
|
| 509 |
status_msg += f"✅ IQ-TREE found: {iqtree_cmd}\n"
|
| 510 |
|
| 511 |
-
# If
|
| 512 |
if not mafft_available and not iqtree_available:
|
| 513 |
guide = install_dependencies_guide()
|
| 514 |
return False, f"{status_msg}\n{guide}", None, None
|
| 515 |
|
| 516 |
-
# If only one tool is missing, provide guidance
|
| 517 |
if not mafft_available:
|
| 518 |
-
return False, f"{status_msg}\n❌ MAFFT required for alignment.
|
| 519 |
|
| 520 |
if not iqtree_available:
|
| 521 |
-
status_msg += "\n⚠
|
| 522 |
|
| 523 |
-
# Try simple tree as fallback
|
| 524 |
-
multi_fasta = create_multi_fasta_with_query(
|
| 525 |
if multi_fasta:
|
| 526 |
# Read sequences
|
| 527 |
sequences = {}
|
|
@@ -551,50 +399,50 @@ def build_maximum_likelihood_tree(f_sequence):
|
|
| 551 |
else:
|
| 552 |
return False, f"{status_msg}❌ Failed to create input sequences", None, None
|
| 553 |
|
| 554 |
-
# Both tools available - proceed with ML analysis
|
| 555 |
# Create output directory
|
| 556 |
output_dir = "ml_tree_output"
|
| 557 |
os.makedirs(output_dir, exist_ok=True)
|
| 558 |
|
| 559 |
-
# Step 1: Create multi-FASTA
|
| 560 |
-
logging.info("Creating multi-FASTA...")
|
| 561 |
-
multi_fasta = create_multi_fasta_with_query(
|
| 562 |
if not multi_fasta:
|
| 563 |
-
return False, f"{status_msg}❌ Failed to create FASTA", None, None
|
| 564 |
|
| 565 |
-
# Step 2: Run MAFFT
|
| 566 |
-
logging.info("Running MAFFT...")
|
| 567 |
aligned_fasta = os.path.join(output_dir, "aligned_sequences.fasta")
|
| 568 |
mafft_success, mafft_result = run_mafft_alignment(multi_fasta, aligned_fasta, mafft_cmd)
|
| 569 |
|
| 570 |
-
# Clean up
|
| 571 |
os.unlink(multi_fasta)
|
| 572 |
|
| 573 |
if not mafft_success:
|
| 574 |
return False, f"{status_msg}❌ MAFFT failed: {mafft_result}", None, None
|
| 575 |
|
| 576 |
-
# Step 3: Run IQ-TREE
|
| 577 |
-
logging.info("Running IQ-TREE...")
|
| 578 |
tree_prefix = os.path.join(output_dir, "ml_tree")
|
| 579 |
iqtree_success, iqtree_result = run_iqtree_analysis(aligned_fasta, tree_prefix, iqtree_cmd)
|
| 580 |
|
| 581 |
if not iqtree_success:
|
| 582 |
return False, f"{status_msg}❌ IQ-TREE failed: {iqtree_result}", aligned_fasta, None
|
| 583 |
|
| 584 |
-
# Step 4: Prepare
|
| 585 |
tree_file = iqtree_result
|
| 586 |
log_file = f"{tree_prefix}.log"
|
| 587 |
|
| 588 |
-
# Copy to standard names
|
| 589 |
-
standard_aligned = "
|
| 590 |
-
standard_tree = "
|
| 591 |
|
| 592 |
if os.path.exists(aligned_fasta):
|
| 593 |
shutil.copy2(aligned_fasta, standard_aligned)
|
| 594 |
if os.path.exists(tree_file):
|
| 595 |
shutil.copy2(tree_file, standard_tree)
|
| 596 |
|
| 597 |
-
success_msg = f"{status_msg}✅
|
| 598 |
success_msg += f"- Alignment: {os.path.basename(aligned_fasta)}\n"
|
| 599 |
success_msg += f"- Tree: {os.path.basename(tree_file)}\n"
|
| 600 |
|
|
@@ -602,76 +450,76 @@ def build_maximum_likelihood_tree(f_sequence):
|
|
| 602 |
try:
|
| 603 |
with open(log_file, 'r') as f:
|
| 604 |
log_content = f.read()
|
| 605 |
-
# Extract model
|
| 606 |
if "Best-fit model:" in log_content:
|
| 607 |
model_lines = [line for line in log_content.split('\n') if "Best-fit model:" in line]
|
| 608 |
if model_lines:
|
| 609 |
success_msg += f"- {model_lines[0].strip()}\n"
|
| 610 |
except Exception as e:
|
| 611 |
-
logging.warning(f"Could not read log: {e}")
|
| 612 |
|
| 613 |
-
logging.info("
|
| 614 |
return True, success_msg, aligned_fasta, tree_file
|
| 615 |
|
| 616 |
except Exception as e:
|
| 617 |
-
logging.error(f"ML tree failed: {e}")
|
| 618 |
-
return False, f"ML tree failed: {str(e)}", None, None
|
| 619 |
|
| 620 |
-
# --- Tree Analysis Function ---
|
| 621 |
def analyze_sequence_for_tree(sequence: str, matching_percentage: float) -> str:
|
| 622 |
"""
|
| 623 |
-
Analyze sequence and create phylogenetic tree
|
| 624 |
"""
|
| 625 |
try:
|
| 626 |
if not analyzer:
|
| 627 |
-
return "Error: Tree analyzer not initialized.
|
| 628 |
|
| 629 |
if not sequence:
|
| 630 |
-
return "Error:
|
| 631 |
|
| 632 |
if not (1 <= matching_percentage <= 99):
|
| 633 |
-
return "Error: Matching percentage must be 1
|
| 634 |
|
| 635 |
-
# Find query
|
| 636 |
if not analyzer.find_query_sequence(sequence):
|
| 637 |
-
return "Error: Invalid query or not in dataset."
|
| 638 |
|
| 639 |
-
# Set percentage
|
| 640 |
analyzer.matching_percentage = matching_percentage
|
| 641 |
|
| 642 |
# Find similar sequences
|
| 643 |
matched_ids, actual_percentage = analyzer.find_similar_sequences(matching_percentage)
|
| 644 |
|
| 645 |
if not matched_ids:
|
| 646 |
-
return f"No similar sequences at {matching_percentage}% similarity.
|
| 647 |
|
| 648 |
-
logging.info(f"Found {len(matched_ids)} similar sequences at {actual_percentage:.1f}%")
|
| 649 |
|
| 650 |
-
# Build tree
|
| 651 |
tree_structure = analyzer.build_tree_structure(matched_ids)
|
| 652 |
if not tree_structure:
|
| 653 |
-
return "Error: Failed to build tree."
|
| 654 |
|
| 655 |
# Create interactive tree
|
| 656 |
fig = analyzer.create_interactive_tree(matched_ids, actual_percentage)
|
| 657 |
if not fig:
|
| 658 |
-
return "Error: Failed to create visualization."
|
| 659 |
|
| 660 |
-
# Generate HTML
|
| 661 |
html_content = fig.to_html(full_html=True, include_plotlyjs='cdn')
|
| 662 |
|
| 663 |
-
# Save to output
|
| 664 |
output_dir = "output"
|
| 665 |
os.makedirs(output_dir, exist_ok=True)
|
| 666 |
|
| 667 |
-
#
|
| 668 |
safe_seq_name = re.sub(r'[^a-zA-Z0-9]', '_', sequence[:20])
|
| 669 |
html_filename = os.path.join(output_dir, f"tree_{safe_seq_name}_{matching_percentage}.html")
|
| 670 |
|
| 671 |
with open(html_filename, "w", encoding='utf-8') as f:
|
| 672 |
f.write(html_content)
|
| 673 |
|
| 674 |
-
logging.info(f"Tree HTML saved
|
| 675 |
|
| 676 |
return html_content
|
| 677 |
|
|
@@ -679,344 +527,498 @@ def analyze_sequence_for_tree(sequence: str, matching_percentage: float) -> str:
|
|
| 679 |
error_msg = f"Tree analysis error: {str(e)}"
|
| 680 |
logging.error(error_msg)
|
| 681 |
import traceback
|
| 682 |
-
logging.error(f"
|
| 683 |
return error_msg
|
| 684 |
|
| 685 |
# --- Keras Prediction ---
|
| 686 |
def predict_with_keras(sequence):
|
| 687 |
try:
|
| 688 |
if not keras_model or not kmer_to_index:
|
| 689 |
-
return f"Keras model
|
| 690 |
|
| 691 |
if len(sequence) < 6:
|
| 692 |
-
return "Sequence too short (
|
| 693 |
|
| 694 |
# Generate k-mers
|
| 695 |
-
kmers = [sequence[i:i+6] for i in range(len(sequence)
|
| 696 |
-
|
| 697 |
-
# Convert to indices
|
| 698 |
-
kmer_indices = []
|
| 699 |
-
for kmer in kmers:
|
| 700 |
-
if kmer in kmer_to_index:
|
| 701 |
-
kmer_indices.append(kmer_to_index[kmer])
|
| 702 |
-
else:
|
| 703 |
-
kmer_indices.append(0)
|
| 704 |
-
|
| 705 |
-
if not kmer_indices:
|
| 706 |
-
return "No valid k-mers."
|
| 707 |
-
|
| 708 |
-
# Pad/truncate
|
| 709 |
-
max_length = 1000
|
| 710 |
-
if len(kmer_indices) > max_length:
|
| 711 |
-
kmer_indices = kmer_indices[:max_length]
|
| 712 |
-
else:
|
| 713 |
-
kmer_indices.extend([0] * (max_length - len(kmer_indices)))
|
| 714 |
-
|
| 715 |
-
# Convert to array
|
| 716 |
-
input_array = np.array([kmer_indices])
|
| 717 |
|
| 718 |
-
#
|
| 719 |
-
|
|
|
|
| 720 |
|
| 721 |
-
# Format
|
| 722 |
-
|
| 723 |
-
|
| 724 |
-
result = "Positive" if probability > 0.5 else "Negative"
|
| 725 |
-
return f"Keras Prediction: {result} (confidence: {probability:.3f})"
|
| 726 |
-
else:
|
| 727 |
-
predicted_class = np.argmax(prediction[0])
|
| 728 |
-
confidence = float(np.max(prediction[0]))
|
| 729 |
-
return f"Keras Prediction: Class {predicted_class} (confidence: {confidence:.3f})"
|
| 730 |
-
|
| 731 |
except Exception as e:
|
| 732 |
-
logging.error(f"Keras prediction
|
| 733 |
return f"Keras prediction failed: {str(e)}"
|
| 734 |
|
| 735 |
-
# ---
|
| 736 |
-
def
|
| 737 |
try:
|
| 738 |
-
if
|
| 739 |
-
return
|
| 740 |
|
| 741 |
-
|
| 742 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 743 |
|
|
|
|
|
|
|
|
|
|
| 744 |
except Exception as e:
|
| 745 |
-
logging.error(f"
|
| 746 |
-
return
|
| 747 |
|
| 748 |
-
# ---
|
| 749 |
-
def
|
| 750 |
-
|
| 751 |
-
|
| 752 |
-
|
| 753 |
-
|
| 754 |
-
return
|
| 755 |
-
|
| 756 |
-
|
| 757 |
-
|
| 758 |
-
|
| 759 |
-
|
| 760 |
-
if len(sequence) < 10:
|
| 761 |
-
return "Sequence too short (min 10 nucleotides)."
|
| 762 |
-
|
| 763 |
-
results.append(f"Analyzing sequence: {len(sequence)} bp")
|
| 764 |
-
results.append("-" * 50)
|
| 765 |
-
|
| 766 |
-
# Keras
|
| 767 |
-
if use_keras:
|
| 768 |
-
keras_result = predict_with_keras(sequence)
|
| 769 |
-
results.append(f"🧠 {keras_result}")
|
| 770 |
-
|
| 771 |
-
# Boundary
|
| 772 |
-
if use_boundary:
|
| 773 |
-
boundary_result = predict_with_boundary_model(sequence)
|
| 774 |
-
results.append(f"🎯 {boundary_result}")
|
| 775 |
-
|
| 776 |
-
return "\n".join(results)
|
| 777 |
|
| 778 |
-
|
| 779 |
-
def process_uploaded_file(file):
|
| 780 |
-
"""Process FASTA file"""
|
| 781 |
try:
|
| 782 |
-
|
| 783 |
-
|
| 784 |
-
|
| 785 |
-
|
| 786 |
-
|
| 787 |
-
|
| 788 |
-
|
| 789 |
-
|
| 790 |
-
|
| 791 |
-
|
| 792 |
-
|
| 793 |
-
|
| 794 |
-
|
| 795 |
-
if line.startswith('>'):
|
| 796 |
-
if current_header and current_seq:
|
| 797 |
-
sequences.append((current_header, current_seq))
|
| 798 |
-
current_header = line[1:]
|
| 799 |
-
current_seq = ""
|
| 800 |
-
else:
|
| 801 |
-
current_seq += line
|
| 802 |
-
|
| 803 |
-
# Add last sequence
|
| 804 |
-
if current_header and current_seq:
|
| 805 |
-
sequences.append((current_header, current_seq))
|
| 806 |
-
|
| 807 |
-
if not sequences:
|
| 808 |
-
return "No valid sequences."
|
| 809 |
-
|
| 810 |
-
# Process sequences
|
| 811 |
-
results = []
|
| 812 |
-
results.append(f"Found {len(sequences)} sequences:")
|
| 813 |
-
results.append("=" * 60)
|
| 814 |
|
| 815 |
-
|
| 816 |
-
|
| 817 |
-
|
| 818 |
-
|
| 819 |
-
|
| 820 |
-
|
| 821 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 822 |
|
| 823 |
-
if len(
|
| 824 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 825 |
|
| 826 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 827 |
|
| 828 |
except Exception as e:
|
| 829 |
-
|
| 830 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 831 |
|
| 832 |
# --- Gradio Interface ---
|
| 833 |
-
def
|
| 834 |
-
"""Create Gradio interface"""
|
| 835 |
|
| 836 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 837 |
gr.Markdown("""
|
| 838 |
-
# 🧬 F Gene Analysis
|
| 839 |
-
|
| 840 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 841 |
""")
|
| 842 |
|
| 843 |
-
with gr.
|
| 844 |
-
|
| 845 |
-
|
| 846 |
-
|
| 847 |
-
|
| 848 |
-
|
| 849 |
-
|
| 850 |
-
|
| 851 |
-
|
| 852 |
-
|
| 853 |
-
|
| 854 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 855 |
)
|
| 856 |
|
| 857 |
-
|
| 858 |
-
|
| 859 |
-
|
| 860 |
-
|
| 861 |
-
|
| 862 |
-
)
|
| 863 |
-
use_boundary_check = gr.Checkbox(
|
| 864 |
-
label="Use Boundary Model",
|
| 865 |
-
value=True,
|
| 866 |
-
info="Boundary detection"
|
| 867 |
-
)
|
| 868 |
-
|
| 869 |
-
predict_btn = gr.Button("🧠 Predict", variant="primary")
|
| 870 |
|
| 871 |
-
|
| 872 |
-
|
| 873 |
-
|
| 874 |
-
|
| 875 |
-
- Min length: 10 bp
|
| 876 |
-
- Longer sequences improve predictions
|
| 877 |
-
- N allowed for unknowns
|
| 878 |
-
""")
|
| 879 |
|
| 880 |
-
|
| 881 |
-
|
| 882 |
-
|
| 883 |
-
|
| 884 |
-
|
| 885 |
-
|
| 886 |
-
|
| 887 |
-
|
| 888 |
-
|
| 889 |
-
|
| 890 |
-
|
| 891 |
-
|
| 892 |
-
|
| 893 |
-
|
| 894 |
-
|
| 895 |
-
|
| 896 |
-
|
| 897 |
-
label="Upload FASTA",
|
| 898 |
-
file_types=[".fasta", ".fa", ".fas", ".txt"],
|
| 899 |
-
file_count="single"
|
| 900 |
-
)
|
| 901 |
-
|
| 902 |
-
process_file_btn = gr.Button("📊 Process", variant="primary")
|
| 903 |
-
|
| 904 |
-
file_output = gr.Textbox(
|
| 905 |
-
label="Results",
|
| 906 |
-
lines=15,
|
| 907 |
-
interactive=False
|
| 908 |
-
)
|
| 909 |
-
|
| 910 |
-
process_file_btn.click(
|
| 911 |
-
fn=process_uploaded_file,
|
| 912 |
-
inputs=file_input,
|
| 913 |
-
outputs=file_output
|
| 914 |
-
)
|
| 915 |
-
|
| 916 |
-
# Tab 3: Phylogenetic Tree
|
| 917 |
-
with gr.TabItem("🌳 Phylogenetic Tree"):
|
| 918 |
-
gr.Markdown("### Tree analysis and placement")
|
| 919 |
-
|
| 920 |
-
with gr.Row():
|
| 921 |
-
with gr.Column():
|
| 922 |
-
tree_sequence_input = gr.Textbox(
|
| 923 |
-
label="Query Sequence",
|
| 924 |
-
placeholder="Enter DNA for placement...",
|
| 925 |
-
lines=5
|
| 926 |
-
)
|
| 927 |
|
| 928 |
-
|
| 929 |
-
|
| 930 |
-
|
| 931 |
-
|
| 932 |
-
step=1,
|
| 933 |
-
label="Similarity Threshold (%)",
|
| 934 |
-
info="Min similarity for tree"
|
| 935 |
-
)
|
| 936 |
|
| 937 |
-
|
| 938 |
-
|
| 939 |
-
|
| 940 |
-
|
| 941 |
-
|
| 942 |
-
|
| 943 |
-
|
| 944 |
-
|
| 945 |
-
|
| 946 |
-
|
| 947 |
-
|
| 948 |
-
|
| 949 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 950 |
interactive=False
|
| 951 |
)
|
| 952 |
|
| 953 |
-
|
| 954 |
-
|
| 955 |
-
|
| 956 |
-
outputs=tree_output
|
| 957 |
-
)
|
| 958 |
-
|
| 959 |
-
def ml_tree_wrapper(sequence):
|
| 960 |
-
success, message, alignment, tree = build_maximum_likelihood_tree(sequence)
|
| 961 |
-
return message
|
| 962 |
-
|
| 963 |
-
ml_tree_btn.click(
|
| 964 |
-
fn=ml_tree_wrapper,
|
| 965 |
-
inputs=tree_sequence_input,
|
| 966 |
-
outputs=tree_status
|
| 967 |
)
|
| 968 |
|
| 969 |
-
|
| 970 |
-
|
| 971 |
-
|
| 972 |
-
|
| 973 |
-
place_sequence_btn.click(
|
| 974 |
-
fn=place_sequence_wrapper,
|
| 975 |
-
inputs=tree_sequence_input,
|
| 976 |
-
outputs=tree_status
|
| 977 |
)
|
| 978 |
|
| 979 |
-
|
| 980 |
-
|
| 981 |
-
|
| 982 |
-
|
| 983 |
-
|
| 984 |
-
|
| 985 |
-
|
| 986 |
-
|
| 987 |
-
|
| 988 |
-
|
| 989 |
-
|
| 990 |
-
|
| 991 |
-
|
| 992 |
-
|
| 993 |
-
|
| 994 |
-
|
| 995 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 996 |
|
| 997 |
-
#
|
| 998 |
-
|
| 999 |
-
|
| 1000 |
-
|
| 1001 |
-
|
| 1002 |
-
|
| 1003 |
-
|
| 1004 |
-
|
| 1005 |
-
|
| 1006 |
-
|
| 1007 |
-
|
| 1008 |
-
|
| 1009 |
-
|
| 1010 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1011 |
)
|
| 1012 |
|
| 1013 |
-
|
| 1014 |
-
|
| 1015 |
-
|
| 1016 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1017 |
|
| 1018 |
-
return
|
| 1019 |
|
|
|
|
| 1020 |
if __name__ == "__main__":
|
| 1021 |
-
|
| 1022 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
|
|
|
|
| 114 |
'/usr/bin/mafft',
|
| 115 |
'/usr/local/bin/mafft',
|
| 116 |
'mafft.bat', # Windows
|
|
|
|
| 117 |
]
|
| 118 |
|
| 119 |
for candidate in mafft_candidates:
|
|
|
|
| 132 |
IQTREE_PATH,
|
| 133 |
'iqtree2',
|
| 134 |
'iqtree',
|
| 135 |
+
'/usr/bin/iqtree2',
|
| 136 |
+
'/usr/local/bin/iqtree2',
|
| 137 |
+
'/usr/bin/iqtree',
|
|
|
|
| 138 |
'/usr/local/bin/iqtree',
|
| 139 |
'iqtree2.exe', # Windows
|
| 140 |
'iqtree.exe', # Windows
|
|
|
|
|
|
|
|
|
|
| 141 |
]
|
| 142 |
|
| 143 |
for candidate in iqtree_candidates:
|
|
|
|
| 149 |
|
| 150 |
return mafft_available, iqtree_available, mafft_cmd, iqtree_cmd
|
| 151 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
def install_dependencies_guide():
|
| 153 |
"""Provide installation guidance for missing dependencies"""
|
| 154 |
guide = """
|
|
|
|
| 323 |
for idx, row in analyzer.data.iterrows():
|
| 324 |
if 'sequence' in row and len(str(row['sequence'])) > 50:
|
| 325 |
seq_id = row.get('id', f"Ref_{count}")
|
| 326 |
+
sequence = str(row['sequence']).upper()
|
| 327 |
temp_fasta.write(f">{seq_id}\n{sequence}\n")
|
| 328 |
count += 1
|
| 329 |
if count >= 20: # Limit to prevent too large datasets
|
| 330 |
break
|
| 331 |
+
logging.info(f"Added {count} reference sequences from CSV")
|
| 332 |
+
|
| 333 |
temp_fasta.close()
|
| 334 |
return temp_fasta.name
|
| 335 |
+
|
| 336 |
except Exception as e:
|
| 337 |
+
logging.error(f"Failed to create multi-FASTA: {e}")
|
| 338 |
return None
|
| 339 |
|
| 340 |
+
def build_maximum_likelihood_tree(f_gene_sequence):
|
| 341 |
+
"""Build maximum likelihood phylogenetic tree with comprehensive fallback options"""
|
| 342 |
try:
|
| 343 |
+
# Check tool availability with enhanced detection
|
| 344 |
mafft_available, iqtree_available, mafft_cmd, iqtree_cmd = check_tool_availability()
|
| 345 |
|
| 346 |
+
# Prepare status message
|
| 347 |
status_msg = "🔍 Checking dependencies...\n"
|
| 348 |
|
| 349 |
if not mafft_available:
|
|
|
|
| 356 |
else:
|
| 357 |
status_msg += f"✅ IQ-TREE found: {iqtree_cmd}\n"
|
| 358 |
|
| 359 |
+
# If neither tool is available, provide installation guide
|
| 360 |
if not mafft_available and not iqtree_available:
|
| 361 |
guide = install_dependencies_guide()
|
| 362 |
return False, f"{status_msg}\n{guide}", None, None
|
| 363 |
|
| 364 |
+
# If only one tool is missing, provide specific guidance
|
| 365 |
if not mafft_available:
|
| 366 |
+
return False, f"{status_msg}\n❌ MAFFT is required for sequence alignment. Please install MAFFT first.", None, None
|
| 367 |
|
| 368 |
if not iqtree_available:
|
| 369 |
+
status_msg += "\n⚠️ IQ-TREE not available. Attempting simple tree construction...\n"
|
| 370 |
|
| 371 |
+
# Try to create a simple tree as fallback
|
| 372 |
+
multi_fasta = create_multi_fasta_with_query(f_gene_sequence)
|
| 373 |
if multi_fasta:
|
| 374 |
# Read sequences
|
| 375 |
sequences = {}
|
|
|
|
| 399 |
else:
|
| 400 |
return False, f"{status_msg}❌ Failed to create input sequences", None, None
|
| 401 |
|
| 402 |
+
# Both tools available - proceed with full ML analysis
|
| 403 |
# Create output directory
|
| 404 |
output_dir = "ml_tree_output"
|
| 405 |
os.makedirs(output_dir, exist_ok=True)
|
| 406 |
|
| 407 |
+
# Step 1: Create multi-FASTA file with query and reference sequences
|
| 408 |
+
logging.info("Creating multi-FASTA file...")
|
| 409 |
+
multi_fasta = create_multi_fasta_with_query(f_gene_sequence)
|
| 410 |
if not multi_fasta:
|
| 411 |
+
return False, f"{status_msg}❌ Failed to create input FASTA", None, None
|
| 412 |
|
| 413 |
+
# Step 2: Run MAFFT alignment
|
| 414 |
+
logging.info("Running MAFFT alignment...")
|
| 415 |
aligned_fasta = os.path.join(output_dir, "aligned_sequences.fasta")
|
| 416 |
mafft_success, mafft_result = run_mafft_alignment(multi_fasta, aligned_fasta, mafft_cmd)
|
| 417 |
|
| 418 |
+
# Clean up temporary file
|
| 419 |
os.unlink(multi_fasta)
|
| 420 |
|
| 421 |
if not mafft_success:
|
| 422 |
return False, f"{status_msg}❌ MAFFT failed: {mafft_result}", None, None
|
| 423 |
|
| 424 |
+
# Step 3: Run IQ-TREE analysis
|
| 425 |
+
logging.info("Running IQ-TREE analysis...")
|
| 426 |
tree_prefix = os.path.join(output_dir, "ml_tree")
|
| 427 |
iqtree_success, iqtree_result = run_iqtree_analysis(aligned_fasta, tree_prefix, iqtree_cmd)
|
| 428 |
|
| 429 |
if not iqtree_success:
|
| 430 |
return False, f"{status_msg}❌ IQ-TREE failed: {iqtree_result}", aligned_fasta, None
|
| 431 |
|
| 432 |
+
# Step 4: Prepare output files
|
| 433 |
tree_file = iqtree_result
|
| 434 |
log_file = f"{tree_prefix}.log"
|
| 435 |
|
| 436 |
+
# Copy to standard names for compatibility
|
| 437 |
+
standard_aligned = "f_gene_sequences_aligned.fasta"
|
| 438 |
+
standard_tree = "f_gene_sequences.phy.treefile"
|
| 439 |
|
| 440 |
if os.path.exists(aligned_fasta):
|
| 441 |
shutil.copy2(aligned_fasta, standard_aligned)
|
| 442 |
if os.path.exists(tree_file):
|
| 443 |
shutil.copy2(tree_file, standard_tree)
|
| 444 |
|
| 445 |
+
success_msg = f"{status_msg}✅ Maximum likelihood tree built successfully!\n"
|
| 446 |
success_msg += f"- Alignment: {os.path.basename(aligned_fasta)}\n"
|
| 447 |
success_msg += f"- Tree: {os.path.basename(tree_file)}\n"
|
| 448 |
|
|
|
|
| 450 |
try:
|
| 451 |
with open(log_file, 'r') as f:
|
| 452 |
log_content = f.read()
|
| 453 |
+
# Extract model information
|
| 454 |
if "Best-fit model:" in log_content:
|
| 455 |
model_lines = [line for line in log_content.split('\n') if "Best-fit model:" in line]
|
| 456 |
if model_lines:
|
| 457 |
success_msg += f"- {model_lines[0].strip()}\n"
|
| 458 |
except Exception as e:
|
| 459 |
+
logging.warning(f"Could not read log file: {e}")
|
| 460 |
|
| 461 |
+
logging.info("Maximum likelihood tree construction completed")
|
| 462 |
return True, success_msg, aligned_fasta, tree_file
|
| 463 |
|
| 464 |
except Exception as e:
|
| 465 |
+
logging.error(f"ML tree construction failed: {e}")
|
| 466 |
+
return False, f"ML tree construction failed: {str(e)}", None, None
|
| 467 |
|
| 468 |
+
# --- Tree Analysis Function (Based on old Gradio API) ---
|
| 469 |
def analyze_sequence_for_tree(sequence: str, matching_percentage: float) -> str:
|
| 470 |
"""
|
| 471 |
+
Analyze sequence and create phylogenetic tree using the working Gradio API pattern
|
| 472 |
"""
|
| 473 |
try:
|
| 474 |
if not analyzer:
|
| 475 |
+
return "Error: Tree analyzer not initialized. Please check if the CSV data file is available."
|
| 476 |
|
| 477 |
if not sequence:
|
| 478 |
+
return "Error: Please provide a sequence."
|
| 479 |
|
| 480 |
if not (1 <= matching_percentage <= 99):
|
| 481 |
+
return "Error: Matching percentage must be between 1 and 99."
|
| 482 |
|
| 483 |
+
# Find query sequence
|
| 484 |
if not analyzer.find_query_sequence(sequence):
|
| 485 |
+
return "Error: Invalid query sequence or sequence not found in dataset."
|
| 486 |
|
| 487 |
+
# Set matching percentage
|
| 488 |
analyzer.matching_percentage = matching_percentage
|
| 489 |
|
| 490 |
# Find similar sequences
|
| 491 |
matched_ids, actual_percentage = analyzer.find_similar_sequences(matching_percentage)
|
| 492 |
|
| 493 |
if not matched_ids:
|
| 494 |
+
return f"No similar sequences found at {matching_percentage}% similarity. Try lowering the threshold."
|
| 495 |
|
| 496 |
+
logging.info(f"Found {len(matched_ids)} similar sequences at {actual_percentage:.1f}% similarity")
|
| 497 |
|
| 498 |
+
# Build tree structure
|
| 499 |
tree_structure = analyzer.build_tree_structure(matched_ids)
|
| 500 |
if not tree_structure:
|
| 501 |
+
return "Error: Failed to build tree structure."
|
| 502 |
|
| 503 |
# Create interactive tree
|
| 504 |
fig = analyzer.create_interactive_tree(matched_ids, actual_percentage)
|
| 505 |
if not fig:
|
| 506 |
+
return "Error: Failed to create tree visualization."
|
| 507 |
|
| 508 |
+
# Generate HTML content
|
| 509 |
html_content = fig.to_html(full_html=True, include_plotlyjs='cdn')
|
| 510 |
|
| 511 |
+
# Save to output folder
|
| 512 |
output_dir = "output"
|
| 513 |
os.makedirs(output_dir, exist_ok=True)
|
| 514 |
|
| 515 |
+
# Create a safe filename
|
| 516 |
safe_seq_name = re.sub(r'[^a-zA-Z0-9]', '_', sequence[:20])
|
| 517 |
html_filename = os.path.join(output_dir, f"tree_{safe_seq_name}_{matching_percentage}.html")
|
| 518 |
|
| 519 |
with open(html_filename, "w", encoding='utf-8') as f:
|
| 520 |
f.write(html_content)
|
| 521 |
|
| 522 |
+
logging.info(f"Tree HTML saved to {html_filename}")
|
| 523 |
|
| 524 |
return html_content
|
| 525 |
|
|
|
|
| 527 |
error_msg = f"Tree analysis error: {str(e)}"
|
| 528 |
logging.error(error_msg)
|
| 529 |
import traceback
|
| 530 |
+
logging.error(f"Full traceback: {traceback.format_exc()}")
|
| 531 |
return error_msg
|
| 532 |
|
| 533 |
# --- Keras Prediction ---
|
| 534 |
def predict_with_keras(sequence):
|
| 535 |
try:
|
| 536 |
if not keras_model or not kmer_to_index:
|
| 537 |
+
return f"Keras model not available. Input sequence: {sequence[:100]}..."
|
| 538 |
|
| 539 |
if len(sequence) < 6:
|
| 540 |
+
return "Sequence too short for k-mer prediction (minimum 6 nucleotides required)."
|
| 541 |
|
| 542 |
# Generate k-mers
|
| 543 |
+
kmers = [sequence[i:i+6] for i in range(len(sequence)-5)]
|
| 544 |
+
indices = [kmer_to_index.get(kmer, 0) for kmer in kmers]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 545 |
|
| 546 |
+
# Prepare input
|
| 547 |
+
input_arr = np.array([indices])
|
| 548 |
+
prediction = keras_model.predict(input_arr, verbose=0)[0]
|
| 549 |
|
| 550 |
+
# Format prediction as probabilities/scores (not a sequence)
|
| 551 |
+
result = ''.join([str(round(p, 3)) for p in prediction])
|
| 552 |
+
return result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 553 |
except Exception as e:
|
| 554 |
+
logging.error(f"Keras prediction failed: {e}")
|
| 555 |
return f"Keras prediction failed: {str(e)}"
|
| 556 |
|
| 557 |
+
# --- FASTA Reader ---
|
| 558 |
+
def read_fasta_file(file_obj):
|
| 559 |
try:
|
| 560 |
+
if file_obj is None:
|
| 561 |
+
return ""
|
| 562 |
|
| 563 |
+
# Handle file object
|
| 564 |
+
if hasattr(file_obj, 'name'):
|
| 565 |
+
with open(file_obj.name, "r") as f:
|
| 566 |
+
content = f.read()
|
| 567 |
+
else:
|
| 568 |
+
content = file_obj.read().decode("utf-8") if hasattr(file_obj, "read") else str(file_obj)
|
| 569 |
|
| 570 |
+
lines = content.strip().split("\n")
|
| 571 |
+
seq_lines = [line.strip() for line in lines if not line.startswith(">")]
|
| 572 |
+
return ''.join(seq_lines)
|
| 573 |
except Exception as e:
|
| 574 |
+
logging.error(f"Failed to read FASTA file: {e}")
|
| 575 |
+
return ""
|
| 576 |
|
| 577 |
+
# --- Full Pipeline ---
|
| 578 |
+
def run_pipeline_from_file(fasta_file_obj, similarity_score, build_ml_tree):
|
| 579 |
+
try:
|
| 580 |
+
dna_input = read_fasta_file(fasta_file_obj)
|
| 581 |
+
if not dna_input:
|
| 582 |
+
return "Failed to read FASTA file", "", "", "", "", None, None, None, "No input sequence"
|
| 583 |
+
return run_pipeline(dna_input, similarity_score, build_ml_tree)
|
| 584 |
+
except Exception as e:
|
| 585 |
+
error_msg = f"Pipeline error: {str(e)}"
|
| 586 |
+
logging.error(error_msg)
|
| 587 |
+
return error_msg, "", "", "", "", None, None, None, error_msg
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 588 |
|
| 589 |
+
def run_pipeline(dna_input, similarity_score=95.0, build_ml_tree=False):
|
|
|
|
|
|
|
| 590 |
try:
|
| 591 |
+
# Clean input
|
| 592 |
+
dna_input = dna_input.upper().strip()
|
| 593 |
+
if not dna_input:
|
| 594 |
+
return "Empty input", "", "", "", "", None, None, None, "No input provided"
|
| 595 |
+
|
| 596 |
+
# Sanitize DNA sequence
|
| 597 |
+
if not re.match('^[ACTGN]+$', dna_input):
|
| 598 |
+
dna_input = ''.join(c if c in 'ACTGN' else 'N' for c in dna_input)
|
| 599 |
+
logging.info("DNA sequence sanitized")
|
| 600 |
+
|
| 601 |
+
# Step 1: Boundary Prediction - Extract F gene sequence
|
| 602 |
+
processed_sequence = dna_input # This will be the sequence used for downstream analysis
|
| 603 |
+
boundary_output = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 604 |
|
| 605 |
+
if boundary_model:
|
| 606 |
+
try:
|
| 607 |
+
predictions, probs, confidence = boundary_model.predict(dna_input)
|
| 608 |
+
regions = boundary_model.extract_gene_regions(predictions, dna_input)
|
| 609 |
+
if regions:
|
| 610 |
+
processed_sequence = regions[0]["sequence"] # Use the extracted gene region
|
| 611 |
+
boundary_output = processed_sequence # Output the actual F gene sequence
|
| 612 |
+
logging.info(f"F gene extracted: {len(processed_sequence)} bp (confidence: {confidence:.3f})")
|
| 613 |
+
else:
|
| 614 |
+
boundary_output = f"No F gene regions found in input sequence"
|
| 615 |
+
processed_sequence = dna_input
|
| 616 |
+
logging.warning("No gene regions found, using full sequence")
|
| 617 |
+
logging.info("Boundary model prediction completed")
|
| 618 |
+
except Exception as e:
|
| 619 |
+
logging.error(f"Boundary model failed: {e}")
|
| 620 |
+
boundary_output = f"Boundary model error: {str(e)}"
|
| 621 |
+
processed_sequence = dna_input # Fall back to original sequence
|
| 622 |
+
else:
|
| 623 |
+
boundary_output = f"Boundary model not available. Using original input: {len(dna_input)} bp"
|
| 624 |
+
processed_sequence = dna_input
|
| 625 |
+
|
| 626 |
+
# Step 2: Keras Prediction (F gene validation)
|
| 627 |
+
keras_output = ""
|
| 628 |
+
if processed_sequence and len(processed_sequence) >= 6:
|
| 629 |
+
keras_prediction = predict_with_keras(processed_sequence)
|
| 630 |
+
# Interpret keras prediction as F gene validation
|
| 631 |
+
if keras_prediction and not keras_prediction.startswith(("Keras", "Sequence too short")):
|
| 632 |
+
# You might want to add logic here to interpret the prediction scores
|
| 633 |
+
# For now, just show the prediction
|
| 634 |
+
keras_output = f"F gene validation scores: {keras_prediction[:100]}..."
|
| 635 |
+
else:
|
| 636 |
+
keras_output = keras_prediction
|
| 637 |
+
else:
|
| 638 |
+
keras_output = "Skipped: sequence too short for F gene validation"
|
| 639 |
+
|
| 640 |
+
# Step 3: Maximum Likelihood Tree (MAFFT + IQ-TREE)
|
| 641 |
+
aligned_file = None
|
| 642 |
+
phy_file = None
|
| 643 |
+
ml_tree_output = ""
|
| 644 |
|
| 645 |
+
if build_ml_tree and processed_sequence and len(processed_sequence) >= 50:
|
| 646 |
+
try:
|
| 647 |
+
logging.info("Starting maximum likelihood tree construction...")
|
| 648 |
+
ml_success, ml_message, ml_aligned, ml_tree = build_maximum_likelihood_tree(processed_sequence)
|
| 649 |
+
|
| 650 |
+
if ml_success:
|
| 651 |
+
ml_tree_output = ml_message
|
| 652 |
+
aligned_file = ml_aligned
|
| 653 |
+
phy_file = ml_tree
|
| 654 |
+
else:
|
| 655 |
+
ml_tree_output = ml_message # This now includes detailed error information
|
| 656 |
+
|
| 657 |
+
except Exception as e:
|
| 658 |
+
ml_tree_output = f"❌ ML Tree construction failed: {str(e)}"
|
| 659 |
+
logging.error(f"ML Tree failed: {e}")
|
| 660 |
+
elif build_ml_tree:
|
| 661 |
+
ml_tree_output = "❌ F gene sequence too short for ML tree construction (minimum 50 bp)"
|
| 662 |
+
else:
|
| 663 |
+
ml_tree_output = "ML tree construction skipped (not requested)"
|
| 664 |
+
|
| 665 |
+
# Step 4: ML Simplified Tree (using the existing approach)
|
| 666 |
+
html_file = None
|
| 667 |
+
tree_html_content = "No tree generated"
|
| 668 |
+
simplified_ml_output = ""
|
| 669 |
|
| 670 |
+
if analyzer and processed_sequence and len(processed_sequence) >= 10:
|
| 671 |
+
try:
|
| 672 |
+
logging.info(f"Starting simplified ML tree analysis with F gene sequence length: {len(processed_sequence)}")
|
| 673 |
+
|
| 674 |
+
# Use the existing tree analysis function with user-specified similarity
|
| 675 |
+
tree_result = analyze_sequence_for_tree(processed_sequence, matching_percentage=similarity_score)
|
| 676 |
+
|
| 677 |
+
if tree_result and not tree_result.startswith("Error:"):
|
| 678 |
+
# Success - we have HTML content
|
| 679 |
+
tree_html_content = tree_result
|
| 680 |
+
simplified_ml_output = "✅ Simplified phylogenetic tree generated successfully!"
|
| 681 |
+
|
| 682 |
+
# Check if HTML file was created
|
| 683 |
+
output_dir = "output"
|
| 684 |
+
if os.path.exists(output_dir):
|
| 685 |
+
html_files = [f for f in os.listdir(output_dir) if f.endswith('.html')]
|
| 686 |
+
if html_files:
|
| 687 |
+
html_file = os.path.join(output_dir, html_files[-1]) # Get the latest
|
| 688 |
+
simplified_ml_output += f"\n- Tree file: {html_files[-1]}"
|
| 689 |
+
|
| 690 |
+
# Count sequences analyzed
|
| 691 |
+
if analyzer.find_query_sequence(processed_sequence):
|
| 692 |
+
matched_ids, perc = analyzer.find_similar_sequences(similarity_score)
|
| 693 |
+
simplified_ml_output += f"\n- {len(matched_ids)} sequences analyzed"
|
| 694 |
+
simplified_ml_output += f"\n- Similarity threshold: {perc:.1f}%"
|
| 695 |
+
else:
|
| 696 |
+
simplified_ml_output = f"❌ Simplified ML tree failed: {tree_result}"
|
| 697 |
+
tree_html_content = f"<p>Error: {tree_result}</p>"
|
| 698 |
+
|
| 699 |
+
except Exception as e:
|
| 700 |
+
logging.error(f"Simplified ML tree analysis failed: {e}")
|
| 701 |
+
simplified_ml_output = f"❌ Simplified ML tree analysis failed: {str(e)}"
|
| 702 |
+
tree_html_content = f"<p>Error: {str(e)}</p>"
|
| 703 |
+
else:
|
| 704 |
+
if not analyzer:
|
| 705 |
+
simplified_ml_output = "❌ Tree analyzer not available"
|
| 706 |
+
else:
|
| 707 |
+
simplified_ml_output = "❌ F gene sequence too short for tree analysis (minimum 10 bp)"
|
| 708 |
+
|
| 709 |
+
# Return all results
|
| 710 |
+
return (
|
| 711 |
+
boundary_output, # F gene extraction result
|
| 712 |
+
keras_output, # F gene validation result
|
| 713 |
+
ml_tree_output, # ML tree construction status
|
| 714 |
+
simplified_ml_output, # Simplified tree analysis status
|
| 715 |
+
tree_html_content, # HTML content for tree display
|
| 716 |
+
aligned_file, # Path to aligned FASTA file
|
| 717 |
+
phy_file, # Path to phylogenetic tree file
|
| 718 |
+
html_file, # Path to HTML tree file
|
| 719 |
+
f"Pipeline completed. F gene length: {len(processed_sequence)} bp" # Summary
|
| 720 |
+
)
|
| 721 |
|
| 722 |
except Exception as e:
|
| 723 |
+
error_msg = f"Pipeline execution failed: {str(e)}"
|
| 724 |
+
logging.error(error_msg)
|
| 725 |
+
import traceback
|
| 726 |
+
logging.error(f"Full traceback: {traceback.format_exc()}")
|
| 727 |
+
return (
|
| 728 |
+
error_msg, "", "", "", f"<p>Error: {error_msg}</p>",
|
| 729 |
+
None, None, None, error_msg
|
| 730 |
+
)
|
| 731 |
|
| 732 |
# --- Gradio Interface ---
|
| 733 |
+
def create_interface():
|
| 734 |
+
"""Create the Gradio interface with enhanced layout and features"""
|
| 735 |
|
| 736 |
+
# Custom CSS for better styling
|
| 737 |
+
custom_css = """
|
| 738 |
+
.gradio-container {
|
| 739 |
+
max-width: 1200px !important;
|
| 740 |
+
}
|
| 741 |
+
.tab-nav button {
|
| 742 |
+
font-size: 16px !important;
|
| 743 |
+
}
|
| 744 |
+
.output-html {
|
| 745 |
+
height: 600px !important;
|
| 746 |
+
overflow: auto;
|
| 747 |
+
}
|
| 748 |
+
"""
|
| 749 |
+
|
| 750 |
+
with gr.Blocks(css=custom_css, title="F Gene Analysis Pipeline") as iface:
|
| 751 |
gr.Markdown("""
|
| 752 |
+
# 🧬 F Gene Analysis Pipeline
|
| 753 |
+
|
| 754 |
+
This tool provides comprehensive analysis of F genes including:
|
| 755 |
+
- **Gene Boundary Detection**: Extract F gene sequences from larger genomic sequences
|
| 756 |
+
- **Gene Validation**: Validate extracted sequences using machine learning
|
| 757 |
+
- **Phylogenetic Analysis**: Build maximum likelihood trees and simplified phylogenetic trees
|
| 758 |
+
|
| 759 |
+
**Instructions:**
|
| 760 |
+
1. Enter your sequence directly or upload a FASTA file
|
| 761 |
+
2. Adjust similarity threshold for phylogenetic analysis (1-99%)
|
| 762 |
+
3. Choose whether to build maximum likelihood trees (requires MAFFT & IQ-TREE)
|
| 763 |
+
4. Click "Run Analysis" to start the pipeline
|
| 764 |
""")
|
| 765 |
|
| 766 |
+
with gr.Tab("🔬 Analysis Pipeline"):
|
| 767 |
+
with gr.Row():
|
| 768 |
+
with gr.Column(scale=2):
|
| 769 |
+
# Input section
|
| 770 |
+
gr.Markdown("### Input Sequence")
|
| 771 |
+
dna_input = gr.Textbox(
|
| 772 |
+
label="DNA Sequence",
|
| 773 |
+
placeholder="Enter your DNA sequence here (ATCG format)...",
|
| 774 |
+
lines=5,
|
| 775 |
+
max_lines=10
|
| 776 |
+
)
|
| 777 |
+
|
| 778 |
+
fasta_file = gr.File(
|
| 779 |
+
label="Or Upload FASTA File",
|
| 780 |
+
file_types=[".fasta", ".fa", ".fas", ".txt"]
|
| 781 |
+
)
|
| 782 |
+
|
| 783 |
+
with gr.Row():
|
| 784 |
+
similarity_score = gr.Slider(
|
| 785 |
+
minimum=1,
|
| 786 |
+
maximum=99,
|
| 787 |
+
value=95.0,
|
| 788 |
+
step=1.0,
|
| 789 |
+
label="Similarity Threshold (%)",
|
| 790 |
+
info="Minimum similarity for phylogenetic analysis"
|
| 791 |
)
|
| 792 |
|
| 793 |
+
build_ml_tree = gr.Checkbox(
|
| 794 |
+
label="Build ML Tree",
|
| 795 |
+
value=False,
|
| 796 |
+
info="Build maximum likelihood tree (requires MAFFT & IQ-TREE)"
|
| 797 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 798 |
|
| 799 |
+
# Action buttons
|
| 800 |
+
with gr.Row():
|
| 801 |
+
run_btn = gr.Button("🚀 Run Analysis", variant="primary", size="lg")
|
| 802 |
+
clear_btn = gr.Button("🗑️ Clear", variant="secondary")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 803 |
|
| 804 |
+
with gr.Column(scale=1):
|
| 805 |
+
# Status and info
|
| 806 |
+
gr.Markdown("### Analysis Status")
|
| 807 |
+
status_display = gr.Textbox(
|
| 808 |
+
label="Status",
|
| 809 |
+
value="Ready to analyze",
|
| 810 |
+
interactive=False,
|
| 811 |
+
lines=3
|
| 812 |
+
)
|
| 813 |
+
|
| 814 |
+
# Model status
|
| 815 |
+
gr.Markdown("### Available Models")
|
| 816 |
+
model_status = []
|
| 817 |
+
if boundary_model:
|
| 818 |
+
model_status.append("✅ Boundary Detection Model")
|
| 819 |
+
else:
|
| 820 |
+
model_status.append("❌ Boundary Detection Model")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 821 |
|
| 822 |
+
if keras_model:
|
| 823 |
+
model_status.append("✅ Gene Validation Model")
|
| 824 |
+
else:
|
| 825 |
+
model_status.append("❌ Gene Validation Model")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 826 |
|
| 827 |
+
if analyzer:
|
| 828 |
+
model_status.append("✅ Tree Analysis Module")
|
| 829 |
+
else:
|
| 830 |
+
model_status.append("❌ Tree Analysis Module")
|
| 831 |
+
|
| 832 |
+
gr.Markdown("\n".join(model_status))
|
| 833 |
+
|
| 834 |
+
with gr.Tab("📊 Results"):
|
| 835 |
+
with gr.Row():
|
| 836 |
+
with gr.Column():
|
| 837 |
+
# Text outputs
|
| 838 |
+
boundary_output = gr.Textbox(
|
| 839 |
+
label="🎯 F Gene Extraction",
|
| 840 |
+
lines=5,
|
| 841 |
+
interactive=False
|
| 842 |
+
)
|
| 843 |
+
|
| 844 |
+
keras_output = gr.Textbox(
|
| 845 |
+
label="🔍 Gene Validation",
|
| 846 |
+
lines=3,
|
| 847 |
+
interactive=False
|
| 848 |
+
)
|
| 849 |
+
|
| 850 |
+
with gr.Column():
|
| 851 |
+
ml_tree_output = gr.Textbox(
|
| 852 |
+
label="🌳 Maximum Likelihood Tree",
|
| 853 |
+
lines=5,
|
| 854 |
+
interactive=False
|
| 855 |
+
)
|
| 856 |
+
|
| 857 |
+
simplified_ml_output = gr.Textbox(
|
| 858 |
+
label="📈 Simplified Phylogenetic Analysis",
|
| 859 |
+
lines=3,
|
| 860 |
+
interactive=False
|
| 861 |
+
)
|
| 862 |
+
|
| 863 |
+
# Tree visualization
|
| 864 |
+
gr.Markdown("### 🌲 Phylogenetic Tree Visualization")
|
| 865 |
+
tree_html = gr.HTML(
|
| 866 |
+
label="Interactive Tree",
|
| 867 |
+
value="<p>No tree generated yet. Run analysis to see results.</p>"
|
| 868 |
+
)
|
| 869 |
+
|
| 870 |
+
# File downloads
|
| 871 |
+
gr.Markdown("### 📁 Download Results")
|
| 872 |
+
with gr.Row():
|
| 873 |
+
aligned_file = gr.File(
|
| 874 |
+
label="Aligned Sequences (FASTA)",
|
| 875 |
interactive=False
|
| 876 |
)
|
| 877 |
|
| 878 |
+
phy_file = gr.File(
|
| 879 |
+
label="Phylogenetic Tree File",
|
| 880 |
+
interactive=False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 881 |
)
|
| 882 |
|
| 883 |
+
html_file = gr.File(
|
| 884 |
+
label="Interactive Tree (HTML)",
|
| 885 |
+
interactive=False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 886 |
)
|
| 887 |
|
| 888 |
+
with gr.Tab("ℹ️ Help & Info"):
|
| 889 |
+
gr.Markdown("""
|
| 890 |
+
## About This Tool
|
| 891 |
+
|
| 892 |
+
### F Gene Analysis Pipeline
|
| 893 |
+
This comprehensive pipeline analyzes F genes through multiple computational approaches:
|
| 894 |
+
|
| 895 |
+
#### 🎯 Gene Boundary Detection
|
| 896 |
+
- Uses deep learning to identify and extract F gene sequences from larger genomic sequences
|
| 897 |
+
- Provides confidence scores for detected boundaries
|
| 898 |
+
- Automatically trims sequences to focus on the F gene region
|
| 899 |
+
|
| 900 |
+
#### 🔍 Gene Validation
|
| 901 |
+
- Employs k-mer based machine learning models to validate extracted sequences
|
| 902 |
+
- Provides probability scores indicating likelihood of being a genuine F gene
|
| 903 |
+
- Uses 6-mer frequency patterns for classification
|
| 904 |
+
|
| 905 |
+
#### 🌳 Phylogenetic Analysis
|
| 906 |
+
|
| 907 |
+
**Maximum Likelihood Trees:**
|
| 908 |
+
- Requires MAFFT (sequence alignment) and IQ-TREE (phylogenetic reconstruction)
|
| 909 |
+
- Performs model selection and bootstrap analysis
|
| 910 |
+
- Generates publication-quality phylogenetic trees
|
| 911 |
+
- Provides detailed evolutionary analysis
|
| 912 |
+
|
| 913 |
+
**Simplified Trees:**
|
| 914 |
+
- Uses built-in algorithms for quick phylogenetic analysis
|
| 915 |
+
- Interactive visualization with similarity-based clustering
|
| 916 |
+
- Faster alternative when external tools are not available
|
| 917 |
+
|
| 918 |
+
### Input Requirements
|
| 919 |
+
- **DNA Sequences**: ATCG format, minimum 50 bp for meaningful analysis
|
| 920 |
+
- **FASTA Files**: Standard FASTA format with single or multiple sequences
|
| 921 |
+
- **Similarity Threshold**: 1-99% for controlling phylogenetic analysis sensitivity
|
| 922 |
+
|
| 923 |
+
### Dependencies
|
| 924 |
+
|
| 925 |
+
**Required for ML Trees:**
|
| 926 |
+
```bash
|
| 927 |
+
# Ubuntu/Debian
|
| 928 |
+
sudo apt-get install mafft iqtree
|
| 929 |
+
|
| 930 |
+
# macOS
|
| 931 |
+
brew install mafft iqtree
|
| 932 |
+
|
| 933 |
+
# Conda
|
| 934 |
+
conda install -c bioconda mafft iqtree
|
| 935 |
+
```
|
| 936 |
+
|
| 937 |
+
### Output Files
|
| 938 |
+
- **Aligned FASTA**: Multiple sequence alignment in FASTA format
|
| 939 |
+
- **Tree File**: Newick format phylogenetic tree
|
| 940 |
+
- **HTML Tree**: Interactive visualization for web browsers
|
| 941 |
+
|
| 942 |
+
### Troubleshooting
|
| 943 |
+
|
| 944 |
+
**Common Issues:**
|
| 945 |
+
- *"No similar sequences found"*: Lower the similarity threshold
|
| 946 |
+
- *"Sequence too short"*: Provide sequences longer than 50 bp
|
| 947 |
+
- *"MAFFT/IQ-TREE not found"*: Install required dependencies
|
| 948 |
+
- *"Model not available"*: Check model files are properly downloaded
|
| 949 |
+
|
| 950 |
+
**Performance Tips:**
|
| 951 |
+
- Use sequences between 100-2000 bp for optimal performance
|
| 952 |
+
- Limit to <50 sequences for faster tree construction
|
| 953 |
+
- Lower similarity thresholds find more distant relatives
|
| 954 |
+
- Higher thresholds focus on closely related sequences
|
| 955 |
+
|
| 956 |
+
### Citation
|
| 957 |
+
If you use this tool in your research, please cite the appropriate methods and tools used.
|
| 958 |
+
""")
|
| 959 |
|
| 960 |
+
# Event handlers
|
| 961 |
+
def run_analysis_text(dna_seq, sim_score, build_tree):
|
| 962 |
+
return run_pipeline(dna_seq, sim_score, build_tree)
|
| 963 |
+
|
| 964 |
+
def run_analysis_file(file_obj, sim_score, build_tree):
|
| 965 |
+
return run_pipeline_from_file(file_obj, sim_score, build_tree)
|
| 966 |
+
|
| 967 |
+
def run_analysis_combined(dna_seq, file_obj, sim_score, build_tree):
|
| 968 |
+
# Priority: file upload over text input
|
| 969 |
+
if file_obj is not None:
|
| 970 |
+
return run_pipeline_from_file(file_obj, sim_score, build_tree)
|
| 971 |
+
else:
|
| 972 |
+
return run_pipeline(dna_seq, sim_score, build_tree)
|
| 973 |
+
|
| 974 |
+
def clear_inputs():
|
| 975 |
+
return "", None, 95.0, False, "Ready to analyze"
|
| 976 |
+
|
| 977 |
+
# Connect events
|
| 978 |
+
run_btn.click(
|
| 979 |
+
fn=run_analysis_combined,
|
| 980 |
+
inputs=[dna_input, fasta_file, similarity_score, build_ml_tree],
|
| 981 |
+
outputs=[
|
| 982 |
+
boundary_output, keras_output, ml_tree_output,
|
| 983 |
+
simplified_ml_output, tree_html, aligned_file,
|
| 984 |
+
phy_file, html_file, status_display
|
| 985 |
+
]
|
| 986 |
)
|
| 987 |
|
| 988 |
+
clear_btn.click(
|
| 989 |
+
fn=clear_inputs,
|
| 990 |
+
outputs=[dna_input, fasta_file, similarity_score, build_ml_tree, status_display]
|
| 991 |
+
)
|
| 992 |
+
|
| 993 |
+
# Example data loading
|
| 994 |
+
gr.Markdown("### 🧪 Example Data")
|
| 995 |
+
example_btn = gr.Button("Load Example F Gene Sequence", variant="secondary")
|
| 996 |
+
|
| 997 |
+
def load_example():
|
| 998 |
+
example_seq = "ATGAAACTGTCAACACTCACTGAGTACATTAGCCAAGTTCTCAAGACTGAGTGTTTACCTTTGTGAATACACTGAGTCCTTGTCAACGTTCGGCTGCAGTCACACTGATGGTCTTGTCTTCAGGAGCAACTGCAGTCTGTGCTGTGTACTATAGTGCTAAGAGTGATAATGCACTGTTCAGTACCTTTGACAGTGTGTCTCTGTCACCTGGTGCTATGCAGAGCTGCGATGAGATCTACATTGGTCTGATCGATAAGACTGAGTCCAAGGGTGTTGCTGTGTGTACTGTAGAGTGTGATAGTGTTGCCTGCACTGTGTCTATGGCTGATCTTGAGGCTCTGCTTATGTCAACACTGAGTGTGAAATGTTCATTTGCTACTTCAAGACTGATGTGAAGACTGTGTATTGTACTCAGTCATGCAGAGTGAAGTCCTTGAGCCACTTGCTTTGTACAATGTGGGTGATGAGATGTTGTGCTGCAGTGTCAAGGGGCCACAGTCTTGCCTTGATAGTGCGATTGCTGTGATGATGTGCACTTCAATGAGTGGTCGAGATGCTGCTGTGTGTAAGGATGCTGCTGTGTGTAAGAAGGATGCTGCTGTGTGTAAGA"
|
| 999 |
+
return example_seq, "Example F gene sequence loaded"
|
| 1000 |
+
|
| 1001 |
+
example_btn.click(
|
| 1002 |
+
fn=load_example,
|
| 1003 |
+
outputs=[dna_input, status_display]
|
| 1004 |
+
)
|
| 1005 |
|
| 1006 |
+
return iface
|
| 1007 |
|
| 1008 |
+
# --- Main Execution ---
|
| 1009 |
if __name__ == "__main__":
|
| 1010 |
+
# Initialize and launch interface
|
| 1011 |
+
interface = create_interface()
|
| 1012 |
+
|
| 1013 |
+
# Launch with enhanced configuration
|
| 1014 |
+
interface.launch(
|
| 1015 |
+
server_name="0.0.0.0", # Allow external connections
|
| 1016 |
+
server_port=7860, # Default Gradio port
|
| 1017 |
+
share=False, # Set to True for public sharing
|
| 1018 |
+
debug=True, # Enable debug mode
|
| 1019 |
+
show_error=True, # Show detailed errors
|
| 1020 |
+
max_threads=4, # Limit concurrent threads
|
| 1021 |
+
auth=None, # Add authentication if needed: ("username", "password")
|
| 1022 |
+
ssl_verify=False, # For development environments
|
| 1023 |
+
quiet=False # Show startup messages
|
| 1024 |
+
)
|