core Package

core Package

ArrayBasedModel Module

class cobra.core.ArrayBasedModel.ArrayBasedModel(description=None, deepcopy_model=False)[source]

Bases: cobra.core.Model.Model

ArrayBasedModel is a class that adds arrays and vectors to a cobra.Model to make it easier to perform linear algebra operations.

S : scipy.sparse.lil_matrix (in CPython)
Stoichiometric matrix of the model

lower_bounds

upper_bounds

objective_coefficients

b

constraint_sense

add_metabolites(metabolite_list, expand_stoichiometric_matrix=True)[source]

Will add a list of metabolites to the the object, if they do not exist and then expand the stochiometric matrix

metabolite_list: A list of Metabolite objects

expand_stoichimetric_matrix: Boolean. If True and self.S is not None then it will add rows to self.S. self.S must be created after adding reactions and metabolites to self before it can be expanded. Trying to expand self.S when self only contains metabolites is ludacris.

add_reactions(reaction_list, update_matrices=False)[source]

Will add a cobra.Reaction object to the model, if reaction.id is not in self.reactions.

reaction_list: A Reaction object or a list of them

update_matrices: Boolean. If true populate / update matrices S, lower_bounds, upper_bounds, .... Note this is slow to run for very large models and using this option with repeated calls will degrade performance. Better to call self.update() after adding all reactions.

If the stoichiometric matrix is initially empty then initialize a 1x1 sparse matrix and add more rows as needed in the self.add_metabolites function
copy(print_time=False)[source]

Provides a partial ‘deepcopy’ of the Model. All of the Metabolite, Gene, and Reaction objects are created anew but in a faster fashion than deepcopy

print_time: Boolean used for debugging

update()[source]

Regenerates the stoichiometric matrix and vectors

DictList Module

class cobra.core.DictList.DictList(*args, **kwargs)[source]

Bases: list

A combined dict and list that feels like a list, but has the speed benefits of a dict. This may be eventually replaced by collections.OrderedDict.

This was written to address the performance issues associated with searching, accessing, or iterating over a list in python that resulted in notable performance decays with COBRA for python.

append(object)[source]
extend(iterable)[source]
get_by_id(id)[source]

return the element with a matching id

index(id)[source]

id: A string or a Object

insert(index, object)[source]
list_attr(attribute)[source]

return a list of the given attribute for every object

pop(*args, **kwargs)[source]
query(search_function, attribute='id')[source]

query the list

search_function: this will be used to select which objects to return This can be:

  • a string, in which case any object.attribute containing the string will be returned
  • a compiled regular expression
  • a boolean function which takes one argument and returns True for desired values
attribute: the attribute to be searched for (default is ‘id’).
If this is None, the object itself is used.

returns: a list of objects which match the query

remove(*args, **kwargs)[source]
reverse(*args, **kwargs)[source]
sort(*args, **kwargs)[source]
union(iterable)[source]

adds elements with id’s not already in the model

cobra.core.DictList.get_id(object)[source]

return an id for the object

This allows the function to be generalize to non-cobra.core objects, however, this added function call slows things down.

Formula Module

class cobra.core.Formula.Formula(formula=None)[source]

Bases: cobra.core.Object.Object

Formula is a class for holding information regarding a Metabolite formula.

This will replace the current way of dealing with molecular formulae in cobra.Metabolite objects

Legal formula string characters include letters, numbers, and *. If a formula string starts with a number then it is assumed that all element counts are multiplied by this number.

calculate_formula_weight(weight_dict=None)[source]

Calculate the formula weight.

weight_dict: None or a dictionary of elements and weights.

parse_composition()[source]

Breaks the chemical formula down by element. Useful for making sure Reactions are balanced.’

TODO: Find a stable python package for parsing chemical formulas.

Gene Module

class cobra.core.Gene.Gene(id, formula=None, name=None, compartment=None, strand='+', locus_start=0, locus_end=0, functional=True)[source]

Bases: cobra.core.Metabolite.Metabolite

A Gene is a special class of metabolite.

TODO: Make design decisions about TUs and such

guided_copy(the_model)[source]

Trying to make a faster copy proceedure for cases where large numbers of metabolites might be copied. Such as when copying reactions.

remove_from_model(the_model, make_dependent_reactions_nonfunctional=True)[source]

Removes the association

the_model: Model object.
Remove the reaction from this model.

make_dependent_reactions_nonfunctional: Boolean. If True then replace the gene with ‘False’ in the gene association, else replace the gene with ‘True’

TODO: Better handling of the gene association

