作者:ahworld
链接:scanpy读取10X HD经验
来源:微信公众号seqyuan
著作权归作者所有,任何形式的转载都请联系作者。
scanpy或squidpy没有专门针对10X HD数据的读取方法,分享给大家一个自用的函数用scanpy读取10X HD数据,并使用squidpy可视化。
import pandas as pd
import scanpy as sc
import squidpy as sq
def read_HD(data_dir):
raw_h5_file = data_dir+'/filtered_feature_bc_matrix.h5'
adata = sc.read_10x_h5(raw_h5_file)
# Load the Spatial Coordinates
tissue_position_file = data_dir+'/spatial/tissue_positions.parquet'
df_tissue_positions=pd.read_table(tissue_position_file, sep=",")
#Set the index of the dataframe to the barcodes
df_tissue_positions = df_tissue_positions.set_index('barcode')
# Create an index in the dataframe to check joins
df_tissue_positions['index']=df_tissue_positions.index
# Adding the tissue positions to the meta data
adata.obs = pd.merge(adata.obs, df_tissue_positions, left_index=True, right_index=True)
adata.obsm['spatial'] = adata.obs[["pxl_col_in_fullres", "pxl_row_in_fullres"]].values
return adata
data_dir = "/path/SpaceRanger_Count/Pancreas/outs/binned_outputs/square_016um/"
data_dir的路径下需要包涵下面的两个文件
.
├── filtered_feature_bc_matrix.h5
└── spatial
└── tissue_positions.parquet
adata = read_HD(data_dir)
sc.pp.calculate_qc_metrics(
adata, inplace=True, log1p=True
)
sq.pl.spatial_scatter(
adata, shape=None, color="n_genes_by_counts", size=0.5, library_id="spatial", figsize=(7, 5)
)