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 validate_model(model): errors = [] warnings = [] errors.extend(check_reaction_bounds(model)) errors.extend(check_metabolite_compartment_formula(model)) # test gpr for reaction in model.reactions: try: parse_gpr(reaction.gene_reaction_rule) except SyntaxError: errors.append("reaction '%s' has invalid gpr '%s'" % (reaction.id, reaction.gene_reaction_rule)) # test mass balance for reaction, balance in iteritems(check_mass_balance(model)): # check if it's a demand or exchange reaction if len(reaction.metabolites) == 1: warnings.append("reaction '%s' is not balanced. Should it " "be annotated as a demand or exchange " "reaction?" % reaction.id) elif "biomass" in reaction.id.lower(): warnings.append("reaction '%s' is not balanced. Should it " "be annotated as a biomass reaction?" % reaction.id) else: warnings.append("reaction '%s' is not balanced for %s" % (reaction.id, ", ".join(sorted(balance)))) # try solving solution = model.optimize(solver="esolver") if solution.status != "optimal": errors.append("model can not be solved (status '%s')" % solution.status) return {"errors": errors, "warnings": warnings} # if there is no objective, then we know why the objective was low if len(model.objective) == 0: warnings.append("model has no objective function") elif solution.f <= 0: warnings.append("model can not produce nonzero biomass") elif solution.f <= 1e-3: warnings.append("biomass flux %s too low" % str(solution.f)) if len(model.objective) > 1: warnings.append("model should only have one reaction as the objective") return {"errors": errors, "warnings": warnings, "objective": solution.f}
def reaction_confidence(rule, conf_genes): """Calculates the confidence for the reaction based on a gene-reaction rule. Args: rule (str): A gene-reaction rule. For instance "A and B". conf_genes (dict): A str->int map denoting the mapping of gene IDs to expression confidence values. Allowed confidence values are -1 (absent/do not include), 0 (unknown), 1 (low confidence), 2 (medium confidence) and 3 (high confidence). """ ast_rule, _ = parse_gpr(rule) return safe_eval_gpr(ast_rule, conf_genes)
def get_compiled_gene_reaction_rules(cobra_model): """Generates a dict of compiled gene_reaction_rules Any gene_reaction_rule expressions which cannot be compiled or do not evaluate after compiling will be excluded. The result can be used in the find_gene_knockout_reactions function to speed up evaluation of these rules. """ return { r: parse_gpr(r.gene_reaction_rule)[0] for r in cobra_model.reactions }
pheno_ess_reac_dict[reac].append(scr_id) # print "Reaction {} is an essential reaction for {}.".format(scr_id, reac) elif test_stat == 'optimal': vKO = math.fabs(test_f) vOrg = math.fabs(org_pheno_f) if vKO <= vOrg * ess_thr: pheno_ess_reac_dict[reac].append(scr_id) # print "Reaction {} is an essential reaction for {}.".format(scr_id, reac) """Extract the related genes in the essential reactions as the essential genes of the phenotypic data""" pheno_ess_gene_dict = defaultdict(list) ess_gene_pheno_dict = defaultdict(list) for keys, values in pheno_ess_reac_dict.items(): print "Reaction {} has {} essential reactions.".format(keys, len(values)) for ess_reac_id in values: ess_reac = model.reactions.get_by_id(ess_reac_id) temp_gpr = parse_gpr(ess_reac.gene_reaction_rule) # print len(temp_gpr[1]) for z in temp_gpr[1]: if z not in pheno_ess_gene_dict[keys] and z not in pseudo_gene_rec: pheno_ess_gene_dict[keys].append(z) ess_gene_pheno_dict[z].append(keys) print "Reaction {} has {} essential genes.\n".format( keys, len(pheno_ess_gene_dict[keys])) # pheno_ess_bool_dict = defaultdict(list) # ess_pheno_bool_dict = defaultdict(list) """Generate the final output protein-phenotype masking matrix.""" row_num = len(phenotype_id_rec) col_num = len(gene_id_rec) mask_matrix = np.zeros((row_num, col_num), dtype=np.int) pheno_id = pheno_ess_gene_dict.keys()