cobrapy

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

COBRApy - 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_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")
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-readable
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")

以多种格式保存模型:
```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
undefined

Access 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
undefined
print(reaction.reaction) # 化学计量方程 print(reaction.bounds) # 通量约束 print(reaction.gene_reaction_rule) # GPR逻辑 print(metabolite.formula) # 化学式 print(metabolite.compartment) # 细胞定位
undefined

3. Flux Balance Analysis (FBA)

3. 通量平衡分析(FBA)

Perform standard FBA simulation:
python
undefined
执行标准FBA模拟:
python
undefined

Basic 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_analysis

Standard 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"] )
undefined
fva_result = flux_variability_analysis( model, reaction_list=["PFK", "FBA", "PGI"] )
undefined

5. 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

退出上下文后模型自动恢复原状

undefined
undefined

6. Growth Media and Minimal Media

6. 生长培养基与最小培养基

Manage growth medium:
python
undefined
管理生长培养基:
python
undefined

View 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 )
undefined
min_medium = minimal_medium( model, minimize_components=True, open_exchanges=True )
undefined

7. Flux Sampling

7. 通量采样

Sample the feasible flux space:
python
from cobra.sampling import sample
对可行通量空间进行采样:
python
from cobra.sampling import sample

Sample 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
undefined
from cobra.sampling import OptGPSampler sampler = OptGPSampler(model, processes=4) sampler.sample(1000) validation = sampler.validate(sampler.samples) print(validation.value_counts()) # 全部为'v'表示有效
undefined

8. Production Envelopes

8. 生产包络分析

Calculate phenotype phase planes:
python
from cobra.flux_analysis import production_envelope
计算表型相平面:
python
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 )
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()
undefined
import matplotlib.pyplot as plt envelope.plot(x="EX_glc__D_e", y="EX_o2_e", kind="scatter") plt.show()
undefined

9. Gapfilling

9. 缺口填补

Add reactions to make models feasible:
python
from cobra.flux_analysis import gapfill
添加反应使模型可行:
python
from cobra.flux_analysis import gapfill

Prepare 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}")
undefined
with model: # 删除反应以制造缺口用于演示 model.remove_reactions([model.reactions.PGI])
# 查找需要添加的反应
solution = gapfill(model, universal)
print(f"需要添加的反应: {solution}")
undefined

10. Model Building

10. 模型构建

Build models from scratch:
python
from cobra import Model, Reaction, Metabolite
从头构建模型:
python
from cobra import Model, Reaction, Metabolite

Create 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"
undefined
model.objective = "ATPASE"
undefined

Common Workflows

常见工作流

Workflow 1: Load Model and Predict Growth

工作流1:加载模型并预测生长速率

python
from cobra.io import load_model
python
from cobra.io import load_model

Load 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])
undefined
print(solution.fluxes[solution.fluxes.abs() > 1e-6])
undefined

Workflow 2: Gene Knockout Screen

工作流2:基因敲除筛选

python
from cobra.io import load_model
from cobra.flux_analysis import single_gene_deletion
python
from cobra.io import load_model
from cobra.flux_analysis import single_gene_deletion

Load 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]
undefined
neutral_genes = results[results["growth"] > 0.9 * solution.objective_value]
undefined

Workflow 3: Media Optimization

工作流3:培养基优化

python
from cobra.io import load_model
from cobra.medium import minimal_medium
python
from cobra.io import load_model
from cobra.medium import minimal_medium

Load 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)
undefined
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)
undefined

Workflow 4: Flux Uncertainty Analysis

工作流4:通量不确定性分析

python
from cobra.io import load_model
from cobra.flux_analysis import flux_variability_analysis
from cobra.sampling import sample
python
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")
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()
undefined
import matplotlib.pyplot as plt samples[reaction_id].hist(bins=50) plt.xlabel(f"{reaction_id}的通量") plt.ylabel("频率") plt.show()
undefined

Workflow 5: Context Manager for Temporary Changes

工作流5:使用上下文管理器进行临时修改

Use context managers to make temporary modifications:
python
undefined
使用上下文管理器进行临时修改:
python
undefined

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}")
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}")
undefined
solution = model.optimize() print(f"原始生长速率: {solution.objective_value}")
undefined

Key Concepts

核心概念

DictList Objects

DictList对象

Models use
DictList
objects for reactions, metabolites, and genes - behaving like both lists and dictionaries:
python
undefined
模型使用
DictList
对象存储反应、代谢物和基因,兼具列表和字典的特性:
python
undefined

Access 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")
undefined
atp_reactions = model.reactions.query("atp")
undefined

Flux 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
    .bounds
    to avoid inconsistencies
反应边界定义可行的通量范围:
  • 不可逆反应
    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
undefined

AND 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)"
undefined
reaction.gene_reaction_rule = "(gene1 and gene2) or (gene3 and gene4)"
undefined

Exchange Reactions

交换反应

Special reactions representing metabolite import/export:
  • Named with prefix
    EX_
    by convention
  • Positive flux = secretion, negative flux = uptake
  • Managed through
    model.medium
    dictionary
代表代谢物导入/导出的特殊反应:
  • 惯例以
    EX_
    为前缀命名
  • 正通量=分泌,负通量=摄取
  • 通过
    model.medium
    字典管理

Best Practices

最佳实践

  1. Use context managers for temporary modifications to avoid state management issues
  2. Validate models before analysis using
    model.slim_optimize()
    to ensure feasibility
  3. Check solution status after optimization -
    optimal
    indicates successful solve
  4. Use loopless FVA when thermodynamic feasibility matters
  5. Set fraction_of_optimum appropriately in FVA to explore suboptimal space
  6. Parallelize computationally expensive operations (sampling, double deletions)
  7. Prefer SBML format for model exchange and long-term storage
  8. Use slim_optimize() when only objective value needed for performance
  9. Validate flux samples to ensure numerical stability
  1. 使用上下文管理器进行临时修改,避免状态管理问题
  2. 分析前验证模型,使用
    model.slim_optimize()
    确保模型可行
  3. 优化后检查解决方案状态 -
    optimal
    表示求解成功
  4. 当热力学可行性重要时,使用无循环FVA
  5. 在FVA中合理设置fraction_of_optimum以探索次优空间
  6. 并行化计算密集型操作(采样、双敲除)
  7. 模型交换和长期存储优先使用SBML格式
  8. **仅需目标值时使用slim_optimize()**以提升性能
  9. 验证通量采样结果确保数值稳定性

Troubleshooting

故障排除

Infeasible 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
无解问题:检查培养基约束、反应边界和模型一致性 优化速度慢:尝试通过
model.solver
更换求解器(GLPK、CPLEX、Gurobi) 无界解:验证交换反应是否设置了合适的上边界 导入错误:确保文件格式正确且SBML标识符有效

References

参考资料

For detailed workflows and API patterns, refer to:
  • references/workflows.md
    - Comprehensive step-by-step workflow examples
  • references/api_quick_reference.md
    - Common function signatures and patterns
如需详细工作流和API模式,请参考:
  • references/workflows.md
    - 全面的分步工作流示例
  • references/api_quick_reference.md
    - 常用函数签名和模式