使用scater包进行单细胞测序分析(一):数据导入与SingleCellExperiment对象构建

scater包简介

scater是一个优秀的单细胞转录组数据分析工具包,它可以对单细胞数据进行常规的质量控制,数据的标准化与归一化,以及数据的降维与可视化分析。它主要基于SingleCellExperiment类(来自SingleCellExperiment包)来进行操作处理,因此可以与其他许多Bioconductor包(如scran,batchelor和iSEE等)相互操作。

scater包主要含有以下特性:

  • Use of the SingleCellExperiment class as a data container for interoperability with a wide range of other Bioconductor packages;
  • Functions to import kallisto and Salmon results;
  • Simple calculation of many quality control metrics from the expression data;
  • Many tools for visualising scRNA-seq data, especially diagnostic plots for quality control;
  • Subsetting and many other methods for filtering out problematic cells and features;
  • Methods for identifying important experimental variables and normalising data ahead of downstream statistical analysis and modeling.

scater包的工作流程为:

image

构建SingleCellExperiment对象

使用SingleCellExperiment函数导入单细胞转录组的基因表达矩阵构建一个SingleCellExperiment对象,该表达矩阵是一个行为基因,列为细胞的大型数据框。

SingleCellExperiment对象内容

image

SingleCellExperiment对象常见操作

image

# 导入scater包
library(scater)
# 加载示例数据
data("sc_example_counts")
data("sc_example_cell_info")
# 查看基因表达矩阵
head(sc_example_counts)
          Cell_001 Cell_002 Cell_003 Cell_004 Cell_005 Cell_006 Cell_007
Gene_0001        0      123        2        0        0        0        0
Gene_0002      575       65        3     1561     2311      160        2
Gene_0003        0        0        0        0     1213        0        0
Gene_0004        0        1        0        0        0       99      476
Gene_0005        0        0       11        0        0        0        0
Gene_0006        0        0        0        0        0        0      673
          Cell_008 Cell_009 Cell_010 Cell_011 Cell_012 Cell_013 Cell_014
Gene_0001       21        2        0     2624        1     1015        0
Gene_0002        1        0        0        2        0     2710        0
Gene_0003        1        0        0        2      178        0        0
Gene_0004        0        1       66        0        1        0        1
Gene_0005        0        1        0        0        2        2        0
Gene_0006        0     3094        0        0      270        2        0
          Cell_015 Cell_016 Cell_017 Cell_018 Cell_019 Cell_020 Cell_021
Gene_0001        0        1       34        1        0        6        0
Gene_0002        4        0      908      673      174      622     2085
Gene_0003        0        0        0        0        1        0     3320
Gene_0004        0      906      655     1020        1        0        0
Gene_0005        0        0        0        2        0        0        3
Gene_0006     1176        0        3        0        0        0        1

# 查看样本信息
head(sc_example_cell_info)
             Cell Mutation_Status Cell_Cycle Treatment
Cell_001 Cell_001        positive          S    treat1
Cell_002 Cell_002        positive         G0    treat1
Cell_003 Cell_003        negative         G1    treat1
Cell_004 Cell_004        negative          S    treat1
Cell_005 Cell_005        negative         G1    treat2
Cell_006 Cell_006        negative         G0    treat1

# 使用SingleCellExperiment函数构建SingleCellExperiment对象
example_sce <- SingleCellExperiment(
    assays = list(counts = sc_example_counts), 
    colData = sc_example_cell_info
)

# 查看SingleCellExperiment对象
example_sce
class: SingleCellExperiment 
dim: 2000 40 
metadata(0):
assays(1): counts
rownames(2000): Gene_0001 Gene_0002 ... Gene_1999 Gene_2000
rowData names(0):
colnames(40): Cell_001 Cell_002 ... Cell_039 Cell_040
colData names(4): Cell Mutation_Status Cell_Cycle Treatment
reducedDimNames(0):
spikeNames(0):

View(example_sce)
image

我们通常使用原始的count矩阵存储到SingleCellExperiment对象的“counts” Assay中,同时也可以使用counts函数提取SingleCellExperiment对象中的count表达矩阵。

str(counts(example_sce))
 int [1:2000, 1:40] 0 575 0 0 0 0 0 0 416 12 ...
 - attr(*, "dimnames")=List of 2
  ..$ : chr [1:2000] "Gene_0001" "Gene_0002" "Gene_0003" "Gene_0004" ...
  ..$ : chr [1:40] "Cell_001" "Cell_002" "Cell_003" "Cell_004" ...

head(counts(example_sce))

对于样本行和列的meta信息,我们也提供了一些常用函数来进行操作处理,如isSpike, sizeFactors, 和reducedDim等函数。

# 添加一个新列的meta信息whee
example_sce$whee <- sample(LETTERS, ncol(example_sce), replace=TRUE)

# 使用colData函数查看列的meta信息
colData(example_sce)
DataFrame with 40 rows and 5 columns
                Cell Mutation_Status  Cell_Cycle   Treatment        whee
         <character>     <character> <character> <character> <character>
Cell_001    Cell_001        positive           S      treat1           N
Cell_002    Cell_002        positive          G0      treat1           T
Cell_003    Cell_003        negative          G1      treat1           Y
Cell_004    Cell_004        negative           S      treat1           T
Cell_005    Cell_005        negative          G1      treat2           C
...              ...             ...         ...         ...         ...
Cell_036    Cell_036        negative          G0      treat1           Q
Cell_037    Cell_037        negative          G0      treat1           X
Cell_038    Cell_038        negative          G0      treat2           W
Cell_039    Cell_039        negative          G1      treat1           B
Cell_040    Cell_040        negative          G0      treat2           Z

