查看原文
其他

跟着 Nature medicine 学画图-tSNE

JunJunLab 老俊俊的生信笔记 2022-08-27


挂在天上放光明

1引言

话不多说,直接上图:

查看数据:

作者直接上传了 Rds 文件,我下载下来后发现有很多信息都不全,于是直接去下载作者的 loom 文件来分析,下载 allcells, 地址是:

https://gbiomed.kuleuven.be/english/research/50000622/laboratories/54213024/scRNAseq-NSCLC

2读取 loom 文件

使用 Connect 函数连接 loom 文件:

library(hdf5r)
library(SeuratDisk)
library(scater)
library(tidyverse)
library(Seurat)

library(ggplot2)
library(cowplot)

# load loom
allcell <- Connect(filename = "./Thienpont_Tumors_52k_v4_R_fixed.loom", mode = "r")
allcell

# Class: loom
# Filename: F:\文章复现数据\11.LungTumor-scRNASEQ\all_Rds_data\Thienpont_Tumors_52k_v4_R_fixed.loom
# Access type: H5F_ACC_RDONLY
# Attributes: title, MetaData, version
# Listing:
#       name    obj_type  dataset.dims dataset.type_class
#  col_attrs   H5I_GROUP          <NA>               <NA>
# col_graphs   H5I_GROUP          <NA>               <NA>
#     layers   H5I_GROUP          <NA>               <NA>
#     matrix H5I_DATASET 52698 x 33694          H5T_FLOAT
#  row_attrs   H5I_GROUP          <NA>               <NA>
# row_graphs   H5I_GROUP          <NA>               <NA>

查看细胞属性

可以看到有 病人信息, 肿瘤样本信息, 亚群名称, 细胞名称 等等:

# check cell metadata
allcell[["col_attrs/"]]

# Listing:
#   name    obj_type dataset.dims dataset.type_class
# CellFromTumor H5I_DATASET        52698         H5T_STRING
#        CellID H5I_DATASET        52698         H5T_STRING
#     ClusterID H5I_DATASET        52698        H5T_INTEGER
#   ClusterName H5I_DATASET        52698         H5T_STRING
#   Clusterings H5I_DATASET        52698       H5T_COMPOUND
#     Embedding H5I_DATASET        52698       H5T_COMPOUND
#  Embeddings_X H5I_DATASET        52698       H5T_COMPOUND
#  Embeddings_Y H5I_DATASET        52698       H5T_COMPOUND
# PatientNumber H5I_DATASET        52698         H5T_STRING
#   RegulonsAUC H5I_DATASET        52698       H5T_COMPOUND
# < Printed 10, out of 13>

查看基因属性

# check gene metadata
allcell[["row_attrs/"]]

# Listing:
#     name    obj_type dataset.dims dataset.type_class
#     Gene H5I_DATASET        33694         H5T_STRING
# Regulons H5I_DATASET        33694       H5T_COMPOUND

3制作 metadata 信息

根据上面信息制作绘图需要的数据:

metadata <- data.frame(
  CellID = allcell[["col_attrs/CellID"]][],
  ClusterName = allcell[["col_attrs/ClusterName"]][],
  CellFromTumor = allcell[["col_attrs/CellFromTumor"]][],
  PatientNumber = allcell[["col_attrs/PatientNumber"]][],
  ClusterID = allcell[["col_attrs/ClusterID"]][],
  nUMI = allcell[["col_attrs/nUMI"]][],
  Embedding = allcell[["col_attrs/Embedding"]][]
)


colnames(metadata)
# [1] "CellID"        "ClusterName"   "CellFromTumor" "PatientNumber" "ClusterID"
# [6] "nUMI"          "Embedding._X"  "Embedding._Y"

4绘图

# plot
# CellFromTumor
p1 <- ggplot(metadata,aes(x = Embedding._X,y = Embedding._Y,
                    color = CellFromTumor)) +
  geom_point(size = 0.2) +
  theme_bw(base_size = 16) +
  theme(panel.grid = element_blank(),
        legend.position = c(0.2,0.1),
        legend.background = element_blank(),
        aspect.ratio = 1) +
  xlab('tSNE 1') + ylab('tSNE 2') +
  scale_color_manual(values = c('0' = '#66CC99','1' = '#336699'),
                    name = '',
                    label = c('0' = 'Non-malignant','1' = 'Tumour')) +
  guides(color = guide_legend(override.aes = list(size = 3)))

# PatientNumber
p2 <- ggplot(metadata,aes(x = Embedding._X,y = Embedding._Y,
                  color = PatientNumber)) +
  geom_point(size = 0.2) +
  theme_bw(base_size = 16) +
  theme(panel.grid = element_blank(),
        legend.position = c(0.1,0.2),
        legend.background = element_blank(),
        aspect.ratio = 1) +
  xlab('tSNE 1') + ylab('tSNE 2') +
  scale_color_manual(values = c('1' = '#339933','2' = '#FF6600','3' = '#CCFF99',
                                '4' = '#FFCC33','5' = '#336699'),
                     name = '') +
  guides(color = guide_legend(override.aes = list(size = 3)))

# ClusterName
p3 <- ggplot(metadata,aes(x = Embedding._X,y = Embedding._Y,
                    color = ClusterName)) +
  geom_point(size = 0.2,show.legend = F) +
  theme_bw(base_size = 16) +
  theme(panel.grid = element_blank(),
        legend.position = c(0.1,0.1),
        legend.background = element_blank(),
        aspect.ratio = 1) +
  xlab('tSNE 1') + ylab('tSNE 2')

# nUMI
p4 <- ggplot(metadata,aes(x = Embedding._X,y = Embedding._Y,
                    color = log10(nUMI))) +
  geom_point(size = 0.2) +
  theme_bw(base_size = 16) +
  theme(panel.grid = element_blank(),
        legend.position = c(0.1,0.1),
        legend.background = element_blank(),
        aspect.ratio = 1) +
  xlab('tSNE 1') + ylab('tSNE 2') +
  scale_color_gradient2(low = 'white',mid = 'grey80',high = 'blue',midpoint = 2.5,
                      name = '',
                      limits = c(2,5),
                      guide = guide_colorbar(direction = 'horizontal'))

# combine
plot_grid(plotlist = list(p1,p2,p3,p4),ncol = 2,align = 'hv')

5结尾

有必要去了解一些 loom 文件的操作哦



  老俊俊生信交流群 (微信交流群需收取20元入群费用(防止骗子和便于管理))


老俊俊微信:


知识星球:



今天的分享就到这里了,敬请期待下一篇!

最后欢迎大家分享转发,您的点赞是对我的鼓励肯定

如果觉得对您帮助很大,赏杯快乐水喝喝吧!



  





单细胞亚群 Marker 基因热图重绘及均值展示

Seurat 官网单细胞教程四 (SCTransform 使用及 SCTransform V2 版本)

Seurat 官网单细胞教程三 (RPCA 快速整合数据)

ggplot 随心所欲的添加注释

Seurat 官网单细胞教程一 (数据整合)

NC 文章单细胞部分图复现

Seurat 官网单细胞教程一 (基础教程)

左下角自定义箭头坐标轴 (批量添加和美化)

单细胞绘图数据提取个性化绘图

UMAP/t-SNE 左下角自定义箭头坐标轴

◀...

您可能也对以下帖子感兴趣

文章有问题?点此查看未经处理的缓存