17.1.1.1.1.4. cobra.core.gene

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, knockouts)

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.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[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) → None[source]
visit_BoolOp(self, node: BoolOp) → None[source]
class cobra.core.gene.GPRCleaner[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)[source]
visit_BinOp(self, node)[source]
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=None, name='', functional=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)[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)[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.

remove_from_model(self, model=None, make_dependent_reactions_nonfunctional=True)[source]

Removes the association.

Parameters
  • model (cobra model) – The model to remove the gene from

  • make_dependent_reactions_nonfunctional (bool) – If True then replace the gene with ‘False’ in the gene association, else replace the gene with ‘True’

Deprecated since version 0.4: Use cobra.manipulation.delete_model_genes to simulate knockouts and cobra.manipulation.remove_genes to remove genes from the model.

_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[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.

__repr__(self)[source]

Return repr(self).

__str__(self)[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)[source]
cobra.core.gene.eval_gpr(expr, knockouts)[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

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