Metabolites Reference

API Documentation for the metabolites submodule

Metchange Functions

Module Implementing the Metchange Algorithm

metworkpy.metabolites.metchange_functions.add_metchange_objective_constraint_(model: Model, metabolite: str, reaction_weights: Series, objective_tolerance: float = 0.05)

Add a metchange objective to a cobra Model

Parameters:
  • model (cobra.Model) – Cobra model to add the metchange objective to

  • metabolite (str) – String ID of metabolite to add metchange objective for

  • reaction_weights (pd.Series) – Weights for each reaction, representing the probability of that reaction being missing. A lower value indicates that the reaction is more likely to be present.

  • objective_tolerance (float default=0.05) – The tolerance for the objective value during the second optimization step, where the inner product of reaction weights and reaction fluxes is minimized. The flux for each metabolite will be constrained to be within objective-tolerance*objective- value of the unconstrained objective value for a metabolite.

Returns:

added_reaction – id of the added sink reaction

Return type:

str

metworkpy.metabolites.metchange_functions.metchange(model: Model, reaction_weights: dict[str, float] | Series, metabolites: Iterable[str] | None = None, objective_tolerance: float = 0.05, progress_bar: bool = False) Series

Use the Metchange algorithm to find the inconsistency scores for a set of metabolites based on reaction weights.

Parameters:
  • model (cobra.Model) – Cobra model to use for performing the metchange algorithm

  • metabolites (Iterable[str]) – Metabolites to calculate consistency scores for, if None (default) will calculate for all metabolites in the model

  • reaction_weights (dict[str, float] | pd.Series) – Weights for the reactions in the model, should correspond to the probability that reaction should not be active.

  • objective_tolerance (float) – The tolerance for the objective value during the second optimization step, where the inner product of reaction weights and reaction fluxes is minimized. The flux for each metabolite will be constrained to be within objective-tolerance*objective- value of the unconstrained objective value for a metabolite. Defaults to 0.05.

  • progress_bar (bool) – Whether a progress bar should be displayed

Returns:

Series of inconsistency scores for all the metabolites

Return type:

pd.Series

Notes

This algorithm seeks to find an inconsistency score for metabolites based on gene expression. The gene expression is represented by reaction weights, which can be calculated by combining metworkpy.utils.expression_utils.expr_to_metchange_gene_weights() and metworkpy.gpr.gpr_functions.gene_to_rxn_weights(). The algorithm calculates the inconsistency score through a two part optimization. First, for a given metabolite, the maximum metabolite production is found. Then the metabolite production is constrained to stay within objective_tolerance*maximum-production of the maximum metabolite production, and the inner product of reaction weights, and reaction fluxes is minimized. This minimum inner product is the inconsistency score.

Metabolite Networks

Module with functions for finding metabolite networks in cobra Models

metworkpy.metabolites.metabolite_network.add_metabolite_absorb_reaction_(model: Model, metabolite: str) str

Add a reaction which consumes the metabolite, and constrain it to consume all the metabolite which is generated, stopping it from being used for any other reactions

Parameters:
  • model (cobra.Model) – The model to add the absorbing reaction to

  • metabolite (str) – The metabolite id for the metabolite to add the absorbing reaction for

Returns:

reaction_id – The id of the added reaction

Return type:

str

metworkpy.metabolites.metabolite_network.add_metabolite_objective_(model: Model, metabolite: str) str

Adds a sink reaction for a metabolite, and sets it as the objective function

Parameters:
  • model (cobra.Model) – The model to update

  • metabolite (str) – The id of the metabolite to set as the objective

Returns:

reaction – id of the added reaction

Return type:

str

Note

If used within a model context everything this function alters will be reset upon leaving the context

metworkpy.metabolites.metabolite_network.eliminate_maintenance_requirements_(model: Model)

Change bounds of maintenance reactions to remove maintenance requirements

Parameters:

