Write the given MetabolicModel to YAML files in dest folder.

    The parameter ``convert_exchange`` indicates whether the exchange reactions
    should be converted automatically to an exchange file.

def write_yaml_model(model, dest='.', convert_exchange=True,
                     split_subsystem=True):
    """Write the given MetabolicModel to YAML files in dest folder.

    The parameter ``convert_exchange`` indicates whether the exchange reactions
    should be converted automatically to an exchange file.
    """
    yaml.SafeDumper.add_representer(OrderedDict, _dict_representer)
    yaml.SafeDumper.add_representer(set, _set_representer)
    yaml.SafeDumper.add_representer(frozenset, _set_representer)
    yaml.SafeDumper.add_representer(
        boolean.Expression, _boolean_expression_representer)
    yaml.SafeDumper.add_representer(Decimal, _decimal_representer)

    yaml.SafeDumper.ignore_aliases = lambda *args: True

    yaml.add_constructor(yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG,
                         _dict_constructor)

    yaml_args = {'default_flow_style': False,
                 'encoding': 'utf-8',
                 'allow_unicode': True,
                 'width': 79}

    # The ModelWriter from PSAMM is not yet able to write the full model but
    # only reactions and compounds.
    writer = ModelWriter()

    with open(os.path.join(dest, 'compounds.yaml'), 'w+') as f:
        writer.write_compounds(f, sorted(model.compounds, key=lambda c: c.id))

    if model.default_flux_limit is None:
        model.default_flux_limit = detect_best_flux_limit(model)

    if model.extracellular_compartment is None:
        model.extracellular_compartment = (
            sbml.detect_extracellular_compartment(model))

    if model.default_compartment is None:
        model.default_compartment = get_default_compartment(model)

    if model.default_flux_limit is not None:
        logger.info('Using default flux limit of {}'.format(
            model.default_flux_limit))

    if convert_exchange:
        logger.info('Converting exchange reactions to exchange file')
        sbml.convert_exchange_to_compounds(model)

    if len(model.compartments) == 0:
        infer_compartment_entries(model)
        logger.info('Inferred {} compartments: {}'.format(
            len(model.compartments),
            ', '.join(c.id for c in model.compartments)))

    if len(model.compartments) != 0 and len(model.compartment_boundaries) == 0:
        infer_compartment_adjacency(model)

    reaction_files = reactions_to_files(model, dest, writer, split_subsystem)

    if len(model.exchange) > 0:
        with open(os.path.join(dest, 'exchange.yaml'), 'w+') as f:
            yaml.safe_dump(model_exchange(model), f, **yaml_args)

    reaction_limits = list(model_reaction_limits(model))
    if len(reaction_limits) > 0:
        with open(os.path.join(dest, 'limits.yaml'), 'w+') as f:
            yaml.safe_dump(reaction_limits, f, **yaml_args)

    model_d = OrderedDict()
    if model.name is not None:
        model_d['name'] = model.name

    if model.biomass_reaction is not None:
        model_d['biomass'] = model.biomass_reaction
    if model.default_flux_limit is not None:
        model_d['default_flux_limit'] = model.default_flux_limit
    if model.extracellular_compartment != 'e':
        model_d['extracellular'] = model.extracellular_compartment
    if model.default_compartment != 'c':
        model_d['default_compartment'] = model.default_compartment

    if len(model.compartments) > 0:
        adjacency = {}
        for c1, c2 in model.compartment_boundaries:
            adjacency.setdefault(c1, set()).add(c2)
            adjacency.setdefault(c2, set()).add(c1)

        compartment_list = []
        for compartment in sorted(model.compartments, key=lambda c: c.id):
            adjacent = adjacency.get(compartment.id)
            if adjacent is not None and len(adjacent) == 1:
                adjacent = next(iter(adjacent))
            compartment_list.append(writer.convert_compartment_entry(
                compartment, adjacent))

        model_d['compartments'] = compartment_list

    model_d['compounds'] = [{'include': 'compounds.yaml'}]
    model_d['reactions'] = []
    for reaction_file in reaction_files:
        model_d['reactions'].append({'include': reaction_file})

    if len(model.exchange) > 0:
        model_d['exchange'] = [{'include': 'exchange.yaml'}]

    if len(reaction_limits) > 0:
        model_d['limits'] = [{'include': 'limits.yaml'}]

    with open(os.path.join(dest, 'model.yaml'), 'w+') as f:
        yaml.safe_dump(model_d, f, **yaml_args)