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 Under development (unstable) (2025-01-28 r87664)
#> Platform: x86_64-pc-linux-gnu
#> Running under: Ubuntu 24.04.1 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.15.6
#> [3] ggraph_2.2.1 scater_1.35.1
#> [5] scuttle_1.17.0 ggplot2_3.5.1
#> [7] miaTime_0.99.5 mia_1.15.21
#> [9] TreeSummarizedExperiment_2.15.0 Biostrings_2.75.3
#> [11] XVector_0.47.2 SingleCellExperiment_1.29.1
#> [13] MultiAssayExperiment_1.33.9 SummarizedExperiment_1.37.0
#> [15] Biobase_2.67.0 GenomicRanges_1.59.1
#> [17] GenomeInfoDb_1.43.4 IRanges_2.41.2
#> [19] S4Vectors_0.45.2 BiocGenerics_0.53.6
#> [21] generics_0.1.3 MatrixGenerics_1.19.1
#> [23] matrixStats_1.5.0 knitr_1.49
#> [25] BiocStyle_2.35.0
#>
#> loaded via a namespace (and not attached):
#> [1] jsonlite_1.8.9 magrittr_2.0.3
#> [3] estimability_1.5.1 ggbeeswarm_0.7.2
#> [5] farver_2.1.2 rmarkdown_2.29
#> [7] fs_1.6.5 ragg_1.3.3
#> [9] vctrs_0.6.5 memoise_2.0.1
#> [11] DelayedMatrixStats_1.29.1 ggtree_3.15.0
#> [13] htmltools_0.5.8.1 S4Arrays_1.7.2
#> [15] BiocBaseUtils_1.9.0 BiocNeighbors_2.1.2
#> [17] janeaustenr_1.0.0 cellranger_1.1.0
#> [19] gridGraphics_0.5-1 SparseArray_1.7.5
#> [21] sass_0.4.9 parallelly_1.42.0
#> [23] bslib_0.9.0 tokenizers_0.3.0
#> [25] htmlwidgets_1.6.4 desc_1.4.3
#> [27] plyr_1.8.9 DECIPHER_3.3.2
#> [29] emmeans_1.10.7 cachem_1.1.0
#> [31] igraph_2.1.4 lifecycle_1.0.4
#> [33] pkgconfig_2.0.3 rsvd_1.0.5
#> [35] Matrix_1.7-2 R6_2.5.1
#> [37] fastmap_1.2.0 GenomeInfoDbData_1.2.13
#> [39] tidytext_0.4.2 aplot_0.2.4
#> [41] digest_0.6.37 colorspace_2.1-1
#> [43] ggnewscale_0.5.0 patchwork_1.3.0
#> [45] irlba_2.3.5.1 SnowballC_0.7.1
#> [47] textshaping_1.0.0 vegan_2.6-10
#> [49] beachmat_2.23.6 labeling_0.4.3
#> [51] polyclip_1.10-7 mgcv_1.9-1
#> [53] httr_1.4.7 abind_1.4-8
#> [55] compiler_4.5.0 withr_3.0.2
#> [57] BiocParallel_1.41.0 viridis_0.6.5
#> [59] DBI_1.2.3 ggforce_0.4.2
#> [61] MASS_7.3-64 DelayedArray_0.33.5
#> [63] bluster_1.17.0 permute_0.9-7
#> [65] tools_4.5.0 vipor_0.4.7
#> [67] beeswarm_0.4.0 ape_5.8-1
#> [69] glue_1.8.0 nlme_3.1-167
#> [71] gridtext_0.1.5 grid_4.5.0
#> [73] cluster_2.1.8 reshape2_1.4.4
#> [75] gtable_0.3.6 fillpattern_1.0.2
#> [77] tzdb_0.4.0 tidyr_1.3.1
#> [79] hms_1.1.3 tidygraph_1.3.1
#> [81] BiocSingular_1.23.0 ScaledMatrix_1.15.0
#> [83] xml2_1.3.6 ggrepel_0.9.6
#> [85] pillar_1.10.1 stringr_1.5.1
#> [87] yulab.utils_0.2.0 splines_4.5.0
#> [89] tweenr_2.0.3 ggtext_0.1.2
#> [91] treeio_1.31.0 lattice_0.22-6
#> [93] tidyselect_1.2.1 DirichletMultinomial_1.49.0
#> [95] gridExtra_2.3 bookdown_0.42
#> [97] xfun_0.50 graphlayouts_1.2.2
#> [99] rbiom_2.0.13 stringi_1.8.4
#> [101] UCSC.utils_1.3.1 ggfun_0.1.8
#> [103] lazyeval_0.2.2 yaml_2.3.10
#> [105] evaluate_1.0.3 codetools_0.2-20
#> [107] tibble_3.2.1 BiocManager_1.30.25
#> [109] ggplotify_0.1.2 cli_3.6.3
#> [111] xtable_1.8-4 systemfonts_1.2.1
#> [113] munsell_0.5.1 jquerylib_0.1.4
#> [115] Rcpp_1.0.14 readxl_1.4.3
#> [117] parallel_4.5.0 pkgdown_2.1.1
#> [119] readr_2.1.5 sparseMatrixStats_1.19.0
#> [121] decontam_1.27.0 viridisLite_0.4.2
#> [123] mvtnorm_1.3-3 slam_0.1-55
#> [125] tidytree_0.4.6 scales_1.3.0
#> [127] purrr_1.0.4 crayon_1.5.3
#> [129] rlang_1.1.5 cowplot_1.1.3