model (cobra.Model) – Model to eliminate maintenance requirements from

Note

When used within a model context, all changes will be reversed on leaving the model context

metworkpy.metabolites.metabolite_network.find_metabolite_consuming_network_genes(model: cobra.Model, metabolites: Iterable[str] | None = None, return_type: Literal['DataFrame', 'dict'] = 'DataFrame', reaction_proportion: float = 0.05, essential: bool = False, progress_bar: bool = False, **kwargs) pd.DataFrame[bool]

Find genes associated with reactions which consume a metabolite or its derivatives

Parameters:
  • model (cobra.Model) – Cobra model used to find which reactions are association with which metabolites

  • metabolites (iterable of str, optional) – Which metabolites to find the consuming networks for, if not provided will find the networks for all the metabolites in the model

  • return_type ({'DataFrame', 'dict'}, default='DataFrame') – How to return the networks, either a dataframe or dict (see returns for more information).

  • reaction_proportion (float) – Proportion used to judge if a reaction consumes a metabolite or its derivatives, if the maximum flux for a reaction drops below reaction_proportion * maximum flux when a metabolite is forced into a sink psuedo-reaction then that reaction will be considered to be a consumer of a metabolite.

  • essential (bool) – Whether to only include genes which are essential for the reactions that consume the metabolite or its derivatives

  • progres_bar (bool) – Whether to display a progress bar

  • kwargs – Keyword arguments passed to cobra.flux_analysis.variability.flux_variability_analysis, which is used to find changes in maximal reaction flux.

Returns:

metabolite_network – A dataframe with genes as the index, and metabolites as the columns, a True value indicates that a particular gene is associated with a reaction that consumes a metabolite or one of its derivatives

Return type:

pd.DataFrame[bool]

metworkpy.metabolites.metabolite_network.find_metabolite_consuming_network_reactions(model: cobra.Model, metabolites: Iterable[str] | None = None, return_type: Literal['DataFrame', 'dict'] = 'DataFrame', reaction_proportion: float = 0.05, check_reverse: bool = True, progress_bar: bool = False, **kwargs) pd.DataFrame[bool]

Find reactions which consume a metabolite, or its derivatives

Parameters:
  • model (cobra.Model) – Cobra Model used to find which reactions are associated with which metabolites

  • metabolites (iterable of str, optional) – Which metabolites to find the consuming networks for, if not provided will find the networks for all the metabolites in the model

  • return_type ({'DataFrame', 'dict'}, default='DataFrame') – How to return the networks, either a dataframe or dict (see returns for more information).

  • reaction_proportion (float) – Proportion used to judge if a reaction consumes a metabolite or its derivatives, if the maximum flux for a reaction drops below reaction_proportion * maximum flux when a metabolite is forced into a sink psuedo-reaction then that reaction will be considered to be a consumer of a metabolite.

  • check_reverse (bool, default=True) – Whether to check for metabolite consumption in reverse reactions, i.e. whether to check if a metabolite or its derivatives is a product of a reversible reaction

  • progress_bar (bool) – Whether to display a progress bar

  • kwargs – Keyword arguments passed to cobra.flux_analysis.variability.flux_variability_analysis, which is used to find changes in maximal reaction flux.

Returns:

metabolite_network – If return_type is ‘DataFrame’ (the default), returns a dataframe with reactions as the index and metabolites as the columns, a True value indicates that a particular reaction consumes a metabolite or one of its derivatives.

If return_type is ‘dict’, returns the network as a dict instead. The dictionary keyed by metabolite id, with values that are lists of the ids of reactions which consume a metabolite or its derivatives.

Return type:

pd.DataFrame[bool] or dict

metworkpy.metabolites.metabolite_network.find_metabolite_network_enrichment(metabolite_networks: DataFrame, target_set=collections.abc.Iterable[str], alternative: Literal['two-sided', 'less', 'greater'] = 'two-sided', fdr: bool = True, **kwargs) Series

