我们还是在正式进行代码操作前想几个小问题:👇
R中?目前常用的scRNA-seq分析包,包括Seurat、Scanpy(python)、Scater、Monocle2、Monocle3等。🤒
rm(list = ls())
library(tidyverse)
library(SingleCellExperiment)
library(DropletUtils)
scRNA-seq的标准格式为SingleCellExperiment。😘
Note! Seurat包有其自己的格式,即Seurat格式,可能因为Seurat太火了吧,越来越多的包都开始兼容Seurat格式的文件了。😂
我们拿到的数据通常是一个feature-by-sample的表达矩阵。
在scRNA-seq分析中,我们一般需要从counts矩阵开始分析,代表每个cell的feature的reads/UMI。feature可以是gene、isoforms或exons。🤒

singlecellexperiment
counts <- read.table("./molecules.txt", sep = "\t")
annotation <- read.table("./annotation.txt", sep = "\t", header = TRUE)

counts

annotation
# 注意assays必须是matrix
tung <- SingleCellExperiment(
assays = list(counts = as.matrix(counts)),
colData = annotation
)
tung

Note! 这个SingleCellExperiment包含:
counts的assays;rownames)和细胞名(colnames);metadata为空 (rowData);metabdata(colData names)像上面那样创建SingleCellExperiment对象,几乎适用于任何情况,我们只需要一个counts矩阵即可。 🤗 但有时候我们获取的文件是cellranger(用于10X Chromium数据)的输出文件,这个时候我们可以用DropletUtils包中的read10xCounts函数。😀
sce <- read10xCounts("./pbmc_1k_raw")
sce <- read10xCounts("./pbmc_1k_filtered")
sce

这里需要说一下的是,如果你的矩阵就叫counts,那么counts(tung) = assay(tung, "counts")。
counts(tung)
assay(tung, "counts")[1:4,1:4]

colData(tung)

table(tung$batch)

🧐 补充!
Function | Description |
|---|---|
rowData(sce) | gene信息 |
colData(sce) | cell信息 |
assay(sce, "counts") | 查看counts矩阵 |
reducedDim(sce, "PCA") | 查看PCA降维table |
sce$colname | 查看colData的colname, 等价于colData(sce)$colname |
sce[<rows>, <columns>] | 查看特定行、列 |
举个栗子 🌰
我们把counts矩阵进行log2(x+1)转换,命名为logcounts。
assay(tung, "logcounts") <- log2(counts(tung) + 1)
# 查看一下
logcounts(tung)[1:4, 1:4]

🧐 补充!
Code | Description |
|---|---|
assay(sce, "name") <- matrix | 添加新矩阵 |
rowData(sce) <- data_frame | 替换rowData |
colData(sce) <- data_frame | 替换colData |
colData(sce)$column_name <- values | 在colData中添加/替换一个新的列 |
rowData(sce)$column_name <- values | 在rowData中添加/替换一个新的行 |
reducedDim(sce, "name") <- matrix | 添加降维矩阵 |
计算我们的数据集中每个细胞(列)的平均counts。
然后我们将它加到column metadata中作为新的一列。😘
colData(tung)$mean_counts <- colMeans(counts(tung))
这个时候我们可以查看一下,多了一列mean_counts。🤔
colData(tung)[1:4,]

这里我们再补充一下基本函数。有兴趣的小伙伴都试试吧。🫶
rowSums(counts(tung)) # sum
rowMeans(counts(tung)) # mean
rowSds(counts(tung)) # standard deviation
rowVars(counts(tung)) # variance
rowIQRs(counts(tung)) # inter-quartile range
rowMads(counts(tung)) # mean absolute deviation
colSums(counts(tung)) # sum
colMeans(counts(tung)) # mean
colSds(counts(tung)) # standard deviation
colVars(counts(tung)) # variance
colIQRs(counts(tung)) # inter-quartile range
colMads(counts(tung)) # mean absolute deviation
这里我们直接使用sweep函数来搞定counts转CPM,在这之前我们先计算一下total counts并新增一列。
colData(tung)$total_counts <- colSums(counts(tung))
assay(tung, "cpm") <- sweep(counts(tung),2,tung$total_counts/1e6,'/')
# check一下
colSums(cpm(tung))[1:10]

cpm(tung)[1:4, 1:4]
assay(tung, "cpm")[1:4, 1:4]

拿到矩阵后,我们一般需要过滤一些低表达或异常表达值。
举个栗子 🌰,假设细胞至少25000counts,基因在至少一半的细胞中拥有超过5个counts。
cell_filter <- colSums(counts(tung)) >= 25000
# check一下
table(cell_filter)

gene_filter <- rowSums(counts(tung) > 5) > ncol(tung)/2
# check一下
table(gene_filter)

tung_filtered <- tung[gene_filter, cell_filter]
tung_filtered

colSums(counts(sce)) > x # 对细胞进行过滤, Total counts per cell greater than x。
colSums(counts(sce) > x) > y # 对细胞进行过滤,Cells with at least y genes having counts greater than x.
rowSums(counts(sce)) > x # 对基因进行滤过,Total counts per gene greater than x.
rowSums(counts(sce) > x) > y # 对基因进行过滤,Genes with at least y cells having counts greater than x.

最后祝大家早日不卷!~