[ PROMPT_NODE_26408 ]
Cobrapy 工作流
[ SKILL_DOCUMENTATION ]
# COBRApy 综合工作流
本文档提供了代谢建模中常见 COBRApy 任务的详细分步工作流。
## 工作流 1:带可视化的完整敲除研究
此工作流演示了如何执行全面的基因敲除研究并可视化结果。
python
import pandas as pd
import matplotlib.pyplot as plt
from cobra.io import load_model
from cobra.flux_analysis import single_gene_deletion, double_gene_deletion
# 第 1 步:加载模型
model = load_model("ecoli")
print(f"已加载模型: {model.id}")
print(f"模型包含 {len(model.reactions)} 个反应, {len(model.metabolites)} 个代谢物, {len(model.genes)} 个基因")
# 第 2 步:获取基准生长速率
baseline = model.slim_optimize()
print(f"基准生长速率: {baseline:.3f} /h")
# 第 3 步:执行单基因敲除
print("正在执行单基因敲除...")
single_results = single_gene_deletion(model)
# 第 4 步:按影响分类基因
essential_genes = single_results[single_results["growth"] = 0.01) &
(single_results["growth"] = 0.5 * baseline) &
(single_results["growth"] = 0.9 * baseline]
print(f"n单基因敲除结果:")
print(f" 必需基因: {len(essential_genes)}")
print(f" 严重受损: {len(severely_impaired)}")
print(f" 中度受损: {len(moderately_impaired)}")
print(f" 中性基因: {len(neutral_genes)}")
# 第 5 步:可视化分布
fig, ax = plt.subplots(figsize=(10, 6))
single_results["growth"].hist(bins=50, ax=ax)
ax.axvline(baseline, color='r', linestyle='--', label='基准')
ax.set_xlabel("生长速率 (/h)")
ax.set_ylabel("基因数量")
ax.set_title("单基因敲除后的生长速率分布")
ax.legend()
plt.tight_layout()
plt.savefig("single_deletion_distribution.png", dpi=300)
# 第 6 步:识别用于双基因敲除的基因对
# 聚焦于非必需基因以寻找合成致死
target_genes = single_results[single_results["growth"] >= 0.5 * baseline].index.tolist()
target_genes = [list(gene)[0] for gene in target_genes[:50]] # 限制数量以保证性能
print(f"n正在对 {len(target_genes)} 个基因执行双基因敲除...")
double_results = double_gene_deletion(
model,