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 19
Visualize 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.0 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.0 sass_0.4.10
#> [23] parallelly_1.45.0 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.2
#> [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 DelayedArray_0.34.1
#> [63] bluster_1.18.0 permute_0.9-8
#> [65] tools_4.5.1 vipor_0.4.7
#> [67] beeswarm_0.4.0 ape_5.8-1
#> [69] glue_1.8.0 nlme_3.1-168
#> [71] gridtext_0.1.5 grid_4.5.1
#> [73] cluster_2.1.8.1 reshape2_1.4.4
#> [75] gtable_0.3.6 fillpattern_1.0.2
#> [77] tzdb_0.5.0 tidyr_1.3.1
#> [79] hms_1.1.3 tidygraph_1.3.1
#> [81] BiocSingular_1.24.0 ScaledMatrix_1.16.0
#> [83] xml2_1.3.8 ggrepel_0.9.6
#> [85] pillar_1.11.0 stringr_1.5.1
#> [87] yulab.utils_0.2.0 splines_4.5.1
#> [89] tweenr_2.0.3 ggtext_0.1.2
#> [91] treeio_1.32.0 lattice_0.22-7
#> [93] tidyselect_1.2.1 DirichletMultinomial_1.50.0
#> [95] gridExtra_2.3 bookdown_0.43
#> [97] xfun_0.52 graphlayouts_1.2.2
#> [99] rbiom_2.2.1 stringi_1.8.7
#> [101] UCSC.utils_1.4.0 ggfun_0.2.0
#> [103] lazyeval_0.2.2 yaml_2.3.10
#> [105] evaluate_1.0.4 codetools_0.2-20
#> [107] tibble_3.3.0 BiocManager_1.30.26
#> [109] ggplotify_0.1.2 cli_3.6.5
#> [111] xtable_1.8-4 systemfonts_1.2.3
#> [113] jquerylib_0.1.4 Rcpp_1.1.0
#> [115] readxl_1.4.5 parallel_4.5.1
#> [117] pkgdown_2.1.3 readr_2.1.5
#> [119] sparseMatrixStats_1.20.0 decontam_1.28.0
#> [121] viridisLite_0.4.2 mvtnorm_1.3-3
#> [123] slam_0.1-55 tidytree_0.4.6
#> [125] scales_1.4.0 purrr_1.1.0
#> [127] crayon_1.5.3 rlang_1.1.6