nateosher / DIMPLE

MIT License
2 stars 1 forks source link

cell marks matrix #104

Closed umasaxena closed 1 year ago

umasaxena commented 1 year ago

Is there an issue in the cell marks matrix generation, I only get the "NA" as list. Could you please elabroate how the matrix should look like.

nateosher commented 1 year ago

Hi Uma,

Would you mind posting the code you're using to generate the distance matrix using some fake data? We'd be happy to take a look and see what might be going wrong.

nateosher commented 1 year ago

I think there are a couple things going on here. One problem is that it looks like the cell data you're using is for just one slide (with one exception, from slide LT051_P3_HN). So instead of storing it as a MltplxExperiment, it probably makes the most sense to store it as a MltplxObject.

I think the problem is that the original code only works when each cell is only positive for one phenotype. In your data set it looks like cells can be positive for many phenotypes. Here's some code that does what I think you want it to:

library(tidyverse)
library(DIMPLE)

rawdata = read_tsv("test.txt") %>% 
  filter(`Slide ID` != "LT051_P3_HN")

phenotype_matrix = tibble(
  cd_112 = rawdata$`Phenotype CD112` == "CD112+",
  cd_226 = rawdata$`Phenotype CD226` == "CD226+",
  cd_56 = rawdata$`Phenotype CD56` == "CD56+",
  cd_96 = rawdata$`Phenotype CD96` == "CD96+",
  ck = rawdata$`Phenotype CK` == "CK+",
  dapi = rawdata$`Phenotype DAPI` == "DAPI+",
  grzb = rawdata$`Phenotype GrzB` == "GrzB+",
  pvr = rawdata$`Phenotype PVR` == "PVR+",
  pvrig = rawdata$`Phenotype PVRIG` == "PVRIG+"
) %>%
  as.matrix()

cell_marks = map(1:nrow(phenotype_matrix), \(row){
  c("CD112+", "CD226+", "CD56+", "CD96+", "CK+", "DAPI+", 
    "GrzB+", "PVR+", "PVRIG+")[phenotype_matrix[row,]]
}) %>% 
  map_chr(\(phenotype_vec){
    paste0(phenotype_vec, collapse = '')
  })

mltplx_obj = new_MltplxObject(
  x = raw_data$`Cell X Position`,
  y = raw_data$`Cell Y Position`,
  marks = cell_marks,
  slide_id = rawdata$`Slide ID`[1]
)

plot(mltplx_obj)
Screenshot 2023-10-13 at 3 46 23 PM

If you do want to make a MltplxExperiment object with multiple slides, the code actually doesn't change much. Here's what it would look like with the test data- however, note that this should theoretically work if you were to concatenate your data from all of your slides together into a single rawdata data frame:

library(tidyverse)
library(DIMPLE)

rawdata = read_tsv("test.txt") %>% 
  arrange(`Slide ID`)

phenotype_matrix = tibble(
  cd_112 = rawdata$`Phenotype CD112` == "CD112+",
  cd_226 = rawdata$`Phenotype CD226` == "CD226+",
  cd_56 = rawdata$`Phenotype CD56` == "CD56+",
  cd_96 = rawdata$`Phenotype CD96` == "CD96+",
  ck = rawdata$`Phenotype CK` == "CK+",
  dapi = rawdata$`Phenotype DAPI` == "DAPI+",
  grzb = rawdata$`Phenotype GrzB` == "GrzB+",
  pvr = rawdata$`Phenotype PVR` == "PVR+",
  pvrig = rawdata$`Phenotype PVRIG` == "PVRIG+"
) %>%
  as.matrix()

cell_marks = map(1:nrow(phenotype_matrix), \(row){
  c("CD112+", "CD226+", "CD56+", "CD96+", "CK+", "DAPI+", 
    "GrzB+", "PVR+", "PVRIG+")[phenotype_matrix[row,]]
}) %>% 
  map_chr(\(phenotype_vec){
    paste0(phenotype_vec, collapse = '')
  })

mltplx_experiment = new_MltplxExperiment(
  x = raw_data$`Cell X Position`,
  y = raw_data$`Cell Y Position`,
  marks = cell_marks,
  slide_id = rawdata$`Slide ID`
)

mltplx_experiment

# MltplxExperiment with 2 slides
# No intensities generated
# No distance matrices generated
# No attached metadata

mltplx_experiment[[1]]

# MltplxObject 
# Slide id: LT051_P3_HN 
# Image with 1 cells across 1 cell types
# Cell types: DAPI+ 
# No intensity generated (yet)
# No distance matrix generated (yet)

mltplx_experiment[[2]]

# MltplxObject 
# Slide id: MG299_P3_HN 
# Image with 99 cells across 5 cell types
# Cell types: CD112+CD96+CK+DAPI+PVR+, CD112+CD96+DAPI+, CD96+DAPI+, CD96+DAPI+PVRIG+, DAPI+ 
# No intensity generated (yet)
# No distance matrix generated (yet)

Hope that helps- let us know if you have any other questions!

umasaxena commented 1 year ago

Thanks, I will try this out. I also see that its possible to add metadata to the data frame. Is there some additional documentation that I can look into.

nateosher commented 1 year ago

The documentation of new_MltplxExperiment gives some examples, which you can view by typing ?new_MltplxExperiment. I think the vignettes are also probably a good place to start. Specifically vignette("mltplx-experiment") for a brief overview. The example analysis vignette is unfortunately not fully incorporated into the master branch yet, but you can still download the .Rmd file from the provided link and build it.

If you you still have questions after that, let us know!

umasaxena commented 1 year ago

Many thanks

nateosher commented 1 year ago

Of course! Happy to help