5. cobramod

CobraMod Package

It is a helpbox and extension package based on COBRApy . This package facilitates the retrieval of biochemical data from multiple databases and help users extend metabolic networks.

CobraMod ensures an easy-friendly API, customization and a record of all the changes made to metabolic models.

Important functions from CobraMod that can be imported directly:

  • get_data: Store and retrieve data from given database.

  • create_object: Create a corresponding COBRApy object from given identifier.

  • add_metabolites: Add metabolites from different objects

  • add_reactions: Add reactions from different objects

  • add_pathway: Extend given pathway into given model.

  • test_non_zero_flux: Test given reaction to check for a feasible solution.

  • translate: Check for cross-references.

The exclusive class cobramod.pathway.Pathway() is an extension of cobra.core.group.Group. CobraMod uses Escher to visualize the pathway and fluxes of that specific class. Some new methods added:

  • visualize: Return a Builder for the representation of the pathway.

  • solution: Filters solution and returns fluxes of only members of the class.

By default, CobraMod creates the file “debug.log”, which displays the changes, which occurred when running a script. In order to modify the logging, import “debug_log” from “cobramod.debug”. The default logging level is defined as INFO. Read the documentation of logging for more information.

For a list of databases, load variable cobramod.available_databases

5.3. Package Contents

5.3.1. Classes

Pathway

A Sub-class from the original COBRApy cobra.Group, which inherits

5.3.2. Functions

create_object(identifier: str, directory: pathlib.Path, database: str, compartment: str, replacement: dict = {}, show_imbalance: bool = True, stop_imbalance: bool = False, model: cobra.Model = Model(0), model_id: str = 'universal', genome: str = None) → Union[cobra.Reaction, cobra.Metabolite, dict]

Creates and returns a COBRApy object based on given identifier and

add_metabolites(model: cobra.Model, obj: Any, database=None, **kwargs)

Adds given object into the model. The options are:

add_reactions(model: cobra.Model, obj: Any, database=None, stop_imbalance: bool = False, show_imbalance: bool = True, **kwargs)

Adds given object into the model. The options are:

add_pathway(model: cobra.Model, pathway: Union[list, str], directory: pathlib.Path, database: str, compartment: str, group: Optional[str] = None, avoid_list: List[str] = [], replacement: dict = {}, ignore_list: List[str] = [], filename: pathlib.Path = None, stop_imbalance: bool = False, show_imbalance: bool = True, model_id: str = 'universal', genome: Optional[str] = None)

Adds a pathway from the given database into a model. The argument ‘pathway’

test_non_zero_flux(model: cobra.Model, reaction: str, times: int = 0, ignore_list: List[str] = [])

Verifies that a simple FBA can be performed. A demand reaction is created

model_convert(model: cobra.core.model.Model)

Converts all Group objects in the given model to proper cobramod

get_data(directory: pathlib.Path, identifier: str, database: str, debug_level: int = 20, **kwargs) → dict

Retrieves and transforms the data into a dictionary for the given

translate(directory: pathlib.Path, target: str, database: str) → str

Return the identifier of crossref for the given target. It can be a

5.3.3. Attributes

available_databases

cobramod.create_object(identifier: str, directory: pathlib.Path, database: str, compartment: str, replacement: dict = {}, show_imbalance: bool = True, stop_imbalance: bool = False, model: cobra.Model = Model(0), model_id: str = 'universal', genome: str = None) Union[cobra.Reaction, cobra.Metabolite, dict]

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

Hint

Hyphens will become underscores. Double hyphens become single underscores.

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

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

  • database (str) – Name of database. Check cobramod.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, optional) – original identifiers to be replaced. Values are the new identifiers. Defaults to {}. Does not apply to pathways.

Arguments for reactions

stop_imbalance (bool, optional): If an unbalanced reaction is found,

stop the process. Defaults to False.

show_imbalance (bool, optional): If an unbalanced reaction is found,

print output. Defaults to True.

model (Model, optional): Model to add search for translated metabolites

or reactions. Defaults to an empty model.

Special arguments for databases

