Replies: 1 comment 1 reply
-
Hello @grindhej , This code seems really useful as far as I understand! I was wondering if it could be adapted to a specific situation I’m working on. I have a merged Xenium object containing 20 samples, with the images stored in the @images slot, and the name of each sample. Would it be possible for this function to process the single Seurat object and calculate niches for the different samples? If so, could you please guide me on how to adapt it for this scenario? Thank you so much for your help! |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Goal
a. Get niches defined across multiple patient samples
b. Figure out how many k-means, aka niches, to use
c. Quantify niche content and and around structures of interest
Background
I have single cell multiplex IF data that I pushed into a Seurat Object.
Specifically, I have per cell mean fluorescence intensity, xy locations, and phenotype. I figure that I don't need to process the MFI data to get clusters or anything, I already have the phenotype from a different and can add those into the Seurat Object metadata.
Here's some non-reproducible code, to get the idea.
init Seurat Object
create FOV
add FOV to SeuratObject
add cell metadata, which includes a phenotype column
change cell Identities to phenotype
Then I can use BuildNicheAssay on 1 sample to get niches.
questions
How to choose how many niches? The classic "how many k to choose" question, but maybe somebody already has an elegant feature built into Seurat for this.
I'm interested in niches specifically around rare structures in the images. Before I figured out how to get the data into Seurat and build niches here, I did a very similar analysis with kmeans clustering by hand on the whole slides--- I think that the biology from the whole slide is swamping out any rare niches that may occur near my rare structures. So should I filter to cells in and around those structures and only build niches from those cells?
I have 20 samples from different patients. How do I build niches with the same niches assigned across all the samples?
I could only find vignette that mentions BuildNicheAssay:
https://satijalab.org/seurat/articles/seurat5_spatial_vignette_2#mouse-brain-vizgen-merscope
Seurat V5 is wonderful! Thanks so much!
Edit: add a hack to perform kmeans clustering on multiple samples
Description: Use the BuildNicheAssay function as a backbone. Hack it to take a list of SeuratObjects and perform ClusterR::MiniBatchKmeans on select fov. Returns the list of SeuratObjects, but now the metadata contains cluster IDs for a the provided range of K. ClusterR::MiniBatchKmeans is good when you have giant datasets to do kmeans on. This function may poop out on you if there are too many input cells.
`#' Construct an assay for spatial niche analysis
#'
#' This function will construct a new assay where each feature is a
#' cell label The values represents the sum of a particular cell label
#' neighboring a given cell.
#'
#' @param list.object list of Seurat objects to do clustering on
#' @param list.fov list of fov names to use for grabbing cells to cluster from list.object. Should be the same length as list.object
#' @param group.by Cell classifications to count in spatial neighborhood
#' @param assay Name for spatial neighborhoods assay
#' @param cluster.name Name of output clusters
#' @param neighbors.k Number of neighbors to consider for each cell
#' @param niches.k.range Number of clusters to return based on the niche assay. provide a range
#' @param batch_size Number of mini-batches for ClusterR::MiniBatchKmeans
#' @param num_init # number of times the algorithm will be run with different centroid seeds for ClusterR::MiniBatchKmeans
#'
#' @importFrom stats kmeans
#' @return Seurat object containing a new assay
#' @concept clustering
#' @export
#'
BuildNicheAssay.multiple_FOVs.MiniBatchKmeans <- function(
list.object,
list.fov,
group.by,
assay = "niche",
cluster.name = "niches",
neighbors.k = 20,
niches.k.range = 2:30 ,
batch_size = 20,
num_init = 20
) {
# check for fov in sample set
# remove if not found in object
remove = NULL # init list of indices to remove
for ( i in seq_along(list.object) ){ # message(i)
# get object and fov for each object
object = list.object[[i]]
fov = list.fov[[i]]
}
`
Beta Was this translation helpful? Give feedback.
All reactions