Finding dysregulated metabolic reactions#

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.

 1import networkx as nx
 2import matplotlib.pyplot as plt
 3
 4from pymantra.network import (
 5    compute_reaction_estimates, add_reaction_estimates,
 6    MetaboliteLocalSearch
 7)
 8from pymantra.datasets import example_metabolome_enrichment_data
 9
10
11# loading example data
12metabolite_data, sample_groups, graph = example_metabolome_enrichment_data()

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. For how to use custom reaction models, see Custom Reaction Model Estimation. For including confounding variables seek Including confounding variables in dysregulation analysis.

14# compute and add reaction estimates #
15residuals = compute_reaction_estimates(graph, metabolite_data, sample_groups)
16add_reaction_estimates(graph, sample_groups, residuals)

Finally, we can initialize a MetaboliteLocalSearch object and run the local search. Afterwards, the local search object contains a number of functions to report and plot the results.

18# generate local search object and run
19# NOTE: these are randomly chose parameters
20m_lso = MetaboliteLocalSearch(graph, 10., 1e-4, 2, 10, 10, 2)
21m_lso.run_local_search(n_threads=1, min_comp_size=2)
22
23# report the results
24print(f"Local Search solution: {m_lso.solution}")
25# saving results
26m_lso.solution[0].to_json("example_metabolome_solution.json")

Full Example Code#

 1import networkx as nx
 2import matplotlib.pyplot as plt
 3
 4from pymantra.network import (
 5    compute_reaction_estimates, add_reaction_estimates,
 6    MetaboliteLocalSearch
 7)
 8from pymantra.datasets import example_metabolome_enrichment_data
 9
10
11# loading example data
12metabolite_data, sample_groups, graph = example_metabolome_enrichment_data()
13
14# compute and add reaction estimates #
15residuals = compute_reaction_estimates(graph, metabolite_data, sample_groups)
16add_reaction_estimates(graph, sample_groups, residuals)
17
18# generate local search object and run
19# NOTE: these are randomly chose parameters
20m_lso = MetaboliteLocalSearch(graph, 10., 1e-4, 2, 10, 10, 2)
21m_lso.run_local_search(n_threads=1, min_comp_size=2)
22
23# report the results
24print(f"Local Search solution: {m_lso.solution}")
25# saving results
26m_lso.solution[0].to_json("example_metabolome_solution.json")
27
28m_lso.plot_score_progression()
29plt.show()
30
31m_lso.plot_subnetwork(graph)
32plt.show()
33
34# option omitting metabolite nodes
35m_lso.plot_subnetwork(node_types=nx.get_node_attributes(graph, "node_type"))
36plt.show()