参考链接:
- https://support.10xgenomics.com/single-cell-gene-expression/software/pipelines/5.0/advanced/h5_matrices
- 参考源码/path/to/CellRanger-6.1.2/lib/python/cellranger/{matrix.py,feature_ref.py}
在Cellranger比对后的数据中也存有h5格式的数据,我们也可以从这个数据里面获取数据。
上述参考链接有两种方法,这里只讲一种,首先是source一下环境:
source /path/to/CellRanger-6.1.2/sourceme.bash
然后编辑一个python脚本test.py
如下:
import cellranger.matrix as cr_matrix
filtered_matrix_h5 = "filtered_feature_bc_matrix.h5"
filtered_feature_bc_matrix = cr_matrix.CountMatrix.load_h5_file(filtered_matrix_h5)
#print(bytes.decode(filtered_feature_bc_matrix.bcs[0])) # 细胞barcode
#print(filtered_feature_bc_matrix.m[1:10,]) # 矩阵10行
#print(filtered_feature_bc_matrix.m.shape) # 矩阵大小
#print(filtered_feature_bc_matrix.feature_ref.feature_defs[0].name) # symbol
# 获取类似feature.tsv格式的数据(三列 : id symbol和type)
for FeatureDef in filtered_feature_bc_matrix.feature_ref.feature_defs:
print(bytes.decode(FeatureDef.id).strip(),"\t",FeatureDef.name.strip(),"\t",FeatureDef.feature_type.strip(), sep='')
load_h5_file()返回的数据格式CountMatrix类
CountMatrix类
也是类似于对应filtered_feature_bc_matrix/目录下面 features.tsv和barcodes.tsv和matrix.mtx,不过数据格式类型不相同。
这里的feature_ref每个基因存储格式是python的namedtuple格式:
barcode是OrderedDict格式的keys(),也就是一个list格式,不过里面的变量是bytes格式,需要使用bytes.decode()转成str来显示
最后是matrix的格式是sp_sparse.spmatrix,是稀疏矩阵的一种存储方式。
稀疏矩阵
网友评论