Processing math: 100%

Last updated: 2024-11-18

Checks: 7 0

Knit directory: single-cell-jamboree/analysis/

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(1) 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 47d0693. 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/

Untracked files:
    Untracked:  .DS_Store
    Untracked:  .gitignore
    Untracked:  analysis/.DS_Store
    Untracked:  data/.DS_Store
    Untracked:  data/mouse_embryo/
    Untracked:  output/.DS_Store
    Untracked:  output/mouse_embryo/

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/mouse_embryo.Rmd) and HTML (docs/mouse_embryo.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 47d0693 Ziang Zhang 2024-11-18 workflowr::wflow_publish("analysis/mouse_embryo.Rmd")
html 99a79f6 Ziang Zhang 2024-11-18 Build site.
Rmd a70fb60 Ziang Zhang 2024-11-18 workflowr::wflow_publish("analysis/mouse_embryo.Rmd")
html 070d27c Ziang Zhang 2024-11-15 Build site.
Rmd 074ea5c Ziang Zhang 2024-11-15 workflowr::wflow_publish("analysis/mouse_embryo.Rmd")
html 4a09688 Ziang Zhang 2024-11-15 Build site.
Rmd 90cbbb9 Ziang Zhang 2024-11-15 workflowr::wflow_publish("analysis/mouse_embryo.Rmd")

Data

This single cell RNA data is available here, as studied in Srivatsan et al, 2021.

The dataset considers 108725 cells and 39198 genes (after QC), measured with spatial locations in the mouse embryo.

library(Seurat)
library(Matrix)
library(data.table)
library(flashier)
library(ggplot2)
library(patchwork)
library(cowplot)
library(RColorBrewer)
library(Biobase)
library(ggpubr)
library(gridExtra)
library(fastTopics)
source('../code/plot_loadings_on_umap.R')
source("../code/Customized_Plots.R")
seurat_object <- readRDS("../data/mouse_embryo/processed_seurat/seurat_object.rds")
Y <- t(seurat_object$RNA$data)

The data contains the UMAP information that we can directly use for visualization, and compare with the “anatomical annotation” and the “cluster label” in Srivatsan et al, 2021.

umap_original_embeddings <- cbind(seurat_object$umap1, seurat_object$umap2)
p1 <- DimPlotSagnik(umap_original_embeddings, group.by = seurat_object$anatomical_annotation, pt.size = 1) +
  theme(plot.title = element_text(hjust = 0.5)) +
  ggtitle("UMAP by Anatomical Annotation")

p2 <- DimPlotSagnik(umap_original_embeddings, group.by = seurat_object$final_cluster_label, pt.size = 1) +
  theme(plot.title = element_text(hjust = 0.5)) +
  ggtitle("UMAP by Cluster Label")

# Combine plots side-by-side
combined_plot <- p1 + p2 + plot_layout(ncol = 2)
combined_plot

Version Author Date
4a09688 Ziang Zhang 2024-11-15

Trying EBMF

Let’s try to implement the EBMF algorithm with the flashier package, with different constraints and prior distributions.

Considering the large computational cost, we will first try to run the EBMF algorithm without backfitting.

cols_to_keep <- colSums(Y != 0, na.rm = TRUE) > 0
Y <- Y[, cols_to_keep]
n  <- nrow(Y)
x  <- rpois(1e7, 1/n)
s1 <- sd(log(x + 1))
a <- 1
size_factors <- rowSums(Y)
size_factors <- size_factors / mean(size_factors)
shifted_log_counts <- log1p(Y / (a * size_factors))

Non-Negative EBMF

For the first EBMF problem, we consider non-negative EBMF with point_exponential priors:

flashier_fit_nn <- flash(shifted_log_counts,
                      ebnm_fn = ebnm_point_exponential,
                      var_type = 2,
                      greedy_Kmax = 25,
                      S = s1,
                      backfit = F)
plot(flashier_fit_nn, 
     plot_type = "structure",
     pm_which = "loadings", 
     pm_groups = seurat_object$final_cluster_label,
     bins = 20, gap = 70)

Version Author Date
99a79f6 Ziang Zhang 2024-11-18
4a09688 Ziang Zhang 2024-11-15

Semi Non-Negative EBMF

ebnm_fn_list <- list()
ebnm_fn_list[[1]] <- flash_ebnm(
  prior_family = "point_exponential"
)
ebnm_fn_list[[2]] <- flash_ebnm(
  prior_family = "point_normal",
  mode = "estimate"
)
flashier_fit_semi <- flash(shifted_log_counts,
                      ebnm_fn = ebnm_fn_list,
                      var_type = 2,
                      greedy_Kmax = 25,
                      S = s1,
                      backfit = F)
plot(flashier_fit_semi, 
     plot_type = "structure",
     pm_which = "loadings", 
     pm_groups = seurat_object$final_cluster_label,
     bins = 20, gap = 70)

Version Author Date
99a79f6 Ziang Zhang 2024-11-18
4a09688 Ziang Zhang 2024-11-15

GB Semi Non-Negative EBMF

ebnm_fn_list <- list()
ebnm_fn_list[[1]] <- flash_ebnm(
  prior_family = "generalized_binary"
)
ebnm_fn_list[[2]] <- flash_ebnm(
  prior_family = "point_laplace",
  mode = "estimate"
)
flashier_fit_gbcd <- flash(shifted_log_counts,
                      ebnm_fn = ebnm_fn_list,
                      var_type = 2,
                      greedy_Kmax = 25,
                      S = s1,
                      backfit = F)
plot(flashier_fit_gbcd, 
     plot_type = "structure",
     pm_which = "loadings", 
     pm_groups = seurat_object$final_cluster_label,
     bins = 20, gap = 70)

Version Author Date
99a79f6 Ziang Zhang 2024-11-18
4a09688 Ziang Zhang 2024-11-15

GBCD

res.gbcd <- fit_gbcd(Y = Y, Kmax = 25, maxiter1 = 100,
                     maxiter2 = 50, maxiter3 = 50, 
                     prior = flash_ebnm(prior_family = "generalized_binary",
                                        scale = 0.04))
saveRDS(res.gbcd, "../output/mouse_embryo/res.gbcd.rds")

Trying Topic Models

Then, let’s try fitting a topic model to this dataset using FastTopics.

fasttopics_fit <- fit_topic_model(Y, k = 25)

Version Author Date
99a79f6 Ziang Zhang 2024-11-18

Summarize Structures from Loadings

There are some interesting structures of the loading based on the result of EBMF and FastTopics, that add new insights to the original UMAP visualization.

Non-negative EBMF

First, based on the non-negative EBMF result, there are some factors that are shared across multiple cell types. For example:

  • The factors k10 and k3 are shared across multiple types of neurons and glial cells.

  • The factor k5 is shared across Chondrocytes and Connective Tissue Progenitors.

  • The factor k2 is shared across Erythroid Progenitors and Hepatocytes.

  • The factor k9 is unique to Endothelial Cells.

  • The factor k7 is unique to White Blood Cells.

Let’s take a look at how the UMAF plot looks if we color each cell based on its composition of these factors.

Version Author Date
99a79f6 Ziang Zhang 2024-11-18

Semi-negative EBMF

The loadings from the semi-negative EBMF are more diverse, where each cell is composed of a larger number of factors. However, there are still both “unique” and “shared” factors across different cell types. For examples:

  • Factor k4 is shared across multiple types of neurons and glial cells.

  • Factor k2 is shared across Erythroid Progenitors and Hepatocytes.

  • Factor k3 is mostly unique to Myocytes and Cardiac Muscle.

  • Factor k12 is unique to Chondrocytes.

Version Author Date
99a79f6 Ziang Zhang 2024-11-18

FastTopics

The result from FastTopics looks quite similar to the semi-negative EBMF, where each cell is composed of a larger number of factors. For examples:

  • Factors k13 and k10 are shared across Erythroid Progenitors and Hepatocytes.

  • Factor k3 is unique to Hepatocytes.

  • Factor k8 is unique to White Blood Cells.

Version Author Date
99a79f6 Ziang Zhang 2024-11-18

Compare these methods


sessionInfo()
R version 4.3.1 (2023-06-16)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS Monterey 12.7.4

Matrix products: default
BLAS:   /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRblas.0.dylib 
LAPACK: /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRlapack.dylib;  LAPACK version 3.11.0

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

time zone: America/Chicago
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] fastTopics_0.6-192  gridExtra_2.3       ggpubr_0.6.0       
 [4] Biobase_2.62.0      BiocGenerics_0.48.1 RColorBrewer_1.1-3 
 [7] cowplot_1.1.3       patchwork_1.3.0     ggplot2_3.5.1      
