JEFworks-Lab / STdeconvolve

Reference-free cell-type deconvolution of multi-cellular spatially resolved transcriptomics data
http://jef.works/STdeconvolve/
99 stars 11 forks source link

How to start with 10 x visium data #4

Closed eric423627 closed 2 years ago

eric423627 commented 2 years ago

Hi,

Very useful software! I just wonder how to use it with the 10 x visium spaceranger output? Do you have a brief intro for that? Thank you.

bmill3r commented 2 years ago

Hello,

Thank you so much for your question!

Currently the easiest way to use with the 10X Visium SpaceRanger output is to load that data into a SpatialExperiment object using the SpatialExperiment package on Bioconductor.

For example:

se <- SpatialExperiment::read10xVisium(samples = "../V1_Adult_Mouse_Brain_Coronal",
     type = "sparse",
     data = "filtered")

From here, the count matrix can be accessed and setup for feature selection in STdeconvolve via:

## this is the genes x spot sparse count matrix
cd <- se@assays@data@listData$counts

## from here, the matrix can be cleaned followed by feature selection
## remove pixels with too few genes
counts <- cleanCounts(cd, min.lib.size = 100)
## feature select for genes
corpus <- restrictCorpus(counts, removeAbove=1.0, removeBelow = 0.05)

x and y coordinates of the spots can be obtained via:

pos <- SpatialExperiment::spatialCoords(se)

## change column names to x and y
## for this dataset, visualized as: pixel column = "y", and pixel row = "x"
colnames(pos) <- c("y", "x")

We are working on a wrapper function to take care of this to make it a little easier to extract the relevant data.

Also note that if you look at the manual for SpatialExperiment::read10Xvisium() it does expect the SpaceRanger output to have a particular directory structure:

The constructor assumes data from each sample are located in a single output directory as returned
by Space Ranger, thus having the following file organization:
sample

|—outs
· · |—raw/filtered_feature_bc_matrix.h5
· · |—raw/filtered_feature_bc_matrix
· · · · |—barcodes.tsv
· · · · |—features.tsv
· · · · |—matrix.mtx
· · |—spatial
· · · · |—scalefactors_json.json
· · · · |—tissue_lowres_image.png
· · · · |—tissue_positions_list.csv

Hope this helps and let me know if you have any other questions, Brendan

NMalhi92 commented 2 years ago

Hi, thank you for this software! I am also trying to deconvolute some Visium data using this tool and just need some advice. I have normalized my data using Seurat SCTransform and would like to deconvolute this data. I am struggling to get the pos and annot in a way that STdeconvolve recognises? I have tried: pos <- Hu1221@images$slice1@coordinates colnames(pos) <- c("y", "x") annot <- Hu1221@meta.data$seurat_clusters and get this error message 'Error in FUN(X[[i]], ...) : object 'x' not found' when I try vizAllTopics. Any advice will be greatly appreciated! Thank you, Naseeb

bmill3r commented 2 years ago

Hi Naseeb,

Thanks for reaching out! Would you mind posting the vizAllTopics() command you are trying to run?

vizAllTopics does requires a theta matrix, which is the predicted pixel x cell-type proportion matrix. Are you including this when running this command? Both theta and pos should have the pixels listed as the rownames.

Let me know if you're still having trouble, Brendan

NMalhi92 commented 2 years ago

Hi Brendan, Thanks for replying so quickly! I've just been following the tutorial on your website, so I've been running the below:

results <- getBetaTheta(optLDA, perc.filt = 0.05, betaScale = 1000) deconProp <- results$theta deconGexp <- results$beta

vizAllTopics(deconProp, pos, groups = 'annot', group_cols = rainbow(length(levels(annot))), r=0.4) Can you see a step I'm missing? Thanks, Naseeb

bmill3r commented 2 years ago

Hi Naseeb,

Thanks so much for sharing! I think the issue might be how you are calling groups = annot. The parameter groups takes as an argument either a vector or factor, that indicates the group or cluster assignment of each of the pixels. Here, it looks like you are calling groups = 'annot' as a character string. See if you can get it to run by removing the single quotes i.e., groups = annot.

In the example tutorial of the mouse olfactory bulb, this is to color the pixels in terms of the previously annotated cell-layers to show how the deconvolved cell-types correspond to, and further partition, the 5 different cell-layers in this tissue. The color of each of the groups is designated by group_cols, and this colors the boundaries of the scatterpies. Conversely, the colors of the deconvolved cell-types is designated by the parameter topicCols. You can see more examples of this in the Additional Features with STdeconvolve tutorial under the "Visualization" section.

The groups parameter is used to label the scatterpies in the output plot the boundaries of the scatterpies (in contrast to the cell-type proportions that fill the scatterpies).

Let me know if this helps, Brendan

NMalhi92 commented 2 years ago

Hi Brendan, I tried doing that but it still didn't work. I realized that my issue was the format of my co-ordinates for 'pos'. I have been performing this analysis from my Seurat object, post-normalization and was using pos <- SeuratObject@images$slice1@coordinates. I was able to get it to work by: pos <- (GetTissueCoordinates(SeuratObject)) m <- results$theta p <- pos plt <- vizAllTopics(theta = m, pos = p, topicOrder=seq(ncol(m)), topicCols=rainbow(ncol(m)), groups = annot, group_cols = rainbow(length(levels(annot))), r = 3, lwd = 0.1, showLegend = TRUE, plotTitle = "K=20") Thank you so much for your help! Nas

bmill3r commented 2 years ago

Hi Nas,

So happy you got it to work! I definitely appreciate you bringing this up because perhaps I should add in some better ways to communicate with Seurat objects. pos needs to be a matrix or data.frame that has the same rownames as theta and the columns need to be labeled as x and y. I probably should add in code to catch this or correct for it if possible.

Thanks again and please reach back out if you have any other issues, Brendan

bmill3r commented 2 years ago

For reference a tutorial demonstrating analysis of 10X Visium data has been added to the documentation on the devel branch.