
作者,Evil Genius




import numpy as np
import pandas as pd
import scanpy as sc
import matplotlib.pyplot as plt
import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)
# Colours
from matplotlib import cm
from matplotlib import colors, colorbar
cmap_own = cm.get_cmap('magma_r', 256)
newcolors = cmap_own(np.linspace(0,0.75 , 256))
Greys = cm.get_cmap('Greys_r', 256)
#newcolors[:1, :] = Greys(np.linspace(0.8125, 0.8725, 1))
newcolors[:10, :] = Greys(np.linspace(0.8125, 0.8725, 10))
pos_cmap = colors.ListedColormap(newcolors)
adata=sc.read('/data1/mdai/Result/single-cell/Methods/LARIS/tutorial/adata_tonsil.h5ad')
# Check spatial mapping
x_width=adata.obsm['X_spatial'][:,0].max()-adata.obsm['X_spatial'][:,0].min()
y_width=adata.obsm['X_spatial'][:,1].max()-adata.obsm['X_spatial'][:,1].min()
plt.rcParams['figure.figsize'] = 5, 5*y_width/x_width
sc.pl.embedding(
adata,
basis='X_spatial',
color=['cell_type'],
size=80,frameon=False)
plt.rcParams['figure.figsize'] = 4, 4
# Check spatial mapping
x_width=adata.obsm['X_spatial'][:,0].max()-adata.obsm['X_spatial'][:,0].min()
y_width=adata.obsm['X_spatial'][:,1].max()-adata.obsm['X_spatial'][:,1].min()
plt.rcParams['figure.figsize'] = 5, 5*y_width/x_width
sc.pl.embedding(
adata,
basis='X_spatial',
color=['cell_type'],
groups=['T_CD4'],
size=80,frameon=False)
plt.rcParams['figure.figsize'] = 4, 4
# Check spatial UMAP
x_width=adata.obsm['X_umap'][:,0].max()-adata.obsm['X_umap'][:,0].min()
y_width=adata.obsm['X_umap'][:,1].max()-adata.obsm['X_umap'][:,1].min()
plt.rcParams['figure.figsize'] = 5, 5*y_width/x_width
sc.pl.embedding(
adata,
basis='X_umap',
color=['cell_type'],
size=40,frameon=False)
plt.rcParams['figure.figsize'] = 4, 4
lr_df=pd.read_csv('/data1/mdai/Result/single-cell/Methods/LARIS/tutorial/human_lr_database_CellChatDB_formatted_v2.csv',index_col=0)
rows_keep=np.logical_and( lr_df['ligand'].isin(adata.var_names), lr_df['receptor'].isin(adata.var_names) )
lr_df=lr_df.loc[rows_keep].copy()
%%time
lr_adata=la.tl.prepareLRInteraction(
adata, # object used to construt the LR object
lr_df, # dataframe to use for interactions
number_nearest_neighbors=20,
use_rep_spatial='X_spatial'
)
%%time
import warnings
warnings.filterwarnings("ignore")
LARIS_variable_interactions, res_LARIS=la.tl.runLARIS(
lr_adata,
adata,
use_rep='X_spatial',
n_nearest_neighbors=20,
random_seed=27,
n_repeats=5,
mu = 0.40, # larger value indicates more consideration for specificity
sigma=100,
remove_lowly_expressed=False,
expressed_pct=0.1,
n_cells_expressed_threshold=100,
n_top_lr=lr_adata.shape[1],
by_celltype=True,
### Parameters for cell type-level inference when by_celltype set to True
groupby='cell_type', # label to group by
use_rep_spatial='X_spatial', # spatial coordinates
mu_celltype=100, # higher value puts more emphasis on cell type specificity
expressed_pct_celltype=0.1, # expression percentage cut off per cell type
remove_lowly_expressed_celltype=False,
mask_threshold=1e-6, # mask for cosg, lower value is less restrictive
n_neighbors_permutation=30,
score_threshold= 1e-10,
spatial_weight = 3.0,
)
sc.pl.dotplot(lr_adata,
LARIS_variable_interactions.index[:10],
groupby='region_name',
standard_scale='var',
cmap='Spectral_r')
x_width=adata.obsm['X_spatial'][:,0].max()-adata.obsm['X_spatial'][:,0].min()
y_width=adata.obsm['X_spatial'][:,1].max()-adata.obsm['X_spatial'][:,1].min()
plt.rcParams['figure.figsize'] = 2.5, 2.5*y_width/x_width
sc.pl.embedding(
lr_adata,
basis='X_spatial',
color=LARIS_variable_interactions.index[:4],
cmap=pos_cmap,
# vmin=1,
# vmax=1.5,
# palette=self_palette2,
ncols=4,
# size=80,
frameon=False)
plt.rcParams['figure.figsize'] = 4, 4
x_width=adata.obsm['X_spatial'][:,0].max()-adata.obsm['X_spatial'][:,0].min()
y_width=adata.obsm['X_spatial'][:,1].max()-adata.obsm['X_spatial'][:,1].min()
plt.rcParams['figure.figsize'] = 2.5, 2.5*y_width/x_width
sc.pl.embedding(
lr_adata,
basis='X_spatial',
color=res_LARIS['interaction_name'][:12].unique(),
cmap=pos_cmap,
# vmin=1,
# vmax=1.5,
# palette=self_palette2,
ncols=3,
# size=80,
frameon=False)
plt.rcParams['figure.figsize'] = 4, 4
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。