def rename_genes(cobra_model, rename_dict):
recompute_reactions = set()  <EOL>remove_genes = []<EOL>for old_name, new_name in iteritems(rename_dict):<EOL><INDENT>try:<EOL><INDENT>gene_index = cobra_model.genes.index(old_name)<EOL><DEDENT>except ValueError:<EOL><INDENT>gene_index = None<EOL><DEDENT>old_gene_present = gene_index is not None<EOL>new_gene_present = new_name in cobra_model.genes<EOL>if old_gene_present and new_gene_present:<EOL><INDENT>old_gene = cobra_model.genes.get_by_id(old_name)<EOL>if old_gene is not cobra_model.genes.get_by_id(new_name):<EOL><INDENT>remove_genes.append(old_gene)<EOL>recompute_reactions.update(old_gene._reaction)<EOL><DEDENT><DEDENT>elif old_gene_present and not new_gene_present:<EOL><INDENT>gene = cobra_model.genes[gene_index]<EOL>cobra_model.genes._dict.pop(gene.id)  <EOL>gene.id = new_name<EOL>cobra_model.genes[gene_index] = gene<EOL><DEDENT>elif not old_gene_present and new_gene_present:<EOL><INDENT>pass<EOL><DEDENT>else:  <EOL><INDENT>pass<EOL><DEDENT><DEDENT>cobra_model.repair()<EOL>class Renamer(NodeTransformer):<EOL><INDENT>def visit_Name(self, node):<EOL><INDENT>node.id = rename_dict.get(node.id, node.id)<EOL>return node<EOL><DEDENT><DEDENT>gene_renamer = Renamer()<EOL>for rxn, rule in iteritems(get_compiled_gene_reaction_rules(cobra_model)):<EOL><INDENT>if rule is not None:<EOL><INDENT>rxn._gene_reaction_rule = ast2str(gene_renamer.visit(rule))<EOL><DEDENT><DEDENT>for rxn in recompute_reactions:<EOL><INDENT>rxn.gene_reaction_rule = rxn._gene_reaction_rule<EOL><DEDENT>for i in remove_genes:<EOL><INDENT>cobra_model.genes.remove(i)<EOL><DEDENT>
renames genes in a model from the rename_dict