edward130603 / BayesSpace

Bayesian model for clustering and enhancing the resolution of spatial gene expression experiments.
http://edward130603.github.io/BayesSpace
Other
106 stars 21 forks source link

Cluster structure lost on enhancement #77

Closed briglo closed 2 years ago

briglo commented 2 years ago

Hi, thanks for an awesome tool! I was making my way through some sections using the tutorial as a guide (optimising number o clusters and jitter scales) but on two of our 4 samples, cluster coherence was lost on spatial enhancement. For the other two the following approach worked a treat- but spatially enhanced clusters at least nominally resembled the low-res ones.

code is

C1<-readVisium("PATH/TO/SPACERANGER/out")
indat<-spatialPreprocess(C1 platform="Visium", log.normalize=TRUE)
indat<-qTune(indat, qs=seq(2, 10), platform="Visium", d=12)
#qval 7 at/near elbow
indat <- spatialCluster(indat, q=qval, platform="Visium", d=12,init.method="mclust", model="t", gamma=2,nrep=1000, burn.in=100, save.chain=TRUE)
tmp.enhanced <- spatialEnhance(indat, q=qval, platform="Visium", d=12, model="t", gamma=2,jitter_prior=0.3,jitter_scale=2,nrep=1000, burn.in=100, save.chain=TRUE)
#mcmcChain(tmp.enhanced, "Ychange"): stabilises at 0.3 (although changing this didnt affect the outcome
clusterPlot(indat) | clusterPlot(tmp.enhanced)

gives the attached cluster plot

I was wondering if you have seen this before. Is there a way around it/am i missing something/ or is there not enough structure in the data itself to support the clustering?- this is possible ; the section itself was more homogeneous

Thanks again and do feel free to redirect me (i did notice a similar looking thing can happen if coordinates are explicitly declared, but i didnt do that here). Regards

edward130603 commented 2 years ago

It's not immediately clear to me what went wrong here. Your code seems fine. I ran the code using your parameters on our data and did not notice anything strange. So I'm not sure what's going on, but the right hand side should not turn out that way. The only time I've seen something similar is in #58 but that shouldn't happen here since you are using BayesSpace functions to read data (not converting from Seurat).

A couple ideas to try:

briglo commented 2 years ago

Hi Thanks a tonne for getting back so quickly. I was baffled to because it worked so well out of the box on the other sections

I tried your suggestions plus remaking the object with fewer clusters and 10k reps on the clustering. And for all i got variants of

this console output while enhancing

and

This output from your plot suggestions

Your surmise is correct, it collapses on the clusterPlot(tmp.enhanced, mcmcChain(tmp.enhanced, "z")[2,])

Thank you for your help, if you have other ideas, ill give them a go. Regards B

edward130603 commented 2 years ago

Would you be able to send me a .RDS file of your indat data object? For confidentiality and file size, can set counts(indat) = NULL and logcounts(indat)=NULL. I just need PCs and coordinates really. My email is ezhao(at)fredhutch[dot]org.

Not sure whether the problem is in the data our our code, but I can try to figure it out.

briglo commented 2 years ago

Done, thanks again ; very much appreciated B

edward130603 commented 2 years ago

Thanks for sending. I think I've figured out the problem. For some reason, the row/col and imagerow/imagecol are mismatched.

library(BayesSpace)
sce = readRDS("indat_for_edward.RDS")
sce.enhanced = spatialEnhance(sce, q = length(unique(sce$spatial.cluster)),
                              verbose = T, burn.in = 100, nrep = 200)
clusterPlot(sce.enhanced)
plot(sce$imagecol~sce$col)
plot(sce$imagerow~sce$row)
plot(sce$imagecol~sce$row)
plot(sce$imagerow~sce$col)

image

We expect row and imagerow to be linearly associated (rather than row and imagecol). So this will definitely mess up the spatialEnhance neighbor finding algorithm (which is different from that of spatialCluster, which I think is what happened to get those really scrambled looking results. This fixed it for me:

#fix coordinates
sce2 = sce
sce2$imagecol.new = sce2$imagerow
sce2$imagerow = sce2$imagecol
sce2$imagecol = sce2$imagecol.new
sce.enhanced.fix = spatialEnhance(sce2, q = length(unique(sce$spatial.cluster)),
                                  verbose = T, burn.in = 100, nrep = 200)
clusterPlot(sce.enhanced.fix)

I'm not sure if there's some updates with SpaceRanger that caused the column switching issue. Any chance there was an issue with how SpaceRanger was run? (I don't have experience using it)

briglo commented 2 years ago

Thats awesome thanks so much! New Results are gorgeous

So this was run on spaceranger 1.3 and i'm willing to bet its because the flag --reorient-images was used (there was some sort of issue with fiducials lining up), those image flip coords things look suspiciously familiar

Awesome to have a fix! Thanks for everything