Find the enrichment of the target set within the metabolite networks

Parameters:
  • metabolite_networks (pd.DataFrame) – DataFrame describing the metabolite networks. Columns should be the metabolite ids, and the index should include the values from the target_set

  • target_set (Iterable of str) – Set to evaluate the enrichment for, if genes the metabolite_networks should be indexed by gene, and if reactions the metabolite_networks should be indexed reaction. This will be filtered to only include elements of the index of the metabolite_networks dataframe prior to enrichment test.

  • alternative ({"two-sided", "less", "greater"}, default="greater") – Alternative hypothesis for the enrichment test

  • fdr (bool) – Whether to perform false discovery rate adjustment on the p-values

  • kwargs – Key word arguments are passed to SciPy’s false_discovery_control function if fdr is True, ignored otherwise

Returns:

enrichment_results – The p-values for the enrichment of the target set within all of the metabolites

Return type:

pd.Series

metworkpy.metabolites.metabolite_network.find_metabolite_synthesis_network_genes(model: cobra.Model, method: Literal['pfba', 'gfba', 'essential'] = 'pfba', return_type: Literal['DataFrame', 'dict'] = 'DataFrame', metabolites: Iterable[str] | None = None, pfba_proportion: float = 0.95, essential_proportion: float = 0.05, progress_bar: bool = False, essential: bool = False, **kwargs) pd.DataFrame[bool | float]

Find which genes are used to generate each metabolite in the model

Parameters:
  • model (cobra.Model) – Cobra Model used to find which genes are associated with which metabolite

  • method (Literal["pfba", "gfba", "essential"]) –

    Which method to use to associate genes with metabolites. Either

    1. ’pfba’(default):

      Use parsimonious flux analysis with the metabolite as the objective to find genes-metabolite associations. Each reaction is associated with a flux for generating a particular metabolite. This is then translated to genes by finding the maximal (in terms of absolute value) flux for a reaction associated with a particular gene.

    2. ’gfba’(default):

      Use geometric flux analysis with the metabolite as the objective to find genes-metabolite associations. Each reaction is associated with a flux for generating a particular metabolite. This is then translated to genes by finding the maximal (in terms of absolute value) flux for a reaction associated with a particular gene.

    3. ’essential’:

      Use essentiality to find gene-metabolite associations. Find which genes are essential for each metabolite.

return_type{‘DataFrame’, ‘dict’}, default=’DataFrame’

How to return the networks, either a dataframe or dict (see returns for more information).

metabolitesiterable of str, optional

Which metabolites to find the synthesis networks for, if not provided will find the networks for all the metabolites in the model

pfba_proportionfloat

Proportion to use for pfba analysis. This represents the fraction of optimum constraint applied before minimizing the sum of fluxes during pFBA.

essential_proportionfloat

Proportion to use for essentiality, gene knockouts which result in an objective function value less than essential_proportion * maximum_objective are considered essential.

progress_barbool

Whether to display a progress bar

essentialbool

When translating from reactions to genes to get the gene metabolite network for the pFBA method, whether to only include genes which are essential for a reaction in the genes associated with said reaction.

kwargsdict

Keyword arguments passed to cobra.flux_analysis.variability.find_essential_genes, cobra.flux_analysis.geometric_fba or to cobra.flux_analysis.pfba depending on the chosen method.

Returns:

If return_type is ‘DataFrame’ (the default), returns a dataframe with genes as the index and metabolites as the columns, containing either

  1. Flux values if pfba or gfba is used. For a given gene and metabolite, this represents the maximum of reaction fluxes associated with a gene, found during parsimoniou/geometric FBA required to maximally produce the metabolite.

  2. Boolean values if essentiality is used. For a given reaction and metabolite, this represents whether the reaction is essential for producing the metabolite.

