前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >单细胞韧皮部研究代码解析3-comparison_brady.R

单细胞韧皮部研究代码解析3-comparison_brady.R

原创
作者头像
小胡子刺猬的生信学习123
修改2023-05-05 20:33:24
1800
修改2023-05-05 20:33:24
举报

单细胞韧皮部研究代码解析1-QC_filtering.R:https://cloud.tencent.com/developer/article/2256814?areaSource=&traceId=

单细胞韧皮部研究代码解析2--comparison_denyer2019.R:https://cloud.tencent.com/developer/beta/article/2260381?areaSource=&traceId=

代码链接:https://github.com/tavareshugo/publication_Otero2022_PhloemPoleAtlas/blob/master/analysis/05-comparison_brady.R

在做单细胞分析的时候,很多研究学者会将以前做的显微切割的RNA-seq的数据与自己的研究内容进行比对,则可以得到在单细胞和亚细胞水平上的数据结果。一般是通过将已经人工注释好的亚群与显微切割数据进行相关性分析,去判断自己的分群是否准确,但是这位作者的代码是可以在进行降维获得亚群后,直接可以与普通的RNA-SEQ数据进行整合分析,为后面的人工亚群注释进行相关的参考。

这篇文章的作者也是通过相关得内容进行了代码演示,通过改写作者的部分内容,是可以做到自己研究的内容的图片可视化的。

今天主要是对上面的图片的代码进行解析。

代码解析

代码语言:javascript
复制
# Setup -------------------------------------------------------------------
# R包的加载
library(scater)
library(tidyverse)
#在R中,经常会需要读入excel文件,这个包也是经常用到的
library(readxl)
library(dplyr)

# set ggplot2 theme
theme_set(theme_bw() + theme(text = element_text(size = 14)))
# 这是作者提的R的函数,在前面的教程里面也提到了这个脚本的下载地址
source("analysis/functions/utils.R")


# Tidy Brady data ------------------------------------------------------
# 这一段是作者要进行比较的数据集,主要是根据自己相关的内容进行更改
# read both sheets
# Brady提供的是probe和gene对应的数据集,是需要将不同的表达组织进行对应的
# 文章中作者选用的数据链接来源:https://science.sciencemag.org/highwire/filestream/588629/field_highwire_adjunct_files/1/1146265BradySTables.zip,可以先自己下载作者的数据进行尝试后,对自己的相关的内容进行改写
brady <- list(
  longitudinal = read_xls("data/external/brady_et_al/ST12.xls",
                          sheet = "LONGITUDINAL", skip = 1) %>%
    pivot_longer(c(-Probe, -Gene),
                 names_to = "slice", values_to = "expr"),
  radial = read_xls("data/external/brady_et_al/ST12.xls",
                    sheet = "RADIAL", skip = 1) %>%
    pivot_longer(c(-Probe, -Gene),
                 names_to = "slice", values_to = "expr")
) %>%
  bind_rows(.id = "set") %>%
  rename_with(tolower) %>%
  # some probes don't have gene
  drop_na(gene)

# annotations for radial sections
radial_annot <- tribble(
  ~slice, ~tissue,
  "AGL42_MEAN", "QC",
  "APL_MEAN", "Phloem + CC",
  "COBL9_MEAN", "Hair cell",
  "CORTEX_MEAN", "Cortex",
  "gl2_MEAN", "Non-hair cell",
  "JO121_MEAN", "Xylem pole pericyle",
  "J0571_MEAN", "Ground tissue",
  "J2661_MEAN", "Mature pericycle",
  "LRC_MEAN", "LRC",
  "pet111_MEAN", "Columella",
  "rm1000_MEAN", "Lateral root",
  "S17_MEAN", "Phloem Pole Pericycle",
  "S18_MEAN", "Maturing xylem",
  "S4_MEAN", "Developing xylem",
  "S32_MEAN", "All protophloem",
  "scr5_MEAN", "Endodermis",
  "SUC2_MEAN", "Phloem CC",
  "wol_MEAN", "Stele",
  "xylem_2501_MEAN", "Stele"
)
## 以上的相关的内容主要是对每一列的值进行宽表改成长表,如何进行表格长宽的改变,也在以前的R语言教程里面有,有不会的友友可以取翻一下以前的教程
# Read SCE data -----------------------------------------------------------