model_id (str, optional): Exclusive for BIGG. Retrieve object from

specified model. Pathways are not available. Defaults to: “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; or the

information for a pathway.

Return type

Union[Reaction, Metabolite]

cobramod.add_metabolites(model: cobra.Model, obj: Any, database=None, **kwargs)

Adds given object into the model. The options are:

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

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

  • Metabolite: A single Metabolite.

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

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

string with the whole attributes. This applies for the Path option. E.g:

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.

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

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

Keyword Arguments

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

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.add_reactions(model: cobra.Model, obj: Any, database=None, stop_imbalance: bool = False, show_imbalance: bool = 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

  • 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.

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

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

  • 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
  • directory (Path) – Path to directory where data is located.

  • replacement (dict) – Original identifiers to be replaced. Values are the new identifiers. Defaults to {}.

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

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.

cobramod.add_pathway(model: cobra.Model, pathway: Union[list, str], directory: pathlib.Path, database: str, compartment: str, group: Optional[str] = None, avoid_list: List[str] = [], replacement: dict = {}, ignore_list: List[str] = [], filename: pathlib.Path = None, stop_imbalance: bool = False, show_imbalance: bool = True, model_id: str = 'universal', genome: Optional[str] = None)

Adds a pathway from the given database into a model. The argument ‘pathway’ can be a list of reactions identifiers or explicitly a pathway identifier. A group of reactions will be included in a custom group. The data will be downloaded and structured according to the database.

Parameters
  • model (Model) – Model to expand.

  • pathway (list, str) – Sequence of reaction identifiers or a single identifier for a pathway. Examples: [“RXN-2206”, “RXN-207”] or “PWY-886”

  • directory (Path) – Path for the directory to stored and retrieve data.

  • database (str) – Name of the database. Check cobramod.available_databases for a list of names.

  • compartment – Location of the reactions.

  • group (str, optional) – Common cobramod.pathway.Pathway identifier. This will overwrite the name of the pathway.

Arguments for complex pathways: avoid_list (list, optional): A sequence of reactions identifiers to avoid

adding to the model. This is useful for long pathways, where X reactions need to be excluded.

replacement (dict, optional): Original identifiers to be replaced.

Values are the new identifiers.

ignore_list (list): A sequence of formatted metabolites to be ignored when testing, and/or reactions that should be added but not tested.

This is useful for long cyclical pathways.

Arguments for summary:
filename (Path, optional): Location for the summary. Defaults to

“summary” in the current working directory. The file format is defined by the suffix. The suffixes ‘.txt’, ‘.csv’ and ‘.xlsx’ can be used. If the filename is set to None, no summary will be created.

Arguments for utilities:
stop_imbalance (bool, optional): If an unbalanced reaction is found,

stop the process. Defaults to False.

show_imbalance (bool, optional): If an unbalanced reaction is found,

print output. Defaults to True.

model_id (str, optional): Exclusive for BIGG. Retrieve object from

specified model. Pathways are not available. Defaults to: “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

cobramod.test_non_zero_flux(model: cobra.Model, reaction: str, times: int = 0, ignore_list: List[str] = [])

Verifies that a simple FBA can be performed. A demand reaction is created with a minimum flux based on the COBRApy Configuration object. It will use its variable ‘tolerance’ multiplied by 10. The function is recursive and checks if sink reactions are sufficient or exceeded. It generates a demand reaction for the reaction used for testing and removes it if necessary.

Parameters
  • model (Model) – Model in which the reaction is present.

  • reaction (str) – Reaction identifier in model to test.

  • times (int, optional) – Track of recursions. Defaults to 0.

  • ignore_list (list, optional) – A sequence of formatted metabolites to be

  • [] (ignored when testing new reactions. Defaults to) –

Raises

NotInRangeError – If the solution is infeasible after many recursions. Depends on the number of metabolites in the reaction.

class cobramod.Pathway(id: str, name: str = '', members: cobra.core.dictlist.DictList = DictList(), kind: str = None)

Bases: cobra.core.group.Group

A Sub-class from the original COBRApy cobra.Group, which inherits all attributes and adds the method solution, to get a Solution for the members of this Class.

Attributes
vertical (bool, optional):

Variable that determines whether the display should be vertical or horizontal using Escher. Defaults to False.

color_negative (str or list of int, optional) :

The color to use as the endpoint for the negative fluxes during the creation of the color gradient. All colors of the CSS standard can be used here or their RGB representation.

color_positive (str or list of int, optional) :

The color to use as the endpoint for the positive fluxes during the creation of the color gradient. All colors of the CSS standard can be used here or their RGB representation.

color_min_max (list of float, optional) :

The maximum and minimum to be taken into account when creating the color gradient. This creates these two values artificially to allow the creation of a data-independent color gradient. Fluxes larger or smaller are ignored accordingly.

color_quantile (bool, optional) :

Attribute that defines whether the color gradient should be determined through quantiles or equally distributed between the maximum and the minimum. Defaults to False which means that the gradations are evenly distributed.

color_n_steps (int, optional) :

The number of steps used when creating the color gradient. Uses the number of fluxes by default. The default value is None.

color_max_steps (int, optional) :

The maximum number of steps to use when creating the color gradient. Default value is 100.

See also

Color names according to the css standard: https://www.w3schools.com/cssref/css_colors.asp

add_members(self, new_members: List[cobra.core.reaction.Reaction])

Add given list of cobra.core.reaction.Reaction into the Pathway.

Parameters

new_members (list) – List of Reactions to add to the class.

Raises

TypeError – If not all members are proper Reaction objects.

solution(self, solution: cobra.core.solution.Solution) cobra.core.solution.Solution

Returns a cobra.Solution with only the members of the pathway.

Parameters

solution (Solution) – Original COBRApy cobra.Solution to filter.

Returns

Filtered solution containing only members of the Pathway

class.

Return type

Solution

modify_graph(self, reaction: str, next_reaction: Union[str, None])

Modifies the order of the graph. This is useful when merging multiple pathways or joining reactions. In the graph, the selected reaction will be forced to show “next_reaction” as its successor.

Parameters
  • reaction (str) – Identifier of the reaction to modify in the graph.

  • next_reaction (str, None) – Identifier of the next reaction. This reaction will take place after “reaction”. If None is passed, then “reaction” will not have successors.

Raises
  • GraphKeyError – If the reaction or the next_reaction does not appear

  • in the graph of the pathway.

visualize(self, solution_fluxes: Union[cobra.core.solution.Solution, Dict[str, float]] = None, filename: pathlib.Path = None) escher.Builder

Returns a escher.Builder, which can be used to create visual representations of the pathway.

Parameters
  • solution_fluxes (Solution, dict) – Series or Dictionary with fluxes. The values will be then showed in the Builder. Defaults to None.

  • filename (Path) – Path for the HTML. Defaults to “pathway.html” in the current working directory.

cobramod.model_convert(model: cobra.core.model.Model)

Converts all Group objects in the given model to proper cobramod cobramod.pathway.Pathway

cobramod.get_data(directory: pathlib.Path, identifier: str, database: str, debug_level: int = 20, **kwargs) dict

Retrieves and transforms the data into a dictionary for the given identifier from a specific database.

Parameters
  • directory (Path) – Directory to store and retrieve local data.

  • identifier (str) – Original identifier.

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

  • debug_level (int, optional) – Level of debugging. Read package logging for more info. Defaults to 20.

Keyword Arguments
  • model_id – Exclusive for BIGG. Original identifier of the model to be searched. Some examples: “e_coli_core”, “universal”.

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

Returns

Relevant data for the given identifier.

Return type

dict

cobramod.translate(directory: pathlib.Path, target: str, database: str) str

Return the identifier of crossref for the given target. It can be a metabolite or a Reaction.

Parameters
  • directory (Path) – Path of stored data.

  • target (str) – Identifier to search for.

  • database (str) – Pattern for the name of the cross-reference, e.g CAS, BIGG.

Returns

str: Corresponding identifier for cross-reference.

Raises

PatternNotFound – If the target cannot be properly identified.

cobramod.available_databases