cobra.core.gene
¶
Provide functions for dealing with genes and gene product rules (GPR).
Module Contents¶
Classes¶
Identifies genes in an AST/GPR tree. |
|
Parses compiled ast of a gene_reaction_rule and identifies genes. |
|
A Gene in a cobra model. |
|
A Gene Reaction rule in a cobra model, using AST as base class. |
Functions¶
|
Parse GPR into AST. |
|
Evaluate compiled ast of gene_reaction_rule with knockouts. |
|
Convert compiled ast to gene_reaction_rule str. |
Attributes¶
- cobra.core.gene.replacements = [['.', '__COBRA_DOT__'], ["'", '__COBRA_SQUOTE__'], ['"', '__COBRA_DQUOTE__'], [':',...[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(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
- 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).
- 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
- 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: bool[source]¶
Flag indicating if the gene is functional.
Changing the flag is reverted upon exit if executed within the model as context.
- class cobra.core.gene.GPR(gpr_from: Union[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
- property genes: 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
- 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: Union[ast.Expression, list, ast.BoolOp, ast.Name], knockouts: Union[cobra.core.dictlist.DictList, set]) bool [source]¶
Evaluate compiled ast of gene_reaction_rule with knockouts.
- eval(knockouts: Union[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
- _ast2str(expr: Union[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
- Returns
The gene reaction rule
- Return type
string
Notes
Calls _aststr()
- __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
- as_symbolic(names: dict = None) Union[sympy.logic.boolalg.Or, sympy.logic.boolalg.And, sympy.Symbol] [source]¶
Convert compiled ast to sympy expression.
- Parameters
- 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: Union[GPR, ast.Expression, ast.BoolOp, ast.Name, list] = None, GPRGene_dict: dict = None) Union[sympy.logic.boolalg.Or, sympy.logic.boolalg.And, sympy.Symbol] [source]¶
Parse gpr into SYMPY using ast similar to _ast2str().
- classmethod from_symbolic(sympy_gpr: Union[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
- cobra.core.gene.eval_gpr(expr: Union[ast.Expression, GPR], knockouts: Union[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.
- cobra.core.gene.ast2str(expr: Union[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.