ludvigla / semla

Other
47 stars 6 forks source link

Overlay categorical features #27

Closed katiestoker closed 2 months ago

katiestoker commented 2 months ago

Hello,

I am looking to map two different clone IDs which I have merged into the object metadata by rownames. Is there a way to map these two clones onto one spatial sample but indicate a third colour when two clones are found in the same spot?

Apologies if I missed this in the documentation!

All the best, Katie.

jemorlanes commented 2 months ago

Hello Katie, thanks for using the package!

We do not have a specific function to do that specific overlay, but you could fix it with a little bit of dataframe wrangling :). Here is an example code to achieve what you want:

library(tidyverse)
library(semla)

# load example dataset
se <- readRDS(file = system.file("extdata", 
                                 "mousebrain/se_mbrain", 
                                 package = "semla"))
# assemble mock clone annotation.
## i am assuming that the dataframe you have to indicate the clones in the ST spots looks like this: a column with the spatial ID,
## another column with clone annotation 1, and another column with the other clone annotation
meta.clone <- data.frame(barcode = colnames(se),
                         annot_1 = c(rep("clone_A", 560), rep("clone_B", 2000)),
                         annot_2 = c(rep("clone_A", 1560), rep("clone_B", 1000)))

# create extra label for overlap
## we have 2 clones in our dataframe, and now we want a specific label for when a spot has 2 different annotations
meta.clone <- meta.clone %>%
  mutate(annot = ifelse(annot_1 == annot_2, annot_1, "multiple_clone_annotations")) # if a spot has the same clone annotation in both columns, keep one annotation. If it has different annotations, annotate the spot as "multiple_annotations"

# we can check that everything is working as intended
## so from the example dataframe I assembled, I expect 560 spots with annotation "clone_A", 1000 spots with annotation "clone_B",
## and 1000 spots with annotation "multiple_clone_annotations"
table(meta.clone$annot)

# awesome! now we can do some cleanup, adding the dataframe as metadata of our seurat object, and plot the labels to make sure it works
meta.clone <- select(meta.clone, all_of(c("barcode", "annot"))) %>%
  column_to_rownames(var = "barcode")

se <- AddMetaData(se, meta.clone)

MapLabels(se, column_name = "annot")

Hope it helps!

katiestoker commented 2 months ago

Very helpful! Thank you very much.

All the best, Katie.