读取h5ad文件时,X是一维而非二维,应将稀疏矩阵转为稠密矩阵;(https://blog.csdn.net/weixin_34945803/article/details/106576629)
# 读取h5ad文件
ref_adata = sc.read("D:/ad.h5ad")
print(ref_adata)
# 稀疏矩阵转为稠密矩阵
def todense(adata):
import scipy
if isinstance(adata.X, scipy.sparse.csr_matrix) or isinstance(adata.X, scipy.sparse.csc_matrix):
return adata.X.todense()
else:
return adata.X
# 调用函数转为稠密矩阵
t = todense(ref_adata)
# 另存为csv文件
expression_data = pd.DataFrame(data=t)
expression_data.to_csv('D:/100expression_data.csv', index=False)
# 读取细胞类型y
cell_types = adata.obs['cell_type'].unique()
# 也可以分别存为x,y文件,再读取
x = pd.read_csv("D:/d3_expression_data副本.csv", header=None)
y = pd.read_csv("D:/d3_celltype副本.csv")
adata = sc.AnnData(x)
adata.obs['Group'] = y
# 其中有时候y的读取出错
y = y.reset_index(drop=True)
adata = sc.AnnData(x)
adata.obs['Group'] = y.values