Files
PlantAI-dev/skills/annotation/tests/test_annotation.py
T
2026-08-03 18:42:47 +08:00

90 lines
3.1 KiB
Python
Executable File

#!/usr/bin/env python3
"""
Annotation Skill Tests - 功能注释技能测试
"""
import json
import os
import sys
import tempfile
import unittest
from pathlib import Path
sys.path.insert(0, str(Path(__file__).parent.parent))
from run import AnnotationSkill
class TestAnnotationSkill(unittest.TestCase):
"""测试功能注释技能"""
def setUp(self):
self.skill = AnnotationSkill()
self.temp_dir = Path(tempfile.mkdtemp())
def tearDown(self):
import shutil
if self.temp_dir.exists():
shutil.rmtree(self.temp_dir)
def test_find_emapper(self):
"""测试查找emapper.py"""
try:
python_path, emapper_path = self.skill._find_emapper()
self.assertTrue(Path(python_path).exists())
self.assertTrue(Path(emapper_path).exists())
except RuntimeError:
self.skipTest("emapper.py not installed")
def test_parse_annotations(self):
"""测试解析annotations文件"""
annot_file = self.temp_dir / "test.emapper.annotations"
annot_file.write_text("""## emapper-version: 2.1.12
#query\tseed_ortholog\tevalue\tscore\teggnog_OGs\tmax_annot_lvl\tCOG_category\tDescription\tPreferred_name\tGOs\tEC\tKEGG_ko\tKEGG_Pathway\tKEGG_Module\tKEGG_Reaction\tKEGG_rclass\tBRITE\tKEGG_TC\tCAZy\tBiGG_Reaction\tPFAMs
Gene001\t10090.1\t1e-50\t200.0\tCOG0001@1|root\tEukaryota\tJ\tDesc\tGENE1\tGO:0005524\t-\tko:K00001\t-\t-\t-\t-\t-\t-\t-\t-\tPF00001
Gene002\t10090.2\t2e-45\t180.0\tCOG0002@1|root\tEukaryota\tK\tDesc\tGENE2\tGO:0003677\t-\tko:K00002\t-\t-\t-\t-\t-\t-\t-\t-\tPF00002
Gene003\t10090.3\t5e-40\t160.0\tCOG0003@1|root\tEukaryota\tL\tDesc\tGENE3\t-\t-\t-\t-\t-\t-\t-\t-\t-\t-\t-\tPF00003
""")
stats = self.skill._parse_annotations(annot_file)
self.assertEqual(stats["total_genes"], 3)
self.assertEqual(stats["with_go"], 2)
self.assertEqual(stats["with_kegg"], 2)
self.assertEqual(stats["with_cog"], 3)
self.assertEqual(stats["with_pfam"], 3)
self.assertEqual(stats["go_count"], 2)
self.assertEqual(stats["kegg_count"], 2)
def test_demo_mode(self):
"""测试Demo模式"""
result = self.skill.run_demo(self.temp_dir)
self.assertEqual(result["status"], "success")
self.assertTrue(Path(result["annotations"]).exists())
self.assertTrue(Path(result["report"]).exists())
# 检查输出文件
annot_file = Path(result["annotations"])
self.assertTrue(annot_file.exists())
# 检查统计
self.assertEqual(result["stats"]["total_genes"], 3)
def test_config_loading(self):
"""测试配置加载"""
config = self.skill.config
self.assertIn("skill", config)
self.assertIn("parameters", config)
self.assertIn("emapper", config["parameters"])
emapper_params = config["parameters"]["emapper"]
self.assertIn("mode", emapper_params)
self.assertIn("evalue", emapper_params)
self.assertIn("cpu", emapper_params)
if __name__ == "__main__":
unittest.main()