MetaboDynamics 1.1.4
The MetaboDynamics vignette explains the package workflow and interpretation of results with a SummarizedExperiment object as input. In this vignette I will show the usage of MetaboDynamics with a data frame as input.
library(MetaboDynamics)
library(SummarizedExperiment) # storing and manipulating simulated metabolomics data
library(ggplot2) # visualization
library(dplyr) # data handling
library(tidyr) # data handling
The MetaboDynamics package also includes a simulated example data set in a data frame format. Please note, that although both data sets were simulated with the same code different seeds were used so that the results can deviate from each other. To load the data set in data frame format, execute the following code:
data("longitudinalMetabolomics_df", package = "MetaboDynamics")
To keep run time of this vignette short we will execute the workflow on a subset of five metabolites across all conditions.
# take a sample of five metabolites and subset data
metabolites <- sample(x = unique(longitudinalMetabolomics_df$metabolite),size = 5)
data <- longitudinalMetabolomics_df%>%filter(metabolite%in%metabolites)
head(data)
## # A tibble: 6 × 8
## # Groups: metabolite, condition [1]
## metabolite condition time replicate measurement log_m m_scaled KEGG
## <chr> <chr> <int> <int> <dbl> <dbl> <dbl> <chr>
## 1 Pyruvate A 1 1 40.5 1.61 -0.540 C00022
## 2 Pyruvate A 1 2 6.20 0.793 -2.03 C00022
## 3 Pyruvate A 1 3 30.7 1.49 -0.761 C00022
## 4 Pyruvate A 2 1 282. 2.45 1.00 C00022
## 5 Pyruvate A 2 2 178. 2.25 0.639 C00022
## 6 Pyruvate A 2 3 21.5 1.33 -1.04 C00022
The measured metabolites are stored in column “metabolite”, time points are specified in column “time”, and column “condition” specifies the experimental condition.
The required log-transformed and scaled (per metabolite and condition to a mean of 0 and sd of 1) metabolite abundances are stored in column “m_scaled”. The function fit_dynamics_model() allows to specify other column names for metabolite, time points, condition and scaled measurement.
First we will fit the dynamics model and extract the diagnostics:
# fit dynamics model
fits <- # when using a data frame as input fits have to stored in a separate object
fit_dynamics_model(
data = data,
metabolite = "metabolite", # in which column are metabolite names stored?
time = "time", # in which column are categorical time points stored?
condition = "condition", # in which are categorical experimental conditions stored?,
scaled_measurement = "m_scaled", # in which column are scaled measurments stored?
max_treedepth = 10,
adapt_delta = 0.95, # default 0.95
iter = 2000,
warmup = 2000/4, # default is 1/4 of iterations
chains = 1, # only set to 1 in vignette, recommended default is 4!
cores = 1 # only set to 1 in vignette, can be same as chains if machine allows for parallelization
)
## Is your data normalized and standardized?
## We recommend normalization by log-transformation.
## Scaling and centering (mean=0, sd=1) should be metabolite and condition specific.
##
## SAMPLING FOR MODEL 'm_ANOVA_partial_pooling' NOW (CHAIN 1).
## Chain 1:
## Chain 1: Gradient evaluation took 2.7e-05 seconds
## Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 0.27 seconds.
## Chain 1: Adjust your expectations accordingly!
## Chain 1:
## Chain 1:
## Chain 1: Iteration: 1 / 2000 [ 0%] (Warmup)
## Chain 1: Iteration: 200 / 2000 [ 10%] (Warmup)
## Chain 1: Iteration: 400 / 2000 [ 20%] (Warmup)
## Chain 1: Iteration: 501 / 2000 [ 25%] (Sampling)
## Chain 1: Iteration: 700 / 2000 [ 35%] (Sampling)
## Chain 1: Iteration: 900 / 2000 [ 45%] (Sampling)
## Chain 1: Iteration: 1100 / 2000 [ 55%] (Sampling)
## Chain 1: Iteration: 1300 / 2000 [ 65%] (Sampling)
## Chain 1: Iteration: 1500 / 2000 [ 75%] (Sampling)
## Chain 1: Iteration: 1700 / 2000 [ 85%] (Sampling)
## Chain 1: Iteration: 1900 / 2000 [ 95%] (Sampling)
## Chain 1: Iteration: 2000 / 2000 [100%] (Sampling)
## Chain 1:
## Chain 1: Elapsed Time: 0.24 seconds (Warm-up)
## Chain 1: 0.578 seconds (Sampling)
## Chain 1: 0.818 seconds (Total)
## Chain 1:
##
## SAMPLING FOR MODEL 'm_ANOVA_partial_pooling' NOW (CHAIN 1).
## Chain 1:
## Chain 1: Gradient evaluation took 2.8e-05 seconds
## Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 0.28 seconds.
## Chain 1: Adjust your expectations accordingly!
## Chain 1:
## Chain 1:
## Chain 1: Iteration: 1 / 2000 [ 0%] (Warmup)
## Chain 1: Iteration: 200 / 2000 [ 10%] (Warmup)
## Chain 1: Iteration: 400 / 2000 [ 20%] (Warmup)
## Chain 1: Iteration: 501 / 2000 [ 25%] (Sampling)
## Chain 1: Iteration: 700 / 2000 [ 35%] (Sampling)
## Chain 1: Iteration: 900 / 2000 [ 45%] (Sampling)
## Chain 1: Iteration: 1100 / 2000 [ 55%] (Sampling)
## Chain 1: Iteration: 1300 / 2000 [ 65%] (Sampling)
## Chain 1: Iteration: 1500 / 2000 [ 75%] (Sampling)
## Chain 1: Iteration: 1700 / 2000 [ 85%] (Sampling)
## Chain 1: Iteration: 1900 / 2000 [ 95%] (Sampling)
## Chain 1: Iteration: 2000 / 2000 [100%] (Sampling)
## Chain 1:
## Chain 1: Elapsed Time: 0.3 seconds (Warm-up)
## Chain 1: 0.638 seconds (Sampling)
## Chain 1: 0.938 seconds (Total)
## Chain 1:
##
## SAMPLING FOR MODEL 'm_ANOVA_partial_pooling' NOW (CHAIN 1).
## Chain 1:
## Chain 1: Gradient evaluation took 2.8e-05 seconds
## Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 0.28 seconds.
## Chain 1: Adjust your expectations accordingly!
## Chain 1:
## Chain 1:
## Chain 1: Iteration: 1 / 2000 [ 0%] (Warmup)
## Chain 1: Iteration: 200 / 2000 [ 10%] (Warmup)
## Chain 1: Iteration: 400 / 2000 [ 20%] (Warmup)
## Chain 1: Iteration: 501 / 2000 [ 25%] (Sampling)
## Chain 1: Iteration: 700 / 2000 [ 35%] (Sampling)
## Chain 1: Iteration: 900 / 2000 [ 45%] (Sampling)
## Chain 1: Iteration: 1100 / 2000 [ 55%] (Sampling)
## Chain 1: Iteration: 1300 / 2000 [ 65%] (Sampling)
## Chain 1: Iteration: 1500 / 2000 [ 75%] (Sampling)
## Chain 1: Iteration: 1700 / 2000 [ 85%] (Sampling)
## Chain 1: Iteration: 1900 / 2000 [ 95%] (Sampling)
## Chain 1: Iteration: 2000 / 2000 [100%] (Sampling)
## Chain 1:
## Chain 1: Elapsed Time: 0.274 seconds (Warm-up)
## Chain 1: 0.648 seconds (Sampling)
## Chain 1: 0.922 seconds (Total)
## Chain 1:
This returns a list of model fits that are named by the experimental condition (in the case of the simulated data set “A”,“B”,“C”).
# Extract diagnostics
diagnostics <- # when using a data frame as input diagnostics have to be stored in a separate object
diagnostics_dynamics(
data = data, # data frame that was used to fit the dynamics model,
fits = fits, # list of fits from dynamics model, result of fit_dynamics_mode function
iter = 2000, # how many iterations were used to fit the dynamics model
chains = 1, # how many chains were used to fit the dynamics model
)
That returns a list with elements [[“model_diagnostics”]] which holds a summary of the model diagnostics and the extracted posterior estimates for all conditions.
To visualize the diagnostics the following code can be used:
plot_diagnostics(
data = data, # data frame used to fit the dynamics model
diagnostics = diagnostics[["model_diagnostics"]] # summary of diagnostics
)
## $divergences
##
## $max_treedepth
##
## $Rhat
##
## $n_eff
In this case the diagnostics are all indicating a sane model fit: The model fitting did not result in
divergent transitions, the maximum tree depth was not exceeded, Rhat values were
below 1.01 and number of effective samples exceeded 100.
To extract the estimates one has to specify again the data, list of fits and number of iterations and chains used to fit the model. Additionally is is possible to specify the columns in which metabolite names, experimental conditions and time points are stored. This is equivalent to the fit_dynamics_model function.
Additionally one can specify how many samples should be drawn from the posterior to for example test clustering performance.
estimates <- # estimates have to be stored in a separate object when using data frames
estimates_dynamics(
data = data,
fits = fits,
iter = 2000,
warmup = 500,
chains = 1,
condition = "condition",
metabolite = "metabolite",
time = "time",
samples = 1 # 1 sample from posterior taken
)
This returns a list containing the estimates for every experimental condition. The estimates can be visualized in two ways by the following code:
# only visualize differences between time points
plot_estimates(data = data,
estimates = estimates,
delta_t = TRUE, # choose to visualize differences between time points
dynamics = FALSE)
## $plot_timepoint_differences
## $plot_timepoint_differences$`1_A`
##
## $plot_timepoint_differences$`2_A`
##
## $plot_timepoint_differences$`3_A`
##
## $plot_timepoint_differences$`1_B`
##
## $plot_timepoint_differences$`2_B`
##
## $plot_timepoint_differences$`3_B`
##
## $plot_timepoint_differences$`1_C`
##
## $plot_timepoint_differences$`2_C`
##
## $plot_timepoint_differences$`3_C`
# only visualize dynamics
plot_estimates(data = data,
estimates = estimates,
delta_t = FALSE,
dynamics = TRUE) # choose to visualize the dynamics
## $plot_dynamics
To cluster the dynamics we need either the estimates from the dynamics model (stored in object “estimates”) or a data frame in which the means of measurements are stored in a column named “mu_mean”, categorical time points are stored in ascending order in column “time.ID” and experimental conditions in a column named “condition”.
The following chunk of code shows the needed data frame format for the clustering function:
head(estimates)
## $A
## metabolite.ID metabolite KEGG time.ID condition mu_mean
## 1 1 Acetoacetate C00164 1 A 0.27416419
## 2 1 Acetoacetate C00164 2 A 0.36012085
## 3 1 Acetoacetate C00164 3 A 0.59272506
## 4 1 Acetoacetate C00164 4 A -0.49145983
## 5 2 Adenine C00147 1 A -0.99304403
## 6 2 Adenine C00147 2 A 0.58704353
## 7 2 Adenine C00147 3 A 0.47420255
## 8 2 Adenine C00147 4 A -0.27571132
## 9 3 DL-Glycerol 1-phosphate C00093 1 A 1.08484297
## 10 3 DL-Glycerol 1-phosphate C00093 2 A 0.20797924
## 11 3 DL-Glycerol 1-phosphate C00093 3 A -0.46768622
## 12 3 DL-Glycerol 1-phosphate C00093 4 A 0.08461766
## 13 4 NADP+ C00006 1 A -0.11659716
## 14 4 NADP+ C00006 2 A -0.95029558
## 15 4 NADP+ C00006 3 A 0.36167650
## 16 4 NADP+ C00006 4 A -0.57259801
## 17 5 Pyruvate C00022 1 A -0.87449147
## 18 5 Pyruvate C00022 2 A -0.27452618
## 19 5 Pyruvate C00022 3 A 0.31195338
## 20 5 Pyruvate C00022 4 A 0.50285731
## mu_lower mu_higher sigma_mean sigma_lower sigma_higher lambda_mean
## 1 -1.5243389 2.0712255 1.7716990 0.7669945 4.309258 0.8210774
## 2 -0.8964610 1.8771529 1.0277149 0.3539651 2.821178 0.8210774
## 3 -1.2958661 2.3481991 1.7150288 0.7413986 4.103281 0.8210774
## 4 -1.5370490 0.6982306 0.8023766 0.2607716 2.274065 0.8210774
## 5 -2.1746200 0.5529755 1.1488984 0.4366878 3.293596 0.7707166
## 6 -1.1901090 2.0306246 1.4487119 0.5645576 3.864948 0.7707166
## 7 -0.5459315 1.4309311 0.7466413 0.2485359 2.381138 0.7707166
## 8 -1.9139786 1.6670622 1.3752952 0.5227019 3.511965 0.7707166
## 9 -0.1203393 2.0858857 0.9510381 0.3764704 2.444129 0.7539087
## 10 -1.3348376 1.7906468 1.4001820 0.5500072 3.265107 0.7539087
## 11 -1.2376389 0.3586892 0.6454639 0.2102049 1.820421 0.7539087
## 12 -1.5120493 1.6242254 1.4483629 0.6030757 3.384559 0.7539087
## 13 -0.6627794 0.3191730 0.3511161 0.0886318 1.271129 0.9427154
## 14 -1.7057356 -0.3493602 0.4756288 0.1349378 1.572410 0.9427154
## 15 -1.1391659 1.7485209 1.2994230 0.5427537 2.986090 0.9427154
## 16 -1.2219668 0.1305514 0.4290290 0.1191781 1.438038 0.9427154
## 17 -2.3492417 1.0912759 1.5249380 0.6172343 3.735400 0.8060602
## 18 -1.7956059 1.4169546 1.5127878 0.5928027 3.618117 0.8060602
## 19 -0.9623762 1.7284941 1.1885916 0.4981521 3.008680 0.8060602
## 20 -0.2704111 1.2618779 0.5699679 0.1798018 1.710456 0.8060602
## lambda_lower lambda_higher delta_mu_mean delta_mu_lower delta_mu_higher
## 1 0.2210530 1.833531 0.3128793 -2.41730046 2.6375495
## 2 0.2210530 1.833531 -1.0547298 -2.78911637 0.9163266
## 3 0.2210530 1.833531 -0.1049118 -1.13412316 0.9447727
## 4 0.2210530 1.833531 NA NA NA
## 5 0.2378274 1.646601 0.1140817 -1.65297965 1.6925356
## 6 0.2378274 1.646601 -0.3895849 -2.16075116 1.3583653
## 7 0.2378274 1.646601 -0.9591091 -3.08800476 1.3585478
## 8 0.2378274 1.646601 NA NA NA
## 9 0.2197033 1.710721 -0.8684364 -3.12102771 1.9154112
## 10 0.2197033 1.710721 0.1591142 -1.83685404 1.8609221
## 11 0.2197033 1.710721 -0.1579290 -1.74380869 1.6115195
## 12 0.2197033 1.710721 NA NA NA
## 13 0.2814687 2.082598 1.5763028 -0.01705008 2.9513762
## 14 0.2814687 2.082598 -2.0351385 -3.40579700 -0.7142624
## 15 0.2814687 2.082598 1.2622490 -0.17966529 2.9545209
## 16 0.2814687 2.082598 NA NA NA
## 17 0.2427680 1.687824 1.2010233 -0.98149895 3.3596152
## 18 0.2427680 1.687824 0.1536973 -1.98924244 2.1643501
## 19 0.2427680 1.687824 0.1411808 -1.45202456 1.8140226
## 20 0.2427680 1.687824 NA NA NA
## mu_sample_1
## 1 0.89124560
## 2 1.19216368
## 3 4.07798775
## 4 -0.30795068
## 5 -0.80474032
## 6 0.25530646
## 7 0.42918574
## 8 0.19369713
## 9 1.54362911
## 10 -0.62065618
## 11 -0.57553498
## 12 0.15784024
## 13 0.20271600
## 14 -0.72576180
## 15 0.49033609
## 16 -0.86435267
## 17 0.46506776
## 18 -0.36872219
## 19 -0.02671433
## 20 -0.42802913
##
## $B
## metabolite.ID metabolite KEGG time.ID condition mu_mean
## 1 1 Acetoacetate C00164 1 B -0.04572527
## 2 1 Acetoacetate C00164 2 B 0.57063873
## 3 1 Acetoacetate C00164 3 B -0.86510001
## 4 1 Acetoacetate C00164 4 B 0.44469844
## 5 2 Adenine C00147 1 B -0.13728337
## 6 2 Adenine C00147 2 B 0.56948206
## 7 2 Adenine C00147 3 B 0.25949232
## 8 2 Adenine C00147 4 B 0.48955371
## 9 3 DL-Glycerol 1-phosphate C00093 1 B -0.30997089
## 10 3 DL-Glycerol 1-phosphate C00093 2 B 0.14155972
## 11 3 DL-Glycerol 1-phosphate C00093 3 B 0.00197623
## 12 3 DL-Glycerol 1-phosphate C00093 4 B -0.41772641
## 13 4 NADP+ C00006 1 B 0.35522658
## 14 4 NADP+ C00006 2 B -0.18239743
## 15 4 NADP+ C00006 3 B -0.01601893
## 16 4 NADP+ C00006 4 B -0.63034225
## 17 5 Pyruvate C00022 1 B -0.29218469
## 18 5 Pyruvate C00022 2 B 0.21679474
## 19 5 Pyruvate C00022 3 B 0.13838660
## 20 5 Pyruvate C00022 4 B -0.14200802
## mu_lower mu_higher sigma_mean sigma_lower sigma_higher lambda_mean
## 1 -1.5000203 1.29685184 1.1976319 0.47021856 3.0944544 0.8570181
## 2 -0.3031200 1.41912289 0.6810474 0.21094353 2.0036808 0.8570181
## 3 -2.5233270 1.01505733 1.6988452 0.76804008 3.9273495 0.8570181
## 4 -1.0608005 1.77299560 1.1768727 0.42696812 3.1060596 0.8570181
## 5 -1.6860384 1.48102231 1.3019817 0.51410859 3.2682581 0.7679598
## 6 -1.5437566 2.48988636 1.9895196 0.86238833 4.9355899 0.7679598
## 7 -1.0055176 1.59118536 1.2184038 0.46533169 3.0429720 0.7679598
## 8 -0.3479389 1.34596823 0.5848614 0.17191677 1.7942570 0.7679598
## 9 -2.1392381 1.68337785 1.8562838 0.80513599 4.5680223 0.9053500
## 10 -1.8246217 2.09449968 2.1348545 0.95481086 5.0823767 0.9053500
## 11 -0.8099117 0.91998914 0.6246820 0.19697486 1.8277461 0.9053500
## 12 -1.6127113 0.75406359 0.9106394 0.30666854 2.6118255 0.9053500
## 13 -0.0036755 0.67491072 0.2313804 0.05304051 0.8521225 0.7116714
## 14 -1.8629572 1.62408443 1.5765193 0.64913877 3.5924428 0.7116714
## 15 -1.3312205 1.23503022 1.0691277 0.38890786 2.8580352 0.7116714
## 16 -1.0707510 -0.05517782 0.4025908 0.11212718 1.4918200 0.7116714
## 17 -2.3439509 1.79285575 1.9861459 0.87472333 4.6054734 0.7670619
## 18 -1.1620497 1.64602363 1.1736856 0.45739089 2.9086008 0.7670619
## 19 -0.8492642 1.23463481 0.7932195 0.26137115 2.3696045 0.7670619
## 20 -0.8302098 0.31690329 0.3684208 0.07134261 1.6407537 0.7670619
## lambda_lower lambda_higher delta_mu_mean delta_mu_lower delta_mu_higher
## 1 0.2429131 1.913690 0.6152073 -1.8369454 2.9362956
## 2 0.2429131 1.913690 -0.5675058 -2.6019421 1.6683762
## 3 0.2429131 1.913690 -0.6323185 -1.6095873 0.4828830
## 4 0.2429131 1.913690 NA NA NA
## 5 0.2176174 1.698201 -0.3111464 -1.8413068 1.3418459
## 6 0.2176174 1.698201 -0.6772187 -2.3497895 0.9852841
## 7 0.2176174 1.698201 0.1255417 -2.3046903 2.4015568
## 8 0.2176174 1.698201 NA NA NA
## 9 0.2813933 1.996777 1.3546537 -0.7045678 3.2582731
## 10 0.2813933 1.996777 -0.1343271 -1.0064800 0.7496072
## 11 0.2813933 1.996777 -0.1384318 -1.5083515 1.3515683
## 12 0.2813933 1.996777 NA NA NA
## 13 0.1850753 1.570892 -0.7546693 -2.9801374 1.9263179
## 14 0.1850753 1.570892 0.1275735 -2.4011837 2.7187798
## 15 0.1850753 1.570892 0.3207840 -1.7873907 2.3651035
## 16 0.1850753 1.570892 NA NA NA
## 17 0.2150114 1.670167 0.2788431 -2.0550827 2.7346365
## 18 0.2150114 1.670167 -0.1575787 -2.5006632 2.1027525
## 19 0.2150114 1.670167 -0.1259891 -1.8257092 1.2778307
## 20 0.2150114 1.670167 NA NA NA
## mu_sample_1
## 1 0.83439736
## 2 0.53112848
## 3 -1.36239478
## 4 0.51245593
## 5 -0.31518859
## 6 -0.06289634
## 7 -0.40418770
## 8 0.54795107
## 9 0.53435991
## 10 1.47191948
## 11 -0.67131500
## 12 -0.45812064
## 13 0.87950889
## 14 -1.17428575
## 15 0.13570216
## 16 -0.49784465
## 17 0.10127017
## 18 -0.54788710
## 19 0.35537074
## 20 -0.24746031
##
## $C
## metabolite.ID metabolite KEGG time.ID condition mu_mean
## 1 1 Acetoacetate C00164 1 C 0.0706242511
## 2 1 Acetoacetate C00164 2 C 0.0947600206
## 3 1 Acetoacetate C00164 3 C -1.1861432749
## 4 1 Acetoacetate C00164 4 C 0.8830622645
## 5 2 Adenine C00147 1 C 0.7249368301
## 6 2 Adenine C00147 2 C 0.0884184355
## 7 2 Adenine C00147 3 C -0.4843416484
## 8 2 Adenine C00147 4 C 0.6116451432
## 9 3 DL-Glycerol 1-phosphate C00093 1 C 0.0003836704
## 10 3 DL-Glycerol 1-phosphate C00093 2 C -0.7374820908
## 11 3 DL-Glycerol 1-phosphate C00093 3 C -0.3745723878
## 12 3 DL-Glycerol 1-phosphate C00093 4 C 0.4000593356
## 13 4 NADP+ C00006 1 C 0.3260421593
## 14 4 NADP+ C00006 2 C -0.4919635242
## 15 4 NADP+ C00006 3 C -0.4578508256
## 16 4 NADP+ C00006 4 C 0.1367245476
## 17 5 Pyruvate C00022 1 C -0.0014101500
## 18 5 Pyruvate C00022 2 C 0.3967271479
## 19 5 Pyruvate C00022 3 C -0.2875324486
## 20 5 Pyruvate C00022 4 C 0.3597991165
## mu_lower mu_higher sigma_mean sigma_lower sigma_higher lambda_mean
## 1 -1.8038390 1.96243063 1.8078579 0.78014754 4.256301 0.7390999
## 2 -1.0877528 1.35693598 1.0032923 0.35042372 2.818735 0.7390999
## 3 -2.3869663 0.54457446 1.1776857 0.45384212 3.090329 0.7390999
## 4 0.2034989 1.47931507 0.4782437 0.14469294 1.484844 0.7390999
## 5 -1.0790155 2.37854448 1.6300675 0.73757930 3.641878 0.7174483
## 6 -1.8585720 1.93427095 1.8183241 0.78419538 4.759843 0.7174483
## 7 -1.6376194 0.74308674 0.9153012 0.32042622 2.497760 0.7174483
## 8 -0.3101769 1.41252036 0.6680577 0.21999068 2.027963 0.7174483
## 9 -1.7397204 1.75230631 1.8527441 0.86152749 4.264307 0.9433840
## 10 -1.9272237 0.57199964 1.0446176 0.38807776 2.836337 0.9433840
## 11 -1.3041604 0.54898304 0.7212178 0.22906773 2.146351 0.9433840
## 12 -1.3117876 2.01222648 1.4221321 0.58562981 3.437995 0.9433840
## 13 -0.4641394 1.10585725 0.4981359 0.13299812 1.616237 0.9119730
## 14 -0.8998952 0.03569735 0.3038941 0.07127457 1.335758 0.9119730
## 15 -1.2131561 0.38348090 0.5901956 0.16800154 1.761872 0.9119730
## 16 -1.1128395 1.27780917 0.8938242 0.30396307 2.611093 0.9119730
## 17 -1.8793004 1.84633242 1.8990288 0.84909938 4.205317 0.8629525
## 18 -0.9391248 1.70054622 1.1055010 0.43693584 2.690874 0.8629525
## 19 -1.5488944 0.95304201 1.0890894 0.43173522 2.845977 0.8629525
## 20 -0.7711583 1.26253307 0.6660266 0.20088866 2.210573 0.8629525
## lambda_lower lambda_higher delta_mu_mean delta_mu_lower delta_mu_higher
## 1 0.2051887 1.663840 0.01779418 -2.6460816 2.5468159
## 2 0.2051887 1.663840 -0.46299082 -2.4678748 1.6190455
## 3 0.2051887 1.663840 0.51129694 -1.0777498 2.0516828
## 4 0.2051887 1.663840 NA NA NA
## 5 0.1984024 1.587084 -0.57910167 -2.2057535 1.1166083
## 6 0.1984024 1.587084 0.88440098 -1.1602045 2.8364376
## 7 0.1984024 1.587084 -0.40146949 -2.7584826 2.1098666
## 8 0.1984024 1.587084 NA NA NA
## 9 0.2782918 2.084831 1.79778842 -0.1190014 3.2589824
## 10 0.2782918 2.084831 -0.28560298 -1.4250713 0.8429331
## 11 0.2782918 2.084831 0.07068499 -1.4804883 1.4343198
## 12 0.2782918 2.084831 NA NA NA
## 13 0.2683373 1.985604 -0.88267859 -2.6918015 1.0472522
## 14 0.2683373 1.985604 -0.49234719 -2.2999367 1.3795590
## 15 0.2683373 1.985604 0.20443108 -1.1413715 1.5110813
## 16 0.2683373 1.985604 NA NA NA
## 17 0.2405670 1.845530 -1.46241892 -3.4126598 0.8478424
## 18 0.2405670 1.845530 0.27963127 -1.2765392 1.7215858
## 19 0.2405670 1.845530 0.81764994 -0.5878228 1.9676097
## 20 0.2405670 1.845530 NA NA NA
## mu_sample_1
## 1 -0.98551002
## 2 0.81946464
## 3 -1.16843817
## 4 0.91009791
## 5 -1.09780556
## 6 1.05881198
## 7 0.49308640
## 8 0.26188758
## 9 0.24993684
## 10 -0.31753761
## 11 -0.37260403
## 12 0.20416788
## 13 0.17176047
## 14 -0.45211875
## 15 -0.36247992
## 16 0.55690917
## 17 -0.91650937
## 18 0.06124678
## 19 -0.19691430
## 20 -0.29310664
With the output from the estimates_dynamics function we can cluster metabolite dynamics per experimental condition.
cluster <- # clustering results have to be stored in separate object when using data frame as input
cluster_dynamics(
data = estimates, # data is now the estimates or a data frame ob similar structure
distance = "euclidean", # which distance method should be used
agglomeration = "ward.D2", # which agglomeration method for hierarchical clustering should be used
deepSplit = 2, # sensitivity of cluster analysis,
minClusterSize = 1 # minimum number of metabolites in one cluster
)
## Is your data normalized and standardized?
## We recommend normalization by log-transformation.
## Scaling and centering (mean=0, sd=1) should be metabolite and condition specific.
## ..cutHeight not given, setting it to 2.27 ===> 99% of the (truncated) height range in dendro.
## ..done.
## ..cutHeight not given, setting it to 1.63 ===> 99% of the (truncated) height range in dendro.
## ..done.
## ..cutHeight not given, setting it to 1.41 ===> 99% of the (truncated) height range in dendro.
## ..done.
To visualize the clustering results we can use the function plot_cluster which returns a list of plots. The list element “lineplot” visualizes the metabolite dynamics in each cluster for every condition:
plots <- plot_cluster(cluster)
plots[["lineplot"]]
Additionally we can visualize for every experimental condition the dendrogram and a Principal component analysis of the clustering solution.
# For condition A
plots[["A"]]
## $dendrogram
##
## $PCA_plot
## Warning: Computation failed in `stat_ellipse()`.
## Caused by error in `if (all(abs(w - w0) < tol)) ...`:
## ! missing value where TRUE/FALSE needed
To conduct the over-representation analysis KEGG IDs of the experimental metabolites are needed. Additionally, information on all metabolites that are stored in the KEGG database is needed. This KEGG background information is stored in the data set “modules_compounds”. To apply ORA to a specific dataset a second dataset “metabolite_modules” is needed that can be obtained by filtering “modules_compounds” for the experimental metabolites.
The clustering result also needs to be converted from a list to a single data frame.
# load background dataset
data("modules_compounds")
# get KEGGs from data
KEGGs <- unique(data$KEGG)
# construct "metabolite_modules" data set
metabolite_modules <- modules_compounds%>%filter(KEGG%in%KEGGs)
# convert clustering results from list to dataframe
cluster_data <- rbind(cluster[["A"]][["data"]],
cluster[["B"]][["data"]],
cluster[["C"]][["data"]])
The over-representation analysis and visualization can be achieved with the following code:
ora <- # store ORA result to separate object when using data frames as input
ORA_hypergeometric(
background = modules_compounds,
annotations = metabolite_modules,
data = cluster_data, # dataframe format of clustering output
tested_column = "middle_hierarchy"
)
# Visualization
plot_ORA(
data = ora,
tested_column = "middle_hierarchy"
)
For the comparison of dynamics clusters between experimental conditions one can employ two apporaches: comparing dynamics between clusters and comparing metabolite composition between clusters.
To compare dynamics between clusters awhile using a data frame as input use the following code:
comparison_dynamics <- # result needs to be stored in a separate object when using data frames
compare_dynamics(
data = cluster_data, # clustering result
dynamics = c("1","2","3","4"), # which columns specify the time point specific mean metabolite abundances?
cores = 1 # only set to 1 for vignette, can be increased to 4 for parallelization
)
##
## SAMPLING FOR MODEL 'm_cluster_distances_padded' NOW (CHAIN 1).
## Chain 1:
## Chain 1: Gradient evaluation took 2.2e-05 seconds
## Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 0.22 seconds.
## Chain 1: Adjust your expectations accordingly!
## Chain 1:
## Chain 1:
## Chain 1: Iteration: 1 / 2000 [ 0%] (Warmup)
## Chain 1: Iteration: 200 / 2000 [ 10%] (Warmup)
## Chain 1: Iteration: 400 / 2000 [ 20%] (Warmup)
## Chain 1: Iteration: 501 / 2000 [ 25%] (Sampling)
## Chain 1: Iteration: 700 / 2000 [ 35%] (Sampling)
## Chain 1: Iteration: 900 / 2000 [ 45%] (Sampling)
## Chain 1: Iteration: 1100 / 2000 [ 55%] (Sampling)
## Chain 1: Iteration: 1300 / 2000 [ 65%] (Sampling)
## Chain 1: Iteration: 1500 / 2000 [ 75%] (Sampling)
## Chain 1: Iteration: 1700 / 2000 [ 85%] (Sampling)
## Chain 1: Iteration: 1900 / 2000 [ 95%] (Sampling)
## Chain 1: Iteration: 2000 / 2000 [100%] (Sampling)
## Chain 1:
## Chain 1: Elapsed Time: 0.178 seconds (Warm-up)
## Chain 1: 0.477 seconds (Sampling)
## Chain 1: 0.655 seconds (Total)
## Chain 1:
##
## SAMPLING FOR MODEL 'm_cluster_distances_padded' NOW (CHAIN 2).
## Chain 2:
## Chain 2: Gradient evaluation took 1.7e-05 seconds
## Chain 2: 1000 transitions using 10 leapfrog steps per transition would take 0.17 seconds.
## Chain 2: Adjust your expectations accordingly!
## Chain 2:
## Chain 2:
## Chain 2: Iteration: 1 / 2000 [ 0%] (Warmup)
## Chain 2: Iteration: 200 / 2000 [ 10%] (Warmup)
## Chain 2: Iteration: 400 / 2000 [ 20%] (Warmup)
## Chain 2: Iteration: 501 / 2000 [ 25%] (Sampling)
## Chain 2: Iteration: 700 / 2000 [ 35%] (Sampling)
## Chain 2: Iteration: 900 / 2000 [ 45%] (Sampling)
## Chain 2: Iteration: 1100 / 2000 [ 55%] (Sampling)
## Chain 2: Iteration: 1300 / 2000 [ 65%] (Sampling)
## Chain 2: Iteration: 1500 / 2000 [ 75%] (Sampling)
## Chain 2: Iteration: 1700 / 2000 [ 85%] (Sampling)
## Chain 2: Iteration: 1900 / 2000 [ 95%] (Sampling)
## Chain 2: Iteration: 2000 / 2000 [100%] (Sampling)
## Chain 2:
## Chain 2: Elapsed Time: 0.197 seconds (Warm-up)
## Chain 2: 0.336 seconds (Sampling)
## Chain 2: 0.533 seconds (Total)
## Chain 2:
##
## SAMPLING FOR MODEL 'm_cluster_distances_padded' NOW (CHAIN 3).
## Chain 3:
## Chain 3: Gradient evaluation took 1.4e-05 seconds
## Chain 3: 1000 transitions using 10 leapfrog steps per transition would take 0.14 seconds.
## Chain 3: Adjust your expectations accordingly!
## Chain 3:
## Chain 3:
## Chain 3: Iteration: 1 / 2000 [ 0%] (Warmup)
## Chain 3: Iteration: 200 / 2000 [ 10%] (Warmup)
## Chain 3: Iteration: 400 / 2000 [ 20%] (Warmup)
## Chain 3: Iteration: 501 / 2000 [ 25%] (Sampling)
## Chain 3: Iteration: 700 / 2000 [ 35%] (Sampling)
## Chain 3: Iteration: 900 / 2000 [ 45%] (Sampling)
## Chain 3: Iteration: 1100 / 2000 [ 55%] (Sampling)
## Chain 3: Iteration: 1300 / 2000 [ 65%] (Sampling)
## Chain 3: Iteration: 1500 / 2000 [ 75%] (Sampling)
## Chain 3: Iteration: 1700 / 2000 [ 85%] (Sampling)
## Chain 3: Iteration: 1900 / 2000 [ 95%] (Sampling)
## Chain 3: Iteration: 2000 / 2000 [100%] (Sampling)
## Chain 3:
## Chain 3: Elapsed Time: 0.178 seconds (Warm-up)
## Chain 3: 0.267 seconds (Sampling)
## Chain 3: 0.445 seconds (Total)
## Chain 3:
##
## SAMPLING FOR MODEL 'm_cluster_distances_padded' NOW (CHAIN 4).
## Chain 4:
## Chain 4: Gradient evaluation took 1.3e-05 seconds
## Chain 4: 1000 transitions using 10 leapfrog steps per transition would take 0.13 seconds.
## Chain 4: Adjust your expectations accordingly!
## Chain 4:
## Chain 4:
## Chain 4: Iteration: 1 / 2000 [ 0%] (Warmup)
## Chain 4: Iteration: 200 / 2000 [ 10%] (Warmup)
## Chain 4: Iteration: 400 / 2000 [ 20%] (Warmup)
## Chain 4: Iteration: 501 / 2000 [ 25%] (Sampling)
## Chain 4: Iteration: 700 / 2000 [ 35%] (Sampling)
## Chain 4: Iteration: 900 / 2000 [ 45%] (Sampling)
## Chain 4: Iteration: 1100 / 2000 [ 55%] (Sampling)
## Chain 4: Iteration: 1300 / 2000 [ 65%] (Sampling)
## Chain 4: Iteration: 1500 / 2000 [ 75%] (Sampling)
## Chain 4: Iteration: 1700 / 2000 [ 85%] (Sampling)
## Chain 4: Iteration: 1900 / 2000 [ 95%] (Sampling)
## Chain 4: Iteration: 2000 / 2000 [100%] (Sampling)
## Chain 4:
## Chain 4: Elapsed Time: 0.186 seconds (Warm-up)
## Chain 4: 0.392 seconds (Sampling)
## Chain 4: 0.578 seconds (Total)
## Chain 4:
## Warning: There were 104 divergent transitions after warmup. See
## https://mc-stan.org/misc/warnings.html#divergent-transitions-after-warmup
## to find out why this is a problem and how to eliminate them.
## Warning: Examine the pairs() plot to diagnose sampling problems
The data frame needed for visualization of the results is the list element [[“estimates”]] of the function results. To visualize the results run the following code:
# Visualize comparison results
heatmap_dynamics(estimates = comparison_dynamics[["estimates"]],
data = cluster_data)
The comparison of metabolite composition follows the same principle as the comparison of dynamics between clusters:
# compare metabolite composition
compare_metabolites <-
compare_metabolites(
data = cluster_data
)
# Visualization
heatmap_metabolites(
distances = compare_metabolites,
data = cluster_data
)
The combination of both comparisons may facilitate detection of differences of longitudinal metabolomes between experimental conditions.
# combine comparison results
temp <- left_join(
comparison_dynamics[["estimates"]], # dynamics comparison
compare_metabolites,
join_by("cluster_a","cluster_b") # join by cluster comparisons
)
# get unique clusters
x <- unique(c(temp[,"cluster_a"], temp[,"cluster_b"]))
# draw plot
ggplot(temp, aes(x = cluster_b, y = cluster_a)) +
geom_point(aes(size = Jaccard, col = mu_mean)) +
theme_bw() +
scale_color_viridis_c(option = "magma") +
scale_x_discrete(limits = x) +
xlab("") +
ylab("") +
scale_y_discrete(limits = x) +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1)) +
labs(col = "dynamics distance", size = "metabolite similarity") +
ggtitle("comparison of clusters", "label = condition + cluster ID")
sessionInfo()
## R version 4.5.1 (2025-06-13)
## Platform: x86_64-pc-linux-gnu
## Running under: Ubuntu 24.04.3 LTS
##
## Matrix products: default
## BLAS: /home/biocbuild/bbs-3.22-bioc/R/lib/libRblas.so
## LAPACK: /usr/lib/x86_64-linux-gnu/openblas-serial/liblapack.so.3; LAPACK version 3.12.0
##
## locale:
## [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
## [3] LC_TIME=en_GB LC_COLLATE=C
## [5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
## [7] LC_PAPER=en_US.UTF-8 LC_NAME=C
## [9] LC_ADDRESS=C LC_TELEPHONE=C
## [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
##
## time zone: America/New_York
## tzcode source: system (glibc)
##
## attached base packages:
## [1] stats4 stats graphics grDevices utils datasets methods
## [8] base
##
## other attached packages:
## [1] tidyr_1.3.1 dplyr_1.1.4
## [3] ggplot2_3.5.2 SummarizedExperiment_1.39.1
## [5] Biobase_2.69.0 GenomicRanges_1.61.1
## [7] Seqinfo_0.99.2 IRanges_2.43.0
## [9] S4Vectors_0.47.0 BiocGenerics_0.55.1
## [11] generics_0.1.4 MatrixGenerics_1.21.0
## [13] matrixStats_1.5.0 MetaboDynamics_1.1.4
## [15] BiocStyle_2.37.1
##
## loaded via a namespace (and not attached):
## [1] gtable_0.3.6 xfun_0.53 bslib_0.9.0
## [4] QuickJSR_1.8.0 inline_0.3.21 lattice_0.22-7
## [7] vctrs_0.6.5 tools_4.5.1 curl_7.0.0
## [10] parallel_4.5.1 tibble_3.3.0 pkgconfig_2.0.3
## [13] Matrix_1.7-4 RColorBrewer_1.1-3 RcppParallel_5.1.11-1
## [16] lifecycle_1.0.4 compiler_4.5.1 farver_2.1.2
## [19] tinytex_0.57 codetools_0.2-20 htmltools_0.5.8.1
## [22] sass_0.4.10 yaml_2.3.10 pillar_1.11.0
## [25] crayon_1.5.3 jquerylib_0.1.4 MASS_7.3-65
## [28] DelayedArray_0.35.2 cachem_1.1.0 magick_2.8.7
## [31] StanHeaders_2.32.10 viridis_0.6.5 abind_1.4-8
## [34] rstan_2.32.7 tidyselect_1.2.1 digest_0.6.37
## [37] purrr_1.1.0 bookdown_0.44 labeling_0.4.3
## [40] fastmap_1.2.0 grid_4.5.1 cli_3.6.5
## [43] SparseArray_1.9.1 magrittr_2.0.3 loo_2.8.0
## [46] S4Arrays_1.9.1 utf8_1.2.6 dichromat_2.0-0.1
## [49] pkgbuild_1.4.8 dynamicTreeCut_1.63-1 withr_3.0.2
## [52] scales_1.4.0 rmarkdown_2.29 XVector_0.49.0
## [55] gridExtra_2.3 evaluate_1.0.5 knitr_1.50
## [58] V8_7.0.0 viridisLite_0.4.2 rstantools_2.5.0
## [61] rlang_1.1.6 Rcpp_1.1.0 dendextend_1.19.1
## [64] glue_1.8.0 BiocManager_1.30.26 jsonlite_2.0.0
## [67] R6_2.6.1