17.1.1.3.2.1. cobra.io.dict

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

17.1.1.3.2.1.1. Module Contents

17.1.1.3.2.1.1.1. Functions

_fix_type(value: Union[str, np.float, np.bool, Set, Dict]) → Union[str, float, bool, List, OrderedDict]

Convert possible types to correct Python types.

_update_optional(cobra_object: Object, new_dict: Dict, optional_attribute_dict: Dict, ordered_keys: Sequence) → None

Update new_dict with optional attributes from cobra_object.

_metabolite_to_dict(metabolite: Metabolite) → OrderedDict

Convert a cobra Metabolite object to dictionary.

_metabolite_from_dict(metabolite: Dict) → Metabolite

Convert a dictionary to cobra Metabolite object.

_gene_to_dict(gene: Gene) → OrderedDict

Convert a cobra Gene object to dictionary.

gene_from_dict(gene: Dict) → Gene

Convert a dictionary to cobra Gene object.

_reaction_to_dict(reaction: Reaction) → OrderedDict

Convert a cobra Reaction object to a dictionary.

_reaction_from_dict(reaction: Dict, model: Model) → Reaction

Convert a dictionary to a cobra Reaction object.

model_to_dict(model: Model, sort: bool = False) → OrderedDict

Convert a cobra Model to a dictionary.

model_from_dict(obj: Dict) → Model

Build a cobra Model from a dictionary.

cobra.io.dict._REQUIRED_REACTION_ATTRIBUTES = ['id', 'name', 'metabolites', 'lower_bound', 'upper_bound', 'gene_reaction_rule'][source]
cobra.io.dict._ORDERED_OPTIONAL_REACTION_KEYS = ['objective_coefficient', 'subsystem', 'notes', 'annotation'][source]
cobra.io.dict._OPTIONAL_REACTION_ATTRIBUTES[source]
cobra.io.dict._REQUIRED_METABOLITE_ATTRIBUTES = ['id', 'name', 'compartment'][source]
cobra.io.dict._ORDERED_OPTIONAL_METABOLITE_KEYS = ['charge', 'formula', '_bound', 'notes', 'annotation'][source]
cobra.io.dict._OPTIONAL_METABOLITE_ATTRIBUTES[source]
cobra.io.dict._REQUIRED_GENE_ATTRIBUTES = ['id', 'name'][source]
cobra.io.dict._ORDERED_OPTIONAL_GENE_KEYS = ['notes', 'annotation'][source]
cobra.io.dict._OPTIONAL_GENE_ATTRIBUTES[source]
cobra.io.dict._ORDERED_OPTIONAL_MODEL_KEYS = ['name', 'compartments', 'notes', 'annotation'][source]
cobra.io.dict._OPTIONAL_MODEL_ATTRIBUTES[source]
cobra.io.dict._fix_type(value: Union[str, np.float, np.bool, Set, Dict]) → Union[str, float, bool, List, OrderedDict][source]

Convert possible types to correct Python types.

Parameters

value (str, np.float, np.bool, set, dict) – The value to fix type for.

Returns

The fixed type for the value.

Return type

str, float, bool, list, dict

cobra.io.dict._update_optional(cobra_object: Object, new_dict: Dict, optional_attribute_dict: Dict, ordered_keys: Sequence) → None[source]

Update new_dict with optional attributes from cobra_object.

Parameters
  • cobra_object (cobra.Object) – The cobra Object to update optional attributes from.

  • new_dict (dict) – The dictionary to update optional attributes for.

  • optional_attribute_dict (dict) – The dictionary to use as default value lookup store.

  • ordered_keys (list, tuple) – The keys to update values for.

Raises
  • IndexError – If key in ordered_keys is not found in optional_attribute_dict.

  • AttributeError – If key in ordered_keys is not found in cobra_object.

cobra.io.dict._metabolite_to_dict(metabolite: Metabolite) → OrderedDict[source]

Convert a cobra Metabolite object to dictionary.

Parameters

metabolite (cobra.Metabolite) – The cobra.Metabolite to convert to dictionary.

Returns

The converted dictionary object.

Return type

dict

See also

_metabolite_from_dict()

Convert a dictionary to cobra Metabolite object.

cobra.io.dict._metabolite_from_dict(metabolite: Dict) → Metabolite[source]

Convert a dictionary to cobra Metabolite object.

Parameters

metabolite (dict) – The dictionary to convert to cobra.Metabolite .

Returns

Return type

cobra.Metabolite

See also

_metabolite_to_dict()

Convert a cobra Metabolite object to dictionary.

cobra.io.dict._gene_to_dict(gene: Gene) → OrderedDict[source]

Convert a cobra Gene object to dictionary.

Parameters

gene (cobra.Gene) – The cobra.Gene to convert to dictionary.

Returns

The converted dictionary object.

Return type

dict

See also

_gene_from_dict()

Convert a dictionary to cobra Gene object.

cobra.io.dict.gene_from_dict(gene: Dict) → Gene[source]

Convert a dictionary to cobra Gene object.

Parameters

gene (dict) – The dictionary to convert to cobra.Gene .

Returns

The converted cobra.Gene object.

Return type

cobra.Gene

See also

_gene_to_dict()

Convert a cobra Gene object to a dictionary.

cobra.io.dict._reaction_to_dict(reaction: Reaction) → OrderedDict[source]

Convert a cobra Reaction object to a dictionary.

Parameters

reaction (cobra.Reaction) – The cobra.Reaction to convert to dictionary.

Returns

The converted dictionary object.

Return type

dict

See also

_reaction_from_dict()

Convert a dictionary to a cobra Reaction object.

cobra.io.dict._reaction_from_dict(reaction: Dict, model: Model) → Reaction[source]

Convert a dictionary to a cobra Reaction object.

Parameters
  • reaction (dict) – The dictionary to convert to cobra.Reaction .

  • model (cobra.Model) – The model to which the reaction should associate with.

Returns

The converted cobra.Reaction object.

Return type

cobra.Reaction

See also

_reaction_to_dict()

Convert a cobra Reaction object to a dictionary.

cobra.io.dict.model_to_dict(model: Model, sort: bool = False) → OrderedDict[source]

Convert a cobra Model to a dictionary.

Parameters
  • model (cobra.Model) – The model to reformulate as a dict.

  • sort (bool, optional) – Whether to sort the metabolites, reactions, and genes or maintain the order defined in the model (default False).

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.

Return type

OrderedDict

See also

model_from_dict()

Convert a dictionary to a cobra Model.

cobra.io.dict.model_from_dict(obj: Dict) → Model[source]

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.

Parameters

obj (dict) – 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.

Returns

The generated model.

Return type

cobra.Model

Raises

ValueError – If obj has no ‘reactions’ attribute.

See also

model_to_dict()

Convert a cobra Model to a dictionary.