def find_gene_knockout_reactions(cobra_model, gene_list, compiled_gene_reaction_rules=None): """identify reactions which will be disabled when the genes are knocked out cobra_model: :class:`~cobra.core.Model.Model` gene_list: iterable of :class:`~cobra.core.Gene.Gene` compiled_gene_reaction_rules: dict of {reaction_id: compiled_string} If provided, this gives pre-compiled gene_reaction_rule strings. The compiled rule strings can be evaluated much faster. If a rule is not provided, the regular expression evaluation will be used. Because not all gene_reaction_rule strings can be evaluated, this dict must exclude any rules which can not be used with eval. """ potential_reactions = set() for gene in gene_list: if isinstance(gene, string_types): gene = cobra_model.genes.get_by_id(gene) potential_reactions.update(gene._reaction) gene_set = {str(i) for i in gene_list} if compiled_gene_reaction_rules is None: compiled_gene_reaction_rules = { r: parse_gpr(r.gene_reaction_rule)[0] for r in potential_reactions } return [ r for r in potential_reactions if not eval_gpr(compiled_gene_reaction_rules[r], gene_set) ]
def remove_genes(cobra_model, gene_list, remove_reactions=True): """remove genes entirely from the model This will also simplify all gene_reaction_rules with this gene inactivated.""" gene_set = {cobra_model.genes.get_by_id(str(i)) for i in gene_list} gene_id_set = {i.id for i in gene_set} remover = _GeneRemover(gene_id_set) ast_rules = get_compiled_gene_reaction_rules(cobra_model) target_reactions = [] for reaction, rule in iteritems(ast_rules): if reaction.gene_reaction_rule is None or \ len(reaction.gene_reaction_rule) == 0: continue # reactions to remove if remove_reactions and not eval_gpr(rule, gene_id_set): target_reactions.append(reaction) else: # if the reaction is not removed, remove the gene # from its gpr remover.visit(rule) new_rule = ast2str(rule) if new_rule != reaction.gene_reaction_rule: reaction.gene_reaction_rule = new_rule for gene in gene_set: cobra_model.genes.remove(gene) cobra_model.remove_reactions(target_reactions)
def find_gene_knockout_reactions(cobra_model, gene_list, compiled_gene_reaction_rules=None): """identify reactions which will be disabled when the genes are knocked out cobra_model: :class:`~cobra.core.Model.Model` gene_list: iterable of :class:`~cobra.core.Gene.Gene` compiled_gene_reaction_rules: dict of {reaction_id: compiled_string} If provided, this gives pre-compiled gene_reaction_rule strings. The compiled rule strings can be evaluated much faster. If a rule is not provided, the regular expression evaluation will be used. Because not all gene_reaction_rule strings can be evaluated, this dict must exclude any rules which can not be used with eval. """ potential_reactions = set() for gene in gene_list: if isinstance(gene, string_types): gene = cobra_model.genes.get_by_id(gene) potential_reactions.update(gene._reaction) gene_set = {str(i) for i in gene_list} if compiled_gene_reaction_rules is None: compiled_gene_reaction_rules = {r: parse_gpr(r.gene_reaction_rule)[0] for r in potential_reactions} return [r for r in potential_reactions if not eval_gpr(compiled_gene_reaction_rules[r], gene_set)]
def remove_genes(cobra_model, gene_list, remove_reactions=True): """remove genes entirely from the model This will also simplify all gene_reaction_rules with this gene inactivated.""" gene_set = {cobra_model.genes.get_by_id(str(i)) for i in gene_list} gene_id_set = {i.id for i in gene_set} remover = _GeneRemover(gene_id_set) ast_rules = get_compiled_gene_reaction_rules(cobra_model) target_reactions = [] for reaction, rule in iteritems(ast_rules): if reaction.gene_reaction_rule is None or \ len(reaction.gene_reaction_rule) == 0: continue # reactions to remove if remove_reactions and not eval_gpr(rule, gene_id_set): target_reactions.append(reaction) else: # if the reaction is not removed, remove the gene # from its gpr remover.visit(rule) new_rule = ast2str(rule) if new_rule != reaction.gene_reaction_rule: reaction.gene_reaction_rule = new_rule for gene in gene_set: cobra_model.genes.remove(gene) # remove reference to the gene in all groups associated_groups = cobra_model.get_associated_groups(gene) for group in associated_groups: group.remove_members(gene) cobra_model.remove_reactions(target_reactions)
def functional(self): """All required enzymes for reaction are functional. Returns ------- bool True if the gene-protein-reaction (GPR) rule is fulfilled for this reaction, or if reaction is not associated to a model, otherwise False. """ if self._model: tree, _ = parse_gpr(self.gene_reaction_rule) return eval_gpr(tree, {gene.id for gene in self.genes if not gene.functional}) return True