17.1.1.3.2.3. cobra.io.mat

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

17.1.1.3.2.3.1. Module Contents

17.1.1.3.2.3.1.1. Functions

_get_id_compartment(_id: str) → str

Extract the compartment from the id string.

_cell(x: Iterable[str]) → np.ndarray

Translate an iterable x into a MATLAB cell array.

_cell_to_str_list(m_cell: np.ndarray, empty_value: Optional[str] = None, pattern_split: Optional[Pattern] = None, str_prefix: str = ‘’) → List

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

_cell_to_float_list(m_cell: np.ndarray, empty_value: Optional[float] = None) → List

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

load_matlab_model(infile_path: str, variable_name: Optional[str] = None, inf: float = np.inf) → Model

Load a cobra model stored as a .mat file.

save_matlab_model(model: Model, file_name: str, varname: Optional[str] = None) → None

Save the cobra model as a .mat file.

mat_parse_annotations(target_list: List[Object], mat_struct: np.ndarray, d_replace: str = DICT_MET) → None

Process mat structure annotations in place.

mat_parse_notes(target_list: List[Object], mat_struct: np.ndarray, d_replace: str = DICT_REACTION_NOTES) → None

Process mat structure notes in place.

annotations_to_mat(mat_dict: OrderedDict, annotation_list: List[Dict], d_replace: str = DICT_MET_REV) → None

Process mat structure annotations in place.

notes_to_mat(mat_dict: OrderedDict, note_list: List[Dict], d_replace: str = DICT_MET_REV) → None

Process mat structure notes in place.

create_mat_dict(model: Model) → OrderedDict

Create a dictionary mapping model attributes to arrays.

from_mat_struct(mat_struct: np.ndarray, model_id: Optional[str] = None, inf: float = np.inf) → Model

Create a model from the cobratoolbox struct.

cobra.io.mat.scipy_sparse[source]
cobra.io.mat.logger[source]
cobra.io.mat.MET_MATLAB_TO_PROVIDERS[source]
cobra.io.mat.MET_PROVIDERS_TO_MATLAB[source]
cobra.io.mat.MET_NOTES_TO_MATLAB[source]
cobra.io.mat.MET_MATLAB_TO_NOTES[source]
cobra.io.mat.RXN_MATLAB_TO_PROVIDERS[source]
cobra.io.mat.RXN_PROVIDERS_TO_MATLAB[source]
cobra.io.mat.RXN_MATLAB_TO_NOTES[source]
cobra.io.mat.RXN_NOTES_TO_MATLAB[source]
cobra.io.mat.GENE_MATLAB_TO_PROVIDERS[source]
cobra.io.mat.GENE_PROVIDERS_TO_MATLAB[source]
cobra.io.mat.DICT_GENE = DICT_GENE[source]
cobra.io.mat.DICT_GENE_REV = DICT_GENE_REV[source]
cobra.io.mat.DICT_MET = DICT_MET[source]
cobra.io.mat.DICT_MET_REV = DICT_MET_REV[source]
cobra.io.mat.DICT_MET_NOTES = DICT_MET_NOTES[source]
cobra.io.mat.DICT_MET_NOTES_REV = DICT_MET_NOTES_REV[source]
cobra.io.mat.DICT_REACTION = DICT_REACTION[source]
cobra.io.mat.DICT_REACTION_REV = DICT_REACTION_REV[source]
cobra.io.mat.DICT_REACTION_NOTES = DICT_REACTION_NOTES[source]
cobra.io.mat.DICT_REACTION_NOTES_REV = DICT_REACTION_NOTES_REV[source]
cobra.io.mat.DICT_REPLACE :dict[source]
cobra.io.mat._bracket_re[source]
cobra.io.mat._underscore_re[source]
cobra.io.mat._pubmed_re[source]
cobra.io.mat._punctuation_re[source]
cobra.io.mat._double_punctuation_re[source]
cobra.io.mat._ec_re[source]
cobra.io.mat._chebi_re[source]
cobra.io.mat._get_id_compartment(_id: str) → str[source]

Extract the compartment from the id string.

Parameters

_id (str) – The ID string to extract component from.

Returns

The extracted component string.

Return type

str

cobra.io.mat._cell(x: Iterable[str]) → np.ndarray[source]

Translate an iterable x into a MATLAB cell array.

Parameters

x (iterable of str) – The data iterable to convert to cell array.

Returns

The converted cell array compatible with MATLAB.

Return type

numpy.ndarray

cobra.io.mat._cell_to_str_list(m_cell: np.ndarray, empty_value: Optional[str] = None, pattern_split: Optional[Pattern] = None, str_prefix: str = '') → List[source]

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

