Spaces:
No application file
No application file
Update app.py
Browse files
app.py
CHANGED
|
@@ -476,27 +476,26 @@ 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}
|
| 485 |
-
|
| 486 |
temp_fasta.close()
|
| 487 |
return temp_fasta.name
|
| 488 |
-
|
| 489 |
except Exception as e:
|
| 490 |
-
logging.error(f"
|
| 491 |
return None
|
| 492 |
|
| 493 |
-
def build_maximum_likelihood_tree(
|
| 494 |
-
"""Build maximum likelihood
|
| 495 |
try:
|
| 496 |
-
# Check tool availability
|
| 497 |
mafft_available, iqtree_available, mafft_cmd, iqtree_cmd = check_tool_availability()
|
| 498 |
|
| 499 |
-
#
|
| 500 |
status_msg = "🔍 Checking dependencies...\n"
|
| 501 |
|
| 502 |
if not mafft_available:
|
|
@@ -509,20 +508,20 @@ def build_maximum_likelihood_tree(f_gene_sequence):
|
|
| 509 |
else:
|
| 510 |
status_msg += f"✅ IQ-TREE found: {iqtree_cmd}\n"
|
| 511 |
|
| 512 |
-
# If
|
| 513 |
if not mafft_available and not iqtree_available:
|
| 514 |
guide = install_dependencies_guide()
|
| 515 |
return False, f"{status_msg}\n{guide}", None, None
|
| 516 |
|
| 517 |
-
# If only one tool is missing, provide
|
| 518 |
if not mafft_available:
|
| 519 |
-
return False, f"{status_msg}\n❌ MAFFT
|
| 520 |
|
| 521 |
if not iqtree_available:
|
| 522 |
-
status_msg += "\n⚠
|
| 523 |
|
| 524 |
-
# Try
|
| 525 |
-
multi_fasta = create_multi_fasta_with_query(
|
| 526 |
if multi_fasta:
|
| 527 |
# Read sequences
|
| 528 |
sequences = {}
|
|
@@ -552,50 +551,50 @@ def build_maximum_likelihood_tree(f_gene_sequence):
|
|
| 552 |
else:
|
| 553 |
return False, f"{status_msg}❌ Failed to create input sequences", None, None
|
| 554 |
|
| 555 |
-
# Both tools available - proceed with
|
| 556 |
# Create output directory
|
| 557 |
output_dir = "ml_tree_output"
|
| 558 |
os.makedirs(output_dir, exist_ok=True)
|
| 559 |
|
| 560 |
-
# Step 1: Create multi-FASTA
|
| 561 |
-
logging.info("Creating multi-FASTA
|
| 562 |
-
multi_fasta = create_multi_fasta_with_query(
|
| 563 |
if not multi_fasta:
|
| 564 |
-
return False, f"{status_msg}❌ Failed to create
|
| 565 |
|
| 566 |
-
# Step 2: Run MAFFT
|
| 567 |
-
logging.info("Running MAFFT
|
| 568 |
aligned_fasta = os.path.join(output_dir, "aligned_sequences.fasta")
|
| 569 |
mafft_success, mafft_result = run_mafft_alignment(multi_fasta, aligned_fasta, mafft_cmd)
|
| 570 |
|
| 571 |
-
# Clean up
|
| 572 |
os.unlink(multi_fasta)
|
| 573 |
|
| 574 |
if not mafft_success:
|
| 575 |
return False, f"{status_msg}❌ MAFFT failed: {mafft_result}", None, None
|
| 576 |
|
| 577 |
-
# Step 3: Run IQ-TREE
|
| 578 |
-
logging.info("Running IQ-TREE
|
| 579 |
tree_prefix = os.path.join(output_dir, "ml_tree")
|
| 580 |
iqtree_success, iqtree_result = run_iqtree_analysis(aligned_fasta, tree_prefix, iqtree_cmd)
|
| 581 |
|
| 582 |
if not iqtree_success:
|
| 583 |
return False, f"{status_msg}❌ IQ-TREE failed: {iqtree_result}", aligned_fasta, None
|
| 584 |
|
| 585 |
-
# Step 4: Prepare
|
| 586 |
tree_file = iqtree_result
|
| 587 |
log_file = f"{tree_prefix}.log"
|
| 588 |
|
| 589 |
-
# Copy to standard names
|
| 590 |
-
standard_aligned = "
|
| 591 |
-
standard_tree = "
|
| 592 |
|
| 593 |
if os.path.exists(aligned_fasta):
|
| 594 |
shutil.copy2(aligned_fasta, standard_aligned)
|
| 595 |
if os.path.exists(tree_file):
|
| 596 |
shutil.copy2(tree_file, standard_tree)
|
| 597 |
|
| 598 |
-
success_msg = f"{status_msg}✅
|
| 599 |
success_msg += f"- Alignment: {os.path.basename(aligned_fasta)}\n"
|
| 600 |
success_msg += f"- Tree: {os.path.basename(tree_file)}\n"
|
| 601 |
|
|
@@ -603,76 +602,76 @@ def build_maximum_likelihood_tree(f_gene_sequence):
|
|
| 603 |
try:
|
| 604 |
with open(log_file, 'r') as f:
|
| 605 |
log_content = f.read()
|
| 606 |
-
# Extract model
|
| 607 |
if "Best-fit model:" in log_content:
|
| 608 |
model_lines = [line for line in log_content.split('\n') if "Best-fit model:" in line]
|
| 609 |
if model_lines:
|
| 610 |
success_msg += f"- {model_lines[0].strip()}\n"
|
| 611 |
except Exception as e:
|
| 612 |
-
logging.warning(f"Could not read log
|
| 613 |
|
| 614 |
-
logging.info("
|
| 615 |
return True, success_msg, aligned_fasta, tree_file
|
| 616 |
|
| 617 |
except Exception as e:
|
| 618 |
-
logging.error(f"ML tree
|
| 619 |
-
return False, f"ML tree
|
| 620 |
|
| 621 |
-
# --- Tree Analysis Function
|
| 622 |
def analyze_sequence_for_tree(sequence: str, matching_percentage: float) -> str:
|
| 623 |
"""
|
| 624 |
-
Analyze sequence and create phylogenetic tree
|
| 625 |
"""
|
| 626 |
try:
|
| 627 |
if not analyzer:
|
| 628 |
-
return "Error: Tree analyzer not initialized.
|
| 629 |
|
| 630 |
if not sequence:
|
| 631 |
-
return "Error:
|
| 632 |
|
| 633 |
if not (1 <= matching_percentage <= 99):
|
| 634 |
-
return "Error: Matching percentage must be
|
| 635 |
|
| 636 |
-
# Find query
|
| 637 |
if not analyzer.find_query_sequence(sequence):
|
| 638 |
-
return "Error: Invalid query
|
| 639 |
|
| 640 |
-
# Set
|
| 641 |
analyzer.matching_percentage = matching_percentage
|
| 642 |
|
| 643 |
# Find similar sequences
|
| 644 |
matched_ids, actual_percentage = analyzer.find_similar_sequences(matching_percentage)
|
| 645 |
|
| 646 |
if not matched_ids:
|
| 647 |
-
return f"No similar sequences
|
| 648 |
|
| 649 |
-
logging.info(f"Found {len(matched_ids)} similar sequences at {actual_percentage:.1f}%
|
| 650 |
|
| 651 |
-
# Build tree
|
| 652 |
tree_structure = analyzer.build_tree_structure(matched_ids)
|
| 653 |
if not tree_structure:
|
| 654 |
-
return "Error: Failed to build tree
|
| 655 |
|
| 656 |
# Create interactive tree
|
| 657 |
fig = analyzer.create_interactive_tree(matched_ids, actual_percentage)
|
| 658 |
if not fig:
|
| 659 |
-
return "Error: Failed to create
|
| 660 |
|
| 661 |
-
# Generate HTML
|
| 662 |
html_content = fig.to_html(full_html=True, include_plotlyjs='cdn')
|
| 663 |
|
| 664 |
-
# Save to output
|
| 665 |
output_dir = "output"
|
| 666 |
os.makedirs(output_dir, exist_ok=True)
|
| 667 |
|
| 668 |
-
#
|
| 669 |
safe_seq_name = re.sub(r'[^a-zA-Z0-9]', '_', sequence[:20])
|
| 670 |
html_filename = os.path.join(output_dir, f"tree_{safe_seq_name}_{matching_percentage}.html")
|
| 671 |
|
| 672 |
with open(html_filename, "w", encoding='utf-8') as f:
|
| 673 |
f.write(html_content)
|
| 674 |
|
| 675 |
-
logging.info(f"Tree HTML saved
|
| 676 |
|
| 677 |
return html_content
|
| 678 |
|
|
@@ -680,54 +679,51 @@ def analyze_sequence_for_tree(sequence: str, matching_percentage: float) -> str:
|
|
| 680 |
error_msg = f"Tree analysis error: {str(e)}"
|
| 681 |
logging.error(error_msg)
|
| 682 |
import traceback
|
| 683 |
-
logging.error(f"
|
| 684 |
return error_msg
|
| 685 |
|
| 686 |
# --- Keras Prediction ---
|
| 687 |
def predict_with_keras(sequence):
|
| 688 |
try:
|
| 689 |
if not keras_model or not kmer_to_index:
|
| 690 |
-
return f"Keras model
|
| 691 |
|
| 692 |
if len(sequence) < 6:
|
| 693 |
-
return "Sequence too short
|
| 694 |
|
| 695 |
# Generate k-mers
|
| 696 |
kmers = [sequence[i:i+6] for i in range(len(sequence) - 5)]
|
| 697 |
|
| 698 |
-
# Convert
|
| 699 |
kmer_indices = []
|
| 700 |
for kmer in kmers:
|
| 701 |
if kmer in kmer_to_index:
|
| 702 |
kmer_indices.append(kmer_to_index[kmer])
|
| 703 |
else:
|
| 704 |
-
# Handle unknown k-mers with a default value (e.g., 0 or len(kmer_to_index))
|
| 705 |
kmer_indices.append(0)
|
| 706 |
|
| 707 |
if not kmer_indices:
|
| 708 |
-
return "No valid k-mers
|
| 709 |
|
| 710 |
-
# Pad
|
| 711 |
-
max_length = 1000
|
| 712 |
if len(kmer_indices) > max_length:
|
| 713 |
kmer_indices = kmer_indices[:max_length]
|
| 714 |
else:
|
| 715 |
kmer_indices.extend([0] * (max_length - len(kmer_indices)))
|
| 716 |
|
| 717 |
-
# Convert to
|
| 718 |
input_array = np.array([kmer_indices])
|
| 719 |
|
| 720 |
-
#
|
| 721 |
prediction = keras_model.predict(input_array, verbose=0)
|
| 722 |
|
| 723 |
-
# Format
|
| 724 |
if prediction.shape[1] == 1:
|
| 725 |
-
# Binary classification
|
| 726 |
probability = float(prediction[0][0])
|
| 727 |
result = "Positive" if probability > 0.5 else "Negative"
|
| 728 |
return f"Keras Prediction: {result} (confidence: {probability:.3f})"
|
| 729 |
else:
|
| 730 |
-
# Multi-class classification
|
| 731 |
predicted_class = np.argmax(prediction[0])
|
| 732 |
confidence = float(np.max(prediction[0]))
|
| 733 |
return f"Keras Prediction: Class {predicted_class} (confidence: {confidence:.3f})"
|
|
@@ -740,9 +736,8 @@ def predict_with_keras(sequence):
|
|
| 740 |
def predict_with_boundary_model(sequence):
|
| 741 |
try:
|
| 742 |
if not boundary_model:
|
| 743 |
-
return f"Boundary model
|
| 744 |
|
| 745 |
-
# Use the boundary model predictor
|
| 746 |
result = boundary_model.predict(sequence)
|
| 747 |
return f"Boundary Prediction: {result}"
|
| 748 |
|
|
@@ -750,30 +745,30 @@ def predict_with_boundary_model(sequence):
|
|
| 750 |
logging.error(f"Boundary prediction error: {e}")
|
| 751 |
return f"Boundary prediction failed: {str(e)}"
|
| 752 |
|
| 753 |
-
# --- Combined Prediction
|
| 754 |
def predict_sequence(sequence, use_keras=True, use_boundary=True):
|
| 755 |
-
"""Combined prediction
|
| 756 |
results = []
|
| 757 |
|
| 758 |
if not sequence:
|
| 759 |
-
return "
|
| 760 |
|
| 761 |
# Clean sequence
|
| 762 |
sequence = sequence.upper().strip()
|
| 763 |
sequence = ''.join(c for c in sequence if c in 'ATCGN')
|
| 764 |
|
| 765 |
if len(sequence) < 10:
|
| 766 |
-
return "Sequence too short
|
| 767 |
|
| 768 |
-
results.append(f"Analyzing sequence
|
| 769 |
results.append("-" * 50)
|
| 770 |
|
| 771 |
-
# Keras
|
| 772 |
if use_keras:
|
| 773 |
keras_result = predict_with_keras(sequence)
|
| 774 |
results.append(f"🧠 {keras_result}")
|
| 775 |
|
| 776 |
-
# Boundary
|
| 777 |
if use_boundary:
|
| 778 |
boundary_result = predict_with_boundary_model(sequence)
|
| 779 |
results.append(f"🎯 {boundary_result}")
|
|
@@ -782,15 +777,15 @@ def predict_sequence(sequence, use_keras=True, use_boundary=True):
|
|
| 782 |
|
| 783 |
# --- File Upload Handler ---
|
| 784 |
def process_uploaded_file(file):
|
| 785 |
-
"""Process
|
| 786 |
try:
|
| 787 |
if file is None:
|
| 788 |
return "No file uploaded."
|
| 789 |
|
| 790 |
-
# Read
|
| 791 |
content = file.read().decode('utf-8')
|
| 792 |
|
| 793 |
-
# Parse FASTA
|
| 794 |
sequences = []
|
| 795 |
current_seq = ""
|
| 796 |
current_header = ""
|
|
@@ -810,24 +805,23 @@ def process_uploaded_file(file):
|
|
| 810 |
sequences.append((current_header, current_seq))
|
| 811 |
|
| 812 |
if not sequences:
|
| 813 |
-
return "No valid sequences
|
| 814 |
|
| 815 |
# Process sequences
|
| 816 |
results = []
|
| 817 |
-
results.append(f"Found {len(sequences)} sequences
|
| 818 |
results.append("=" * 60)
|
| 819 |
|
| 820 |
-
for i, (header, seq) in enumerate(sequences[:5]):
|
| 821 |
results.append(f"\nSequence {i+1}: {header}")
|
| 822 |
results.append(f"Length: {len(seq)} bp")
|
| 823 |
|
| 824 |
-
# Run predictions
|
| 825 |
pred_result = predict_sequence(seq)
|
| 826 |
results.append(pred_result)
|
| 827 |
results.append("-" * 40)
|
| 828 |
|
| 829 |
if len(sequences) > 5:
|
| 830 |
-
results.append(f"\n... and {len(sequences) - 5} more
|
| 831 |
|
| 832 |
return "\n".join(results)
|
| 833 |
|
|
@@ -837,25 +831,25 @@ def process_uploaded_file(file):
|
|
| 837 |
|
| 838 |
# --- Gradio Interface ---
|
| 839 |
def create_gradio_interface():
|
| 840 |
-
"""Create
|
| 841 |
|
| 842 |
with gr.Blocks(title="F Gene Analysis Tool", theme=gr.themes.Soft()) as demo:
|
| 843 |
gr.Markdown("""
|
| 844 |
# 🧬 F Gene Analysis Tool
|
| 845 |
|
| 846 |
-
Advanced
|
| 847 |
""")
|
| 848 |
|
| 849 |
with gr.Tabs():
|
| 850 |
# Tab 1: Sequence Prediction
|
| 851 |
with gr.TabItem("🔬 Sequence Prediction"):
|
| 852 |
-
gr.Markdown("### Enter
|
| 853 |
|
| 854 |
with gr.Row():
|
| 855 |
with gr.Column(scale=2):
|
| 856 |
sequence_input = gr.Textbox(
|
| 857 |
label="DNA Sequence",
|
| 858 |
-
placeholder="Enter
|
| 859 |
lines=5,
|
| 860 |
max_lines=10
|
| 861 |
)
|
|
@@ -864,12 +858,12 @@ def create_gradio_interface():
|
|
| 864 |
use_keras_check = gr.Checkbox(
|
| 865 |
label="Use Keras Model",
|
| 866 |
value=True,
|
| 867 |
-
info="Deep learning
|
| 868 |
)
|
| 869 |
use_boundary_check = gr.Checkbox(
|
| 870 |
label="Use Boundary Model",
|
| 871 |
value=True,
|
| 872 |
-
info="
|
| 873 |
)
|
| 874 |
|
| 875 |
predict_btn = gr.Button("🧠 Predict", variant="primary")
|
|
@@ -877,14 +871,14 @@ def create_gradio_interface():
|
|
| 877 |
with gr.Column(scale=1):
|
| 878 |
gr.Markdown("""
|
| 879 |
**Tips:**
|
| 880 |
-
- Use
|
| 881 |
-
-
|
| 882 |
-
- Longer sequences
|
| 883 |
-
- N
|
| 884 |
""")
|
| 885 |
|
| 886 |
prediction_output = gr.Textbox(
|
| 887 |
-
label="
|
| 888 |
lines=8,
|
| 889 |
interactive=False
|
| 890 |
)
|
|
@@ -895,20 +889,20 @@ def create_gradio_interface():
|
|
| 895 |
outputs=prediction_output
|
| 896 |
)
|
| 897 |
|
| 898 |
-
# Tab 2: File
|
| 899 |
with gr.TabItem("📁 File Analysis"):
|
| 900 |
-
gr.Markdown("### Upload FASTA file
|
| 901 |
|
| 902 |
file_input = gr.File(
|
| 903 |
-
label="Upload FASTA
|
| 904 |
file_types=[".fasta", ".fa", ".fas", ".txt"],
|
| 905 |
file_count="single"
|
| 906 |
)
|
| 907 |
|
| 908 |
-
process_file_btn = gr.Button("📊 Process
|
| 909 |
|
| 910 |
file_output = gr.Textbox(
|
| 911 |
-
label="
|
| 912 |
lines=15,
|
| 913 |
interactive=False
|
| 914 |
)
|
|
@@ -919,15 +913,15 @@ def create_gradio_interface():
|
|
| 919 |
outputs=file_output
|
| 920 |
)
|
| 921 |
|
| 922 |
-
# Tab 3: Phylogenetic
|
| 923 |
with gr.TabItem("🌳 Phylogenetic Tree"):
|
| 924 |
-
gr.Markdown("###
|
| 925 |
|
| 926 |
with gr.Row():
|
| 927 |
with gr.Column():
|
| 928 |
tree_sequence_input = gr.Textbox(
|
| 929 |
label="Query Sequence",
|
| 930 |
-
placeholder="Enter DNA
|
| 931 |
lines=5
|
| 932 |
)
|
| 933 |
|
|
@@ -937,7 +931,7 @@ def create_gradio_interface():
|
|
| 937 |
value=80,
|
| 938 |
step=1,
|
| 939 |
label="Similarity Threshold (%)",
|
| 940 |
-
info="
|
| 941 |
)
|
| 942 |
|
| 943 |
with gr.Row():
|
|
@@ -946,17 +940,16 @@ def create_gradio_interface():
|
|
| 946 |
place_sequence_btn = gr.Button("📍 Place in Tree", variant="secondary")
|
| 947 |
|
| 948 |
tree_output = gr.HTML(
|
| 949 |
-
label="
|
| 950 |
-
value="<p>Tree visualization
|
| 951 |
)
|
| 952 |
|
| 953 |
tree_status = gr.Textbox(
|
| 954 |
-
label="
|
| 955 |
lines=5,
|
| 956 |
interactive=False
|
| 957 |
)
|
| 958 |
|
| 959 |
-
# Event handlers for tree analysis
|
| 960 |
tree_analysis_btn.click(
|
| 961 |
fn=analyze_sequence_for_tree,
|
| 962 |
inputs=[tree_sequence_input, similarity_slider],
|
|
@@ -982,83 +975,44 @@ def create_gradio_interface():
|
|
| 982 |
inputs=tree_sequence_input,
|
| 983 |
outputs=tree_status
|
| 984 |
)
|
| 985 |
-
|
| 986 |
-
# Tab 4: Help & Information
|
| 987 |
-
with gr.TabItem("ℹ️ Help"):
|
| 988 |
-
gr.Markdown("""
|
| 989 |
-
## 📖 User Guide
|
| 990 |
-
|
| 991 |
-
### Sequence Prediction
|
| 992 |
-
- **Input**: DNA sequences in standard format (A, T, C, G)
|
| 993 |
-
- **Models**:
|
| 994 |
-
- Keras Model: Deep learning classification
|
| 995 |
-
- Boundary Model: Gene boundary detection
|
| 996 |
-
- **Output**: Classification results with confidence scores
|
| 997 |
-
|
| 998 |
-
### File Analysis
|
| 999 |
-
- **Supported formats**: FASTA (.fasta, .fa, .fas, .txt)
|
| 1000 |
-
- **Batch processing**: Analyzes multiple sequences at once
|
| 1001 |
-
- **Limitations**: Processes first 5 sequences for performance
|
| 1002 |
-
|
| 1003 |
-
### Phylogenetic Analysis
|
| 1004 |
-
- **Tree Building**: Creates interactive phylogenetic trees
|
| 1005 |
-
- **ML Trees**: Maximum likelihood trees (requires MAFFT + IQ-TREE)
|
| 1006 |
-
- **Sequence Placement**: Places query sequences in reference trees
|
| 1007 |
-
- **Similarity Control**: Adjustable similarity thresholds
|
| 1008 |
-
|
| 1009 |
-
### Dependencies
|
| 1010 |
-
For phylogenetic analysis, install:
|
| 1011 |
-
- **MAFFT**: Multiple sequence alignment
|
| 1012 |
-
- **IQ-TREE**: Maximum likelihood phylogenetic inference
|
| 1013 |
-
|
| 1014 |
-
### Data Sources
|
| 1015 |
-
- Pre-trained models from Hugging Face Hub
|
| 1016 |
-
- Reference alignments and trees included
|
| 1017 |
-
- CSV data for sequence similarity analysis
|
| 1018 |
-
|
| 1019 |
-
---
|
| 1020 |
-
|
| 1021 |
-
**Note**: Some features may be limited based on available dependencies and models.
|
| 1022 |
-
""")
|
| 1023 |
|
| 1024 |
# Footer
|
| 1025 |
gr.Markdown("""
|
| 1026 |
---
|
| 1027 |
-
**F Gene Analysis Tool** -
|
| 1028 |
""")
|
| 1029 |
|
| 1030 |
return demo
|
| 1031 |
|
| 1032 |
# --- Main Application ---
|
| 1033 |
def main():
|
| 1034 |
-
"""Main
|
| 1035 |
try:
|
| 1036 |
-
# Log startup
|
| 1037 |
logging.info("Starting F Gene Analysis Tool")
|
| 1038 |
-
logging.info(f"Boundary model
|
| 1039 |
-
logging.info(f"Keras model
|
| 1040 |
-
logging.info(f"Tree analyzer
|
| 1041 |
|
| 1042 |
-
# Check
|
| 1043 |
mafft_available, iqtree_available, mafft_cmd, iqtree_cmd = check_tool_availability()
|
| 1044 |
-
logging.info(f"MAFFT
|
| 1045 |
-
logging.info(f"IQ-TREE
|
| 1046 |
|
| 1047 |
-
# Create and launch
|
| 1048 |
demo = create_gradio_interface()
|
| 1049 |
|
| 1050 |
-
# Launch
|
| 1051 |
demo.launch(
|
| 1052 |
server_name="0.0.0.0",
|
| 1053 |
server_port=7860,
|
| 1054 |
-
share=False,
|
| 1055 |
-
show_error=True
|
| 1056 |
-
enable_queue=True
|
| 1057 |
)
|
| 1058 |
|
| 1059 |
except Exception as e:
|
| 1060 |
-
logging.error(f"
|
| 1061 |
-
print(f"Error starting
|
| 1062 |
return 1
|
| 1063 |
|
| 1064 |
return 0
|
|
|
|
| 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']).strip()
|
| 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 reference file")
|
|
|
|
| 485 |
temp_fasta.close()
|
| 486 |
return temp_fasta.name
|
| 487 |
+
|
| 488 |
except Exception as e:
|
| 489 |
+
logging.error(f"Error creating multi-FASTA: {str(e)}")
|
| 490 |
return None
|
| 491 |
|
| 492 |
+
def build_maximum_likelihood_tree(f_sequence):
|
| 493 |
+
"""Build a maximum likelihood tree with comprehensive fallback options"""
|
| 494 |
try:
|
| 495 |
+
# Check tool availability
|
| 496 |
mafft_available, iqtree_available, mafft_cmd, iqtree_cmd = check_tool_availability()
|
| 497 |
|
| 498 |
+
# Status message
|
| 499 |
status_msg = "🔍 Checking dependencies...\n"
|
| 500 |
|
| 501 |
if not mafft_available:
|
|
|
|
| 508 |
else:
|
| 509 |
status_msg += f"✅ IQ-TREE found: {iqtree_cmd}\n"
|
| 510 |
|
| 511 |
+
# If no tools are available, provide installation guide
|
| 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. Install MAFFT.", None, None
|
| 519 |
|
| 520 |
if not iqtree_available:
|
| 521 |
+
status_msg += "\n⚠ IQ-TREE unavailable. Attempting simple tree...\n"
|
| 522 |
|
| 523 |
+
# Try simple tree as fallback
|
| 524 |
+
multi_fasta = create_multi_fasta_with_query(f_sequence)
|
| 525 |
if multi_fasta:
|
| 526 |
# Read sequences
|
| 527 |
sequences = {}
|
|
|
|
| 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(f_sequence)
|
| 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 outputs
|
| 585 |
tree_file = iqtree_result
|
| 586 |
log_file = f"{tree_prefix}.log"
|
| 587 |
|
| 588 |
+
# Copy to standard names
|
| 589 |
+
standard_aligned = "f_sequences_aligned.fasta"
|
| 590 |
+
standard_tree = "f_sequences.phy.treefile"
|
| 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}✅ ML tree built!\n"
|
| 598 |
success_msg += f"- Alignment: {os.path.basename(aligned_fasta)}\n"
|
| 599 |
success_msg += f"- Tree: {os.path.basename(tree_file)}\n"
|
| 600 |
|
|
|
|
| 602 |
try:
|
| 603 |
with open(log_file, 'r') as f:
|
| 604 |
log_content = f.read()
|
| 605 |
+
# Extract model info
|
| 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("ML tree construction completed")
|
| 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. Check CSV data."
|
| 628 |
|
| 629 |
if not sequence:
|
| 630 |
+
return "Error: Provide a sequence."
|
| 631 |
|
| 632 |
if not (1 <= matching_percentage <= 99):
|
| 633 |
+
return "Error: Matching percentage must be 1-99."
|
| 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. Lower threshold."
|
| 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 |
+
# Safe filename
|
| 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: {html_filename}")
|
| 675 |
|
| 676 |
return html_content
|
| 677 |
|
|
|
|
| 679 |
error_msg = f"Tree analysis error: {str(e)}"
|
| 680 |
logging.error(error_msg)
|
| 681 |
import traceback
|
| 682 |
+
logging.error(f"Traceback: {traceback.format_exc()}")
|
| 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 unavailable. Input: {sequence[:100]}..."
|
| 690 |
|
| 691 |
if len(sequence) < 6:
|
| 692 |
+
return "Sequence too short (min 6 nucleotides)."
|
| 693 |
|
| 694 |
# Generate k-mers
|
| 695 |
kmers = [sequence[i:i+6] for i in range(len(sequence) - 5)]
|
| 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 |
+
# Predict
|
| 719 |
prediction = keras_model.predict(input_array, verbose=0)
|
| 720 |
|
| 721 |
+
# Format results
|
| 722 |
if prediction.shape[1] == 1:
|
|
|
|
| 723 |
probability = float(prediction[0][0])
|
| 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})"
|
|
|
|
| 736 |
def predict_with_boundary_model(sequence):
|
| 737 |
try:
|
| 738 |
if not boundary_model:
|
| 739 |
+
return f"Boundary model unavailable. Input: {sequence[:100]}..."
|
| 740 |
|
|
|
|
| 741 |
result = boundary_model.predict(sequence)
|
| 742 |
return f"Boundary Prediction: {result}"
|
| 743 |
|
|
|
|
| 745 |
logging.error(f"Boundary prediction error: {e}")
|
| 746 |
return f"Boundary prediction failed: {str(e)}"
|
| 747 |
|
| 748 |
+
# --- Combined Prediction ---
|
| 749 |
def predict_sequence(sequence, use_keras=True, use_boundary=True):
|
| 750 |
+
"""Combined prediction"""
|
| 751 |
results = []
|
| 752 |
|
| 753 |
if not sequence:
|
| 754 |
+
return "Provide a DNA sequence."
|
| 755 |
|
| 756 |
# Clean sequence
|
| 757 |
sequence = sequence.upper().strip()
|
| 758 |
sequence = ''.join(c for c in sequence if c in 'ATCGN')
|
| 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}")
|
|
|
|
| 777 |
|
| 778 |
# --- File Upload Handler ---
|
| 779 |
def process_uploaded_file(file):
|
| 780 |
+
"""Process FASTA file"""
|
| 781 |
try:
|
| 782 |
if file is None:
|
| 783 |
return "No file uploaded."
|
| 784 |
|
| 785 |
+
# Read content
|
| 786 |
content = file.read().decode('utf-8')
|
| 787 |
|
| 788 |
+
# Parse FASTA
|
| 789 |
sequences = []
|
| 790 |
current_seq = ""
|
| 791 |
current_header = ""
|
|
|
|
| 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 |
+
for i, (header, seq) in enumerate(sequences[:5]):
|
| 816 |
results.append(f"\nSequence {i+1}: {header}")
|
| 817 |
results.append(f"Length: {len(seq)} bp")
|
| 818 |
|
|
|
|
| 819 |
pred_result = predict_sequence(seq)
|
| 820 |
results.append(pred_result)
|
| 821 |
results.append("-" * 40)
|
| 822 |
|
| 823 |
if len(sequences) > 5:
|
| 824 |
+
results.append(f"\n... and {len(sequences) - 5} more.")
|
| 825 |
|
| 826 |
return "\n".join(results)
|
| 827 |
|
|
|
|
| 831 |
|
| 832 |
# --- Gradio Interface ---
|
| 833 |
def create_gradio_interface():
|
| 834 |
+
"""Create Gradio interface"""
|
| 835 |
|
| 836 |
with gr.Blocks(title="F Gene Analysis Tool", theme=gr.themes.Soft()) as demo:
|
| 837 |
gr.Markdown("""
|
| 838 |
# 🧬 F Gene Analysis Tool
|
| 839 |
|
| 840 |
+
Advanced tool for F gene sequence analysis with prediction and phylogenetics.
|
| 841 |
""")
|
| 842 |
|
| 843 |
with gr.Tabs():
|
| 844 |
# Tab 1: Sequence Prediction
|
| 845 |
with gr.TabItem("🔬 Sequence Prediction"):
|
| 846 |
+
gr.Markdown("### Enter DNA sequence")
|
| 847 |
|
| 848 |
with gr.Row():
|
| 849 |
with gr.Column(scale=2):
|
| 850 |
sequence_input = gr.Textbox(
|
| 851 |
label="DNA Sequence",
|
| 852 |
+
placeholder="Enter DNA sequence (ATCG)...",
|
| 853 |
lines=5,
|
| 854 |
max_lines=10
|
| 855 |
)
|
|
|
|
| 858 |
use_keras_check = gr.Checkbox(
|
| 859 |
label="Use Keras Model",
|
| 860 |
value=True,
|
| 861 |
+
info="Deep learning classification"
|
| 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")
|
|
|
|
| 871 |
with gr.Column(scale=1):
|
| 872 |
gr.Markdown("""
|
| 873 |
**Tips:**
|
| 874 |
+
- Use A, T, C, G
|
| 875 |
+
- Min length: 10 bp
|
| 876 |
+
- Longer sequences improve predictions
|
| 877 |
+
- N allowed for unknowns
|
| 878 |
""")
|
| 879 |
|
| 880 |
prediction_output = gr.Textbox(
|
| 881 |
+
label="Results",
|
| 882 |
lines=8,
|
| 883 |
interactive=False
|
| 884 |
)
|
|
|
|
| 889 |
outputs=prediction_output
|
| 890 |
)
|
| 891 |
|
| 892 |
+
# Tab 2: File Analysis
|
| 893 |
with gr.TabItem("📁 File Analysis"):
|
| 894 |
+
gr.Markdown("### Upload FASTA file")
|
| 895 |
|
| 896 |
file_input = gr.File(
|
| 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 |
)
|
|
|
|
| 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 |
|
|
|
|
| 931 |
value=80,
|
| 932 |
step=1,
|
| 933 |
label="Similarity Threshold (%)",
|
| 934 |
+
info="Min similarity for tree"
|
| 935 |
)
|
| 936 |
|
| 937 |
with gr.Row():
|
|
|
|
| 940 |
place_sequence_btn = gr.Button("📍 Place in Tree", variant="secondary")
|
| 941 |
|
| 942 |
tree_output = gr.HTML(
|
| 943 |
+
label="Tree",
|
| 944 |
+
value="<p>Tree visualization here.</p>"
|
| 945 |
)
|
| 946 |
|
| 947 |
tree_status = gr.Textbox(
|
| 948 |
+
label="Status",
|
| 949 |
lines=5,
|
| 950 |
interactive=False
|
| 951 |
)
|
| 952 |
|
|
|
|
| 953 |
tree_analysis_btn.click(
|
| 954 |
fn=analyze_sequence_for_tree,
|
| 955 |
inputs=[tree_sequence_input, similarity_slider],
|
|
|
|
| 975 |
inputs=tree_sequence_input,
|
| 976 |
outputs=tree_status
|
| 977 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 978 |
|
| 979 |
# Footer
|
| 980 |
gr.Markdown("""
|
| 981 |
---
|
| 982 |
+
**F Gene Analysis Tool** - Sequence analysis with ML and phylogenetics
|
| 983 |
""")
|
| 984 |
|
| 985 |
return demo
|
| 986 |
|
| 987 |
# --- Main Application ---
|
| 988 |
def main():
|
| 989 |
+
"""Main entry point"""
|
| 990 |
try:
|
| 991 |
+
# Log startup
|
| 992 |
logging.info("Starting F Gene Analysis Tool")
|
| 993 |
+
logging.info(f"Boundary model: {boundary_model is not None}")
|
| 994 |
+
logging.info(f"Keras model: {keras_model is not None}")
|
| 995 |
+
logging.info(f"Tree analyzer: {analyzer is not None}")
|
| 996 |
|
| 997 |
+
# Check tools
|
| 998 |
mafft_available, iqtree_available, mafft_cmd, iqtree_cmd = check_tool_availability()
|
| 999 |
+
logging.info(f"MAFFT: {mafft_available}")
|
| 1000 |
+
logging.info(f"IQ-TREE: {iqtree_available}")
|
| 1001 |
|
| 1002 |
+
# Create and launch interface
|
| 1003 |
demo = create_gradio_interface()
|
| 1004 |
|
| 1005 |
+
# Launch
|
| 1006 |
demo.launch(
|
| 1007 |
server_name="0.0.0.0",
|
| 1008 |
server_port=7860,
|
| 1009 |
+
share=False,
|
| 1010 |
+
show_error=True
|
|
|
|
| 1011 |
)
|
| 1012 |
|
| 1013 |
except Exception as e:
|
| 1014 |
+
logging.error(f"Startup failed: {e}")
|
| 1015 |
+
print(f"Error starting: {e}")
|
| 1016 |
return 1
|
| 1017 |
|
| 1018 |
return 0
|