cobra.io.mat
============

.. py:module:: cobra.io.mat

.. autoapi-nested-parse::

   Provide functions for I/O in MATLAB (.mat) format.



Attributes
----------

.. autoapisummary::

   cobra.io.mat.scipy_sparse
   cobra.io.mat.logger
   cobra.io.mat.MET_MATLAB_TO_PROVIDERS
   cobra.io.mat.MET_PROVIDERS_TO_MATLAB
   cobra.io.mat.MET_NOTES_TO_MATLAB
   cobra.io.mat.MET_MATLAB_TO_NOTES
   cobra.io.mat.RXN_MATLAB_TO_PROVIDERS
   cobra.io.mat.RXN_PROVIDERS_TO_MATLAB
   cobra.io.mat.CONFIDENCE_STR
   cobra.io.mat.RXN_MATLAB_TO_NOTES
   cobra.io.mat.RXN_NOTES_TO_MATLAB
   cobra.io.mat.GENE_MATLAB_TO_PROVIDERS
   cobra.io.mat.GENE_PROVIDERS_TO_MATLAB
   cobra.io.mat.DICT_GENE
   cobra.io.mat.DICT_GENE_REV
   cobra.io.mat.DICT_MET
   cobra.io.mat.DICT_MET_REV
   cobra.io.mat.DICT_MET_NOTES
   cobra.io.mat.DICT_MET_NOTES_REV
   cobra.io.mat.DICT_REACTION
   cobra.io.mat.DICT_REACTION_REV
   cobra.io.mat.DICT_REACTION_NOTES
   cobra.io.mat.DICT_REACTION_NOTES_REV
   cobra.io.mat.DICT_REPLACE
   cobra.io.mat._bracket_re
   cobra.io.mat._underscore_re
   cobra.io.mat._pubmed_re
   cobra.io.mat._punctuation_re
   cobra.io.mat._double_punctuation_re
   cobra.io.mat._ec_re
   cobra.io.mat._chebi_re
   cobra.io.mat._sbo_re


Functions
---------

.. autoapisummary::

   cobra.io.mat._get_id_compartment
   cobra.io.mat._cell
   cobra.io.mat._cell_to_str_list
   cobra.io.mat._cell_to_float_list
   cobra.io.mat.load_matlab_model
   cobra.io.mat.save_matlab_model
   cobra.io.mat.mat_parse_annotations
   cobra.io.mat.mat_parse_notes
   cobra.io.mat.annotations_to_mat
   cobra.io.mat.notes_to_mat
   cobra.io.mat.create_mat_dict
   cobra.io.mat.from_mat_struct


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

.. py:data:: scipy_sparse
   :value: None


.. py:data:: logger

.. py:data:: MET_MATLAB_TO_PROVIDERS

.. py:data:: MET_PROVIDERS_TO_MATLAB

.. py:data:: MET_NOTES_TO_MATLAB

.. py:data:: MET_MATLAB_TO_NOTES

.. py:data:: RXN_MATLAB_TO_PROVIDERS

.. py:data:: RXN_PROVIDERS_TO_MATLAB

.. py:data:: CONFIDENCE_STR
   :value: 'Confidence Level'


.. py:data:: RXN_MATLAB_TO_NOTES

.. py:data:: RXN_NOTES_TO_MATLAB

.. py:data:: GENE_MATLAB_TO_PROVIDERS

.. py:data:: GENE_PROVIDERS_TO_MATLAB

.. py:data:: DICT_GENE
   :value: 'DICT_GENE'


.. py:data:: DICT_GENE_REV
   :value: 'DICT_GENE_REV'


.. py:data:: DICT_MET
   :value: 'DICT_MET'


.. py:data:: DICT_MET_REV
   :value: 'DICT_MET_REV'


.. py:data:: DICT_MET_NOTES
   :value: 'DICT_MET_NOTES'


.. py:data:: DICT_MET_NOTES_REV
   :value: 'DICT_MET_NOTES_REV'


.. py:data:: DICT_REACTION
   :value: 'DICT_REACTION'


.. py:data:: DICT_REACTION_REV
   :value: 'DICT_REACTION_REV'


