cobra.io.yaml
=============

.. py:module:: cobra.io.yaml

.. autoapi-nested-parse::

   Provide functions for I/O in YAML format.



Attributes
----------

.. autoapisummary::

   cobra.io.yaml.YAML_SPEC
   cobra.io.yaml.yaml


Classes
-------

.. autoapisummary::

   cobra.io.yaml.CobraYAML


Functions
---------

.. autoapisummary::

   cobra.io.yaml.to_yaml
   cobra.io.yaml.from_yaml
   cobra.io.yaml.save_yaml_model
   cobra.io.yaml.load_yaml_model


Module Contents
---------------

.. py:data:: YAML_SPEC
   :value: '1.2'


.. py:class:: CobraYAML(*, typ: Optional[Union[List[Text], Text]] = None, pure: Any = False, output: Any = None, plug_ins: Any = None)

   Bases: :py:obj:`ruamel.yaml.main.YAML`


   Define custom subclass for YAML I/O.


   .. py:method:: dump(data: Dict, stream: Optional[io.TextIOBase] = None, **kwargs: Any) -> str

      Dump YAML data.

      :param data: A dictionary representing the cobra model and its components.
      :type data: dict
      :param stream: A text stream object inheriting from `io.TextIOBase`. If None,
                     `ruamel.yaml.compat.StringIO` is used (default None).
      :type stream: TextIOBase, optional
      :param \*\*kwargs: Keyword arguments passed on to `ruamel.yaml.main.YAML.dump`.
      :type \*\*kwargs: Any

      :returns: YAML string representation of `data`.
      :rtype: str



.. py:data:: yaml

.. py:function:: to_yaml(model: cobra.Model, sort: bool = False, **kwargs: Any) -> str

   Return the model as a YAML string.

   :param model: The cobra model to represent.
   :type model: cobra.Model
   :param sort: Whether to sort the metabolites, reactions, and genes or maintain the
                order defined in the model (default False).
   :type sort: bool, optional
   :param \*\*kwargs: Keyword arguments passed on to `CobraYAML.dump`.
   :type \*\*kwargs: Any

   :returns: YAML string representation of the cobra model.
   :rtype: str

   .. seealso::

      :py:obj:`save_yaml_model`
          Write directly to a file.

      :py:obj:`ruamel.yaml.dump`
          Base function.


.. py:function:: from_yaml(document: str) -> cobra.Model

   Load a cobra model from a YAML string.

   :param document: The YAML string representation of a cobra model.
   :type document: str

   :returns: The cobra model as interpreted from the YAML document.
   :rtype: cobra.Model

   .. seealso::

      :py:obj:`load_yaml_model`
          Load directly from a file.


.. py:function:: save_yaml_model(model: cobra.Model, filename: str, sort: bool = False, **kwargs: Any) -> None

   Write the cobra model to a file in YAML format.

   :param model: The cobra model to represent.
   :type model: cobra.Model
   :param filename: File path or descriptor that the YAML representation should be
                    written to.
   :type filename: str or file-like
   :param sort: Whether to sort the metabolites, reactions, and genes or maintain the
                order defined in the model (default False).
   :type sort: bool, optional
   :param \*\*kwargs: Keyword arguments passed to `CobraYAML.dump`.
   :type \*\*kwargs: Any

   .. seealso::

      :py:obj:`to_yaml`
          Return a string representation.

      :py:obj:`ruamel.yaml.dump`
          Base function.


.. py:function:: load_yaml_model(filename: Union[str, pathlib.Path]) -> cobra.Model

   Load a cobra model from a file in YAML format.

   :param filename: File path or descriptor that contains the YAML document describing the
                    cobra model.
   :type filename: str or file-like

   :returns: The cobra model as represented in the YAML document.
   :rtype: cobra.Model

   .. seealso::

      :py:obj:`from_yaml`
          Load from a string.


