cobra.io.mat

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

Module Contents

Functions

_get_id_compartment(→ str)

Extract the compartment from the id string.

_cell(→ numpy.ndarray)

Translate an iterable x into a MATLAB cell array.

_cell_to_str_list(→ List)

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

_cell_to_float_list(→ List)

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

load_matlab_model(→ cobra.core.Model)

Load a cobra model stored as a .mat file.

save_matlab_model(→ None)

Save the cobra model as a .mat file.

mat_parse_annotations(→ None)

Process mat structure annotations in place.

mat_parse_notes(→ None)

Process mat structure notes in place.

annotations_to_mat(→ None)

Process mat structure annotations in place.

notes_to_mat(→ None)

Process mat structure notes in place.

create_mat_dict(→ collections.OrderedDict)

Create a dictionary mapping model attributes to arrays.

from_mat_struct(→ cobra.core.Model)

Create a model from the cobratoolbox struct.

Attributes

scipy_sparse

logger

MET_MATLAB_TO_PROVIDERS

MET_PROVIDERS_TO_MATLAB

MET_NOTES_TO_MATLAB

MET_MATLAB_TO_NOTES

RXN_MATLAB_TO_PROVIDERS

RXN_PROVIDERS_TO_MATLAB

CONFIDENCE_STR

RXN_MATLAB_TO_NOTES

RXN_NOTES_TO_MATLAB

GENE_MATLAB_TO_PROVIDERS

GENE_PROVIDERS_TO_MATLAB

DICT_GENE

DICT_GENE_REV

DICT_MET

DICT_MET_REV

DICT_MET_NOTES

DICT_MET_NOTES_REV

DICT_REACTION

DICT_REACTION_REV

DICT_REACTION_NOTES

DICT_REACTION_NOTES_REV

DICT_REPLACE

_bracket_re

_underscore_re

_pubmed_re

_punctuation_re

_double_punctuation_re

_ec_re

_chebi_re

_sbo_re

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.CONFIDENCE_STR = 'Confidence Level'[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._sbo_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]) numpy.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: numpy.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: numpy.ndarray, empty_value: Optional[float] = None, inf_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.

  • inf_value (float, optional) – Replace infinite values with defined inf. Default None (don’t replace), will be replaced by given value if given.

Returns

A list of processed floats.

Return type

List

cobra.io.mat.load_matlab_model(infile_path: Union[str, pathlib.Path, IO], variable_name: Optional[str] = None, inf: float = np.inf) cobra.core.Model[source]

Load a cobra model stored as a .mat file.

Parameters
  • infile_path (str or Path or filehandle) – 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: cobra.core.Model, file_name: Union[str, pathlib.Path, IO], 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 or Path) – 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[cobra.core.Object], mat_struct: numpy.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[cobra.core.Object], mat_struct: numpy.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: collections.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: collections.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: cobra.core.Model) collections.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: numpy.ndarray, model_id: Optional[str] = None, inf: float = np.inf) cobra.core.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