Dense samples of the minimal gut microbiome. In the initial hours,
MDb-MM was grown under batch condition and 24 h onwards, continuous
feeding of media with pulse feeding cycles. This information is stored
in the colData.
library(miaTime)
data(minimalgut)
tse <- minimalgut
# Quick check of number of samples
table(tse[["StudyIdentifier"]], tse[["condition_1"]])
#>
#> batch_carbs DoS pulse Overnight
#> Bioreactor A 4 38 19
#> Bioreactor B 4 38 19
#> Bioreactor C 4 38 19Visualize samples available for each of the bioreactors. This allows to identify if there are any missing samples for specific times.
library(ggplot2)
colData(tse) |>
ggplot() +
geom_tile(
aes(x = as.factor(Time.hr), y = StudyIdentifier, fill = condition_1))
The minimalgut dataset, mucus-diet based minimal
microbiome (MDbMM-16), consists of 16 species assembled in three
bioreactors. We can investigate the succession of mdbMM16 from the start
of experiment here hour zero until the end of the experiment.
# Transform data to relativeS
tse <- transformAssay(tse, method = "relabundance")
# Divergence from baseline i.e from hour zero
tse <- addBaselineDivergence(
tse,
assay.type = "relabundance",
method = "bray",
group = "StudyIdentifier",
time.col = "Time.hr",
)Let’s then visualize the divergence.
library(scater)
# Create a time series plot for divergence
p <- plotColData(
tse, x = "Time.hr", y = "divergence", colour_by = "StudyIdentifier") +
# Add line between points
geom_line(aes(group = .data[["colour_by"]], colour = .data[["colour_by"]]))
p
Now visualize abundance of Blautia hydrogenotrophica using
the miaViz::plotSeries() function.
library(miaViz)
# Plot certain feature by time
p <- plotSeries(
tse,
x = "Time.hr", y = "Blautia_hydrogenotrophica", colour_by = "Species",
assay.type = "relabundance")
p
Sample dissimilarity between consecutive time steps(step size n >=
1) within a group(subject, age, reaction chamber, etc.) can be
calculated by addStepwiseDivergence.
# Divergence between consecutive time points
tse <- addStepwiseDivergence(
tse,
assay.type = "relabundance",
method = "bray",
group = "StudyIdentifier",
time.interval = 1,
time.col = "Time.hr",
name = c("divergence_from_previous_step",
"time_from_previous_step", "reference_samples")
)The results are again stored in colData. We calculate
the speed of divergence change by dividing each divergence change by the
corresponding change in time. Then we use similar plotting methods as
previously.
# Calculate slope for the change
tse[["divergence_change"]] <- tse[["divergence_from_previous_step"]] /
tse[["time_from_previous_step"]]
# Create a time series plot for divergence
p <- plotColData(
tse,
x = "Time.hr",
y = "divergence_change",
colour_by = "StudyIdentifier"
) +
# Add line between points
geom_line(aes(group = .data[["colour_by"]], colour = .data[["colour_by"]]))
p
This shows how to calculate and plot moving average for the variable of interest (here: slope).
library(dplyr)
# Calculate moving average with time window of 3 time points
tse[["sliding_divergence"]] <- colData(tse) |>
as.data.frame() |>
# Group based on reactor
group_by(StudyIdentifier) |>
# Calculate moving average
mutate(sliding_avg = (
# We get the previous 2 samples
lag(divergence_change, 2) +
lag(divergence_change, 1) +
# And the current sample
divergence_change
# And take average
) / 3
) |>
# Get only the values as vector
ungroup() |>
pull(sliding_avg)After calculating the moving average of divergences, we can visualize the result in a similar way to our previous approach.
# Create a time series plot for divergence
p <- plotColData(
tse,
x = "Time.hr",
y = "sliding_divergence",
colour_by = "StudyIdentifier"
) +
# Add line between points
geom_line(aes(group = .data[["colour_by"]], colour = .data[["colour_by"]]))
p
sessionInfo()
#> R version 4.5.1 (2025-06-13)
#> Platform: x86_64-pc-linux-gnu
#> Running under: Ubuntu 24.04.2 LTS
#>
#> Matrix products: default
#> BLAS: /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3
#> LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.26.so; LAPACK version 3.12.0
#>
#> locale:
#> [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
#> [3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
#> [5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
#> [7] LC_PAPER=en_US.UTF-8 LC_NAME=C
#> [9] LC_ADDRESS=C LC_TELEPHONE=C
#> [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
#>
#> time zone: UTC
#> tzcode source: system (glibc)
#>
#> attached base packages:
#> [1] stats4 stats graphics grDevices utils datasets methods
#> [8] base
#>
#> other attached packages:
#> [1] dplyr_1.1.4 miaViz_1.16.0
#> [3] ggraph_2.2.1 scater_1.36.0
#> [5] scuttle_1.18.0 ggplot2_3.5.2
#> [7] miaTime_0.99.8 mia_1.16.1
#> [9] TreeSummarizedExperiment_2.16.1 Biostrings_2.76.0
#> [11] XVector_0.48.0 SingleCellExperiment_1.30.1
#> [13] MultiAssayExperiment_1.34.0 SummarizedExperiment_1.38.1
#> [15] Biobase_2.68.0 GenomicRanges_1.60.0
#> [17] GenomeInfoDb_1.44.2 IRanges_2.42.0
#> [19] S4Vectors_0.46.0 BiocGenerics_0.54.0
#> [21] generics_0.1.4 MatrixGenerics_1.20.0
#> [23] matrixStats_1.5.0 knitr_1.50
#> [25] BiocStyle_2.36.0
#>
#> loaded via a namespace (and not attached):
#> [1] RColorBrewer_1.1-3 jsonlite_2.0.0
#> [3] magrittr_2.0.3 estimability_1.5.1
#> [5] ggbeeswarm_0.7.2 farver_2.1.2
#> [7] rmarkdown_2.29 fs_1.6.6
#> [9] ragg_1.4.0 vctrs_0.6.5
#> [11] memoise_2.0.1 DelayedMatrixStats_1.30.0
#> [13] ggtree_3.16.3 htmltools_0.5.8.1
#> [15] S4Arrays_1.8.1 BiocBaseUtils_1.10.0
#> [17] BiocNeighbors_2.2.0 janeaustenr_1.0.0
#> [19] cellranger_1.1.0 gridGraphics_0.5-1
#> [21] SparseArray_1.8.1 sass_0.4.10
#> [23] parallelly_1.45.1 bslib_0.9.0
#> [25] tokenizers_0.3.0 htmlwidgets_1.6.4
#> [27] desc_1.4.3 plyr_1.8.9
#> [29] DECIPHER_3.4.0 emmeans_1.11.2
#> [31] cachem_1.1.0 igraph_2.1.4
#> [33] lifecycle_1.0.4 pkgconfig_2.0.3
#> [35] rsvd_1.0.5 Matrix_1.7-3
#> [37] R6_2.6.1 fastmap_1.2.0
#> [39] GenomeInfoDbData_1.2.14 tidytext_0.4.3
#> [41] aplot_0.2.8 digest_0.6.37
#> [43] ggnewscale_0.5.2 patchwork_1.3.1
#> [45] irlba_2.3.5.1 SnowballC_0.7.1
#> [47] textshaping_1.0.1 vegan_2.7-1
#> [49] beachmat_2.24.0 labeling_0.4.3
#> [51] polyclip_1.10-7 mgcv_1.9-3
#> [53] httr_1.4.7 abind_1.4-8
#> [55] compiler_4.5.1 withr_3.0.2
#> [57] BiocParallel_1.42.1 viridis_0.6.5
#> [59] DBI_1.2.3 ggforce_0.5.0
#> [61] MASS_7.3-65 rappdirs_0.3.3
#> [63] DelayedArray_0.34.1 bluster_1.18.0
#> [65] permute_0.9-8 tools_4.5.1
#> [67] vipor_0.4.7 beeswarm_0.4.0
#> [69] ape_5.8-1 glue_1.8.0
#> [71] nlme_3.1-168 gridtext_0.1.5
#> [73] grid_4.5.1 cluster_2.1.8.1
#> [75] reshape2_1.4.4 gtable_0.3.6
#> [77] fillpattern_1.0.2 tzdb_0.5.0
#> [79] tidyr_1.3.1 hms_1.1.3
#> [81] tidygraph_1.3.1 BiocSingular_1.24.0
#> [83] ScaledMatrix_1.16.0 xml2_1.4.0
#> [85] ggrepel_0.9.6 pillar_1.11.0
#> [87] stringr_1.5.1 yulab.utils_0.2.1
#> [89] splines_4.5.1 tweenr_2.0.3
#> [91] ggtext_0.1.2 treeio_1.32.0
#> [93] lattice_0.22-7 tidyselect_1.2.1
#> [95] DirichletMultinomial_1.50.0 gridExtra_2.3
#> [97] bookdown_0.44 xfun_0.53
#> [99] graphlayouts_1.2.2 rbiom_2.2.1
#> [101] stringi_1.8.7 UCSC.utils_1.4.0
#> [103] ggfun_0.2.0 lazyeval_0.2.2
#> [105] yaml_2.3.10 evaluate_1.0.4
#> [107] codetools_0.2-20 tibble_3.3.0
#> [109] BiocManager_1.30.26 ggplotify_0.1.2
#> [111] cli_3.6.5 xtable_1.8-4
#> [113] systemfonts_1.2.3 jquerylib_0.1.4
#> [115] Rcpp_1.1.0 readxl_1.4.5
#> [117] parallel_4.5.1 pkgdown_2.1.3
#> [119] readr_2.1.5 sparseMatrixStats_1.20.0
#> [121] decontam_1.28.0 viridisLite_0.4.2
#> [123] mvtnorm_1.3-3 slam_0.1-55
#> [125] tidytree_0.4.6 scales_1.4.0
#> [127] purrr_1.1.0 crayon_1.5.3
#> [129] rlang_1.1.6