If return_type is ‘dict’, returns a dict keyed by metabolite. If method is ‘essential’, then the values are lists of gene ids which are required to produce the metabolite. If method is ‘pfba’, or ‘gfba’, the the values are another dict, keyed by gene id, with values representing the maximum of reaction fluxes associated with a gene, found during parsimonious/geometric FBA required to maximally produce the metabolite.

with values corresponding to the flux associated with that reaction in the parsimonious/geometric FBA solution when optimizing for the production of the metabolite.

Return type:

pd.DataFrame[bool|float] or dict

Notes

For converting from the reaction fluxes to gene fluxes, the gene is assigned a value corresponding to the maximum magnitude flux the gene is associated with (but the value assigned keeps the sign). For example, if a gene was associated with reactions which had parsimonious flux values of -10, and 1 the gene would be assigned a value of -10.

See also

find_metabolite_synthesis_network_reactions

Equivalent method with reactions

metworkpy.metabolites.metabolite_network.find_metabolite_synthesis_network_reactions(model: cobra.Model, method: Literal['pfba', 'gfba', 'essential'] = 'pfba', return_type: Literal['dict', 'DataFrame'] = 'DataFrame', metabolites: Iterable[str] | None = None, pfba_proportion: float = 0.95, essential_proportion: float = 0.05, progress_bar: bool = False, **kwargs) pd.DataFrame[bool | float] | dict[str, list[str]] | dict[str, dict[str, float]]

Find which reactions are used to generate each metabolite in the model

Parameters:
  • model (cobra.Model) – Cobra Model used to find which reactions are associated with which metabolite

  • method (Literal["pfba", "essential"]) –

    Which method to use to associate reactions with metabolites. Either

    1. ’pfba’(default):

      Use parsimonious flux analysis with the metabolite as the objective to find reaction-metabolite associations. Each reaction is associated with a flux for generating a particular metabolite.

    2. ’gfba’(default):

      Use geometric flux analysis with the metabolite as the objective to find reaction-metabolite associations. Each reaction is associated with a flux for generating a particular metabolite.

    3. ’essential’:

      Use essentiality to find reaction-metabolite associations. Find which reactions are essential for each metabolite.

  • return_type ({'DataFrame', 'dict'}, default='DataFrame') – How to return the networks, either a dataframe or dict (see returns for more information).

  • metabolites (iterable of str, optional) – Which metabolites to find the synthesis networks for, if not provided will find the networks for all the metabolites in the model

  • pfba_proportion (float) – Proportion to use for pfba analysis. This represents the fraction of optimum constraint applied before minimizing the sum of fluxes during pFBA.

  • essential_proportion (float) – Proportion to use for essentiality, gene knockouts which result in an objective function value less than essential_proportion * maximum_objective are considered essential.

  • progress_bar (bool) – Whether a progress bar should be displayed

  • **kwargs (dict) – Keyword arguments passed to cobra.flux_analysis.variability.find_essential_genes, cobra.flux_analysis.geometric_fba, or to cobra.flux_analysis.pfba depending on the chosen method.

Returns:

If return_type is ‘DataFrame’ (the default), returns a dataframe with reactions as the index and metabolites as the columns, containing either

  1. Flux values if pfba or gfba are used. For a given reaction and metabolite, this represents the reaction flux found during pFBA required to maximally produce the metabolite.

  2. Boolean values if essentiality is used. For a given reaction and metabolite, this represents whether the reaction is essential for producing the metabolite.

If return_type is ‘dict’, returns a dict keyed by metabolite. If method is ‘essential’, then the values are lists of reaction ids which are required to produce the metabolite. If method is ‘pfba’, or ‘gfba’, the the values are another dict, keyed by reaction id, with values corresponding to the flux associated with that reaction in the parsimonious/geometric FBA solution when optimizing for the production of the metabolite.

Return type:

pd.DataFrame[bool|float] or dict

See also

find_metabolite_synthesis_network_genes

Equivalent method with genes