[10] flashier_1.0.54     ebnm_1.1-34         data.table_1.16.2  
[13] Matrix_1.6-4        Seurat_5.1.0        SeuratObject_5.0.2 
[16] sp_2.1-4           

loaded via a namespace (and not attached):
  [1] RcppAnnoy_0.0.22       splines_4.3.1          later_1.3.2           
  [4] tibble_3.2.1           polyclip_1.10-7        fastDummies_1.7.4     
  [7] lifecycle_1.0.4        mixsqp_0.3-54          rstatix_0.7.2         
 [10] rprojroot_2.0.4        globals_0.16.3         lattice_0.22-6        
 [13] MASS_7.3-60            backports_1.5.0        magrittr_2.0.3        
 [16] plotly_4.10.4          sass_0.4.9             rmarkdown_2.28        
 [19] jquerylib_0.1.4        yaml_2.3.10            httpuv_1.6.15         
 [22] sctransform_0.4.1      spam_2.11-0            spatstat.sparse_3.1-0 
 [25] reticulate_1.39.0      pbapply_1.7-2          abind_1.4-8           
 [28] Rtsne_0.17             quadprog_1.5-8         purrr_1.0.2           
 [31] git2r_0.33.0           ggrepel_0.9.6          irlba_2.3.5.1         
 [34] listenv_0.9.1          spatstat.utils_3.1-0   goftest_1.2-3         
 [37] RSpectra_0.16-2        spatstat.random_3.3-2  fitdistrplus_1.2-1    
 [40] parallelly_1.38.0      leiden_0.4.3.1         codetools_0.2-20      
 [43] tidyselect_1.2.1       farver_2.1.2           matrixStats_1.4.1     
 [46] spatstat.explore_3.3-3 jsonlite_1.8.9         progressr_0.14.0      
 [49] Formula_1.2-5          ggridges_0.5.6         survival_3.7-0        
 [52] tools_4.3.1            progress_1.2.3         ica_1.0-3             
 [55] Rcpp_1.0.13-1          glue_1.8.0             xfun_0.48             
 [58] dplyr_1.1.4            withr_3.0.2            fastmap_1.2.0         
 [61] fansi_1.0.6            digest_0.6.37          truncnorm_1.0-9       
 [64] R6_2.5.1               mime_0.12              colorspace_2.1-1      
 [67] scattermore_1.2        gtools_3.9.5           tensor_1.5            
 [70] spatstat.data_3.1-2    RhpcBLASctl_0.23-42    utf8_1.2.4            
 [73] tidyr_1.3.1            generics_0.1.3         prettyunits_1.2.0     
 [76] httr_1.4.7             htmlwidgets_1.6.4      scatterplot3d_0.3-44  
 [79] deconvolveR_1.2-1      whisker_0.4.1          uwot_0.1.16           
 [82] pkgconfig_2.0.3        gtable_0.3.6           workflowr_1.7.1       
 [85] lmtest_0.9-40          htmltools_0.5.8.1      carData_3.0-5         
 [88] dotCall64_1.2          horseshoe_0.2.0        scales_1.3.0          
 [91] png_0.1-8              spatstat.univar_3.0-1  ashr_2.2-66           
 [94] knitr_1.48             rstudioapi_0.16.0      reshape2_1.4.4        
 [97] nlme_3.1-166           cachem_1.1.0           zoo_1.8-12            
[100] Polychrome_1.5.1       stringr_1.5.1          KernSmooth_2.23-24    
[103] parallel_4.3.1         miniUI_0.1.1.1         softImpute_1.4-1      
[106] pillar_1.9.0           grid_4.3.1             vctrs_0.6.5           
[109] RANN_2.6.2             promises_1.3.0         car_3.1-3             
[112] xtable_1.8-4           cluster_2.1.6          evaluate_1.0.1        
[115] invgamma_1.1           cli_3.6.3              compiler_4.3.1        
[118] rlang_1.1.4            crayon_1.5.3           SQUAREM_2021.1        
[121] future.apply_1.11.2    ggsignif_0.6.4         labeling_0.4.3        
[124] plyr_1.8.9             fs_1.6.4               stringi_1.8.4         
[127] viridisLite_0.4.2      deldir_2.0-4           munsell_0.5.1         
[130] lazyeval_0.2.2         spatstat.geom_3.3-3    RcppHNSW_0.6.0        
[133] hms_1.1.3              future_1.34.0          shiny_1.9.1           
[136] highr_0.11             trust_0.1-8            ROCR_1.0-11           
[139] igraph_2.1.1           broom_1.0.7            RcppParallel_5.1.9    
[142] bslib_0.8.0