sqjin / CellChat

R toolkit for inference, visualization and analysis of cell-cell communication from single-cell data
GNU General Public License v3.0
640 stars 145 forks source link

How to use subsetCellChat? #210

Open lizanyu opened 3 years ago

lizanyu commented 3 years ago

Hello! Thank you for creating such an awesome tool! I'm following the tutorial for Comparison Analysis of Multiple Datasets and would like to subset the merged cellchat object for ease of analysis. I am trying to use subsetCellChat using vectors identified from cellchat@idents$joint:

sort(unique(cellchat@idents$joint))
subsetCellChat(object=cellchat, idents.use=c(6:10,12:13,19:23,25:26,32:36,38:39))

I get the following error message:

Use the joint cell labels from the merged CellChat object
The subset of cell groups used for CellChat analysis are   
Update slots object@net, object@netP, object@idents in dataset  TN 
Error in aperm.default(apply(prob, c(1, 2), by, group, sum), c(2, 3, 1)) : 'perm' is of wrong length 3 (!= 2)

How should I be identifying idents.use? Any help would be greatly appreciated!

Thanks, Liz

Here is my sessionInfo:

R version 4.0.2 (2020-06-22)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Mojave 10.14.6

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib

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

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

other attached packages:
[1] patchwork_1.1.1     CellChat_1.1.0      ggplot2_3.3.3       igraph_1.2.6        dplyr_1.0.6        
[6] Biobase_2.50.0      BiocGenerics_0.36.1

loaded via a namespace (and not attached):
 [1] jsonlite_1.7.2        foreach_1.5.1         network_1.16.1        assertthat_0.2.1     
 [5] stats4_4.0.2          ggrepel_0.9.1         globals_0.14.0        pillar_1.6.1         
 [9] lattice_0.20-41       glue_1.4.2            reticulate_1.20       digest_0.6.27        
[13] RColorBrewer_1.1-2    colorspace_2.0-1      cowplot_1.1.1         Matrix_1.3-2         
[17] plyr_1.8.6            pkgconfig_2.0.3       GetoptLong_1.0.5      listenv_0.8.0        
[21] purrr_0.3.4           xtable_1.8-4          scales_1.1.1          svglite_2.0.0        
[25] sna_2.6               ggalluvial_0.12.3     RSpectra_0.16-0       tibble_3.1.2         
[29] pkgmaker_0.32.2       generics_0.1.0        IRanges_2.24.1        ellipsis_0.3.2       
[33] withr_2.4.2           pbapply_1.4-3         gg.gap_1.3            magrittr_2.0.1       
[37] crayon_1.4.1          statnet.common_4.4.1  fansi_0.5.0           future_1.21.0        
[41] parallelly_1.25.0     doParallel_1.0.16     NMF_0.23.0            FNN_1.1.3            
[45] Cairo_1.5-12.2        tools_4.0.2           registry_0.5-1        GlobalOptions_0.1.2  
[49] lifecycle_1.0.0       matrixStats_0.58.0    gridBase_0.4-7        ComplexHeatmap_2.7.11
[53] stringr_1.4.0         S4Vectors_0.28.1      munsell_0.5.0         cluster_2.1.1        
[57] rngtools_1.5          irlba_2.3.3           compiler_4.0.2        systemfonts_1.0.2    
[61] rlang_0.4.11          grid_4.0.2            iterators_1.0.13      rjson_0.2.20         
[65] circlize_0.4.13.1001  gtable_0.3.0          codetools_0.2-18      DBI_1.1.1            
[69] reshape2_1.4.4        R6_2.5.0              rle_0.9.2             knitr_1.31           
[73] future.apply_1.7.0    utf8_1.2.1            clue_0.3-59           shape_1.4.6          
[77] stringi_1.6.2         Rcpp_1.0.6            vctrs_0.3.8           png_0.1-7            
[81] xfun_0.22             coda_0.19-4           tidyselect_1.1.1 
sqjin commented 3 years ago

@lizanyu There is no error when I tested the example data. Could you run the source codes to figure out the reason? You can also share your object with me if you like.

lizanyu commented 3 years ago

