Run CellChat, endometrial atlas

Published

June 30, 2025

Load packages

suppressPackageStartupMessages({
    library(CellChat)
    library(SingleCellExperiment)
    library(future)
    library(patchwork)
    library(NMF)
    library(ggalluvial)
    library(ComplexHeatmap)
    library(dplyr)
})

options(future.globals.maxSize = 10000 * 1024 ^ 2, 
        future.seed = TRUE,
        future.rng.onMisuse = "ignore")

## Decide whether to force re-execution of CellChat even if files exist
force_cellchat <- TRUE

Read data

sce <- readRDS("invivo_cell_atlas.rds")
clustercolumn <- "celltype_annot"

table(sce$Stage, useNA = "ifany")

early-mid-secretory     early-secretory      late-secretory           menstrual 
              37466               24385               16459                2233 
      mid-secretory       proliferative 
              30137               62887 
# Define stages to include
stages <- list(menstrual_proliferative = c("menstrual", "proliferative"))

Run CellChat (for each stage)

minClusterSize <- 100

for (stgn in names(stages)) {
    if (!file.exists(paste0("cellchat_", stgn, ".rds")) || force_cellchat) {
        sub <- sce[, sce$Stage %in% stages[[stgn]]]
        
        ## Remove rare cell types
        tbl <- table(sub[[clustercolumn]])
        kp <- names(tbl)[tbl >= minClusterSize]
        sub <- sub[, sub[[clustercolumn]] %in% kp]
        
        ## Create CellChat object
        cellchat <- createCellChat(object = sub, group.by = clustercolumn)
        
        ## Define which CellChatDB to use
        CellChatDB <- CellChatDB.human
        
        ## Potentially subset the CellChatDB
        CellChatDB.use <- subsetDB(CellChatDB, search = "Secreted Signaling")
        # CellChatDB.use <- CellChatDB
        cellchat@DB <- CellChatDB.use
        
        ## Subset to only signalling genes
        cellchat <- subsetData(cellchat)
        future::plan("multisession", workers = 8)
        
        ## Process the expression data
        cellchat <- identifyOverExpressedGenes(cellchat)
        cellchat <- identifyOverExpressedInteractions(cellchat)
        
        ## Infer cell-cell communication network
        cellchat <- computeCommunProb(cellchat, raw.use = TRUE, type = "triMean", 
                                      population.size = FALSE)
        ## To apply less stringent filtering
        # cellchat <- computeCommunProb(cellchat, raw.use = TRUE, type = "truncatedMean", 
        #                               trim = 0.1)
        
        ## Filter out the cell-cell communication if there are only few cells
        cellchat <- filterCommunication(cellchat, min.cells = minClusterSize)
        
        ## Calculate communication probability on pathway level
        cellchat <- computeCommunProbPathway(cellchat)
        
        ## Aggregated cell-cell communication network by counting the number of
        ## links or summarizing the communication probability
        cellchat <- aggregateNet(cellchat)
        
        ## Compute network centrality scores
        ## Unless 'future.rng.onMisuse' is set to "ignore", this 
        ## generates warnings about random numbers being generated without
        ## this being set
        cellchat <- netAnalysis_computeCentrality(cellchat, slot.name = "netP")
        
        ## Save object
        saveRDS(cellchat, file = paste0("cellchat_", stgn, ".rds"))
        
        ## Close background workers
        future::plan("sequential")
    }
}
[1] "Create a CellChat object from a SingleCellExperiment object"
The `logcounts` assay is used 
The `colData` assay in the SingleCellExperiment object is used as cell meta information 
Set cell identities for the new CellChat object 
The cell groups used for CellChat analysis are  Arterial B_cell cDC2 ciliated cycling dStromal eM1 eM2 ePV eStromal Fibroblast_basalis hormone responsive early hormone responsive late HOXA13 ILC3 luminal early Monocyte mPV pDC Peripheral_lymphocyte pre-ciliated remodeling secretory mid sHormones T_cell_CD4 T_cell_CD8 T_cell_cycling T_Reg transcriptionally active uNK1 uNK2 uNK3 uSMCs Venous 
triMean is used for calculating the average gene expression per cell group. 
[1] ">>> Run CellChat on sc/snRNA-seq data <<< [2025-06-30 21:30:33.605831]"
[1] ">>> CellChat inference is done. Parameter values are stored in `object@options$parameter` <<< [2025-06-30 22:48:16.290903]"

Session info

