cobra.core.gene#

Provide functions for dealing with genes and gene product rules (GPR).

Attributes#

Classes#

GPRWalker

Identifies genes in an AST/GPR tree.

GPRCleaner

Parses compiled ast of a gene_reaction_rule and identifies genes.

Gene

A Gene in a cobra model.

GPR

A Gene Reaction rule in a cobra model, using AST as base class.

Functions#

parse_gpr(→ Tuple)

Parse GPR into AST.

eval_gpr(→ bool)

Evaluate compiled ast of gene_reaction_rule with knockouts.

ast2str(→ str)

Convert compiled ast to gene_reaction_rule str.

Module Contents#

cobra.core.gene.logger#
cobra.core.gene.keywords = ['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue',...#
cobra.core.gene.keyword_re#
cobra.core.gene.number_start_re#
cobra.core.gene.replacements = (('.', '__COBRA_DOT__'), ("'", '__COBRA_SQUOTE__'), ('"', '__COBRA_DQUOTE__'), (':',...#
class cobra.core.gene.GPRWalker(**kwargs)[source]#

Bases: ast.NodeVisitor

Identifies genes in an AST/GPR tree.

Walks over the tree, and identifies the id of each Name node

gene_set#
visit_Name(node: ast.Name) None[source]#

Visit a Gene node and add the id to the gene_set.

Parameters:

node (ast.Name) – The node to visit

visit_BoolOp(node: ast.BoolOp) None[source]#

Visit a BoolOp node (AND/OR) and visit the children to add them to gene_set.

Parameters:

node (ast.BoolOp) – The node to visit

class cobra.core.gene.GPRCleaner(**kwargs)[source]#

Bases: ast.NodeTransformer

Parses compiled ast of a gene_reaction_rule and identifies genes.

Parts of the tree are rewritten to allow periods in gene ID’s and bitwise boolean operations

gene_set#
visit_Name(node: ast.Name) ast.Name[source]#

Visit a Gene node and add the id to the gene_set.

The gene id will be cleaned used __cobra_escape__ and replacements dictionary (see above).

Parameters:

node (ast.Name) – The node to visit

Returns:

node – The transformed node (with the id changed).

Return type:

ast.Name

visit_BinOp(node: ast.BoolOp) None[source]#

Visit a BoolOp node (AND/OR) and visit the children (genes) to process them.

Parameters:

node (ast.BoolOp) – The node to visit. Nodes other than And() and Or() will cause an error.

Returns:

node – The node with the children transformed.

Return type:

ast.BoolOp

cobra.core.gene.parse_gpr(str_expr: str) Tuple[source]#

Parse GPR into AST.

Parameters:

str_expr (string) – string with the gene reaction rule to parse

Returns:

  • tuple – elements ast_tree and gene_ids as a set

  • .. deprecated ::

  • Use GPR(string_gpr=str_expr) in the future. Because of the GPR() class,

  • this function will be removed.

class cobra.core.gene.Gene(id: str = None, name: str = '', functional: bool = True)[source]#

Bases: cobra.core.species.Species

A Gene in a cobra model.

Parameters:
  • id (string) – The identifier to associate the gene with

  • name (string) – A longer human readable name for the gene

  • functional (bool) – Indicates whether the gene is functional. If it is not functional then it cannot be used in an enzyme complex nor can its products be used.

_functional = True#
property functional: bool#

Flag indicating if the gene is functional.

Changing the flag is reverted upon exit if executed within the model as context.

knock_out() None[source]#

Knockout gene by marking it as non-functional.

Knockout gene by marking it as non-functional and setting all associated reactions bounds to zero. The change is reverted upon exit if executed within the model as context.

_repr_html_()[source]#
class cobra.core.gene.GPR(gpr_from: ast.Expression | ast.Module | ast.AST = None, **kwargs)[source]#

Bases: ast.Module

A Gene Reaction rule in a cobra model, using AST as base class.

Parameters:

gpr_from (Expression or Module or AST) – A GPR in AST format

_genes#
body: list | None = None#
classmethod from_string(string_gpr: str) GPR[source]#

Construct a GPR from a string.

Parameters:

string_gpr (str) – a string that describes the gene rules, in a format like A & B

Returns:

returns a new GPR while setting self.body as Parsed AST tree that has the gene rules This function also sets self._genes with the gene ids in the AST

Return type:

GPR

property genes: FrozenSet#

To check the genes.

This property updates the genes before returning them, in case the GPR was changed and the genes weren’t.

Returns:

genes – All the genes in a frozen set. Do not try to change them with this property.

Return type:

frozenset

update_genes() None[source]#

Update genes, used after changes in GPR.

Walks along the AST tree of the GPR class, and modifies self._genes

_eval_gpr(expr: ast.Expression | list | ast.BoolOp | ast.Name, knockouts: cobra.core.dictlist.DictList | set) bool[source]#

Evaluate compiled ast of gene_reaction_rule with knockouts.

Parameters:
  • expr (Expression or GPR or list or BoolOp or Name) – The ast of the gene reaction rule

  • knockouts (DictList, set) – Set of genes that are knocked out

Returns:

True if the gene reaction rule is true with the given knockouts otherwise false

Return type:

bool

eval(knockouts: cobra.core.dictlist.DictList | Set | str | Iterable = None) bool[source]#

Evaluate compiled ast of gene_reaction_rule with knockouts.

This function calls _eval_gpr, but allows more flexibility in input, including name, and list.

Parameters:

knockouts – Which gene or genes to knoc out

Returns:

True if the gene reaction rule is true with the given knockouts otherwise false

Return type:

bool

_ast2str(expr: GPR | ast.Expression | ast.BoolOp | ast.Name | list, level: int = 0, names: dict = None) str[source]#

Convert compiled ast to gene_reaction_rule str.

Parameters:
  • expr (AST or GPR or list or Name or BoolOp) – string for a gene reaction rule, e.g “a and b”

  • level (int) – internal use only

  • names (dict) – Dict where each element id a gene identifier and the value is the gene name. Use this to get a rule str which uses names instead. This should be done for display purposes only. All gene_reaction_rule strings which are computed with should use the id.

Returns:

The gene reaction rule

Return type:

string

to_string(names: dict = None) str[source]#

Convert compiled ast to gene_reaction_rule str.

Parameters:
  • self (GPR) – compiled ast Module describing GPR

  • names (dict) – dictionary of gene ids to gene names. If this is empty, returns gene ids

Returns:

The gene reaction rule

Return type:

string

Notes

Calls _aststr()

copy() GPR[source]#

Copy a GPR.

__copy__() GPR[source]#

Ensure a correct shallow copy.

__repr__() str[source]#

Return the GPR with module, class, and code to recreate it.

__str__() str[source]#

Convert compiled ast to gene_reaction_rule str.

Parameters:

self (GPR) – compiled ast Module describing GPR

Returns:

The gene reaction rule

Return type:

string

_repr_html_() str[source]#
as_symbolic(names: dict = None) sympy.logic.boolalg.Or | sympy.logic.boolalg.And | sympy.Symbol[source]#

Convert compiled ast to sympy expression.

Parameters:
  • self (GPR) – compiled ast Module describing GPR

  • names (dict) – dictionary of gene ids to gene names. If this is empty, returns sympy expression using gene ids

Returns:

SYMPY expression (Symbol or And or Or). Symbol(“”) if the GPR is empty

Return type:

Symbol or BooleanFunction

Notes

Calls _symbolic_gpr()

_symbolic_gpr(expr: GPR | ast.Expression | ast.BoolOp | ast.Name | list = None, GPRGene_dict: dict = None) sympy.logic.boolalg.Or | sympy.logic.boolalg.And | sympy.Symbol[source]#

Parse gpr into SYMPY using ast similar to _ast2str().

Parameters:
  • expr (AST or GPR or list or Name or BoolOp) – compiled GPR

  • GPRGene_dict (dict) – dictionary from gene id to GPRGeneSymbol

Returns:

SYMPY expression (Symbol or And or Or). Symbol(“”) if the GPR is empty

Return type:

Symbol or BooleanFunction

classmethod from_symbolic(sympy_gpr: sympy.logic.boolalg.BooleanFunction | sympy.Symbol) GPR[source]#

Construct a GPR from a sympy expression.

Parameters:

sympy_gpr (sympy) – a sympy that describes the gene rules, being a Symbol for single genes or a BooleanFunction for AND/OR relationships

Returns:

returns a new GPR while setting self.body as Parsed AST tree that has the gene rules This function also sets self._genes with the gene ids in the AST

Return type:

GPR

__eq__(other) bool[source]#

Check equality of GPR via symbolic equality.

cobra.core.gene.eval_gpr(expr: ast.Expression | GPR, knockouts: cobra.core.dictlist.DictList | set) bool[source]#

Evaluate compiled ast of gene_reaction_rule with knockouts.

Use GPR().eval() in the future. Because of the GPR() class, this function will be removed.

Parameters:
  • expr (Expression or GPR) – The ast of the gene reaction rule

  • knockouts (DictList, set) – Set of genes that are knocked out

Returns:

True if the gene reaction rule is true with the given knockouts otherwise false

Return type:

bool

cobra.core.gene.ast2str(expr: ast.Expression | GPR, level: int = 0, names: dict = None) str[source]#

Convert compiled ast to gene_reaction_rule str.

Parameters:
  • expr (AST or GPR) – AST or GPR

  • level (int) – internal use only. Ignored because of GPR() class, kept only for interface consistency with code still using ast2str.

  • names (dict) – Dict where each element id a gene identifier and the value is the gene name. Use this to get a rule str which uses names instead. This should be done for display purposes only. All gene_reaction_rule strings which are computed with should use the id.

Returns:

  • string – The gene reaction rule

  • .. deprecated ::

  • Use GPR.to_string(names=) in the future. Because of the GPR() class,

  • this function will be removed.