Samad14 commited on
Commit
938c5af
Β·
verified Β·
1 Parent(s): 76b5dbe

Upload folder using huggingface_hub

Browse files
Files changed (3) hide show
  1. Dockerfile +7 -0
  2. Dockerfile.worker +7 -0
  3. app/routers/phylo.py +89 -41
Dockerfile CHANGED
@@ -37,6 +37,13 @@ RUN wget -qO /tmp/phyml.tar.bz2 \
37
  chmod +x /usr/local/bin/phyml && \
38
  rm -rf /tmp/phyml.tar.bz2 /tmp/bin
39
 
 
 
 
 
 
 
 
40
  # Download pre-compiled AutoDock Vina binary from GitHub releases
41
  RUN wget -q "https://github.com/ccsb-scripps/AutoDock-Vina/releases/download/v1.2.7/vina_1.2.7_linux_x86_64" -O /usr/local/bin/vina && \
42
  chmod +x /usr/local/bin/vina
 
37
  chmod +x /usr/local/bin/phyml && \
38
  rm -rf /tmp/phyml.tar.bz2 /tmp/bin
39
 
40
+ # IQ-TREE2 (ultrafast bootstrap β€” 10-100x faster than PhyML for bootstrap)
41
+ RUN wget -q https://github.com/iqtree/iqtree2/releases/download/v2.3.6/iqtree-2.3.6-Linux-intel.tar.gz -O /tmp/iqtree.tar.gz && \
42
+ tar xzf /tmp/iqtree.tar.gz -C /opt && \
43
+ cp /opt/iqtree-2.3.6-Linux-intel/bin/iqtree2 /usr/local/bin/iqtree2 && \
44
+ chmod +x /usr/local/bin/iqtree2 && \
45
+ rm -rf /tmp/iqtree.tar.gz /opt/iqtree-2.3.6-Linux-intel
46
+
47
  # Download pre-compiled AutoDock Vina binary from GitHub releases
48
  RUN wget -q "https://github.com/ccsb-scripps/AutoDock-Vina/releases/download/v1.2.7/vina_1.2.7_linux_x86_64" -O /usr/local/bin/vina && \
49
  chmod +x /usr/local/bin/vina
Dockerfile.worker CHANGED
@@ -33,6 +33,13 @@ RUN wget -qO /tmp/phyml.tar.bz2 \
33
  chmod +x /usr/local/bin/phyml && \
34
  rm -rf /tmp/phyml.tar.bz2 /tmp/bin
35
 
 
 
 
 
 
 
 
36
  # minimap2 (for sequencing)
37
  RUN wget -q https://github.com/lh3/minimap2/releases/download/v2.28/minimap2-2.28_x64-linux.tar.bz2 -O /tmp/minimap2.tar.bz2 && \
38
  tar xjf /tmp/minimap2.tar.bz2 -C /tmp --no-same-owner && \
 
33
  chmod +x /usr/local/bin/phyml && \
34
  rm -rf /tmp/phyml.tar.bz2 /tmp/bin
35
 
36
+ # IQ-TREE2 (ultrafast bootstrap β€” 10-100x faster than PhyML for bootstrap)
37
+ RUN wget -q https://github.com/iqtree/iqtree2/releases/download/v2.3.6/iqtree-2.3.6-Linux-intel.tar.gz -O /tmp/iqtree.tar.gz && \
38
+ tar xzf /tmp/iqtree.tar.gz -C /opt && \
39
+ cp /opt/iqtree-2.3.6-Linux-intel/bin/iqtree2 /usr/local/bin/iqtree2 && \
40
+ chmod +x /usr/local/bin/iqtree2 && \
41
+ rm -rf /tmp/iqtree.tar.gz /opt/iqtree-2.3.6-Linux-intel
42
+
43
  # minimap2 (for sequencing)
44
  RUN wget -q https://github.com/lh3/minimap2/releases/download/v2.28/minimap2-2.28_x64-linux.tar.bz2 -O /tmp/minimap2.tar.bz2 && \
45
  tar xjf /tmp/minimap2.tar.bz2 -C /tmp --no-same-owner && \
app/routers/phylo.py CHANGED
@@ -286,79 +286,122 @@ def fasta_to_phylip(fasta: str) -> str:
286
 
287
 
288
  async def _run_phyml_local(job_id: str, aln_fasta: str, req: PhyloRequest) -> None:
289
- """Run PhyML as a subprocess on a temp PHYLIP file."""
290
  _patch(job_id, phase="tree_running")
291
 
292
  import os
293
  import shutil
294
  import tempfile
295
 
 
296
  phyml_path = shutil.which("phyml")
297
- if not phyml_path:
 
298
  _patch(job_id, phase="error",
299
- error="PhyML binary not found. ML method requires PhyML compiled from "
300
- "https://github.com/stephaneguindon/phyml. Try NJ or UPGMA instead.")
301
  return
302
 
303
- fd, phy_path = tempfile.mkstemp(suffix=".phy")
 
 
 
304
  os.close(fd)
305
  try:
306
- with open(phy_path, "w") as f:
307
  f.write(fasta_to_phylip(aln_fasta))
308
 
 
309
  datatype = "aa" if req.seq_type == "protein" else "nt"
310
  model = req.model if req.model else ("LG" if req.seq_type == "protein" else "GTR")
311
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
312
  proc = await asyncio.create_subprocess_exec(
313
- "phyml",
314
- "-i", phy_path,
315
- "-d", datatype,
316
- "-m", model,
317
- "-b", str(req.bootstrap if req.bootstrap else 0),
318
- "-o", "tlr",
319
- "--no_memory_check",
320
  stdout=asyncio.subprocess.PIPE,
321
  stderr=asyncio.subprocess.PIPE,
322
  )
323
- bs = req.bootstrap if req.bootstrap else 0
324
- if bs <= 0:
325
- timeout_s = 900
326
- elif bs <= 100:
327
- timeout_s = 900
328
- elif bs <= 500:
329
- timeout_s = 1800
330
- else:
331
- timeout_s = 3600
332
  try:
333
  _, stderr = await asyncio.wait_for(proc.communicate(), timeout=timeout_s)
334
  except asyncio.TimeoutError:
335
  proc.kill()
336
  await proc.communicate()
337
  _patch(job_id, phase="error",
338
- error=f"PhyML timed out after {timeout_s // 60} minutes "
339
- f"(bootstrap={bs} may require more time β€” try reducing to 100-200)")
340
  return
341
 
342
  if proc.returncode != 0:
343
  err_text = stderr.decode("utf-8", errors="replace")[:500] if stderr else ""
344
  _patch(job_id, phase="error",
345
- error=f"PhyML failed (exit {proc.returncode}): {err_text}")
346
  return
347
 
348
- tree_path = phy_path + "_phyml_tree.txt"
349
- stats_path = phy_path + "_phyml_stats.txt"
350
-
351
  newick = None
352
  stats = None
353
- if os.path.exists(tree_path):
354
- with open(tree_path) as f:
355
- newick = f.read().strip()
356
- if os.path.exists(stats_path):
357
- with open(stats_path) as f:
358
- stats = f.read().strip()
 
 
 
 
 
 
 
 
 
 
 
 
359
 
360
  if not newick:
361
- _patch(job_id, phase="error", error="PhyML produced no output tree")
362
  return
363
 
364
  _patch(job_id, phase="complete", newick=newick, stats=stats, done_at=time.time())
@@ -366,19 +409,24 @@ async def _run_phyml_local(job_id: str, aln_fasta: str, req: PhyloRequest) -> No
366
  # AI interpretation (best-effort, never blocks)
367
  try:
368
  from app.ai.tool_interpreter import interpret_tool_result
369
- result_data = {"newick": newick, "method": "ml", "stats": stats}
370
  ai_interp = await interpret_tool_result("phylo", result_data)
371
  if ai_interp:
372
  _patch(job_id, ai_interpretation=ai_interp)
373
  except Exception:
374
  pass
375
  except Exception as e:
376
- _patch(job_id, phase="error", error=f"PhyML error: {e}")
377
  finally:
378
- for suffix in ["", "_phyml_tree.txt", "_phyml_stats.txt", "_phyml_boot_trees.txt"]:
379
- p = phy_path + suffix
380
- if os.path.exists(p):
381
- os.remove(p)
 
 
 
 
 
382
 
383
 
384
  # ─── Main pipeline worker ─────────────────────────────────────────────────────
 
286
 
287
 
288
  async def _run_phyml_local(job_id: str, aln_fasta: str, req: PhyloRequest) -> None:
289
+ """Run ML tree building β€” prefers IQ-TREE2 (ultrafast bootstrap), falls back to PhyML."""
290
  _patch(job_id, phase="tree_running")
291
 
292
  import os
293
  import shutil
294
  import tempfile
295
 
296
+ iqtree_path = shutil.which("iqtree2") or shutil.which("iqtree")
297
  phyml_path = shutil.which("phyml")
298
+
299
+ if not iqtree_path and not phyml_path:
300
  _patch(job_id, phase="error",
301
+ error="No ML tree tool found. Install IQ-TREE2 (recommended) or PhyML.")
 
302
  return
303
 
304
+ use_iqtree = iqtree_path is not None
305
+ engine = "iqtree2" if use_iqtree else "phyml"
306
+
307
+ fd, aln_path = tempfile.mkstemp(suffix=".phy")
308
  os.close(fd)
309
  try:
310
+ with open(aln_path, "w") as f:
311
  f.write(fasta_to_phylip(aln_fasta))
312
 
313
+ bs = req.bootstrap if req.bootstrap else 0
314
  datatype = "aa" if req.seq_type == "protein" else "nt"
315
  model = req.model if req.model else ("LG" if req.seq_type == "protein" else "GTR")
316
 
317
+ if use_iqtree:
318
+ out_prefix = aln_path.replace(".phy", "")
319
+ cmd = [
320
+ iqtree_path,
321
+ "-s", aln_path,
322
+ "-m", model,
323
+ "-nt", "AUTO",
324
+ "-pre", out_prefix,
325
+ ]
326
+ if bs > 0:
327
+ cmd += ["-bb", str(bs)]
328
+ else:
329
+ cmd += ["-bb", "0"]
330
+ # IQ-TREE timeout: much shorter since ultrafast bootstrap is fast
331
+ if bs <= 0:
332
+ timeout_s = 300
333
+ elif bs <= 1000:
334
+ timeout_s = 600
335
+ else:
336
+ timeout_s = 1200
337
+ else:
338
+ cmd = [
339
+ "phyml",
340
+ "-i", aln_path,
341
+ "-d", datatype,
342
+ "-m", model,
343
+ "-b", str(bs),
344
+ "-o", "tlr",
345
+ "--no_memory_check",
346
+ ]
347
+ # PhyML timeout (slow classic bootstrap)
348
+ if bs <= 0:
349
+ timeout_s = 900
350
+ elif bs <= 100:
351
+ timeout_s = 900
352
+ elif bs <= 500:
353
+ timeout_s = 1800
354
+ else:
355
+ timeout_s = 3600
356
+
357
+ _patch(job_id, engine=engine, bootstrap=bs)
358
+ logger.info(f"[{job_id}] Running {engine} with bootstrap={bs}")
359
+
360
  proc = await asyncio.create_subprocess_exec(
361
+ *cmd,
 
 
 
 
 
 
362
  stdout=asyncio.subprocess.PIPE,
363
  stderr=asyncio.subprocess.PIPE,
364
  )
 
 
 
 
 
 
 
 
 
365
  try:
366
  _, stderr = await asyncio.wait_for(proc.communicate(), timeout=timeout_s)
367
  except asyncio.TimeoutError:
368
  proc.kill()
369
  await proc.communicate()
370
  _patch(job_id, phase="error",
371
+ error=f"{engine} timed out after {timeout_s // 60} minutes "
372
+ f"(bootstrap={bs}). Try reducing to 100-200.")
373
  return
374
 
375
  if proc.returncode != 0:
376
  err_text = stderr.decode("utf-8", errors="replace")[:500] if stderr else ""
377
  _patch(job_id, phase="error",
378
+ error=f"{engine} failed (exit {proc.returncode}): {err_text}")
379
  return
380
 
381
+ # Read output tree
 
 
382
  newick = None
383
  stats = None
384
+ if use_iqtree:
385
+ tree_file = aln_path.replace(".phy", ".treefile")
386
+ iqtree_file = aln_path.replace(".phy", ".iqtree")
387
+ if os.path.exists(tree_file):
388
+ with open(tree_file) as f:
389
+ newick = f.read().strip()
390
+ if os.path.exists(iqtree_file):
391
+ with open(iqtree_file) as f:
392
+ stats = f.read().strip()
393
+ else:
394
+ tree_path = aln_path + "_phyml_tree.txt"
395
+ stats_path = aln_path + "_phyml_stats.txt"
396
+ if os.path.exists(tree_path):
397
+ with open(tree_path) as f:
398
+ newick = f.read().strip()
399
+ if os.path.exists(stats_path):
400
+ with open(stats_path) as f:
401
+ stats = f.read().strip()
402
 
403
  if not newick:
404
+ _patch(job_id, phase="error", error=f"{engine} produced no output tree")
405
  return
406
 
407
  _patch(job_id, phase="complete", newick=newick, stats=stats, done_at=time.time())
 
409
  # AI interpretation (best-effort, never blocks)
410
  try:
411
  from app.ai.tool_interpreter import interpret_tool_result
412
+ result_data = {"newick": newick, "method": "ml", "engine": engine, "stats": stats}
413
  ai_interp = await interpret_tool_result("phylo", result_data)
414
  if ai_interp:
415
  _patch(job_id, ai_interpretation=ai_interp)
416
  except Exception:
417
  pass
418
  except Exception as e:
419
+ _patch(job_id, phase="error", error=f"ML tree error: {e}")
420
  finally:
421
+ # Clean up all temp files (IQ-TREE produces many)
422
+ dir_name = os.path.dirname(aln_path)
423
+ base_name = os.path.basename(aln_path).replace(".phy", "")
424
+ for f in os.listdir(dir_name):
425
+ if f.startswith(base_name):
426
+ try:
427
+ os.remove(os.path.join(dir_name, f))
428
+ except OSError:
429
+ pass
430
 
431
 
432
  # ─── Main pipeline worker ─────────────────────────────────────────────────────