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 dGMP A 1 1 348. 2.54 0.102 C00362
## 2 dGMP A 1 2 212. 2.33 -0.343 C00362
## 3 dGMP A 1 3 477. 2.68 0.387 C00362
## 4 dGMP A 2 1 114. 2.06 -0.901 C00362
## 5 dGMP A 2 2 349. 2.54 0.105 C00362
## 6 dGMP A 2 3 4192. 3.62 2.34 C00362
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 1.3e-05 seconds
## Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 0.13 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.133 seconds (Warm-up)
## Chain 1: 0.259 seconds (Sampling)
## Chain 1: 0.392 seconds (Total)
## Chain 1:
##
## SAMPLING FOR MODEL 'm_ANOVA_partial_pooling' NOW (CHAIN 1).
## Chain 1:
## Chain 1: Gradient evaluation took 1.3e-05 seconds
## Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 0.13 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.106 seconds (Warm-up)
## Chain 1: 0.29 seconds (Sampling)
## Chain 1: 0.396 seconds (Total)
## Chain 1:
##
## SAMPLING FOR MODEL 'm_ANOVA_partial_pooling' NOW (CHAIN 1).
## Chain 1:
## Chain 1: Gradient evaluation took 1.2e-05 seconds
## Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 0.12 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.126 seconds (Warm-up)
## Chain 1: 0.348 seconds (Sampling)
## Chain 1: 0.474 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 mu_lower
## 1 1 Adenosine C00212 1 A 0.30970547 -0.5503186
## 2 1 Adenosine C00212 2 A -0.13747892 -1.3172716
## 3 1 Adenosine C00212 3 A 0.03059028 -0.3362077
## 4 1 Adenosine C00212 4 A 0.78367856 -1.1315279
## 5 2 L-Cysteine C00097 1 A 0.07356471 -0.7774618
## 6 2 L-Cysteine C00097 2 A 0.54791342 -1.4007907
## 7 2 L-Cysteine C00097 3 A 0.21981160 -1.2360899
## 8 2 L-Cysteine C00097 4 A -0.72094752 -2.5215554
## 9 3 L-Lysine C00047 1 A -0.31258200 -1.3032577
## 10 3 L-Lysine C00047 2 A 0.37648482 -1.5420112
## 11 3 L-Lysine C00047 3 A -0.06407601 -1.1814128
## 12 3 L-Lysine C00047 4 A 0.02021058 -0.6416068
## 13 4 Palmitic acid C00249 1 A 0.75298875 -0.2163331
## 14 4 Palmitic acid C00249 2 A -0.15319853 -1.4336880
## 15 4 Palmitic acid C00249 3 A -0.58732522 -1.6061669
## 16 4 Palmitic acid C00249 4 A -0.81777696 -2.1308277
## 17 5 dGMP C00362 1 A -0.08930665 -2.1993770
## 18 5 dGMP C00362 2 A 0.11736566 -0.4104451
## 19 5 dGMP C00362 3 A -0.44552632 -1.5114116
## 20 5 dGMP C00362 4 A 0.08118506 -1.7093005
## mu_higher sigma_mean sigma_lower sigma_higher lambda_mean lambda_lower
## 1 1.1559031 0.7217810 0.23766006 1.984317 0.8044951 0.2638209
## 2 1.0708719 0.9177621 0.30434460 2.526925 0.8044951 0.2638209
## 3 0.4590154 0.2346118 0.05185789 1.054305 0.8044951 0.2638209
## 4 2.4826240 1.7883248 0.77740157 4.027047 0.8044951 0.2638209
## 5 1.1219820 0.7042977 0.21318155 2.245528 0.7758048 0.2422937
## 6 2.2082778 1.7284846 0.77506559 3.878994 0.7758048 0.2422937
## 7 1.5834777 1.3422504 0.55137452 3.581704 0.7758048 0.2422937
## 8 1.3388856 1.8125422 0.89251547 3.881518 0.7758048 0.2422937
## 9 0.9783903 0.7413337 0.20722262 2.654352 1.0209234 0.3385195
## 10 2.1365107 1.9164284 0.83091434 4.416640 1.0209234 0.3385195
## 11 1.1109034 0.9574499 0.34253509 2.891304 1.0209234 0.3385195
## 12 0.5987767 0.4342412 0.11738403 1.624087 1.0209234 0.3385195
## 13 1.6992810 0.7284816 0.25688549 2.072649 0.8049654 0.2395935
## 14 1.1903124 1.1298519 0.44602447 2.706649 0.8049654 0.2395935
## 15 0.6761020 0.8390722 0.27838759 2.522060 0.8049654 0.2395935
## 16 0.7479626 1.1111626 0.40306036 3.197853 0.8049654 0.2395935
## 17 1.9429837 2.1025652 0.98632355 4.625866 0.7447112 0.2097122
## 18 0.6435627 0.4062346 0.11809334 1.465972 0.7447112 0.2097122
## 19 0.6815209 0.8345535 0.28086345 2.456217 0.7447112 0.2097122
## 20 1.7845127 1.5180580 0.59121429 3.547474 0.7447112 0.2097122
## lambda_higher delta_mu_mean delta_mu_lower delta_mu_higher mu_sample_1
## 1 1.700597 0.2382080 -1.8269225 2.1426124 0.183277554
## 2 1.700597 -0.6119894 -2.6037405 1.4920311 -0.385450854
## 3 1.700597 -0.7537009 -2.4526284 1.2307009 -0.005613175
## 4 1.700597 NA NA NA 1.040391039
## 5 1.662509 0.3572905 -1.5003619 2.1981528 -0.117521164
## 6 1.662509 -0.1996010 -1.8321417 1.4316892 1.021527170
## 7 1.662509 -0.1095172 -2.2689775 2.1648557 0.507608756
## 8 1.662509 NA NA NA -0.963280397
## 9 2.168243 -0.7515378 -2.7197723 1.4006041 -0.046752120
## 10 2.168243 1.4739363 -0.6202577 3.5320143 -0.171564449
## 11 2.168243 -0.6356231 -1.6681060 0.5136139 -0.499336751
## 12 2.168243 NA NA NA 0.311463877
## 13 1.765807 -1.0962606 -3.1425606 1.1238065 0.630459283
## 14 1.765807 0.1593835 -1.7175308 1.9235285 -0.852727769
## 15 1.765807 -0.2923278 -1.9393869 1.4286543 -0.535339453
## 16 1.765807 NA NA NA -0.782303753
## 17 1.655655 0.3029201 -2.0064158 2.2687673 -0.738845390
## 18 1.655655 -0.9638100 -2.9775985 1.3951018 -0.059093078
## 19 1.655655 0.6685103 -1.5093220 2.5254066 -0.568568461
## 20 1.655655 NA NA NA -0.140310440
##
## $B
## metabolite.ID metabolite KEGG time.ID condition mu_mean mu_lower
## 1 1 Adenosine C00212 1 B -0.60927417 -1.5947714
## 2 1 Adenosine C00212 2 B 0.33956399 -1.2224401
## 3 1 Adenosine C00212 3 B 0.01926977 -0.4789315
## 4 1 Adenosine C00212 4 B -0.61725043 -2.4487353
## 5 2 L-Cysteine C00097 1 B 0.53674508 -0.8527761
## 6 2 L-Cysteine C00097 2 B -0.42059482 -1.7649336
## 7 2 L-Cysteine C00097 3 B -1.03808339 -2.4301819
## 8 2 L-Cysteine C00097 4 B -0.81324803 -2.1520506
## 9 3 L-Lysine C00047 1 B 0.38200694 -0.7764247
## 10 3 L-Lysine C00047 2 B -1.21499073 -2.5380407
## 11 3 L-Lysine C00047 3 B 0.20447080 -1.0592263
## 12 3 L-Lysine C00047 4 B 0.11128278 -1.0460898
## 13 4 Palmitic acid C00249 1 B 0.58352554 -0.9971477
## 14 4 Palmitic acid C00249 2 B 0.11085013 -1.5802703
## 15 4 Palmitic acid C00249 3 B 0.33120019 -0.1505663
## 16 4 Palmitic acid C00249 4 B 0.72542464 -1.0420711
## 17 5 dGMP C00362 1 B 0.62056271 -0.6083438
## 18 5 dGMP C00362 2 B 0.29007161 -1.1745125
## 19 5 dGMP C00362 3 B 0.22589166 -0.7157864
## 20 5 dGMP C00362 4 B 0.41072484 -0.3785056
## mu_higher sigma_mean sigma_lower sigma_higher lambda_mean lambda_lower
## 1 0.6016182 0.8738054 0.29338396 2.557622 0.7726454 0.2384358
## 2 1.7217631 1.2374016 0.50169359 3.229231 0.7726454 0.2384358
## 3 0.6142169 0.3391908 0.07665979 1.251577 0.7726454 0.2384358
## 4 1.5035223 1.9329065 0.84229424 4.775920 0.7726454 0.2384358
## 5 1.8773325 1.1264567 0.46619251 2.696388 0.7994001 0.2106618
## 6 1.1418783 1.2302699 0.46206841 3.044030 0.7994001 0.2106618
## 7 0.5910848 1.3343672 0.52609624 3.537058 0.7994001 0.2106618
## 8 1.0446121 1.4266141 0.58708719 3.751228 0.7994001 0.2106618
## 9 1.3443366 0.7906056 0.25663014 2.327680 0.8169305 0.2224747
## 10 0.3043917 1.2139580 0.46198877 3.010382 0.8169305 0.2224747
## 11 1.5309207 1.0931728 0.41444417 2.937192 0.8169305 0.2224747
## 12 1.2466490 1.0363798 0.36011383 2.944742 0.8169305 0.2224747
## 13 2.0819918 1.3399854 0.56386002 3.076258 0.7636799 0.2179174
## 14 1.7542548 1.3604482 0.53172272 3.397176 0.7636799 0.2179174
## 15 0.7497501 0.2850310 0.06359016 1.228389 0.7636799 0.2179174
## 16 2.3629857 1.5949325 0.61824629 4.076668 0.7636799 0.2179174
## 17 1.8244852 0.9892199 0.33141467 2.700792 0.9902507 0.2761602
## 18 1.7348790 1.2777881 0.50669520 3.184101 0.9902507 0.2761602
## 19 1.1030594 0.7795615 0.26705885 2.300499 0.9902507 0.2761602
## 20 1.3439670 0.6830638 0.23855454 1.811709 0.9902507 0.2761602
## lambda_higher delta_mu_mean delta_mu_lower delta_mu_higher mu_sample_1
## 1 1.661099 0.18867935 -1.49291695 1.9808695 -1.25197632
## 2 1.661099 0.62506562 -1.22539441 2.5195528 0.67986944
## 3 1.661099 0.52095383 -1.75252458 2.5238230 0.10283291
## 4 1.661099 NA NA NA -1.51858652
## 5 1.788427 -1.37764737 -3.28234377 0.7702477 0.52861933
## 6 1.788427 1.14936616 -0.71331807 2.8326125 -4.79094935
## 7 1.788427 0.50927994 -1.15133043 2.1859646 -0.36165283
## 8 1.788427 NA NA NA -2.04682602
## 9 1.806575 -0.83251780 -2.36869613 1.0619334 0.09285952
## 10 1.806575 1.39677357 -0.88672872 3.3068489 -0.58953552
## 11 1.806575 -0.29345393 -2.37247781 1.6987843 0.23202791
## 12 1.806575 NA NA NA 0.30934270
## 13 1.647294 0.99925737 -1.32694355 3.1307628 0.89058191
## 14 1.647294 -0.27115681 -2.13840541 1.5727521 0.05182052
## 15 1.647294 0.11504153 -1.59901486 1.9435459 0.31285153
## 16 1.647294 NA NA NA 1.31922392
## 17 2.120178 -1.75173581 -3.62635118 0.3896777 0.53089322
## 18 2.120178 1.54619092 -0.02148281 2.8747415 0.23967151
## 19 2.120178 0.07952465 -0.77425682 1.2353727 0.22875180
## 20 2.120178 NA NA NA 0.17094430
##
## $C
## metabolite.ID metabolite KEGG time.ID condition mu_mean
## 1 1 Adenosine C00212 1 C 0.3029063187
## 2 1 Adenosine C00212 2 C 0.4155788595
## 3 1 Adenosine C00212 3 C 0.1293367749
## 4 1 Adenosine C00212 4 C 0.4989880162
## 5 2 L-Cysteine C00097 1 C -0.1307490685
## 6 2 L-Cysteine C00097 2 C 0.1719804885
## 7 2 L-Cysteine C00097 3 C 0.3384795709
## 8 2 L-Cysteine C00097 4 C -0.5710811164
## 9 3 L-Lysine C00047 1 C -0.0005215994
## 10 3 L-Lysine C00047 2 C -0.2549485988
## 11 3 L-Lysine C00047 3 C -0.6474912612
## 12 3 L-Lysine C00047 4 C -0.1714491489
## 13 4 Palmitic acid C00249 1 C 0.1466624997
## 14 4 Palmitic acid C00249 2 C -0.7099261279
## 15 4 Palmitic acid C00249 3 C 0.6455242453
## 16 4 Palmitic acid C00249 4 C 0.1540056575
## 17 5 dGMP C00362 1 C -0.6865504591
## 18 5 dGMP C00362 2 C 0.3940116114
## 19 5 dGMP C00362 3 C 0.2597134270
## 20 5 dGMP C00362 4 C -0.2252527268
## mu_lower mu_higher sigma_mean sigma_lower sigma_higher lambda_mean
## 1 -0.3652993 0.8364102 0.3858232 0.10185817 1.438698 0.7860146
## 2 -0.1058433 0.9397750 0.3503744 0.09115821 1.194073 0.7860146
## 3 -0.8097132 1.0353158 0.7862109 0.26807968 2.189366 0.7860146
## 4 -1.3268289 2.2216100 1.7574289 0.80471781 4.009966 0.7860146
## 5 -0.8750532 0.6661626 0.6389317 0.20240821 1.963349 0.8985605
## 6 -1.1481343 1.3820332 1.0763532 0.38831379 3.028855 0.8985605
## 7 -1.7856644 2.1158637 1.9804027 0.93462148 4.333014 0.8985605
## 8 -2.0516013 1.1272362 1.4642442 0.60983708 3.443033 0.8985605
## 9 -0.6831816 0.8301193 0.5362825 0.15710064 1.716115 0.8014859
## 10 -2.3629701 1.8672097 2.0977075 0.96027476 4.646072 0.8014859
## 11 -2.0242423 0.9455020 1.3636861 0.53657203 3.419890 0.8014859
## 12 -1.0583842 0.7271123 0.5833731 0.16694845 1.942021 0.8014859
## 13 -1.7558552 2.0754312 1.8825106 0.83446200 4.441976 0.8548624
## 14 -2.2614959 1.0374196 1.4449090 0.55902420 3.837074 0.8548624
## 15 -0.6007398 1.7977822 0.9973387 0.36844343 2.679457 0.8548624
## 16 -1.7708997 1.9822256 1.8548691 0.80787145 4.467180 0.8548624
## 17 -1.7036705 0.4029051 0.8744097 0.30232055 2.400561 0.8519487
## 18 -0.1234366 0.8911437 0.3585337 0.09466150 1.293727 0.8519487
## 19 -0.2372376 0.6927719 0.2871989 0.07741671 1.052195 0.8519487
## 20 -0.7761283 0.3870517 0.4191268 0.12149673 1.365289 0.8519487
## lambda_lower lambda_higher delta_mu_mean delta_mu_lower delta_mu_higher
## 1 0.2348683 1.770529 -0.13092583 -1.5549301 1.1856244
## 2 0.2348683 1.770529 -0.81947175 -2.7026297 1.3791793
## 3 0.2348683 1.770529 0.80149692 -1.8619611 3.1582097
## 4 0.2348683 1.770529 NA NA NA
## 5 0.2741008 1.926508 -0.07709929 -2.3936067 1.9784929
## 6 0.2741008 1.926508 -0.50992872 -2.5348005 1.9107193
## 7 0.2741008 1.926508 -0.51510131 -1.8805140 0.9326630
## 8 0.2741008 1.926508 NA NA NA
## 9 0.2299930 1.725590 -0.70041789 -2.5082069 1.2310483
## 10 0.2299930 1.725590 0.71774362 -1.9631376 3.0666837
## 11 0.2299930 1.725590 0.24734911 -1.7195150 2.2437007
## 12 0.2299930 1.725590 NA NA NA
## 13 0.2454777 1.912809 -0.49950962 -2.3127910 1.4491078
## 14 0.2454777 1.912809 -0.70940453 -2.4330232 1.0932976
## 15 0.2454777 1.912809 0.96963955 -0.8385193 2.5645680
## 16 0.2454777 1.912809 NA NA NA
## 17 0.2729061 1.822790 -0.12419953 -2.4086509 2.1807841
## 18 0.2729061 1.822790 0.90047284 -1.6266477 3.2888872
## 19 0.2729061 1.822790 -0.87077697 -2.2052729 0.4460459
## 20 0.2729061 1.822790 NA NA NA
## mu_sample_1
## 1 0.40848195
## 2 0.46231932
## 3 -0.32544002
## 4 1.03746244
## 5 -0.07099011
## 6 1.40842520
## 7 -0.85681249
## 8 -1.19186004
## 9 0.59317166
## 10 -2.30299336
## 11 -0.39877509
## 12 -0.20494543
## 13 0.80806665
## 14 -0.67461571
## 15 1.42687482
## 16 0.56735552
## 17 -1.27976870
## 18 0.49386102
## 19 0.34540645
## 20 -0.44637543
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.65 ===> 99% of the (truncated) height range in dendro.
## ..done.
## ..cutHeight not given, setting it to 2.3 ===> 99% of the (truncated) height range in dendro.
## ..done.
## ..cutHeight not given, setting it to 1.47 ===> 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 1e-05 seconds
## Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 0.1 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.067 seconds (Warm-up)
## Chain 1: 0.101 seconds (Sampling)
## Chain 1: 0.168 seconds (Total)
## Chain 1:
##
## SAMPLING FOR MODEL 'm_cluster_distances_padded' NOW (CHAIN 2).
## Chain 2:
## Chain 2: Gradient evaluation took 1e-05 seconds
## Chain 2: 1000 transitions using 10 leapfrog steps per transition would take 0.1 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.068 seconds (Warm-up)
## Chain 2: 0.174 seconds (Sampling)
## Chain 2: 0.242 seconds (Total)
## Chain 2:
##
## SAMPLING FOR MODEL 'm_cluster_distances_padded' NOW (CHAIN 3).
## Chain 3:
## Chain 3: Gradient evaluation took 8e-06 seconds
## Chain 3: 1000 transitions using 10 leapfrog steps per transition would take 0.08 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.056 seconds (Warm-up)
## Chain 3: 0.103 seconds (Sampling)
## Chain 3: 0.159 seconds (Total)
## Chain 3:
##
## SAMPLING FOR MODEL 'm_cluster_distances_padded' NOW (CHAIN 4).
## Chain 4:
## Chain 4: Gradient evaluation took 8e-06 seconds
## Chain 4: 1000 transitions using 10 leapfrog steps per transition would take 0.08 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.069 seconds (Warm-up)
## Chain 4: 0.112 seconds (Sampling)
## Chain 4: 0.181 seconds (Total)
## Chain 4:
## Warning: There were 1 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 Patched (2025-06-14 r88325)
## Platform: aarch64-apple-darwin20
## Running under: macOS Ventura 13.7.7
##
## Matrix products: default
## BLAS: /Library/Frameworks/R.framework/Versions/4.5-arm64/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/4.5-arm64/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