2020-04-17

2020-04-17  本文已影响0人  吹不散的烽烟

自学单细胞笔记

认识sce object

[参考文献]https://www.researchgate.net/publication/332045971_Orchestrating_Single-Cell_Analysis_with_Bioconductor
image.png

(图片引自Robert A. Amezquita et al, 2019 )

学习sce Object

环境变量设置

rm(list = ls())
library(BiocManager)
# BiocManager::install("SingleCellExperiment")
# BiocManager::install("scran")
# BiocManager::install("scater")
library(SingleCellExperiment)
library(scran)
library(scater)

构建一个表达矩阵

counts_matrix <- data.frame(cell_1 = rpois(10, 10),
                            cell_2 = rpois(10, 10),
                            cell_3 = rpois(10, 30))
rownames(counts_matrix) <- paste0("gene_", 1:10)
counts_matrix <- as.matrix(counts_matrix) # must be a matrix object!

构建sce对象

sce <- SingleCellExperiment(assays = list(counts = counts_matrix))
class(sce)
sce

取出表达矩阵counts

assay(sce,"counts") #方法1
counts(sce) #方法2
#metadata(1): log.exprs.offset
#assays(2): counts logcounts

添加矩阵

sce <- scran::computeSumFactors(sce)
sce
sce <- scater::normalize(sce)
sce

提取添加的矩阵

assay(sce,"logcounts") #方法1
logcounts(sce)  #方法2
assays(sce) ### 查看assays列表

添加矩阵counts_100

counts_100 <- assay(sce, "counts") + 100
counts_100
assay(sce, "counts_100") <- counts_100 # assign a new entry to assays slot
sce
assays(sce)

构建sample metadata:batch

cell_metadata <- data.frame(batch = c(1, 1, 2))
rownames(cell_metadata) <- paste0("cell_", 1:3) ##paste和paste0区别sep="是否有空格"
## From scratch:
sce <- SingleCellExperiment(assays = list(counts = counts_matrix),
                            colData = cell_metadata) ##添加sample metadata: batch effect通过colData
## Appending to existing object (requires DataFrame() coercion)
## colData(sce) <- DataFrame(cell_metadata)
sce
colData(sce) #获取batch effect

利用scater添加calculateQCMetrics 查看colData

sce <- scater::calculateQCMetrics(sce)
colData(sce)[, 1:5]
sce$batch
## colData(sce)$batch # same as above

构建子集

sce[, sce$batch == 1] ##构建子集batch=1

添加feature metadata

rowRanges(sce)  # empty
rowData(sce)[, 1:3]
sce[c("gene_1", "gene_4"), ]
## sce[c(1, 4), ] # same as above in this case

添加sizeFactors

sce <- scran::computeSumFactors(sce)
sce <- scater::normalize(sce)
sizeFactors(sce)

添加reducedDim:PCA,TSNE,UMAP

sce <- scater::runPCA(sce)#降维分析
reducedDim(sce, "PCA")
sce <- scater::runTSNE(sce, perplexity = 0.1)
reducedDim(sce, "TSNE")
reducedDims(sce)
u <- uwot::umap(t(logcounts(sce)), n_neighbors = 2)
reducedDim(sce, "UMAP_uwot") <- u
reducedDim(sce, "UMAP_uwot")
reducedDims(sce)

添加others metadata

my_genes <- c("gene_1", "gene_5")
metadata(sce) <- list(favorite_genes = my_genes)
metadata(sce)
your_genes <- c("gene_4", "gene_8")
metadata(sce)$your_genes <- your_genes
metadata(sce)
上一篇 下一篇

猜你喜欢

热点阅读