def _model_to_sbml(cobra_model, f_replace=None, units=True):
if f_replace is None:<EOL><INDENT>f_replace = {}<EOL><DEDENT>sbml_ns = libsbml.SBMLNamespaces(<NUM_LIT:3>, <NUM_LIT:1>)  <EOL>sbml_ns.addPackageNamespace("<STR_LIT>", <NUM_LIT:2>)  <EOL>doc = libsbml.SBMLDocument(sbml_ns)  <EOL>doc.setPackageRequired("<STR_LIT>", False)<EOL>doc.setSBOTerm(SBO_FBA_FRAMEWORK)<EOL>model = doc.createModel()  <EOL>model_fbc = model.getPlugin("<STR_LIT>")  <EOL>model_fbc.setStrict(True)<EOL>if cobra_model.id is not None:<EOL><INDENT>model.setId(cobra_model.id)<EOL>model.setMetaId("<STR_LIT>" + cobra_model.id)<EOL><DEDENT>else:<EOL><INDENT>model.setMetaId("<STR_LIT>")<EOL><DEDENT>if cobra_model.name is not None:<EOL><INDENT>model.setName(cobra_model.name)<EOL><DEDENT>_sbase_annotations(model, cobra_model.annotation)<EOL>if hasattr(cobra_model, "<STR_LIT>"):<EOL><INDENT>meta = cobra_model._sbml<EOL>if "<STR_LIT>" in meta:<EOL><INDENT>_sbase_annotations(doc, meta["<STR_LIT>"])<EOL><DEDENT>if "<STR_LIT>" in meta:<EOL><INDENT>_sbase_notes_dict(doc, meta["<STR_LIT>"])<EOL><DEDENT>history = libsbml.ModelHistory()  <EOL>if "<STR_LIT>" in meta and meta["<STR_LIT>"]:<EOL><INDENT>history.setCreatedDate(meta["<STR_LIT>"])<EOL><DEDENT>else:<EOL><INDENT>time = datetime.datetime.now()<EOL>timestr = time.strftime('<STR_LIT>')<EOL>date = libsbml.Date(timestr)<EOL>_check(history.setCreatedDate(date), '<STR_LIT>')<EOL>_check(history.setModifiedDate(date), '<STR_LIT>')<EOL><DEDENT>if "<STR_LIT>" in meta:<EOL><INDENT>for cobra_creator in meta["<STR_LIT>"]:<EOL><INDENT>creator = libsbml.ModelCreator()  <EOL>if cobra_creator.get("<STR_LIT>", None):<EOL><INDENT>creator.setFamilyName(cobra_creator["<STR_LIT>"])<EOL><DEDENT>if cobra_creator.get("<STR_LIT>", None):<EOL><INDENT>creator.setGivenName(cobra_creator["<STR_LIT>"])<EOL><DEDENT>if cobra_creator.get("<STR_LIT>", None):<EOL><INDENT>creator.setOrganisation(cobra_creator["<STR_LIT>"])<EOL><DEDENT>if cobra_creator.get("<STR_LIT:email>", None):<EOL><INDENT>creator.setEmail(cobra_creator["<STR_LIT:email>"])<EOL><DEDENT>_check(history.addCreator(creator),<EOL>"<STR_LIT>")<EOL><DEDENT><DEDENT>_check(model.setModelHistory(history), '<STR_LIT>')<EOL><DEDENT>if units:<EOL><INDENT>flux_udef = model.createUnitDefinition()  <EOL>flux_udef.setId(UNITS_FLUX[<NUM_LIT:0>])<EOL>for u in UNITS_FLUX[<NUM_LIT:1>]:<EOL><INDENT>unit = flux_udef.createUnit()  <EOL>unit.setKind(u.kind)<EOL>unit.setExponent(u.exponent)<EOL>unit.setScale(u.scale)<EOL>unit.setMultiplier(u.multiplier)<EOL><DEDENT><DEDENT>if len(cobra_model.reactions) > <NUM_LIT:0>:<EOL><INDENT>min_value = min(cobra_model.reactions.list_attr("<STR_LIT>"))<EOL>max_value = max(cobra_model.reactions.list_attr("<STR_LIT>"))<EOL><DEDENT>else:<EOL><INDENT>min_value = config.lower_bound<EOL>max_value = config.upper_bound<EOL><DEDENT>_create_parameter(model, pid=LOWER_BOUND_ID,<EOL>value=min_value, sbo=SBO_DEFAULT_FLUX_BOUND)<EOL>_create_parameter(model, pid=UPPER_BOUND_ID,<EOL>value=max_value, sbo=SBO_DEFAULT_FLUX_BOUND)<EOL>_create_parameter(model, pid=ZERO_BOUND_ID,<EOL>value=<NUM_LIT:0>, sbo=SBO_DEFAULT_FLUX_BOUND)<EOL>_create_parameter(model, pid=BOUND_MINUS_INF,<EOL>value=-float("<STR_LIT>"), sbo=SBO_FLUX_BOUND)<EOL>_create_parameter(model, pid=BOUND_PLUS_INF,<EOL>value=float("<STR_LIT>"), sbo=SBO_FLUX_BOUND)<EOL>for cid, name in iteritems(cobra_model.compartments):<EOL><INDENT>compartment = model.createCompartment()  <EOL>compartment.setId(cid)<EOL>compartment.setName(name)<EOL>compartment.setConstant(True)<EOL><DEDENT>for metabolite in cobra_model.metabolites:<EOL><INDENT>specie = model.createSpecies()  <EOL>mid = metabolite.id<EOL>if f_replace and F_SPECIE_REV in f_replace:<EOL><INDENT>mid = f_replace[F_SPECIE_REV](mid)<EOL><DEDENT>specie.setId(mid)<EOL>specie.setConstant(False)<EOL>specie.setBoundaryCondition(False)<EOL>specie.setHasOnlySubstanceUnits(False)<EOL>specie.setName(metabolite.name)<EOL>specie.setCompartment(metabolite.compartment)<EOL>s_fbc = specie.getPlugin("<STR_LIT>")  <EOL>if metabolite.charge is not None:<EOL><INDENT>s_fbc.setCharge(metabolite.charge)<EOL><DEDENT>if metabolite.formula is not None:<EOL><INDENT>s_fbc.setChemicalFormula(metabolite.formula)<EOL><DEDENT>_sbase_annotations(specie, metabolite.annotation)<EOL>_sbase_notes_dict(specie, metabolite.notes)<EOL><DEDENT>for cobra_gene in cobra_model.genes:<EOL><INDENT>gp = model_fbc.createGeneProduct()  <EOL>gid = cobra_gene.id<EOL>if f_replace and F_GENE_REV in f_replace:<EOL><INDENT>gid = f_replace[F_GENE_REV](gid)<EOL><DEDENT>gp.setId(gid)<EOL>gname = cobra_gene.name<EOL>if gname is None or len(gname) == <NUM_LIT:0>:<EOL><INDENT>gname = gid<EOL><DEDENT>gp.setName(gname)<EOL>gp.setLabel(gid)<EOL>_sbase_annotations(gp, cobra_gene.annotation)<EOL>_sbase_notes_dict(gp, cobra_gene.notes)<EOL><DEDENT>objective = model_fbc.createObjective()  <EOL>objective.setId("<STR_LIT>")<EOL>objective.setType(SHORT_LONG_DIRECTION[cobra_model.objective.direction])<EOL>model_fbc.setActiveObjectiveId("<STR_LIT>")<EOL>reaction_coefficients = linear_reaction_coefficients(cobra_model)<EOL>for cobra_reaction in cobra_model.reactions:<EOL><INDENT>rid = cobra_reaction.id<EOL>if f_replace and F_REACTION_REV in f_replace:<EOL><INDENT>rid = f_replace[F_REACTION_REV](rid)<EOL><DEDENT>reaction = model.createReaction()  <EOL>reaction.setId(rid)<EOL>reaction.setName(cobra_reaction.name)<EOL>reaction.setFast(False)<EOL>reaction.setReversible((cobra_reaction.lower_bound < <NUM_LIT:0>))<EOL>_sbase_annotations(reaction, cobra_reaction.annotation)<EOL>_sbase_notes_dict(reaction, cobra_reaction.notes)<EOL>for metabolite, stoichiometry in iteritems(cobra_reaction._metabolites):  <EOL><INDENT>sid = metabolite.id<EOL>if f_replace and F_SPECIE_REV in f_replace:<EOL><INDENT>sid = f_replace[F_SPECIE_REV](sid)<EOL><DEDENT>if stoichiometry < <NUM_LIT:0>:<EOL><INDENT>sref = reaction.createReactant()  <EOL>sref.setSpecies(sid)<EOL>sref.setStoichiometry(-stoichiometry)<EOL>sref.setConstant(True)<EOL><DEDENT>else:<EOL><INDENT>sref = reaction.createProduct()  <EOL>sref.setSpecies(sid)<EOL>sref.setStoichiometry(stoichiometry)<EOL>sref.setConstant(True)<EOL><DEDENT><DEDENT>r_fbc = reaction.getPlugin("<STR_LIT>")  <EOL>r_fbc.setLowerFluxBound(_create_bound(model, cobra_reaction,<EOL>"<STR_LIT>",<EOL>f_replace=f_replace, units=units,<EOL>flux_udef=flux_udef))<EOL>r_fbc.setUpperFluxBound(_create_bound(model, cobra_reaction,<EOL>"<STR_LIT>",<EOL>f_replace=f_replace, units=units,<EOL>flux_udef=flux_udef))<EOL>gpr = cobra_reaction.gene_reaction_rule<EOL>if gpr is not None and len(gpr) > <NUM_LIT:0>:<EOL><INDENT>if f_replace and F_GENE_REV in f_replace:<EOL><INDENT>gpr = gpr.replace('<STR_LIT:(>', '<STR_LIT>')<EOL>gpr = gpr.replace('<STR_LIT:)>', '<STR_LIT>')<EOL>tokens = gpr.split('<STR_LIT:U+0020>')<EOL>for k in range(len(tokens)):<EOL><INDENT>if tokens[k] not in ['<STR_LIT:U+0020>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT:(>', '<STR_LIT:)>']:<EOL><INDENT>tokens[k] = f_replace[F_GENE_REV](tokens[k])<EOL><DEDENT><DEDENT>gpr_new = "<STR_LIT:U+0020>".join(tokens)<EOL><DEDENT>gpa = r_fbc.createGeneProductAssociation()  <EOL>gpa.setAssociation(gpr_new)<EOL><DEDENT>if reaction_coefficients.get(cobra_reaction, <NUM_LIT:0>) != <NUM_LIT:0>:<EOL><INDENT>flux_obj = objective.createFluxObjective()  <EOL>flux_obj.setReaction(rid)<EOL>flux_obj.setCoefficient(cobra_reaction.objective_coefficient)<EOL><DEDENT><DEDENT>if len(cobra_model.groups) > <NUM_LIT:0>:<EOL><INDENT>doc.enablePackage(<EOL>"<STR_LIT>",<EOL>"<STR_LIT>", True)<EOL>doc.setPackageRequired("<STR_LIT>", False)<EOL>model_group = model.getPlugin("<STR_LIT>")  <EOL>for cobra_group in cobra_model.groups:<EOL><INDENT>group = model_group.createGroup()  <EOL>group.setId(cobra_group.id)<EOL>group.setName(cobra_group.name)<EOL>group.setKind(cobra_group.kind)<EOL>_sbase_notes_dict(group, cobra_group.notes)<EOL>_sbase_annotations(group, cobra_group.annotation)<EOL>for cobra_member in cobra_group.members:<EOL><INDENT>member = group.createMember()  <EOL>mid = cobra_member.id<EOL>m_type = str(type(cobra_member))<EOL>if "<STR_LIT>" in m_type:<EOL><INDENT>if f_replace and F_REACTION_REV in f_replace:<EOL><INDENT>mid = f_replace[F_REACTION_REV](mid)<EOL><DEDENT><DEDENT>if "<STR_LIT>" in m_type:<EOL><INDENT>if f_replace and F_SPECIE_REV in f_replace:<EOL><INDENT>mid = f_replace[F_SPECIE_REV](mid)<EOL><DEDENT><DEDENT>if "<STR_LIT>" in m_type:<EOL><INDENT>if f_replace and F_GENE_REV in f_replace:<EOL><INDENT>mid = f_replace[F_GENE_REV](mid)<EOL><DEDENT><DEDENT>member.setIdRef(mid)<EOL>if cobra_member.name and len(cobra_member.name) > <NUM_LIT:0>:<EOL><INDENT>member.setName(cobra_member.name)<EOL><DEDENT><DEDENT><DEDENT><DEDENT>return doc<EOL>
Convert Cobra model to SBMLDocument.

    Parameters
    ----------
    cobra_model : cobra.core.Model
        Cobra model instance
    f_replace : dict of replacement functions
        Replacement to apply on identifiers.
    units : boolean
        Should the FLUX_UNITS be written in the SBMLDocument.

    Returns
    -------
    libsbml.SBMLDocument