$npx -y skills add K-Dense-AI/scientific-agent-skills --skill cobrapyConstraint-based metabolic modeling (COBRA). FBA, FVA, gene knockouts, flux sampling, SBML models, for systems biology and metabolic engineering analysis.
| 1 | # COBRApy - Constraint-Based Reconstruction and Analysis |
| 2 | |
| 3 | ## Overview |
| 4 | |
| 5 | 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. |
| 6 | |
| 7 | **Version note:** Examples target **cobra 0.31.1** on PyPI (import `cobra`). Docs: [cobrapy.readthedocs.io](https://cobrapy.readthedocs.io/en/latest/). Repo: [opencobra/cobrapy](https://github.com/opencobra/cobrapy). |
| 8 | |
| 9 | ## When to Use This Skill |
| 10 | |
| 11 | Use this skill when: |
| 12 | - Loading, building, or exporting genome-scale metabolic models (SBML, JSON, YAML) |
| 13 | - Running FBA, pFBA, FVA, or flux sampling on COBRA models |
| 14 | - Performing gene or reaction knockout screens and production envelope analysis |
| 15 | - Designing or optimizing growth media and exchange constraints |
| 16 | - Gap-filling infeasible models or validating model consistency |
| 17 | |
| 18 | ## Installation |
| 19 | |
| 20 | ```bash |
| 21 | uv pip install "cobra==0.31.1" |
| 22 | ``` |
| 23 | |
| 24 | MATLAB model I/O (optional): |
| 25 | |
| 26 | ```bash |
| 27 | uv pip install "cobra[array]==0.31.1" |
| 28 | ``` |
| 29 | |
| 30 | COBRApy uses [optlang](https://optlang.readthedocs.io/) for solvers. GLPK installs automatically via `swiglpk`. For large MILPs/QPs, cobra 0.29+ adds a **hybrid** solver (HIGHS/OSQP); `model.solver = "osqp"` now routes through hybrid and may error on plain LPs in a future release—prefer `model.solver = "hybrid"` when available. |
| 31 | |
| 32 | ## Core Capabilities |
| 33 | |
| 34 | COBRApy provides comprehensive tools organized into several key areas: |
| 35 | |
| 36 | ### 1. Model Management |
| 37 | |
| 38 | Load existing models from repositories or files: |
| 39 | ```python |
| 40 | from cobra.io import load_model |
| 41 | |
| 42 | # Bundled locally (no network): textbook, iJO1366, salmonella |
| 43 | model = load_model("textbook") # alias for e_coli_core (95 reactions) |
| 44 | model = load_model("e_coli_core") # same core E. coli model |
| 45 | model = load_model("iJO1366") # genome-scale E. coli (bundled) |
| 46 | model = load_model("salmonella") # Salmonella iYS1720 (bundled) |
| 47 | |
| 48 | # Remote (BiGG / BioModels; requires network, cached after first fetch) |
| 49 | model = load_model("iML1515") # E. coli genome-scale on BiGG |
| 50 | |
| 51 | # Load from files |
| 52 | from cobra.io import read_sbml_model, load_json_model, load_yaml_model |
| 53 | model = read_sbml_model("path/to/model.xml") |
| 54 | model = load_json_model("path/to/model.json") |
| 55 | model = load_yaml_model("path/to/model.yml") |
| 56 | ``` |
| 57 | |
| 58 | Save models in various formats: |
| 59 | ```python |
| 60 | from cobra.io import write_sbml_model, save_json_model, save_yaml_model |
| 61 | write_sbml_model(model, "output.xml") # Preferred format |
| 62 | save_json_model(model, "output.json") # For Escher compatibility |
| 63 | save_yaml_model(model, "output.yml") # Human-readable |
| 64 | ``` |
| 65 | |
| 66 | ### 2. Model Structure and Components |
| 67 | |
| 68 | Access and inspect model components: |
| 69 | ```python |
| 70 | # Access components |
| 71 | model.reactions # DictList of all reactions |
| 72 | model.metabolites # DictList of all metabolites |
| 73 | model.genes # DictList of all genes |
| 74 | |
| 75 | # Get specific items by ID or index |
| 76 | reaction = model.reactions.get_by_id("PFK") |
| 77 | metabolite = model.metabolites[0] |
| 78 | |
| 79 | # Inspect properties |
| 80 | print(reaction.reaction) # Stoichiometric equation |
| 81 | print(reaction.bounds) # Flux constraints |
| 82 | print(reaction.gene_reaction_rule) # GPR logic |
| 83 | print(metabolite.formula) # Chemical formula |
| 84 | print(metabolite.compartment) # Cellular location |
| 85 | ``` |
| 86 | |
| 87 | ### 3. Flux Balance Analysis (FBA) |
| 88 | |
| 89 | Perform standard FBA simulation: |
| 90 | ```python |
| 91 | # Basic optimization |
| 92 | solution = model.optimize() |
| 93 | print(f"Objective value: {solution.objective_value}") |
| 94 | print(f"Status: {solution.status}") |
| 95 | |
| 96 | # Access fluxes |
| 97 | print(solution.fluxes["PFK"]) |
| 98 | print(solution.fluxes.head()) |
| 99 | |
| 100 | # Fast optimization (objective value only) |
| 101 | objective_value = model.slim_optimize() |
| 102 | |
| 103 | # Change objective |
| 104 | model.objective = "ATPM" |
| 105 | solution = model.optimize() |
| 106 | ``` |
| 107 | |
| 108 | Parsimonious FBA (minimize total flux): |
| 109 | ```python |
| 110 | from cobra.flux_analysis import pfba |
| 111 | solution = pfba(model) |
| 112 | ``` |
| 113 | |
| 114 | Geometric FBA (find central solution): |
| 115 | ```python |
| 116 | from cobra.flux_analysis import geometric_fba |
| 117 | solution = geometric_fba(model) |
| 118 | ``` |
| 119 | |
| 120 | ### 4. Flux Variability Analysis (FVA) |
| 121 | |
| 122 | Determine flux ranges for all reactions: |
| 123 | ```python |
| 124 | from cobra.flux_analysis import flux_variability_analysis |
| 125 | |
| 126 | # Standard FVA |
| 127 | fva_result = flux_variability_analysis(model) |
| 128 | |
| 129 | # FVA at 90% optimality |
| 130 | fva_result = flux_variability_analysis(model, fraction_of_optimum=0.9) |
| 131 | |
| 132 | # Loopless FVA (eliminates thermodynamically infeasible loops) |
| 133 | fva_result = f |