def write_model(self, file, model, pretty=False):
ET.register_namespace('<STR_LIT>', MATHML_NS)<EOL>ET.register_namespace('<STR_LIT>', XHTML_NS)<EOL>ET.register_namespace('<STR_LIT>', FBC_V2)<EOL>compound_name = {}<EOL>compound_properties = {}<EOL>for compound in model.compounds:<EOL><INDENT>compound_name[compound.id] = (<EOL>compound.name if compound.name is not None else compound.id)<EOL>compound_properties[compound.id] = compound.properties<EOL><DEDENT>model_reactions = set(model.model)<EOL>reaction_properties = {}<EOL>biomass_id = None<EOL>for r in model.reactions:<EOL><INDENT>if (model_reactions is not None and<EOL>r.id not in model_reactions):<EOL><INDENT>continue<EOL><DEDENT>reaction_id = util.create_unique_id(<EOL>self._make_safe_id(r.id), reaction_properties)<EOL>if r.id == model.biomass_reaction:<EOL><INDENT>biomass_id = reaction_id<EOL><DEDENT>reaction_properties[reaction_id] = r.properties<EOL><DEDENT>flux_limits = {}<EOL>for compound, reaction_id, lower, upper in itervalues(model.exchange):<EOL><INDENT>if reaction_id is None:<EOL><INDENT>reaction_id = create_exchange_id(reaction_properties, compound)<EOL><DEDENT>reaction_id = util.create_unique_id(<EOL>self._make_safe_id(reaction_id), reaction_properties)<EOL>reaction_properties[reaction_id] = {<EOL>'<STR_LIT:id>': reaction_id,<EOL>'<STR_LIT>': Reaction(Direction.Both, {compound: -<NUM_LIT:1>})<EOL>}<EOL>if lower is None:<EOL><INDENT>lower = -model.default_flux_limit<EOL><DEDENT>if upper is None:<EOL><INDENT>upper = model.default_flux_limit<EOL><DEDENT>flux_limits[reaction_id] = (lower, upper)<EOL>if compound.name not in compound_properties:<EOL><INDENT>compound_properties[compound.name] = {<EOL>'<STR_LIT:id>': compound.name<EOL>}<EOL><DEDENT><DEDENT>root = ET.Element(self._sbml_tag('<STR_LIT>'))<EOL>root.set(self._sbml_tag('<STR_LIT>'), '<STR_LIT:3>')<EOL>root.set(self._sbml_tag('<STR_LIT:version>'), '<STR_LIT:1>')<EOL>root.set(_tag('<STR_LIT>', FBC_V2), '<STR_LIT:false>')<EOL>if model.version_string is not None:<EOL><INDENT>notes_tag = ET.SubElement(root, self._sbml_tag('<STR_LIT>'))<EOL>body_tag = ET.SubElement(notes_tag, _tag('<STR_LIT:body>', XHTML_NS))<EOL>self._add_properties_notes(<EOL>body_tag, {'<STR_LIT>': model.version_string})<EOL><DEDENT>model_tag = ET.SubElement(root, self._sbml_tag('<STR_LIT>'))<EOL>model_tag.set(_tag('<STR_LIT:strict>', FBC_V2), '<STR_LIT:true>')<EOL>if model.name is not None:<EOL><INDENT>model_tag.set(self._sbml_tag('<STR_LIT:name>'), model.name)<EOL><DEDENT>model_compartments = {}<EOL>model_species = {}<EOL>species_ids = set()<EOL>for _, properties in iteritems(reaction_properties):<EOL><INDENT>for compound, _ in properties['<STR_LIT>'].compounds:<EOL><INDENT>if compound in model_species:<EOL><INDENT>continue<EOL><DEDENT>if compound.name not in compound_properties:<EOL><INDENT>compound_properties[compound.name] = {<EOL>'<STR_LIT:id>': compound.name<EOL>}<EOL><DEDENT>compound_id = util.create_unique_id(<EOL>self._make_safe_id(compound.name), species_ids)<EOL>model_species[compound] = compound_id<EOL>species_ids.add(compound_id)<EOL>if compound.compartment not in model_compartments:<EOL><INDENT>model_compartments[<EOL>compound.compartment] = '<STR_LIT>' + util.create_unique_id(<EOL>self._make_safe_id(compound.compartment),<EOL>model_compartments)<EOL><DEDENT><DEDENT><DEDENT>compartments = ET.SubElement(<EOL>model_tag, self._sbml_tag('<STR_LIT>'))<EOL>for _, compartment_id in iteritems(model_compartments):<EOL><INDENT>compartment_tag = ET.SubElement(<EOL>compartments, self._sbml_tag('<STR_LIT>'))<EOL>compartment_tag.set(self._sbml_tag('<STR_LIT:id>'), compartment_id)<EOL>compartment_tag.set(self._sbml_tag('<STR_LIT>'), '<STR_LIT:true>')<EOL><DEDENT>species_list = ET.SubElement(<EOL>model_tag, self._sbml_tag('<STR_LIT>'))<EOL>for species, species_id in sorted(<EOL>iteritems(model_species), key=lambda x: x[<NUM_LIT:1>]):<EOL><INDENT>species_tag = ET.SubElement(species_list,<EOL>self._sbml_tag('<STR_LIT>'))<EOL>species_tag.set(self._sbml_tag('<STR_LIT:id>'), '<STR_LIT>' + species_id)<EOL>species_tag.set(<EOL>self._sbml_tag('<STR_LIT:name>'),<EOL>compound_name.get(species.name, species.name))<EOL>species_tag.set(<EOL>self._sbml_tag('<STR_LIT>'),<EOL>model_compartments[species.compartment])<EOL>species_tag.set(self._sbml_tag('<STR_LIT>'), '<STR_LIT:false>')<EOL>species_tag.set(self._sbml_tag('<STR_LIT>'), '<STR_LIT:false>')<EOL>species_tag.set(self._sbml_tag('<STR_LIT>'), '<STR_LIT:true>')<EOL>if '<STR_LIT>' in compound_properties[species.name]:<EOL><INDENT>species_tag.set(_tag('<STR_LIT>', FBC_V2), text_type(<EOL>compound_properties[species.name]['<STR_LIT>']))<EOL><DEDENT>if '<STR_LIT>' in compound_properties[species.name]:<EOL><INDENT>species_tag.set(_tag(<EOL>'<STR_LIT>', FBC_V2), text_type(<EOL>compound_properties[species.name]['<STR_LIT>']))<EOL><DEDENT>notes_tag = ET.SubElement(species_tag, self._sbml_tag('<STR_LIT>'))<EOL>body_tag = ET.SubElement(notes_tag, _tag('<STR_LIT:body>', XHTML_NS))<EOL>self._add_properties_notes(<EOL>body_tag, compound_properties[species.name])<EOL><DEDENT>params_list = ET.SubElement(<EOL>model_tag, self._sbml_tag('<STR_LIT>'))<EOL>for rxn_id, lower_lim, upper_lim in itervalues(model.limits):<EOL><INDENT>flux_limits[rxn_id] = lower_lim, upper_lim<EOL><DEDENT>params = {}<EOL>gene_ids = {}<EOL>if biomass_id is not None:<EOL><INDENT>self._add_fbc_objective(model_tag, biomass_id)<EOL><DEDENT>reactions = ET.SubElement(model_tag, self._sbml_tag('<STR_LIT>'))<EOL>for eq_id, properties in sorted(iteritems(reaction_properties)):<EOL><INDENT>reaction_tag = ET.SubElement(reactions, self._sbml_tag('<STR_LIT>'))<EOL>equation = properties['<STR_LIT>']<EOL>reaction_tag.set(self._sbml_tag('<STR_LIT:id>'), '<STR_LIT>' + eq_id)<EOL>if '<STR_LIT:name>' in properties:<EOL><INDENT>reaction_tag.set(self._sbml_tag('<STR_LIT:name>'), properties['<STR_LIT:name>'])<EOL><DEDENT>reaction_tag.set(self._sbml_tag('<STR_LIT>'), text_type(<EOL>equation.direction == Direction.Both).lower())<EOL>reaction_tag.set(self._sbml_tag('<STR_LIT>'), '<STR_LIT:false>')<EOL>lower_str, upper_str = self._get_flux_bounds(<EOL>eq_id, model, flux_limits, equation)<EOL>params[upper_str] = '<STR_LIT>'+self._make_safe_numerical_id(upper_str)<EOL>params[lower_str] = '<STR_LIT>'+self._make_safe_numerical_id(lower_str)<EOL>reaction_tag.set(<EOL>_tag('<STR_LIT>', FBC_V2), params[upper_str])<EOL>reaction_tag.set(<EOL>_tag('<STR_LIT>', FBC_V2), params[lower_str])<EOL>if '<STR_LIT>' in properties:<EOL><INDENT>self._add_gene_associations(<EOL>eq_id, properties['<STR_LIT>'], gene_ids, reaction_tag)<EOL><DEDENT>if any(value < <NUM_LIT:0> for _, value in equation.compounds):<EOL><INDENT>reactants = ET.SubElement(<EOL>reaction_tag, self._sbml_tag('<STR_LIT>'))<EOL><DEDENT>if any(value > <NUM_LIT:0> for _, value in equation.compounds):<EOL><INDENT>products = ET.SubElement(<EOL>reaction_tag, self._sbml_tag('<STR_LIT>'))<EOL><DEDENT>for compound, value in sorted(equation.compounds):<EOL><INDENT>dest_list = reactants if value < <NUM_LIT:0> else products<EOL>spec_ref = ET.SubElement(<EOL>dest_list, self._sbml_tag('<STR_LIT>'))<EOL>spec_ref.set(<EOL>self._sbml_tag('<STR_LIT>'), '<STR_LIT>' + model_species[compound])<EOL>spec_ref.set(<EOL>self._sbml_tag('<STR_LIT>'), '<STR_LIT:true>')<EOL>spec_ref.set(<EOL>self._sbml_tag('<STR_LIT>'), text_type(abs(value)))<EOL><DEDENT>notes_tag = ET.SubElement(reaction_tag, self._sbml_tag('<STR_LIT>'))<EOL>body_tag = ET.SubElement(notes_tag, _tag('<STR_LIT:body>', XHTML_NS))<EOL>self._add_properties_notes(body_tag, reaction_properties[eq_id])<EOL>if self._cobra_flux_bounds is True:<EOL><INDENT>kl_tag = ET.SubElement(<EOL>reaction_tag, self._sbml_tag('<STR_LIT>'))<EOL>math_tag = ET.SubElement(kl_tag, self._sbml_tag('<STR_LIT>'))<EOL>ci_tag = ET.SubElement(math_tag, _tag('<STR_LIT>', MATHML_NS))<EOL>ci_tag.text = '<STR_LIT>'<EOL>param_list = ET.SubElement(<EOL>kl_tag, self._sbml_tag('<STR_LIT>'))<EOL>ET.SubElement(param_list, self._sbml_tag('<STR_LIT>'), {<EOL>self._sbml_tag('<STR_LIT:id>'): '<STR_LIT>',<EOL>self._sbml_tag('<STR_LIT:name>'): '<STR_LIT>',<EOL>self._sbml_tag('<STR_LIT:value>'): lower_str,<EOL>self._sbml_tag('<STR_LIT>'): '<STR_LIT:true>'<EOL>})<EOL>ET.SubElement(param_list, self._sbml_tag('<STR_LIT>'), {<EOL>self._sbml_tag('<STR_LIT:id>'): '<STR_LIT>',<EOL>self._sbml_tag('<STR_LIT:name>'): '<STR_LIT>',<EOL>self._sbml_tag('<STR_LIT:value>'): upper_str,<EOL>self._sbml_tag('<STR_LIT>'): '<STR_LIT:true>'<EOL>})<EOL><DEDENT><DEDENT>for val, id in iteritems(params):<EOL><INDENT>param_tag = ET.SubElement(params_list, self._sbml_tag('<STR_LIT>'))<EOL>param_tag.set(self._sbml_tag('<STR_LIT:id>'), id)<EOL>param_tag.set(self._sbml_tag('<STR_LIT:value>'), val)<EOL>param_tag.set(self._sbml_tag('<STR_LIT>'), '<STR_LIT:true>')<EOL><DEDENT>self._add_gene_list(model_tag, gene_ids)<EOL>tree = ET.ElementTree(root)<EOL>if pretty:<EOL><INDENT>self._indent(root)<EOL><DEDENT>write_options = dict(<EOL>encoding='<STR_LIT:utf-8>',<EOL>default_namespace=self._namespace)<EOL>if PY3:<EOL><INDENT>write_options['<STR_LIT>'] = '<STR_LIT>'<EOL><DEDENT>tree.write(file, **write_options)<EOL>
Write a given model to file.

        Args:
            file: File-like object open for writing.
            model: Instance of :class:`NativeModel` to write.
            pretty: Whether to format the XML output for readability.