:py:mod:`cobra.core.gene` ========================= .. py:module:: cobra.core.gene .. autoapi-nested-parse:: Provide functions for dealing with genes and gene product rules (GPR). Module Contents --------------- Classes ~~~~~~~ .. autoapisummary:: cobra.core.gene.GPRWalker cobra.core.gene.GPRCleaner cobra.core.gene.Gene cobra.core.gene.GPR Functions ~~~~~~~~~ .. autoapisummary:: cobra.core.gene.parse_gpr cobra.core.gene.eval_gpr cobra.core.gene.ast2str Attributes ~~~~~~~~~~ .. autoapisummary:: cobra.core.gene.logger cobra.core.gene.keywords cobra.core.gene.keyword_re cobra.core.gene.number_start_re cobra.core.gene.replacements .. py:data:: logger .. py:data:: keywords .. py:data:: keyword_re .. py:data:: number_start_re .. py:data:: replacements :value: (('.', '__COBRA_DOT__'), ("'", '__COBRA_SQUOTE__'), ('"', '__COBRA_DQUOTE__'), (':',... .. py:class:: GPRWalker(**kwargs) Bases: :py:obj:`ast.NodeVisitor` Identifies genes in an AST/GPR tree. Walks over the tree, and identifies the id of each Name node .. py:method:: visit_Name(node: ast.Name) -> None Visit a Gene node and add the id to the gene_set. :param node: The node to visit :type node: ast.Name .. py:method:: visit_BoolOp(node: ast.BoolOp) -> None Visit a BoolOp node (AND/OR) and visit the children to add them to gene_set. :param node: The node to visit :type node: ast.BoolOp .. py:class:: GPRCleaner(**kwargs) Bases: :py:obj:`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 .. py:method:: visit_Name(node: ast.Name) -> ast.Name 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). :param node: The node to visit :type node: ast.Name :returns: **node** -- The transformed node (with the id changed). :rtype: ast.Name .. py:method:: visit_BinOp(node: ast.BoolOp) -> None Visit a BoolOp node (AND/OR) and visit the children (genes) to process them. :param node: The node to visit. Nodes other than And() and Or() will cause an error. :type node: ast.BoolOp :returns: **node** -- The node with the children transformed. :rtype: ast.BoolOp .. py:function:: parse_gpr(str_expr: str) -> Tuple Parse GPR into AST. :param str_expr: string with the gene reaction rule to parse :type str_expr: string :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.* .. py:class:: Gene(id: str = None, name: str = '', functional: bool = True) Bases: :py:obj:`cobra.core.species.Species` A Gene in a cobra model. :param id: The identifier to associate the gene with :type id: string :param name: A longer human readable name for the gene :type name: string :param functional: 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. :type functional: bool .. py:property:: functional :type: bool Flag indicating if the gene is functional. Changing the flag is reverted upon exit if executed within the model as context. .. py:method:: knock_out() -> None 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. .. py:method:: _repr_html_() .. py:class:: GPR(gpr_from: Union[ast.Expression, ast.Module, ast.AST] = None, **kwargs) Bases: :py:obj:`ast.Module` A Gene Reaction rule in a cobra model, using AST as base class. :param gpr_from: A GPR in AST format :type gpr_from: Expression or Module or AST .. py:property:: genes :type: 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. :rtype: frozenset .. py:method:: from_string(string_gpr: str) -> GPR :classmethod: Construct a GPR from a string. :param string_gpr: a string that describes the gene rules, in a format like A & B :type string_gpr: str :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 :rtype: GPR .. py:method:: update_genes() -> None Update genes, used after changes in GPR. Walks along the AST tree of the GPR class, and modifies self._genes .. py:method:: _eval_gpr(expr: Union[ast.Expression, list, ast.BoolOp, ast.Name], knockouts: Union[cobra.core.dictlist.DictList, set]) -> bool Evaluate compiled ast of gene_reaction_rule with knockouts. :param expr: The ast of the gene reaction rule :type expr: Expression or GPR or list or BoolOp or Name :param knockouts: Set of genes that are knocked out :type knockouts: DictList, set :returns: True if the gene reaction rule is true with the given knockouts otherwise false :rtype: bool .. py:method:: eval(knockouts: Union[cobra.core.dictlist.DictList, Set, str, Iterable] = None) -> bool Evaluate compiled ast of gene_reaction_rule with knockouts. This function calls _eval_gpr, but allows more flexibility in input, including name, and list. :param knockouts: Which gene or genes to knoc out :returns: True if the gene reaction rule is true with the given knockouts otherwise false :rtype: bool .. py:method:: _ast2str(expr: Union[GPR, ast.Expression, ast.BoolOp, ast.Name, list], level: int = 0, names: dict = None) -> str Convert compiled ast to gene_reaction_rule str. :param expr: string for a gene reaction rule, e.g "a and b" :type expr: AST or GPR or list or Name or BoolOp :param level: internal use only :type level: int :param names: 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. :type names: dict :returns: The gene reaction rule :rtype: string .. py:method:: to_string(names: dict = None) -> str Convert compiled ast to gene_reaction_rule str. :param self: compiled ast Module describing GPR :type self: GPR :param names: dictionary of gene ids to gene names. If this is empty, returns gene ids :type names: dict :returns: The gene reaction rule :rtype: string .. rubric:: Notes Calls _aststr() .. py:method:: copy() Copy a GPR. .. py:method:: __copy__() -> GPR Ensure a correct shallow copy. .. py:method:: __repr__() -> str Return the GPR with module, class, and code to recreate it. .. py:method:: __str__() -> str Convert compiled ast to gene_reaction_rule str. :param self: compiled ast Module describing GPR :type self: GPR :returns: The gene reaction rule :rtype: string .. py:method:: _repr_html_() -> str .. py:method:: as_symbolic(names: dict = None) -> Union[sympy.logic.boolalg.Or, sympy.logic.boolalg.And, sympy.Symbol] Convert compiled ast to sympy expression. :param self: compiled ast Module describing GPR :type self: GPR :param names: dictionary of gene ids to gene names. If this is empty, returns sympy expression using gene ids :type names: dict :returns: SYMPY expression (Symbol or And or Or). Symbol("") if the GPR is empty :rtype: Symbol or BooleanFunction .. rubric:: Notes Calls _symbolic_gpr() .. py:method:: _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] Parse gpr into SYMPY using ast similar to _ast2str(). :param expr: compiled GPR :type expr: AST or GPR or list or Name or BoolOp :param GPRGene_dict: dictionary from gene id to GPRGeneSymbol :type GPRGene_dict: dict :returns: SYMPY expression (Symbol or And or Or). Symbol("") if the GPR is empty :rtype: Symbol or BooleanFunction .. py:method:: from_symbolic(sympy_gpr: Union[sympy.logic.boolalg.BooleanFunction, sympy.Symbol]) -> GPR :classmethod: Construct a GPR from a sympy expression. :param sympy_gpr: a sympy that describes the gene rules, being a Symbol for single genes or a BooleanFunction for AND/OR relationships :type sympy_gpr: sympy :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 :rtype: GPR .. py:method:: __eq__(other) -> bool Check equality of GPR via symbolic equality. .. py:function:: eval_gpr(expr: Union[ast.Expression, GPR], knockouts: Union[cobra.core.dictlist.DictList, set]) -> bool Evaluate compiled ast of gene_reaction_rule with knockouts. .. deprecated :: Use GPR().eval() in the future. Because of the GPR() class, this function will be removed. :param expr: The ast of the gene reaction rule :type expr: Expression or GPR :param knockouts: Set of genes that are knocked out :type knockouts: DictList, set :returns: True if the gene reaction rule is true with the given knockouts otherwise false :rtype: bool .. py:function:: ast2str(expr: Union[ast.Expression, GPR], level: int = 0, names: dict = None) -> str Convert compiled ast to gene_reaction_rule str. :param expr: AST or GPR :type expr: AST or GPR :param level: internal use only. Ignored because of GPR() class, kept only for interface consistency with code still using ast2str. :type level: int :param names: 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. :type names: dict :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.*