作者,Evil Genius
其实关于细胞niche,之前已经分享了R版本的代码了,内容在
10X空间转录组数据分析之细胞niche
甚至还分享了关于分子niche的内容10X空间转录组数据分析补充之分子niche
分析代码拿到如下的结果:
但是我们还是需要更新python版本,因为python版本的的细胞niche与cell2location进行对接,用到了目前比较流程的NMF的方法,所以在分析内容上会丰富很多。
即使现在空间平台精度是不统一的,
但是空间的四大矩阵是逃不开的。
1、分子矩阵,gene X barcode,这是最开始大家拿到的矩阵
2、细胞矩阵,空间解卷积之后的矩阵,细胞 X Barcode
3、分子niche矩阵, 即分子生态位矩阵,主要研究分子微环境,包括邻域通讯等, gene X Barcode
4、细胞niche矩阵,即细胞生态位矩阵,主要用来研究细胞的空间排布,例如侵袭性的肿瘤细胞空间临近巨噬,所有细胞的空间排布形成了细胞niche矩阵.
在做细胞niche的时候,需要先进行单细胞空间的联合分析,推荐cell2location(python版本)
关于cell2location在空转系列课程上全部讲过了,大家回看视频就可以了,视频全放在B站了。
其中cell2cation可以拿到如下的分析结果
cell2location在平常的个性化项目分析中也是非常常用和推荐的,客户都非常认可。
进一步可以看通路的空间分布,空转打分的方法也在空转课程的系列视频中讲过了,大家会看就好,视频也在B站。
为了在组织切片上识别细胞niche,借助cell2location包的NMF功能来预测细胞类型丰度,注意这里就不同于R版本,R版本需要Barcode X 细胞矩阵先进行聚类,然后识别每一类的细胞类型的丰度,而这里在寻找niche的时候采用的是目前比较流程的NMF的方法,
方法上是有一些改进的,我们来分享一下这部分代码
加载模块
import scanpy as sc
import squidpy as sq
import numpy as np
import pandas as pd
import os
import sys
import seaborn as sb
import matplotlib.pyplot as plt
from matplotlib import colors
#import scvi
import anndata as ad
import warnings
warnings.filterwarnings("ignore")
from collections import Counter
import ipywidgets as widgets
from ipywidgets import interact, interact_manual
plt.rcParams['figure.figsize'] = (6, 6)
from IPython.core.display import display, HTML
import random
#Define a colour map for gene expression
colors2 = plt.cm.Reds(np.linspace(0, 1, 128))
colors3 = plt.cm.Greys_r(np.linspace(0.7,0.8,20))
#colorsComb = np.vstack([colors3, colors2])
#mymap = colors.LinearSegmentedColormap.from_list('my_colormap', colorsComb)
from matplotlib import colors
colorsComb = np.vstack([plt.cm.Reds(np.linspace(0, 1, 128)), plt.cm.Greys_r(np.linspace(0.7, 0.8, 0))])
mymap = colors.LinearSegmentedColormap.from_list('my_colormap', colorsComb)
# Helper function to split list in chunks
def chunks(lista, n):
for i in range(0, len(lista), n):
yield lista[i:i + n]
plt.rcParams['figure.figsize'] = (6, 5)
sc.set_figure_params(dpi=100, vector_friendly=True)
def mysize(w, h, d):
fig, ax = plt.subplots(figsize = (w, h), dpi = d)
return(fig.gca())
plt.rcParams['figure.figsize'] = (6, 5)
sc.set_figure_params(dpi=100, vector_friendly=True)
sc.settings.figdir = "./figures/"
import scvi
## frequently used variables
from matplotlib import colors
import matplotlib.pyplot as plt
colorsComb = np.vstack([plt.cm.Reds(np.linspace(0, 1, 128)), plt.cm.Greys_r(np.linspace(0.7, 0.8, 0))])
mymap = colors.LinearSegmentedColormap.from_list('my_colormap', colorsComb)
## Along these Lines, a colourmap diverging from gray to red
gray_red = colors.LinearSegmentedColormap.from_list("grouping", ["lightgray", "red", "darkred"], N = 128)
## Some more Colour Maps
gray_violet = colors.LinearSegmentedColormap.from_list("grouping", ["lightgray", "mediumvioletred", "indigo"], N = 128)
gray_blue = colors.LinearSegmentedColormap.from_list("grouping", ["lightgray", "cornflowerblue", "darkblue"], N = 128)
def mysize(w, h, d):
fig, ax = plt.subplots(figsize = (w, h), dpi = d)
return(fig.gca())
#plt.rcParams['figure.figsize'] = (6, 5)
#sc.set_figure_params(dpi=120, vector_friendly=True)
import matplotlib.colors as colors
c_low = colors.colorConverter.to_rgba('orange', alpha = 0)
c_high = colors.colorConverter.to_rgba('red',alpha = 1)
cmap_transparent = colors.LinearSegmentedColormap.from_list('rb_cmap',[c_low, c_high], 512)
import matplotlib.colors as colors
c_low2 = colors.colorConverter.to_rgba('green', alpha = 0)
c_high2 = colors.colorConverter.to_rgba('darkblue',alpha = 1)
cmap_transparent2 = colors.LinearSegmentedColormap.from_list('rb_cmap',[c_low2, c_high2], 512)
import cell2location as c2l
from cell2location.utils import select_slide
读取数据,这里注意需要完成单细胞空间联合分析的部分
##load
adata_vis = sc.read(f"/.h5ad")
####细胞类型,根据自己的情况设定
cts = ['AT0', 'AT1', 'AT2', 'Aberrant basaloid', 'Adventitial fibroblast', 'Alveolar fibroblast',
'Artery', 'B/Plasma', 'Basal', 'Basophil/Mast', 'Bronchial Vessel', 'Capillary', 'Capillary Aerocyte',
'Ciliated', 'Ciliated SFTPB+/SCGB1A1+', 'Dendritic', 'Ionocyte', 'Lymphatic', 'Macrophage C1Q hi',
'Macrophage CHI3L1+/CD9 hi/', 'Macrophage FABP4+', 'Macrophage IL1B+', 'Macrophage LYVE1+',
'Macrophage RETN+/VCAN+', 'Mesothelial', 'Monocyte', 'Mucous', 'Myofibroblast', 'NK',
'Peribronchial fibroblast', 'Pericyte', 'Smooth Muscle', 'T cell', 'TB-SC', 'Vein', 'pDC', 'preTB-SC/RAS']