Last updated: 2025-01-10
Checks: 7 0
Knit directory: muse/
This reproducible R Markdown analysis was created with workflowr (version 1.7.1). The Checks tab describes the reproducibility checks that were applied when the results were created. The Past versions tab lists the development history.
Great! Since the R Markdown file has been committed to the Git repository, you know the exact version of the code that produced these results.
Great job! The global environment was empty. Objects defined in the global environment can affect the analysis in your R Markdown file in unknown ways. For reproduciblity it’s best to always run the code in an empty environment.
The command set.seed(20200712)
was run prior to running
the code in the R Markdown file. Setting a seed ensures that any results
that rely on randomness, e.g. subsampling or permutations, are
reproducible.
Great job! Recording the operating system, R version, and package versions is critical for reproducibility.
Nice! There were no cached chunks for this analysis, so you can be confident that you successfully produced the results during this run.
Great job! Using relative paths to the files within your workflowr project makes it easier to run your code on other machines.
Great! You are using Git for version control. Tracking code development and connecting the code version to the results is critical for reproducibility.
The results in this page were generated with repository version 28f42c0. See the Past versions tab to see a history of the changes made to the R Markdown and HTML files.
Note that you need to be careful to ensure that all relevant files for
the analysis have been committed to Git prior to generating the results
(you can use wflow_publish
or
wflow_git_commit
). workflowr only checks the R Markdown
file, but you know if there are other scripts or data files that it
depends on. Below is the status of the Git repository when the results
were generated:
Ignored files:
Ignored: .Rhistory
Ignored: .Rproj.user/
Ignored: data/pbmc3k.csv
Ignored: data/pbmc3k.csv.gz
Ignored: data/pbmc3k/
Ignored: r_packages_4.4.0/
Ignored: r_packages_4.4.1/
Note that any generated files, e.g. HTML, png, CSS, etc., are not included in this status report because it is ok for generated content to have uncommitted changes.
These are the previous versions of the repository in which changes were
made to the R Markdown (analysis/miloR.Rmd
) and HTML
(docs/miloR.html
) files. If you’ve configured a remote Git
repository (see ?wflow_git_remote
), click on the hyperlinks
in the table below to view the files as they were in that past version.
File | Version | Author | Date | Message |
---|---|---|---|---|
Rmd | 28f42c0 | Dave Tang | 2025-01-10 | Update notebook |
html | 710ddf2 | Dave Tang | 2025-01-10 | Build site. |
Rmd | 2dd7fac | Dave Tang | 2025-01-10 | DA testing with miloR |
Milo is a tool for analysis of complex single cell datasets generated from replicated multi-condition experiments, which detects changes in composition between conditions. While differential abundance (DA) is commonly quantified in discrete cell clusters, Milo uses partially overlapping neighbourhoods of cells on a KNN graph. Starting from a graph that faithfully recapitulates the biology of the cell population, Milo analysis consists of 3 steps:
Sampling of representative neighbourhoods Testing for differential abundance of conditions in all neighbourhoods Accounting for multiple hypothesis testing using a weighted FDR procedure that accounts for the overlap of neighbourhoods
Install Bioconductor packages using
BiocManager::install()
.
if (!require("BiocManager", quietly = TRUE))
install.packages("BiocManager")
BiocManager::install("SingleCellExperiment")
BiocManager::install("scran")
BiocManager::install("scater")
BiocManager::install("miloR")
install.packages('dplyr')
install.packages('patchwork')
Load libraries.
suppressPackageStartupMessages(library(miloR))
suppressPackageStartupMessages(library(SingleCellExperiment))
suppressPackageStartupMessages(library(scater))
suppressPackageStartupMessages(library(scran))
suppressPackageStartupMessages(library(dplyr))
suppressPackageStartupMessages(library(patchwork))
suppressPackageStartupMessages(library(ggplot2))
Load testing data.
data("sim_trajectory", package = "miloR")
## Extract SingleCellExperiment object
traj_sce <- sim_trajectory[['SCE']]
## Extract sample metadata to use for testing
traj_meta <- sim_trajectory[["meta"]]
## Add metadata to colData slot
colData(traj_sce) <- DataFrame(traj_meta)
colnames(traj_sce) <- colData(traj_sce)$cell_id
redim <- reducedDim(traj_sce, "PCA")
dimnames(redim) <- list(colnames(traj_sce), paste0("PC", c(1:50)))
reducedDim(traj_sce, "PCA") <- redim
Sample and conditions.
table(colData(traj_sce)$Sample)
A_R1 A_R2 A_R3 B_R1 B_R2 B_R3
46 42 58 103 107 144
set.seed(1984)
logcounts(traj_sce) <- log(counts(traj_sce) + 1)
traj_sce <- runPCA(traj_sce, ncomponents=30)
traj_sce <- runUMAP(traj_sce)
cbind(
colData(traj_sce),
reducedDim(traj_sce, "UMAP")
) |>
ggplot(aes(UMAP1, UMAP2, colour = Condition)) +
geom_point() +
theme_minimal() -> my_umap
my_umap
Version | Author | Date |
---|---|---|
710ddf2 | Dave Tang | 2025-01-10 |
traj_milo <- Milo(traj_sce)
reducedDim(traj_milo, "UMAP") <- reducedDim(traj_sce, "UMAP")
traj_milo
class: Milo
dim: 500 500
metadata(0):
assays(2): counts logcounts
rownames(500): G1 G2 ... G499 G500
rowData names(0):
colnames(500): C1 C2 ... C499 C500
colData names(5): cell_id group_id Condition Replicate Sample
reducedDimNames(2): PCA UMAP
mainExpName: NULL
altExpNames(0):
nhoods dimensions(2): 1 1
nhoodCounts dimensions(2): 1 1
nhoodDistances dimension(1): 0
graph names(0):
nhoodIndex names(1): 0
nhoodExpression dimension(2): 1 1
nhoodReducedDim names(0):
nhoodGraph names(0):
nhoodAdjacency dimension(2): 1 1
traj_milo <- buildGraph(traj_milo, k = 10, d = 30)
Constructing kNN graph with k:10
traj_milo <- makeNhoods(traj_milo, prop = 0.1, k = 10, d=30, refined = TRUE)
Checking valid object
Running refined sampling with reduced_dim
plotNhoodSizeHist(traj_milo)
Version | Author | Date |
---|---|---|
710ddf2 | Dave Tang | 2025-01-10 |
traj_milo <- countCells(traj_milo, meta.data = data.frame(colData(traj_milo)), samples="Sample")
Checking meta.data validity
Counting cells in neighbourhoods
head(nhoodCounts(traj_milo))
6 x 6 sparse Matrix of class "dgCMatrix"
B_R1 A_R1 A_R2 B_R2 B_R3 A_R3
1 15 . 1 22 29 1
2 8 . . 14 28 1
3 9 6 7 7 6 10
4 13 2 1 19 21 1
5 5 6 4 8 6 7
6 6 7 5 12 8 11
traj_design <- data.frame(colData(traj_milo))[,c("Sample", "Condition")]
traj_design <- distinct(traj_design)
rownames(traj_design) <- traj_design$Sample
## Reorder rownames to match columns of nhoodCounts(milo)
traj_design <- traj_design[colnames(nhoodCounts(traj_milo)), , drop=FALSE]
traj_design
Sample Condition
B_R1 B_R1 B
A_R1 A_R1 A
A_R2 A_R2 A
B_R2 B_R2 B
B_R3 B_R3 B
A_R3 A_R3 A
traj_milo <- calcNhoodDistance(traj_milo, d=30)
'as(<dgTMatrix>, "dgCMatrix")' is deprecated.
Use 'as(., "CsparseMatrix")' instead.
See help("Deprecated") and help("Matrix-deprecated").
da_results <- testNhoods(traj_milo, design = ~ Condition, design.df = traj_design)
Using TMM normalisation
Running with model contrasts
Performing spatial FDR correction with k-distance weighting
da_results %>%
dplyr::arrange(SpatialFDR) %>%
head()
logFC logCPM F PValue FDR Nhood SpatialFDR
1 3.886596 16.30369 33.87551 6.099827e-08 1.646953e-06 1 1.611592e-06
2 4.337415 15.95908 29.07187 4.140178e-07 5.589241e-06 2 5.504741e-06
4 2.655301 16.09039 19.26657 2.658333e-05 2.392499e-04 4 2.371232e-04
8 2.897422 15.53655 20.75784 6.887852e-05 4.649300e-04 8 4.627806e-04
24 -1.527532 15.73713 29.71687 5.308236e-04 2.866447e-03 24 2.872952e-03
23 2.278126 15.50551 10.21171 1.830055e-03 8.235247e-03 23 8.283248e-03
traj_milo <- buildNhoodGraph(traj_milo)
my_umap + plotNhoodGraphDA(traj_milo, da_results, alpha=0.05) +
plot_layout(guides="collect")
Adding nhood effect sizes to neighbourhood graph attributes
Version | Author | Date |
---|---|---|
710ddf2 | Dave Tang | 2025-01-10 |
sessionInfo()
R version 4.4.1 (2024-06-14)
Platform: x86_64-pc-linux-gnu
Running under: Ubuntu 22.04.5 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.20.so; LAPACK version 3.10.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: Etc/UTC
tzcode source: system (glibc)
attached base packages:
[1] stats4 stats graphics grDevices utils datasets methods
[8] base
other attached packages:
[1] patchwork_1.3.0 dplyr_1.1.4
[3] scran_1.34.0 scater_1.34.0
[5] ggplot2_3.5.1 scuttle_1.16.0
[7] SingleCellExperiment_1.28.1 SummarizedExperiment_1.36.0
[9] Biobase_2.66.0 GenomicRanges_1.58.0
[11] GenomeInfoDb_1.42.1 IRanges_2.40.1
[13] S4Vectors_0.44.0 BiocGenerics_0.52.0
[15] MatrixGenerics_1.18.0 matrixStats_1.4.1
[17] miloR_2.2.0 edgeR_4.4.1
[19] limma_3.62.1 workflowr_1.7.1
loaded via a namespace (and not attached):
[1] RColorBrewer_1.1-3 rstudioapi_0.17.1 jsonlite_1.8.9
[4] magrittr_2.0.3 ggbeeswarm_0.7.2 farver_2.1.2
[7] rmarkdown_2.28 fs_1.6.4 zlibbioc_1.52.0
[10] vctrs_0.6.5 memoise_2.0.1 htmltools_0.5.8.1
[13] S4Arrays_1.6.0 BiocNeighbors_2.0.1 SparseArray_1.6.0
[16] sass_0.4.9 pracma_2.4.4 bslib_0.8.0
[19] cachem_1.1.0 whisker_0.4.1 igraph_2.1.2
[22] lifecycle_1.0.4 pkgconfig_2.0.3 rsvd_1.0.5
[25] Matrix_1.7-0 R6_2.5.1 fastmap_1.2.0
[28] GenomeInfoDbData_1.2.13 digest_0.6.37 numDeriv_2016.8-1.1
[31] colorspace_2.1-1 ps_1.8.1 rprojroot_2.0.4
[34] dqrng_0.4.1 irlba_2.3.5.1 beachmat_2.22.0
[37] labeling_0.4.3 fansi_1.0.6 httr_1.4.7
[40] polyclip_1.10-7 abind_1.4-8 compiler_4.4.1
[43] withr_3.0.2 BiocParallel_1.40.0 viridis_0.6.5
[46] highr_0.11 ggforce_0.4.2 MASS_7.3-60.2
[49] DelayedArray_0.32.0 bluster_1.16.0 gtools_3.9.5
[52] tools_4.4.1 vipor_0.4.7 beeswarm_0.4.0
[55] httpuv_1.6.15 glue_1.8.0 callr_3.7.6
[58] promises_1.3.0 grid_4.4.1 getPass_0.2-4
[61] cluster_2.1.6 generics_0.1.3 gtable_0.3.6
[64] tidyr_1.3.1 BiocSingular_1.22.0 tidygraph_1.3.1
[67] ScaledMatrix_1.14.0 metapod_1.14.0 utf8_1.2.4
[70] XVector_0.46.0 ggrepel_0.9.6 pillar_1.9.0
[73] stringr_1.5.1 later_1.3.2 splines_4.4.1
[76] tweenr_2.0.3 lattice_0.22-6 FNN_1.1.4.1
[79] tidyselect_1.2.1 locfit_1.5-9.10 knitr_1.48
[82] git2r_0.35.0 gridExtra_2.3 xfun_0.48
[85] graphlayouts_1.2.0 statmod_1.5.0 stringi_1.8.4
[88] UCSC.utils_1.2.0 yaml_2.3.10 evaluate_1.0.1
[91] codetools_0.2-20 ggraph_2.2.1 tibble_3.2.1
[94] cli_3.6.3 uwot_0.2.2 munsell_0.5.1
[97] processx_3.8.4 jquerylib_0.1.4 Rcpp_1.0.13
[100] parallel_4.4.1 viridisLite_0.4.2 scales_1.3.0
[103] purrr_1.0.2 crayon_1.5.3 rlang_1.1.4
[106] cowplot_1.1.3