# 添加一个新行的meta信息
rowData(example_sce)$stuff <- runif(nrow(example_sce))
# 使用rowData函数查看行的meta信息
rowData(example_sce)
DataFrame with 2000 rows and 1 column
                       stuff
                   <numeric>
Gene_0001  0.146899100858718
Gene_0002  0.547358682611957
Gene_0003  0.381470382912084
Gene_0004 0.0698823253624141
Gene_0005  0.577666614903137
...                      ...
Gene_1996  0.810028552776203
Gene_1997   0.92471176572144
Gene_1998   0.73105761455372
Gene_1999  0.496801204746589
Gene_2000  0.135669085429981

# 根据基因的表达过滤掉那些在所有细胞中表达量之和为0的基因
keep_feature <- rowSums(counts(example_sce) > 0) > 0
example_sce <- example_sce[keep_feature,]

对于原始的count表达矩阵,我们也提供了一些函数对其进行数据的归一化和标准化处理。如使用calculateCPM函数计算表达量的CPM(counts-per-million)值,其结果将会存储在SingleCellExperiment对象的“cpm” Assay中,可以通过cpm函数进行访问

cpm(example_sce) <- calculateCPM(example_sce)
head(cpm(example_sce))
          Cell_001   Cell_002  Cell_003 Cell_004 Cell_005 Cell_006
Gene_0001     0.00 749.529259  6.561271    0.000    0.000   0.0000
Gene_0002  1344.85 396.092698  9.841906 5558.424 2826.476 923.5422
Gene_0003     0.00   0.000000  0.000000    0.000 1483.563   0.0000
Gene_0004     0.00   6.093734  0.000000    0.000    0.000 571.4418
Gene_0005     0.00   0.000000 36.086989    0.000    0.000   0.0000
Gene_0006     0.00   0.000000  0.000000    0.000    0.000   0.0000
             Cell_007  Cell_008    Cell_009 Cell_010    Cell_011    Cell_012
Gene_0001    0.000000 69.780424    2.698593   0.0000 9959.085768    5.882457
Gene_0002    6.938109  3.322877    0.000000   0.0000    7.590767    0.000000
Gene_0003    0.000000  3.322877    0.000000   0.0000    7.590767 1047.077301
Gene_0004 1651.269847  0.000000    1.349296 168.5733    0.000000    5.882457
Gene_0005    0.000000  0.000000    1.349296   0.0000    0.000000   11.764913
Gene_0006 2334.673545  0.000000 4174.723091   0.0000    0.000000 1588.263322
             Cell_013 Cell_014   Cell_015   Cell_016   Cell_017    Cell_018
Gene_0001 2013.948828 0.000000    0.00000    2.64154  118.89733    2.069181
Gene_0002 5377.144161 0.000000   11.56233    0.00000 3175.25816 1392.558811
Gene_0003    0.000000 0.000000    0.00000    0.00000    0.00000    0.000000
Gene_0004    0.000000 3.334801    0.00000 2393.23554 2290.52213 2110.564617
Gene_0005    3.968372 0.000000    0.00000    0.00000    0.00000    4.138362
Gene_0006    3.968372 0.000000 3399.32534    0.00000   10.49094    0.000000

同样的,我们也可以使用normalize函数进行数据的归一化处理,它将对原始的count矩阵进行一个log2的转换处理。This is done by dividing each count by its size factor (or scaled library size, if no size factors are defined), adding a pseudo-count and log-transforming. 归一化后的结果存储在"logcounts" Assay中,可以通过logcounts函数进行访问。

# 使用normalize函数进行数据归一化
example_sce <- normalize(example_sce)
# 查看assay的信息
assayNames(example_sce)
[1] "counts"    "cpm"       "logcounts"

head(logcounts(example_sce))

我们可以使用calcAverage函数计算基因的平均表达量

head(calcAverage(example_sce))
 Gene_0001  Gene_0002  Gene_0003  Gene_0004  Gene_0005  Gene_0006 
305.551749 325.719897 183.090462 162.143201   1.231123 187.167913

使用其他的方法导入基因表达矩阵

  • 对于CSV格式存储的基因表达矩阵,我们可以通过read.table()函数或data.table包中的fread()函数进行读取。
  • 对于一些大型的数据集,在读取的过程中会产生大量的缓存,需要较大的内存,因此我们可以通过Matrix包中的readSparseCounts()函数读取大型数据集,并将其存储为一个稀疏矩阵,可以有效减小系统的读取内存。
  • 对于来自10x Genomics产生的表达矩阵,我们可以通过DropletUtils包中的read10xCounts()函数进行读取,读取后它会自动生成一个SingleCellExperiment对象
  • 对于kallisto和Salmon等比对软件产生的基因表达矩阵,我们可以通过tximeta包中的 readSalmonResults()readKallistoResults()函数进行读取。

参考来源:
http://www.bioconductor.org/packages/release/bioc/vignettes/scater/inst/doc/overview.html

image
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,204评论 6 506
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,091评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,548评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,657评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,689评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,554评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,302评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,216评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,661评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,851评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,977评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,697评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,306评论 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,898评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,019评论 1 270
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,138评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,927评论 2 355

推荐阅读更多精彩内容