Skip to content

SpatialCellChat::computeCommunProb on CosMx data #455

@ynpan910

Description

@ynpan910

Hi,

Thanks for developing the new version of CellChat that's more recommended for imaging-based ST data. I'm running it on one example CosMx NSCLC lung sample data provided on the vendor website. The CosMx data has one sample with 81,236 cells and 960 gene panel.

I'm having errors with the SpatialCellChat::computeCommunProb() function which shows

Error in `vec_rbind()`:
! Negative `n` in `compact_rep()`.
ℹ In file utils.c at line 897.
ℹ Install the winch package to get additional debugging info the next time you get this error.
ℹ This is an internal error that was detected in the vctrs package.
  Please report it at <https://github.com/r-lib/vctrs/issues> with a reprex and the full backtrace.
Backtrace:
    ▆
 1. ├─purrr::map_dfr(...)
 2. │ └─dplyr::bind_rows(res, .id = .id)
 3. │   └─vctrs::vec_rbind(!!!dots, .names_to = .id, .error_call = current_env())
 4. └─rlang:::stop_internal_c_lib(...)
 5.   └─rlang::abort(message, call = call, .internal = TRUE, .frame = frame)

I digged into the bug and found that the error comes from the purrr::map_dfr() function in the SpatialCellChat::computeCellDistance function. And the error is probably due to the large cell number in the sample. Because I played around and changed the NC which is the cell number in the the chunk below to a smaller number gradually (i.e., from 81,236 to a smaller value). I found that when NC is 20,000, the function worked fine. But it starts to show the error when NC is 30,000.

SparseMatSlots <- purrr::map_dfr(.x = 1:NC, .f = function(j) {
  i = which(d.spatial[, j] <= interaction.range.threshold & 
              d.spatial[, j] > 0)
  x = d.spatial[i, j, drop = T]
  j = rep.int(j, length(i))
  return(data.frame(i = i, j = j, x = x))
}, .progress = T)

I understand that the error is not due to SpatialCellChat but a different package. But I'm wondering if you have any workaround suggestions? The CellChat v2 works perfectly on this sample and I'm thinking about upgrading to the v3 to compute single-cell level estimates.

Just FYI here is the codes I used before the error step:

# spatial coords
spatial.locs = seu@meta.data %>% dplyr::select(sdimx, sdimy)
colnames(spatial.locs) <- c("x", "y")

# spatial factors
conversion.factor = 0.18
d = CellChat:: computeCellDistance(spatial.locs)
spot.size = min(d)*conversion.factor
spatial.factors = list(ratio = conversion.factor, tol = spot.size/2)

#DB
chat@DB <- CellChatDB.use

# preprocess
chat <- createSpatialCellChat(
  object = seu, # Seurat
  group.by = "cell_type_me",
  assay = "SpaNorm", # normalized
  datatype = "spatial",
  coordinates = spatial.locs,
  spatial.factors = spatial.factors
)

chat <- subsetData(chat)
chat <- preProcessing(chat) 

chat <- CellChat:: identifyOverExpressedGenes( # smaller gene panel so skip the feature selection step)
  chat,
  do.DE = F,
  min.cells = 10
)
chat <- SpatialCellChat:: identifyOverExpressedInteractions(
  chat,
  variable.both = F # only require that either ligand or receptor from one pair is over-expressed
)

# inference
chat <- SpatialCellChat:: computeCommunProb(
  chat,
  distance.use = F, scale.distance = NULL,
  contact.dependent = T,
  interaction.range = 250, contact.range = 10
)
sessionInfo()
R version 4.5.2 (2025-10-31)
Platform: x86_64-pc-linux-gnu
Running under: Red Hat Enterprise Linux 9.3 (Plow)

Matrix products: default
BLAS:   /stornext/System/data/software/rhel/9/base/tools/R/4.5.2/lib64/R/lib/libRblas.so 
LAPACK: /stornext/System/data/software/rhel/9/base/tools/R/4.5.2/lib64/R/lib/libRlapack.so;  LAPACK version 3.12.1

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               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    LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C             LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

time zone: Australia/Melbourne
tzcode source: system (glibc)

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

other attached packages:
 [1] future_1.58.0         pheatmap_1.0.13       viridis_0.6.5         viridisLite_0.4.2     patchwork_1.3.1.9000 
 [6] lubridate_1.9.4       forcats_1.0.0         stringr_1.5.1         purrr_1.1.0           readr_2.1.5          
[11] tidyr_1.3.1           tibble_3.3.0          tidyverse_2.0.0       Seurat_5.0.1.9001     SeuratObject_5.1.0   
[16] sp_2.2-0              CellChat_2.2.0        ggplot2_3.5.2         optparse_1.7.5        SpatialCellChat_0.1.0
[21] Biobase_2.68.0        BiocGenerics_0.54.0   generics_0.1.4        igraph_2.2.0          dplyr_1.1.4          

