edward130603 / BayesSpace

Bayesian model for clustering and enhancing the resolution of spatial gene expression experiments.
http://edward130603.github.io/BayesSpace
Other
96 stars 20 forks source link

plot_expression per sample #95

Closed parkjooyoung99 closed 1 year ago

parkjooyoung99 commented 1 year ago

Hello, I am using multiple visium data set and using 'plot_expression' following tutorial https://edward130603.github.io/BayesSpace/articles/ji_SCC.html .

Modifying the code, I could plot expression of each clusters in all samples like below image.

image

But I wonder if there is a way to plot expression of all clusters per sample like this

image

where cell types will be cluster name.

Under is the code that i used. If there is any information you need please let me know. Thank you!

markers <- list()
for (i in top20$cluster %>% unique() %>% as.character() %>% as.numeric() %>% sort()){
  print(i)
  markers[[paste0('cluster',i)]] =  top20$gene[top20$cluster == i] %>% as.character()
}
idx = markers[[10]] %>% str_detect('-EN') %>% which()
markers[[10]] = markers[[10]][-11]

sum_counts <- function(sce, features) {
  if (length(features) > 1) {
    colSums(logcounts(sce)[features, ])
  } else {
    logcounts(sce)[features, ]
  }
}

spot_expr <- purrr::map(markers, function(xs) sum_counts(sce.combined , xs))

plot_expression <- function(sce, expr, name) {
  featurePlot(sce, expr, color=NA) +
    scale_fill_gradient(low = 'lightgray',high = 'red3') +
    labs(title=name, fill="Log-normalized\nexpression") + theme(text = element_text(family = 'Arial'), title = element_text(family = 'Arial_bold'), legend.title = element_text(family = 'Arial'))
}
spot_plots <- purrr::imap(spot_expr, function(x, y) plot_expression(sce.combined , x, y))

for (i in 1:length(spot_plots)){
  print(i)
  p = patchwork::wrap_plots(spot_plots[[i]])
  pdf(paste0('20221108_cluster',i,'_expression.pdf'), width = 6.5,height = 6)
  print(p)
  dev.off()
}
parkjooyoung99 commented 1 year ago

solved! thanks