Cobrapy

From aipoch/medical-research-skills · View interactive version

Cobrapy is an AI agent skill from the GitHub repository aipoch/medical-research-skills. Constraint-based reconstruction and analysis (COBRA) for metabolic models; use when you need to simulate growth/production, analyze flux ranges, or run knockout and medium studies from SBML/JSON/YAML models.

How to use this skill

Skill file

Source: https://github.com/aipoch/medical-research-skills

COBRApy (COBRA: Constraint-Based Reconstruction and Analysis)

When to Use

Key Features

Dependencies

Example Usage

Skill directory: 20260316/scientific-skills/Data Analytics/cobrapy
No packaged executable script was detected.
Use the documented workflow in SKILL.md together with the references/assets in this folder.

Example run plan:

  1. Read the skill instructions and collect the required inputs.
  2. Follow the documented workflow exactly.
  3. Use packaged references/assets from this folder when the task needs templates or rules.
  4. Return a structured result tied to the requested deliverable.

Implementation Details

1. When to Use

Use this skill when you need to perform constraint-based analysis on metabolic networks, especially for:

  1. Predicting growth or production under specific media and objectives using Flux Balance Analysis (FBA).
  2. Quantifying flux uncertainty and alternative optima using Flux Variability Analysis (FVA) and flux sampling.
  3. Identifying essential genes/reactions via single/double knockout (deletion) studies.
  4. Designing or optimizing media (e.g., minimal medium) to support a target growth rate.
  5. Repairing infeasible models by gapfilling against a universal reaction database/model.

2. Key Features

3. Dependencies

4. Example Usage

The following script is a complete, runnable example that loads a built-in model, runs FBA, performs FVA, runs a gene knockout, adjusts medium, and samples fluxes.


# cobrapy_example.py
from cobra.io import load_model
from cobra.flux_analysis import flux_variability_analysis, single_gene_deletion, pfba
from cobra.sampling import sample

def main():
    # 1) Load a model (built-in test model)
    model = load_model("textbook")  # E. coli core model

    # 2) Run standard FBA
    sol = model.optimize()
    print("=== FBA ===")
    print("Status:", sol.status)
    print("Objective (growth):", sol.objective_value)

    # 3) Run pFBA (minimize total flux at optimal growth)
    pfba_sol = pfba(model)
    print("\n=== pFBA ===")
    print("Objective (growth):", pfba_sol.objective_value)

    # 4) Flux Variability Analysis at 90% of optimum
    print("\n=== FVA (90% optimum) ===")
    fva = flux_variability_analysis(model, fraction_of_optimum=0.9)
    print(fva.head())

    # 5) Single gene deletion screen (may take time on large models)
    print("\n=== Single Gene Deletion (first 5 rows) ===")
    del_res = single_gene_deletion(model)
    print(del_res.head())

    # 6) Medium modification (must re-assign the full dict)
    print("\n=== Medium ===")
    medium = model.medium
    # Example: limit glucose uptake (exchange IDs depend on the model)
    if "EX_glc__D_e" in medium:
        medium["EX_glc__D_e"] = 5.0
        model.medium = medium
        sol2 = model.optimize()
        print("Growth after limiting glucose:", sol2.objective_value)
    else:
        print("Model has no EX_glc__D_e in medium; skipping medium edit.")

    # 7) Flux sampling (small n for quick demo)
    print("\n=== Flux Sampling ===")
    samples = sample(model, n=200, method="optgp")
    print(samples.head())

if __name__ == "__main__":
    main()

Run:

python cobrapy_example.py

5. Implementation Details

5.1 Core optimization model (FBA)

5.2 Reaction directionality and bounds

5.3 Gene-Protein-Reaction (GPR) rules

5.4 FVA parameters

5.5 Context manager for temporary edits

5.6 Flux sampling

5.7 Medium handling

5.8 Gapfilling

When Not to Use

Required Inputs

Recommended Workflow

  1. Validate the request against the skill boundary and confirm all required inputs are present.
  2. Select the documented execution path and prefer the simplest supported command or procedure.
  3. Produce the expected output using the documented file format, schema, or narrative structure.
  4. Run a final validation pass for completeness, consistency, and safety before returning the result.

Deterministic Output Rules

Output Contract

Validation and Safety Rules

Failure Handling

Completion Checklist

Quick Validation

Run this minimal verification path before full execution when possible:

No local script validation step is required for this skill.

Expected output format:

Result file: cobrapy_result.md
Validation summary: PASS/FAIL with brief notes
Assumptions: explicit list if any

Scope Reminder

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.