和Seurat一样,Scanpy也提供了多样本空转数据的整合
https://scanpy-tutorials.readthedocs.io/en/latest/spatial/integration-scanorama.html
单细胞空间转录分析之Scanpy:https://www.jianshu.com/p/8dc231c06932
单细胞空间转录分析之Scanpy-多样本整合:https://www.jianshu.com/p/caa98aeac191
单细胞空间转录分析之Seurat:https://www.jianshu.com/p/c9a601ced91f
单细胞空间转录分析之Seurat-多样本整合(浅谈空间批次):https://www.jianshu.com/p/609b04096b79
这是也使用了10X官网,下载获得小鼠大脑同一张切片前端和后端空转数据,包括anterior1,posterior1。https://support.10xgenomics.com/spatial-gene-expression/datasets/1.1.0/V1_Mouse_Brain_Sagittal_Anterior
https://support.10xgenomics.com/spatial-gene-expression/datasets/1.1.0/V1_Mouse_Brain_Sagittal_Posterior
导入相关包
import scanpy as sc
import anndata as an
import pandas as pd
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import seaborn as sns
import scanorama
sc.logging.print_versions()
sc.set_figure_params(facecolor="white", figsize=(8, 8))
sc.settings.verbosity = 3
##设置输入路径
import os
os.chdir('./scanpy/Integrate') ##修改路径
results_file = 'integrat_spatial.h5ad' ##设置结果文件保存路径
读取数据,标准化
adata_spatial_anterior1 = sc.read_visium("./Mouse/Brain_Section1_Sagittal_Anterior/Brain_anterior1/outs")
adata_spatial_posterior1 = sc.read_visium("./Mouse/Brain_Section1_Sagittal_Posterior/Brain_posterior1/outs")
adata_spatial_anterior1.var_names_make_unique()
adata_spatial_posterior1.var_names_make_unique()
sc.pp.calculate_qc_metrics(adata_spatial_anterior1, inplace=True)
sc.pp.calculate_qc_metrics(adata_spatial_posterior1, inplace=True)
for name, adata in [
("anterior", adata_spatial_anterior1),
("posterior", adata_spatial_posterior1),
]:
fig, axs = plt.subplots(1, 4, figsize=(20, 4))
fig.suptitle(f"Covariates for filtering: {name}")
sns.distplot(adata.obs["total_counts"], kde=False, bins=60, ax=axs[0])
sns.distplot(adata.obs["total_counts"][adata.obs["total_counts"] < 20000],kde=False, bins=60,ax=axs[1],)
sns.distplot(adata.obs["n_genes_by_counts"], kde=False, bins=60, ax=axs[2])
sns.distplot(adata.obs["n_genes_by_counts"][adata.obs["n_genes_by_counts"] < 4000],kde=False,bins=60,ax=axs[3], )
plt.savefig(f"QC_plot_{name}.pdf")
for adata in [
adata_spatial_anterior1,
adata_spatial_posterior1,
]:
sc.pp.normalize_total(adata, inplace=True)
sc.pp.log1p(adata)
sc.pp.highly_variable_genes(adata, flavor="seurat", n_top_genes=2000, inplace=True)
Scanorama数据整合
adatas = [adata_spatial_anterior1,adata_spatial_posterior1]
adatas_cor = scanorama.correct_scanpy(adatas, return_dimred=True)
adata_spatial = adatas_cor[0].concatenate(
adatas_cor[1],
batch_key="library_id",
uns_merge="unique",
batch_categories=[
k
for d in [
adatas_cor[0].uns["spatial"],
adatas_cor[1].uns["spatial"],
]
for k, v in d.items()
],
)
sc.pp.neighbors(adata_spatial, use_rep="X_scanorama")
sc.tl.umap(adata_spatial)
sc.tl.leiden(adata_spatial, key_added="clusters")
sc.pl.umap(adata_spatial, color=["clusters", "library_id"], palette=sc.pl.palettes.default_20)
plt.savefig("umap.pdf")
clusters_colors = dict(
zip([str(i) for i in range(18)], adata_spatial.uns["clusters_colors"])
)
fig, axs = plt.subplots(1, 2, figsize=(15, 10))
for i, library in enumerate(
['Brain_anterior1','Brain_posterior1']
):
ad = adata_spatial[adata_spatial.obs.library_id == library, :].copy()
sc.pl.spatial(
ad,
img_key="hires",
library_id=library,
color="clusters",
size=1.5,
palette=[
v
for k, v in clusters_colors.items()
if k in ad.obs.clusters.unique().tolist()
],
legend_loc=None,
show=False,
ax=axs[i],
)
plt.tight_layout()
plt.savefig("spatial.pdf")
相比Seurat merge直接整合,我们可以看到Scanpy去掉了部分批次,两张切片连接处有所改善。