Parameters
  • m_cell (np.ndarray) –

  • empty_value (str, optional) – What value to replace empty cells with. Default None.

  • pattern_split (Pattern, optional) – Regular expression to use to split the expression. Used for annotations. Default None.

  • str_prefix (str, optional) – A prefix that will be added to each value in the list if present. Default “”.

Returns

A list of processed strings.

Return type

List

cobra.io.mat._cell_to_float_list(m_cell: np.ndarray, empty_value: Optional[float] = None) → List[source]

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

Parameters
  • m_cell (np.ndarray) –

  • empty_value (float, optional) – What value to replace empty cells with. Default None.

Returns

A list of processed floats.

Return type

List

cobra.io.mat.load_matlab_model(infile_path: str, variable_name: Optional[str] = None, inf: float = np.inf) → Model[source]

Load a cobra model stored as a .mat file.

Parameters
  • infile_path (str) – File path or descriptor of the .mat file describing the cobra model.

  • variable_name (str, optional) – 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).

  • inf (float, optional) – 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).

Returns

The cobra model as represented in the .mat file.

Return type

cobra.Model

Raises
  • ImportError – If scipy is not found in the Python environment.

  • IOError – If no COBRA model is found in the .mat file.

cobra.io.mat.save_matlab_model(model: Model, file_name: str, varname: Optional[str] = None) → None[source]

Save the cobra model as a .mat file.

This .mat file can be used directly in cobratoolbox.

Parameters
  • model (cobra.Model) – The cobra model to represent.

  • file_name (str or file-like) – File path or descriptor that the MATLAB representation should be written to.

  • varname (str, optional) – The name of the variable within the MATLAB workspace. Model ID is used if available, else ‘exported_model’ is used (default None).

cobra.io.mat.mat_parse_annotations(target_list: List[Object], mat_struct: np.ndarray, d_replace: str = DICT_MET) → None[source]

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.

Parameters
  • target_list (list[cobra.Object]) – A list of cobra objects, including metabolites, reactions or genes. The annotations will be added to these lists.

  • mat_struct (np.ndarray) – A darray that includes the data imported from matlab file.

  • d_replace (str) – A string that points to the dictionary of converstions between MATLAB and providers. Default DICT_MET (for metabolite).

cobra.io.mat.mat_parse_notes(target_list: List[Object], mat_struct: np.ndarray, d_replace: str = DICT_REACTION_NOTES) → None[source]

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.

Parameters
  • target_list (list[cobra.Object]) – A list of cobra objects, including metabolites, reactions or genes. The notes will be added to these lists.

  • mat_struct (np.ndarray) – A darray that includes the data imported from matlab file.

  • d_replace (str) – A string that points to the dictionary of converstions between MATLAB and notes. Default DICT_REACTION_NOTES (for reactions).

cobra.io.mat.annotations_to_mat(mat_dict: OrderedDict, annotation_list: List[Dict], d_replace: str = DICT_MET_REV) → None[source]

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.

Parameters
  • mat_dict (OrderedDict) – 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.

  • annotation_list (list[Dict]) – A list of cobra annotations, in the form of a dictionary.

  • d_replace (str) – A string that points to the dictionary of converstions between MATLAB and providers. Default DICT_MET_REV (for metabolite).

cobra.io.mat.notes_to_mat(mat_dict: OrderedDict, note_list: List[Dict], d_replace: str = DICT_MET_REV) → None[source]

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.

Parameters
  • mat_dict (OrderedDict) – 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.

  • note_list (list[Dict]) – A list of cobra annotations, in the form of a dictionary.

  • d_replace (str) – A string that points to the dictionary of converstions between MATLAB and providers. Default DICT_MET_REV (for metabolite).

cobra.io.mat.create_mat_dict(model: Model) → OrderedDict[source]

Create a dictionary mapping model attributes to arrays.

Parameters

model (cobra.Model) – The model to create dictionary for.

Returns

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

Return type

OrderedDict

cobra.io.mat.from_mat_struct(mat_struct: np.ndarray, model_id: Optional[str] = None, inf: float = np.inf) → Model[source]

Create a model from the cobratoolbox struct.

Parameters
  • mat_struct (numpy.ndarray) – The numpy.ndarray that most likely contains the model, being chosen by load_matlab_file after loading the matlab structure via scipy_io.loadmat.

  • model_id (str, optional) – 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).

  • inf (float, optional) – 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).

Returns

The model as represented in .mat file.

Return type

cobra.Model