首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >课程复习----多样本进行共定位分析

课程复习----多样本进行共定位分析

原创
作者头像
追风少年i
发布2024-12-25 16:09:54
发布2024-12-25 16:09:54
4200
举报

作者,Evil Genius

过年前我们就先停一停把,多复习复习。

这一篇我们进行多样本的细胞类型共定位分析。

但是要注意,这里的多样本指水平重复的空间样本, 不同组(例如疾病与对照)之间共定位要分开做的。

首先单细胞空间联合,这个cell2location或者RCTD二选一,celltrek、spotlight等也可以考虑,联合的时候最好是配对样本,次选同组样本。

拿到解卷积之后细胞矩阵,如下图。

包含了sample、Disease信息。

矩阵拿到,开始单样本共定位分析,这里就不过多介绍了,无论是课程还是文章都写了很多了,大部分也都拿到盗版的资料了。

参考文章在10X空间转录组数据分析之细胞的空间依赖性

每个空间样本分析完,都会拿到如下的结果,大家每个样本放在一个文件夹下面,每种细胞类型与其他细胞类型的空间共定位分数。

接下来多样本合并,其实就是合并共定位分数矩阵,大家分析因为命名的不同,所以内部脚本的变量和列名有差异,但是抓住一点,每个样本都分析到了一种细胞类型和其他所有细胞类型的共定位分数,我们的目的是将每个样本的共定位分数merge起来,求平均值,即为该组的细胞共定位分析结果,下面是代码示例。

代码语言:javascript
复制
misty_out_folder <- "/mistyr/"
misty_outs <- list.files(misty_out_folder, full.names = F)
misty_outs <- set_names(misty_outs)

misty_res <- collect_results(paste0(misty_out_folder, misty_outs))
####多样本合并矩阵(这个地方大家自己根据项目自己调整一下)
sample_importances <- misty_res$importances

多样本分数平均值并且绘图

代码语言:javascript
复制
#Mean importance for each sample of the intra View
summarized_interactions_group <- sample_importances %>%
  group_by(view, Predictor, Target, sample) %>%
  summarize(median_importance = mean(Importance)) %>%
  ungroup() %>%
  group_by(view)

summarized_interactions_sample <- summarized_interactions_group %>%
  filter(view =="intra") %>%
  ggplot(aes(x = Target, y = Predictor, fill = median_importance)) +
  geom_tile() + theme(axis.text = element_text(size = 5))+
  scale_fill_gradient2(high = "blue", 
                       midpoint = 0,
                       low = "white",
                       na.value = "grey") +
  theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5)) +
  facet_wrap(view ~ sample, scales = "free")

#Mean importance for Disease_State and View
summarized_interactions_group <- sample_importances %>%
  group_by(view, Predictor, Target, Disease) %>%
  #group_by(Predictor, Target, Disease) %>%
  summarize(mean_importance = mean(Importance)) %>%
  ungroup() 

summarized_interactions_sample <- summarized_interactions_group %>%
  filter(view =="intra") %>%
  ggplot(aes(x = Target, y = Predictor, fill = median_importance)) +
  geom_tile() + theme(axis.text = element_text(size = 5))+
  scale_fill_gradient2(high = "blue", 
                       midpoint = 0,
                       low = "white",
                       na.value = "grey") +
  theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5)) +
  facet_wrap(view ~ sample, scales = "free")

#Mean importance for Disease_State and View
summarized_interactions_group <- sample_importances %>%
  group_by(view, Predictor, Target, Disease) %>%
  #group_by(Predictor, Target, Disease) %>%
  summarize(mean_importance = mean(Importance)) %>%
  ungroup() 

summarized_interactions_group$Disease <- factor(summarized_interactions_group$Disease, levels = c("Control", "Intermediate\nlesion", "Atheroma"))
summarized_interactions <- summarized_interactions_group %>% filter(Disease != "Intermediate\nlesion")  %>%
  ggplot(aes(x = Target, y = Predictor, fill = mean_importance)) +
  geom_tile() +
  scale_fill_gradient2(high = "#0080bf", 
                       midpoint = 0,
                       low = "white",
                       na.value = "gray") +
  theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5), axis.title = element_text(size=12)) +
  facet_grid(Disease ~ view)+coord_equal()+labs(fill="mean\nimportance")


