theMILOlab / SPATA2

A Toolbox for Spatial Transcriptomics Analysis
https://themilolab.github.io/SPATA2/
93 stars 16 forks source link

How to subset cell types within a spatial annotation #177

Open SavKilPat opened 6 days ago

SavKilPat commented 6 days ago

Hi Milo lab,

Thanks very much for providing such a user-friendly tool for annotating spatial transcriptomics.

I've been attempting to isolate/subset specific cell types within a region of interest created with the createImageAnnotations function. The cell types are interest are from my previous Seurat-processed dataset, which I'm able to isolate successfully using the subsetSpataObject function. However, I cannot seem to isolate the regions of interest using a similar approach. I'm met with the following error :

15:57:05 Keeping 28293 observation(s). Error in ma@mtr_proc[[nm]][, bcs_keep] : subscript out of bounds

Which presumably is because the spatial annotation barcodes are pulled from a different part of the metadata. Below is my code to get the error.

# obtain the IDs of all spatial annotations
getSpatAnnIds(FAM3WT_Init)
#"MI-1_MID-L" "MI-2_MID-L" "MI-1_BOT"   "MI-2_BOT"
Migr_1 <- getCoordsDfSA(FAM3WT_Init, ids = "MI-1_MID-L", incl_edge = TRUE) %>%  pull(barcodes)
Migr_2 <- getCoordsDfSA(FAM3WT_Init, ids = "MI-2_MID-L") %>% pull(barcodes)
Migr_3 <- getCoordsDfSA(FAM3WT_Init, ids = "MI-1_BOT") %>% pull(barcodes)
Migr_4 <- getCoordsDfSA(FAM3WT_Init, ids = "MI-2_BOT") %>% pull(barcodes)

Migr_all <- c(Migr_1, Migr_2, Migr_3, Migr_4, Migr_5)

All_Migr <- subsetSpataObject(FAM3WT_Init, barcodes = Migr_1)

So far I have tried the following:

  1. Setting different active matrices in the SPATA2 object
  2. Adding the barcodes into the metadata via addMetaDataObs
  3. Creating a Seurat object from this SPATA2 object <-- Here I also get a weird

to do

error, and the object comes back as

NULL

I'm hopeful I can proceed with some form of subsetting or asSeurat option as I'll likely have to do this in the future with the object for CellChat analysis.

Do you have any suggestions on how to proceed? Thanks as always!!

Sincerely,

Savannah

PS if context helps, I'm trying to isolate cells pre- and post-migration and perform DEG analysis between them and other genotypes.

kueckelj commented 5 days ago

Hi, try getSpatAnnBarcodes(object). This function returns a character vector of barcodes of the specified annotations. If you want to work with getCoordsDfSA(), this works, too. But you have to subset it such that only the spots remain that are located inside the annotations - rel_loc == 'core'. See the example below:

extract barcodes

library(SPATA2) library(tidyverse)

object <- loadExampleObject("UKF313T", process = T, meta = T)

getSpatAnnIds(object)

bcs_necr_area <- getSpatAnnBarcodes(object, ids = "necrotic_area")

ids_all <- c("necrotic_area", "necrotic_edge", "necrotic_edge2") bcs_necr_all <- getSpatAnnBarcodes(object, ids = ids_all)

plot results as proof of principle

coords_df <- getCoordsDfSA(object, ids = ids_all)

coords_df$necr_area <- coords_df$barcodes %in% bcs_necr_area coords_df$necr_all <- coords_df$barcodes %in% bcs_necr_all

plotSurface(coords_df, "necr_area") plotSurface(coords_df, "necr_all")

plotSurface(coords_df, color_by = "id") # closest to which annotation? plotSurface(coords_df, color_by = "rel_loc")

dplyr::filter(coords_df, rel_loc == "core") %>% plotSurface(object = ., color_by = "id")

Does that help?

kueckelj commented 1 day ago

Hey, for some reason I only see your reply by mail not in this issue. But I'll answer here anyway.

First of all: This error here...

coords_df <- getCoordsDfSA(FAM3WT_Init3, ids = ids_all)

Error in !is.character(ids) || !ids %in% getSpatAnnIds(object) : 'length = 2' in coercion to 'logical(1)'

...makes me believe that you are currently working in the master branch and not in the dev-branch. Because what causes this error does not exist any longer in the dev-branch. Can you make sure to have the dev-branch installed and loaded? The function currentSpata2Version() should output a list with $major == 3, $minor == 1, $patch == 0. And then try getCoordsDfSA() again.

If the error "Error in ma@mtr_proc[[nm]][, bcs_keep] : incorrect number of dimensions" remains during subsetting. Can you give me the output of assay <- getAssay(object); purrr::map(assay@mtr_proc, .f = str).