Files
PlantAI-dev/apps/web/vue3/main_new_funcs.py
T
2026-08-03 18:42:47 +08:00

110 lines
5.1 KiB
Python
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
def generate_stage_connection(from_stage: str, to_stage: str, from_metrics: dict, to_metrics: dict, prefix: str) -> dict:
"""基于真实metrics生成阶段间关联分析"""
# 获取关键数值
from_count = from_metrics.get("final_members") or from_metrics.get("gene_count") or from_metrics.get("sequences_count") or from_metrics.get("sequence_count")
to_count = to_metrics.get("final_members") or to_metrics.get("gene_count") or to_metrics.get("sequences_count") or to_metrics.get("sequence_count")
# 阶段名称映射
stage_names = {
"identify": "基因家族鉴定",
"phylogeny": "系统发育分析",
"motif": "Motif分析",
"domain": "结构域分析",
"structure": "基因结构分析",
"promoter": "启动子分析",
"pep": "蛋白属性分析",
"synteny": "共线性分析",
"expression": "表达分析"
}
from_name = stage_names.get(from_stage, from_stage)
to_name = stage_names.get(to_stage, to_stage)
# 提取from阶段的详细信息
from_details = []
to_details = []
if from_stage == "identify":
candidates = from_metrics.get("candidates", 0)
final = from_metrics.get("final_members", 0)
from_details.append(f"从{candidates}个候选基因筛选出{final}个成员")
elif from_stage == "pep":
avg_len = from_metrics.get("avg_length", 0)
avg_pi = from_metrics.get("avg_pi", 0)
from_details.append(f"平均长度{avg_len:.0f}aa,等电点{avg_pi:.1f}")
elif from_stage == "structure":
genes = from_metrics.get("genes_with_structure", 0) or from_count
from_details.append(f"{genes}个基因的结构信息")
elif from_stage == "promoter":
total_elem = from_metrics.get("total_elements", 0)
unique_elem = from_metrics.get("unique_elements", 0)
from_details.append(f"识别出{total_elem}个顺式作用元件({unique_elem}种类型)")
# 提取to阶段的详细信息
if to_stage == "promoter":
genes_with_elem = to_metrics.get("genes_with_elements", 0)
total_elem = to_metrics.get("total_elements", 0)
unique_elem = to_metrics.get("unique_elements", 0)
enriched = to_metrics.get("enriched_pairs", 0)
to_details.append(f"{genes_with_elem}个基因含有顺式作用元件")
to_details.append(f"共发现{total_elem}个元件({unique_elem}种类型)")
if enriched > 0:
to_details.append(f"{enriched}对显著富集的元件组合")
elif to_stage == "phylogeny":
msa_len = to_metrics.get("msa_length", 0)
model = to_metrics.get("tree_model", "").split()[0] if to_metrics.get("tree_model") else ""
to_details.append(f"MSA长度{msa_len}bp" if msa_len else f"{to_count}条序列")
if model:
to_details.append(f"使用{model}模型")
elif to_stage == "motif":
motif_count = to_metrics.get("motif_count", 0)
to_details.append(f"识别出{motif_count}个保守Motif")
elif to_stage == "domain":
domain_count = to_metrics.get("domain_count", 0)
to_details.append(f"发现{domain_count}类功能结构域")
# 生成具体描述
if from_details and to_details:
description = f"{from_name}提供了{'; '.join(from_details)},基于此进行{to_name},获得{'; '.join(to_details)}。"
elif from_details:
description = f"{from_name}提供了{'; '.join(from_details)},为{to_name}奠定基础。"
elif to_details:
description = f"基于{from_name}的结果进行{to_name},获得{'; '.join(to_details)}。"
else:
description = f"{from_name}的结果为{to_name}提供了必要的数据基础。"
# 生成洞察(基于数据关系)
insights = []
# 检查数据一致性
if from_count and to_count:
if abs(from_count - to_count) <= 1:
insights.append(f"两阶段数据高度一致({from_count} vs {to_count}),表明分析质量良好")
elif to_count < from_count:
ratio = to_count / from_count * 100
insights.append(f"{to_name}覆盖了{ratio:.0f}%的{from_name}数据,部分基因可能因不符合条件被过滤")
else:
insights.append(f"{to_name}涉及更多数据点,可能包含了参考序列或外群")
# 阶段特定洞察
if from_stage == "identify" and to_stage == "promoter":
genes_with_elem = to_metrics.get("genes_with_elements", 0)
final = from_metrics.get("final_members", 0) or from_count
if genes_with_elem and final:
coverage = genes_with_elem / final * 100
insights.append(f"{coverage:.0f}%的家族成员具有可识别的启动子元件")
if from_stage == "structure" and to_stage == "promoter":
insights.append("基因结构与启动子元件的联合分析可揭示调控机制与基因组织的关联")
if not insights:
insights.append("连续的分析流程确保了数据的完整性和一致性")
return {
"from": from_stage,
"to": to_stage,
"description": description,
"insight": ";".join(insights)
}