Spaces:
No application file
No application file
Update app.py
Browse files
app.py
CHANGED
|
@@ -217,6 +217,11 @@ def phylogenetic_placement(sequence: str, mafft_cmd: str, iqtree_cmd: str):
|
|
| 217 |
aligned_with_query = os.path.join(QUERY_OUTPUT_DIR, f"{query_id}_aligned.fa")
|
| 218 |
output_prefix = os.path.join(QUERY_OUTPUT_DIR, f"{query_id}_placed_tree")
|
| 219 |
if not os.path.exists(ALIGNMENT_PATH) or not os.path.exists(TREE_PATH):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 220 |
return False, "Reference alignment or tree not found.", None, None
|
| 221 |
query_record = SeqRecord(Seq(sequence.upper()), id=query_id, description="")
|
| 222 |
SeqIO.write([query_record], query_fasta, "fasta")
|
|
@@ -225,6 +230,11 @@ def phylogenetic_placement(sequence: str, mafft_cmd: str, iqtree_cmd: str):
|
|
| 225 |
mafft_cmd, "--add", query_fasta, "--reorder", ALIGNMENT_PATH
|
| 226 |
], stdout=output_file, stderr=subprocess.PIPE, text=True, timeout=600, check=True)
|
| 227 |
if not os.path.exists(aligned_with_query) or os.path.getsize(aligned_with_query) == 0:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 228 |
return False, "MAFFT alignment failed.", None, None
|
| 229 |
subprocess.run([
|
| 230 |
iqtree_cmd, "-s", aligned_with_query, "-g", TREE_PATH,
|
|
@@ -232,19 +242,27 @@ def phylogenetic_placement(sequence: str, mafft_cmd: str, iqtree_cmd: str):
|
|
| 232 |
], capture_output=True, text=True, timeout=1200, check=True)
|
| 233 |
treefile = f"{output_prefix}.treefile"
|
| 234 |
if not os.path.exists(treefile):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 235 |
return False, "IQ-TREE placement failed.", aligned_with_query, None
|
| 236 |
success_msg = f"Placement completed!\nQuery ID: {query_id}\nAlignment: {os.path.basename(aligned_with_query)}\nTree: {os.path.basename(treefile)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 237 |
return True, success_msg, aligned_with_query, treefile
|
| 238 |
-
except Exception as main_error: # Renamed from 'e'
|
| 239 |
logger.error(f"Phylogenetic placement failed: {main_error}", exc_info=True)
|
| 240 |
-
|
| 241 |
-
finally:
|
| 242 |
-
if query_fasta and os.path.exists(query_fasta):
|
| 243 |
try:
|
| 244 |
os.unlink(query_fasta)
|
| 245 |
except Exception as cleanup_error:
|
| 246 |
logger.warning(f"Failed to clean up {query_fasta}: {cleanup_error}")
|
| 247 |
-
|
| 248 |
try:
|
| 249 |
logger.debug("Starting tree analysis...")
|
| 250 |
if not analyzer:
|
|
|
|
| 217 |
aligned_with_query = os.path.join(QUERY_OUTPUT_DIR, f"{query_id}_aligned.fa")
|
| 218 |
output_prefix = os.path.join(QUERY_OUTPUT_DIR, f"{query_id}_placed_tree")
|
| 219 |
if not os.path.exists(ALIGNMENT_PATH) or not os.path.exists(TREE_PATH):
|
| 220 |
+
if query_fasta and os.path.exists(query_fasta): # Cleanup before return
|
| 221 |
+
try:
|
| 222 |
+
os.unlink(query_fasta)
|
| 223 |
+
except Exception as cleanup_error:
|
| 224 |
+
logger.warning(f"Failed to clean up {query_fasta}: {cleanup_error}")
|
| 225 |
return False, "Reference alignment or tree not found.", None, None
|
| 226 |
query_record = SeqRecord(Seq(sequence.upper()), id=query_id, description="")
|
| 227 |
SeqIO.write([query_record], query_fasta, "fasta")
|
|
|
|
| 230 |
mafft_cmd, "--add", query_fasta, "--reorder", ALIGNMENT_PATH
|
| 231 |
], stdout=output_file, stderr=subprocess.PIPE, text=True, timeout=600, check=True)
|
| 232 |
if not os.path.exists(aligned_with_query) or os.path.getsize(aligned_with_query) == 0:
|
| 233 |
+
if query_fasta and os.path.exists(query_fasta): # Cleanup before return
|
| 234 |
+
try:
|
| 235 |
+
os.unlink(query_fasta)
|
| 236 |
+
except Exception as cleanup_error:
|
| 237 |
+
logger.warning(f"Failed to clean up {query_fasta}: {cleanup_error}")
|
| 238 |
return False, "MAFFT alignment failed.", None, None
|
| 239 |
subprocess.run([
|
| 240 |
iqtree_cmd, "-s", aligned_with_query, "-g", TREE_PATH,
|
|
|
|
| 242 |
], capture_output=True, text=True, timeout=1200, check=True)
|
| 243 |
treefile = f"{output_prefix}.treefile"
|
| 244 |
if not os.path.exists(treefile):
|
| 245 |
+
if query_fasta and os.path.exists(query_fasta): # Cleanup before return
|
| 246 |
+
try:
|
| 247 |
+
os.unlink(query_fasta)
|
| 248 |
+
except Exception as cleanup_error:
|
| 249 |
+
logger.warning(f"Failed to clean up {query_fasta}: {cleanup_error}")
|
| 250 |
return False, "IQ-TREE placement failed.", aligned_with_query, None
|
| 251 |
success_msg = f"Placement completed!\nQuery ID: {query_id}\nAlignment: {os.path.basename(aligned_with_query)}\nTree: {os.path.basename(treefile)}"
|
| 252 |
+
if query_fasta and os.path.exists(query_fasta): # Cleanup before return
|
| 253 |
+
try:
|
| 254 |
+
os.unlink(query_fasta)
|
| 255 |
+
except Exception as cleanup_error:
|
| 256 |
+
logger.warning(f"Failed to clean up {query_fasta}: {cleanup_error}")
|
| 257 |
return True, success_msg, aligned_with_query, treefile
|
| 258 |
+
except Exception as main_error: # Renamed from 'e'
|
| 259 |
logger.error(f"Phylogenetic placement failed: {main_error}", exc_info=True)
|
| 260 |
+
if query_fasta and os.path.exists(query_fasta): # Cleanup in except
|
|
|
|
|
|
|
| 261 |
try:
|
| 262 |
os.unlink(query_fasta)
|
| 263 |
except Exception as cleanup_error:
|
| 264 |
logger.warning(f"Failed to clean up {query_fasta}: {cleanup_error}")
|
| 265 |
+
return False, f"Error: {str(main_error)}", None, Nonedef analyze_sequence_for_tree(sequence: str, matching_percentage: float):
|
| 266 |
try:
|
| 267 |
logger.debug("Starting tree analysis...")
|
| 268 |
if not analyzer:
|