loaded via a namespace (and not attached):
  [1] RcppAnnoy_0.0.22       splines_4.5.2          later_1.4.2            polyclip_1.10-7        ggnetwork_0.5.13      
  [6] fastDummies_1.7.5      lifecycle_1.0.4        ALRA_0.0.0.9000        rstatix_0.7.2          doParallel_1.0.17     
 [11] globals_0.18.0         lattice_0.22-7         MASS_7.3-65            backports_1.5.0        magrittr_2.0.3        
 [16] plotly_4.11.0          sass_0.4.10            jquerylib_0.1.4        httpuv_1.6.16          NMF_0.28              
 [21] sctransform_0.4.2      spam_2.11-1            spatstat.sparse_3.1-0  reticulate_1.42.0      cowplot_1.2.0         
 [26] pbapply_1.7-2          RColorBrewer_1.1-3     abind_1.4-8            Rtsne_0.17             pracma_2.4.6          
 [31] circlize_0.4.16        IRanges_2.42.0         S4Vectors_0.48.0       ggrepel_0.9.6          irlba_2.3.5.1         
 [36] listenv_0.9.1          spatstat.utils_3.2-1   goftest_1.2-3          RSpectra_0.16-2        spatstat.random_3.4-4 
 [41] fitdistrplus_1.2-4     parallelly_1.45.0      svglite_2.2.1          leiden_0.4.3.1         codetools_0.2-20      
 [46] getopt_1.20.4          tidyselect_1.2.1       shape_1.4.6.1          farver_2.1.2           matrixStats_1.5.0     
 [51] stats4_4.5.2           spatstat.explore_3.7-0 jsonlite_2.0.0         GetoptLong_1.0.5       BiocNeighbors_2.2.0   
 [56] ks_1.15.1              progressr_0.15.1       Formula_1.2-5          ggridges_0.5.6         ggalluvial_0.12.5     
 [61] survival_3.8-3         iterators_1.0.14       systemfonts_1.2.3      foreach_1.5.2          progress_1.2.3        
 [66] tools_4.5.2            sna_2.8                ica_1.0-3              Rcpp_1.1.0             glue_1.8.0            
 [71] gridExtra_2.3          withr_3.0.2            BiocManager_1.30.26    fastmap_1.2.0          rsvd_1.0.5            
 [76] digest_0.6.37          timechange_0.3.0       R6_2.6.1               mime_0.13              textshaping_1.0.1     
 [81] colorspace_2.1-1       scattermore_1.2        tensor_1.5.1           spatstat.data_3.1-9    data.table_1.17.8     
 [86] FNN_1.1.4.1            prettyunits_1.2.0      httr_1.4.7             htmlwidgets_1.6.4      uwot_0.2.3            
 [91] pkgconfig_2.0.3        gtable_0.3.6           registry_0.5-1         ComplexHeatmap_2.25.2  lmtest_0.9-40         
 [96] htmltools_0.5.8.1      carData_3.0-5          dotCall64_1.2          zigg_0.0.2             clue_0.3-66           
[101] scales_1.4.0           png_0.1-8              spatstat.univar_3.1-6  rstudioapi_0.17.1      tzdb_0.5.0            
[106] reshape2_1.4.4         rjson_0.2.23           coda_0.19-4.1          statnet.common_4.12.0  nlme_3.1-168          
[111] cachem_1.1.0           zoo_1.8-14             GlobalOptions_0.1.2    KernSmooth_2.23-26     parallel_4.5.2        
[116] miniUI_0.1.2           pillar_1.11.0          grid_4.5.2             vctrs_0.6.5            RANN_2.6.2            
[121] promises_1.3.3         ggpubr_0.6.1           car_3.1-3              xtable_1.8-4           cluster_2.1.8.1       
[126] mvtnorm_1.3-3          cli_3.6.5              compiler_4.5.2         rlang_1.1.6            crayon_1.5.3          
[131] rngtools_1.5.2         future.apply_1.20.0    ggsignif_0.6.4         mclust_6.1.1           plyr_1.8.9            
[136] stringi_1.8.7          network_1.19.0         deldir_2.0-4           gridBase_0.4-7         lazyeval_0.2.2        
[141] spatstat.geom_3.7-0    Matrix_1.7-4           RcppHNSW_0.6.0         hms_1.1.3              shiny_1.11.1          
[146] ROCR_1.0-11            Rfast_2.1.5.1          broom_1.0.8            RcppParallel_5.1.10    bslib_0.9.0    

Thank you so much! Appreciate any help!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions