CellChat icon indicating copy to clipboard operation
CellChat copied to clipboard

error in subsetCellChat function

Open rbatorsky opened this issue 2 years ago • 4 comments

Hi and thanks for the useful tool.

I found that subsetCellChat may have an error in selecting by idents.use.

Here is the example

load(url("https://ndownloader.figshare.com/files/25950872"))
data.input = data_humanSkin$data # normalized data matrix
meta = data_humanSkin$meta # a dataframe with rownames containing cell mata data
cell.use = rownames(meta)[meta$condition == "LS"] # extract the cell names from disease data
data.input = data.input[, cell.use]
meta = meta[cell.use, ]
cellchat <- createCellChat(object = data.input, meta = meta, group.by = "labels")
cellchat@DB <- CellChatDB.human
cellchat = subsetData(cellchat)
future::plan("multiprocess", workers = 4)
cellchat <- identifyOverExpressedGenes(cellchat)
cellchat <- identifyOverExpressedInteractions(cellchat)
cellchat <- computeCommunProb(cellchat)
cellchat <- filterCommunication(cellchat, min.cells = 10)
cellchat <- computeCommunProbPathway(cellchat)
cellchat <- aggregateNet(cellchat)

levels(cellchat@idents)

subset = subsetCellChat(
  cellchat,
  idents.use = c("cDC1","cDC2")
)

Which gives the error:

Error in values[group.existing.index, group.existing.index, drop = FALSE] : 
  incorrect number of dimensions

I think this comes from CellChat_class.R line 827:

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

Which should have an extra comma for the 3rd matrix dimension:

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

rbatorsky avatar Jan 10 '23 18:01 rbatorsky

Hi,

Thank you for pointing out the error. I did the subsetcellchat but it is not a cellchat object anymore. I created three cellchat objects for three conditions and merged them. Now I want to only look at interactions between specific clusters from one experiment only. Not sure what went wrong. My code:

`object.list <- list(Untreated = cellchat_untreated, trt1 = cellchat_trt1, trt2=cellchat_trt2) cellchat <- mergeCellChat(object.list, add.names = names(object.list))

#subset cell chat cellchat_subset_untreated<- subsetCellChat(object=cellchat_untreated,idents.use=c("Malignant","T-cells","Myeloid"))

par(mfrow = c(1,2), xpd=TRUE) netVisual_diffInteraction(cellchat_subset_untreated, weight.scale = T) netVisual_diffInteraction(cellchat_subset_untreated, weight.scale = T, measure = "weight") Error in object@net : no applicable method for@` applied to an object of class "factor"

Thank you in advance!

apal6 avatar Dec 21 '23 23:12 apal6

@apal6 You should do it as cellchat_subset_untreated<- subsetCellChat(object=cellchat,idents.use=c("Malignant","T-cells","Myeloid")) because netVisual_diffInteraction only works for comparison analysis between two conditions.

sqjin avatar Dec 29 '23 07:12 sqjin

Hi @sqjin. Thank you for your reply. I have three cell chat objects for three conditions. I took one of these and ran subset function and exactly how you suggested in the last commen and the error comes out to be:

cellchat_xrt_subset<- subsetCellChat(object=cellchat_xrt,idents.use=c("Malignant","T-cells","Myeloid")) The subset of cell groups used for CellChat analysis are T-cells Malignant Myeloid Error in [email protected][, cells.use.index] : subscript out of bounds

apal6 avatar Jan 22 '24 18:01 apal6

@apal6 You can run the source codes or share your object that can replicate your error.

sqjin avatar Jan 23 '24 01:01 sqjin