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 Glutathione disulf… A 1 1 42.2 1.63 -1.88 C001…
## 2 Glutathione disulf… A 1 2 262. 2.42 0.532 C001…
## 3 Glutathione disulf… A 1 3 761. 2.88 1.94 C001…
## 4 Glutathione disulf… A 2 1 331. 2.52 0.839 C001…
## 5 Glutathione disulf… A 2 2 91.7 1.96 -0.855 C001…
## 6 Glutathione disulf… A 2 3 165. 2.22 -0.0760 C001…
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 4.8e-05 seconds
## Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 0.48 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.593 seconds (Warm-up)
## Chain 1: 1.001 seconds (Sampling)
## Chain 1: 1.594 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.657 seconds (Warm-up)
## Chain 1: 1.136 seconds (Sampling)
## Chain 1: 1.793 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.529 seconds (Warm-up)
## Chain 1: 0.99 seconds (Sampling)
## Chain 1: 1.519 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
## 1 1 DL-Glycerol 1-phosphate C00093 1 A 0.58280431
## 2 1 DL-Glycerol 1-phosphate C00093 2 A 0.06488229
## 3 1 DL-Glycerol 1-phosphate C00093 3 A 0.07812052
## 4 1 DL-Glycerol 1-phosphate C00093 4 A -0.09385724
## 5 2 Glutathione disulfide C00127 1 A -0.72560340
## 6 2 Glutathione disulfide C00127 2 A -0.25849127
## 7 2 Glutathione disulfide C00127 3 A -0.01723341
## 8 2 Glutathione disulfide C00127 4 A -0.38407193
## 9 3 L-Leucine C00123 1 A -0.53433981
## 10 3 L-Leucine C00123 2 A 0.74229410
## 11 3 L-Leucine C00123 3 A -0.10037364
## 12 3 L-Leucine C00123 4 A -0.22482259
## 13 4 L-Valine C00183 1 A -0.49039358
## 14 4 L-Valine C00183 2 A 0.38688950
## 15 4 L-Valine C00183 3 A -0.07062660
## 16 4 L-Valine C00183 4 A -0.29240506
## 17 5 Orotidine C00438 1 A 0.06723015
## 18 5 Orotidine C00438 2 A 0.64887224
## 19 5 Orotidine C00438 3 A 0.37373376
## 20 5 Orotidine C00438 4 A 0.14669103
## mu_lower mu_higher sigma_mean sigma_lower sigma_higher lambda_mean
## 1 -1.4486330 2.3162407 1.7133714 0.72516282 4.1342517 0.7642684
## 2 -2.2712448 2.1642494 2.1678476 0.95520388 5.0614059 0.7642684
## 3 -1.4118769 1.6147970 1.3699065 0.56820739 3.1427065 0.7642684
## 4 -0.9691672 0.9377534 0.8102001 0.27511749 2.2817131 0.7642684
## 5 -2.5005014 1.4608183 1.7566875 0.75956763 4.3479511 0.7032265
## 6 -1.8060961 1.5049094 1.3697755 0.52151853 3.6039968 0.7032265
## 7 -1.6057284 1.4878879 1.2188299 0.47699295 3.1801635 0.7032265
## 8 -0.9133949 0.1915896 0.4139199 0.12508828 1.4621769 0.7032265
## 9 -2.4987088 1.6060479 2.0305981 0.97623633 4.5335546 0.8971195
## 10 -0.4415359 1.8173647 0.9952480 0.39384495 2.4598281 0.8971195
## 11 -0.6867290 0.5189393 0.3746731 0.09134105 1.4979655 0.8971195
## 12 -1.2387505 0.9103215 0.8729658 0.28304590 2.5236462 0.8971195
## 13 -0.6834119 -0.3323389 0.1225994 0.02609763 0.5204868 0.8330885
## 14 -0.4314307 1.1772215 0.5944723 0.17384937 1.8821619 0.8330885
## 15 -1.1377646 0.8140928 0.6320492 0.18994109 2.0957563 0.8330885
## 16 -1.7806211 1.3830360 1.4926099 0.61356501 3.4928453 0.8330885
## 17 -1.4003040 1.4467510 1.1691460 0.44080233 3.0597800 0.8584937
## 18 -1.2959928 2.4643880 1.8609795 0.78013137 4.5470299 0.8584937
## 19 -0.7606083 1.4656364 0.7929409 0.25291515 2.5454705 0.8584937
## 20 -0.7762188 1.0632636 0.6687731 0.20014812 2.2467406 0.8584937
## lambda_lower lambda_higher delta_mu_mean delta_mu_lower delta_mu_higher
## 1 0.2284094 1.666214 -0.84129558 -3.1198853 1.6232128
## 2 0.2284094 1.666214 0.15811763 -1.7489022 1.8453196
## 3 0.2284094 1.666214 -0.19203143 -1.7829141 1.4623123
## 4 0.2284094 1.666214 NA NA NA
## 5 0.2060031 1.532138 -0.08211570 -2.7084277 2.6058697
## 6 0.2060031 1.532138 -0.20758918 -2.0039704 1.7189918
## 7 0.2060031 1.532138 0.29205273 -1.5523069 2.0074332
## 8 0.2060031 1.532138 NA NA NA
## 9 0.2729157 2.006630 -0.46219245 -2.0945794 1.1698303
## 10 0.2729157 2.006630 -0.10632164 -0.7483904 0.4156927
## 11 0.2729157 2.006630 1.13926582 -0.7939618 2.9353804
## 12 0.2729157 2.006630 NA NA NA
## 13 0.2472657 1.834282 -0.44048257 -2.6122606 1.7994294
## 14 0.2472657 1.834282 0.92122931 -1.2623362 2.9042317
## 15 0.2472657 1.834282 -0.01315573 -1.3385801 1.4324750
## 16 0.2472657 1.834282 NA NA NA
## 17 0.2542902 1.853758 1.46789749 -0.9743250 3.5496351
## 18 0.2542902 1.853758 -0.81292070 -2.3855239 0.5487503
## 19 0.2542902 1.853758 0.21731763 -0.9513807 1.5658688
## 20 0.2542902 1.853758 NA NA NA
## mu_sample_1
## 1 -0.647882813
## 2 1.060601540
## 3 0.259340249
## 4 -0.225997041
## 5 -0.552563160
## 6 1.907138227
## 7 -0.249218695
## 8 -0.522595132
## 9 -0.223793506
## 10 0.849511932
## 11 -0.046084154
## 12 0.006892123
## 13 -0.518539493
## 14 0.286866931
## 15 0.154092802
## 16 -0.373177360
## 17 -1.309253848
## 18 0.648377949
## 19 0.220146766
## 20 1.244219377
##
## $B
## metabolite.ID metabolite KEGG time.ID condition mu_mean
## 1 1 DL-Glycerol 1-phosphate C00093 1 B -0.79963624
## 2 1 DL-Glycerol 1-phosphate C00093 2 B -0.59038080
## 3 1 DL-Glycerol 1-phosphate C00093 3 B -1.45744164
## 4 1 DL-Glycerol 1-phosphate C00093 4 B 0.21532518
## 5 2 Glutathione disulfide C00127 1 B 0.44523838
## 6 2 Glutathione disulfide C00127 2 B 0.44720088
## 7 2 Glutathione disulfide C00127 3 B 0.04956215
## 8 2 Glutathione disulfide C00127 4 B 0.49228738
## 9 3 L-Leucine C00123 1 B -0.30827453
## 10 3 L-Leucine C00123 2 B -0.87958581
## 11 3 L-Leucine C00123 3 B 0.35766217
## 12 3 L-Leucine C00123 4 B 0.14718466
## 13 4 L-Valine C00183 1 B 0.59155912
## 14 4 L-Valine C00183 2 B -0.08080801
## 15 4 L-Valine C00183 3 B 0.05055501
## 16 4 L-Valine C00183 4 B 0.20040384
## 17 5 Orotidine C00438 1 B 0.59372098
## 18 5 Orotidine C00438 2 B 0.42443656
## 19 5 Orotidine C00438 3 B 0.21937773
## 20 5 Orotidine C00438 4 B 0.26805789
## mu_lower mu_higher sigma_mean sigma_lower sigma_higher lambda_mean
## 1 -2.42444034 1.1674260 1.7337327 0.76313522 4.1270284 0.9208298
## 2 -2.35584824 1.3886037 1.7484272 0.75206350 4.1243089 0.9208298
## 3 -2.42741697 -0.2415358 0.8250538 0.31943439 2.1443299 0.9208298
## 4 -1.57269073 1.7956153 1.3953942 0.55822224 3.5213959 0.9208298
## 5 -1.33699484 2.2082860 1.7457913 0.75966321 4.0211413 0.7976755
## 6 -0.38070050 1.1767672 0.5762292 0.17082409 1.9284029 0.7976755
## 7 -1.63755317 1.5256414 1.4015853 0.57335360 3.3773778 0.7976755
## 8 0.07891832 1.0133967 0.2883399 0.07051579 0.9680572 0.7976755
## 9 -2.15150309 1.6398689 1.8253933 0.82554570 4.0578625 1.2384400
## 10 -1.81159975 0.1625004 0.7286010 0.22341046 2.2730631 1.2384400
## 11 -0.02383300 0.7112478 0.2350841 0.05757462 0.8226890 1.2384400
## 12 -1.01711278 1.3141745 0.9628398 0.36532843 2.5812790 1.2384400
## 13 0.29259768 0.9356670 0.2043898 0.04829849 0.8052479 0.7486459
## 14 -0.46302074 0.4473128 0.2812630 0.06242975 1.2318771 0.7486459
## 15 -1.51995631 1.5141269 1.3415985 0.55294648 3.2973153 0.7486459
## 16 -1.22036996 1.6452035 1.2008761 0.47523386 3.0700606 0.7486459
## 17 -0.17389894 1.4840840 0.5135459 0.13869780 1.6790509 0.8566147
## 18 -0.70884743 1.4493709 0.8759859 0.33176200 2.4735081 0.8566147
## 19 -1.52639659 2.0957428 1.5235621 0.57289151 3.7848011 0.8566147
## 20 -0.16594103 0.7101815 0.3485829 0.09466082 1.2378899 0.8566147
## lambda_lower lambda_higher delta_mu_mean delta_mu_lower delta_mu_higher
## 1 0.2630974 2.058438 1.24683712 -0.7701403 3.0360266
## 2 0.2630974 2.058438 -0.08953872 -0.9480920 0.8298713
## 3 0.2630974 2.058438 -0.15725833 -1.5735433 1.3290986
## 4 0.2630974 2.058438 NA NA NA
## 5 0.2329316 1.711791 0.63994295 -2.0382592 2.8635100
## 6 0.2329316 1.711791 0.09762252 -1.7675083 2.1058628
## 7 0.2329316 1.711791 0.44653631 -0.8873073 1.8965306
## 8 0.2329316 1.711791 NA NA NA
## 9 0.3874741 2.619810 1.94972902 0.6966516 3.0281576
## 10 0.3874741 2.619810 0.09927174 -0.5600777 0.7149302
## 11 0.3874741 2.619810 -0.16712256 -1.3189546 0.8506131
## 12 0.3874741 2.619810 NA NA NA
## 13 0.2146511 1.655052 -0.52359971 -2.6868784 1.9683634
## 14 0.2146511 1.655052 0.22746652 -1.7335761 2.1745776
## 15 0.2146511 1.655052 0.30018574 -1.6392821 2.2296147
## 16 0.2146511 1.655052 NA NA NA
## 17 0.2560938 1.801493 -1.32482419 -3.2571805 0.5805644
## 18 0.2560938 1.801493 0.93014082 -0.9199687 2.6268351
## 19 0.2560938 1.801493 0.21750288 -1.2738345 1.8350765
## 20 0.2560938 1.801493 NA NA NA
## mu_sample_1
## 1 -1.46490545
## 2 -0.21518048
## 3 -1.10274327
## 4 0.35751586
## 5 1.32434244
## 6 -0.25478313
## 7 -0.47080714
## 8 0.44967940
## 9 -0.40940374
## 10 -0.22926024
## 11 0.32413611
## 12 0.31555894
## 13 0.55425898
## 14 -0.08414911
## 15 -0.54119427
## 16 1.59510899
## 17 0.43371493
## 18 -0.62190692
## 19 0.77565854
## 20 -0.20818655
##
## $C
## metabolite.ID metabolite KEGG time.ID condition mu_mean
## 1 1 DL-Glycerol 1-phosphate C00093 1 C -1.22944500
## 2 1 DL-Glycerol 1-phosphate C00093 2 C 1.04721176
## 3 1 DL-Glycerol 1-phosphate C00093 3 C -0.08581585
## 4 1 DL-Glycerol 1-phosphate C00093 4 C 0.01695643
## 5 2 Glutathione disulfide C00127 1 C -0.94967279
## 6 2 Glutathione disulfide C00127 2 C 0.59794468
## 7 2 Glutathione disulfide C00127 3 C -0.06012923
## 8 2 Glutathione disulfide C00127 4 C -0.00810129
## 9 3 L-Leucine C00123 1 C -0.21461805
## 10 3 L-Leucine C00123 2 C 0.49517937
## 11 3 L-Leucine C00123 3 C 0.30058347
## 12 3 L-Leucine C00123 4 C -0.22199270
## 13 4 L-Valine C00183 1 C 0.14389327
## 14 4 L-Valine C00183 2 C 0.20469608
## 15 4 L-Valine C00183 3 C 0.68530171
## 16 4 L-Valine C00183 4 C 0.37534350
## 17 5 Orotidine C00438 1 C -0.89201407
## 18 5 Orotidine C00438 2 C 0.04786994
## 19 5 Orotidine C00438 3 C 0.10562420
## 20 5 Orotidine C00438 4 C -0.39628823
## mu_lower mu_higher sigma_mean sigma_lower sigma_higher lambda_mean
## 1 -2.4939629 0.53363449 1.1590408 0.43395287 3.2043021 0.9577328
## 2 -0.5639653 2.42717908 1.3566138 0.58647015 3.3406752 0.9577328
## 3 -1.7360335 1.68224753 1.5516795 0.66405326 3.5200963 0.9577328
## 4 -0.5180678 0.62278001 0.3908783 0.10285468 1.3319698 0.9577328
## 5 -1.5129304 -0.47272586 0.2939304 0.06475101 1.1930802 1.0054083
## 6 -0.3976402 1.41283325 0.6557565 0.21851646 1.9839671 1.0054083
## 7 -0.7366479 0.49189763 0.3749294 0.10152535 1.2240570 1.0054083
## 8 -0.6055499 0.84259953 0.4750782 0.12374119 1.8621303 1.0054083
## 9 -2.1865689 2.01961572 2.1974068 1.02726374 4.9948626 0.8300323
## 10 -1.0008740 1.83521001 1.2418232 0.50831750 3.1987664 0.8300323
## 11 -0.4284997 0.86708988 0.4640008 0.13756634 1.5238795 0.8300323
## 12 -1.1986529 0.77040081 0.8278670 0.29174481 2.2732368 0.8300323
## 13 -0.1937262 0.52927966 0.2425559 0.05293340 0.9914029 0.9902325
## 14 -0.5697625 0.83488608 0.4783524 0.13853210 1.5942836 0.9902325
## 15 -0.9508368 2.31536603 1.4112044 0.61969461 3.2541334 0.9902325
## 16 -0.8908927 1.66874284 1.1070087 0.42409466 2.7596509 0.9902325
## 17 -1.6126552 -0.03313633 0.5962422 0.17835442 1.9296162 0.9762891
## 18 -1.9120614 2.11927933 2.0070351 0.93869085 4.3915485 0.9762891
## 19 -0.1766481 0.36356650 0.2023534 0.05087415 0.7110606 0.9762891
## 20 -0.9171336 0.29799177 0.3895959 0.09768453 1.4707159 0.9762891
## lambda_lower lambda_higher delta_mu_mean delta_mu_lower delta_mu_higher
## 1 0.2928328 2.046725 1.82738968 -0.09138132 3.2967546
## 2 0.2928328 2.046725 -0.29736121 -1.39118037 0.8428419
## 3 0.2928328 2.046725 0.07476003 -1.31294964 1.5030791
## 4 0.2928328 2.046725 NA NA NA
## 5 0.2898725 2.223308 -1.10734100 -2.62968332 0.5637059
## 6 0.2898725 2.223308 -0.16186347 -1.34631428 1.0951916
## 7 0.2898725 2.223308 -0.67002137 -2.03083185 0.6395782
## 8 0.2898725 2.223308 NA NA NA
## 9 0.2506397 1.840672 0.07771456 -1.78153027 2.0709538
## 10 0.2506397 1.840672 0.15199456 -0.80243963 0.9220487
## 11 0.2506397 1.840672 -0.09602333 -2.05152231 1.9695158
## 12 0.2506397 1.840672 NA NA NA
## 13 0.2832993 2.238814 -0.23157449 -2.30478374 2.0715863
## 14 0.2832993 2.238814 0.41931413 -1.85373253 2.4969774
## 15 0.2832993 2.238814 -0.09907188 -0.76769272 0.6681810
## 16 0.2832993 2.238814 NA NA NA
## 17 0.2821948 2.181532 1.44485217 -0.08480332 2.8860805
## 18 0.2821948 2.181532 0.19012234 -1.91635307 2.3332778
## 19 0.2821948 2.181532 -1.08158993 -2.80510192 0.8105170
## 20 0.2821948 2.181532 NA NA NA
## mu_sample_1
## 1 -0.240004807
## 2 1.543270559
## 3 0.952409184
## 4 0.044361258
## 5 -0.804726395
## 6 0.520922129
## 7 -0.076107180
## 8 0.288379147
## 9 -0.626573055
## 10 0.189752946
## 11 0.267857382
## 12 -0.436809510
## 13 0.053426639
## 14 0.256148357
## 15 1.292816210
## 16 0.203754507
## 17 -1.213011953
## 18 -0.302450106
## 19 0.309104079
## 20 -0.007229374
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.55 ===> 99% of the (truncated) height range in dendro.
## ..done.
## ..cutHeight not given, setting it to 2.58 ===> 99% of the (truncated) height range in dendro.
## ..done.
## ..cutHeight not given, setting it to 1.77 ===> 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 `svd()`:
## ! infinite or missing values in 'x'
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.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.267 seconds (Warm-up)
## Chain 1: 0.572 seconds (Sampling)
## Chain 1: 0.839 seconds (Total)
## Chain 1:
##
## SAMPLING FOR MODEL 'm_cluster_distances_padded' NOW (CHAIN 2).
## Chain 2:
## Chain 2: Gradient evaluation took 4.2e-05 seconds
## Chain 2: 1000 transitions using 10 leapfrog steps per transition would take 0.42 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.501 seconds (Warm-up)
## Chain 2: 0.521 seconds (Sampling)
## Chain 2: 1.022 seconds (Total)
## Chain 2:
##
## SAMPLING FOR MODEL 'm_cluster_distances_padded' NOW (CHAIN 3).
## Chain 3:
## Chain 3: Gradient evaluation took 3.9e-05 seconds
## Chain 3: 1000 transitions using 10 leapfrog steps per transition would take 0.39 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.25 seconds (Warm-up)
## Chain 3: 0.466 seconds (Sampling)
## Chain 3: 0.716 seconds (Total)
## Chain 3:
##
## SAMPLING FOR MODEL 'm_cluster_distances_padded' NOW (CHAIN 4).
## Chain 4:
## Chain 4: Gradient evaluation took 4.1e-05 seconds
## Chain 4: 1000 transitions using 10 leapfrog steps per transition would take 0.41 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.286 seconds (Warm-up)
## Chain 4: 0.838 seconds (Sampling)
## Chain 4: 1.124 seconds (Total)
## Chain 4:
## Warning: There were 80 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 ucrt)
## Platform: x86_64-w64-mingw32/x64
## Running under: Windows Server 2022 x64 (build 20348)
##
## Matrix products: default
## LAPACK version 3.12.1
##
## locale:
## [1] LC_COLLATE=C
## [2] LC_CTYPE=English_United States.utf8
## [3] LC_MONETARY=English_United States.utf8
## [4] LC_NUMERIC=C
## [5] LC_TIME=English_United States.utf8
##
## 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.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