17.1.1.1.1.4. cobra.core.gene

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

17.1.1.1.1.4.1. Module Contents

17.1.1.1.1.4.1.1. 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.

17.1.1.1.1.4.1.2. Functions

parse_gpr(str_expr: str) → Tuple

Parse GPR into AST.

eval_gpr(expr: Union[Expression, GPR], knockouts: Union[DictList, set]) → bool

Evaluate compiled ast of gene_reaction_rule with knockouts.

ast2str(expr: Union[Expression, GPR], level: int = 0, names: dict = None) → str

Convert compiled ast to gene_reaction_rule str.

cobra.core.gene.logger[source]
cobra.core.gene.keywords[source]
cobra.core.gene.keyword_re[source]
cobra.core.gene.number_start_re[source]
cobra.core.gene.replacements = [['.', '__COBRA_DOT__'], ["'", '__COBRA_SQUOTE__'], ['"', '__COBRA_DQUOTE__'], [':', '__COBRA_COLON__'], ['/', '__COBRA_FSLASH__'], ['\\', '__COBRA_BSLASH'], ['-', '__COBRA_DASH__'], ['=', '__COBRA_EQ__']][source]
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

visit_Name(self, node: 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(self, node: 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

visit_Name(self, node: Name) → 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(self, node: 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.

property functional(self) → bool[source]

Flag indicating if the gene is functional.

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

knock_out(self) → 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_(self)[source]
class cobra.core.gene.GPR(gpr_from: Union[Expression, Module, 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

classmethod from_string(cls, 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(self) → FrozenSet[source]

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(self) → None[source]

Update genes, used after changes in GPR.

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

_eval_gpr(self, expr: Union[Expression, list, BoolOp, Name], knockouts: Union[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(self, knockouts: Union[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(self, expr: Union['GPR', Expression, BoolOp, 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(self, 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(self)[source]

Copy a GPR.

__copy__(self) → 'GPR'[source]

Ensure a correct shallow copy.

__repr__(self) → str[source]

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

__str__(self) → 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_(self) → str[source]
as_symbolic(self, names: dict = None) → Union[spl.Or, spl.And, 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(self, expr: Union['GPR', Expression, BoolOp, Name, list] = None, GPRGene_dict: dict = None) → Union[spl.Or, spl.And, 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(cls, sympy_gpr: Union[spl.BooleanFunction, 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__(self, other) → bool[source]

Check equality of GPR via symbolic equality.

cobra.core.gene.eval_gpr(expr: Union[Expression, GPR], knockouts: Union[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: Union[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.