def convert_model_entries(<EOL>model, convert_id=create_convert_sbml_id_function(),<EOL>create_unique_id=None,<EOL>translate_compartment=translate_sbml_compartment,<EOL>translate_reaction=translate_sbml_reaction,<EOL>translate_compound=translate_sbml_compound):
def find_new_ids(entries):<EOL><INDENT>"""<STR_LIT>"""<EOL>id_map = {}<EOL>new_ids = set()<EOL>for entry in entries:<EOL><INDENT>new_id = convert_id(entry)<EOL>if new_id in new_ids:<EOL><INDENT>if create_unique_id is not None:<EOL><INDENT>new_id = create_unique_id(new_ids, new_id)<EOL><DEDENT>else:<EOL><INDENT>raise ValueError(<EOL>'<STR_LIT>'.format(<EOL>entry.id))<EOL><DEDENT><DEDENT>id_map[entry.id] = new_id<EOL>new_ids.add(new_id)<EOL><DEDENT>return id_map<EOL><DEDENT>compartment_map = find_new_ids(model.compartments)<EOL>compound_map = find_new_ids(model.compounds)<EOL>reaction_map = find_new_ids(model.reactions)<EOL>new_compartments = []<EOL>for compartment in model.compartments:<EOL><INDENT>new_id = compartment_map[compartment.id]<EOL>new_compartments.append(translate_compartment(compartment, new_id))<EOL><DEDENT>new_compounds = []<EOL>for compound in model.compounds:<EOL><INDENT>new_id = compound_map[compound.id]<EOL>new_compounds.append(<EOL>translate_compound(compound, new_id, compartment_map))<EOL><DEDENT>new_reactions = []<EOL>for reaction in model.reactions:<EOL><INDENT>new_id = reaction_map[reaction.id]<EOL>new_entry = translate_reaction(<EOL>reaction, new_id, compartment_map, compound_map)<EOL>new_reactions.append(new_entry)<EOL><DEDENT>model.compartments.clear()<EOL>model.compartments.update(new_compartments)<EOL>model.compounds.clear()<EOL>model.compounds.update(new_compounds)<EOL>model.reactions.clear()<EOL>model.reactions.update(new_reactions)<EOL>new_boundaries = []<EOL>for boundary in model.compartment_boundaries:<EOL><INDENT>c1, c2 = (compartment_map.get(c, c) for c in boundary)<EOL>new_boundaries.append(tuple(sorted(c1, c2)))<EOL><DEDENT>model.compartment_boundaries.clear()<EOL>model.compartment_boundaries.update(new_boundaries)<EOL>new_limits = []<EOL>for reaction, lower, upper in itervalues(model.limits):<EOL><INDENT>new_reaction_id = reaction_map.get(reaction, reaction)<EOL>new_limits.append((new_reaction_id, lower, upper))<EOL><DEDENT>model.limits.clear()<EOL>model.limits.update((limit[<NUM_LIT:0>], limit) for limit in new_limits)<EOL>new_exchanges = []<EOL>for compound, reaction, lower, upper in itervalues(model.exchange):<EOL><INDENT>new_compound_id = compound.translated(<EOL>lambda name: compound_map.get(name, name))<EOL>new_reaction_id = reaction_map.get(reaction, reaction)<EOL>new_exchanges.append((new_compound_id, new_reaction_id, lower, upper))<EOL><DEDENT>model.exchange.clear()<EOL>model.exchange.update((ex[<NUM_LIT:0>], ex) for ex in new_exchanges)<EOL>new_model = []<EOL>for reaction in model.model:<EOL><INDENT>new_id = reaction_map.get(reaction, reaction)<EOL>new_model.append(new_id)<EOL><DEDENT>model.model.clear()<EOL>model.model.update((new_id, None) for new_id in new_model)<EOL>if model.biomass_reaction is not None:<EOL><INDENT>old_id = model.biomass_reaction<EOL>model.biomass_reaction = reaction_map.get(old_id, old_id)<EOL><DEDENT>if model.extracellular_compartment is not None:<EOL><INDENT>old_id = model.extracellular_compartment<EOL>model.extracellular_compartment = compartment_map.get(old_id, old_id)<EOL><DEDENT>if model.default_compartment is not None:<EOL><INDENT>old_id = model.default_compartment<EOL>model.default_compartment = compartment_map.get(old_id, old_id)<EOL><DEDENT>
Convert and decode model entries.

    Model entries are converted to new entries using the translate functions
    and IDs are converted using the given coversion function. If ID conversion
    would create a clash of IDs, the ``create_unique_id`` function is called
    with a container of current IDs and the base ID to generate a unique ID
    from. The translation functions take an existing entry and the new ID.

    All references within the model are updated to use new IDs: compartment
    boundaries, limits, exchange, model, biomass reaction, etc.

    Args:
        model: :class:`NativeModel`.