cobra.io.dict
=============

.. py:module:: cobra.io.dict

.. autoapi-nested-parse::

   Provide functions for cobrapy objects to generic Python objects and vice-versa.



Attributes
----------

.. autoapisummary::

   cobra.io.dict._REQUIRED_REACTION_ATTRIBUTES
   cobra.io.dict._ORDERED_OPTIONAL_REACTION_KEYS
   cobra.io.dict._OPTIONAL_REACTION_ATTRIBUTES
   cobra.io.dict._REQUIRED_METABOLITE_ATTRIBUTES
   cobra.io.dict._ORDERED_OPTIONAL_METABOLITE_KEYS
   cobra.io.dict._OPTIONAL_METABOLITE_ATTRIBUTES
   cobra.io.dict._REQUIRED_GENE_ATTRIBUTES
   cobra.io.dict._ORDERED_OPTIONAL_GENE_KEYS
   cobra.io.dict._OPTIONAL_GENE_ATTRIBUTES
   cobra.io.dict._ORDERED_OPTIONAL_MODEL_KEYS
   cobra.io.dict._OPTIONAL_MODEL_ATTRIBUTES


Functions
---------

.. autoapisummary::

   cobra.io.dict._fix_type
   cobra.io.dict._update_optional
   cobra.io.dict._metabolite_to_dict
   cobra.io.dict._metabolite_from_dict
   cobra.io.dict._gene_to_dict
   cobra.io.dict.gene_from_dict
   cobra.io.dict._reaction_to_dict
   cobra.io.dict._reaction_from_dict
   cobra.io.dict.model_to_dict
   cobra.io.dict.model_from_dict


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

.. py:data:: _REQUIRED_REACTION_ATTRIBUTES
   :value: ['id', 'name', 'metabolites', 'lower_bound', 'upper_bound', 'gene_reaction_rule']


.. py:data:: _ORDERED_OPTIONAL_REACTION_KEYS
   :value: ['objective_coefficient', 'subsystem', 'notes', 'annotation']


.. py:data:: _OPTIONAL_REACTION_ATTRIBUTES

.. py:data:: _REQUIRED_METABOLITE_ATTRIBUTES
   :value: ['id', 'name', 'compartment']


.. py:data:: _ORDERED_OPTIONAL_METABOLITE_KEYS
   :value: ['charge', 'formula', '_bound', 'notes', 'annotation']


.. py:data:: _OPTIONAL_METABOLITE_ATTRIBUTES

.. py:data:: _REQUIRED_GENE_ATTRIBUTES
   :value: ['id', 'name']


.. py:data:: _ORDERED_OPTIONAL_GENE_KEYS
   :value: ['notes', 'annotation']


.. py:data:: _OPTIONAL_GENE_ATTRIBUTES

.. py:data:: _ORDERED_OPTIONAL_MODEL_KEYS
   :value: ['name', 'compartments', 'notes', 'annotation']


.. py:data:: _OPTIONAL_MODEL_ATTRIBUTES

.. py:function:: _fix_type(value: Union[str, float, bool, Set, Dict]) -> Union[str, float, bool, List, collections.OrderedDict]

   Convert possible types to correct Python types.

   :param value: The value to fix type for.
   :type value: str, float, bool, set, dict

   :returns: The fixed type for the value.
   :rtype: str, float, bool, list, dict


.. py:function:: _update_optional(cobra_object: cobra.Object, new_dict: Dict, optional_attribute_dict: Dict, ordered_keys: Sequence) -> None

   Update `new_dict` with optional attributes from `cobra_object`.

   :param cobra_object: The cobra Object to update optional attributes from.
   :type cobra_object: cobra.Object
   :param new_dict: The dictionary to update optional attributes for.
   :type new_dict: dict
   :param optional_attribute_dict: The dictionary to use as default value lookup store.
   :type optional_attribute_dict: dict
   :param ordered_keys: The keys to update values for.
   :type ordered_keys: list, tuple

   :raises IndexError: If key in `ordered_keys` is not found in `optional_attribute_dict`.
   :raises AttributeError: If key in `ordered_keys` is not found in `cobra_object`.


