7.1.1.1.1.1. cobramod.core.creation

Object creation

This module handles the creation of COBRApy’s objects cobra.core.metabolite.Metabolite and cobra.core.reaction.Reaction. Dictionaries containing the data of a given database are used.

7.1.1.1.1.1.1. Attributes

7.1.1.1.1.1.2. Functions

build_metabolite(identifier, formula, name, charge, ...)

Returns a basic cobra.Metabolite.

metabolite_from_string(line_string, replacement, model)

Creates a Metabolite object based on a string. Function will try to find

metabolite_from_data(data, compartment[, model])

Parses the metabolite information from a Data object and creates a

convert_string_metabolite(line, model, replacement, ...)

Transform a string into a Metabolite and returns it. This object can be

get_file_metabolites(model, filename, replacement, ...)

Return a list with Metabolites from a file. If a metabolite is found in

get_reaction(data, compartment, replacement, ...)

Creates a cobra:Reaction from reaction Data. Location of the

reaction_from_string(line_string, directory, database, ...)

Returns a custom reaction from a string that includes the information

string_to_reaction(line, model, directory, database, ...)

Returns a cobra.Reaction from a string. It can be either

get_file_reactions(model, filename, directory, ...)

Returns list with reactions from file. All reactions can be either created

create_object(identifier, directory, database, compartment)

Creates and returns a COBRApy object based on given identifier and

add_metabolites(model, obj[, directory, database])

Adds given object into the model. The options are:

find_replacements(identifier, obj_type, replace_dict, ...)

Returns either a COBRApy object if found in the model or a string with the

add_reactions(model, obj, directory[, database, ...])

Adds given object into the model. The options are:

7.1.1.1.1.1.3. Module Contents

cobramod.core.creation.debug_log
cobramod.core.creation.build_metabolite(identifier, formula, name, charge, compartment)

Returns a basic cobra.Metabolite.

LOG level: Debug

Parameters:
  • identifier (str) – Short name for Metabolite

  • formula (str) – Chemical formula

  • name (name) – Long name for Metabolite

  • charge (float) – Charge of the metabolite

  • compartment (str) – Location

Returns:

Metabolite

Return type:

cobra.core.Metabolite

cobramod.core.creation.metabolite_from_string(line_string, replacement, model)

Creates a Metabolite object based on a string. Function will try to find the metabolite in the model or cross-reference and return it if possible.

The string must follow the syntax:

formatted identifier, name , compartment, chemical_formula, molecular_charge

Parameters:
  • line_string (str) – string with information

  • replacement (dict[str, str]) – Dictionary with either the new identifier and/or the identifier of an object inside the model

  • model (Model) – Model to search for cross-references

Raises:

WrongSyntax – if format is does not follow syntax

Returns:

Metabolite

Return type:

cobra.core.Metabolite

cobramod.core.creation.metabolite_from_data(data, compartment, model=cobra_core.Model())

Parses the metabolite information from a Data object and creates a Metabolite in given compartment. This function will try to figure out if the metabolite is already in the model

Returns:

Metabolite

Parameters:
Return type:

cobra.core.Metabolite

cobramod.core.creation.convert_string_metabolite(line, model, replacement, directory, database, model_id)

Transform a string into a Metabolite and returns it. This object can be either manually-curated or from a database. If the compound if found under a different name (cross-reference), this will be returned instead.

Custom metabolite syntax:

formatted_identifier, name, compartment, chemical_formula, molecular_charge

Metabolite from database:

metabolite_identifier, compartment

Parameters:
  • line (str) – string with information of metabolite

  • model (Model) – Model to check for cross-references

  • replacement (dict[str, str]) – Dictionary with either the new identifier and/or the identifier of an object inside the model

  • directory (Path) – Path to directory where data is located

  • database (str, optional) – Name of database. Check cobramod.retrieval.available_databases for a list of names. The argument can be None ONLY for manually curated metabolites

  • model_id (str, optional) – Exclusive for BIGG. Name of model

Returns:

Metabolite

Return type:

cobra.core.Metabolite

cobramod.core.creation.get_file_metabolites(model, filename, replacement, directory, database, model_id)

Return a list with Metabolites from a file. If a metabolite is found in model under a different name (cross-reference), it will be included in the list instead.

