jinworks / CellChat

R toolkit for inference, visualization and analysis of cell-cell communication from single-cell and spatially resolved transcriptomics
GNU General Public License v3.0
277 stars 45 forks source link

Error when some clusters are not presented in some of the datasets #53

Open mvfki opened 9 months ago

mvfki commented 9 months ago

Hi CellChat team,

So the story is like this. I had a multi-sample analysis from elsewhere and had joint cluster labels assigned already. Then I came to CellChat, followed the tutorials to have single-dataset analyses done first, and merged the CellChat objects for the cross-comparisons afterward. I ended up seeing an error saying uncomfortable dimensions when running netVisual_diffInteraction().

I dug into it a bit myself and kind of know what the issue is now. In my prior integrative clustering analysis, the smallest cluster does not have any cells contributed from a couple of my datasets. There are totally 28 clusters, but as a result, those datasets only have 27 clusters if I do a subset for them and create the single-dataset CellChat objects. And that's why when it reaches that visualization function at this line, R refuses to calculate the subtraction of a 28x28 matrix from a 27x27 matrix.

The next thing I tried was to preserve the 28 levels for the cluster label subset even if there are only 27 unique values in the factor object, for the "ident". This ended up failing a sanity check at this line. I'm wondering if the model has any limitations with allowing unexisting levels when calculating the probability.

What I'm planning to try next is to manually insert all-0 rows and columns to the merged object at cellchat.merged@net[[datasetName]]$count to address the fact that there's another cluster but no interaction is detected from there.

Overall, I think my case could be something realistic, and any fix towards such issue would be very appreciated.

Best, Yichen

sqjin commented 9 months ago

@mvfki You should drop the un-used clusters when running cellchat on an individual dataset (you can check droplevels fucntion) and then merged the two cellchat object by following the toturial "Comparison analysis of multiple datasets with different cell type compositions

mvfki commented 9 months ago

I did exactly the same. Anyway, I figured out that the real issue is that I should have biological replicates of the same condition as one individual dataset, instead of having each replicate a dataset, if the cellchat paper indicates this. Then I don't actually have any "level" that needs to be dropped out.