Files
PlantAI-dev/skills/karyotype/run.py
T
2026-08-03 18:42:47 +08:00

75 lines
2.9 KiB
Python
Executable File

#!/usr/bin/env python3
"""Karyotype Skill - 核型分析技能"""
import argparse, hashlib, json, logging, os, sys
from datetime import datetime
from pathlib import Path
from typing import Dict, Optional
import yaml
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
class KaryotypeSkill:
def __init__(self, config_path=None):
self.config = self._load_config(config_path)
self.commands, self.checksums = [], {}
def _load_config(self, config_path):
if config_path and os.path.exists(config_path):
with open(config_path) as f: return yaml.safe_load(f)
return {"skill": {"name": "karyotype", "version": "1.0.0"}, "parameters": {}, "dependencies": {"conda": {"channels": ["bioconda"], "packages": ["matplotlib"]}}}
def _compute_checksum(self, filepath):
sha256 = hashlib.sha256()
with open(filepath, "rb") as f:
for chunk in iter(lambda: f.read(8192), b""): sha256.update(chunk)
return sha256.hexdigest()
def _record_file(self, filepath, file_type="output"):
if filepath.exists():
self.checksums[str(filepath)] = {"sha256": self._compute_checksum(filepath), "size": filepath.stat().st_size, "type": file_type}
def generate_report(self, output_dir):
report_file = output_dir / "report.md"
content = f"""# 核型分析报告
## 基本信息
- **分析时间**: {datetime.now().strftime("%Y-%m-%d %H:%M:%S")}
- **技能版本**: {self.config["skill"]["version"]}
## 分析结果
| 文件 | 描述 |
|------|------|
| karyotype.png | 核型图 |
## 可复现性: commands.sh, environment.yml
"""
report_file.write_text(content)
self._record_file(report_file)
return report_file
def write_reproducibility_bundle(self, output_dir):
(output_dir / "commands.sh").write_text("#!/bin/bash\n# 核型分析\n")
(output_dir / "environment.yml").write_text("name: karyotype\nchannels: [bioconda]\ndependencies: [matplotlib]")
with open(output_dir / "checksums.sha256", "w") as f:
for filepath, info in self.checksums.items(): f.write(f"{info['sha256']} {filepath}\n")
def run_demo(self, output_dir):
output_dir.mkdir(parents=True, exist_ok=True)
self._record_file(output_dir / "karyotype.png")
report = self.generate_report(output_dir)
self.write_reproducibility_bundle(output_dir)
return {"status": "success", "output_dir": str(output_dir), "report": str(report)}
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--output", "-o", type=Path, required=True)
parser.add_argument("--demo", action="store_true")
args = parser.parse_args()
skill = KaryotypeSkill()
result = skill.run_demo(args.output)
print(json.dumps(result, indent=2))
return 0
if __name__ == "__main__": sys.exit(main())