Including confounding variables in dysregulation analysis#

We first load some example data from the packages. We simply load a .csv file with metabolite measurements and a group annotation file, together with a pre-computed metabolite-reaction graph. For more details on how to generate such a graph and what its structure, looks like see Network Generation and Using custom networks. The metabolite data should contain the samples in rows. In addition to the example data, we also generate some confounding data.

 1import pandas as pd
 2
 3from pymantra.network import compute_reaction_estimates, add_reaction_estimates
 4from pymantra.datasets import example_metabolome_enrichment_data
 5
 6
 7# loading example data and graph
 8metabolite_data, sample_groups, graph = example_metabolome_enrichment_data()
 9control_mask = sample_groups == '0'
10
11# define some confounding variables
12confounders = pd.DataFrame(
13    {
14        "site": [],
15        "gender": [],
16        "age": []
17    }
18)

Once all data is loaded, we compute the linear models and the difference in residuals between samples groups by calling compute_reaction_estimates and add them to the graph in the required way using add_reaction_estimates. The confounder correction is happening inside compute_reaction_estimates. In this case we are using the following formula: products ~ substrates + site + gender + (1|age)

20# compute and add reaction estimates with confounding factors
21residuals = compute_reaction_estimates(
22    graph, metabolite_data, sample_groups, covariates=confounders,
23    random_effects=[""]
24)
25add_reaction_estimates(graph, sample_groups, residuals)

After this step the enrichment can be performed the same way as demonstrated in Finding dysregulated metabolic reactions. Furthermore, the multi-omics association function compute_multiomics_associations (see Reaction-based Metabolome-Microbiome Integration for reference) also allows for correction in the same way as compute_reaction_estimates.

Full Example Code#

 1import pandas as pd
 2
 3from pymantra.network import compute_reaction_estimates, add_reaction_estimates
 4from pymantra.datasets import example_metabolome_enrichment_data
 5
 6
 7# loading example data and graph
 8metabolite_data, sample_groups, graph = example_metabolome_enrichment_data()
 9control_mask = sample_groups == '0'
10
11# define some confounding variables
12confounders = pd.DataFrame(
13    {
14        "site": [],
15        "gender": [],
16        "age": []
17    }
18)
19
20# compute and add reaction estimates with confounding factors
21residuals = compute_reaction_estimates(
22    graph, metabolite_data, sample_groups, covariates=confounders,
23    random_effects=[""]
24)
25add_reaction_estimates(graph, sample_groups, residuals)