sqjin / CellChat

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

ERROR in `netAnalysis_signalingRole_heatmap()`: Error in outgoing[, i] <- centr[[i]][[x.measure]] #559

Open ManuelMoradiellos opened 1 year ago

ManuelMoradiellos commented 1 year ago

Hi all!

I've used CellChat the previous month for an important experiment that needed some revisiting now. While looking at the update I wanted to use some of the new functions available to obtain a general overview of the pathways being "communicated" within my experiment.

In my experiment I have three different conditions that I store in CellChat objects like so (from scratch. not updating previous objects):

seurat <- readRDS('/local/mmoradiellos/work_stuff/post-TFM/breast.cancer_single_cell_collaboration/1_cellchat_work_per_se/input/seurat_new_annotation.RDS')

  experiment_conditions <- SplitObject(seurat, split.by = 'hypoxia_label') # Split by control, low-hypoxia or high-hypoxia
  names(experiment_conditions) <- c('control', 'high_hypoxia', 'low_hypoxia')
  experiment_conditions <- experiment_conditions[c(1,3,2)] # Reorder to have the timeline right

  # Data initialization and CellChat object creation separately per experiment condition
  cellchat_breast <- setNames(lapply(names(experiment_conditions), function(condition) { 
    data.input <- GetAssayData(experiment_conditions[[condition]], assay = 'RNA', slot = 'data') # Normalized data matrix
    labels <- Idents(experiment_conditions[[condition]]) # Cell types defined from Seurat's resulting clusters
    cell_type_df <- data.frame(group = labels, row.names = names(labels)) # To identify the different cell clusters

    cellchat_cond <- createCellChat(object = data.input, meta = cell_type_df, group.by = 'group') 
    cellchat_cond <- addMeta(cellchat_cond, meta = cell_type_df, meta.name = 'group') # Add cell information
    cellchat_cond <- setIdent(cellchat_cond, ident.use = 'group') # Set "labels" as default cell identity
    cellchat_cond@DB <- CellChatDB.mouse # Import Ligand-Receptor Database on Mouse
    cellchat_cond <- subsetData(cellchat_cond) # Uses whole database to extract all signaling genes
    future::plan('multisession', workers = 4)
    cellchat_cond <- identifyOverExpressedGenes(cellchat_cond)        # Identify Genes and Ligand-Receptor interactions
    cellchat_cond <- identifyOverExpressedInteractions(cellchat_cond) # from the database and adds it to the object
    cellchat_cond <- computeCommunProb(cellchat_cond, population.size = T) # scRNAseq experiment was unsorted, uses Chromium 10x Standard Procedure
    cellchat_cond <- filterCommunication(cellchat_cond, min.cells = 10) # Filter out groups with low cell count in groups, maybe not representative 
    cellchat_cond <- computeCommunProbPathway(cellchat_cond)  # Infers probability from expression value of genes and reference database
    cellchat_cond <- aggregateNet(cellchat_cond) 
    print( data.frame('cell_type' = levels(cellchat_cond@idents) , 'group' = as.numeric(table(cellchat_cond@idents))) ) # To check "composition" differences
    return(cellchat_cond) 
  }), names(experiment_conditions) )
  saveRDS(cellchat_breast, file = 'cellchat/cellchat_outputs/cellchat_object.cellclustersize_filtered.pop_size_F.RDS')

Next I follow the new part of the vignette (specifically Part IV) but whenever I want to use any of the functions on one of my experimental conditions-specific object I get an error:

cellchat_breast$low_hypoxia <- netAnalysis_computeCentrality(cellchat_breast$low_hypoxia, slot.name = "netP")
netAnalysis_signalingRole_heatmap(cellchat_breast$low_hypoxia, pattern = "outgoing")
Error in outgoing[, i] <- centr[[i]][[x.measure]] : 
  number of items to replace is not a multiple of replacement length

I don't know exactly what I'm doing wrong and when I checked for past issues some looked pretty similar (#479 ) but their solution didn't help me or it did not seem to fit what I want to do, which is working on separate objects per experimental condition to check overall trends to later focus on.

Thanks in advance and sorry if this is a stupid question

htejedam commented 1 year ago

Hey Manuel, I had the same issue as you when performing some comparisons between groups. First comparisons went smooth. But on the later ones I got the same error as you.

The problem was that not all the conditions shared cells on all clusters. Therefore if you have any cluster/cell group that is not present in one of the conditions the analysis won't run.

How to solve it. Just subset the your object on both conditions to make sure that you have the same clusters/cell groups and re-run the cellchat analysis.