Metabolite Module

class cobra.core.Metabolite.Metabolite(id=None, formula=None, name=None, compartment=None)[source]

Bases: cobra.core.Object.Object

Metabolite is a class for holding information regarding a metabolite in a cobra.Reaction object.

copy()[source]

When copying a reaction, it is necessary to deepcopy the components so the list references aren’t carried over.

Additionally, a copy of a reaction is no longer in a cobra.Model.

This should be fixed with self.__deecopy__ if possible

get_model()[source]

Returns the Model object that contain this Object

get_reaction()[source]

Returns a list of Reactions that contain this Object

guided_copy(the_model)[source]

Trying to make a faster copy procedure for cases where large numbers of metabolites might be copied. Such as when copying reactions.

parse_composition()[source]

Breaks the chemical formula down by element. Useful for making sure Reactions are balanced.’

remove_from_model(model=None, method='subtractive')[source]

Removes the association

model: None or Model object.
Remove the reaction from this model.

method: ‘subtractive’ or ‘destructive’. If ‘subtractive’ then the metabolite is removed from all associated reactions. If ‘destructive’ then all associated reactions are removed from the Model.

Model Module

class cobra.core.Model.Model(description=None)[source]

Bases: cobra.core.Object.Object

Model is a class for analyzing metabolic models with the COBRA toolbox developed in the Palsson Lab at UCSD. Make all of the objects (Reaction, Metabolite, ...) to make OOP easier.

add_metabolites(metabolite_list)[source]

Will add a list of metabolites to the the object, if they do not exist and then expand the stochiometric matrix

metabolite_list: A list of Metabolite objects

add_reaction(reaction)[source]

Will add a cobra.Reaction object to the model, if reaction.id is not in self.reactions.

reaction: A Reaction object

add_reactions(reaction_list)[source]

Will add a cobra.Reaction object to the model, if reaction.id is not in self.reactions.

reaction_list: A Reaction object or a list of them

copy(print_time=False)[source]

Provides a partial ‘deepcopy’ of the Model. All of the Metabolite, Gene, and Reaction objects are created anew but in a faster fashion than deepcopy

print_time: Boolean used for debugging

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)[source]

Optimize self for self._objective_coefficients or new_objective.

NOTE: Only the most commonly used parameters are presented here. Additional parameters for cobra.solvers may be available and specified with the appropriate keyword=value.

new_objective: Reaction, String, or Integer referring to a reaction in cobra_model.reactions to set as the objective. Currently, only supports single objective coeffients. Will expand to include mixed objectives.

objective_sense: ‘maximize’ or ‘minimize’

the_problem: None or a problem object for the specific solver that can be used to hot start the next solution.

solver: ‘glpk’, ‘gurobi’, or ‘cplex’

error_reporting: None or True to disable or enable printing errors encountered when trying to find the optimal solution.

quadratic_component: None or scipy.sparse.dok of dim(len(cobra_model.reactions),len(cobra_model.reactions)) If not None:

Solves quadratic programming problems for cobra_models of the form: cobra_model = ArrayBasedModel(cobra_model) minimize: 0.5 * x’ * quadratic_component * x + cobra_model._objective_coefficients’ * x such that,

cobra_model._lower_bounds <= x <= cobra_model._upper_bounds cobra_model._S * x (cobra_model._constraint_sense) cobra_model._b

#See cobra.flux_analysis.solvers for more info on the following parameters. Also, refer to your solver’s manual

tolerance_optimality: Solver tolerance for optimality.

tolerance_feasibility: Solver tolerance for feasibility.

tolerance_barrier: Solver tolerance for barrier method

lp_method: Solver method to solve the problem

#End solver parameters

**kwargs: See additional parameters for your specific solver module in cobra.solvers

remove_reactions(the_reactions)[source]

the_reactions: instance or list of cobra.Reactions or strings of self.reactions[:].id.

to_array_based_model(deepcopy_model=False)[source]

Makes a ArrayBasedModel from a cobra.Model which may be used to perform linear algebra operations with the stoichiomatric matrix.

deepcopy_model: Boolean. If False then the ArrayBasedModel points to the Model

update()[source]

Non functional. Model.update is moved to ArrayBasedModel. Please use the to_array_based_model property to create an ArrayBasedModel.

Object Module

class cobra.core.Object.Object(id=None)[source]

Bases: object

endswith(x)[source]
guided_copy()[source]

Trying to make a faster copy procedure for cases where large numbers of metabolites might be copied. Such as when copying reactions.

