MetaboDynamics 1.1.2
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 D-Sorbitol A 1 1 225. 2.35 -0.150 C00794
## 2 D-Sorbitol A 1 2 323. 2.51 0.138 C00794
## 3 D-Sorbitol A 1 3 401. 2.60 0.313 C00794
## 4 D-Sorbitol A 2 1 478. 2.68 0.454 C00794
## 5 D-Sorbitol A 2 2 412. 2.62 0.335 C00794
## 6 D-Sorbitol A 2 3 108. 2.04 -0.739 C00794
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.1e-05 seconds
## Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 0.11 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.173 seconds (Warm-up)
## Chain 1: 0.299 seconds (Sampling)
## Chain 1: 0.472 seconds (Total)
## Chain 1:
##
## SAMPLING FOR MODEL 'm_ANOVA_partial_pooling' NOW (CHAIN 1).
## Chain 1:
## Chain 1: Gradient evaluation took 1.1e-05 seconds
## Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 0.11 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.174 seconds (Warm-up)
## Chain 1: 0.62 seconds (Sampling)
## Chain 1: 0.794 seconds (Total)
## Chain 1:
##
## SAMPLING FOR MODEL 'm_ANOVA_partial_pooling' 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.096 seconds (Warm-up)
## Chain 1: 0.173 seconds (Sampling)
## Chain 1: 0.269 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 2-Phosphoglyceric acid C00631 1 A 0.060186478
## 2 1 2-Phosphoglyceric acid C00631 2 A -0.549107407
## 3 1 2-Phosphoglyceric acid C00631 3 A 0.055582485
## 4 1 2-Phosphoglyceric acid C00631 4 A 0.150234103
## 5 2 Creatine C00300 1 A -0.417335572
## 6 2 Creatine C00300 2 A 0.709876686
## 7 2 Creatine C00300 3 A 0.908429661
## 8 2 Creatine C00300 4 A 0.029045833
## 9 3 D-Sorbitol C00794 1 A -0.316586994
## 10 3 D-Sorbitol C00794 2 A 0.219207970
## 11 3 D-Sorbitol C00794 3 A -0.009386217
## 12 3 D-Sorbitol C00794 4 A -0.222297376
## 13 4 Glutathionesulfonic acid <NA> 1 A -0.449798375
## 14 4 Glutathionesulfonic acid <NA> 2 A 0.470688969
## 15 4 Glutathionesulfonic acid <NA> 3 A 0.669723119
## 16 4 Glutathionesulfonic acid <NA> 4 A -0.728220434
## 17 5 UDP C00015 1 A -0.163782901
## 18 5 UDP C00015 2 A 0.291153899
## 19 5 UDP C00015 3 A -0.375643240
## 20 5 UDP C00015 4 A -0.352335699
## mu_lower mu_higher sigma_mean sigma_lower sigma_higher lambda_mean
## 1 -0.8986279 0.8318669 0.6139269 0.18568432 1.8501947 0.7830011
## 2 -1.9073808 0.8896922 1.3948623 0.56165221 3.4951176 0.7830011
## 3 -0.8535995 0.6474976 0.5075195 0.13373397 1.8804800 0.7830011
## 4 -0.2480687 0.5048026 0.1957731 0.03737467 0.8972036 0.7830011
## 5 -2.1636674 1.3953052 1.6941064 0.70398985 4.1735796 0.8715421
## 6 -0.9786752 2.1858028 1.3436496 0.53358818 3.3356655 0.8715421
## 7 -0.9085032 2.4594467 1.5201428 0.64203141 3.9911735 0.8715421
## 8 -1.1992944 1.2212919 1.0121333 0.36274123 2.9173269 0.8715421
## 9 -1.9616051 1.3229561 1.4048027 0.57612899 3.3431252 0.7811560
## 10 -1.3929729 1.8098697 1.4489024 0.60797350 3.5782484 0.7811560
## 11 -1.5801019 1.5499103 1.4580904 0.61965476 3.5968223 0.7811560
## 12 -0.6441366 0.3476639 0.3581304 0.08332689 1.2815015 0.7811560
## 13 -1.7029500 0.9314538 1.1274324 0.42504913 3.1083587 0.8162368
## 14 -1.4751833 2.2866310 1.8147493 0.84794210 4.1200204 0.8162368
## 15 0.3536083 0.9189306 0.1924330 0.03691805 0.9790080 0.8162368
## 16 -2.1431631 0.9075957 1.2880701 0.48888850 3.4226573 0.8162368
## 17 -1.0822013 0.7482059 0.6933168 0.21875497 2.0203488 0.8054261
## 18 -1.7462413 2.2571977 2.1140463 0.96585737 4.5427890 0.8054261
## 19 -1.7232679 0.9027158 1.0633220 0.39526329 2.8225802 0.8054261
## 20 -1.7696569 1.2239896 1.1967777 0.42208497 3.2337314 0.8054261
## lambda_lower lambda_higher delta_mu_mean delta_mu_lower delta_mu_higher
## 1 0.2035700 1.698562 0.64969021 -1.329714 2.5111297
## 2 0.2035700 1.698562 -0.71926290 -2.804264 1.5980665
## 3 0.2035700 1.698562 -0.71883422 -2.807006 1.4966087
## 4 0.2035700 1.698562 NA NA NA
## 5 0.2647361 1.933325 1.45753707 -1.062459 3.6009655
## 6 0.2647361 1.933325 -1.13072704 -2.776523 0.8491365
## 7 0.2647361 1.933325 0.05851447 -1.060672 1.0252787
## 8 0.2647361 1.933325 NA NA NA
## 9 0.2361669 1.738406 -0.02653665 -1.340405 1.4014483
## 10 0.2361669 1.738406 -0.47884421 -2.194844 1.2374215
## 11 0.2361669 1.738406 0.74095227 -1.605149 3.1150375
## 12 0.2361669 1.738406 NA NA NA
## 13 0.2371130 1.764288 -0.46682110 -2.077993 1.1967597
## 14 0.2371130 1.764288 0.78727596 -1.696241 3.2502496
## 15 0.2371130 1.764288 -0.84633221 -2.955066 1.4377754
## 16 0.2371130 1.764288 NA NA NA
## 17 0.2148017 1.713354 0.63654354 -1.739509 3.0123224
## 18 0.2148017 1.713354 0.45051515 -1.261235 2.1235692
## 19 0.2148017 1.713354 -1.02205882 -2.436637 0.5916312
## 20 0.2148017 1.713354 NA NA NA
## mu_sample_1
## 1 0.05713180
## 2 0.52252943
## 3 0.07335925
## 4 0.16233841
## 5 0.43011268
## 6 1.41881344
## 7 -1.33744589
## 8 0.69429940
## 9 -0.90525433
## 10 1.05803119
## 11 -0.04519121
## 12 -0.20513806
## 13 -0.21376413
## 14 1.03781255
## 15 0.67128724
## 16 -1.06716814
## 17 0.01193736
## 18 -0.82102037
## 19 0.10867050
## 20 -1.28863003
##
## $B
## metabolite.ID metabolite KEGG time.ID condition mu_mean
## 1 1 2-Phosphoglyceric acid C00631 1 B 0.22450020
## 2 1 2-Phosphoglyceric acid C00631 2 B -1.40322892
## 3 1 2-Phosphoglyceric acid C00631 3 B 0.36001612
## 4 1 2-Phosphoglyceric acid C00631 4 B -0.02208808
## 5 2 Creatine C00300 1 B -0.30338758
## 6 2 Creatine C00300 2 B 0.61404874
## 7 2 Creatine C00300 3 B 0.20563437
## 8 2 Creatine C00300 4 B 0.22895782
## 9 3 D-Sorbitol C00794 1 B 0.50472710
## 10 3 D-Sorbitol C00794 2 B -0.14929012
## 11 3 D-Sorbitol C00794 3 B -0.89884383
## 12 3 D-Sorbitol C00794 4 B 0.73274563
## 13 4 Glutathionesulfonic acid <NA> 1 B 0.49867690
## 14 4 Glutathionesulfonic acid <NA> 2 B 0.37776897
## 15 4 Glutathionesulfonic acid <NA> 3 B -0.05298448
## 16 4 Glutathionesulfonic acid <NA> 4 B -0.05438762
## 17 5 UDP C00015 1 B 0.38419749
## 18 5 UDP C00015 2 B -0.98264713
## 19 5 UDP C00015 3 B -1.03312959
## 20 5 UDP C00015 4 B 0.51130862
## mu_lower mu_higher sigma_mean sigma_lower sigma_higher lambda_mean
## 1 -0.50657616 0.9724715 0.5239244 0.15360633 1.7274447 0.9351117
## 2 -1.77098228 -0.9669653 0.2737939 0.07797922 1.0486321 0.9351117
## 3 -0.82936830 1.3955294 0.8109121 0.26889457 2.4191226 0.9351117
## 4 -0.17773828 0.1239817 0.1167579 0.02172522 0.5490523 0.9351117
## 5 -2.22839295 1.6594540 1.8833075 0.81869927 4.8144987 1.1692275
## 6 -1.53036097 2.4322975 1.8240194 0.83965688 4.1442753 1.1692275
## 7 -0.73289062 1.2080928 0.7279116 0.25051743 2.0626533 1.1692275
## 8 -1.05778970 1.4926826 0.9963759 0.37002046 2.7212158 1.1692275
## 9 -0.84878426 1.8485254 1.0533742 0.39823369 2.7094420 0.8400117
## 10 -1.01269506 0.6773763 0.6854146 0.21894337 1.9160726 0.8400117
## 11 -1.89102318 0.2662521 0.7806769 0.25130611 2.2414683 0.8400117
## 12 -0.74869314 1.8821289 1.1673344 0.50039903 2.8932516 0.8400117
## 13 -0.51437368 1.4546291 0.7517481 0.24677470 2.0824876 0.9639216
## 14 -1.57836642 1.9462276 1.6729804 0.78261493 3.5098737 0.9639216
## 15 -1.03257745 0.9675683 0.7942884 0.26290265 2.2674902 0.9639216
## 16 -0.73263699 0.5896867 0.4720062 0.14632864 1.4380341 0.9639216
## 17 -0.03661436 0.7193013 0.2707397 0.07202910 0.9586151 0.7589523
## 18 -2.63515483 0.7381921 1.6077117 0.69943653 3.5568660 0.7589523
## 19 -1.64023403 -0.2910279 0.5032696 0.15267730 1.5992399 0.7589523
## 20 -1.27784679 2.3456908 1.6575813 0.70013157 3.9530003 0.7589523
## lambda_lower lambda_higher delta_mu_mean delta_mu_lower delta_mu_higher
## 1 0.2707527 2.040651 0.38954853 -1.8530780 2.3454630
## 2 0.2707527 2.040651 -1.51289256 -3.5716365 0.8551763
## 3 0.2707527 2.040651 0.84445621 -0.4684512 2.0527057
## 4 0.2707527 2.040651 NA NA NA
## 5 0.3216819 2.532331 1.60886329 0.5993782 2.6007559
## 6 0.3216819 2.532331 0.52711126 -1.2323852 2.0338711
## 7 0.3216819 2.532331 -0.34854813 -1.5508849 1.1563329
## 8 0.3216819 2.532331 NA NA NA
## 9 0.2339542 1.879172 -0.13105830 -1.6683059 1.5632882
## 10 0.2339542 1.879172 0.26971908 -1.3059908 1.7722224
## 11 0.2339542 1.879172 -1.48132403 -3.5226608 0.5198743
## 12 0.2339542 1.879172 NA NA NA
## 13 0.2878817 2.148465 0.52681519 -0.8174829 1.8629367
## 14 0.2878817 2.148465 -0.12695814 -2.2878968 1.9366961
## 15 0.2878817 2.148465 -1.41089855 -3.1429361 0.5460620
## 16 0.2878817 2.148465 NA NA NA
## 17 0.2208918 1.653805 0.15409745 -1.9651252 2.1777259
## 18 0.2208918 1.653805 0.09630564 -1.2332665 1.4474359
## 19 0.2208918 1.653805 0.56429310 -1.3683575 2.6128317
## 20 0.2208918 1.653805 NA NA NA
## mu_sample_1
## 1 0.127907928
## 2 -1.415487591
## 3 0.973552792
## 4 0.039575922
## 5 0.151431562
## 6 0.990197985
## 7 -0.001007945
## 8 1.124586096
## 9 1.277043551
## 10 -0.648618420
## 11 0.802018596
## 12 0.959931428
## 13 0.389755372
## 14 0.790287888
## 15 -0.131674827
## 16 -0.049044121
## 17 0.321498999
## 18 -1.601255363
## 19 -1.084078519
## 20 0.342316984
##
## $C
## metabolite.ID metabolite KEGG time.ID condition mu_mean
## 1 1 2-Phosphoglyceric acid C00631 1 C 0.03667481
## 2 1 2-Phosphoglyceric acid C00631 2 C 0.39185096
## 3 1 2-Phosphoglyceric acid C00631 3 C -0.40254350
## 4 1 2-Phosphoglyceric acid C00631 4 C 0.16755406
## 5 2 Creatine C00300 1 C 0.20213824
## 6 2 Creatine C00300 2 C 0.34861742
## 7 2 Creatine C00300 3 C 0.13421702
## 8 2 Creatine C00300 4 C 0.40512010
## 9 3 D-Sorbitol C00794 1 C 0.08310735
## 10 3 D-Sorbitol C00794 2 C -0.51879786
## 11 3 D-Sorbitol C00794 3 C 0.46071242
## 12 3 D-Sorbitol C00794 4 C -0.81537913
## 13 4 Glutathionesulfonic acid <NA> 1 C -0.31754892
## 14 4 Glutathionesulfonic acid <NA> 2 C -0.63073377
## 15 4 Glutathionesulfonic acid <NA> 3 C 0.30034199
## 16 4 Glutathionesulfonic acid <NA> 4 C -0.95278499
## 17 5 UDP C00015 1 C 0.37752219
## 18 5 UDP C00015 2 C 0.32415942
## 19 5 UDP C00015 3 C 0.44246877
## 20 5 UDP C00015 4 C -0.01894137
## mu_lower mu_higher sigma_mean sigma_lower sigma_higher lambda_mean
## 1 -1.5959212 1.5647013 1.2547919 0.4814752 3.217380 0.8319287
## 2 -1.0013565 1.7020878 1.1555309 0.4298942 2.887873 0.8319287
## 3 -1.4529641 0.6776605 0.7888117 0.2392683 2.550799 0.8319287
## 4 -0.4147575 0.7995841 0.3915061 0.1006232 1.397293 0.8319287
## 5 -1.8457266 2.3411064 2.0517146 0.9400766 4.415480 0.7757873
## 6 -0.6333343 1.3358438 0.7747520 0.2620491 2.323656 0.7757873
## 7 -1.3872815 1.3677518 1.0967905 0.4022399 2.909945 0.7757873
## 8 -1.1489033 1.7219609 1.1856503 0.4598914 3.077330 0.7757873
## 9 -1.3046801 1.3804376 1.1410220 0.4222913 3.158406 0.7624289
## 10 -1.8253182 0.9012739 1.1790639 0.4333939 2.896045 0.7624289
## 11 -1.4523515 2.3784596 1.8050594 0.7863572 4.097459 0.7624289
## 12 -2.5590786 1.2520457 1.7172022 0.7221718 3.996530 0.7624289
## 13 -2.1231388 1.5593479 2.0484933 0.9190091 4.864394 0.7780775
## 14 -2.3818423 1.3165176 1.8211468 0.7924307 4.212996 0.7780775
## 15 -0.3167988 0.7719224 0.4134224 0.1084456 1.474585 0.7780775
## 16 -1.6232657 -0.2434050 0.5009070 0.1471705 1.470095 0.7780775
## 17 -0.4828402 1.1772899 0.6646489 0.2297809 1.878572 0.7777869
## 18 -0.7868023 1.3752145 0.8802290 0.2929591 2.347242 0.7777869
## 19 -1.2537938 1.9812122 1.4066421 0.5586999 3.532163 0.7777869
## 20 -1.4179485 1.1956180 1.0672372 0.4043989 2.826667 0.7777869
## lambda_lower lambda_higher delta_mu_mean delta_mu_lower delta_mu_higher
## 1 0.2355938 1.837997 0.31194262 -1.3708058 2.2107314
## 2 0.2355938 1.837997 0.11209499 -2.0678208 2.2545058
## 3 0.2355938 1.837997 -1.41349740 -3.4477290 0.5813471
## 4 0.2355938 1.837997 NA NA NA
## 5 0.2350180 1.650252 -0.25763394 -2.1974558 1.5105552
## 6 0.2350180 1.650252 -0.94959615 -3.1035052 1.3255364
## 7 0.2350180 1.650252 1.19290133 -0.9058758 3.1628607
## 8 0.2350180 1.650252 NA NA NA
## 9 0.2287729 1.669896 0.80766360 -1.1288580 2.5318116
## 10 0.2287729 1.669896 -0.72266903 -2.8758555 1.7740405
## 11 0.2287729 1.669896 0.64170834 -1.4233955 2.7540735
## 12 0.2287729 1.669896 NA NA NA
## 13 0.2230381 1.796781 -0.08444671 -1.5252404 1.2786174
## 14 0.2230381 1.796781 -0.71384112 -2.8354710 1.7112063
## 15 0.2230381 1.796781 1.07320255 -1.4792618 3.3315718
## 16 0.2230381 1.796781 NA NA NA
## 17 0.2317713 1.682110 -0.72093610 -3.1450153 1.7692447
## 18 0.2317713 1.682110 0.81913985 -0.7235699 2.2447453
## 19 0.2317713 1.682110 -0.31928336 -1.8584725 1.0341124
## 20 0.2317713 1.682110 NA NA NA
## mu_sample_1
## 1 0.43033769
## 2 -0.08964475
## 3 -0.36577993
## 4 -0.02567970
## 5 -0.21922980
## 6 -0.35382618
## 7 0.26267632
## 8 0.90864036
## 9 -0.30572389
## 10 -1.64857644
## 11 -0.63633654
## 12 -1.19775927
## 13 1.28503927
## 14 -0.47820862
## 15 0.32330750
## 16 -0.97271610
## 17 0.59135089
## 18 0.51833510
## 19 -0.22321843
## 20 0.73714068
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.75 ===> 99% of the (truncated) height range in dendro.
## ..done.
## ..cutHeight not given, setting it to 2.35 ===> 99% of the (truncated) height range in dendro.
## ..done.
## ..cutHeight not given, setting it to 2.29 ===> 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 in MASS::cov.trob(data[, vars]): Probable convergence failure
## Warning: Computation failed in `stat_ellipse()`.
## Caused by error in `chol.default()`:
## ! the leading minor of order 2 is not positive
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 9e-06 seconds
## Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 0.09 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.053 seconds (Warm-up)
## Chain 1: 0.096 seconds (Sampling)
## Chain 1: 0.149 seconds (Total)
## Chain 1:
##
## SAMPLING FOR MODEL 'm_cluster_distances_padded' NOW (CHAIN 2).
## Chain 2:
## Chain 2: Gradient evaluation took 8e-06 seconds
## Chain 2: 1000 transitions using 10 leapfrog steps per transition would take 0.08 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.051 seconds (Warm-up)
## Chain 2: 0.086 seconds (Sampling)
## Chain 2: 0.137 seconds (Total)
## Chain 2:
##
## SAMPLING FOR MODEL 'm_cluster_distances_padded' NOW (CHAIN 3).
## Chain 3:
## Chain 3: Gradient evaluation took 7e-06 seconds
## Chain 3: 1000 transitions using 10 leapfrog steps per transition would take 0.07 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.046 seconds (Warm-up)
## Chain 3: 0.087 seconds (Sampling)
## Chain 3: 0.133 seconds (Total)
## Chain 3:
##
## SAMPLING FOR MODEL 'm_cluster_distances_padded' NOW (CHAIN 4).
## Chain 4:
## Chain 4: Gradient evaluation took 5e-06 seconds
## Chain 4: 1000 transitions using 10 leapfrog steps per transition would take 0.05 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.042 seconds (Warm-up)
## Chain 4: 0.09 seconds (Sampling)
## Chain 4: 0.132 seconds (Total)
## Chain 4:
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.1
##
## 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.0
## [11] generics_0.1.4 MatrixGenerics_1.21.0
## [13] matrixStats_1.5.0 MetaboDynamics_1.1.2
## [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 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.4
## [58] knitr_1.50 V8_6.0.4 viridisLite_0.4.2
## [61] rstantools_2.4.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