.. py:data:: DICT_REACTION_NOTES
   :value: 'DICT_REACTION_NOTES'


.. py:data:: DICT_REACTION_NOTES_REV
   :value: 'DICT_REACTION_NOTES_REV'


.. py:data:: DICT_REPLACE
   :type:  dict

.. py:data:: _bracket_re

.. py:data:: _underscore_re

.. py:data:: _pubmed_re

.. py:data:: _punctuation_re

.. py:data:: _double_punctuation_re

.. py:data:: _ec_re

.. py:data:: _chebi_re

.. py:data:: _sbo_re

.. py:function:: _get_id_compartment(_id: str) -> str

   Extract the compartment from the `id` string.

   :param _id: The ID string to extract component from.
   :type _id: str

   :returns: The extracted component string.
   :rtype: str


.. py:function:: _cell(x: Iterable[str]) -> numpy.ndarray

   Translate an iterable `x` into a MATLAB cell array.

   :param x: The data iterable to convert to cell array.
   :type x: iterable of str

   :returns: The converted cell array compatible with MATLAB.
   :rtype: numpy.ndarray


.. py:function:: _cell_to_str_list(m_cell: numpy.ndarray, empty_value: Optional[str] = None, pattern_split: Optional[Pattern] = None, str_prefix: str = '') -> List

   Turn an ndarray (cell) to a list of strings.

   :param m_cell:
   :type m_cell: np.ndarray
   :param empty_value: What value to replace empty cells with. Default None.
   :type empty_value: str, optional
   :param pattern_split: Regular expression to use to split the expression. Used for annotations.
                         Default None.
   :type pattern_split: Pattern, optional
   :param str_prefix: A prefix that will be added to each value in the list if present. Default "".
   :type str_prefix: str, optional

   :returns: A list of processed strings.
   :rtype: List


