假设您正在使用类似于 os 与 Seurat 包一起加载的 pbmc_small 数据对象。查看?SubsetRow
帮助页面上的示例:
# Installing the package:Seurat does install quite a few additonal packages
library(Seurat)
cd_genes <- SubsetRow(data = pbmc_small@raw.data, code = 'CD')
str(cd_genes)
#=================
Formal class 'dgTMatrix' [package "Matrix"] with 6 slots
..@ i : int [1:209] 0 5 6 9 5 8 9 10 15 5 ...
..@ j : int [1:209] 0 0 0 0 1 1 1 1 1 2 ...
..@ Dim : int [1:2] 16 80
..@ Dimnames:List of 2
.. ..$ : chr [1:16] "CD79B" "CD79A" "CD19" "CD180" ...
.. ..$ : chr [1:80] "ATGCCAGAACGACT" "CATGGCCTGTGCAT" "GAACCTGATGAACC" "TGACTGGATTCTCA" ...
..@ x : num [1:209] 1 4 1 2 4 2 2 1 1 4 ...
..@ factors : list()
#===========
rownames(x = cd_genes@data)
行名中的错误(x = cd_genes@data):类“dgTMatrix”的这个对象没有名称“数据”的槽
所以该对象中没有@data 插槽
相反,只需在 cd_genes 上使用行名:
rownames(x = cd_genes)
[1] "CD79B" "CD79A" "CD19" "CD180" "CD200" "CD3D" "CD2" "CCDC104" "CD3E"
[10] "CD7" "CD8A" "CD14" "CD1C" "CD68" "CD9" "CD247"
所以这会从该对象中删除名称“CD200”:
> object<-SubsetRow(pbmc_small@raw.data, code="^CD200$", invert = TRUE)
> str(object)
Formal class 'dgTMatrix' [package "Matrix"] with 6 slots
..@ i : int [1:4453] 1 5 8 11 22 29 32 33 35 37 ...
..@ j : int [1:4453] 0 0 0 0 0 0 0 0 0 0 ...
..@ Dim : int [1:2] 229 80
..@ Dimnames:List of 2
.. ..$ : chr [1:229] "MS4A1" "CD79B" "CD79A" "HLA-DRA" ...
.. ..$ : chr [1:80] "ATGCCAGAACGACT" "CATGGCCTGTGCAT" "GAACCTGATGAACC" "TGACTGGATTCTCA" ...
..@ x : num [1:4453] 1 1 3 1 1 4 1 5 1 1 ...
..@ factors : list()
> "CD200" %in% rownames(object)
[1] FALSE
> "CD200" %in% rownames(pbmc_small@raw.data)
[1] TRUE
-objects 中有一个-nameddata
插槽,Seurat
但是一旦您将其提取出来,该对象中就不再有data
-slot:
slotNames(pbmc_small)
[1] "raw.data" "data" "scale.data" "var.genes" "is.expr" "ident"
[7] "meta.data" "project.name" "dr" "assay" "hvg.info" "imputed"
[13] "cell.names" "cluster.tree" "snn" "calc.params" "kmeans" "spatial"
[19] "misc" "version"
slotNames(pbmc_small@data)
[1] "i" "p" "Dim" "Dimnames" "x" "factors"
根据评论,问题的沟通似乎不完整。如果问题是如何修改现有的插槽值,那么只需@<-
按照此处的示例使用:
pbmc_small2 <- pbmc_small
pbmc_small2@data <- SubsetRow(data = pbmc_small@data, code = 'CD')
但是,我不确定它是否安全。插槽的尺寸@data
现在与插槽的尺寸不同@raw.data
,其他功能可能不匹配,尽管我对该结构的了解不够多,无法确定。使用 S4 对象的安全方法是依赖包作者提供的功能,而不是使用像插槽这样的低级东西。显然,他们希望您能够从稀疏矩阵中进行子集化,但他们是否希望您将它们分配回插槽并不那么清楚。