# Comparative mean importance
Comparative <- summarized_interactions_group %>%
  filter(Disease!="Intermediate\nlesion") %>%
  group_by(Predictor, Target) %>%
  mutate(ratio = mean_importance - lead(mean_importance)) %>%
  filter(!is.na(ratio))

Comparative_Importance_plot <- ggplot(Comparative, aes(x = Target, y = Predictor, size=mean_importance, color = ratio)) +
  geom_point() + theme(panel.background = element_blank())+
  theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5), axis.title = element_text(size=12)) +
  scale_color_distiller(palette = "RdBu")+
  coord_equal()+labs(color="Importance\nAtheroma\nvs.\nControl", size="Mean\nImportance\nAtheroma")

# R2 distributions
R2_data <- misty_res$improvements

cell_order <- R2_data %>% 
  group_by(target) %>%
  summarize(med_value = median(value)) %>%
  arrange(-med_value) %>%
  pull(target)

cells_R2_tile <- ggplot(R2_data, aes(x = factor(target,
                                                levels = cell_order), 
                                     y = sample, fill = value)) +
  geom_tile() +
  coord_equal() +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5),
        panel.border = element_rect(colour = "black", fill=NA, size=1)) +
  scale_fill_gradient(low = "black", high = "yellow")

cells_R2_box <- ggplot(R2_data, aes(x = factor(target,
                                               levels = cell_order), y = value)) +
  geom_boxplot() +
  geom_point(aes(color = sample)) +
  theme_minimal() + 
  theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5),
        axis.text = element_text(size = 12),
        axis.title = element_text(size =12),
        panel.border = element_rect(colour = "black", fill=NA, size=1)) +
  ylab("Explained variance") +
  xlab("")



#Summary plots
pdf("Misty_importance_Cell_Types.pdf", width = 6, height = 6)

plot(summarized_interactions_sample)
plot(summarized_interactions)
plot(Comparative_Importance_plot)
mistyR::plot_interaction_communities(misty_res, "intra", cutoff = 0.5)
mistyR::plot_interaction_communities(misty_res, "juxta_6", cutoff = 0.5)
plot(cells_R2_box)
plot(cells_R2_tile)

dev.off()

生活很好,有你更好

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 作者,Evil Genius
  • 过年前我们就先停一停把,多复习复习。
  • 这一篇我们进行多样本的细胞类型共定位分析。
  • 但是要注意,这里的多样本指水平重复的空间样本, 不同组(例如疾病与对照)之间共定位要分开做的。
  • 首先单细胞空间联合,这个cell2location或者RCTD二选一,celltrek、spotlight等也可以考虑,联合的时候最好是配对样本,次选同组样本。
  • 拿到解卷积之后细胞矩阵,如下图。
  • 包含了sample、Disease信息。
  • 矩阵拿到,开始单样本共定位分析,这里就不过多介绍了,无论是课程还是文章都写了很多了,大部分也都拿到盗版的资料了。
  • 参考文章在10X空间转录组数据分析之细胞的空间依赖性
  • 每个空间样本分析完,都会拿到如下的结果,大家每个样本放在一个文件夹下面,每种细胞类型与其他细胞类型的空间共定位分数。
  • 接下来多样本合并,其实就是合并共定位分数矩阵,大家分析因为命名的不同,所以内部脚本的变量和列名有差异,但是抓住一点,每个样本都分析到了一种细胞类型和其他所有细胞类型的共定位分数,我们的目的是将每个样本的共定位分数merge起来,求平均值,即为该组的细胞共定位分析结果,下面是代码示例。
  • 多样本分数平均值并且绘图
  • 生活很好,有你更好
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档