@sqjin I shared my repository with you (also for issue #201) -- I assume I'm just not selecting the correct 'idents.use'?? Thanks for your help!

zh-Bian commented 3 years ago

I think I find this reason in subsetCellChat. The error arises in the last step of building the CellChat object. The code for 'meta.subset' will be factor when the meta only have a col in 'meta.subset <- object@meta[cells.use.index, ]'. So I add One more step when to select the new meta.subset. The code like this.

if (ncol(object@meta)==1) { meta.subset <- data.frame(row.names = rownames(object@meta)[cells.use.index],xx = object@meta[cells.use.index,1]) colnames(meta.subset)[1] <- colnames(object@meta) }else{meta.subset <- object@meta[cells.use.index, ]}

@lizanyu

rbatorsky commented 1 year ago

I encountered this problem as well in cellchat v 1.6.1.

I think it's caused by not subsetting the network components correctly that have a separate matrix for each pathway:

values.new <- values[group.existing.index, group.existing.index, drop = FALSE]

Should be the following, in order to do the same for all pathways:

values.new <- values[group.existing.index, group.existing.index, ,drop = FALSE]

The code block is here:

    # cat("Update slot object@net...", '\n')
    net <- object@net

    for (net.j in names(net)) {
      values <- net[[net.j]]
      if (net.j %in% c("prob","pval")) {
        values.new <- values[group.existing.index, group.existing.index, , drop = FALSE]
        net[[net.j]] <- values.new
      }
      if (net.j %in% c("count","sum","weight")) {
        values.new <- values[group.existing.index, group.existing.index, drop = FALSE]
        net[[net.j]] <- values.new
      }
    }
    net.subset <- net
ywangaz commented 1 year ago

Hi, may I check if this issue has been fixed in cellchat? I'm still getting Update slots object@images, object@net, object@netP in a single dataset... Error in values[group.existing.index, group.existing.index, drop = FALSE] : incorrect number of dimensions

TzuTingWei commented 1 year ago

I am also working with CellChat 1.6.1 and the code block from @rbatorsky solved the matrix dimension problem.

Another reason behind the incorrect number of dimensions happens when using the cells.use to subset the CellChat object, the level.use will return a NULL since names(labels) is NULL.

The solution for me was the following:

labels <- object@idents # This is a factor without names
names(labels) = colnames(object@data) # The new line to name the factor by cell id and fix the level.use is NULL issue
level.use0 <- levels(labels)
level.use = levels(labels)[levels(labels) %in% unique(as.character(labels[cells.use]))] 
cells.use.index <- which(names(labels) %in% cells.use)
sqjin commented 1 year ago

@TzuTingWei Thanks. I will fix this issue by using your solution.

JiahaoWongg commented 1 year ago

line 827 in R/CellChat_class.R maybe missing a comma , should be:

values.new <- values[group.existing.index, group.existing.index, , drop = FALSE]

that's work for me!

QianhuiXu commented 1 year ago

Hello,

cellchat is a wonderful tool! I ran into the same issue with running subsetCellChat with my data.

code: cellchat_interface20_subset <- subsetCellChat( cellchat_interface20, cells.use = c('Cholangiocyte',"M.APOE"), idents.use = NULL, group.by = NULL, invert = FALSE, thresh = 0.05 )

error: The subset of cell groups used for CellChat analysis are
Update slots object@images, object@net, object@netP in a single dataset... Error in values[group.existing.index, group.existing.index, drop = FALSE] : 量度数目不对

I'm really at a loss as to how to proceed, and any guidance would be much appreciated! Thank you for your kind help!

sqjin commented 1 year ago

Hello,

cellchat is a wonderful tool! I ran into the same issue with running subsetCellChat with my data.

code: cellchat_interface20_subset <- subsetCellChat( cellchat_interface20, cells.use = c('Cholangiocyte',"M.APOE"), idents.use = NULL, group.by = NULL, invert = FALSE, thresh = 0.05 )

error: The subset of cell groups used for CellChat analysis are Update slots object@images, object@net, object@netP in a single dataset... Error in values[group.existing.index, group.existing.index, drop = FALSE] : 量度数目不对

I'm really at a loss as to how to proceed, and any guidance would be much appreciated! Thank you for your kind help!

@QianhuiXu Can you share me your CellChat object so that I can replicate your error?

QianhuiXu commented 1 year ago

I have sent my CellChat object to your email, Thank you very much!

sqjin commented 1 year ago

I have sent my CellChat object to your email, Thank you very much!

Received it. will take into it later today.

apal6 commented 10 months ago

Hi,

I have three conditions and I created three cellchat objects to look communication between specific clusters at individual treatment level. BUT I am getting an error while using subsetCellChat: subsetCellChat(object=cellchat, idents.use=c("T-cells","Myeloid")) The subset of cell groups used for CellChat analysis are T-cells Myeloid Update slots object@images, object@net, object@netP in a single dataset... Error in values[group.existing.index, group.existing.index, drop = FALSE] : incorrect number of dimensions

Would appreciate your help! Thank you in advance.

sqjin commented 10 months ago

@apal6 No idea on this error except that I can replicate your error using your cellchat object.

apal6 commented 10 months ago

@sqjin, sorry to bother but I am still getting the same error even when I ran the subset function on individual cellchat object (one object for one condition). I am happy to share my cellchat object if you need to debug? I highly appreciate your time. Thank you!

sqjin commented 10 months ago

@apal6 You can find my email in the Description file.

apal6 commented 10 months ago

@QianhuiXu, how were you able to debug? I got this error too. Thank you!