feat(worker): integrate CRISPR-Cas pipeline execution logic

This commit is contained in:
zly
2026-01-14 15:57:12 +08:00
parent 92b01b408f
commit 59d7f8e051
2 changed files with 124 additions and 10 deletions

View File

@@ -26,9 +26,9 @@
- [x] **F4.3**: 在结果页面展示 CRISPR 融合分析结果 (UI参数展示已完成结果展示待后端集成)
## 后续计划: 任务执行集成 (P1)
- [ ] **E1.1**: 更新 `tasks.py` 调用 `detect_crispr.py`
- [ ] **E1.2**: 更新 `tasks.py` 调用 `fusion_analysis.py`
- [ ] **E1.3**: 更新 `bttoxin_shoter.py` 使用 CRISPR 结果
- [x] **E1.1**: 更新 `tasks.py` 调用 `detect_crispr.py`
- [x] **E1.2**: 更新 `tasks.py` 调用 `fusion_analysis.py`
- [x] **E1.3**: 更新 `bttoxin_shoter.py` 使用 CRISPR 结果 (Worker integration complete)
## 已完成 (上一阶段)

View File

@@ -4,6 +4,8 @@ from pathlib import Path
import shutil
import logging
import asyncio
import subprocess
import os
from ..core.celery_app import celery_app
from ..core.docker_client import DockerManager
@@ -14,7 +16,30 @@ from ..services.concurrency_manager import get_concurrency_manager
logger = logging.getLogger(__name__)
# Pipeline 阶段定义
PIPELINE_STAGES = ["digger", "shoter", "plots", "bundle"]
PIPELINE_STAGES = ["digger", "crispr", "shoter", "plots", "bundle"]
def run_local_command(cmd: list, cwd: Path = None, env: dict = None) -> dict:
"""Run a command locally in the container"""
try:
logger.info(f"Running command: {' '.join(cmd)}")
result = subprocess.run(
cmd,
cwd=cwd,
env=env or os.environ.copy(),
capture_output=True,
text=True,
check=False
)
return {
'success': result.returncode == 0,
'stdout': result.stdout,
'stderr': result.stderr,
'exit_code': result.returncode
}
except Exception as e:
logger.error(f"Command failed: {e}")
return {'success': False, 'error': str(e)}
@celery_app.task(bind=True, max_retries=3)
@@ -86,6 +111,57 @@ def run_bttoxin_analysis(
job.progress_percent = 40
db.commit()
# 阶段 1.5: CRISPR-Cas (如果启用)
crispr_results_file = None
if crispr_fusion:
logger.info(f"Job {job_id}: Starting CRISPR stage")
job.current_stage = "crispr"
db.commit()
self.update_state(
state='PROGRESS',
meta={'stage': 'crispr', 'progress': 45, 'status': 'Running CRISPR Detection...'}
)
crispr_out = Path(output_dir) / "crispr" / "results.json"
crispr_out.parent.mkdir(parents=True, exist_ok=True)
# 1. Detection
detect_cmd = [
"pixi", "run", "-e", "crispr", "python", "crispr_cas/scripts/detect_crispr.py",
"--input", str(Path(input_dir) / f"{job_id}{scaf_suffix}"), # Assuming input file name matches
"--output", str(crispr_out),
"--mock" # Always use mock for now as we don't have the tool installed
]
# Find input file - might be named differently
input_files = list(Path(input_dir).glob(f"*{scaf_suffix}"))
if input_files:
detect_cmd[7] = str(input_files[0])
res = run_local_command(detect_cmd, cwd=Path("/app"))
if not res['success']:
logger.warning(f"CRISPR detection failed: {res.get('stderr')}")
else:
crispr_results_file = crispr_out
# 2. Fusion (if requested)
fusion_out = Path(output_dir) / "crispr" / "fusion_analysis.json"
# TODO: We need the toxins file from Digger output.
# Assuming Digger output structure: output_dir/Results/Toxins/All_Toxins.txt (Need to verify)
# But DockerManager output might be different. Let's assume standard structure.
toxins_file = Path(output_dir) / "Results" / "Toxins" / "All_Toxins.txt"
if toxins_file.exists():
fusion_cmd = [
"pixi", "run", "-e", "crispr", "python", "crispr_cas/scripts/fusion_analysis.py",
"--crispr-results", str(crispr_out),
"--toxin-results", str(toxins_file),
"--genome", str(input_files[0]),
"--output", str(fusion_out),
"--mock"
]
run_local_command(fusion_cmd, cwd=Path("/app"))
# 阶段 2: Shoter - 评估毒性活性
logger.info(f"Job {job_id}: Starting Shoter stage")
job.current_stage = "shoter"
@@ -95,10 +171,35 @@ def run_bttoxin_analysis(
meta={'stage': 'shoter', 'progress': 50, 'status': 'Running BtToxin_Shoter...'}
)
# TODO: 实现 Shoter 调用
# shoter_result = run_shoter_pipeline(...)
# 暂时跳过
logger.info(f"Job {job_id}: Shoter stage not implemented yet, skipping")
# 构建 Shoter 命令
# 假设 Digger 输出在 output_dir/Results/Toxins/All_Toxins.txt
toxins_file = Path(output_dir) / "Results" / "Toxins" / "All_Toxins.txt"
shoter_out_dir = Path(output_dir) / "shoter"
# 即使 Digger 失败或没有结果,我们也可以尝试运行(脚本会处理空文件)
# 如果文件不存在,可能 Digger 结构不同,需要适配
shoter_cmd = [
"pixi", "run", "-e", "pipeline", "python", "scripts/bttoxin_shoter.py",
"--all_toxins", str(toxins_file),
"--output_dir", str(shoter_out_dir),
"--min_identity", str(min_identity),
"--min_coverage", str(min_coverage)
]
if allow_unknown_families:
shoter_cmd.append("--allow_unknown_families")
if require_index_hit:
shoter_cmd.append("--require_index_hit")
# CRISPR Integration
if crispr_results_file:
shoter_cmd.extend(["--crispr_results", str(crispr_results_file)])
shoter_cmd.extend(["--crispr_weight", str(crispr_weight)])
if crispr_fusion:
shoter_cmd.append("--crispr_fusion")
run_local_command(shoter_cmd, cwd=Path("/app"))
job.progress_percent = 70
db.commit()
@@ -112,8 +213,21 @@ def run_bttoxin_analysis(
meta={'stage': 'plots', 'progress': 80, 'status': 'Generating plots...'}
)
# TODO: 实现 Plots 生成
logger.info(f"Job {job_id}: Plots stage not implemented yet, skipping")
plot_cmd = [
"pixi", "run", "-e", "pipeline", "python", "scripts/plot_shotter.py",
"--strain_scores", str(shoter_out_dir / "strain_target_scores.tsv"),
"--toxin_support", str(shoter_out_dir / "toxin_support.tsv"),
"--species_scores", str(shoter_out_dir / "strain_target_species_scores.tsv"),
"--out_dir", str(shoter_out_dir),
"--output_prefix", "Activity_Heatmap"
]
if crispr_results_file:
plot_cmd.extend(["--crispr_results", str(crispr_results_file)])
if crispr_fusion:
plot_cmd.append("--crispr_fusion")
run_local_command(plot_cmd, cwd=Path("/app"))
job.progress_percent = 90
db.commit()