User:Jarle Pahr/CobraPy

From OpenWetWare
Jump to: navigation, search

Notes on the Python version of OpenCOBRA:!forum/cobra-pie


CobraPy @ Github:

Implemented features

Flux analysis package:

  • Double deletion
  • MOMA
  • Essentiality (assess_medium_component_essentiality)
  • Objective (assess objective, update objective)
  • Single deletion
  • Variability (find blocked reactions, FVA)
  • I/O: Load Matlab model, save matlab model.
  • SBML: Create Cobra model from SBML, write Cobra model to SBML
  • Manipulate: Prune unused metabolites, prune unused reactions.
  • Modify: Initalize growth medium
  • mlab:
  • Solvers: cplex, glpk, gurobi.
  • Stats

MATLAB Cobra Toolbox Features


  • MOMA
  • OMNI
  • Detect dead ends (metabolites which either participate in only one reaction or can only be produced or consumed)
  • Double robustness analysis
  • Dynamic FBA
  • Extract subnetwork
  • Find synthetic lethals
  • Find metabolic junctions
  • Find neighbor reactions
  • FVA
  • Geometric FBA
  • Linear MOMA
  • Optimize two models (minimize difference between flux vectors)
  • PhenotypePhasePlanes
  • Random objective FBA solution
  • Robustness analysis
  • Gap filling


  • Genetic Design Through Local Search
  • OptKnock
  • OptGene
  • Random KO
  • Simple OptKnock



  • Find orphan reactions
  • GapFind


  • Output Cytoscape network


  • Add sink reactions
  • Check biomass precursor synthesis

CobraPy Missing features

Cobra Toolbox documentation for reference:

  • OptKnock?
  • Fluxomics?
  • rFBA?


Basic commands

Import model create and write routines:

from import create_cobra_model_from_sbml_file
from import write_cobra_model_to_sbml_file


from cobra.test import test_all

Import sbml routines:

from import create_cobra_model_from_sbml_file
from import write_cobra_model_to_sbml_file

Create cobra model from SBML file:

cobra_model = create_cobra_model_from_sbml_file(sbml_file, print_time = True)

Save Cobra model as SBML file:

write_cobra_model_to_sbml_file(cobra_model, sbml_out_file, sbml_level, sbml_version, print_time = True)

Run optimization:


Display result:

print '\nSimulated growth rate is %1.3f' % cobra_model.solution.f

Set growth medium:

from cobra.manipulation import initialize_growth_medium
initialize_growth_medium(cobra_model, 'LB')

Test effect of gene deletion on growth rates:

from cobra.flux_analysis import single_deletion
from cPickle import load
from time import time
from cobra.test import salmonella_pickle #This is the name of the test file
with open(salmonella_pickle) as in_file:
   cobra_model = load(in_file)
target_genes =  ['STM4081', 'STM0247', 'STM3867', 'STM2952']
rates, statuses, problems = single_deletion(cobra_model, target_genes)

Change objective function:

new_objective = cobra_model.reactions.get_by_id('3OAS140')

Add a reaction:

from cobra import Reaction
reaction = Reaction('My_reaction') = 'Testreaction'
reaction.subsystem = 'Central carbon metabolism'
reaction.lower_bound = 0. 
reaction.upper_bound = 1000.
reaction.reversibility = 0
reaction.objective_coefficient =0.

Creating metabolite:

from cobra import Metabolite
ACP_c = Metabolite('ACP_c', formula = 'C11H21N2O9PRS',name='acyl-carrier-protein', compartment='c')

Add metabolites to reaction:

reaction.add_metabolites({malACP_c: -1.0,h_c: -1.0}

Display reaction formula:

print reaction.reaction



Core package

Metabolite module:

  • get_model()
  • get_reactions()
  • remove_from_model()

Model module:

  • add_metabolites(metabolite_list) #Adds metabolites to the model and expands the stoichiometric matrix.
  • add_reaction(reaction)
  • add_reactions(reaction_list)

Reaction module:


Adds a boolean gene requirement rule. See Schellenberger et al 2011 Nature Protocols 6(9):1290-307.

add_metabolites(the_metabolites, combine=True, add_to_container_model=True)

  • the_metabolites: dict of metabolites and stoichiometric coefficients
  • combine: If the metabolite already exists in the reaction, and combine = True, coefficients will be added together. If False, the old value is discarded.
  • add_to_container: If true and the reaction exists within a model object, the metabolite is added to the model.


optimize(new_objective=None, objective_sense='maximize', the_problem=None, solver='glpk', error_reporting=None, quadratic_component=None, tolerance_optimality=1e-06, tolerance_feasibility=1e-06, tolerance_barrier=1e-10, **kwargs)

Flux analysis package

io package

manipulation package


Ebrahim A, Lerman JA, Palsson BO, Hyduke DR✝. COBRApy: COnstraints-Based Reconstruction and Analysis for Python. BMC Syst Bio.:



Todo delete Module

Mailing lists!forum/cobra-pie!forum/cobra-toolbox