cobrapy by davila7/claude-code-templates
npx skills add https://github.com/davila7/claude-code-templates --skill cobrapyCOBRApy 是一个用于代谢模型约束性重构与分析(COBRA)的 Python 库,是系统生物学研究的重要工具。可用于处理基因组尺度代谢模型、执行细胞代谢的计算模拟、进行代谢工程分析以及预测表型行为。
COBRApy 提供了全面的工具,组织成以下几个关键领域:
从存储库或文件加载现有模型:
from cobra.io import load_model
# 加载捆绑的测试模型
model = load_model("textbook") # 大肠杆菌核心模型
model = load_model("ecoli") # 完整大肠杆菌模型
model = load_model("salmonella")
# 从文件加载
from cobra.io import read_sbml_model, load_json_model, load_yaml_model
model = read_sbml_model("path/to/model.xml")
model = load_json_model("path/to/model.json")
model = load_yaml_model("path/to/model.yml")
以各种格式保存模型:
from cobra.io import write_sbml_model, save_json_model, save_yaml_model
write_sbml_model(model, "output.xml") # 首选格式
save_json_model(model, "output.json") # 用于 Escher 兼容性
save_yaml_model(model, "output.yml") # 人类可读
访问和检查模型组件:
# 访问组件
model.reactions # 所有反应的 DictList
model.metabolites # 所有代谢物的 DictList
model.genes # 所有基因的 DictList
# 通过 ID 或索引获取特定项
reaction = model.reactions.get_by_id("PFK")
metabolite = model.metabolites[0]
# 检查属性
print(reaction.reaction) # 化学计量方程
print(reaction.bounds) # 通量约束
print(reaction.gene_reaction_rule) # GPR 逻辑
print(metabolite.formula) # 化学式
print(metabolite.compartment) # 细胞位置
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
执行标准 FBA 模拟:
# 基本优化
solution = model.optimize()
print(f"目标值: {solution.objective_value}")
print(f"状态: {solution.status}")
# 访问通量
print(solution.fluxes["PFK"])
print(solution.fluxes.head())
# 快速优化(仅目标值)
objective_value = model.slim_optimize()
# 更改目标
model.objective = "ATPM"
solution = model.optimize()
简约 FBA(最小化总通量):
from cobra.flux_analysis import pfba
solution = pfba(model)
几何 FBA(寻找中心解):
from cobra.flux_analysis import geometric_fba
solution = geometric_fba(model)
确定所有反应的可行通量范围:
from cobra.flux_analysis import flux_variability_analysis
# 标准 FVA
fva_result = flux_variability_analysis(model)
# 在 90% 最优性下的 FVA
fva_result = flux_variability_analysis(model, fraction_of_optimum=0.9)
# 无循环 FVA(消除热力学上不可行的循环)
fva_result = flux_variability_analysis(model, loopless=True)
# 针对特定反应的 FVA
fva_result = flux_variability_analysis(
model,
reaction_list=["PFK", "FBA", "PGI"]
)
执行敲除分析:
from cobra.flux_analysis import (
single_gene_deletion,
single_reaction_deletion,
double_gene_deletion,
double_reaction_deletion
)
# 单敲除
gene_results = single_gene_deletion(model)
reaction_results = single_reaction_deletion(model)
# 双敲除(使用多进程)
double_gene_results = double_gene_deletion(
model,
processes=4 # CPU 核心数
)
# 使用上下文管理器进行手动敲除
with model:
model.genes.get_by_id("b0008").knock_out()
solution = model.optimize()
print(f"敲除后的生长: {solution.objective_value}")
# 上下文退出后模型自动恢复
管理生长培养基:
# 查看当前培养基
print(model.medium)
# 修改培养基(必须重新分配整个字典)
medium = model.medium
medium["EX_glc__D_e"] = 10.0 # 设置葡萄糖摄取
medium["EX_o2_e"] = 0.0 # 厌氧条件
model.medium = medium
# 计算最小培养基
from cobra.medium import minimal_medium
# 最小化总导入通量
min_medium = minimal_medium(model, minimize_components=False)
# 最小化组分数量(使用 MILP,较慢)
min_medium = minimal_medium(
model,
minimize_components=True,
open_exchanges=True
)
对可行通量空间进行采样:
from cobra.sampling import sample
# 使用 OptGP 采样(默认,支持并行处理)
samples = sample(model, n=1000, method="optgp", processes=4)
# 使用 ACHR 采样
samples = sample(model, n=1000, method="achr")
# 验证样本
from cobra.sampling import OptGPSampler
sampler = OptGPSampler(model, processes=4)
sampler.sample(1000)
validation = sampler.validate(sampler.samples)
print(validation.value_counts()) # 应全为 'v' 表示有效
计算表型相平面:
from cobra.flux_analysis import production_envelope
# 标准生产包络线
envelope = production_envelope(
model,
reactions=["EX_glc__D_e", "EX_o2_e"],
objective="EX_ac_e" # 乙酸生产
)
# 带碳产率
envelope = production_envelope(
model,
reactions=["EX_glc__D_e", "EX_o2_e"],
carbon_sources="EX_glc__D_e"
)
# 可视化(使用 matplotlib 或 pandas 绘图)
import matplotlib.pyplot as plt
envelope.plot(x="EX_glc__D_e", y="EX_o2_e", kind="scatter")
plt.show()
添加反应以使模型可行:
from cobra.flux_analysis import gapfill
# 准备包含候选反应的通用模型
universal = load_model("universal")
# 执行间隙填充
with model:
# 移除反应以创建间隙用于演示
model.remove_reactions([model.reactions.PGI])
# 查找所需反应
solution = gapfill(model, universal)
print(f"需要添加的反应: {solution}")
从零开始构建模型:
from cobra import Model, Reaction, Metabolite
# 创建模型
model = Model("my_model")
# 创建代谢物
atp_c = Metabolite("atp_c", formula="C10H12N5O13P3",
name="ATP", compartment="c")
adp_c = Metabolite("adp_c", formula="C10H12N5O10P2",
name="ADP", compartment="c")
pi_c = Metabolite("pi_c", formula="HO4P",
name="Phosphate", compartment="c")
# 创建反应
reaction = Reaction("ATPASE")
reaction.name = "ATP 水解"
reaction.subsystem = "能量"
reaction.lower_bound = 0.0
reaction.upper_bound = 1000.0
# 添加具有化学计量的代谢物
reaction.add_metabolites({
atp_c: -1.0,
adp_c: 1.0,
pi_c: 1.0
})
# 添加基因-反应规则
reaction.gene_reaction_rule = "(gene1 and gene2) or gene3"
# 添加到模型
model.add_reactions([reaction])
# 添加边界反应
model.add_boundary(atp_c, type="exchange")
model.add_boundary(adp_c, type="demand")
# 设置目标
model.objective = "ATPASE"
from cobra.io import load_model
# 加载模型
model = load_model("ecoli")
# 运行 FBA
solution = model.optimize()
print(f"生长速率: {solution.objective_value:.3f} /h")
# 显示活跃通路
print(solution.fluxes[solution.fluxes.abs() > 1e-6])
from cobra.io import load_model
from cobra.flux_analysis import single_gene_deletion
# 加载模型
model = load_model("ecoli")
# 执行单基因敲除
results = single_gene_deletion(model)
# 查找必需基因(生长 < 阈值)
essential_genes = results[results["growth"] < 0.01]
print(f"找到 {len(essential_genes)} 个必需基因")
# 查找影响最小的基因
neutral_genes = results[results["growth"] > 0.9 * solution.objective_value]
from cobra.io import load_model
from cobra.medium import minimal_medium
# 加载模型
model = load_model("ecoli")
# 计算最大生长 50% 的最小培养基
target_growth = model.slim_optimize() * 0.5
min_medium = minimal_medium(
model,
target_growth,
minimize_components=True
)
print(f"最小培养基组分: {len(min_medium)}")
print(min_medium)
from cobra.io import load_model
from cobra.flux_analysis import flux_variability_analysis
from cobra.sampling import sample
# 加载模型
model = load_model("ecoli")
# 首先检查最优性下的通量范围
fva = flux_variability_analysis(model, fraction_of_optimum=1.0)
# 对于范围较大的反应,采样以了解分布
samples = sample(model, n=1000)
# 分析特定反应
reaction_id = "PFK"
import matplotlib.pyplot as plt
samples[reaction_id].hist(bins=50)
plt.xlabel(f"{reaction_id} 的通量")
plt.ylabel("频率")
plt.show()
使用上下文管理器进行临时修改:
# 模型在上下文外部保持不变
with model:
# 临时更改目标
model.objective = "ATPM"
# 临时修改边界
model.reactions.EX_glc__D_e.lower_bound = -5.0
# 临时敲除基因
model.genes.b0008.knock_out()
# 使用更改进行优化
solution = model.optimize()
print(f"修改后的生长: {solution.objective_value}")
# 所有更改自动恢复
solution = model.optimize()
print(f"原始生长: {solution.objective_value}")
模型使用 DictList 对象处理反应、代谢物和基因 - 同时具有列表和字典的行为:
# 通过索引访问
first_reaction = model.reactions[0]
# 通过 ID 访问
pfk = model.reactions.get_by_id("PFK")
# 查询方法
atp_reactions = model.reactions.query("atp")
反应边界定义可行通量范围:
lower_bound = 0, upper_bound > 0lower_bound < 0, upper_bound > 0.bounds 同时设置两个边界以避免不一致连接基因与反应的布尔逻辑:
# AND 逻辑(两者都需要)
reaction.gene_reaction_rule = "gene1 and gene2"
# OR 逻辑(任一即可)
reaction.gene_reaction_rule = "gene1 or gene2"
# 复杂逻辑
reaction.gene_reaction_rule = "(gene1 and gene2) or (gene3 and gene4)"
代表代谢物导入/导出的特殊反应:
EX_ 前缀命名model.medium 字典管理model.slim_optimize() 确保可行性optimal 表示求解成功不可行解:检查培养基约束、反应边界和模型一致性 优化缓慢:尝试通过 model.solver 使用不同的求解器(GLPK、CPLEX、Gurobi) 无界解:验证交换反应具有适当的边界 导入错误:确保正确的文件格式和有效的 SBML 标识符
有关详细工作流程和 API 模式,请参阅:
references/workflows.md - 全面的逐步工作流程示例references/api_quick_reference.md - 常见函数签名和模式每周安装量
122
代码仓库
GitHub 星标数
22.6K
首次出现
2026 年 1 月 21 日
安全审计
安装于
claude-code102
opencode95
gemini-cli92
cursor90
antigravity86
codex80
COBRApy is a Python library for constraint-based reconstruction and analysis (COBRA) of metabolic models, essential for systems biology research. Work with genome-scale metabolic models, perform computational simulations of cellular metabolism, conduct metabolic engineering analyses, and predict phenotypic behaviors.
COBRApy provides comprehensive tools organized into several key areas:
Load existing models from repositories or files:
from cobra.io import load_model
# Load bundled test models
model = load_model("textbook") # E. coli core model
model = load_model("ecoli") # Full E. coli model
model = load_model("salmonella")
# Load from files
from cobra.io import read_sbml_model, load_json_model, load_yaml_model
model = read_sbml_model("path/to/model.xml")
model = load_json_model("path/to/model.json")
model = load_yaml_model("path/to/model.yml")
Save models in various formats:
from cobra.io import write_sbml_model, save_json_model, save_yaml_model
write_sbml_model(model, "output.xml") # Preferred format
save_json_model(model, "output.json") # For Escher compatibility
save_yaml_model(model, "output.yml") # Human-readable
Access and inspect model components:
# Access components
model.reactions # DictList of all reactions
model.metabolites # DictList of all metabolites
model.genes # DictList of all genes
# Get specific items by ID or index
reaction = model.reactions.get_by_id("PFK")
metabolite = model.metabolites[0]
# Inspect properties
print(reaction.reaction) # Stoichiometric equation
print(reaction.bounds) # Flux constraints
print(reaction.gene_reaction_rule) # GPR logic
print(metabolite.formula) # Chemical formula
print(metabolite.compartment) # Cellular location
Perform standard FBA simulation:
# Basic optimization
solution = model.optimize()
print(f"Objective value: {solution.objective_value}")
print(f"Status: {solution.status}")
# Access fluxes
print(solution.fluxes["PFK"])
print(solution.fluxes.head())
# Fast optimization (objective value only)
objective_value = model.slim_optimize()
# Change objective
model.objective = "ATPM"
solution = model.optimize()
Parsimonious FBA (minimize total flux):
from cobra.flux_analysis import pfba
solution = pfba(model)
Geometric FBA (find central solution):
from cobra.flux_analysis import geometric_fba
solution = geometric_fba(model)
Determine flux ranges for all reactions:
from cobra.flux_analysis import flux_variability_analysis
# Standard FVA
fva_result = flux_variability_analysis(model)
# FVA at 90% optimality
fva_result = flux_variability_analysis(model, fraction_of_optimum=0.9)
# Loopless FVA (eliminates thermodynamically infeasible loops)
fva_result = flux_variability_analysis(model, loopless=True)
# FVA for specific reactions
fva_result = flux_variability_analysis(
model,
reaction_list=["PFK", "FBA", "PGI"]
)
Perform knockout analyses:
from cobra.flux_analysis import (
single_gene_deletion,
single_reaction_deletion,
double_gene_deletion,
double_reaction_deletion
)
# Single deletions
gene_results = single_gene_deletion(model)
reaction_results = single_reaction_deletion(model)
# Double deletions (uses multiprocessing)
double_gene_results = double_gene_deletion(
model,
processes=4 # Number of CPU cores
)
# Manual knockout using context manager
with model:
model.genes.get_by_id("b0008").knock_out()
solution = model.optimize()
print(f"Growth after knockout: {solution.objective_value}")
# Model automatically reverts after context exit
Manage growth medium:
# View current medium
print(model.medium)
# Modify medium (must reassign entire dict)
medium = model.medium
medium["EX_glc__D_e"] = 10.0 # Set glucose uptake
medium["EX_o2_e"] = 0.0 # Anaerobic conditions
model.medium = medium
# Calculate minimal media
from cobra.medium import minimal_medium
# Minimize total import flux
min_medium = minimal_medium(model, minimize_components=False)
# Minimize number of components (uses MILP, slower)
min_medium = minimal_medium(
model,
minimize_components=True,
open_exchanges=True
)
Sample the feasible flux space:
from cobra.sampling import sample
# Sample using OptGP (default, supports parallel processing)
samples = sample(model, n=1000, method="optgp", processes=4)
# Sample using ACHR
samples = sample(model, n=1000, method="achr")
# Validate samples
from cobra.sampling import OptGPSampler
sampler = OptGPSampler(model, processes=4)
sampler.sample(1000)
validation = sampler.validate(sampler.samples)
print(validation.value_counts()) # Should be all 'v' for valid
Calculate phenotype phase planes:
from cobra.flux_analysis import production_envelope
# Standard production envelope
envelope = production_envelope(
model,
reactions=["EX_glc__D_e", "EX_o2_e"],
objective="EX_ac_e" # Acetate production
)
# With carbon yield
envelope = production_envelope(
model,
reactions=["EX_glc__D_e", "EX_o2_e"],
carbon_sources="EX_glc__D_e"
)
# Visualize (use matplotlib or pandas plotting)
import matplotlib.pyplot as plt
envelope.plot(x="EX_glc__D_e", y="EX_o2_e", kind="scatter")
plt.show()
Add reactions to make models feasible:
from cobra.flux_analysis import gapfill
# Prepare universal model with candidate reactions
universal = load_model("universal")
# Perform gapfilling
with model:
# Remove reactions to create gaps for demonstration
model.remove_reactions([model.reactions.PGI])
# Find reactions needed
solution = gapfill(model, universal)
print(f"Reactions to add: {solution}")
Build models from scratch:
from cobra import Model, Reaction, Metabolite
# Create model
model = Model("my_model")
# Create metabolites
atp_c = Metabolite("atp_c", formula="C10H12N5O13P3",
name="ATP", compartment="c")
adp_c = Metabolite("adp_c", formula="C10H12N5O10P2",
name="ADP", compartment="c")
pi_c = Metabolite("pi_c", formula="HO4P",
name="Phosphate", compartment="c")
# Create reaction
reaction = Reaction("ATPASE")
reaction.name = "ATP hydrolysis"
reaction.subsystem = "Energy"
reaction.lower_bound = 0.0
reaction.upper_bound = 1000.0
# Add metabolites with stoichiometry
reaction.add_metabolites({
atp_c: -1.0,
adp_c: 1.0,
pi_c: 1.0
})
# Add gene-reaction rule
reaction.gene_reaction_rule = "(gene1 and gene2) or gene3"
# Add to model
model.add_reactions([reaction])
# Add boundary reactions
model.add_boundary(atp_c, type="exchange")
model.add_boundary(adp_c, type="demand")
# Set objective
model.objective = "ATPASE"
from cobra.io import load_model
# Load model
model = load_model("ecoli")
# Run FBA
solution = model.optimize()
print(f"Growth rate: {solution.objective_value:.3f} /h")
# Show active pathways
print(solution.fluxes[solution.fluxes.abs() > 1e-6])
from cobra.io import load_model
from cobra.flux_analysis import single_gene_deletion
# Load model
model = load_model("ecoli")
# Perform single gene deletions
results = single_gene_deletion(model)
# Find essential genes (growth < threshold)
essential_genes = results[results["growth"] < 0.01]
print(f"Found {len(essential_genes)} essential genes")
# Find genes with minimal impact
neutral_genes = results[results["growth"] > 0.9 * solution.objective_value]
from cobra.io import load_model
from cobra.medium import minimal_medium
# Load model
model = load_model("ecoli")
# Calculate minimal medium for 50% of max growth
target_growth = model.slim_optimize() * 0.5
min_medium = minimal_medium(
model,
target_growth,
minimize_components=True
)
print(f"Minimal medium components: {len(min_medium)}")
print(min_medium)
from cobra.io import load_model
from cobra.flux_analysis import flux_variability_analysis
from cobra.sampling import sample
# Load model
model = load_model("ecoli")
# First check flux ranges at optimality
fva = flux_variability_analysis(model, fraction_of_optimum=1.0)
# For reactions with large ranges, sample to understand distribution
samples = sample(model, n=1000)
# Analyze specific reaction
reaction_id = "PFK"
import matplotlib.pyplot as plt
samples[reaction_id].hist(bins=50)
plt.xlabel(f"Flux through {reaction_id}")
plt.ylabel("Frequency")
plt.show()
Use context managers to make temporary modifications:
# Model remains unchanged outside context
with model:
# Temporarily change objective
model.objective = "ATPM"
# Temporarily modify bounds
model.reactions.EX_glc__D_e.lower_bound = -5.0
# Temporarily knock out genes
model.genes.b0008.knock_out()
# Optimize with changes
solution = model.optimize()
print(f"Modified growth: {solution.objective_value}")
# All changes automatically reverted
solution = model.optimize()
print(f"Original growth: {solution.objective_value}")
Models use DictList objects for reactions, metabolites, and genes - behaving like both lists and dictionaries:
# Access by index
first_reaction = model.reactions[0]
# Access by ID
pfk = model.reactions.get_by_id("PFK")
# Query methods
atp_reactions = model.reactions.query("atp")
Reaction bounds define feasible flux ranges:
lower_bound = 0, upper_bound > 0lower_bound < 0, upper_bound > 0.bounds to avoid inconsistenciesBoolean logic linking genes to reactions:
# AND logic (both required)
reaction.gene_reaction_rule = "gene1 and gene2"
# OR logic (either sufficient)
reaction.gene_reaction_rule = "gene1 or gene2"
# Complex logic
reaction.gene_reaction_rule = "(gene1 and gene2) or (gene3 and gene4)"
Special reactions representing metabolite import/export:
EX_ by conventionmodel.medium dictionarymodel.slim_optimize() to ensure feasibilityoptimal indicates successful solveInfeasible solutions : Check medium constraints, reaction bounds, and model consistency Slow optimization : Try different solvers (GLPK, CPLEX, Gurobi) via model.solver Unbounded solutions : Verify exchange reactions have appropriate upper bounds Import errors : Ensure correct file format and valid SBML identifiers
For detailed workflows and API patterns, refer to:
references/workflows.md - Comprehensive step-by-step workflow examplesreferences/api_quick_reference.md - Common function signatures and patternsOfficial documentation: https://cobrapy.readthedocs.io/en/latest/
Weekly Installs
122
Repository
GitHub Stars
22.6K
First Seen
Jan 21, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
claude-code102
opencode95
gemini-cli92
cursor90
antigravity86
codex80
PPTX 文件处理全攻略:Python 脚本创建、编辑、分析 .pptx 文件内容与结构
915 周安装