Custom metabolite syntax:

formatted_identifier, name, compartment, chemical_formula, molecular_charge

Metabolite from database:

metabolite_identifier, compartment

Parameters:
  • model (Model) – Model to get metabolites if available

  • filename (Path) – location of the file with metabolites

  • replacement (dict[str, str]) – Dictionary with either the new identifier and/or the identifier of object inside the model.

  • directory (Path) – Path to directory where data is located.

  • database (str, optional) – Name of database. Check cobramod.retrieval.available_databases for a list of names. The argument can be None ONLY for manually curated metabolites

  • model_id (str, optional) – Bigg-specific argument. Name of the model

Raises:

FileNotFoundError – If given file is not found

Returns:

list[Metabolite]

Return type:

list[cobra.core.Metabolite]

cobramod.core.creation.get_reaction(data, compartment, replacement, show_imbalance, stop_imbalance, model)

Creates a cobra:Reaction from reaction Data. Location of the reaction can be set with the argument ‘compartment’.

This function searchs if the reaction and their metabolites are already in the model and will use them instead. Additionally, genes will be created and linked to the reaction.

Parameters:
  • data_dict (Data) – Biochemical information of the reaction

  • compartment (str) – Locations of the reaction

  • replacement (dict[str, str]) – Original identifiers to be replaced Values are the new identifiers.

  • model (Model) – Model to search for duplicates and cross-references

  • stop_imbalance (bool) – If unbalanced reaction is found, stop process

  • show_imbalance (bool) – If unbalanced reaction is found, show output

  • data (cobramod.retrieval.Data)

Returns:

Reaction

Return type:

cobra.core.Reaction

cobramod.core.creation.reaction_from_string(line_string, directory, database, stop_imbalance, show_imbalance, replacement, model_id, model=cobra_core.Model())

Returns a custom reaction from a string that includes the information of the reaction and its metabolites.

If the metabolites are not in inside the model, they will be retrieved from the specified database. This Function will also search for cross-references in the model.

Syntax:

:code:`reaction_identifier, reaction_name | coefficient metabolite <-> coefficient metabolite

Identifiers of metabolites have to end with an underscore and a compartment:

E.g `4 OXYGEN-MOLECULE_c`

Else, include a model to retrieve identifiers from it.

Reversibility will depend on the type of arrow. Options are:

`<–, –>, <=>, <->`

Parameters:
  • line_string (str) – string with information

  • directory (Path) – Path to directory where data is located

  • database (str, optional) – Name of database. Check cobramod.retrieval.available_databases for a list of names. The argument can be None ONLY for manually curated metabolites

  • stop_imbalance (bool) – If unbalanced reaction is found, stop process

  • show_imbalance (bool) – If unbalanced reaction is found, show output

  • replacement (dict) – Dictionary with either the new identifier and/or the identifier of an object inside the model

  • model_id (optional, str) – Bigg-specific argument. Name of the model

  • model (Model, optional) – A model to obtain metabolite objects from. Defaults to an empty Model

Raises:

NoDelimiter – If no delimiter “|” is found.

Returns:

Reaction

Return type:

cobra.core.Reaction

cobramod.core.creation.string_to_reaction(line, model, directory, database, stop_imbalance, show_imbalance, replacement, model_id, genome)

Returns a cobra.Reaction from a string. It can be either manually-curated, or the identifier for a reaction in a database. The function will search for cross-references of the reaction and its metabolites if the database provides that information

Syntax:

reaction_identifier, compartment

For custom reactions:

:code:`reaction_identifier, reaction_name | coefficient metabolite <-> coefficient metabolite

Identifiers of metabolites have to end with an underscore and a compartment:

E.g `4 OXYGEN-MOLECULE_c`

Else, include a model to retrieve identifiers from it.

Reversibility will depend on the type of arrow that. Options are

`<–, –>, <=>, <->`

Parameters:
  • line (str) – String with custom reaction or identifier of reaction

  • model (Model) – Model to search for cross-references

  • directory (Path) – Path to directory, where data is located

  • database (str, optional) – Name of database. Check cobramod.retrieval.available_databases for a list of names. This argument can be empty ONLY for manually-curated reactions

  • stop_imbalance (bool) – If unbalanced reaction is found, stop process

  • show_imbalance (bool) – If unbalanced reaction is found, show output

  • replacement (dict[str, str]) – Dictionary with either the new identifier and/or the identifier of an object inside the model.

  • genome (str, optional) – Exclusive for KEGG. Abbreviation for the specie involved. Genes will be obtained from this specie

  • model_id (str, optional) – Exclusive for BIGG. Retrieve object from specified model