sessionInfo()
R version 4.4.2 (2024-10-31)
Platform: x86_64-pc-linux-gnu
Running under: Rocky Linux 8.10 (Green Obsidian)

Matrix products: default
BLAS/LAPACK: FlexiBLAS OPENBLAS;  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: Europe/Zurich
tzcode source: system (glibc)

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

other attached packages:
 [1] ComplexHeatmap_2.22.0       ggalluvial_0.12.5          
 [3] NMF_0.28                    cluster_2.1.6              
 [5] rngtools_1.5.2              registry_0.5-1             
 [7] patchwork_1.3.0             future_1.40.0              
 [9] SingleCellExperiment_1.28.1 SummarizedExperiment_1.36.0
[11] GenomicRanges_1.58.0        GenomeInfoDb_1.42.3        
[13] IRanges_2.40.1              S4Vectors_0.44.0           
[15] MatrixGenerics_1.18.1       matrixStats_1.5.0          
[17] CellChat_1.6.1              bigmemory_4.6.4            
[19] Biobase_2.66.0              BiocGenerics_0.52.0        
[21] ggplot2_3.5.2               igraph_2.1.4               
[23] dplyr_1.1.4                

loaded via a namespace (and not attached):
  [1] pbapply_1.7-2           rlang_1.1.6             magrittr_2.0.3         
  [4] clue_0.3-66             GetoptLong_1.0.5        gridBase_0.4-7         
  [7] compiler_4.4.2          systemfonts_1.2.2       png_0.1-8              
 [10] vctrs_0.6.5             reshape2_1.4.4          stringr_1.5.1          
 [13] pkgconfig_2.0.3         shape_1.4.6.1           crayon_1.5.3           
 [16] fastmap_1.2.0           XVector_0.46.0          backports_1.5.0        
 [19] scuttle_1.16.0          rmarkdown_2.29          UCSC.utils_1.2.0       
 [22] network_1.19.0          purrr_1.0.4             bluster_1.16.0         
 [25] xfun_0.52               beachmat_2.22.0         zlibbioc_1.52.0        
 [28] jsonlite_2.0.0          DelayedArray_0.32.0     BiocParallel_1.40.2    
 [31] uuid_1.2-1              broom_1.0.8             irlba_2.3.5.1          
 [34] parallel_4.4.2          R6_2.6.1                stringi_1.8.7          
 [37] RColorBrewer_1.1-3      limma_3.62.2            reticulate_1.42.0      
 [40] parallelly_1.43.0       car_3.1-3               Rcpp_1.0.14            
 [43] iterators_1.0.14        knitr_1.50              future.apply_1.11.3    
 [46] FNN_1.1.4.1             Matrix_1.7-1            tidyselect_1.2.1       
 [49] dichromat_2.0-0.1       abind_1.4-8             yaml_2.3.10            
 [52] doParallel_1.0.17       codetools_0.2-20        listenv_0.9.1          
 [55] lattice_0.22-6          tibble_3.2.1            plyr_1.8.9             
 [58] withr_3.0.2             coda_0.19-4.1           evaluate_1.0.3         
 [61] circlize_0.4.16         pillar_1.10.2           BiocManager_1.30.25    
 [64] ggpubr_0.6.0            carData_3.0-5           foreach_1.5.2          
 [67] generics_0.1.3          scales_1.3.0.9000       ggnetwork_0.5.13       
 [70] globals_0.17.0          glue_1.8.0              metapod_1.14.0         
 [73] tools_4.4.2             BiocNeighbors_2.0.1     ScaledMatrix_1.14.0    
 [76] RSpectra_0.16-2         locfit_1.5-9.12         ggsignif_0.6.4         
 [79] scran_1.34.0            cowplot_1.1.3           tidyr_1.3.1            
 [82] edgeR_4.4.2             colorspace_2.1-1        GenomeInfoDbData_1.2.13
 [85] BiocSingular_1.22.0     rsvd_1.0.5              Formula_1.2-5          
 [88] cli_3.6.4               bigmemory.sri_0.1.8     S4Arrays_1.6.0         
 [91] svglite_2.1.3           gtable_0.3.6            rstatix_0.7.2          
 [94] digest_0.6.37           dqrng_0.4.1             SparseArray_1.6.2      
 [97] ggrepel_0.9.6           rjson_0.2.23            htmlwidgets_1.6.4      
[100] sna_2.8                 farver_2.1.2            htmltools_0.5.8.1      
[103] lifecycle_1.0.4         httr_1.4.7              statmod_1.5.0          
[106] GlobalOptions_0.1.2     statnet.common_4.11.0