# sce object
# 对降维分析的数据进行加载
ring <- readRDS("data/processed/SingleCellExperiment/ring_batches_strictfilt.rds")


# Longitudinal sections ----------------------------------------------------
# assign each cell to a section in Brady using correlation

## 根据gene内容对两个数据集进行合并
# matrix of expression
lon_matrix <- brady %>%
  filter(set == "longitudinal") %>%
  pivot_wider(names_from = slice, values_from = expr) %>%
  # retain only genes with a single probe
  group_by(gene) %>%
  filter(n() == 1) %>%
  ungroup() %>%
  # retain genes that also occur in our dataset
  filter(gene %in% rownames(ring)) %>%
  # convert to matrix
  select(-set, -probe) %>%
  column_to_rownames("gene") %>%
  as.matrix()

# 选择spearman进行相关系数计算
# correlation matrix
lon_cor <- cor(as.matrix(assay(ring, "logvst")[rownames(lon_matrix), ]),
               lon_matrix,
               method = "spearman")

## 得到每个细胞和brady数据的相关性数据
# get the Brady section with maximum correlation for each cell
lon_classes <- tibble(cell = rownames(lon_cor),
                      section = colnames(lon_cor)[max.col(lon_cor)],
                      cor = rowMax(lon_cor))

## 随后是选用作者写的函数getReducedDim进行绘图
# Plot
ring %>%
  getReducedDim("UMAP30_MNN_logvst") %>%
  mutate(cell = paste(Sample, Barcode, sep = "_")) %>%
  full_join(lon_classes, by = "cell") %>%
  mutate(section = str_remove(section, "SB_MEAN")) %>%
  mutate(section = as.numeric(str_remove(section, "L"))) %>%
  ggplot(aes(V1, V2)) +
  geom_point(data = getReducedDim(ring, "UMAP30_MNN_logvst"), colour = "lightgrey") +
  geom_point(aes(colour = factor(section))) +
  scale_colour_viridis_d() +
  coord_equal() + theme_void() +
  labs(colour = "Brady et. al\nSection")

ring %>%
  getReducedDim("UMAP30_MNN_logvst") %>%
  mutate(cell = paste(Sample, Barcode, sep = "_")) %>%
  full_join(lon_classes, by = "cell") %>%
  mutate(section = str_remove(section, "SB_MEAN")) %>%
  mutate(section = as.numeric(str_remove(section, "L"))) %>%
  ggplot(aes(V1, V2)) +
  geom_point(data = getReducedDim(ring, "UMAP30_MNN_logvst"), colour = "lightgrey") +
  ggpointdensity::geom_pointdensity() +
  facet_grid(~ section) +
  scale_colour_viridis_c(option = "B") +
  coord_fixed() +
  theme_void() +
  theme(legend.position = "none")

总结

作者通过将单细胞数据集与显微切割的RNA的数据集进行整合,计算了细胞与组织之间的相关性系数,为鉴定细胞亚群也做了相关的参考,在细胞层面和亚细胞层面上都做了相关的分析,也是在以前的文章中没有看到的内容,同时我自己对自己的数据也进行了测试,是可以进行相关的分析的,同时得到的结果也是十分可靠的。

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

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 代码解析
  • 总结
相关产品与服务
命令行工具
腾讯云命令行工具 TCCLI 是管理腾讯云资源的统一工具。使用腾讯云命令行工具,您可以快速调用腾讯云 API 来管理您的腾讯云资源。此外,您还可以基于腾讯云的命令行工具来做自动化和脚本处理,以更多样的方式进行组合和重用。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档