python和R写出表达矩阵为稀疏矩阵matrix.mtx.gz的方法

###python部分
加载读取稀疏矩阵的mmread和构建数据框的pandas

from scipy.io import mmread
import pandas as pd
import numpy as np

读取10X单细胞矩阵文件: matrix.mtx.gz(coo_matrix格式的sparse 矩阵) 、barcodes.tsv.gz (构成单细胞密集表达矩阵的列名,对应测序的细胞样本)、features.tsv.gz (构成单细胞密集表达矩阵的行名,是细胞的表达基因ID或symbolName)

_index = pd.read_csv("./features.tsv.gz", index_col=0,sep = '\t',header=None)
_index.index.name =None #把索引列的列名去掉
_col   = pd.read_csv("./barcodes.tsv.gz", index_col=0,sep = '\t',header=None)
_col.index.name =None #把列名向量的名去掉
_data  = mmread("./matrix.mtx.gz").todense()

将稀疏矩阵转换成DataFrame用pandas处理:

rna_count = pd.DataFrame(data=_data,index = _index.index,columns=_col.index)
print(rna_count .iloc[0:3,0:2])
print("gene_ID_len : "+str(rna_count .shape[0]))  #获取表达矩阵基因长度

python和R写出表达矩阵为稀疏矩阵matrix.mtx.gz的方法_第1张图片

对pd类型的表达矩阵简单标准化处理:

rna_count  = ( rna_count +1 ).applymap(np.log2)

对处理后的DataFrame保存出同cellranger格式的sparse matrix等相关文件

import os
import shutil 
import gzip
import scipy
import time
fmt='%Y-%m-%d %a %H:%M:%S'
Date=time.strftime(fmt,time.localtime(time.time()))
outdir = ".Matrix_reAnno"
os.makedirs(outdir, exist_ok=True)
##save matrix.mtx.gz
reAnno_count_sparse_mtx = scipy.sparse.coo_matrix(rna_countrna_count_combine.values)
scipy.io.mmwrite(os.path.join(outdir,'matrix.mtx'),
                 reAnno_count_sparse_mtx,
                 comment='This counts is regenerate and remapped symbol by zhuzhiyong \n Generate DateTime::'+str(Date)
                )
with open(os.path.join(outdir,'matrix.mtx'),'rb') as mtx_in:
        with gzip.open(os.path.join(outdir,'matrix.mtx') + '.gz','wb') as mtx_gz: #创建一个读写文件'matrix.mtx.gz',用以将matrix.mtx拷贝过去
            shutil.copyfileobj(mtx_in, mtx_gz)
os.remove(os.path.join(outdir,'matrix.mtx'))
##save barcodes.tsv.gz
barcodesFile = pd.DataFrame(rna_countrna_count_combine.columns)
barcodesFile.to_csv(os.path.join(outdir,"barcodes.tsv.gz"),sep='\t',header =False,index=False)
##save features.tsv.gz
featuresFile = pd.DataFrame(rna_countrna_count_combine.index)
featuresFile.to_csv(os.path.join(outdir,"features.tsv.gz"),sep='\t',header =False,index=False)

###R部分
写出expr counts 为matrix.mtx.gz

library(Matrix)
sparse.gbm <- Matrix(scRNA@assays$RNA@counts, sparse = T )
write(x = sparse.gbm@Dimnames[[1]], file = "features.tsv")
write.table([email protected], file = 'scRNA_ref_meta.tsv', sep = '\t', quote = FALSE)
writeMM(obj = sparse.gbm, file="matrix.mtx")
system("gzip matrix.mtx")  #创建压缩文件并删除原文件 matrix.mtx.gz
scales::number_bytes(file.size("matrix.mtx.gz"))

你可能感兴趣的:(python,矩阵,r语言)