.. py:function:: _metabolite_to_dict(metabolite: cobra.core.Metabolite) -> collections.OrderedDict

   Convert a cobra Metabolite object to dictionary.

   :param metabolite: The cobra.Metabolite to convert to dictionary.
   :type metabolite: cobra.Metabolite

   :returns: The converted dictionary object.
   :rtype: dict

   .. seealso::

      :py:obj:`_metabolite_from_dict`
          Convert a dictionary to cobra Metabolite object.


.. py:function:: _metabolite_from_dict(metabolite: Dict) -> cobra.core.Metabolite

   Convert a dictionary to cobra Metabolite object.

   :param metabolite: The dictionary to convert to cobra.Metabolite .
   :type metabolite: dict

   :rtype: cobra.Metabolite

   .. seealso::

      :py:obj:`_metabolite_to_dict`
          Convert a cobra Metabolite object to dictionary.


.. py:function:: _gene_to_dict(gene: cobra.core.Gene) -> collections.OrderedDict

   Convert a cobra Gene object to dictionary.

   :param gene: The cobra.Gene to convert to dictionary.
   :type gene: cobra.Gene

   :returns: The converted dictionary object.
   :rtype: dict

   .. seealso::

      :py:obj:`_gene_from_dict`
          Convert a dictionary to cobra Gene object.


.. py:function:: gene_from_dict(gene: Dict) -> cobra.core.Gene

   Convert a dictionary to cobra Gene object.

   :param gene: The dictionary to convert to cobra.Gene .
   :type gene: dict

   :returns: The converted cobra.Gene object.
   :rtype: cobra.Gene

   .. seealso::

      :py:obj:`_gene_to_dict`
          Convert a cobra Gene object to a dictionary.


.. py:function:: _reaction_to_dict(reaction: cobra.core.Reaction) -> collections.OrderedDict

   Convert a cobra Reaction object to a dictionary.

   :param reaction: The cobra.Reaction to convert to dictionary.
   :type reaction: cobra.Reaction

   :returns: The converted dictionary object.
   :rtype: dict

   .. seealso::

      :py:obj:`_reaction_from_dict`
          Convert a dictionary to a cobra Reaction object.


.. py:function:: _reaction_from_dict(reaction: Dict, model: cobra.core.Model) -> cobra.core.Reaction

   Convert a dictionary to a cobra Reaction object.

   :param reaction: The dictionary to convert to cobra.Reaction .
   :type reaction: dict
   :param model: The model to which the reaction should associate with.
   :type model: cobra.Model

   :returns: The converted cobra.Reaction object.
   :rtype: cobra.Reaction

   .. seealso::

      :py:obj:`_reaction_to_dict`
          Convert a cobra Reaction object to a dictionary.


.. py:function:: model_to_dict(model: cobra.core.Model, sort: bool = False) -> collections.OrderedDict

   Convert a cobra Model to a dictionary.

   :param model: The model to reformulate as a dict.
   :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

   :returns: A dictionary with keys: 'genes', 'compartments', 'id',
             'metabolites', 'notes' and 'reactions'; where 'metabolites', 'genes'
             and 'metabolites' are in turn lists of dictionaries holding all
             attributes to form the corresponding object.
   :rtype: OrderedDict

   .. seealso::

      :py:obj:`model_from_dict`
          Convert a dictionary to a cobra Model.


.. py:function:: model_from_dict(obj: Dict) -> cobra.core.Model

   Build a cobra Model from a dictionary.

   Models stored in JSON are first formulated as a dictionary that can be read
   to a cobra Model using this function.

   :param obj: A dictionary with keys: 'genes', 'compartments', 'id',
               'metabolites', 'notes' and 'reactions'; where 'metabolites', 'genes'
               and 'metabolites' are in turn lists of dictionaries holding all
               attributes to form the corresponding object.
   :type obj: dict

   :returns: The generated model.
   :rtype: cobra.Model

   :raises ValueError: If `obj` has no 'reactions' attribute.

   .. seealso::

      :py:obj:`model_to_dict`
          Convert a cobra Model to a dictionary.


