Plotting mantra results#

The plotting example uses mantra’s standard example dummy dataset, which is loaded first. Additionally, a custom color palette is defined to showcase how to set custom colors.

 1import matplotlib.pyplot as plt
 2
 3from pymantra.network import (
 4    compute_reaction_estimates, compute_multiomics_associations,)
 5from pymantra.datasets import example_multiomics_enrichment_data
 6from pymantra.plotting import (
 7    plot_directed_graph, residual_violinplot, plot_correlation_differences,
 8    plot_reaction_association
 9)
10
11
12# loading example data
13metabolite_data, microbiome_data, sample_groups, graph = \
14    example_multiomics_enrichment_data()
15
16# we set a custom color palette for the plots
17colors = {
18    "Control": plt.get_cmap("Set2")(0), "CD": plt.get_cmap("Set2")(2)}

First the full example graph is plotted using plot_directed_graph. A function for undirected plotting with the same interface is also available.

21# plotting the full example graph
22fig, ax = plt.subplots(figsize=(16, 9))
23plot_directed_graph(graph, ax=ax)
24plt.show()
25plt.close(fig)

Next the plotting function for estimated reaction values is conducted. The created plot is a violin plot indicating the residual distributions of the compared sample groups

27# compute reaction estimates
28residuals = compute_reaction_estimates(graph, metabolite_data, sample_groups)
29# plot the residuals per group as violins
30residual_violinplot(
31    residuals, sample_groups, palette=colors, plot_significant_features=False)
32plt.show()

Last, the plotting of multi-omics associations is done. After computing the associations plot_correlation_differences is used to plot a heatmap showing the differences in associations for each reaction/multi-omic feature pair. In this example set_zero is set to False, due to the example data not having many significant associations, but in practice we recommend using the default True.

Additionally, the correlations between reaction/multi-omic feature pairs with the highest absolute difference in correlations between groups can be plotted as individual scatter plots to get a more detail view on their changes.

47# compute microbiome-reaction associations
48corr_associations, pvals = compute_multiomics_associations(
49    residuals, microbiome_data, sample_groups, comparison=("0", "1"))
50# plot the difference in associations between groups in a heatmap
51# `return_differences` also returns the correlation differences instead of
52# just clust_map
53# `reorder` reorders rows and columns according to hierarchical clustering but
54# without showing the cluster tree (to show use `cluster=True`)
55diff_associations, clust_map = plot_correlation_differences(
56    corr_associations, pvals, "0", "1", reorder=True, return_differences=True,
57    set_zero=False
58)
59plt.show()
60# plot the reaction/microbe pairs with the highest difference in correlations
61# between groups as scatter plots colored by group
62plot_reaction_association(
63    residuals, microbiome_data, corr_associations, sample_groups, pal=colors)
64plt.show()

Full Example Code#

 1import matplotlib.pyplot as plt
 2
 3from pymantra.network import (
 4    compute_reaction_estimates, compute_multiomics_associations,)
 5from pymantra.datasets import example_multiomics_enrichment_data
 6from pymantra.plotting import (
 7    plot_directed_graph, residual_violinplot, plot_correlation_differences,
 8    plot_reaction_association
 9)
10
11
12# loading example data
13metabolite_data, microbiome_data, sample_groups, graph = \
14    example_multiomics_enrichment_data()
15
16# we set a custom color palette for the plots
17colors = {
18    "Control": plt.get_cmap("Set2")(0), "CD": plt.get_cmap("Set2")(2)}
19
20
21# plotting the full example graph
22fig, ax = plt.subplots(figsize=(16, 9))
23plot_directed_graph(graph, ax=ax)
24plt.show()
25plt.close(fig)
26
27# compute reaction estimates
28residuals = compute_reaction_estimates(graph, metabolite_data, sample_groups)
29# plot the residuals per group as violins
30residual_violinplot(
31    residuals, sample_groups, palette=colors, plot_significant_features=False)
32plt.show()
33
34# compute microbiome-reaction associations
35corr_associations, pvals = compute_multiomics_associations(
36    residuals, microbiome_data, sample_groups, comparison=("0", "1"))
37# plot the difference in associations between groups in a heatmap
38# `return_differences` also returns the correlation differences instead of
39# just clust_map
40# `reorder` reorders rows and columns according to hierarchical clustering but
41# without showing the cluster tree (to show use `cluster=True`)
42diff_associations, clust_map = plot_correlation_differences(
43    corr_associations, pvals, "0", "1", reorder=True, return_differences=True,
44    set_zero=False
45)
46plt.show()
47# plot the reaction/microbe pairs with the highest difference in correlations
48# between groups as scatter plots colored by group
49plot_reaction_association(
50    residuals, microbiome_data, corr_associations, sample_groups, pal=colors)
51plt.show()