Returns:

Reaction

Return type:

cobra.core.Reaction

cobramod.core.creation.get_file_reactions(model, filename, directory, stop_imbalance, show_imbalance, replacement, database, model_id, genome)

Returns list with reactions from file. All reactions can be either created manually or retrieved from a database. For each reactions, its always checks for mass balance.

Custom reactions:

:code:`reaction_identifier, reaction_name | coefficient metabolite <-> coefficient metabolite

Identifiers of metabolites have to end with an underscore and a compartment:

E.g `4 OXYGEN-MOLECULE_c`

Else, include a model to retrieve identifiers from it. Reversibility will depend on the type of arrow that. Options are:

`<–, –>, <=>, <->`

From database:

original_identifier, compartment

Parameters:
  • model (Model) – model to check for cross-references

  • filename (Path, str) – location of the file with reaction information

  • directory (Path) – Path to directory where data is located

  • stop_imbalance (bool) – If unbalanced reaction is found, stop process

  • show_imbalance (bool) – If unbalanced reaction is found, show output

  • replacement (dict[str, str]) – Dictionary with either the new identifier and/or the identifier of an object inside the model

  • database (str, optional) – Name of database. Check cobramod.retrieval.available_databases for a list of names. This argument can be empty ONLY for manually-curated reactions

  • model_id (str, optional) – Exclusive for BIGG. Retrieve object from specified model

  • genome (str, optional) – Exclusive for KEGG. Abbreviation for the specie involved. Genes will be obtained from this specie

Raises:

FileNotFoundError – If file does not exists

Returns:

list[Reaction]

Return type:

list[cobra.core.Reaction]

cobramod.core.creation.create_object(identifier, directory, database, compartment, replacement={}, show_imbalance=True, stop_imbalance=False, model=cobra_core.Model(), model_id=None, genome=None)

Creates and returns a COBRApy object based on given identifier and database. Identifier names will be formatted.

Hint

Hyphens will become underscores.

Parameters:
  • identifier (str) – Original identifier of the database.

  • directory (Path) – Path to directory where data is stored.

  • database (Optional[str]) – Name of database. Check cobramod.retrieval.available_databases for a list of names.

  • compartment (str) – Location of the object. In case of reaction, all metabolites will be included in the same location.

  • replacement (dict[str, str]) – Dictionary with either the new identifier and/or the identifier of an object inside the model.

  • show_imbalance (bool)

  • stop_imbalance (bool)

  • model (cobra.core.Model)

  • model_id (Optional[str])

  • genome (Optional[str])

Arguments for reactions:
  • stop_imbalance (bool) – If an unbalanced reaction is found, stop the process. Defaults to False.

  • show_imbalance (bool) – If an unbalanced reaction is found, print output. Defaults to True.

  • model (Model) – Model in which cross-references for reaction and metabolites might be found and used. If nothing is specified, it uses an empty model by default

Special arguments for databases:
  • model_id (str, optional) – Exclusive for BIGG. Retrieve object from specified model. Internal functions use the default: “universal”

  • genome (str, optional) – Exclusive for KEGG. Abbreviation for the species involved. Genes will be obtained for this species. List available at https://www.genome.jp/kegg/catalog/org_list.html

Returns:

A Reaction or Metabolite object

Return type:

cobra.core.Object

cobramod.core.creation.add_metabolites(model, obj, directory=Path.cwd().joinpath('data'), database=None, **kwargs)

Adds given object into the model. The options are:

  • Path or str: A file with the metabolites. E. g:

    Path.cwd().joinpath(“file_with_names.txt”) or “./file_with_names.txt”

  • List[Metabolites]: A list with multiple Metabolite objects.

    Custom metabolite syntax:

    formatted_identifier, name, compartment, chemical_formula, molecular_charge

    Metabolite from database:

    metabolite_identifier, compartment

  • List[str]: A list with multiple str with the mentioned syntax.

CobraMod will try to download the biochemical information and create instead custom metabolites if not found in the database. In case of only custom metabolites, it is recommended to use None for the argument database

Parameters:
  • model (Model) – Model to be expanded and searched for metabolites.

  • obj (Union[pathlib.Path, str, list[cobra.core.Object], list[str]]) – A Path; a list with either strings or Metabolite objects, or a single string. See syntax above.

  • database (str) – Name of database. Check cobramod.available_databases for a list of names. Defaults to None (This is useful for custom metabolites).

  • directory (Path) – Path to directory where the data is located. If nothing is specified, then it defaults to the directory “data” in the current working directory.

Keyword Arguments:
  • replacement (dict) – Dictionary with either the new identifier and/or the identifier of an object inside the model.

  • include_metanetx_specific_ec – Determines whether MetaNetX specific EC numbers should be taken over. These are generally not found in other databases. Furthermore, this could result in non-existing Brenda IDs being created. The default value is False.

Raises:
  • WrongSyntax (from str) – If the syntax is not followed correctly as mentioned above.

  • ValueError – If Keyword Arguments are missing.

  • FileNotFoundError (from Path) – if file does not exists

cobramod.core.creation.find_replacements(identifier, obj_type, replace_dict, model)

Returns either a COBRApy object if found in the model or a string with the new identifier, which can be used to create a new object or retrieve data. The replace_dict has two options for keys. One can be the unmodified identifier given by a database, or a formated identifier. E.g: “WATER_c”: “C00001_c” or “ACETALD-DEHYDROG-RXN” : “R00228_c”

Parameters:
  • identifier (str) – Original identifier to replace or rename.

  • obj_type (str) – Type of object to search for. Only options are “reactions” or “metabolites”.

  • replace_dict (dict) – Dictionary with either the new identifier and or the identifier of object inside the model.

  • model (Model)

Returns:

A COBRApy Object if replacement is found in the model. Otherwise nothing is returned

Return type:

Optional[cobra.core.Object]

cobramod.core.creation.add_reactions(model, obj, directory, database=None, stop_imbalance=False, show_imbalance=True, **kwargs)

Adds given object into the model. The options are:

  • Path: A file with components. E. g. :

    Path.cwd().joinpath(“file_with_names.txt”)

  • List[Reactions]: A list with regular Reactions

  • List[str]: Either the identifier with its corresponding compartment or a

string with all components. This applies for the Path option. E.g. :

reaction_identifier, compartment

For custom reactions

:code:`reaction_identifier, reaction_name | coefficient metabolite <-> coefficient metabolite

