cobra.io.mat
¶
Provide functions for I/O in MATLAB (.mat) format.
Module Contents¶
Functions¶
|
Extract the compartment from the id string. |
|
Translate an iterable x into a MATLAB cell array. |
|
Turn an ndarray (cell) to a list of strings. |
|
Turn an ndarray (cell) to a list of floats. |
|
Load a cobra model stored as a .mat file. |
|
Save the cobra model as a .mat file. |
|
Process mat structure annotations in place. |
|
Process mat structure notes in place. |
|
Process mat structure annotations in place. |
|
Process mat structure notes in place. |
|
Create a dictionary mapping model attributes to arrays. |
|
Create a model from the cobratoolbox struct. |
Attributes¶
- cobra.io.mat._get_id_compartment(_id: str) str [source]¶
Extract the compartment from the id string.
- 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
- 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
- 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
- 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