Spaces:
No application file
No application file
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,7 +13,10 @@ import os
|
|
| 13 |
# Configure logging
|
| 14 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
| 15 |
|
| 16 |
-
# --------- Load Models ---------
|
|
|
|
|
|
|
|
|
|
| 17 |
try:
|
| 18 |
boundary_model = GenePredictor("best_boundary_aware_model.pth")
|
| 19 |
keras_model = load_model("best_model.keras")
|
|
@@ -21,14 +24,15 @@ try:
|
|
| 21 |
kmer_to_index = pickle.load(f)
|
| 22 |
logging.info("Models and kmer_to_index loaded successfully from Space")
|
| 23 |
except FileNotFoundError as e:
|
| 24 |
-
logging.
|
| 25 |
-
raise
|
| 26 |
except Exception as e:
|
| 27 |
-
logging.
|
| 28 |
-
raise
|
| 29 |
|
| 30 |
# --------- Utilities ---------
|
| 31 |
def predict_with_keras(sequence):
|
|
|
|
|
|
|
|
|
|
| 32 |
try:
|
| 33 |
kmers = [sequence[i:i+6] for i in range(len(sequence)-5)]
|
| 34 |
indices = [kmer_to_index.get(kmer, 0) for kmer in kmers]
|
|
@@ -76,24 +80,24 @@ def run_mafft_and_iqtree(sequence):
|
|
| 76 |
|
| 77 |
def run_full_pipeline(dna_input):
|
| 78 |
try:
|
| 79 |
-
# Step 1: Boundary-Aware Prediction
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
|
|
|
|
|
|
| 83 |
logging.info(f"Boundary model output: {step1_out}")
|
| 84 |
|
| 85 |
-
# Step 2: Keras Prediction
|
| 86 |
-
step2_out =
|
|
|
|
|
|
|
| 87 |
logging.info(f"Keras model output: {step2_out}")
|
| 88 |
|
| 89 |
-
# Step 3a: Save for
|
| 90 |
-
fasta_status = save_to_fasta("Predicted_Seq", step2_out, "f_gene_sequences_aligned.fasta")
|
| 91 |
csv_status = save_to_csv(step2_out, "f_gene_clean_dataset.csv")
|
| 92 |
|
| 93 |
-
# Step 3b: Run
|
| 94 |
-
mafft_status = run_mafft_and_iqtree(step2_out)
|
| 95 |
-
|
| 96 |
-
# Step 3c: Run ML Simplified Tree using PhylogeneticTreeAnalyzer
|
| 97 |
analyzer = ml_simplified_tree.PhylogeneticTreeAnalyzer()
|
| 98 |
if not analyzer.load_data("f_gene_clean_dataset.csv"):
|
| 99 |
ml_output = "Error: Failed to load data"
|
|
@@ -103,16 +107,18 @@ def run_full_pipeline(dna_input):
|
|
| 103 |
else:
|
| 104 |
matched_ids, actual_percentage = analyzer.find_similar_sequences(analyzer.matching_percentage)
|
| 105 |
analyzer.create_interactive_tree(matched_ids, actual_percentage)
|
| 106 |
-
ml_output = f"Tree generated and saved as phylogenetic_tree_normalized_horizontal.html"
|
| 107 |
-
|
|
|
|
|
|
|
| 108 |
html_file = "phylogenetic_tree_normalized_horizontal.html" if os.path.exists("phylogenetic_tree_normalized_horizontal.html") else None
|
| 109 |
|
| 110 |
return {
|
| 111 |
"Boundary Model Output": step1_out,
|
| 112 |
"Keras Model Output": step2_out,
|
| 113 |
-
"FASTA Save Status":
|
| 114 |
"CSV Save Status": csv_status,
|
| 115 |
-
"MAFFT + IQTree Status":
|
| 116 |
"Maximum Likelihood Tree Output": ml_output,
|
| 117 |
"Tree HTML File": html_file
|
| 118 |
}
|
|
@@ -131,18 +137,16 @@ def run_full_pipeline(dna_input):
|
|
| 131 |
# --------- Gradio Interface and API ---------
|
| 132 |
with gr.Blocks() as gr_interface:
|
| 133 |
gr.Markdown("# Sequential Phylogenetic Inference Pipeline")
|
| 134 |
-
gr.Markdown("This pipeline
|
| 135 |
|
| 136 |
-
dna_input = gr.Textbox(label="Input DNA Sequence")
|
| 137 |
-
submit_button = gr.Button("
|
| 138 |
|
| 139 |
boundary_output = gr.Textbox(label="Boundary Model Output")
|
| 140 |
keras_output = gr.Textbox(label="Keras Model Output")
|
| 141 |
-
fasta_status = gr.Textbox(label="FASTA Save Status")
|
| 142 |
csv_status = gr.Textbox(label="CSV Save Status")
|
| 143 |
-
mafft_status = gr.Textbox(label="MAFFT + IQTree Status")
|
| 144 |
ml_output = gr.Textbox(label="Maximum Likelihood Tree Output")
|
| 145 |
-
tree_download = gr.File(label="Download Tree (HTML)")
|
| 146 |
|
| 147 |
submit_button.click(
|
| 148 |
fn=run_full_pipeline,
|
|
@@ -150,9 +154,7 @@ with gr.Blocks() as gr_interface:
|
|
| 150 |
outputs=[
|
| 151 |
boundary_output,
|
| 152 |
keras_output,
|
| 153 |
-
fasta_status,
|
| 154 |
csv_status,
|
| 155 |
-
mafft_status,
|
| 156 |
ml_output,
|
| 157 |
tree_download
|
| 158 |
]
|
|
|
|
| 13 |
# Configure logging
|
| 14 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
| 15 |
|
| 16 |
+
# --------- Load Models (Optional) ---------
|
| 17 |
+
boundary_model = None
|
| 18 |
+
keras_model = None
|
| 19 |
+
kmer_to_index = None
|
| 20 |
try:
|
| 21 |
boundary_model = GenePredictor("best_boundary_aware_model.pth")
|
| 22 |
keras_model = load_model("best_model.keras")
|
|
|
|
| 24 |
kmer_to_index = pickle.load(f)
|
| 25 |
logging.info("Models and kmer_to_index loaded successfully from Space")
|
| 26 |
except FileNotFoundError as e:
|
| 27 |
+
logging.warning(f"Model file not found in Space: {e}. Proceeding without Boundary-Aware or Keras models.")
|
|
|
|
| 28 |
except Exception as e:
|
| 29 |
+
logging.warning(f"Error loading models or files: {e}. Proceeding without Boundary-Aware or Keras models.")
|
|
|
|
| 30 |
|
| 31 |
# --------- Utilities ---------
|
| 32 |
def predict_with_keras(sequence):
|
| 33 |
+
global keras_model, kmer_to_index
|
| 34 |
+
if keras_model is None or kmer_to_index is None:
|
| 35 |
+
return f"Skipped (Models not loaded): {sequence}"
|
| 36 |
try:
|
| 37 |
kmers = [sequence[i:i+6] for i in range(len(sequence)-5)]
|
| 38 |
indices = [kmer_to_index.get(kmer, 0) for kmer in kmers]
|
|
|
|
| 80 |
|
| 81 |
def run_full_pipeline(dna_input):
|
| 82 |
try:
|
| 83 |
+
# Step 1: Boundary-Aware Prediction (Optional)
|
| 84 |
+
step1_out = dna_input # Fallback to input if models missing
|
| 85 |
+
if boundary_model:
|
| 86 |
+
predictions, probs, confidence = boundary_model.predict(dna_input)
|
| 87 |
+
gene_regions = boundary_model.extract_gene_regions(predictions, dna_input)
|
| 88 |
+
step1_out = gene_regions[0]["sequence"] if gene_regions else dna_input
|
| 89 |
logging.info(f"Boundary model output: {step1_out}")
|
| 90 |
|
| 91 |
+
# Step 2: Keras Prediction (Optional)
|
| 92 |
+
step2_out = step1_out # Fallback to step1_out if models missing
|
| 93 |
+
if keras_model and kmer_to_index:
|
| 94 |
+
step2_out = predict_with_keras(step1_out)
|
| 95 |
logging.info(f"Keras model output: {step2_out}")
|
| 96 |
|
| 97 |
+
# Step 3a: Save for ML Simplified Tree
|
|
|
|
| 98 |
csv_status = save_to_csv(step2_out, "f_gene_clean_dataset.csv")
|
| 99 |
|
| 100 |
+
# Step 3b: Run ML Simplified Tree using PhylogeneticTreeAnalyzer
|
|
|
|
|
|
|
|
|
|
| 101 |
analyzer = ml_simplified_tree.PhylogeneticTreeAnalyzer()
|
| 102 |
if not analyzer.load_data("f_gene_clean_dataset.csv"):
|
| 103 |
ml_output = "Error: Failed to load data"
|
|
|
|
| 107 |
else:
|
| 108 |
matched_ids, actual_percentage = analyzer.find_similar_sequences(analyzer.matching_percentage)
|
| 109 |
analyzer.create_interactive_tree(matched_ids, actual_percentage)
|
| 110 |
+
ml_output = f"Tree generated and saved as phylogenetic_tree_normalized_horizontal.html"
|
| 111 |
+
if not os.path.exists("phylogenetic_tree_normalized_horizontal.html"):
|
| 112 |
+
ml_output += " (File not found after generation)"
|
| 113 |
+
|
| 114 |
html_file = "phylogenetic_tree_normalized_horizontal.html" if os.path.exists("phylogenetic_tree_normalized_horizontal.html") else None
|
| 115 |
|
| 116 |
return {
|
| 117 |
"Boundary Model Output": step1_out,
|
| 118 |
"Keras Model Output": step2_out,
|
| 119 |
+
"FASTA Save Status": "N/A (Skipped due to model issue)" if not boundary_model else "N/A",
|
| 120 |
"CSV Save Status": csv_status,
|
| 121 |
+
"MAFFT + IQTree Status": "N/A (Skipped due to model issue)" if not boundary_model else "N/A",
|
| 122 |
"Maximum Likelihood Tree Output": ml_output,
|
| 123 |
"Tree HTML File": html_file
|
| 124 |
}
|
|
|
|
| 137 |
# --------- Gradio Interface and API ---------
|
| 138 |
with gr.Blocks() as gr_interface:
|
| 139 |
gr.Markdown("# Sequential Phylogenetic Inference Pipeline")
|
| 140 |
+
gr.Markdown("This pipeline generates a phylogenetic tree. Model-based steps are optional if files are missing.")
|
| 141 |
|
| 142 |
+
dna_input = gr.Textbox(label="Input DNA Sequence", placeholder="Enter DNA sequence (e.g., ATGCGTAACTAGCTAGCTAA)")
|
| 143 |
+
submit_button = gr.Button("Generate Tree")
|
| 144 |
|
| 145 |
boundary_output = gr.Textbox(label="Boundary Model Output")
|
| 146 |
keras_output = gr.Textbox(label="Keras Model Output")
|
|
|
|
| 147 |
csv_status = gr.Textbox(label="CSV Save Status")
|
|
|
|
| 148 |
ml_output = gr.Textbox(label="Maximum Likelihood Tree Output")
|
| 149 |
+
tree_download = gr.File(label="Download Tree (HTML)", file_types=[".html"])
|
| 150 |
|
| 151 |
submit_button.click(
|
| 152 |
fn=run_full_pipeline,
|
|
|
|
| 154 |
outputs=[
|
| 155 |
boundary_output,
|
| 156 |
keras_output,
|
|
|
|
| 157 |
csv_status,
|
|
|
|
| 158 |
ml_output,
|
| 159 |
tree_download
|
| 160 |
]
|