.. py:function:: _cell_to_float_list(m_cell: numpy.ndarray, empty_value: Optional[float] = None, inf_value: Optional[float] = None) -> List

   Turn an ndarray (cell) to a list of floats.

   :param m_cell:
   :type m_cell: np.ndarray
   :param empty_value: What value to replace empty cells with. Default None.
   :type empty_value: float, optional
   :param inf_value: Replace infinite values with defined inf. Default None (don't replace), will be
                     replaced by given value  if given.
   :type inf_value: float, optional

   :returns: A list of processed floats.
   :rtype: List


.. py:function:: load_matlab_model(infile_path: Union[str, pathlib.Path, IO], variable_name: Optional[str] = None, inf: float = np.inf) -> cobra.core.Model

   Load a cobra model stored as a .mat file.

   :param infile_path: File path or descriptor of the .mat file describing the cobra model.
   :type infile_path: str or Path or filehandle
   :param variable_name: The variable name of the model in the .mat file. If None, then the
                         first MATLAB variable which looks like a COBRA model will be used
                         (default None).
   :type variable_name: str, optional
   :param inf: The value to use for infinite bounds. Some solvers do not handle
               infinite values so for using those, set this to a high numeric value
               (default `numpy.inf`).
   :type inf: float, optional

   :returns: The cobra model as represented in the .mat file.
   :rtype: cobra.Model

   :raises ImportError: If scipy is not found in the Python environment.
   :raises IOError: If no COBRA model is found in the .mat file.


.. py:function:: save_matlab_model(model: cobra.core.Model, file_name: Union[str, pathlib.Path, IO], varname: Optional[str] = None) -> None

   Save the cobra model as a .mat file.

   This .mat file can be used directly in cobratoolbox.

   :param model: The cobra model to represent.
   :type model: cobra.Model
   :param file_name: File path or descriptor that the MATLAB representation should be
                     written to.
   :type file_name: str or file-like or Path
   :param varname: The name of the variable within the MATLAB workspace. Model ID is
                   used if available, else 'exported_model' is used (default None).
   :type varname: str, optional


.. py:function:: mat_parse_annotations(target_list: List[cobra.core.Object], mat_struct: numpy.ndarray, d_replace: str = DICT_MET) -> None

   Process mat structure annotations in place.

   Will process mat structured annotations and add them to a list of new entities
   (metabolites, reactions, genes) in a format based on identifiers.org.

   :param target_list: A list of cobra objects, including metabolites, reactions or genes. The
                       annotations will be added to these lists.
   :type target_list: list[cobra.Object]
   :param mat_struct: A darray that includes the data imported from matlab file.
   :type mat_struct: np.ndarray
   :param d_replace: A string that points to the dictionary of converstions between MATLAB and
                     providers. Default DICT_MET (for metabolite).
   :type d_replace: str


.. py:function:: mat_parse_notes(target_list: List[cobra.core.Object], mat_struct: numpy.ndarray, d_replace: str = DICT_REACTION_NOTES) -> None

   Process mat structure notes in place.

   Will process mat structured notes and add them to a list of new entities
   (metabolites, reactions, genes) in a format based on identifiers.org.

   :param target_list: A list of cobra objects, including metabolites, reactions or genes. The
                       notes will be added to these lists.
   :type target_list: list[cobra.Object]
   :param mat_struct: A darray that includes the data imported from matlab file.
   :type mat_struct: np.ndarray
   :param d_replace: A string that points to the dictionary of converstions between MATLAB and
                     notes. Default DICT_REACTION_NOTES (for reactions).
   :type d_replace: str


.. py:function:: annotations_to_mat(mat_dict: collections.OrderedDict, annotation_list: List[Dict], d_replace: str = DICT_MET_REV) -> None

   Process mat structure annotations in place.

   Will process mat structured annotations and add them to a list of new entities
   (metabolites, reactions, genes) in a format based on identifiers.org.

   :param mat_dict: An ordered dictionary having model attributes as keys and their
                    respective values represented as arrays, as the values. Annotations will
                    be inserted into this OrderdDict.
   :type mat_dict: OrderedDict
   :param annotation_list: A list of cobra annotations, in the form of a dictionary.
   :type annotation_list: list[Dict]
   :param d_replace: A string that points to the dictionary of converstions between MATLAB and
                     providers. Default DICT_MET_REV (for metabolite).
   :type d_replace: str


.. py:function:: notes_to_mat(mat_dict: collections.OrderedDict, note_list: List[Dict], d_replace: str = DICT_MET_REV) -> None

   Process mat structure notes in place.

   Will process mat structured annotations and add them to a list of new entities
   (metabolites, reactions, genes) in a format based on identifiers.org.

   :param mat_dict: An ordered dictionary having model attributes as keys and their
                    respective values represented as arrays, as the values. Annotations will
                    be inserted into this OrderdDict.
   :type mat_dict: OrderedDict
   :param note_list: A list of cobra annotations, in the form of a dictionary.
   :type note_list: list[Dict]
   :param d_replace: A string that points to the dictionary of converstions between MATLAB and
                     providers. Default DICT_MET_REV (for metabolite).
   :type d_replace: str


.. py:function:: create_mat_dict(model: cobra.core.Model) -> collections.OrderedDict

   Create a dictionary mapping model attributes to arrays.

   :param model: The model to create dictionary for.
   :type model: cobra.Model

   :returns: The ordered dictionary having model attributes as keys and their
             respective values represented as arrays, as the values.
   :rtype: OrderedDict


.. py:function:: from_mat_struct(mat_struct: numpy.ndarray, model_id: Optional[str] = None, inf: float = np.inf) -> cobra.core.Model

   Create a model from the cobratoolbox struct.

   :param mat_struct: The `numpy.ndarray` that most likely contains the model, being chosen by
                      load_matlab_file after loading the matlab structure via scipy_io.loadmat.
   :type mat_struct: numpy.ndarray
   :param model_id: The ID of the model generated. If None, will try to look for ID in
                    model's description. If multiple IDs are found, the first one is
                    used. If no IDs are found, will use 'imported_model' (default None).
   :type model_id: str, optional
   :param inf: The value to use for infinite bounds. Some solvers do not handle
               infinite values so for using those, set this to a high numeric value
               (default `numpy.inf`).
   :type inf: float, optional

   :returns: The model as represented in .mat file.
   :rtype: cobra.Model


