cobrapy
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseCOBRApy - Constraint-Based Reconstruction and Analysis
COBRApy - 基于约束的重构与分析
Overview
概述
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是一款用于基于约束的代谢模型重构与分析(COBRA)的Python库,是系统生物学研究的必备工具。可用于处理基因组规模的代谢模型、执行细胞代谢的计算模拟、开展代谢工程分析以及预测表型行为。
Core Capabilities
核心功能
COBRApy provides comprehensive tools organized into several key areas:
COBRApy提供的全面工具分为以下几个关键领域:
1. Model Management
1. 模型管理
Load existing models from repositories or files:
python
from cobra.io import load_model从存储库或文件加载现有模型:
python
from cobra.io import load_modelLoad bundled test models
加载内置测试模型
model = load_model("textbook") # E. coli core model
model = load_model("ecoli") # Full E. coli model
model = load_model("salmonella")
model = load_model("textbook") # 大肠杆菌核心模型
model = load_model("ecoli") # 完整大肠杆菌模型
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:
```python
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-readablefrom 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")
以多种格式保存模型:
```python
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") # 人类可读格式2. Model Structure and Components
2. 模型结构与组件
Access and inspect model components:
python
undefined访问和检查模型组件:
python
undefinedAccess components
访问组件
model.reactions # DictList of all reactions
model.metabolites # DictList of all metabolites
model.genes # DictList of all genes
model.reactions # 所有反应的DictList对象
model.metabolites # 所有代谢物的DictList对象
model.genes # 所有基因的DictList对象
Get specific items by ID or index
通过ID或索引获取特定条目
reaction = model.reactions.get_by_id("PFK")
metabolite = model.metabolites[0]
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
undefinedprint(reaction.reaction) # 化学计量方程
print(reaction.bounds) # 通量约束
print(reaction.gene_reaction_rule) # GPR逻辑
print(metabolite.formula) # 化学式
print(metabolite.compartment) # 细胞定位
undefined3. Flux Balance Analysis (FBA)
3. 通量平衡分析(FBA)
Perform standard FBA simulation:
python
undefined执行标准FBA模拟:
python
undefinedBasic optimization
基础优化
solution = model.optimize()
print(f"Objective value: {solution.objective_value}")
print(f"Status: {solution.status}")
solution = model.optimize()
print(f"目标值: {solution.objective_value}")
print(f"状态: {solution.status}")
Access fluxes
访问通量
print(solution.fluxes["PFK"])
print(solution.fluxes.head())
print(solution.fluxes["PFK"])
print(solution.fluxes.head())
Fast optimization (objective value only)
快速优化(仅获取目标值)
objective_value = model.slim_optimize()
objective_value = model.slim_optimize()
Change objective
修改目标
model.objective = "ATPM"
solution = model.optimize()
Parsimonious FBA (minimize total flux):
```python
from cobra.flux_analysis import pfba
solution = pfba(model)Geometric FBA (find central solution):
python
from cobra.flux_analysis import geometric_fba
solution = geometric_fba(model)model.objective = "ATPM"
solution = model.optimize()
简约FBA(最小化总通量):
```python
from cobra.flux_analysis import pfba
solution = pfba(model)几何FBA(寻找中心解):
python
from cobra.flux_analysis import geometric_fba
solution = geometric_fba(model)4. Flux Variability Analysis (FVA)
4. 通量变异性分析(FVA)
Determine flux ranges for all reactions:
python
from cobra.flux_analysis import flux_variability_analysis确定所有反应的通量范围:
python
from cobra.flux_analysis import flux_variability_analysisStandard FVA
标准FVA
fva_result = flux_variability_analysis(model)
fva_result = flux_variability_analysis(model)
FVA at 90% optimality
90%最优性下的FVA
fva_result = flux_variability_analysis(model, fraction_of_optimum=0.9)
fva_result = flux_variability_analysis(model, fraction_of_optimum=0.9)
Loopless FVA (eliminates thermodynamically infeasible loops)
无循环FVA(消除热力学不可行循环)
fva_result = flux_variability_analysis(model, loopless=True)
fva_result = flux_variability_analysis(model, loopless=True)
FVA for specific reactions
针对特定反应的FVA
fva_result = flux_variability_analysis(
model,
reaction_list=["PFK", "FBA", "PGI"]
)
undefinedfva_result = flux_variability_analysis(
model,
reaction_list=["PFK", "FBA", "PGI"]
)
undefined5. Gene and Reaction Deletion Studies
5. 基因与反应敲除研究
Perform knockout analyses:
python
from cobra.flux_analysis import (
single_gene_deletion,
single_reaction_deletion,
double_gene_deletion,
double_reaction_deletion
)执行敲除分析:
python
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)
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
)
double_gene_results = double_gene_deletion(
model,
processes=4 # CPU核心数
)
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}")
with model:
model.genes.get_by_id("b0008").knock_out()
solution = model.optimize()
print(f"敲除后的生长速率: {solution.objective_value}")
Model automatically reverts after context exit
退出上下文后模型自动恢复原状
undefinedundefined6. Growth Media and Minimal Media
6. 生长培养基与最小培养基
Manage growth medium:
python
undefined管理生长培养基:
python
undefinedView current medium
查看当前培养基
print(model.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
medium = model.medium
medium["EX_glc__D_e"] = 10.0 # 设置葡萄糖摄取量
medium["EX_o2_e"] = 0.0 # 厌氧条件
model.medium = medium
Calculate minimal media
计算最小培养基
from cobra.medium import minimal_medium
from cobra.medium import minimal_medium
Minimize total import flux
最小化总输入通量
min_medium = minimal_medium(model, minimize_components=False)
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
)
undefinedmin_medium = minimal_medium(
model,
minimize_components=True,
open_exchanges=True
)
undefined7. Flux Sampling
7. 通量采样
Sample the feasible flux space:
python
from cobra.sampling import sample对可行通量空间进行采样:
python
from cobra.sampling import sampleSample using OptGP (default, supports parallel processing)
使用OptGP采样(默认,支持并行处理)
samples = sample(model, n=1000, method="optgp", processes=4)
samples = sample(model, n=1000, method="optgp", processes=4)
Sample using ACHR
使用ACHR采样
samples = sample(model, n=1000, method="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
undefinedfrom cobra.sampling import OptGPSampler
sampler = OptGPSampler(model, processes=4)
sampler.sample(1000)
validation = sampler.validate(sampler.samples)
print(validation.value_counts()) # 全部为'v'表示有效
undefined8. Production Envelopes
8. 生产包络分析
Calculate phenotype phase planes:
python
from cobra.flux_analysis import production_envelope计算表型相平面:
python
from cobra.flux_analysis import production_envelopeStandard production envelope
标准生产包络分析
envelope = production_envelope(
model,
reactions=["EX_glc__D_e", "EX_o2_e"],
objective="EX_ac_e" # Acetate production
)
envelope = production_envelope(
model,
reactions=["EX_glc__D_e", "EX_o2_e"],
objective="EX_ac_e" # 乙酸生产
)
With carbon yield
包含碳产量的分析
envelope = production_envelope(
model,
reactions=["EX_glc__D_e", "EX_o2_e"],
carbon_sources="EX_glc__D_e"
)
envelope = production_envelope(
model,
reactions=["EX_glc__D_e", "EX_o2_e"],
carbon_sources="EX_glc__D_e"
)
Visualize (use matplotlib or pandas plotting)
可视化(使用matplotlib或pandas绘图)
import matplotlib.pyplot as plt
envelope.plot(x="EX_glc__D_e", y="EX_o2_e", kind="scatter")
plt.show()
undefinedimport matplotlib.pyplot as plt
envelope.plot(x="EX_glc__D_e", y="EX_o2_e", kind="scatter")
plt.show()
undefined9. Gapfilling
9. 缺口填补
Add reactions to make models feasible:
python
from cobra.flux_analysis import gapfill添加反应使模型可行:
python
from cobra.flux_analysis import gapfillPrepare universal model with candidate reactions
准备包含候选反应的通用模型
universal = load_model("universal")
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}")undefinedwith model:
# 删除反应以制造缺口用于演示
model.remove_reactions([model.reactions.PGI])
# 查找需要添加的反应
solution = gapfill(model, universal)
print(f"需要添加的反应: {solution}")undefined10. Model Building
10. 模型构建
Build models from scratch:
python
from cobra import Model, Reaction, Metabolite从头构建模型:
python
from cobra import Model, Reaction, MetaboliteCreate model
创建模型
model = Model("my_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")
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
reaction = Reaction("ATPASE")
reaction.name = "ATP水解"
reaction.subsystem = "能量代谢"
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
})
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"
reaction.gene_reaction_rule = "(gene1 and gene2) or gene3"
Add to model
添加到模型
model.add_reactions([reaction])
model.add_reactions([reaction])
Add boundary reactions
添加边界反应
model.add_boundary(atp_c, type="exchange")
model.add_boundary(adp_c, type="demand")
model.add_boundary(atp_c, type="exchange")
model.add_boundary(adp_c, type="demand")
Set objective
设置目标
model.objective = "ATPASE"
undefinedmodel.objective = "ATPASE"
undefinedCommon Workflows
常见工作流
Workflow 1: Load Model and Predict Growth
工作流1:加载模型并预测生长速率
python
from cobra.io import load_modelpython
from cobra.io import load_modelLoad model
加载模型
model = load_model("ecoli")
model = load_model("ecoli")
Run FBA
运行FBA
solution = model.optimize()
print(f"Growth rate: {solution.objective_value:.3f} /h")
solution = model.optimize()
print(f"生长速率: {solution.objective_value:.3f} /h")
Show active pathways
显示活跃通路
print(solution.fluxes[solution.fluxes.abs() > 1e-6])
undefinedprint(solution.fluxes[solution.fluxes.abs() > 1e-6])
undefinedWorkflow 2: Gene Knockout Screen
工作流2:基因敲除筛选
python
from cobra.io import load_model
from cobra.flux_analysis import single_gene_deletionpython
from cobra.io import load_model
from cobra.flux_analysis import single_gene_deletionLoad model
加载模型
model = load_model("ecoli")
model = load_model("ecoli")
Perform single gene deletions
执行单基因敲除
results = single_gene_deletion(model)
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")
essential_genes = results[results["growth"] < 0.01]
print(f"找到 {len(essential_genes)} 个必需基因")
Find genes with minimal impact
查找影响极小的基因
neutral_genes = results[results["growth"] > 0.9 * solution.objective_value]
undefinedneutral_genes = results[results["growth"] > 0.9 * solution.objective_value]
undefinedWorkflow 3: Media Optimization
工作流3:培养基优化
python
from cobra.io import load_model
from cobra.medium import minimal_mediumpython
from cobra.io import load_model
from cobra.medium import minimal_mediumLoad model
加载模型
model = load_model("ecoli")
model = load_model("ecoli")
Calculate minimal medium for 50% of max growth
计算支持50%最大生长速率的最小培养基
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)
undefinedtarget_growth = model.slim_optimize() * 0.5
min_medium = minimal_medium(
model,
target_growth,
minimize_components=True
)
print(f"最小培养基组件数量: {len(min_medium)}")
print(min_medium)
undefinedWorkflow 4: Flux Uncertainty Analysis
工作流4:通量不确定性分析
python
from cobra.io import load_model
from cobra.flux_analysis import flux_variability_analysis
from cobra.sampling import samplepython
from cobra.io import load_model
from cobra.flux_analysis import flux_variability_analysis
from cobra.sampling import sampleLoad model
加载模型
model = load_model("ecoli")
model = load_model("ecoli")
First check flux ranges at optimality
首先检查最优状态下的通量范围
fva = flux_variability_analysis(model, fraction_of_optimum=1.0)
fva = flux_variability_analysis(model, fraction_of_optimum=1.0)
For reactions with large ranges, sample to understand distribution
对范围较大的反应进行采样以了解分布
samples = sample(model, n=1000)
reaction_id = "PFK"
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()
undefinedimport matplotlib.pyplot as plt
samples[reaction_id].hist(bins=50)
plt.xlabel(f"{reaction_id}的通量")
plt.ylabel("频率")
plt.show()
undefinedWorkflow 5: Context Manager for Temporary Changes
工作流5:使用上下文管理器进行临时修改
Use context managers to make temporary modifications:
python
undefined使用上下文管理器进行临时修改:
python
undefinedModel 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}")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}")All changes automatically reverted
所有修改自动恢复
solution = model.optimize()
print(f"Original growth: {solution.objective_value}")
undefinedsolution = model.optimize()
print(f"原始生长速率: {solution.objective_value}")
undefinedKey Concepts
核心概念
DictList Objects
DictList对象
Models use objects for reactions, metabolites, and genes - behaving like both lists and dictionaries:
DictListpython
undefined模型使用对象存储反应、代谢物和基因,兼具列表和字典的特性:
DictListpython
undefinedAccess by index
通过索引访问
first_reaction = model.reactions[0]
first_reaction = model.reactions[0]
Access by ID
通过ID访问
pfk = model.reactions.get_by_id("PFK")
pfk = model.reactions.get_by_id("PFK")
Query methods
查询方法
atp_reactions = model.reactions.query("atp")
undefinedatp_reactions = model.reactions.query("atp")
undefinedFlux Constraints
通量约束
Reaction bounds define feasible flux ranges:
- Irreversible:
lower_bound = 0, upper_bound > 0 - Reversible:
lower_bound < 0, upper_bound > 0 - Set both bounds simultaneously with to avoid inconsistencies
.bounds
反应边界定义可行的通量范围:
- 不可逆反应:
lower_bound = 0, upper_bound > 0 - 可逆反应:
lower_bound < 0, upper_bound > 0 - 使用同时设置上下边界,避免不一致
.bounds
Gene-Reaction Rules (GPR)
基因-反应规则(GPR)
Boolean logic linking genes to reactions:
python
undefined连接基因与反应的布尔逻辑:
python
undefinedAND logic (both required)
AND逻辑(同时需要)
reaction.gene_reaction_rule = "gene1 and gene2"
reaction.gene_reaction_rule = "gene1 and gene2"
OR logic (either sufficient)
OR逻辑(任一即可)
reaction.gene_reaction_rule = "gene1 or gene2"
reaction.gene_reaction_rule = "gene1 or gene2"
Complex logic
复杂逻辑
reaction.gene_reaction_rule = "(gene1 and gene2) or (gene3 and gene4)"
undefinedreaction.gene_reaction_rule = "(gene1 and gene2) or (gene3 and gene4)"
undefinedExchange Reactions
交换反应
Special reactions representing metabolite import/export:
- Named with prefix by convention
EX_ - Positive flux = secretion, negative flux = uptake
- Managed through dictionary
model.medium
代表代谢物导入/导出的特殊反应:
- 惯例以为前缀命名
EX_ - 正通量=分泌,负通量=摄取
- 通过字典管理
model.medium
Best Practices
最佳实践
- Use context managers for temporary modifications to avoid state management issues
- Validate models before analysis using to ensure feasibility
model.slim_optimize() - Check solution status after optimization - indicates successful solve
optimal - Use loopless FVA when thermodynamic feasibility matters
- Set fraction_of_optimum appropriately in FVA to explore suboptimal space
- Parallelize computationally expensive operations (sampling, double deletions)
- Prefer SBML format for model exchange and long-term storage
- Use slim_optimize() when only objective value needed for performance
- Validate flux samples to ensure numerical stability
- 使用上下文管理器进行临时修改,避免状态管理问题
- 分析前验证模型,使用确保模型可行
model.slim_optimize() - 优化后检查解决方案状态 - 表示求解成功
optimal - 当热力学可行性重要时,使用无循环FVA
- 在FVA中合理设置fraction_of_optimum以探索次优空间
- 并行化计算密集型操作(采样、双敲除)
- 模型交换和长期存储优先使用SBML格式
- **仅需目标值时使用slim_optimize()**以提升性能
- 验证通量采样结果确保数值稳定性
Troubleshooting
故障排除
Infeasible solutions: Check medium constraints, reaction bounds, and model consistency
Slow optimization: Try different solvers (GLPK, CPLEX, Gurobi) via
Unbounded solutions: Verify exchange reactions have appropriate upper bounds
Import errors: Ensure correct file format and valid SBML identifiers
model.solver无解问题:检查培养基约束、反应边界和模型一致性
优化速度慢:尝试通过更换求解器(GLPK、CPLEX、Gurobi)
无界解:验证交换反应是否设置了合适的上边界
导入错误:确保文件格式正确且SBML标识符有效
model.solverReferences
参考资料
For detailed workflows and API patterns, refer to:
- - Comprehensive step-by-step workflow examples
references/workflows.md - - Common function signatures and patterns
references/api_quick_reference.md
Official documentation: https://cobrapy.readthedocs.io/en/latest/
如需详细工作流和API模式,请参考:
- - 全面的分步工作流示例
references/workflows.md - - 常用函数签名和模式
references/api_quick_reference.md