Identifiers of metabolites have to end with an underscore and a compartment:

E.g. `4 OXYGEN-MOLECULE_c`

  • List[str]: A list with multiple str with the mentioned syntax.

CobraMod will try to use the same metabolites and reactions inside of the model. If not found, then CobraMod will try to download its biochemical information or create custom objects.

Parameters:
  • model (Model) – Model to expand and search for reactions.

  • obj (Union[str, pathlib.Path, list[cobra.core.Object], list[str]]) – A Path; a list with either strings or Reaction objects, or a single string. See syntax above.

  • database (str) – Name of database. Check cobramod.available_databases for a list of names. Defaults to None (Useful for custom reactions).

  • directory (Path) – Path to directory where data is located.

  • stop_imbalance (bool) – If an unbalanced reaction is found, stop the process. Defaults to False.

  • show_imbalance (bool) – If an unbalanced reaction is found, print output. Defaults to True.

Keyword Arguments:
  • replacement (dict) – Dictionary with either the new identifier and/or the identifier of an object inside the model.

  • genome (str) – Exclusive for KEGG. Abbreviation for the species involved. Genes will be obtained for this species.

  • consider_sub_elements – Specifies whether additional cross-references should also be added to the subelements. For example, you can specify whether only the reaction or also its metabolites should be expanded. Defaults to True

  • include_metanetx_specific_ec – Determines whether MetaNetX specific EC numbers should be taken over. These are generally not found in other databases. Furthermore, this could result in non-existing Brenda IDs being created. The default value is False.

Raises:
  • WrongSyntax (from str) – If the syntax is not followed correctly as mentioned above.

  • ValueError – If Keyword Arguments are missing.

  • FileNotFoundError (from Path) – If the file does not exists.