单细胞三大R包之scater

1.准备R包和数据

library(BiocStyle)
set.seed(10918)
library(scRNAseq)
example_sce <- ZeiselBrainData()
example_sce

## class: SingleCellExperiment 
## dim: 20006 3005 
## metadata(0):
## assays(1): counts
## rownames(20006): Tspan12 Tshz1 ... mt-Rnr1 mt-Nd4l
## rowData names(1): featureType
## colnames(3005): 1772071015_C02 1772071017_G12 ... 1772066098_A12
##   1772058148_F03
## colData names(10): tissue group # ... level1class level2class
## reducedDimNames(0):
## altExpNames(2): ERCC repeat

# 可以看到维度20006 3005 

2.质控

质控,去掉线粒体基因(以mt开头的)

library(scater)
example_sce <- addPerCellQC(example_sce, 
                            subsets=list(Mito=grep("mt-", rownames(example_sce))))

3.了解细胞现有的注释信息

coldata里面有记录tissue和class

tmp = as.data.frame(example_sce@colData)
table(tmp$tissue)

## 
## ca1hippocampus       sscortex 
##           1314           1691

table(tmp$level1class)

## 
## astrocytes_ependymal    endothelial-mural         interneurons 
##                  224                  235                  290 
##            microglia     oligodendrocytes        pyramidal CA1 
##                   98                  820                  939 
##         pyramidal SS 
##                  399

table(tmp$sex)

## 
##    1    2    3 
## 1524   46 1435

fivenum(tmp$total.mRNA.mol)

## [1]    1  703 1429 2144 2831

#横坐标是总count数,纵坐标是检测到的基因数量。
plotColData(example_sce, x = "sum", y="detected", colour_by="tissue") 

image.png

#线粒体基因的比例
plotColData(example_sce, 
            x = "sum", 
            y="subsets_Mito_percent",
            other_fields="tissue") + 
  facet_wrap(~tissue)

image.png

按照tissue分了两张子图。

看每个变量对解释变异的贡献

example_sce <- logNormCounts(example_sce) 

vars <- getVarianceExplained(example_sce, 
                             variables=c("tissue", "total mRNA mol", "sex", "age"))
head(vars)

##              tissue total mRNA mol         sex        age
## Tspan12  0.02207262    0.074086504 0.146344996 0.09472155
## Tshz1    3.36083014    0.003846487 0.001079356 0.31262288
## Fnbp1l   0.43597185    0.421086301 0.003071630 0.64964174
## Adamts15 0.54233888    0.005348505 0.030821621 0.01393787
## Cldn12   0.03506751    0.309128294 0.008341408 0.02363737
## Rxfp1    0.18559637    0.016290703 0.055646799 0.02128006

plotExplanatoryVariables(vars)

image.png

画基因表达量与注释信息关系

x是协变量,如果是个连续型变量,就画出点图,是离散型变量,则画小提琴图。如果不指定x,就每个基因成为一个小提琴图。

plotExpression(example_sce, 
               rownames(example_sce)[1:6],
               x = rownames(example_sce)[10])

image.png

plotExpression(example_sce, 
               rownames(example_sce)[1:6], 
               x = "level1class")

image.png

plotExpression(example_sce, rownames(example_sce)[1:6],
               x = "level1class", 
               colour_by="tissue")

image.png

plotExpression(example_sce, 
               rownames(example_sce)[1:6])

image.png

4.降维

默认情况下,runPCA()使用方差最高的前500个基因来计算PC1。默认计算50个主成分。可以用subset_row指定自选基因,用ncomponents指定要计算的主成分数。

PCA、t-SNE、UMAP三个方法

example_sce <- runPCA(example_sce)
str(reducedDim(example_sce, "PCA"))

##  num [1:3005, 1:50] -15.4 -15 -17.2 -16.9 -18.4 ...
##  - attr(*, "dimnames")=List of 2
##   ..$ : chr [1:3005] "1772071015_C02" "1772071017_G12" "1772071017_A05" "1772071014_B06" ...
##   ..$ : chr [1:50] "PC1" "PC2" "PC3" "PC4" ...
##  - attr(*, "varExplained")= num [1:50] 478 112.8 51.1 47 33.2 ...
##  - attr(*, "percentVar")= num [1:50] 39.72 9.38 4.25 3.9 2.76 ...
##  - attr(*, "rotation")= num [1:500, 1:50] 0.1471 0.1146 0.1084 0.0958 0.0953 ...
##   ..- attr(*, "dimnames")=List of 2
##   .. ..$ : chr [1:500] "Plp1" "Trf" "Mal" "Apod" ...
##   .. ..$ : chr [1:50] "PC1" "PC2" "PC3" "PC4" ...

# set.seed(1000)
# example_sce < runTSNE(example_sce, perplexity=10)

## 使用PCA结果的前10个维度来运行T-SNE
set.seed(1000)
example_sce <- runTSNE(example_sce, perplexity=50,
                      dimred="PCA", n_dimred=10)

example_sce <- runUMAP(example_sce)

降维的可视化。颜色可以根据colData里的已有注释,也可以根据某个基因的表达量来定义。下面代码里的Snap25和Mog都是基因名。

plotReducedDim(example_sce, 
               dimred = "PCA", 
               colour_by = "level1class")

image.png

plotTSNE(example_sce, colour_by = "Snap25")

image.png

plotPCA(example_sce, colour_by="Mog")

image.png

画多于两个维度的PCA图

plotPCA(example_sce, ncomponents = 4, colour_by = "level1class")

image.png

其他可视化

同一个基因在不同细胞间的表达量比较

ggcells(example_sce, 
        mapping=aes(x=level1class, y=Snap25)) +
  geom_boxplot() +
  facet_wrap(~tissue)+ 
  theme(axis.text.x = element_text(angle=50,vjust = 0.5))

image.png

ggcells(example_sce, 
        mapping=aes(x=TSNE.1, y=TSNE.2, colour=Snap25)) +
  geom_point() +
  stat_density_2d() +
  facet_wrap(~tissue) +
  scale_colour_distiller(direction=1)

image.png

基因表达量与sizeFactor之间的关系

ggcells(example_sce, mapping=aes(x=sizeFactor, y=Actb)) +
  geom_point() +
  geom_smooth()

image.png

某一细胞的内源性基因与线粒体基因的表达量比较

colnames(example_sce) <- make.names(colnames(example_sce))
ggfeatures(example_sce, mapping=aes(x=featureType, y=X1772062111_E06)) +
  geom_violin()

image.png

  • 0
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小洁忘了怎么分身

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值