








import scniche as sn
import numpy as np
import scanpy as sc
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from scniche.pl import *
import warnings
warnings.filterwarnings('ignore')
adata = sc.read_h5ad('tnbc.h5ad.gz')
adata
AnnData object with n_obs × n_vars = 173205 × 36
obs: 'SampleID', 'cellLabelInImage', 'cellSize', 'C', 'Na', 'Si', 'P', 'Ca', 'Fe', 'Background', 'B7H3', 'OX40', 'CD163', 'CSF.1R', 'Ta', 'Au', 'tumorYN', 'tumorCluster', 'Group', 'immuneCluster', 'immuneGroup', 'group_name', 'immuneGroup_name', 'all_group_name', 'leiden', 'x', 'y', 'scNiche', 'subtype', 'all_group_name2'
obsm: 'X_cn', 'X_cn_norm', 'X_data', 'X_data_nbr', 'X_pca', 'X_scniche', 'spatial'
kwargs = {'figsize': (5, 3)}
sn.pl.stacked_barplot(adata, x_axis='scNiche', y_axis='all_group_name', mode='proportion', palette=palettes.default_20, kwargs=kwargs)
sn.pl.stacked_barplot(adata, x_axis='scNiche', y_axis='all_group_name2', mode='proportion', palette=['#426cb5', '#bababa', '#ff7700']
##Enrichment analysis framework
# cell type enrichment
sn.al.enrichment(adata, id_key='all_group_name', val_key='scNiche', library_key='SampleID')
# plot
kwargs = {'figsize': (8, 8), 'vmax': 4, 'cmap': 'YlOrBr', 'linewidths': 0, 'linecolor': 'white', }
col_order = ['CD4 T', 'CD8 T', 'DC/Mono', 'NK', 'B', 'DC', 'Tregs', 'Macrophages', 'CD3 T', 'Endothelial', 'Other immune', 'Mono/Neu', 'Neutrophils', 'Mesenchymal like', 'Tumor', 'Keratin+ tumor', ]
row_order = ['Niche11', 'Niche5', 'Niche3', 'Niche9', 'Niche2', 'Niche10', 'Niche1', 'Niche12', 'Niche8', 'Niche0', 'Niche4', 'Niche6', 'Niche7', ]
adata.uns['scNiche_colors'] = palettes.default_57
colors1 = adata.uns['scNiche_colors']
categories1 = adata.obs['scNiche'].cat.categories
palette_use = [colors1[list(categories1).index(cat)] for cat in row_order]
sn.pl.enrichment_heatmap(adata=adata, id_key='all_group_name', val_key='scNiche', binarized=False, show_pval=True,
col_order=col_order, row_order=row_order, anno_key=None, anno_palette=palette_use, kwargs=kwargs)
# sample enrichment
sn.al.enrichment(adata, id_key='scNiche', val_key='SampleID', library_key=None)
# plot
kwargs = {'figsize': (6.2, 10), 'vmax': 4, 'cmap': 'magma'}
col_order = ['Niche2', 'Niche9', 'Niche4', 'Niche6', 'Niche7', 'Niche11', 'Niche12', 'Niche0', 'Niche5', 'Niche10', 'Niche1', 'Niche8', 'Niche3', ]
row_order = [37, 29, 13, 39, 11, 12, 32, 16, 4, 9, 17, 5, 35, 18, 20,14, 2, 10, 34, 23, 38, 31,6, 27,7, 1, 8,28, 21,36, 41, 33, 40, 3, ]
palette_use = ['#2166AC', '#B2182B', ]
sn.pl.enrichment_heatmap(adata=adata, id_key='scNiche', val_key='SampleID', binarized=False, col_order=col_order, row_order=row_order,
anno_key='subtype', anno_palette=palette_use, kwargs=kwargs)
# tumor niches: cell type enrichmant
tumor_list = ['Niche1', 'Niche10', 'Niche2', 'Niche9', 'Niche3', 'Niche5', 'Niche11']
adata_tumor = adata[adata.obs['scNiche'].isin(tumor_list)].copy()
sn.al.enrichment(adata_tumor, id_key='all_group_name', val_key='scNiche', library_key='SampleID')
# plot
kwargs = {'figsize': (9, 6), 'vmax': 4, 'cmap': 'YlOrBr', 'linewidths': 0, 'linecolor': 'white', }
row_order = ['Niche9', 'Niche3', 'Niche10', 'Niche2', 'Niche1', 'Niche5', 'Niche11']
col_order = ['CD4 T', 'CD8 T', 'DC/Mono', 'NK', 'B', 'DC', 'Tregs', 'Macrophages', 'CD3 T', 'Endothelial', 'Other immune', 'Mono/Neu', 'Neutrophils', 'Mesenchymal like', 'Tumor', 'Keratin+ tumor',]
palette_use = [palettes.default_57[i] for i in [9, 3, 10, 2, 1, 5, 11]]
sn.pl.enrichment_heatmap(adata=adata_tumor, id_key='all_group_name', val_key='scNiche', binarized=False, col_order=col_order, row_order=row_order,
anno_key=None, anno_palette=palette_use, show_pval=True, kwargs=kwargs)
import scniche as sn
import numpy as np
import scanpy as sc
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from scniche.pl import *
import warnings
warnings.filterwarnings('ignore')
print("Last run with scNiche version:", sn.__version__)
# set seed
sn.pp.set_seed()
adata = sc.read_h5ad('liver.h5ad.gz')
# normalize first
sc.pp.normalize_total(adata, target_sum=1e4)
sc.pp.log1p(adata)
adata
AnnData object with n_obs × n_vars = 37505 × 20423
obs: 'tile', 'x', 'y', 'condition', 'celltype', 'cell', 'scNiche', 'group'
uns: 'log1p'
obsm: 'X_cn', 'X_cn_norm', 'X_data', 'X_data_nbr', 'X_scVI', 'X_scniche', 'spatial'
kwargs = {'figsize': (5, 3)}
sn.pl.stacked_barplot(adata, x_axis='scNiche', y_axis='celltype', mode='proportion', palette=palettes.default_20, kwargs=kwargs)
palette_use = ["#1e40af", "#1d4ed8", "#2563eb", "#3b82f6", "#60a5fa", "#93c5fd", "#fdba74", "#fb923c", "#f97316", "#ea580c"]
sn.pl.stacked_barplot(adata, x_axis='scNiche', y_axis='tile', mode='proportion', palette=palette_use, kwargs=kwargs)
# cell type enrichment
sn.al.enrichment(adata, id_key='celltype', val_key='scNiche', library_key='tile')
# plot
kwargs = {'figsize': (8, 8), 'vmax': 4, 'cmap': 'YlOrBr', 'linewidths': 0, 'linecolor': 'white', }
row_order =[ 'Niche0', 'Niche3', 'Niche12', 'Niche5', 'Niche14', 'Niche11', 'Niche1', 'Niche10', 'Niche2', 'Niche6', 'Niche8', 'Niche13', 'Niche7', 'Niche9', 'Niche4', ]
col_order = ['Macrophage_Inflamed', 'HPC', 'HSC-A', 'Hep_Injured', 'RBC', 'Macrophage_Kupffer', 'HSC-N', 'ENDO', 'Hep_MT', 'Hep_Nuc', 'Hep_PP', 'Hep_Mup10','Hep_Gstp', 'Hep_Eef1a1', 'Hep_PC', ]
adata.uns['scNiche_colors'] = palettes.default_57
colors1 = adata.uns['scNiche_colors']
categories1 = adata.obs['scNiche'].cat.categories
palette_use = [colors1[list(categories1).index(cat)] for cat in row_order]
sn.pl.enrichment_heatmap(adata=adata, id_key='celltype', val_key='scNiche', binarized=False, show_pval=True, col_order=col_order, row_order=row_order,
anno_key=None, anno_palette=palette_use, kwargs=kwargs)
# sample enrichment
sn.al.enrichment(adata, id_key='scNiche', val_key='tile', library_key=None)
# plot
kwargs = {'figsize': (5, 5), 'vmax': 3, 'cmap': 'magma'}
col_order = [ 'Niche0', 'Niche3', 'Niche12', 'Niche5', 'Niche14', 'Niche11', 'Niche1', 'Niche10', 'Niche2', 'Niche6', 'Niche8', 'Niche13', 'Niche7', 'Niche9', 'Niche4', ]
palette_use = ['#2166AC', '#B2182B', ]
sn.pl.enrichment_heatmap(adata=adata, id_key='scNiche', val_key='tile', binarized=False, col_order=col_order, row_order=None,
anno_key='group', anno_palette=palette_use, kwargs=kwargs)
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。