This function allows us to manipulate how specific attributes are copied.

startswith(x)[source]

Reaction Module

class cobra.core.Reaction.Reaction(name=None)[source]

Bases: cobra.core.Object.Object

Reaction is a class for holding information regarding a biochemical reaction in a cobra.Model object

add_gene_reaction_rule(the_rule)[source]

This adds a gene reaction rule.

the_rule: A boolean representation of the gene requirements for this reaction to be active as described in Schellenberger et al 2011 Nature Protocols 6(9):1290-307.

Note that this method currently replaces any pre-existing rules

add_metabolites(the_metabolites, combine=True, add_to_container_model=True)[source]

Add metabolites and stoichiometric coefficients to the reaction. If the final coefficient for a metabolite is 0 then it is removed from the reaction.

the_metabolites: A dict of cobra.Metabolites and their coefficient

combine: Boolean. If True and a metabolite already exists in the reaction then the coefficients will be added. If False the old metabolite will be discarded and the new one added.

add_to_container_model: Boolean. If True and this reaction is contained within a cobra.Model (i.e., self._model is a cobra.Model) then add the metabolite to the model.

build_reaction_string(use_metabolite_names=False)[source]

Generate a human readable reaction string.

check_mass_balance()[source]

Makes sure that the reaction is elementally-balanced.

copy()[source]

When copying a reaction, it is necessary to deepcopy the components so the list references aren’t carried over.

Additionally, a copy of a reaction is no longer in a cobra.Model.

This should be fixed with self.__deecopy__ if possible

delete()[source]

Removes all associations between a reaction and its container _model and metabolites and genes.

TODO: Decide whether orphan metabolites should automatically be removed from the model.

get_coefficient(the_metabolite)[source]

Return the stoichiometric coefficient for a metabolite in the reaction.

the_metabolite: A metabolite Id.

get_coefficients(the_metabolites)[source]

Return the stoichiometric coefficients for a list of metabolites in the reaction.

the_metabolites: A list of metabolite Ids.

get_compartments()[source]
get_gene()[source]

Return a list of reactants for the reaction.

get_model()[source]

Returns the Model object that this Reaction is associated with.

get_products()[source]

Return a list of products for the reaction

get_reactants()[source]

Return a list of reactants for the reaction.

guided_copy(the_model, metabolite_dict, gene_dict=None)[source]

Trying to make a faster copy procedure for cases where large numbers of metabolites might be copied. Such as when copying reactions.

parse_gene_association(the_type='gene')[source]

Extract all genes from the Boolean Gene_Association string.

#Formerly, update_names

pop(the_metabolite)[source]

Remove a metabolite from the reaction and return the stoichiometric coefficient.

the_metabolite: A cobra.Metabolite that is in the reaction

print_values()[source]

Prints most of the contents of a reaction as a series of strings.

process_prefixed_reaction()[source]

Deal with reaction names that have a prefix.

DEPRECATED This is necessary when parsing text files. It is better to get the reactions from SBML files.

This can be moved to a tools section

reconstruct_reaction()[source]

Generate a human readable reaction string.

remove_from_model(model=None)[source]

Removes the association

model: cobra.Model object. remove the reaction from this model.

subtract_metabolites(metabolites)[source]

This function will ‘subtract’ cobra.metabolites from a reaction, which means add the metabolites with -1*coefficient. If the final coefficient for a metabolite is 0 then the metabolite is removed from the reaction.

Note

That a final coefficient < 0 implies a reactant.

metabolites: dict of {Metabolite: coefficient}
These metabolites will be added to the reaction

Note

This function uses deepcopy in case the reaction is being

subtracted from itself.

Solution Module

class cobra.core.Solution.Solution(the_f, x=None, x_dict=None, y=None, y_dict=None, the_solver=None, the_time=0, status='NA')[source]

Bases: cobra.core.Object.Object

Stores the solution from optimizing a cobra.Model. This is used to provide a single interface to results from different solvers that store their values in different ways.

NOTE: This class might be deprecated in favor of associating the values with the Reactions and Metabolites in the cobra.Model.

f: The objective value

the_time: Float. Sometimes indicates how long it took to solve a problem. As this is typically negligible and not used in cobra pie, it might be deprecated.

the_solver: A string indicating which solver package was used.

x: List or Array of the values from the primal.

x_dict: A dictionary of reaction ids that maps to the primal values.

y: List or Array of the values from the dual.

y_dict: A dictionary of reaction ids that maps to the dual values.

Table Of Contents

Previous topic

cobra Package

Next topic

external Package

This Page