def read_seurat_hdf5(hdf5_file):
import h5py
with h5py.File(hdf5_file, 'r') as handle:
cols = handle.get("seurat_matrix/columns").value
rows = handle.get("seurat_matrix/rows").value
df = handle.get("seurat_matrix/matrix").value
seurat_matrix = pd.DataFrame(df, index=cols, columns=rows).T
# add info as multiindex columns
condition = map(lambda x: x[0], seurat_matrix.columns.str.split("|"))
replicate = map(lambda x: x[1], seurat_matrix.columns.str.split("|"))
cell = map(lambda x: x[2], seurat_matrix.columns.str.split("|"))
grna = map(lambda x: x[3], seurat_matrix.columns.str.split("|"))
gene = map(lambda x: x[1] if len(x) > 1 else x[0][:4], pd.Series(grna).str.split("_"))
seurat_matrix.columns = pd.MultiIndex.from_arrays([condition, replicate, cell, grna, gene], names=['condition', 'replicate', 'cell', 'grna', 'gene'])
return seurat_matrix
评论列表
文章目录