MetaboDynamics 1.1.3
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 datasets 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 beta-Alanine A 1 1 274. 2.44 -0.751 C00099
## 2 beta-Alanine A 1 2 1762. 3.25 0.601 C00099
## 3 beta-Alanine A 1 3 222. 2.35 -0.904 C00099
## 4 beta-Alanine A 2 1 14371. 4.16 2.13 C00099
## 5 beta-Alanine A 2 2 205. 2.31 -0.962 C00099
## 6 beta-Alanine A 2 3 951. 2.98 0.152 C00099
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.6e-05 seconds
## Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 0.26 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.239 seconds (Warm-up)
## Chain 1: 0.435 seconds (Sampling)
## Chain 1: 0.674 seconds (Total)
## Chain 1:
##
## SAMPLING FOR MODEL 'm_ANOVA_partial_pooling' NOW (CHAIN 1).
## Chain 1:
## Chain 1: Gradient evaluation took 2.3e-05 seconds
## Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 0.23 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.224 seconds (Warm-up)
## Chain 1: 0.658 seconds (Sampling)
## Chain 1: 0.882 seconds (Total)
## Chain 1:
##
## SAMPLING FOR MODEL 'm_ANOVA_partial_pooling' NOW (CHAIN 1).
## Chain 1:
## Chain 1: Gradient evaluation took 3.3e-05 seconds
## Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 0.33 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.244 seconds (Warm-up)
## Chain 1: 1.334 seconds (Sampling)
## Chain 1: 1.578 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 is 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 fine: The model fitting did not result in
divergent transitions, the maximum treedepth was not exceeded, Rhat values were
below 1.01 and number of effective samples exceeded 100.
The 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 visulized 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
# only visualize dynamics
plot_estimates(data = data,
estimates = estimates,
delta_t = FALSE,
dynamics = TRUE) # choose to visualize the dynamics
## $plot_dynamics
# Clustering 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 less then triplicate 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 shows the needed data frame format for the clustering function:
head(estimates)
## $A
## metabolite.ID metabolite KEGG time.ID condition mu_mean mu_lower
## 1 1 CDP C00112 1 A -0.26886179 -1.8527332
## 2 1 CDP C00112 2 A 0.16627281 -1.6666605
## 3 1 CDP C00112 3 A 0.21951854 -2.0027765
## 4 1 CDP C00112 4 A 0.76231751 -1.4565237
## 5 2 L-Threonine C00188 1 A -0.35598608 -1.8760260
## 6 2 L-Threonine C00188 2 A -0.21997796 -2.0166914
## 7 2 L-Threonine C00188 3 A 0.37880799 -0.5144847
## 8 2 L-Threonine C00188 4 A -0.74033630 -1.4773002
## 9 3 NADH C00004 1 A -0.30289417 -1.0817264
## 10 3 NADH C00004 2 A 0.34154756 -1.6192101
## 11 3 NADH C00004 3 A 0.39637027 -1.2394069
## 12 3 NADH C00004 4 A 0.03994129 -1.3506320
## 13 4 Palmitic acid C00249 1 A 0.34273558 -0.4764669
## 14 4 Palmitic acid C00249 2 A -0.10316816 -1.4061130
## 15 4 Palmitic acid C00249 3 A -0.35624346 -1.7443523
## 16 4 Palmitic acid C00249 4 A 0.13393593 -0.8478089
## 17 5 beta-Alanine C00099 1 A -0.54796520 -2.1858212
## 18 5 beta-Alanine C00099 2 A 0.10328969 -1.0913918
## 19 5 beta-Alanine C00099 3 A -0.42457770 -1.4921415
## 20 5 beta-Alanine C00099 4 A 0.25599572 -1.5144297
## mu_higher sigma_mean sigma_lower sigma_higher lambda_mean lambda_lower
## 1 1.3456773 1.6572601 0.7329698 4.123736 0.7088261 0.2053329
## 2 1.9354928 1.7177532 0.6968706 4.169671 0.7088261 0.2053329
## 3 2.1642362 2.0155206 0.9463478 4.403154 0.7088261 0.2053329
## 4 2.5430745 1.8308621 0.8000810 4.356729 0.7088261 0.2053329
## 5 0.9991290 1.2367993 0.4754094 3.315342 0.7106168 0.2159526
## 6 1.6585182 1.7889807 0.7612933 4.309565 0.7106168 0.2159526
## 7 1.2389737 0.7391684 0.2294101 2.203532 0.7106168 0.2159526
## 8 0.0941614 0.5895851 0.1867080 1.806359 0.7106168 0.2159526
## 9 0.6827268 0.6509837 0.2098077 1.974505 0.8194789 0.2198289
## 10 2.3540677 1.8919720 0.7951864 4.626224 0.8194789 0.2198289
## 11 1.7131984 1.0242561 0.3518111 2.884695 0.8194789 0.2198289
## 12 1.3666624 1.1727633 0.4399841 3.374195 0.8194789 0.2198289
## 13 1.3420612 0.6344749 0.1900192 2.034576 0.8099679 0.2296443
## 14 1.3130230 1.1369843 0.4498693 2.792948 0.8099679 0.2296443
## 15 1.1420591 1.1210277 0.3965402 3.147326 0.8099679 0.2296443
## 16 1.1239903 0.8375496 0.2799563 2.422066 0.8099679 0.2296443
## 17 1.2862984 1.7309157 0.7314572 4.004486 0.6866111 0.1965600
## 18 1.2942238 1.0019279 0.3507424 2.967253 0.6866111 0.1965600
## 19 0.6975508 0.8630712 0.2736831 2.482304 0.6866111 0.1965600
## 20 1.7131301 1.3691537 0.5384273 3.542809 0.6866111 0.1965600
## lambda_higher delta_mu_mean delta_mu_lower delta_mu_higher mu_sample_1
## 1 1.513522 0.04888384 -2.17898352 2.644957 -2.75595966
## 2 1.513522 0.61634823 -1.69499091 2.853168 -0.82101490
## 3 1.513522 -0.26243434 -1.97164592 1.748879 2.32984650
## 4 1.513522 NA NA NA 0.38525629
## 5 1.602901 0.21253519 -1.66667793 2.223195 -0.50354239
## 6 1.602901 -0.33886670 -1.98188093 1.232779 -0.09754726
## 7 1.602901 -0.58790649 -2.78634221 1.735181 0.39773075
## 8 1.602901 NA NA NA -0.79214297
## 9 1.782519 -0.95985484 -2.92005665 1.481191 -0.53353240
## 10 1.782519 1.08307188 -0.07386394 2.311830 0.75371281
## 11 1.782519 -0.23944590 -1.83086903 1.252127 0.48579397
## 12 1.782519 NA NA NA 0.30775192
## 13 1.727023 -1.06521168 -2.95312676 1.095694 -0.05030239
## 14 1.727023 0.19972601 -1.36193723 1.795139 0.13527979
## 15 1.727023 -0.32140954 -2.01071630 1.281364 0.06126120
## 16 1.727023 NA NA NA 0.15429430
## 17 1.477192 0.69753363 -1.72212917 3.223442 -1.13813636
## 18 1.477192 -0.69779102 -2.97717840 1.595296 1.40147684
## 19 1.477192 0.61223918 -1.72829631 2.613248 -0.86043901
## 20 1.477192 NA NA NA 0.77744306
##
## $B
## metabolite.ID metabolite KEGG time.ID condition mu_mean mu_lower
## 1 1 CDP C00112 1 B 0.31337356 -0.2785768
## 2 1 CDP C00112 2 B -0.51072281 -1.9413348
## 3 1 CDP C00112 3 B -0.31370512 -1.7226675
## 4 1 CDP C00112 4 B -0.61790629 -2.5086725
## 5 2 L-Threonine C00188 1 B -0.89301802 -1.9355825
## 6 2 L-Threonine C00188 2 B 0.19327271 -1.8499185
## 7 2 L-Threonine C00188 3 B -0.06539995 -1.5009701
## 8 2 L-Threonine C00188 4 B 0.17060945 -1.5708017
## 9 3 NADH C00004 1 B 0.40045475 -0.6576840
## 10 3 NADH C00004 2 B 0.36214426 -0.9923029
## 11 3 NADH C00004 3 B -0.48103819 -1.2653261
## 12 3 NADH C00004 4 B 0.62118794 -1.0629589
## 13 4 Palmitic acid C00249 1 B 0.66548686 -0.9891734
## 14 4 Palmitic acid C00249 2 B 0.14232001 -1.3234285
## 15 4 Palmitic acid C00249 3 B 0.41506362 -0.2050056
## 16 4 Palmitic acid C00249 4 B -0.06274856 -1.4234937
## 17 5 beta-Alanine C00099 1 B 0.01634795 -1.4904524
## 18 5 beta-Alanine C00099 2 B -0.61995532 -1.2353108
## 19 5 beta-Alanine C00099 3 B 0.20526344 -0.7842238
## 20 5 beta-Alanine C00099 4 B 0.10046476 -1.7573171
## mu_higher sigma_mean sigma_lower sigma_higher lambda_mean lambda_lower
## 1 0.8728311 0.4173851 0.1085624 1.566474 0.8200095 0.2622024
## 2 1.1516668 1.5545898 0.6373925 3.762128 0.8200095 0.2622024
## 3 1.4105074 1.3902934 0.5620831 3.445817 0.8200095 0.2622024
## 4 1.6168582 1.8998077 0.8328989 4.420515 0.8200095 0.2622024
## 5 0.2585274 0.8755300 0.3177626 2.323054 0.6875469 0.1939117
## 6 2.1590260 2.0612579 0.9636590 4.498931 0.6875469 0.1939117
## 7 1.4526611 1.2564165 0.4619251 3.233216 0.6875469 0.1939117
## 8 1.7211646 1.6011589 0.6727403 3.750401 0.6875469 0.1939117
## 9 1.3361349 0.8029659 0.2615727 2.449345 0.7780201 0.2544639
## 10 1.6193684 1.1677281 0.4342290 2.903796 0.7780201 0.2544639
## 11 0.3692276 0.5642247 0.1610393 1.789832 0.7780201 0.2544639
## 12 2.2456546 1.3953269 0.5150823 3.649393 0.7780201 0.2544639
## 13 2.1546933 1.2505832 0.4708541 3.191930 0.7698002 0.2276688
## 14 1.5282674 1.3241986 0.5381390 3.358318 0.7698002 0.2276688
## 15 1.0868251 0.4331936 0.1065570 1.813719 0.7698002 0.2276688
## 16 1.4541639 1.2797456 0.5111279 3.065629 0.7698002 0.2276688
## 17 1.5260925 1.3477190 0.5359866 3.340967 0.8193578 0.2649074
## 18 0.1226904 0.4766762 0.1180458 1.731792 0.8193578 0.2649074
## 19 1.1150761 0.7821115 0.2565265 2.275798 0.8193578 0.2649074
## 20 1.9876388 1.8067269 0.7953066 4.117061 0.8193578 0.2649074
## lambda_higher delta_mu_mean delta_mu_lower delta_mu_higher mu_sample_1
## 1 1.705073 -0.12010085 -2.2851681 1.9567459 0.09575999
## 2 1.705073 -0.67431090 -2.7389936 1.5099294 -1.84950567
## 3 1.705073 0.41828964 -1.1554675 2.1467289 0.24395414
## 4 1.705073 NA NA NA -1.47363766
## 5 1.528413 0.44532286 -1.6847856 2.6333251 -0.70029979
## 6 1.528413 0.68658788 -1.5084322 2.7681697 -0.57151806
## 7 1.528413 -0.60483998 -2.6810593 1.5351729 0.26643025
## 8 1.528413 NA NA NA 0.20290793
## 9 1.684660 0.48431457 -1.8039585 2.6901771 0.14475391
## 10 1.684660 0.49487741 -1.8745029 2.6084171 1.99514248
## 11 1.684660 -1.28544218 -2.8797619 0.5558187 -0.59224814
## 12 1.684660 NA NA NA 0.05228421
## 13 1.632471 1.01836103 -1.3639699 3.1645525 1.56354813
## 14 1.632471 -0.25813474 -1.9031055 1.4372878 0.13222133
## 15 1.632471 0.06294344 -1.7019718 1.9729540 0.63014118
## 16 1.632471 NA NA NA -0.37159521
## 17 1.676210 1.25516228 -0.4489207 2.8730950 0.41169680
## 18 1.676210 0.05291936 -1.4107317 1.5942838 -0.58189523
## 19 1.676210 -0.31459886 -2.3741419 1.7147660 0.03373612
## 20 1.676210 NA NA NA -0.29346804
##
## $C
## metabolite.ID metabolite KEGG time.ID condition mu_mean mu_lower
## 1 1 CDP C00112 1 C -0.22090336 -2.2406245
## 2 1 CDP C00112 2 C -0.29710973 -2.1410278
## 3 1 CDP C00112 3 C -0.07245762 -1.6513812
## 4 1 CDP C00112 4 C 0.54456451 -1.1672145
## 5 2 L-Threonine C00188 1 C 0.01900219 -1.8597143
## 6 2 L-Threonine C00188 2 C -0.13178393 -1.1487489
## 7 2 L-Threonine C00188 3 C -0.36668786 -1.8649744
## 8 2 L-Threonine C00188 4 C 0.34818209 -1.6566007
## 9 3 NADH C00004 1 C -0.00144419 -0.6847248
## 10 3 NADH C00004 2 C 0.62696986 -0.4971458
## 11 3 NADH C00004 3 C 0.30221661 -1.4448483
## 12 3 NADH C00004 4 C 0.48359812 -0.4302690
## 13 4 Palmitic acid C00249 1 C 0.15282680 -1.2142933
## 14 4 Palmitic acid C00249 2 C -0.72490362 -2.0416156
## 15 4 Palmitic acid C00249 3 C 0.03216662 -1.1702512
## 16 4 Palmitic acid C00249 4 C 0.16906037 -0.4099004
## 17 5 beta-Alanine C00099 1 C 0.21999437 -1.1210563
## 18 5 beta-Alanine C00099 2 C -0.50169509 -1.5540642
## 19 5 beta-Alanine C00099 3 C 0.25065931 -0.1846906
## 20 5 beta-Alanine C00099 4 C -0.63847669 -2.0765365
## mu_higher sigma_mean sigma_lower sigma_higher lambda_mean lambda_lower
## 1 1.9152187 2.0396996 0.86504391 4.900701 0.7508767 0.2169460
## 2 1.6757309 1.9303575 0.83146939 4.332854 0.7508767 0.2169460
## 3 1.4677374 1.2140093 0.46870400 3.182271 0.7508767 0.2169460
## 4 2.3397866 1.7950791 0.74128684 4.089590 0.7508767 0.2169460
## 5 2.0109714 1.8856111 0.80187410 4.497379 0.7651754 0.2356300
## 6 1.0866213 0.9740439 0.34133302 2.783213 0.7651754 0.2356300
## 7 1.2992093 1.3243298 0.50571411 3.471903 0.7651754 0.2356300
## 8 2.3059103 1.9331066 0.85620242 4.359686 0.7651754 0.2356300
## 9 0.7278622 0.5299591 0.16208075 1.646255 0.7218071 0.2045713
## 10 1.5233530 0.8185964 0.26986059 2.443348 0.7218071 0.2045713
## 11 2.0065660 1.5474559 0.61411848 4.030843 0.7218071 0.2045713
## 12 1.2503081 0.6149172 0.18977613 1.909539 0.7218071 0.2045713
## 13 1.3634147 1.1999143 0.43766956 3.286801 0.8764362 0.2574690
## 14 0.9315820 1.3678378 0.54517229 3.487867 0.8764362 0.2574690
## 15 1.1967745 0.9347084 0.32008070 2.846291 0.8764362 0.2574690
## 16 0.8676857 0.4519325 0.12873086 1.520566 0.8764362 0.2574690
## 17 1.3350743 1.0025844 0.36205547 2.746462 0.7618688 0.2297000
## 18 0.7089755 0.9729992 0.36094806 2.782398 0.7618688 0.2297000
## 19 0.6717163 0.3093369 0.07730651 1.160108 0.7618688 0.2297000
## 20 0.7383330 1.2994089 0.51715467 3.102712 0.7618688 0.2297000
## lambda_higher delta_mu_mean delta_mu_lower delta_mu_higher mu_sample_1
## 1 1.676522 0.08911943 -2.3726516 2.389972 0.16062129
## 2 1.676522 0.43400054 -1.7339741 2.484385 -1.30790920
## 3 1.676522 -0.13315624 -1.9131453 1.766293 -0.71312221
## 4 1.676522 NA NA NA 0.80431537
## 5 1.673193 -0.06957813 -2.5274588 2.176263 0.20774643
## 6 1.673193 0.85028599 -0.9382447 2.519825 -0.61231764
## 7 1.673193 -0.26360376 -1.8638999 1.215019 -1.35944032
## 8 1.673193 NA NA NA -0.08139402
## 9 1.631893 0.42063971 -2.0607383 2.962670 0.04474859
## 10 1.631893 -0.19535528 -2.7692366 2.240666 0.62674996
## 11 1.631893 -0.65452189 -2.3334729 1.191889 -0.40840004
## 12 1.631893 NA NA NA 0.27608280
## 13 1.957424 -0.54600870 -2.4504875 1.389412 0.19240174
## 14 1.957424 -0.72345943 -2.2302727 1.045929 -1.25144112
## 15 1.957424 0.97556292 -0.7606147 2.451467 0.93658900
## 16 1.957424 NA NA NA 0.17731876
## 17 1.680353 0.60796766 -1.5945262 2.651563 0.11886839
## 18 1.680353 -0.59480323 -2.0973477 1.076467 -0.44133007
## 19 1.680353 -0.67064331 -2.5616717 1.073530 0.06584169
## 20 1.680353 NA NA NA -0.24081603
With the estimates obtained with 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 1.49 ===> 99% of the (truncated) height range in dendro.
## ..done.
## ..cutHeight not given, setting it to 1.68 ===> 99% of the (truncated) height range in dendro.
## ..done.
## ..cutHeight not given, setting it to 1.46 ===> 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. One element “lineplot” which 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 information is stored in the dataset “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.1e-05 seconds
## Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 0.21 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.567 seconds (Warm-up)
## Chain 1: 1.389 seconds (Sampling)
## Chain 1: 1.956 seconds (Total)
## Chain 1:
##
## SAMPLING FOR MODEL 'm_cluster_distances_padded' NOW (CHAIN 2).
## Chain 2:
## Chain 2: Gradient evaluation took 1.4e-05 seconds
## Chain 2: 1000 transitions using 10 leapfrog steps per transition would take 0.14 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.452 seconds (Warm-up)
## Chain 2: 1.179 seconds (Sampling)
## Chain 2: 1.631 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.533 seconds (Warm-up)
## Chain 3: 1.33 seconds (Sampling)
## Chain 3: 1.863 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.501 seconds (Warm-up)
## Chain 4: 1.445 seconds (Sampling)
## Chain 4: 1.946 seconds (Total)
## Chain 4:
## Warning: There were 584 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
## Warning: Tail Effective Samples Size (ESS) is too low, indicating posterior variances and tail quantiles may be unreliable.
## Running the chains for more iterations may help. See
## https://mc-stan.org/misc/warnings.html#tail-ess
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.2 LTS
##
## Matrix products: default
## BLAS: /home/biocbuild/bbs-3.22-bioc/R/lib/libRblas.so
## LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.12.0 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.3
## [15] BiocStyle_2.37.0
##
## loaded via a namespace (and not attached):
## [1] gtable_0.3.6 xfun_0.52 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_6.4.0
## [10] parallel_4.5.1 tibble_3.3.0 pkgconfig_2.0.3
## [13] Matrix_1.7-3 RColorBrewer_1.1-3 RcppParallel_5.1.10
## [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.43 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.4 knitr_1.50
## [58] V8_6.0.5 viridisLite_0.4.2 rstantools_2.4.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