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 Fumarate A 1 1 213. 2.33 -0.565 C00122
## 2 Fumarate A 1 2 575. 2.76 -0.0579 C00122
## 3 Fumarate A 1 3 199. 2.30 -0.599 C00122
## 4 Fumarate A 2 1 2049. 3.31 0.591 C00122
## 5 Fumarate A 2 2 809. 2.91 0.116 C00122
## 6 Fumarate A 2 3 1026. 3.01 0.238 C00122
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 3.2e-05 seconds
## Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 0.32 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.558 seconds (Warm-up)
## Chain 1: 2.445 seconds (Sampling)
## Chain 1: 3.003 seconds (Total)
## Chain 1:
##
## SAMPLING FOR MODEL 'm_ANOVA_partial_pooling' NOW (CHAIN 1).
## Chain 1:
## Chain 1: Gradient evaluation took 3.1e-05 seconds
## Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 0.31 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.493 seconds (Warm-up)
## Chain 1: 0.921 seconds (Sampling)
## Chain 1: 1.414 seconds (Total)
## Chain 1:
##
## SAMPLING FOR MODEL 'm_ANOVA_partial_pooling' 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.756 seconds (Warm-up)
## Chain 1: 1.173 seconds (Sampling)
## Chain 1: 1.929 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 Fumarate C00122 1 A -0.37105365
## 2 1 Fumarate C00122 2 A 0.16032630
## 3 1 Fumarate C00122 3 A 0.09391440
## 4 1 Fumarate C00122 4 A -0.33077972
## 5 2 Glutathione disulfide C00127 1 A 0.16409337
## 6 2 Glutathione disulfide C00127 2 A 0.33048756
## 7 2 Glutathione disulfide C00127 3 A -0.04944323
## 8 2 Glutathione disulfide C00127 4 A -0.30240107
## 9 3 L-Alanine C00041 1 A 0.43646757
## 10 3 L-Alanine C00041 2 A -0.41927195
## 11 3 L-Alanine C00041 3 A -0.20053544
## 12 3 L-Alanine C00041 4 A -0.22059296
## 13 4 L-Cystine C00491 1 A -0.29130497
## 14 4 L-Cystine C00491 2 A -0.09672434
## 15 4 L-Cystine C00491 3 A 0.37204047
## 16 4 L-Cystine C00491 4 A 0.29653405
## 17 5 Malic acid C00711 1 A 0.08518087
## 18 5 Malic acid C00711 2 A 0.35381229
## 19 5 Malic acid C00711 3 A -0.16763941
## 20 5 Malic acid C00711 4 A -0.07292919
## mu_lower mu_higher sigma_mean sigma_lower sigma_higher lambda_mean
## 1 -1.1379460 0.5365908 0.6269913 0.17339967 2.2798657 0.7942805
## 2 -2.1553301 2.2791008 2.0982523 0.95372311 4.5119802 0.7942805
## 3 -1.9345725 2.0535531 1.9308889 0.83966797 4.1815425 0.7942805
## 4 -1.3183646 0.6634436 0.7220873 0.23524433 2.2978312 0.7942805
## 5 -1.1871627 1.5242671 1.2324132 0.48160683 3.2728998 0.7129758
## 6 -0.3874089 1.2447110 0.5347312 0.14582508 1.7252000 0.7129758
## 7 -1.5777799 1.3372653 1.2171732 0.45027127 3.3386932 0.7129758
## 8 -0.8019761 0.1155723 0.3074935 0.07190322 1.3193023 0.7129758
## 9 -1.6195313 2.5599653 2.1420401 0.99569203 4.4988028 0.7557905
## 10 -2.4239470 1.7846960 1.9458192 0.82780622 4.2435585 0.7557905
## 11 -2.0486090 1.7411066 1.9758698 0.89139453 4.4232566 0.7557905
## 12 -1.2496136 0.9101053 0.8456960 0.28242205 2.5233694 0.7557905
## 13 -1.7163683 1.2580232 1.3016729 0.51192277 3.3558499 0.9426565
## 14 -0.4154174 0.2434110 0.2033254 0.04132858 0.9578039 0.9426565
## 15 -1.2709253 1.7406345 1.2554605 0.45087579 3.4346421 0.9426565
## 16 -1.3699850 1.8617952 1.4997202 0.60736025 4.0266034 0.9426565
## 17 -1.3361526 1.4878117 1.2085059 0.44715477 3.2302431 0.7306555
## 18 -1.3899184 1.9730425 1.4160296 0.55550918 3.6591007 0.7306555
## 19 -0.9469582 0.4371948 0.4418263 0.12578601 1.5205281 0.7306555
## 20 -1.0742441 0.9952764 0.8277292 0.25873056 2.5080003 0.7306555
## lambda_lower lambda_higher delta_mu_mean delta_mu_lower delta_mu_higher
## 1 0.2213175 1.798945 0.70154121 -0.4440659 1.8981264
## 2 0.2213175 1.798945 -0.53102299 -2.6085354 1.5488838
## 3 0.2213175 1.798945 0.49706949 -2.0860941 2.7949529
## 4 0.2213175 1.798945 NA NA NA
## 5 0.2058697 1.550474 -0.20976953 -2.7566175 2.3304697
## 6 0.2058697 1.550474 -0.17114973 -1.8199767 1.7090787
## 7 0.2058697 1.550474 0.30577383 -1.3620993 2.0908821
## 8 0.2058697 1.550474 NA NA NA
## 9 0.2449228 1.596661 -0.39631547 -2.4761398 1.7094281
## 10 0.2449228 1.596661 0.01109610 -1.4855766 1.6100806
## 11 0.2449228 1.596661 0.64511726 -1.7141931 2.7989659
## 12 0.2449228 1.596661 NA NA NA
## 13 0.2750152 1.962149 0.76724729 -1.4063045 3.0247077
## 14 0.2750152 1.962149 -0.53319191 -2.6769594 1.5274024
## 15 0.2750152 1.962149 -0.07091507 -0.8866328 0.6328194
## 16 0.2750152 1.962149 NA NA NA
## 17 0.2162002 1.572151 -0.58336533 -2.9575352 1.9547573
## 18 0.2162002 1.572151 0.79131242 -1.9318100 3.3242885
## 19 0.2162002 1.572151 -0.44496966 -2.2332500 1.3783769
## 20 0.2162002 1.572151 NA NA NA
## mu_sample_1
## 1 -0.016661751
## 2 -1.506231103
## 3 0.364507836
## 4 1.217452919
## 5 0.008704782
## 6 0.806781043
## 7 -0.486914754
## 8 -0.328159448
## 9 2.884184433
## 10 -1.054519507
## 11 -0.221847148
## 12 0.505157651
## 13 -0.035308786
## 14 -0.040265077
## 15 0.154241220
## 16 0.367410535
## 17 0.601959813
## 18 0.072136710
## 19 -0.218592996
## 20 0.027583287
##
## $B
## metabolite.ID metabolite KEGG time.ID condition mu_mean
## 1 1 Fumarate C00122 1 B 0.471875034
## 2 1 Fumarate C00122 2 B -0.694028433
## 3 1 Fumarate C00122 3 B -0.400965549
## 4 1 Fumarate C00122 4 B -0.210357594
## 5 2 Glutathione disulfide C00127 1 B 0.040236198
## 6 2 Glutathione disulfide C00127 2 B -0.884713648
## 7 2 Glutathione disulfide C00127 3 B 0.105927866
## 8 2 Glutathione disulfide C00127 4 B -0.353279576
## 9 3 L-Alanine C00041 1 B 1.151126831
## 10 3 L-Alanine C00041 2 B -0.751310515
## 11 3 L-Alanine C00041 3 B 0.899331561
## 12 3 L-Alanine C00041 4 B 0.161216313
## 13 4 L-Cystine C00491 1 B 0.333652161
## 14 4 L-Cystine C00491 2 B -0.522664989
## 15 4 L-Cystine C00491 3 B -0.005305676
## 16 4 L-Cystine C00491 4 B -0.425876313
## 17 5 Malic acid C00711 1 B 0.542684319
## 18 5 Malic acid C00711 2 B 0.468324019
## 19 5 Malic acid C00711 3 B -0.487550322
## 20 5 Malic acid C00711 4 B 0.709669647
## mu_lower mu_higher sigma_mean sigma_lower sigma_higher lambda_mean
## 1 -0.80232042 1.71022454 1.0908662 0.41705157 2.858503 0.9322558
## 2 -2.59395883 1.34904363 1.7788836 0.76905243 4.141115 0.9322558
## 3 -2.03474751 1.53901818 1.7458154 0.77522996 4.237150 0.9322558
## 4 -1.44391015 1.02550779 1.0134526 0.38552859 2.673431 0.9322558
## 5 -1.25126026 1.42347342 1.1058846 0.41630821 2.948813 0.7928558
## 6 -1.86870499 0.21612585 0.8572278 0.32571235 2.206571 0.7928558
## 7 -1.28285670 1.73899074 1.3792660 0.55878905 3.282926 0.7928558
## 8 -0.96760372 0.26677760 0.3724572 0.07943488 1.450843 0.7928558
## 9 -0.36973232 2.61882381 1.3885635 0.59339249 2.999794 0.7814033
## 10 -2.43177224 1.01474023 1.5013716 0.58634460 3.773553 0.7814033
## 11 0.07324911 1.42521023 0.3980763 0.10016010 1.618277 0.7814033
## 12 -1.06377554 1.36652592 1.0173808 0.37325387 2.846237 0.7814033
## 13 -1.21565779 1.70238848 1.3199759 0.50304941 3.202994 0.9502756
## 14 -1.53935312 0.60515926 0.8267525 0.30210765 2.145720 0.9502756
## 15 -1.22785771 1.24158735 1.0013554 0.32766691 3.029569 0.9502756
## 16 -1.74062996 0.95106878 1.2948589 0.55113501 2.903173 0.9502756
## 17 -0.38126907 1.18553614 0.4989576 0.13805397 1.677731 0.7530272
## 18 -0.99698517 1.89110478 1.2737686 0.48950527 3.231761 0.7530272
## 19 -0.96503553 0.08078959 0.3191260 0.06773382 1.384942 0.7530272
## 20 -1.20445360 2.40027922 1.3997088 0.56723811 3.465473 0.7530272
## lambda_lower lambda_higher delta_mu_mean delta_mu_lower delta_mu_higher
## 1 0.2742736 1.886738 -1.35658868 -2.9615844 0.2031733
## 2 0.2742736 1.886738 1.78404521 0.3394590 2.8510529
## 3 0.2742736 1.886738 -1.32520787 -2.7246717 0.3458873
## 4 0.2742736 1.886738 NA NA NA
## 5 0.2388514 1.754594 0.79995630 -1.6123634 3.3345880
## 6 0.2388514 1.754594 0.05528845 -2.0366259 1.8701008
## 7 0.2388514 1.754594 0.38146801 -1.0228229 1.7178211
## 8 0.2388514 1.754594 NA NA NA
## 9 0.2417951 1.620306 0.04768597 -1.9515803 1.8335430
## 10 0.2417951 1.620306 0.68693174 -1.0143644 2.2502203
## 11 0.2417951 1.620306 0.13467186 -1.8612684 2.0664664
## 12 0.2417951 1.620306 NA NA NA
## 13 0.2991452 2.140462 1.36148443 -0.5817403 3.1614171
## 14 0.2991452 2.140462 -1.67379182 -3.4017494 0.1740751
## 15 0.2991452 2.140462 0.03511467 -1.3632231 1.2582515
## 16 0.2991452 2.140462 NA NA NA
## 17 0.2245380 1.633075 -0.79154671 -2.9553548 1.4009768
## 18 0.2245380 1.633075 0.74600484 -1.2664685 2.8105898
## 19 0.2245380 1.633075 0.71497532 -1.5351507 2.7675052
## 20 0.2245380 1.633075 NA NA NA
## mu_sample_1
## 1 0.46693373
## 2 -1.46753536
## 3 -0.70518676
## 4 1.17008175
## 5 0.04282238
## 6 -1.40962120
## 7 -0.25032157
## 8 -0.31859562
## 9 0.89235195
## 10 -0.62799374
## 11 0.93901306
## 12 0.08135199
## 13 0.14016578
## 14 -2.13921983
## 15 -0.48093000
## 16 -0.81479904
## 17 0.36546837
## 18 0.72509317
## 19 -0.50006873
## 20 -0.95798299
##
## $C
## metabolite.ID metabolite KEGG time.ID condition mu_mean
## 1 1 Fumarate C00122 1 C 0.27592444
## 2 1 Fumarate C00122 2 C 1.07523301
## 3 1 Fumarate C00122 3 C 0.15123438
## 4 1 Fumarate C00122 4 C 0.18467152
## 5 2 Glutathione disulfide C00127 1 C 0.44472869
## 6 2 Glutathione disulfide C00127 2 C 0.31449549
## 7 2 Glutathione disulfide C00127 3 C -0.04350955
## 8 2 Glutathione disulfide C00127 4 C 0.35311205
## 9 3 L-Alanine C00041 1 C -0.14545687
## 10 3 L-Alanine C00041 2 C 0.05337757
## 11 3 L-Alanine C00041 3 C 0.05314479
## 12 3 L-Alanine C00041 4 C -0.24473071
## 13 4 L-Cystine C00491 1 C -0.01378163
## 14 4 L-Cystine C00491 2 C 0.30314062
## 15 4 L-Cystine C00491 3 C 0.37594036
## 16 4 L-Cystine C00491 4 C -0.66936221
## 17 5 Malic acid C00711 1 C -0.91957409
## 18 5 Malic acid C00711 2 C -0.44241201
## 19 5 Malic acid C00711 3 C -0.30839846
## 20 5 Malic acid C00711 4 C -0.89780475
## mu_lower mu_higher sigma_mean sigma_lower sigma_higher lambda_mean
## 1 -1.5021622 1.9606075 1.6820874 0.70886561 3.8562733 0.7159342
## 2 -0.5861566 2.4746406 1.4166634 0.58477178 3.5442834 0.7159342
## 3 -1.1310053 1.3403221 1.0802607 0.37699307 3.0229764 0.7159342
## 4 -0.3282529 0.6808233 0.3894440 0.11710044 1.2414235 0.7159342
## 5 -0.5306571 1.4539917 0.8031273 0.27600974 2.3323091 0.9845320
## 6 -1.3322503 1.8542041 1.2728051 0.45743421 3.5015618 0.9845320
## 7 -0.6167804 0.4946363 0.3820217 0.10273647 1.4426430 0.9845320
## 8 -0.4514985 1.2423555 0.6223117 0.19013918 2.0082197 0.9845320
## 9 -2.0733745 1.8344366 2.1256654 1.03252619 4.4784396 0.7364765
## 10 -1.4692391 1.5856457 1.4380172 0.59556263 3.6439761 0.7364765
## 11 -1.3878681 1.3258428 1.0267256 0.35879328 3.0615396 0.7364765
## 12 -1.1702289 0.7260279 0.7988892 0.29768229 2.0416728 0.7364765
## 13 -1.9034126 1.8612551 1.8797586 0.85014602 4.3164311 0.9956428
## 14 -0.5084065 1.0749221 0.5775763 0.18163274 1.7884186 0.9956428
## 15 -1.3245432 2.1118398 1.5523136 0.64735879 3.6544058 0.9956428
## 16 -2.1254137 0.9370064 1.3947696 0.56620901 3.4714191 0.9956428
## 17 -1.6788695 -0.1499127 0.6082896 0.16765517 1.9421264 0.7518733
## 18 -1.9558267 1.0299530 1.5120890 0.66200478 3.4793159 0.7518733
## 19 -0.5071700 -0.1385862 0.1100378 0.02011536 0.5537664 0.7518733
## 20 -2.0474418 0.3406517 1.0628443 0.38379828 2.8970171 0.7518733
## lambda_lower lambda_higher delta_mu_mean delta_mu_lower delta_mu_higher
## 1 0.2201000 1.493357 0.03857105 -2.209765 2.4097538
## 2 0.2201000 1.493357 -0.26135071 -2.255121 1.8058503
## 3 0.2201000 1.493357 -0.72250699 -2.685069 1.4211071
## 4 0.2201000 1.493357 NA NA NA
## 5 0.3225775 2.171908 -1.11874256 -2.635944 0.7402668
## 6 0.3225775 2.171908 -0.20122116 -1.221962 0.9514902
## 7 0.3225775 2.171908 -0.67484339 -1.835740 0.5158585
## 8 0.3225775 2.171908 NA NA NA
## 9 0.2122780 1.636378 0.20187768 -1.315384 1.8297029
## 10 0.2122780 1.636378 -0.36689369 -2.437309 1.6141737
## 11 0.2122780 1.636378 -0.42863037 -2.745855 2.0185191
## 12 0.2122780 1.636378 NA NA NA
## 13 0.2697323 2.054569 -0.33012839 -2.323716 1.6744613
## 14 0.2697323 2.054569 0.44859750 -1.592626 2.7018405
## 15 0.2697323 2.054569 -0.61153908 -1.401522 0.2174718
## 16 0.2697323 2.054569 NA NA NA
## 17 0.2360951 1.636300 -0.39135112 -2.202220 1.3726906
## 18 0.2360951 1.636300 0.32256279 -1.852761 2.7000639
## 19 0.2360951 1.636300 -1.27374511 -3.279244 0.8190192
## 20 0.2360951 1.636300 NA NA NA
## mu_sample_1
## 1 -0.46052088
## 2 1.53883475
## 3 0.54596399
## 4 0.20713322
## 5 0.84556789
## 6 0.20584098
## 7 -0.17034331
## 8 0.41761906
## 9 1.00314490
## 10 0.53697709
## 11 0.30176119
## 12 -0.30854048
## 13 -0.57408023
## 14 0.09366546
## 15 -1.02871282
## 16 -1.59397261
## 17 -1.39063223
## 18 0.16195682
## 19 -0.54548685
## 20 -0.78808365
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 1.08 ===> 99% of the (truncated) height range in dendro.
## ..done.
## ..cutHeight not given, setting it to 2.04 ===> 99% of the (truncated) height range in dendro.
## ..done.
## ..cutHeight not given, setting it to 2.08 ===> 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.516 seconds (Warm-up)
## Chain 1: 0.867 seconds (Sampling)
## Chain 1: 1.383 seconds (Total)
## Chain 1:
##
## SAMPLING FOR MODEL 'm_cluster_distances_padded' NOW (CHAIN 2).
## Chain 2:
## Chain 2: Gradient evaluation took 2.2e-05 seconds
## Chain 2: 1000 transitions using 10 leapfrog steps per transition would take 0.22 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.7 seconds (Warm-up)
## Chain 2: 1.636 seconds (Sampling)
## Chain 2: 2.336 seconds (Total)
## Chain 2:
##
## SAMPLING FOR MODEL 'm_cluster_distances_padded' NOW (CHAIN 3).
## Chain 3:
## Chain 3: Gradient evaluation took 2e-05 seconds
## Chain 3: 1000 transitions using 10 leapfrog steps per transition would take 0.2 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.523 seconds (Warm-up)
## Chain 3: 1.851 seconds (Sampling)
## Chain 3: 2.374 seconds (Total)
## Chain 3:
##
## SAMPLING FOR MODEL 'm_cluster_distances_padded' NOW (CHAIN 4).
## Chain 4:
## Chain 4: Gradient evaluation took 2.1e-05 seconds
## Chain 4: 1000 transitions using 10 leapfrog steps per transition would take 0.21 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.639 seconds (Warm-up)
## Chain 4: 1.064 seconds (Sampling)
## Chain 4: 1.703 seconds (Total)
## Chain 4:
## Warning: There were 624 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: Bulk Effective Samples Size (ESS) is too low, indicating posterior means and medians may be unreliable.
## Running the chains for more iterations may help. See
## https://mc-stan.org/misc/warnings.html#bulk-ess
## 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-apple-darwin20
## Running under: macOS Monterey 12.7.6
##
## Matrix products: default
## BLAS: /Library/Frameworks/R.framework/Versions/4.5-x86_64/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/4.5-x86_64/Resources/lib/libRlapack.dylib; LAPACK version 3.12.1
##
## locale:
## [1] C/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
##
## time zone: America/New_York
## tzcode source: internal
##
## 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 colorspace_2.1-1
## [43] cli_3.6.5 SparseArray_1.9.1 magrittr_2.0.3
## [46] loo_2.8.0 S4Arrays_1.9.1 utf8_1.2.6
## [49] dichromat_2.0-0.1 pkgbuild_1.4.8 dynamicTreeCut_1.63-1
## [52] withr_3.0.2 scales_1.4.0 rmarkdown_2.29
## [55] XVector_0.49.0 gridExtra_2.3 evaluate_1.0.5
## [58] knitr_1.50 V8_7.0.0 viridisLite_0.4.2
## [61] rstantools_2.5.0 rlang_1.1.6 Rcpp_1.1.0
## [64] dendextend_1.19.1 glue_1.8.0 BiocManager_1.30.26
## [67] jsonlite_2.0.0 R6_2.6.1