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
294 stars 52 forks source link

problem cellchat spatial analysis in CosMX analysis #76

Open giulialopatriello95 opened 9 months ago

giulialopatriello95 commented 9 months ago

Hi there!

I would like to apply Cellchat spatial analysis on CosMX data.

#READ SLIDE DATA AND SELECT SPECIFIC FOV
nano.obj__3_filter = readRDS("nano.obj__3_filter.RDS")
seurat_object <-subset_opt(nano.obj__3_filter ,  fov %in% c("1", "2"))

#ADD FOV INFORMATION TO METADATA
seurat_object$slices<-as.factor(seurat_object$fov)
#ADD CELLType INFORMATION TO METADATA
seurat_object$annot<-Idents(seurat_object)

#GET COORDINATES PER CELL AND SHIFTED NEGATIVE  COORDINATES VALUE 
spatial.locs=Seurat::GetTissueCoordinates(seurat_object)
rownames(spatial.locs)<-spatial.locs$cell
spatial.locs<-spatial.locs[,1:2]
spatial.locs$x<-(min(spatial.locs$x)*(-1))+spatial.locs$x
spatial.locs$y<-(min(spatial.locs$y)*(-1))+spatial.locs$y

#ADJUSTING CONVERSION FACTOR AS COSMX DATA
conversion.factor = 0.18
d = computeCellDistance(spatial.locs)
spot.size = min(d)*conversion.factor # converting the distance in Pixels to Micrometers
spatial.factors = data.frame(ratio = conversion.factor, tol = spot.size/2)

#CREATE CELLCHAT OBJECT
cellchat <- createCellChat(object = seurat_object, meta = seurat_object@meta.data,group.by="annot",
                           datatype = "spatial", coordinates = spatial.locs, scale.factors = spatial.factors)
cellchat@meta$annot = droplevels(cellchat@meta$annot, exclude = setdiff(levels(cellchat@meta$annot),unique(cellchat@meta$annot)))
cellchat<- setIdent(cellchat, ident.use = "annot") # set "labels" as default cell identity
groupSize <- as.numeric(table(cellchat@idents)) # number of cells in each cell group
CellChatDB <- CellChatDB.human # use CellChatDB.mouse if running on mouse data
showDatabaseCategory(CellChatDB)
cellchat@DB <- CellChatDB
cellchat <- subsetData(cellchat) # This step is necessary even if using the whole database
future::plan("multisession", workers = 4) # do parallel
cellchat <- identifyOverExpressedGenes(cellchat)
cellchat <- identifyOverExpressedInteractions(cellchat)
cellchat <- computeCommunProb(cellchat, type = "truncatedMean", trim = 0.1)

In computeCommunProb step, I am getting this type of error:

The suggested minimum value of scaled distances is in [1,2], and the calculated value here is  0.03135326 
Error in computeCommunProb(cellchat, type = "truncatedMean", trim = 0.1) : 
  Please increase the value of `scale.distance` and check the suggested values in the parameter description (e.g., 1, 0.1, 0.01, 0.001, 0.11, 0.011)

which is not resolving if i am changing the parameter to the suggested values. I've succedeed to process the data if I am setting distance.use=FALSE but I would like to process the analysis with this parameter setted as true.

Best regards, Giulia

der4005 commented 9 months ago

I think you need a number that will put 0.03135... into the range [1, 2]. For you, setting scale.distance to 32 should work. While this should get the function working I'm not sure what the implications of the error are. I had a similar issue with Visium replicates that were mapped onto a single graph space using Spiral and I needed to set scale.distance to 134. This is obviously very far from the suggested values so I guess interpret with caution until the author or someone else that understands the how the function works can chime in here.

sqjin commented 9 months ago

@der4005 I am not clear how Spiral aligns the spatial locations. But for each replicate before alignment, when you run the code below `spot.size = 65 # the theoretical spot size (um) in 10X Visium conversion.factor = spot.size/scalefactors$spot_diameter_fullres spatial.factors = data.frame(ratio = conversion.factor, tol = spot.size/2)

d.spatial2 <- CellChat::computeCellDistance(coordinates = w10x@images$slice1@coordinates[, c("imagerow", "imagecol")], interaction.range = NULL, ratio = spatial.factors$ratio, tol = spatial.factors$tol) min(d.spatial2[d.spatial2!=0])`

you should get a minimum distance around 100um. You can also check if this holds for the aligned coordinates.

sqjin commented 9 months ago

@giulialopatriello95 Hi Giulia, I am wondering if you have worked it out? If you re-install CellChat package, you can get a suggested value of scale.distance . In addition, we changed seurat_object$slices to seurat_object$samples in the new version.