Crop image of ROI

Main function

Function 1:For Visium, crop the region of interest (ROI) image and see cell type or gene expr.
- Usually, the HE image of Visium (NPC) < 1 GB, in our test, nuclei-segmentation is worked.
- The input roi_path is unnecessary for run StarDist_nuclei_segmente.py.
- It can be set segment=False in fst.crop_img_adata() to avoide .obsm['spatial'] ajustifation.
Function 2:For Visium HD, crop ROI image with corresponding adata, save for nuclei-segmentation.
- Usually, the HE image of Visium HD (CRC) > 10 GB, nuclei-segmentation is limited by storage.
- Notice: the measured region for CRC dataset is much less than the given HE mage (~1/6).
- Set the selected region as roi_path to run StarDist_nuclei_segmente.py, see ROI4_shape.csv here.
- At this time, setting segment=True (default) in fst.crop_img_adata() to adjust .obsm['spatial'].
- When making nuclei-segmentation, roi_path is necessary for run StarDist_nuclei_segmente.py.
- Here, we also provide cropping the measured/whole image from one big HE image.
[2]:
import os
import warnings
warnings.filterwarnings('ignore')
import matplotlib.pyplot as plt
import numpy as np
[ ]:
# import FineST as fst
# from FineST.datasets import dataset
# import FineST.plottings as fstplt
# print("FineST version: %s" %fst.__version__)
[ ]:
path = '/mnt/lingyu/nfs_share2/Python/'
os.chdir(str(path) + 'FineST/FineST/')
import FineST as fst
from FineST.datasets import dataset
import FineST.plottings as fstplt
print("FineST version: %s" %fst.__version__)
FineST version: 0.0.9
[ ]:
# import importlib
# import sys
# sys.path.append(str(path)+'FineST/FineST/')
# import FineST as fst
# import FineST.plottings
# importlib.reload(FineST.plottings )
# from FineST.plottings  import *
# print("FineST version: %s" %fst.__version__)
FineST version: 0.0.9

1. Crop ROI image from NPC Visium dataset

1.1 Crop image with adata

The 10x Visium dataset (NPC_patient_1) from Gong, et al can be downloaded from NPC1 in Goole Drive, where ROI1.csv is another ROI in paper.

Input
- roi_path: the pathway of the selected region using napari package.
- img_path: the original .tif HE image with high-resolution, it is about 800 MB here.
- adata_path: the .h5ad adata corresponding to the HE image, from 10x Visium dataset.
Output
- crop_img_path: the cropped image, saved in .tif format.
- crop_adata_path: the saved ST data, selected the .obsm['spatial'] matched cropped image.
[5]:
os.chdir(str(path))
roi_path = './FineST/FineST_local/Dataset/NPC/CropROI/Annodata/ROI1_TLS.csv'
img_path = './NPC/Data/stdata/GSE200310_RAW/patient1/20210809-C-AH4199551.tif'
adata_path = './FineST/FineST_local/Dataset/NPC/TransImp/patient1_nuclei_anno_TransImp_NPC1_sc.h5ad'
crop_img_path = './FineST/FineST_local/Dataset/NPC/CropROI/NPC1_cropped_ROI_image.tif'
crop_adata_path = './FineST/FineST_local/Dataset/NPC/CropROI/NPC1_sc_ROI.h5ad'
[6]:
cropped_img, adata_roi = fst.crop_img_adata(roi_path,
                                            img_path, adata_path,
                                            crop_img_path, crop_adata_path,
                                            segment=False, save=True)
ROI coordinates from napari package:
    index shape-type  vertex-index        axis-0       axis-1
0      3    polygon             0   9233.387767  5426.173165
1      3    polygon             1   9233.387767  6922.797893
2      3    polygon             2  10150.000000  6922.797893
3      3    polygon             3  10150.000000  5426.173165
img shape:
 (17351, 17319, 3)
polygon:
 [[ 9233.387767  5426.173165]
 [ 9233.387767  6922.797893]
 [10150.        6922.797893]
 [10150.        5426.173165]]
polygon adjusted:
 [[ 9233.387767  5426.173165]
 [ 9233.387767  6922.797893]
 [10150.        6922.797893]
 [10150.        5426.173165]]
cropped_img shape:
 (917, 1496, 3)
The adata:
 AnnData object with n_obs × n_vars = 40068 × 596
    obs: 'x', 'y', 'cell_type'
    uns: 'spatial'
    obsm: 'TransImp_ct_pred', 'X_pca', 'spatial'
    varm: 'PCs'
The range of original adata:
 [[1791, 13306], [1357, 11398]]

Show image shape and adata property

[7]:
print(cropped_img.shape)
print(adata_roi)
(917, 1496, 3)
AnnData object with n_obs × n_vars = 869 × 596
    obs: 'x', 'y', 'cell_type'
    uns: 'spatial'
    obsm: 'TransImp_ct_pred', 'X_pca', 'spatial'
    varm: 'PCs'

See the range of the spatial coordinats of the 1st colnum and 2nd colnum in adata.obsm['spatial']

[8]:
print(adata_roi.obsm["spatial"][:,0].min(), adata_roi.obsm["spatial"][:,0].max())
print(adata_roi.obsm["spatial"][:,1].min(), adata_roi.obsm["spatial"][:,1].max())
5547 6921
9234 10149

Plot the crooped ROI region in original HE image with high-resolution

[9]:
plt.imshow(cropped_img)
plt.show()
_images/Crop_ROI_Boundary_image_17_0.png

See the gene expression saved in adata.X, only contains the samples that belongs to the cropped ROI image

[10]:
adata_roi.to_df()
[10]:
TGFB1 TGFBR1 TGFBR2 TGFB2 TGFB3 ACVR1B ACVR1C ACVR1 BMP2 BMPR1A ... KDR TREM2 SEMA6A SEMA6B SEMA7A PLXNC1 SIGLEC1 THY1 VCAM1 VSIR
6447 0.198093 0.097630 0.198527 0.003750 0.152948 0.097517 0.003173 0.004177 0.003968 0.132939 ... 0.004140 0.004109 0.004189 0.004627 0.004840 0.074180 0.004038 0.069006 0.462114 0.589054
6455 0.334080 0.441975 0.119213 0.004921 0.119349 0.028712 0.003824 0.048485 0.004010 0.149004 ... 0.021774 0.066070 0.004470 0.071718 0.059498 0.017121 0.004436 0.205508 0.440389 1.044084
6565 0.451144 0.128095 0.081239 0.004624 0.377256 0.071486 0.004701 0.012491 0.004921 0.077157 ... 0.063230 0.004575 0.004751 0.014711 0.081885 0.059292 0.004636 0.144947 0.282062 0.860366
6606 0.202606 0.170748 0.219795 0.004157 0.084789 0.053321 0.002863 0.004264 0.004208 0.162051 ... 0.003749 0.003521 0.004434 0.004202 0.004913 0.003386 0.003636 0.115993 0.592637 0.791180
6628 0.451679 0.128360 0.097917 0.004488 0.364679 0.099784 0.004456 0.027165 0.033183 0.118700 ... 0.004788 0.004697 0.010375 0.004548 0.111223 0.056878 0.004427 0.122713 0.414587 0.874933
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
17926 0.703735 0.155722 0.054981 0.082285 0.075187 0.339756 0.002853 0.103673 0.003895 0.168941 ... 0.004126 0.011032 0.004477 0.055611 0.276188 0.133410 0.134314 0.673328 0.720801 0.772462
17928 0.660003 0.204267 0.056055 0.004687 0.103312 0.282786 0.008591 0.154334 0.003907 0.055584 ... 0.055088 0.023433 0.025247 0.046613 0.277214 0.187400 0.187673 1.119787 0.798056 0.844024
17929 0.294317 0.169527 0.067037 0.064530 0.026901 0.416031 0.002459 0.092877 0.004721 0.123815 ... 0.004063 0.038419 0.048832 0.003885 0.164652 0.346541 0.346903 1.783141 0.899748 0.836969
17936 0.981570 0.781067 0.356109 0.354484 0.447250 0.898237 0.003626 0.397402 0.055774 0.461117 ... 0.056405 0.004630 0.004537 0.356769 0.220263 0.003947 0.044389 0.052299 0.802892 1.417577
17937 1.145367 1.005562 0.463364 0.507830 0.519761 1.077884 0.002977 0.532443 0.010940 0.594457 ... 0.011548 0.028324 0.004479 0.464335 0.167199 0.003616 0.010824 0.007024 0.748205 1.649552

869 rows × 596 columns

The NPC1 adata visium_nuclei_annotated_TransImp_NPC1.h5ad is obtained from nuclei-segmentaion and cell type annotation, so we can show the cells within this ROI. For others ones can only show the gene expression situation.

[12]:
import matplotlib.pyplot as plt
ctype_hex_map = {'B': '#565DFD',
 'normal': '#4fa9ff',
 'Treg': '#FE664D',
 'fibroblast': '#9f50f9',
 'Myeloid': '#009203',
 'tumor': '#e5e022',
 'T': '#CB6035'}

fig, ax = plt.subplots(1, 1, dpi=100)
sc.pl.spatial(adata_roi, img_key='hires', color="cell_type", title='TransImp',
              size=0.25, alpha_img=0.8, palette=ctype_hex_map, ax=ax)
_images/Crop_ROI_Boundary_image_21_0.png
[13]:
os.chdir(f"{path}/FineST/FineST_local/Dataset/NPC/CropROI/")
!pwd
/mnt/lingyu/nfs_share2/Python/FineST/FineST_local/Dataset/NPC/CropROI
[16]:
fstplt.gene_expr(adata_roi, adata_roi.to_df(), gene_selet='CD70', marker='o',
                 s=8, figsize=(5, 2.5), save_path='CD70_expr_ROI1.pdf')
fstplt.gene_expr(adata_roi, adata_roi.to_df(), gene_selet='CD27', marker='o',
                 s=8, figsize=(5, 2.5), save_path='CD27_expr_ROI1.pdf')
_images/Crop_ROI_Boundary_image_23_0.png
_images/Crop_ROI_Boundary_image_23_1.png

1.2 CCC analysis within the cropped ROI

[17]:
import spatialdm as sdm
import spatialdm.plottings as pl
import matplotlib.pyplot as plt
print("SpatailDM version: %s" %sdm.__version__)
SpatailDM version: 0.2.0
[18]:
adata_impt_sc = adata_roi
[19]:
visium_scale_factors = fst.json_load(f"{path}/FineST/FineST_local/Dataset/NPC/patient1/")
print(visium_scale_factors['spot_diameter_fullres'])
139.44595843130838
[20]:
# spot_diameter_fullres = visium_scale_factors["spot_diameter_fullres"]
spot_diameter_fullres = 112
fst.weight_matrix(adata_impt_sc, l = spot_diameter_fullres,
                  cutoff = 0.001, single_cell = True, n_nearest_neighbors=6)
[20]:
AnnData object with n_obs × n_vars = 869 × 596
    obs: 'x', 'y', 'cell_type'
    uns: 'spatial', 'cell_type_colors', 'single_cell'
    obsm: 'TransImp_ct_pred', 'X_pca', 'spatial'
    varm: 'PCs'
    obsp: 'weight', 'nearest_neighbors'
[21]:
## Visualize the range of interaction
plt.figure(figsize=(5, 2.5))
plt.scatter(adata_impt_sc.obsm['spatial'][:,0], adata_impt_sc.obsm['spatial'][:,1],
            c=adata_impt_sc.obsp['weight'].A[500], s=10.0)
plt.gca().invert_yaxis()
plt.tick_params(axis='both', which='both', bottom=True, left=True, labelbottom=True)
_images/Crop_ROI_Boundary_image_29_0.png
[22]:
## find overlapping LRs from CellChatDB
start = time.time()
sdm.extract_lr(adata_impt_sc, 'human', min_cell=3)
print("%.3f seconds" %(time.time()-start))
print(adata_impt_sc)
11.320 seconds
AnnData object with n_obs × n_vars = 869 × 596
    obs: 'x', 'y', 'cell_type'
    uns: 'spatial', 'cell_type_colors', 'single_cell', 'mean', 'ligand', 'receptor', 'num_pairs', 'geneInter'
    obsm: 'TransImp_ct_pred', 'X_pca', 'spatial'
    varm: 'PCs'
    obsp: 'weight', 'nearest_neighbors'
[23]:
## see the condidiate LR pairs for this NPC datset
adata_impt_sc.uns['geneInter']
[23]:
interaction_name pathway_name agonist antagonist co_A_receptor co_I_receptor evidence annotation interaction_name_2
EFNA5_EPHB2 EFNA5_EPHB2 EPHA NaN NaN NaN NaN PMID:15107857; PMID: 15114347 Cell-Cell Contact EFNA5 - EPHB2
EFNB1_EPHA4 EFNB1_EPHA4 EPHB NaN NaN NaN NaN PMID: 15114347 Cell-Cell Contact EFNB1 - EPHA4
EFNB1_EPHB1 EFNB1_EPHB1 EPHB NaN NaN NaN NaN PMID: 15114347 Cell-Cell Contact EFNB1 - EPHB1
EFNB1_EPHB2 EFNB1_EPHB2 EPHB NaN NaN NaN NaN PMID: 15114347 Cell-Cell Contact EFNB1 - EPHB2
EFNB1_EPHB3 EFNB1_EPHB3 EPHB NaN NaN NaN NaN PMID: 15114347 Cell-Cell Contact EFNB1 - EPHB3
... ... ... ... ... ... ... ... ... ...
CCL3_CCR5 CCL3_CCR5 CCL NaN NaN NaN NaN KEGG: hsa04060 Secreted Signaling CCL3 - CCR5
CCL5_CCR5 CCL5_CCR5 CCL NaN NaN NaN NaN KEGG: hsa04060 Secreted Signaling CCL5 - CCR5
CCL4_CCR5 CCL4_CCR5 CCL NaN NaN NaN NaN KEGG: hsa04060 Secreted Signaling CCL4 - CCR5
CXCL1_ACKR1 CXCL1_ACKR1 CXCL NaN NaN NaN NaN PMID: 26740381 Secreted Signaling CXCL1 - ACKR1
CORT_SSTR2 CORT_SSTR2 SEMATOSTATIN NaN NaN NaN NaN KEGG: hsa04080 Secreted Signaling CORT - SSTR2

1129 rows × 9 columns

[24]:
## Identify dataset-specific interacting LR pairs (global selection)
start = time.time()

# global Moran selection
sdm.spatialdm_global(adata_impt_sc, n_perm=1000, specified_ind=None, method='z-score', nproc=1)

# select significant pairs
sdm.sig_pairs(adata_impt_sc, method='z-score', fdr=True, threshold=0.05)

print("%.3f seconds" %(time.time()-start))
print(adata_impt_sc)
2.895 seconds
AnnData object with n_obs × n_vars = 869 × 596
    obs: 'x', 'y', 'cell_type'
    uns: 'spatial', 'cell_type_colors', 'single_cell', 'mean', 'ligand', 'receptor', 'num_pairs', 'geneInter', 'global_I', 'global_stat', 'global_res'
    obsm: 'TransImp_ct_pred', 'X_pca', 'spatial'
    varm: 'PCs'
    obsp: 'weight', 'nearest_neighbors'
[25]:
spa_coexp_pair_sc = fst.anno_LRpair(adata_impt_sc)
spa_coexp_pair_sc
[25]:
Ligand0 Ligand1 Receptor0 Receptor1 Receptor2 z_pval z fdr selected evidence annotation
SEMA3B_NRP1_PLXNA1 SEMA3B None NRP1 PLXNA1 None 0.000000 58.350883 0.0 True PMID: 27533782 Secreted Signaling
WNT5B_FZD3 WNT5B None FZD3 None None 0.000000 58.902293 0.0 True KEGG: hsa04310 Secreted Signaling
WNT5B_FZD2 WNT5B None FZD2 None None 0.000000 117.182087 0.0 True KEGG: hsa04310 Secreted Signaling
WNT5A_FZD1 WNT5A None FZD1 None None 0.000000 79.102866 0.0 True KEGG: hsa04310 Secreted Signaling
WNT4_FZD6_LRP6 WNT4 None FZD6 LRP6 None 0.000000 100.444906 0.0 True KEGG: hsa04310; PMID: 23209147 Secreted Signaling
... ... ... ... ... ... ... ... ... ... ... ...
TGFB3_ACVR1C_TGFBR2 TGFB3 None ACVR1C TGFBR2 None 1.000000 -21.368593 1.0 False PMID: 27449815 Secreted Signaling
SEMA3G_NRP2_PLXNA3 SEMA3G None NRP2 PLXNA3 None 1.000000 -12.182868 1.0 False PMID: 27533782 Secreted Signaling
TGFB2_ACVR1_TGFBR1 TGFB2 None ACVR1 TGFBR1 TGFBR2 0.704050 -0.536083 1.0 False PMID: 29376829 Secreted Signaling
SEMA3A_NRP1_PLXNA1 SEMA3A None NRP1 PLXNA1 None 1.000000 -80.378268 1.0 False PMID: 27533782 Secreted Signaling
NRXN1_NLGN3 NRXN1 None NLGN3 None None 0.999822 -3.570680 1.0 False KEGG: hsa04514 Cell-Cell Contact

1129 rows × 11 columns

[26]:
!pwd
/mnt/lingyu/nfs_share2/Python/FineST/FineST_local/Dataset/NPC/CropROI
[27]:
## see the unique gene of sig LR pairs
spa_coexp_pairTgene_sc = fst.LRpair_gene(spa_coexp_pair_sc)
print("spa_coexp_pairTgene_sc shape:", len(spa_coexp_pairTgene_sc))
spa_coexp_pairTgene_sc shape: 392
[28]:
## save 798 significant LR pairs
# spa_coexp_pair_sc.to_csv("spa_coexp_pair_sc_ROI_singleT.csv", index=True, header=True)
# spa_coexp_pairTgene_sc.to_csv("spa_coexp_LRgene_sc_ROI_singleT.csv", index=True, header=True)
[29]:
## use fdr corrected global p-values and a threshold FDR < 0.1 (default)
print(adata_impt_sc.uns['global_res'].selected.sum())
adata_impt_sc.uns['global_res'].sort_values(by='fdr')
633
[29]:
Ligand0 Ligand1 Receptor0 Receptor1 Receptor2 z_pval z fdr selected
SEMA3B_NRP1_PLXNA1 SEMA3B None NRP1 PLXNA1 None 0.000000 58.350883 0.0 True
WNT5B_FZD3 WNT5B None FZD3 None None 0.000000 58.902293 0.0 True
WNT5B_FZD2 WNT5B None FZD2 None None 0.000000 117.182087 0.0 True
WNT5A_FZD1 WNT5A None FZD1 None None 0.000000 79.102866 0.0 True
WNT4_FZD6_LRP6 WNT4 None FZD6 LRP6 None 0.000000 100.444906 0.0 True
... ... ... ... ... ... ... ... ... ...
TGFB3_ACVR1C_TGFBR2 TGFB3 None ACVR1C TGFBR2 None 1.000000 -21.368593 1.0 False
SEMA3G_NRP2_PLXNA3 SEMA3G None NRP2 PLXNA3 None 1.000000 -12.182868 1.0 False
TGFB2_ACVR1_TGFBR1 TGFB2 None ACVR1 TGFBR1 TGFBR2 0.704050 -0.536083 1.0 False
SEMA3A_NRP1_PLXNA1 SEMA3A None NRP1 PLXNA1 None 1.000000 -80.378268 1.0 False
NRXN1_NLGN3 NRXN1 None NLGN3 None None 0.999822 -3.570680 1.0 False

1129 rows × 9 columns

[30]:
## Local selection is then run for the selected 787 pairs to identify where the LRI takes place
adata_impt_sc.raw = adata_impt_sc

start = time.time()

# local spot selection
sdm.spatialdm_local(adata_impt_sc, n_perm=1000, method='z-score', specified_ind=None, nproc=1)

# significant local spots
sdm.sig_spots(adata_impt_sc, method='z-score', fdr=False, threshold=0.05)

print("%.3f seconds" %(time.time()-start))

print(adata_impt_sc)
2.012 seconds
AnnData object with n_obs × n_vars = 869 × 596
    obs: 'x', 'y', 'cell_type'
    uns: 'spatial', 'cell_type_colors', 'single_cell', 'mean', 'ligand', 'receptor', 'num_pairs', 'geneInter', 'global_I', 'global_stat', 'global_res', 'local_stat', 'local_z', 'local_z_p', 'selected_spots'
    obsm: 'TransImp_ct_pred', 'X_pca', 'spatial'
    varm: 'PCs'
    obsp: 'weight', 'nearest_neighbors'
[31]:
adata_impt_sc.uns["local_z_p"]
[31]:
6447 6455 6565 6606 6628 6742 6745 6750 6751 6756 ... 17866 17867 17896 17918 17921 17926 17928 17929 17936 17937
EFNA5_EPHB2 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.0 1.0 1.000000 ... 1.000000 0.999999 0.680734 7.843035e-01 7.611461e-01 1.000000 0.358194 0.589553 0.999999 1.000000
EFNB1_EPHA4 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.0 1.0 1.000000 ... 1.000000 1.000000 1.000000 9.998118e-01 9.999623e-01 1.000000 1.000000 1.000000 1.000000 1.000000
EFNB1_EPHB2 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.0 1.0 1.000000 ... 1.000000 1.000000 0.782109 3.991351e-04 5.161432e-06 1.000000 1.000000 1.000000 1.000000 1.000000
EFNB1_EPHB3 1.000000 0.999975 1.000000 0.860773 1.000000 1.000000 0.862382 1.0 1.0 1.000000 ... 0.794550 0.998119 0.998188 3.091866e-07 1.703935e-09 0.976528 0.967216 0.352597 0.999810 0.999999
EFNB1_EPHB4 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 0.474585 1.0 1.0 1.000000 ... 0.594133 0.954794 0.978483 8.458961e-01 9.556086e-01 0.815551 0.738425 0.474521 0.974414 0.992965
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
CCL20_CCR6 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.0 1.0 1.000000 ... 0.973242 0.560701 1.000000 1.000000e+00 1.000000e+00 0.783516 1.000000 0.980221 0.532373 0.855033
CCL3_CCR5 0.722064 1.000000 0.999726 1.000000 0.999105 0.998531 1.000000 1.0 1.0 0.997533 ... 1.000000 0.877487 1.000000 4.094817e-05 3.691173e-02 1.000000 1.000000 1.000000 0.375085 1.000000
CCL5_CCR5 0.849123 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.0 1.0 1.000000 ... 1.000000 1.000000 1.000000 1.848242e-09 6.749696e-17 1.000000 1.000000 1.000000 1.000000 1.000000
CCL4_CCR5 0.505591 1.000000 0.838770 1.000000 0.684509 0.330065 1.000000 1.0 1.0 0.236554 ... 0.869476 0.562464 1.000000 9.809579e-02 9.248781e-05 1.000000 0.999896 0.787115 0.501884 0.989836
CORT_SSTR2 0.207027 1.000000 0.835457 1.000000 0.963006 0.976470 0.371912 1.0 1.0 1.000000 ... 1.000000 1.000000 0.381642 8.120394e-01 6.526331e-01 0.338745 1.000000 0.474874 1.000000 1.000000

633 rows × 869 columns

[32]:
fst.topLRpairs(adata_impt_sc, spa_coexp_pair_sc, num=10)
[32]:
['CDH1_CDH1',
 'JAM3_JAM3',
 'CADM1_CADM1',
 'F11R_F11R',
 'JAM2_JAM2',
 'ESAM_ESAM',
 'PECAM1_PECAM1',
 'MPZL1_MPZL1',
 'CD99_CD99',
 'CDH2_CDH2']
[34]:
fstplt.global_plot(adata_impt_sc, pairs=['CXCL16_CXCR6', "MIF_ACKR3", 'PVR_TIGIT'],
               figsize=(4.0, 4.0), loc=4, cmap='RdGy_r', vmin=-1.5, vmax=2,  max_step=0.3, min_step=0.1)
plt.savefig('NPC1_3pair_sc_ROI1_singleT.svg', transparent=True, dpi=300, bbox_inches='tight')
_images/Crop_ROI_Boundary_image_41_0.png
[35]:
fstplt.plot_pairs_dot(adata_impt_sc, ['PVR_TIGIT'], trans=True, figsize=(30,2.4),
                      marker_size=10)
_images/Crop_ROI_Boundary_image_42_0.png
[36]:
fstplt.plot_pairs_dot(adata_impt_sc, ['PVR_TIGIT'], trans=True, figsize=(30,2.4),
                      marker_size=10, pdf="NPC1_sc_pair_ROI1_PVR_TIGIT")
_images/Crop_ROI_Boundary_image_43_0.png
[37]:
# bin_spots = (1-adata_impt_sc.uns["local_z_p"])[adata_impt_sc.uns['local_stat']['n_spots']>2]
bin_spots = adata_impt_sc.uns['selected_spots'].astype(int)[adata_impt_sc.uns['local_stat']['n_spots']>2]
print(bin_spots.shape[0], " pairs used for spatial clustering")
bin_spots=bin_spots.fillna(0)
bin_spots
633  pairs used for spatial clustering
[37]:
6447 6455 6565 6606 6628 6742 6745 6750 6751 6756 ... 17866 17867 17896 17918 17921 17926 17928 17929 17936 17937
EFNA5_EPHB2 0 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0
EFNB1_EPHA4 0 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0
EFNB1_EPHB2 0 0 0 0 0 0 0 0 0 0 ... 0 0 0 1 1 0 0 0 0 0
EFNB1_EPHB3 0 0 0 0 0 0 0 0 0 0 ... 0 0 0 1 1 0 0 0 0 0
EFNB1_EPHB4 0 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
CCL20_CCR6 0 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0
CCL3_CCR5 0 0 0 0 0 0 0 0 0 0 ... 0 0 0 1 1 0 0 0 0 0
CCL5_CCR5 0 0 0 0 0 0 0 0 0 0 ... 0 0 0 1 1 0 0 0 0 0
CCL4_CCR5 0 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 1 0 0 0 0 0
CORT_SSTR2 0 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0

633 rows × 869 columns

[44]:
from threadpoolctl import threadpool_limits
import NaiveDE
import SpatialDE

with threadpool_limits(limits=1, user_api='blas'):
    start = time.time()
    results = SpatialDE.run(adata_impt_sc.obsm['spatial'], bin_spots.transpose())
    print("--- %.3f seconds ---" %(time.time()-start))

    start = time.time()
    histology_results, patterns = SpatialDE.aeh.spatial_patterns(
        adata_impt_sc.obsm['spatial'], bin_spots.transpose(), results, C=3, l=3, verbosity=1)
    print("--- %.3f seconds ---" %(time.time()-start))
INFO:root:Performing DE test
INFO:root:Pre-calculating USU^T = K's ...
INFO:root:Done: 1.2s
INFO:root:Fitting gene models
Models:   0%|                                                                                                                 | 0/10 [00:00<?, ?it/s]
  0%|                                                                                                                        | 0/633 [00:00<?, ?it/s]
  9%|█████████▉                                                                                                    | 57/633 [00:00<00:01, 559.99it/s]
 18%|███████████████████▍                                                                                         | 113/633 [00:00<00:00, 543.78it/s]
 27%|█████████████████████████████                                                                                | 169/633 [00:00<00:00, 548.83it/s]
 37%|███████████████████████████████████████▉                                                                     | 232/633 [00:00<00:00, 578.53it/s]
 46%|██████████████████████████████████████████████████▎                                                          | 292/633 [00:00<00:00, 583.90it/s]
 55%|████████████████████████████████████████████████████████████▍                                                | 351/633 [00:00<00:00, 581.37it/s]
 67%|████████████████████████████████████████████████████████████████████████▍                                    | 421/633 [00:00<00:00, 616.05it/s]
 77%|████████████████████████████████████████████████████████████████████████████████████▍                        | 490/633 [00:00<00:00, 637.39it/s]
 88%|███████████████████████████████████████████████████████████████████████████████████████████████▍             | 554/633 [00:00<00:00, 637.82it/s]
 98%|██████████████████████████████████████████████████████████████████████████████████████████████████████████▍  | 618/633 [00:01<00:00, 618.72it/s]
Models:  10%|██████████▌                                                                                              | 1/10 [00:01<00:09,  1.05s/it]
  0%|                                                                                                                        | 0/633 [00:00<?, ?it/s]
  9%|█████████▋                                                                                                    | 56/633 [00:00<00:01, 558.11it/s]
 18%|███████████████████▎                                                                                         | 112/633 [00:00<00:00, 547.24it/s]
 26%|████████████████████████████▊                                                                                | 167/633 [00:00<00:00, 541.75it/s]
 35%|██████████████████████████████████████▏                                                                      | 222/633 [00:00<00:00, 540.65it/s]
 44%|███████████████████████████████████████████████▋                                                             | 277/633 [00:00<00:00, 538.02it/s]
 52%|█████████████████████████████████████████████████████████▏                                                   | 332/633 [00:00<00:00, 541.39it/s]
 61%|██████████████████████████████████████████████████████████████████▋                                          | 387/633 [00:00<00:00, 540.51it/s]
 70%|████████████████████████████████████████████████████████████████████████████                                 | 442/633 [00:00<00:00, 540.14it/s]
 79%|█████████████████████████████████████████████████████████████████████████████████████▌                       | 497/633 [00:00<00:00, 540.33it/s]
 88%|███████████████████████████████████████████████████████████████████████████████████████████████▌             | 555/633 [00:01<00:00, 549.66it/s]
 96%|█████████████████████████████████████████████████████████████████████████████████████████████████████████    | 610/633 [00:01<00:00, 548.38it/s]
Models:  20%|█████████████████████                                                                                    | 2/10 [00:02<00:08,  1.12s/it]
  0%|                                                                                                                        | 0/633 [00:00<?, ?it/s]
  9%|█████████▍                                                                                                    | 54/633 [00:00<00:01, 535.34it/s]
 17%|██████████████████▉                                                                                          | 110/633 [00:00<00:00, 547.17it/s]
 26%|████████████████████████████▍                                                                                | 165/633 [00:00<00:00, 548.37it/s]
 35%|█████████████████████████████████████▉                                                                       | 220/633 [00:00<00:00, 548.67it/s]
 44%|███████████████████████████████████████████████▋                                                             | 277/633 [00:00<00:00, 553.49it/s]
 53%|█████████████████████████████████████████████████████████▎                                                   | 333/633 [00:00<00:00, 552.30it/s]
 61%|██████████████████████████████████████████████████████████████████▉                                          | 389/633 [00:00<00:00, 550.13it/s]
 70%|████████████████████████████████████████████████████████████████████████████▋                                | 445/633 [00:00<00:00, 544.59it/s]
 79%|██████████████████████████████████████████████████████████████████████████████████████▍                      | 502/633 [00:00<00:00, 551.37it/s]
 88%|████████████████████████████████████████████████████████████████████████████████████████████████             | 558/633 [00:01<00:00, 548.84it/s]
 97%|█████████████████████████████████████████████████████████████████████████████████████████████████████████▌   | 613/633 [00:01<00:00, 548.90it/s]
Models:  30%|███████████████████████████████▌                                                                         | 3/10 [00:03<00:07,  1.14s/it]
  0%|                                                                                                                        | 0/633 [00:00<?, ?it/s]
 12%|█████████████▏                                                                                                | 76/633 [00:00<00:00, 754.98it/s]
 24%|██████████████████████████▎                                                                                  | 153/633 [00:00<00:00, 760.24it/s]
 37%|████████████████████████████████████████▎                                                                    | 234/633 [00:00<00:00, 778.82it/s]
 50%|██████████████████████████████████████████████████████                                                       | 314/633 [00:00<00:00, 784.30it/s]
 62%|███████████████████████████████████████████████████████████████████▋                                         | 393/633 [00:00<00:00, 738.22it/s]
 74%|████████████████████████████████████████████████████████████████████████████████▌                            | 468/633 [00:00<00:00, 723.75it/s]
 85%|█████████████████████████████████████████████████████████████████████████████████████████████▏               | 541/633 [00:00<00:00, 700.26it/s]
 97%|█████████████████████████████████████████████████████████████████████████████████████████████████████████▍   | 612/633 [00:00<00:00, 689.88it/s]
Models:  40%|██████████████████████████████████████████                                                               | 4/10 [00:04<00:06,  1.04s/it]
  0%|                                                                                                                        | 0/633 [00:00<?, ?it/s]
  9%|█████████▋                                                                                                    | 56/633 [00:00<00:01, 542.20it/s]
 18%|███████████████████                                                                                          | 111/633 [00:00<00:01, 521.75it/s]
 26%|████████████████████████████▏                                                                                | 164/633 [00:00<00:00, 508.51it/s]
 34%|█████████████████████████████████████                                                                        | 215/633 [00:00<00:00, 502.85it/s]
 42%|█████████████████████████████████████████████▊                                                               | 266/633 [00:00<00:00, 483.02it/s]
 50%|██████████████████████████████████████████████████████▏                                                      | 315/633 [00:00<00:00, 476.13it/s]
 58%|███████████████████████████████████████████████████████████████▎                                             | 368/633 [00:00<00:00, 490.70it/s]
 66%|███████████████████████████████████████████████████████████████████████▉                                     | 418/633 [00:00<00:00, 483.01it/s]
 74%|████████████████████████████████████████████████████████████████████████████████▍                            | 467/633 [00:00<00:00, 466.37it/s]
 82%|████████████████████████████████████████████████████████████████████████████████████████▊                    | 516/633 [00:01<00:00, 470.74it/s]
 89%|█████████████████████████████████████████████████████████████████████████████████████████████████            | 564/633 [00:01<00:00, 466.46it/s]
 97%|█████████████████████████████████████████████████████████████████████████████████████████████████████████▏   | 611/633 [00:01<00:00, 456.47it/s]
Models:  50%|████████████████████████████████████████████████████▌                                                    | 5/10 [00:05<00:05,  1.15s/it]
  0%|                                                                                                                        | 0/633 [00:00<?, ?it/s]
  8%|█████████▏                                                                                                    | 53/633 [00:00<00:01, 523.49it/s]
 17%|██████████████████▍                                                                                          | 107/633 [00:00<00:00, 529.02it/s]
 25%|███████████████████████████▋                                                                                 | 161/633 [00:00<00:00, 531.72it/s]
 34%|█████████████████████████████████████                                                                        | 215/633 [00:00<00:00, 529.86it/s]
 42%|██████████████████████████████████████████████▏                                                              | 268/633 [00:00<00:00, 526.57it/s]
 51%|███████████████████████████████████████████████████████▎                                                     | 321/633 [00:00<00:00, 523.57it/s]
 59%|████████████████████████████████████████████████████████████████▍                                            | 374/633 [00:00<00:00, 519.14it/s]
 67%|█████████████████████████████████████████████████████████████████████████▎                                   | 426/633 [00:00<00:00, 517.71it/s]
 76%|██████████████████████████████████████████████████████████████████████████████████▎                          | 478/633 [00:00<00:00, 501.79it/s]
 84%|███████████████████████████████████████████████████████████████████████████████████████████                  | 529/633 [00:01<00:00, 503.56it/s]
 92%|████████████████████████████████████████████████████████████████████████████████████████████████████         | 581/633 [00:01<00:00, 505.67it/s]
100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████| 633/633 [00:01<00:00, 507.96it/s]
Models:  60%|███████████████████████████████████████████████████████████████                                          | 6/10 [00:06<00:04,  1.18s/it]
  0%|                                                                                                                        | 0/633 [00:00<?, ?it/s]
  8%|████████▊                                                                                                     | 51/633 [00:00<00:01, 502.68it/s]
 16%|█████████████████▋                                                                                           | 103/633 [00:00<00:01, 509.59it/s]
 24%|██████████████████████████▋                                                                                  | 155/633 [00:00<00:00, 510.83it/s]
 33%|███████████████████████████████████▋                                                                         | 207/633 [00:00<00:00, 505.40it/s]
 41%|████████████████████████████████████████████▍                                                                | 258/633 [00:00<00:00, 502.34it/s]
 49%|█████████████████████████████████████████████████████▏                                                       | 309/633 [00:00<00:00, 504.67it/s]
 57%|██████████████████████████████████████████████████████████████▏                                              | 361/633 [00:00<00:00, 506.17it/s]
 65%|██████████████████████████████████████████████████████████████████████▉                                      | 412/633 [00:00<00:00, 499.79it/s]
 73%|███████████████████████████████████████████████████████████████████████████████▌                             | 462/633 [00:00<00:00, 493.24it/s]
 81%|████████████████████████████████████████████████████████████████████████████████████████▌                    | 514/633 [00:01<00:00, 498.96it/s]
 89%|█████████████████████████████████████████████████████████████████████████████████████████████████            | 564/633 [00:01<00:00, 495.59it/s]
 97%|█████████████████████████████████████████████████████████████████████████████████████████████████████████▋   | 614/633 [00:01<00:00, 489.81it/s]
Models:  70%|█████████████████████████████████████████████████████████████████████████▌                               | 7/10 [00:08<00:03,  1.21s/it]
  0%|                                                                                                                        | 0/633 [00:00<?, ?it/s]
  9%|██████████                                                                                                    | 58/633 [00:00<00:01, 568.46it/s]
 18%|███████████████████▉                                                                                         | 116/633 [00:00<00:00, 571.49it/s]
 28%|██████████████████████████████▊                                                                              | 179/633 [00:00<00:00, 595.77it/s]
 38%|█████████████████████████████████████████▊                                                                   | 243/633 [00:00<00:00, 610.66it/s]
 48%|████████████████████████████████████████████████████▌                                                        | 305/633 [00:00<00:00, 611.47it/s]
 58%|███████████████████████████████████████████████████████████████▏                                             | 367/633 [00:00<00:00, 588.14it/s]
 67%|█████████████████████████████████████████████████████████████████████████▎                                   | 426/633 [00:00<00:00, 568.30it/s]
 76%|███████████████████████████████████████████████████████████████████████████████████▎                         | 484/633 [00:00<00:00, 548.91it/s]
 85%|████████████████████████████████████████████████████████████████████████████████████████████▉                | 540/633 [00:00<00:00, 538.58it/s]
 94%|██████████████████████████████████████████████████████████████████████████████████████████████████████▎      | 594/633 [00:01<00:00, 532.81it/s]
Models:  80%|████████████████████████████████████████████████████████████████████████████████████                     | 8/10 [00:09<00:02,  1.19s/it]
  0%|                                                                                                                        | 0/633 [00:00<?, ?it/s]
 11%|████████████▎                                                                                                 | 71/633 [00:00<00:00, 704.58it/s]
 22%|████████████████████████▍                                                                                    | 142/633 [00:00<00:00, 660.08it/s]
 34%|████████████████████████████████████▊                                                                        | 214/633 [00:00<00:00, 685.24it/s]
 45%|████████████████████████████████████████████████▋                                                            | 283/633 [00:00<00:00, 679.91it/s]
 56%|████████████████████████████████████████████████████████████▌                                                | 352/633 [00:00<00:00, 668.46it/s]
 67%|████████████████████████████████████████████████████████████████████████▍                                    | 421/633 [00:00<00:00, 673.82it/s]
 77%|████████████████████████████████████████████████████████████████████████████████████▏                        | 489/633 [00:00<00:00, 659.52it/s]
 88%|███████████████████████████████████████████████████████████████████████████████████████████████▋             | 556/633 [00:00<00:00, 644.82it/s]
 99%|███████████████████████████████████████████████████████████████████████████████████████████████████████████▉ | 627/633 [00:00<00:00, 661.25it/s]
Models:  90%|██████████████████████████████████████████████████████████████████████████████████████████████▌          | 9/10 [00:10<00:01,  1.11s/it]
  0%|                                                                                                                        | 0/633 [00:00<?, ?it/s]
 10%|███████████▍                                                                                                  | 66/633 [00:00<00:00, 652.64it/s]
 21%|██████████████████████▋                                                                                      | 132/633 [00:00<00:00, 613.55it/s]
 31%|█████████████████████████████████▊                                                                           | 196/633 [00:00<00:00, 621.98it/s]
 41%|████████████████████████████████████████████▊                                                                | 260/633 [00:00<00:00, 626.47it/s]
 51%|███████████████████████████████████████████████████████▌                                                     | 323/633 [00:00<00:00, 610.95it/s]
 61%|██████████████████████████████████████████████████████████████████▊                                          | 388/633 [00:00<00:00, 623.21it/s]
 71%|█████████████████████████████████████████████████████████████████████████████▋                               | 451/633 [00:00<00:00, 622.48it/s]
 81%|████████████████████████████████████████████████████████████████████████████████████████▌                    | 514/633 [00:00<00:00, 603.46it/s]
 92%|████████████████████████████████████████████████████████████████████████████████████████████████████▋        | 585/633 [00:00<00:00, 633.03it/s]
Models: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████| 10/10 [00:11<00:00,  1.12s/it]
INFO:root:Finished fitting 10 models to 633 genes
--- 12.735 seconds ---
iter 0, ELBO: -1.84e+08
iter 1, ELBO: -1.51e+08, delta_ELBO: 3.29e+07
iter 2, ELBO: -1.51e+08, delta_ELBO: 8.31e+04
iter 3, ELBO: -1.51e+08, delta_ELBO: 9.37e+03
iter 4, ELBO: -1.51e+08, delta_ELBO: 2.11e+03
iter 5, ELBO: -1.51e+08, delta_ELBO: 1.68e+03
iter 6, ELBO: -1.51e+08, delta_ELBO: 2.29e+02
iter 7, ELBO: -1.51e+08, delta_ELBO: 1.75e+03
iter 8, ELBO: -1.51e+08, delta_ELBO: 8.58e+02
iter 9, ELBO: -1.51e+08, delta_ELBO: 3.24e+02
iter 10, ELBO: -1.51e+08, delta_ELBO: 6.63e+01
iter 11, ELBO: -1.51e+08, delta_ELBO: 2.35e+02
iter 12, ELBO: -1.51e+08, delta_ELBO: 1.75e+02
iter 13, ELBO: -1.51e+08, delta_ELBO: 9.05e+01
iter 14, ELBO: -1.51e+08, delta_ELBO: 2.06e+01
iter 15, ELBO: -1.51e+08, delta_ELBO: 2.72e-01
iter 16, ELBO: -1.51e+08, delta_ELBO: 8.06e+00
iter 17, ELBO: -1.51e+08, delta_ELBO: 3.23e+00
iter 18, ELBO: -1.51e+08, delta_ELBO: 1.46e+01
iter 19, ELBO: -1.51e+08, delta_ELBO: 1.76e+01
iter 20, ELBO: -1.51e+08, delta_ELBO: 5.17e+01
iter 21, ELBO: -1.51e+08, delta_ELBO: 3.02e-02
iter 22, ELBO: -1.51e+08, delta_ELBO: 2.16e-01
iter 23, ELBO: -1.51e+08, delta_ELBO: 1.79e-01
iter 24, ELBO: -1.51e+08, delta_ELBO: 2.90e-01
iter 25, ELBO: -1.51e+08, delta_ELBO: 8.70e-01
iter 26, ELBO: -1.51e+08, delta_ELBO: 4.47e-01
iter 27, ELBO: -1.51e+08, delta_ELBO: 6.30e-01
iter 28, ELBO: -1.51e+08, delta_ELBO: 1.88e+00
iter 29, ELBO: -1.51e+08, delta_ELBO: 1.30e+00
iter 30, ELBO: -1.51e+08, delta_ELBO: 2.27e+00
iter 31, ELBO: -1.51e+08, delta_ELBO: 1.92e+00
iter 32, ELBO: -1.51e+08, delta_ELBO: 3.24e-01
iter 33, ELBO: -1.51e+08, delta_ELBO: 3.23e+00
iter 34, ELBO: -1.51e+08, delta_ELBO: 2.91e+00
iter 35, ELBO: -1.51e+08, delta_ELBO: 2.15e-01
iter 36, ELBO: -1.51e+08, delta_ELBO: 5.58e-01
iter 37, ELBO: -1.51e+08, delta_ELBO: 9.94e-01
iter 38, ELBO: -1.51e+08, delta_ELBO: 1.81e+00
iter 39, ELBO: -1.51e+08, delta_ELBO: 8.35e-01
iter 40, ELBO: -1.51e+08, delta_ELBO: 7.74e-01
iter 41, ELBO: -1.51e+08, delta_ELBO: 1.88e+00
iter 42, ELBO: -1.51e+08, delta_ELBO: 3.70e-02
iter 43, ELBO: -1.51e+08, delta_ELBO: 1.12e-03
Converged on iter 43
--- 137.249 seconds ---
[52]:
fst.spatialDE_clusters(histology_results, patterns, adata_impt_sc.obsm['spatial'], w=3, s=10,
                       figsize=(16,2.2), trans=True, format='svg', marker='o',
                       save_path='NPC1_mel_DE_clusters_singleT.svg')
_images/Crop_ROI_Boundary_image_46_0.png
[54]:
histology_results.to_csv('NPC1_histology_results_sc_ROI1.csv', index=False)
patterns.to_csv('NPC1_patterns_sc_ROI1.csv', index=False)

1.3 L-R-TF-TG analysis

[7]:
import os
import pandas as pd
os.chdir(str(path)+'FineST/FineST_local/Dataset/NPC/CropROI/')
histology_results = pd.read_csv('NPC1_histology_results_sc_ROI1.csv')
[8]:
histology_results
[8]:
g pattern membership
0 FGF7_FGFR1 1 1.0
1 CDH4_CDH4 1 1.0
2 EFNA1_EPHA3 2 1.0
3 CLDN11_CLDN11 1 1.0
4 CD274_PDCD1 1 1.0
... ... ... ...
628 CCL8_ACKR4 1 1.0
629 CCL21_ACKR4 1 1.0
630 BMP4_BMPR1B_ACVR2A 1 1.0
631 BMP4_BMPR1A_ACVR2A 1 1.0
632 INHBABB_ACVR1C_ACVR2A 1 1.0

633 rows × 3 columns

[18]:
path = '/mnt/lingyu/nfs_share2/Python/'
os.chdir(str(path) + 'FineST/FineST/')
[19]:
Receptor2TF = fst.extract_tf(species='human')
Receptor2TF
[19]:
receptor pathway tf tf_PPR category
0 PKM Glycolysis / Gluconeogenesis ENO1 0.042934 Metabolism
1 ALDOA Glycolysis / Gluconeogenesis ENO1 0.009696 Metabolism
2 GPI Glycolysis / Gluconeogenesis ENO1 0.000961 Metabolism
3 MINPP1 Glycolysis / Gluconeogenesis ENO1 0.066090 Metabolism
4 NPR2 Purine metabolism NME2 0.022436 Metabolism
... ... ... ... ... ...
93601 PLXNA1,TREM2,TYROBP Nervous system development PSMD9 0.000153 Developmental Biology
93602 PLXNA1,TREM2,TYROBP Nervous system development SOS1 0.002213 Developmental Biology
93603 PLXNA1,TREM2,TYROBP Nervous system development SOS2 0.000206 Developmental Biology
93604 PLXNA1,TREM2,TYROBP Nervous system development SRC 0.009406 Developmental Biology
93605 PLXNA1,TREM2,TYROBP Nervous system development UPF2 0.000044 Developmental Biology

93606 rows × 5 columns

[20]:
RegNetwork = pd.read_csv(str(path)+'FineST/FineST/FineST/datasets/RegNetwork/Regnetwork_hum.csv')
RegNetwork.columns = ['tf', 'target']
RegNetwork
[20]:
tf target
0 ZBTB33 WASH8P
1 ZBTB33 CHAF1A
2 ZBTB33 MTRNR2L1
3 ZBTB33 MTRNR2L2
4 ZBTB33 MTRNR2L8
... ... ...
192067 ZNF76 CDKN1A
192068 ZNF76 PCYT1A
192069 ZNF76 TALDO1
192070 ZNRD1 ABCB1
192071 ZNRD1 BCL2

192072 rows × 2 columns

[37]:
tmp = fst.pattern_LR2TF2TG(histology_results, pattern_num=2, R_TFdatabase=Receptor2TF, TF_TGdatabase=RegNetwork)
tmp
This pattern contain %s unique ligand 89
This pattern contain %s unique receptor 94
This pattern contain %s unique tf 307
[37]:
Ligand Receptor tf Target value
0 CD80 CD274 BATF MSC-AS1 0.020307
1 EBI3 IL6ST BATF MSC-AS1 0.000288
2 HBEGF EGFR BATF MSC-AS1 0.007423
4 IL11 IL11RA BATF MSC-AS1 0.000356
5 IL11 IL6ST BATF MSC-AS1 0.000288
... ... ... ... ... ...
15226945 VTN ITGB3 ZNF383 JUN 0.010592
15226946 TNC ITGB3 ZNF383 JUN 0.010592
15226947 SPP1 ITGA5 ZNF383 JUN 0.010592
15226948 IGF1 ITGB3 ZNF383 JUN 0.010592
15226949 SPP1 ITGB3 ZNF383 JUN 0.010592

9870019 rows × 5 columns

[38]:
## order according to 'value'
tmp = tmp.sort_values(by="value", ascending=False)
## statistic value classes
num_classes = tmp['value'].nunique()
print('Number of unique classes: ', num_classes)

selected_rows = []
num_sele = 1
for value, group in tmp.groupby('value'):
    selected_rows.append(group.head(num_sele))

tmp_sele = pd.concat(selected_rows)
tmp_sele = tmp_sele.sort_values(by="value", ascending=False)
print('Length of tmp_sele:\n', len(tmp_sele))
print(tmp_sele)
Number of unique classes:  1398
Length of tmp_sele:
 1398
          Ligand Receptor      tf Target     value
11991626   TGFB2   TGFBR2   SMAD3   BTRC  0.459459
12018766   TGFB2   TGFBR2   SMAD3  PCDH1  0.459459
11977187   TGFB2   TGFBR2   SMAD4   TFE3  0.390541
11971408   TGFB2   TGFBR2   SMAD4  RPL28  0.390541
14434846  LRRC4B    PTPRF  CTNNB1  PTPRU  0.285677
...          ...      ...     ...    ...       ...
14787206   LAMB1    ITGB1    ABL1   CSF1  0.000002
14766249   WNT3A     FZD2  RUVBL1    PLG  0.000002
15225500   CNTN2    L1CAM    UPF2   UPF1  0.000001
15225504  COL9A2    ITGA9    UPF2   UPF1  0.000001
15225509  COL1A2    ITGB1    UPF2   UPF1  0.000001

[1398 rows x 5 columns]
[39]:
num_top = 20
tmp_sele_top = tmp_sele[:num_top]
tmp_sele_top
[39]:
Ligand Receptor tf Target value
11991626 TGFB2 TGFBR2 SMAD3 BTRC 0.459459
12018766 TGFB2 TGFBR2 SMAD3 PCDH1 0.459459
11977187 TGFB2 TGFBR2 SMAD4 TFE3 0.390541
11971408 TGFB2 TGFBR2 SMAD4 RPL28 0.390541
14434846 LRRC4B PTPRF CTNNB1 PTPRU 0.285677
15085114 JAG2 NOTCH3 RBPJ NFKB1 0.283606
10525370 IL11 IL6ST STAT3 HLA-DRB1 0.228232
13441976 BMP8A ACVR2B SMAD2 OS9 0.221048
11974051 GDF7 ACVR2A SMAD4 SMAD9 0.214991
12032964 TGFB2 TGFBR2 SMAD3 TGFBR1 0.183767
14842858 PTPRM PTPRM CTNNB1 PTGS2 0.180933
14759224 HBEGF EGFR RB1 TFDP1 0.166107
11962335 BMP8A BMPR1A SMAD4 MYOD1 0.162637
13588540 HBEGF EGFR MAPK1 CDK4 0.157417
10636560 BMP7 BMPR1A NANOG TBX15 0.153488
14302779 HBEGF ERBB2 CTNNB1 BTRC 0.147881
12023986 TGFB2 TGFBR2 SMAD3 RIT1 0.147526
13587741 HBEGF EGFR MAPK1 CXCL8 0.146250
356658 HBEGF EGFR E2F1 GPR158 0.143775
13452366 TGFB2 TGFBR2 SMAD2 SMAD4 0.140927
[40]:
## select some inmortant ligands and receptors
ligand_list = {'MIF', 'CXCL16', 'PVR',  'EFNA5', 'ICAM2', 'JAM3', 'EFNA5', 'L1CAM', 'JAM2', 'LCK', 'GP1BA'}
receptor_list = {'ACKR3', 'CXCR6', 'TIGIT',  'CD8B1', 'EPHA3', 'ITGAL', 'ITGB2', 'JAM2', 'EPHA3', 'ITGA4', 'CD8A', 'ITGAM'}
subdf = fst.top_pattern_LR2TF(tmp, ligand_list, receptor_list, top_num=16)
subdf
Ligand and Receptor in R2TFdatabase: 6913
[40]:
Ligand_symbol Receptor_symbol TF Target value
10506211 CXCL16 CXCR6 STAT2 LIF 0.03535
10572744 CXCL16 CXCR6 STAT3 A2M 0.03535
10387783 CXCL16 CXCR6 STAT1 CSF1 0.03535
10488823 CXCL16 CXCR6 STAT1 PPP2R1B 0.03535
10396263 CXCL16 CXCR6 STAT1 PLCG2 0.03535
10508722 CXCL16 CXCR6 STAT2 GFRA1 0.03535
10437463 CXCL16 CXCR6 STAT1 NPTX2 0.03535
10563999 CXCL16 CXCR6 STAT3 FAM216B 0.03535
10528701 CXCL16 CXCR6 STAT3 MBNL1 0.03535
10528807 CXCL16 CXCR6 STAT3 ASCL1 0.03535
10514953 CXCL16 CXCR6 STAT2 ADAMTS1 0.03535
10528966 CXCL16 CXCR6 STAT3 MYO1B 0.03535
10490343 CXCL16 CXCR6 STAT1 UBE2I 0.03535
10508691 CXCL16 CXCR6 STAT2 GLTPD2 0.03535
10493036 CXCL16 CXCR6 STAT2 BST2 0.03535
10441383 CXCL16 CXCR6 STAT1 MIR5187 0.03535
[42]:
fstplt.sankey_LR2TF2TG(subdf, width=550, height=600, title='Pattern 2')
[ ]:
fstplt.sankey_LR2TF2TG(subdf, width=550, height=600, title='Pattern 2',
                    save_path=str(path)+'FineST/FineST_local/Dataset/NPC/CropROI/LRTR_ROI1_pattern2.svg', fig_format='svg')

1.4 Pathway enrichment

[63]:
dic=dict()
for i in histology_results.sort_values('pattern').pattern.unique():
    dic['Pattern_{}'.format(i)]=histology_results.query('pattern == @i').sort_values('membership')['g'].values
[64]:
## run code
(result,
 pathway_res,
 result_select,
 result_pattern_all) = fst.pathway_analysis(sample=adata_impt_sc,
                                            all_interactions=None,
                                            interaction_ls=None,
                                            name=None,
                                            groups=["Pattern_0"],
                                            cut_off=1,
                                            dic=dic)
[65]:
print(result.shape)
(438, 5)
[67]:
print(result_select.shape)
result_select.head()
(23, 5)
[67]:
fisher_p pathway_size selected selected_inters name
1
LAMININ 1.765761e-13 143 50 {LAMC1_ITGA7_ITGB1, LAMA3_ITGA3_ITGB1, LAMC1_C... Pattern_0
WNT 1.197436e-01 160 26 {WNT2_FZD6_LRP5, WNT4_FZD1_LRP5, WNT10B_FZD9_L... Pattern_0
COLLAGEN 5.012397e-02 120 22 {COL1A1_ITGA3_ITGB1, COL4A5_ITGA10_ITGB1, COL4... Pattern_0
THBS 1.066749e-02 30 9 {COMP_SDC4, THBS3_ITGAV_ITGB3, THBS3_SDC4, COM... Pattern_0
FN1 5.899368e-02 12 4 {FN1_ITGAV_ITGB1, FN1_ITGA5_ITGB1, FN1_ITGAV_I... Pattern_0
[68]:
# fst.dot_path(adata_impt_sc, dic=dic,  cut_off=3, step=8, figsize=(4,15))
[73]:
# figsize=(7,20) for num_cutoff=1, p_cutoff=None,
Pattern = 'Pattern_0'
fstplt.dot_path(adata_impt_sc, dic=dic, num_cutoff=2, p_cutoff=0.5, groups=[str(Pattern)], step=6, figsize=(3,6))
_images/Crop_ROI_Boundary_image_66_0.png
[74]:
# fstplt.dot_path(adata_impt_sc, dic=dic, num_cutoff=2, p_cutoff=0.5, groups=[str(Pattern)], step=6, figsize=(3,6),
#              pdf=f"{path}/FineST/FineST_local/Dataset/NPC/CropROI/mel_DE_enrichment_{Pattern}_ROI1")
_images/Crop_ROI_Boundary_image_67_0.png
[77]:
Pattern = 'Pattern_1'
fstplt.dot_path(adata_impt_sc, dic=dic, num_cutoff=2, p_cutoff=0.5, groups=[str(Pattern)], step=6, figsize=(3,11))
_images/Crop_ROI_Boundary_image_68_0.png
[79]:
# fstplt.dot_path(adata_impt_sc, dic=dic, num_cutoff=2, p_cutoff=0.5, groups=[str(Pattern)], step=6, figsize=(3,11),
#                pdf=f"{path}/FineST/FineST_local/Dataset/NPC/CropROI/mel_DE_enrichment_{Pattern}_ROI1")
[83]:
Pattern = 'Pattern_2'
fstplt.dot_path(adata_impt_sc, dic=dic, num_cutoff=2, p_cutoff=0.5, groups=[str(Pattern)], step=6, figsize=(3,7))
_images/Crop_ROI_Boundary_image_70_0.png
[85]:
# fstplt.dot_path(adata_impt_sc, dic=dic, num_cutoff=2, p_cutoff=0.5, groups=[str(Pattern)], step=6, figsize=(3,7),
#                 pdf=f"{path}/FineST/FineST_local/Dataset/NPC/CropROI/mel_DE_enrichment_{Pattern}_ROI1")
[86]:
## chord diagrams to visualize the aggregated cell types (or interactions)
adata_impt_sc.obsm['celltypes'] = adata_impt_sc.obsm['TransImp_ct_pred']
pl.chord_celltype(adata_impt_sc, pairs=['MIF_ACKR3'], ncol=1, min_quantile=0.01)
[86]:
GridPlot(
id = 'p1165', …)

2. Bounary image from NPC Visium dataset

2.1 Crop image with correspondng adata

The 10x Visium dataset (NPC_patient_1) from Gong, et al can be downloaded from NPC1 in Goole Drive, where ROI1.csv is another ROI in paper.

Input
- roi_path: the pathway of the selected region using napari package.
- img_path: the original .tif HE image with high-resolution, it is about 800 MB here.
- adata_path: the .h5ad adata corresponding to the HE image, from 10x Visium dataset.
Output
- crop_img_path: the cropped image, saved in .tif format.
- crop_adata_path: the saved ST data, selected the .obsm['spatial'] matched cropped image.
[13]:
os.chdir(str(path))
roi_path = './FineST/FineST_local/Dataset/NPC/CropROI/Annodata/Boundary1.csv'
img_path = './NPC/Data/stdata/GSE200310_RAW/patient1/20210809-C-AH4199551.tif'
adata_path = './FineST/FineST_local/Dataset/NPC/TransImp/patient1_nuclei_anno_TransImp_NPC1_sc.h5ad'
crop_img_path = './FineST/FineST_local/Dataset/NPC/CropRec/NPC1_cropped_ROI_image.tif'
crop_adata_path = './FineST/FineST_local/Dataset/NPC/CropRec/NPC1_sc_ROI.h5ad'
[14]:
cropped_img, adata_roi = fst.crop_img_adata(roi_path,
                                            img_path, adata_path,
                                            crop_img_path, crop_adata_path,
                                            segment=False, save=True)
ROI coordinates from napari package:
    index shape-type  vertex-index        axis-0       axis-1
0      3    polygon             0   6640.434693  7203.335263
1      3    polygon             1  10347.593830  5767.797045
2      3    polygon             2  10994.374780  6635.430034
3      3    polygon             3   6750.860710  7629.264185
img shape:
 (17351, 17319, 3)
polygon:
 [[ 6640.434693  7203.335263]
 [10347.59383   5767.797045]
 [10994.37478   6635.430034]
 [ 6750.86071   7629.264185]]
polygon adjusted:
 [[ 6640.434693  7203.335263]
 [10347.59383   5767.797045]
 [10994.37478   6635.430034]
 [ 6750.86071   7629.264185]]
cropped_img shape:
 (4354, 1861, 3)
The adata:
 AnnData object with n_obs × n_vars = 40068 × 596
    obs: 'x', 'y', 'cell_type'
    uns: 'spatial'
    obsm: 'TransImp_ct_pred', 'X_pca', 'spatial'
    varm: 'PCs'
The range of original adata:
 [[1791, 13306], [1357, 11398]]

Show image shape and adata property

[15]:
print(cropped_img.shape)
print(adata_roi)
(4354, 1861, 3)
AnnData object with n_obs × n_vars = 2672 × 596
    obs: 'x', 'y', 'cell_type'
    uns: 'spatial'
    obsm: 'TransImp_ct_pred', 'X_pca', 'spatial'
    varm: 'PCs'

See the range of the spatial coordinats of the 1st colnum and 2nd colnum in adata.obsm['spatial']

[16]:
print(adata_roi.obsm["spatial"][:,0].min(), adata_roi.obsm["spatial"][:,0].max())
print(adata_roi.obsm["spatial"][:,1].min(), adata_roi.obsm["spatial"][:,1].max())
5784 7612
6680 10971

Plot the crooped ROI region in original HE image with high-resolution

[17]:
plt.imshow(cropped_img)
plt.show()
_images/Crop_ROI_Boundary_image_84_0.png

See the gene expression saved in adata.X, only contains the samples that belongs to the cropped ROI image

[18]:
adata_roi.to_df()
[18]:
TGFB1 TGFBR1 TGFBR2 TGFB2 TGFB3 ACVR1B ACVR1C ACVR1 BMP2 BMPR1A ... KDR TREM2 SEMA6A SEMA6B SEMA7A PLXNC1 SIGLEC1 THY1 VCAM1 VSIR
8041 0.302604 0.109524 0.148009 0.053230 0.064085 0.181022 0.002292 0.075156 0.067228 0.136538 ... 0.003583 0.067403 0.004730 0.003778 0.179127 0.108147 0.004352 0.106178 0.435501 0.483437
8049 0.318536 0.118025 0.124202 0.035704 0.119441 0.122999 0.003493 0.066417 0.065475 0.102506 ... 0.012929 0.065112 0.004817 0.004524 0.182994 0.102539 0.004472 0.157304 0.417952 0.439255
8097 0.273096 0.076893 0.136564 0.025154 0.089150 0.104589 0.003453 0.035427 0.085269 0.076331 ... 0.004822 0.075949 0.012939 0.004475 0.145200 0.113573 0.004415 0.138573 0.393523 0.390203
8207 0.311711 0.102568 0.166698 0.004814 0.093933 0.080958 0.004147 0.027886 0.154552 0.066274 ... 0.004888 0.102577 0.017543 0.004252 0.175728 0.141577 0.004340 0.173253 0.454644 0.425937
8282 0.383476 0.095612 0.203807 0.063937 0.091198 0.169869 0.002620 0.094756 0.120204 0.164543 ... 0.004221 0.146890 0.004259 0.004619 0.166209 0.159947 0.004756 0.109616 0.377819 0.553325
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
24271 0.662846 0.514540 0.032835 0.040933 0.075112 0.115587 0.003582 0.064994 0.004516 0.068413 ... 0.004908 0.004416 0.025440 0.004530 0.550846 0.024451 0.004471 0.932140 1.604519 1.041233
24329 0.624141 0.499691 0.035832 0.004959 0.085457 0.086626 0.004089 0.077279 0.004925 0.060645 ... 0.006636 0.004956 0.030244 0.004753 0.527468 0.018320 0.004683 0.967321 1.613216 1.025589
24366 0.625403 0.489985 0.033611 0.021726 0.086315 0.113832 0.003293 0.064736 0.033775 0.068268 ... 0.004914 0.004631 0.027921 0.004390 0.525987 0.019299 0.004469 0.964156 1.655212 1.034990
24389 0.678020 0.522608 0.040336 0.004818 0.114615 0.102922 0.004322 0.076132 0.039875 0.071850 ... 0.005973 0.004393 0.034565 0.004430 0.575876 0.025741 0.004205 0.978740 1.691294 1.036511
24548 0.606016 0.406864 0.078743 0.028539 0.110128 0.127418 0.003527 0.110582 0.004551 0.076842 ... 0.004825 0.015694 0.052163 0.004720 0.447906 0.032973 0.004578 0.824053 1.477615 0.903713

2672 rows × 596 columns

The NPC1 adata visium_nuclei_annotated_TransImp_NPC1.h5ad is obtained from nuclei-segmentaion and cell type annotation, so we can show the cells within this ROI. For others ones can only show the gene expression situation.

[21]:
import matplotlib.pyplot as plt
ctype_hex_map = {'B': '#565DFD',
 'normal': '#4fa9ff',
 'Treg': '#FE664D',
 'fibroblast': '#9f50f9',
 'Myeloid': '#009203',
 'tumor': '#e5e022',
 'T': '#CB6035'}

fig, ax = plt.subplots(1, 1, dpi=100)
sc.pl.spatial(adata_roi, img_key='hires', color="cell_type", title='TransImp',
              size=0.25, alpha_img=0.8, palette=ctype_hex_map, ax=ax)
_images/Crop_ROI_Boundary_image_88_0.png
[23]:
os.chdir(f"{path}/FineST/FineST_local/Dataset/NPC/CropRec/")
!pwd
/mnt/lingyu/nfs_share2/Python/FineST/FineST_local/Dataset/NPC/CropRec
[25]:
fstplt.gene_expr(adata_roi, adata_roi.to_df(), gene_selet='CD70', marker='o',
                 s=2, figsize=(2.8, 5), save_path='CD70_expr_Bond1.pdf')
fstplt.gene_expr(adata_roi, adata_roi.to_df(), gene_selet='CD27', marker='o',
                 s=2, figsize=(2.8, 5), save_path='CD27_expr_Bond1.pdf')
_images/Crop_ROI_Boundary_image_90_0.png
_images/Crop_ROI_Boundary_image_90_1.png

2.2 CCC analysis

[26]:
import spatialdm as sdm
import spatialdm.plottings as pl
import matplotlib.pyplot as plt
print("SpatailDM version: %s" %sdm.__version__)
SpatailDM version: 0.2.0
[27]:
adata_impt_sc = adata_roi
[28]:
visium_scale_factors = fst.json_load(f"{path}/FineST/FineST_local/Dataset/NPC/patient1/")
print(visium_scale_factors['spot_diameter_fullres'])
139.44595843130838
[29]:
# spot_diameter_fullres = visium_scale_factors["spot_diameter_fullres"]
spot_diameter_fullres = 112
fst.weight_matrix(adata_impt_sc, l = spot_diameter_fullres,
                  cutoff = 0.001, single_cell = True, n_nearest_neighbors=6)
[29]:
AnnData object with n_obs × n_vars = 2672 × 596
    obs: 'x', 'y', 'cell_type'
    uns: 'spatial', 'cell_type_colors', 'single_cell'
    obsm: 'TransImp_ct_pred', 'X_pca', 'spatial'
    varm: 'PCs'
    obsp: 'weight', 'nearest_neighbors'
[33]:
## Visualize the range of interaction
plt.figure(figsize=(2.8, 5))
plt.scatter(adata_impt_sc.obsm['spatial'][:,0], adata_impt_sc.obsm['spatial'][:,1],
            c=adata_impt_sc.obsp['weight'].A[500], s=2.0)
plt.gca().invert_yaxis()
plt.tick_params(axis='both', which='both', bottom=True, left=True, labelbottom=True)
_images/Crop_ROI_Boundary_image_96_0.png
[34]:
## find overlapping LRs from CellChatDB
start = time.time()
sdm.extract_lr(adata_impt_sc, 'human', min_cell=3)
print("%.3f seconds" %(time.time()-start))
print(adata_impt_sc)
23.248 seconds
AnnData object with n_obs × n_vars = 2672 × 596
    obs: 'x', 'y', 'cell_type'
    uns: 'spatial', 'cell_type_colors', 'single_cell', 'mean', 'ligand', 'receptor', 'num_pairs', 'geneInter'
    obsm: 'TransImp_ct_pred', 'X_pca', 'spatial'
    varm: 'PCs'
    obsp: 'weight', 'nearest_neighbors'
[35]:
## see the condidiate LR pairs for this NPC datset
adata_impt_sc.uns['geneInter']
[35]:
interaction_name pathway_name agonist antagonist co_A_receptor co_I_receptor evidence annotation interaction_name_2
EFNA5_EPHB2 EFNA5_EPHB2 EPHA NaN NaN NaN NaN PMID:15107857; PMID: 15114347 Cell-Cell Contact EFNA5 - EPHB2
EFNB1_EPHA4 EFNB1_EPHA4 EPHB NaN NaN NaN NaN PMID: 15114347 Cell-Cell Contact EFNB1 - EPHA4
EFNB1_EPHB1 EFNB1_EPHB1 EPHB NaN NaN NaN NaN PMID: 15114347 Cell-Cell Contact EFNB1 - EPHB1
EFNB1_EPHB2 EFNB1_EPHB2 EPHB NaN NaN NaN NaN PMID: 15114347 Cell-Cell Contact EFNB1 - EPHB2
EFNB1_EPHB3 EFNB1_EPHB3 EPHB NaN NaN NaN NaN PMID: 15114347 Cell-Cell Contact EFNB1 - EPHB3
... ... ... ... ... ... ... ... ... ...
CCL3_CCR5 CCL3_CCR5 CCL NaN NaN NaN NaN KEGG: hsa04060 Secreted Signaling CCL3 - CCR5
CCL5_CCR5 CCL5_CCR5 CCL NaN NaN NaN NaN KEGG: hsa04060 Secreted Signaling CCL5 - CCR5
CCL4_CCR5 CCL4_CCR5 CCL NaN NaN NaN NaN KEGG: hsa04060 Secreted Signaling CCL4 - CCR5
CXCL1_ACKR1 CXCL1_ACKR1 CXCL NaN NaN NaN NaN PMID: 26740381 Secreted Signaling CXCL1 - ACKR1
CORT_SSTR2 CORT_SSTR2 SEMATOSTATIN NaN NaN NaN NaN KEGG: hsa04080 Secreted Signaling CORT - SSTR2

1129 rows × 9 columns

[36]:
## Identify dataset-specific interacting LR pairs (global selection)
start = time.time()

# global Moran selection
sdm.spatialdm_global(adata_impt_sc, n_perm=1000, specified_ind=None, method='z-score', nproc=1)
# select significant pairs
sdm.sig_pairs(adata_impt_sc, method='z-score', fdr=True, threshold=0.05)

print("%.3f seconds" %(time.time()-start))
print(adata_impt_sc)
3.310 seconds
AnnData object with n_obs × n_vars = 2672 × 596
    obs: 'x', 'y', 'cell_type'
    uns: 'spatial', 'cell_type_colors', 'single_cell', 'mean', 'ligand', 'receptor', 'num_pairs', 'geneInter', 'global_I', 'global_stat', 'global_res'
    obsm: 'TransImp_ct_pred', 'X_pca', 'spatial'
    varm: 'PCs'
    obsp: 'weight', 'nearest_neighbors'
[37]:
spa_coexp_pair_sc = fst.anno_LRpair(adata_impt_sc)
spa_coexp_pair_sc
[37]:
Ligand0 Ligand1 Receptor0 Receptor1 Receptor2 z_pval z fdr selected evidence annotation
SEMA3B_NRP1_PLXNA1 SEMA3B None NRP1 PLXNA1 None 0.000000 43.233954 0.0 True PMID: 27533782 Secreted Signaling
WNT5B_FZD1 WNT5B None FZD1 None None 0.000000 50.186704 0.0 True KEGG: hsa04310 Secreted Signaling
WNT5B_FZD4 WNT5B None FZD4 None None 0.000000 78.985347 0.0 True KEGG: hsa04310 Secreted Signaling
WNT5B_FZD5 WNT5B None FZD5 None None 0.000000 160.524778 0.0 True KEGG: hsa04310 Secreted Signaling
WNT5B_FZD6 WNT5B None FZD6 None None 0.000000 120.199832 0.0 True KEGG: hsa04310 Secreted Signaling
... ... ... ... ... ... ... ... ... ... ... ...
WNT2_FZD7_LRP6 WNT2 None FZD7 LRP6 None 1.000000 -22.204888 1.0 False KEGG: hsa04310; PMID: 23209147 Secreted Signaling
WNT2_FZD5_LRP6 WNT2 None FZD5 LRP6 None 1.000000 -28.739469 1.0 False KEGG: hsa04310; PMID: 23209147 Secreted Signaling
WNT2_FZD4_LRP6 WNT2 None FZD4 LRP6 None 1.000000 -33.850176 1.0 False KEGG: hsa04310; PMID: 23209147 Secreted Signaling
WNT1_FZD5_LRP6 WNT1 None FZD5 LRP6 None 0.955122 -1.696686 1.0 False KEGG: hsa04310; PMID: 23209147 Secreted Signaling
EFNA5_EPHB2 EFNA5 None EPHB2 None None 1.000000 -7.388503 1.0 False PMID:15107857; PMID: 15114347 Cell-Cell Contact

1129 rows × 11 columns

[38]:
!pwd
/mnt/lingyu/nfs_share2/Python/FineST/FineST_local/Dataset/NPC/CropRec
[39]:
## see the unique gene of sig LR pairs
spa_coexp_pairTgene_sc = fst.LRpair_gene(spa_coexp_pair_sc)
print("spa_coexp_pairTgene_sc shape:", len(spa_coexp_pairTgene_sc))
spa_coexp_pairTgene_sc shape: 426
[40]:
# # save 798 significant LR pairs
# spa_coexp_pair_sc.to_csv("spa_coexp_pair_sc_Bound1_singleT.csv", index=True, header=True)
# spa_coexp_pairTgene_sc.to_csv("spa_coexp_LRgene_sc_Bound1_singleT.csv", index=True, header=True)
[41]:
## use fdr corrected global p-values and a threshold FDR < 0.1 (default)
print(adata_impt_sc.uns['global_res'].selected.sum())
adata_impt_sc.uns['global_res'].sort_values(by='fdr')
677
[41]:
Ligand0 Ligand1 Receptor0 Receptor1 Receptor2 z_pval z fdr selected
SEMA3B_NRP1_PLXNA1 SEMA3B None NRP1 PLXNA1 None 0.000000 43.233954 0.0 True
WNT5B_FZD1 WNT5B None FZD1 None None 0.000000 50.186704 0.0 True
WNT5B_FZD4 WNT5B None FZD4 None None 0.000000 78.985347 0.0 True
WNT5B_FZD5 WNT5B None FZD5 None None 0.000000 160.524778 0.0 True
WNT5B_FZD6 WNT5B None FZD6 None None 0.000000 120.199832 0.0 True
... ... ... ... ... ... ... ... ... ...
WNT2_FZD7_LRP6 WNT2 None FZD7 LRP6 None 1.000000 -22.204888 1.0 False
WNT2_FZD5_LRP6 WNT2 None FZD5 LRP6 None 1.000000 -28.739469 1.0 False
WNT2_FZD4_LRP6 WNT2 None FZD4 LRP6 None 1.000000 -33.850176 1.0 False
WNT1_FZD5_LRP6 WNT1 None FZD5 LRP6 None 0.955122 -1.696686 1.0 False
EFNA5_EPHB2 EFNA5 None EPHB2 None None 1.000000 -7.388503 1.0 False

1129 rows × 9 columns

[42]:
## Local selection is then run for the selected 787 pairs to identify where the LRI takes place
adata_impt_sc.raw = adata_impt_sc

start = time.time()

# local spot selection
sdm.spatialdm_local(adata_impt_sc, n_perm=1000, method='z-score', specified_ind=None, nproc=1)
# significant local spots
sdm.sig_spots(adata_impt_sc, method='z-score', fdr=False, threshold=0.05)

print("%.3f seconds" %(time.time()-start))
2.617 seconds
[43]:
print(adata_impt_sc)
AnnData object with n_obs × n_vars = 2672 × 596
    obs: 'x', 'y', 'cell_type'
    uns: 'spatial', 'cell_type_colors', 'single_cell', 'mean', 'ligand', 'receptor', 'num_pairs', 'geneInter', 'global_I', 'global_stat', 'global_res', 'local_stat', 'local_z', 'local_z_p', 'selected_spots'
    obsm: 'TransImp_ct_pred', 'X_pca', 'spatial'
    varm: 'PCs'
    obsp: 'weight', 'nearest_neighbors'
[44]:
adata_impt_sc.uns["local_z_p"]
[44]:
8041 8049 8097 8207 8282 8354 8374 8419 8434 8469 ... 24138 24144 24148 24191 24267 24271 24329 24366 24389 24548
EFNB1_EPHA4 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 ... 0.752921 0.508404 0.825745 0.926070 0.529031 0.878696 0.875564 0.892854 0.859717 0.846208
EFNB1_EPHB2 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 ... 0.937062 0.846200 0.902077 0.962316 0.855496 0.954743 0.923138 0.934162 0.942227 0.902452
EFNB1_EPHB4 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 ... 0.303911 0.352038 0.314697 0.055870 0.553391 0.132192 0.206513 0.158968 0.183553 0.207745
EFNB2_EPHB1 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 ... 0.832148 0.989964 0.694063 1.000000 0.954220 0.519933 0.572628 0.543469 0.548614 0.692403
EFNB2_EPHB2 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 ... 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
CCL20_CCR6 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 ... 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000
CCL5_CCR5 1.000000e+00 1.000000e+00 1.000000e+00 9.998560e-01 7.208948e-01 9.359357e-01 1.000000e+00 1.000000e+00 2.461541e-02 5.322498e-12 ... 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000
CCL4_CCR5 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 ... 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000
CXCL1_ACKR1 4.645221e-12 9.530826e-08 5.085867e-08 1.567185e-32 5.384154e-26 2.910531e-164 6.792969e-178 1.611468e-226 5.801459e-155 1.269356e-81 ... 0.963522 1.000000 0.965090 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000
CORT_SSTR2 9.534013e-01 1.000000e+00 1.000000e+00 1.000000e+00 4.723882e-01 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 ... 1.000000 0.994830 1.000000 1.000000 0.998839 1.000000 1.000000 1.000000 1.000000 1.000000

677 rows × 2672 columns

[45]:
fst.topLRpairs(adata_impt_sc, spa_coexp_pair_sc, num=10)
[45]:
['NCAM1_NCAM1',
 'CADM1_CADM1',
 'ESAM_ESAM',
 'JAM2_JAM2',
 'F11R_F11R',
 'PECAM1_PECAM1',
 'CDH2_CDH2',
 'MPZL1_MPZL1',
 'CD99_CD99',
 'CDH1_CDH1']
[50]:
fstplt.global_plot(adata_impt_sc, pairs=['CD70_CD27', 'CXCL16_CXCR6', 'PVR_TIGIT'],
               figsize=(4.0, 4.0), loc=4, cmap='RdGy_r', vmin=-1.5, vmax=2,  max_step=0.2, min_step=0.1)
plt.savefig('NPC1_3pair_sc_Bound1_singleT.svg', transparent=True, dpi=300, bbox_inches='tight')
_images/Crop_ROI_Boundary_image_109_0.png
[72]:
# Example usage
pairs=['CD70_CD27', 'CXCL16_CXCR6', 'PVR_TIGIT']
fst.LR_global_moranR(adata_impt_sc, pairs, fig_size=(3.5, 3), font_size=12,
                 # save_path=None)
                save_path='LRpair_sele_globalR_bond1.svg')
['PVR_TIGIT', 'CD70_CD27', 'CXCL16_CXCR6']
_images/Crop_ROI_Boundary_image_110_1.png
[76]:
# Example usage
# pair = 'CD70_CD27'
# pair='CXCL16_CXCR6'
pair='PVR_TIGIT'
fstplt.LR_local_moranR(adata_impt_sc, pair, fig_size=(4, 2.5),
            # save_path=None)
            save_path=f'{pair}_interaction.svg')
_images/Crop_ROI_Boundary_image_111_0.png
[66]:
fstplt.plot_pairs_dot(adata_impt_sc, ['CXCL16_CXCR6'], trans=True, figsize=(16, 5),
                      marker_size=1.5, pdf="NPC1_sc_pair_Bond1_CXCL16_CXCR6")
fstplt.plot_pairs_dot(adata_impt_sc, ['CD70_CD27'], trans=True, figsize=(16, 5),
                      marker_size=1.5, pdf="NPC1_sc_pair_Bond1_CD70_CD27")
_images/Crop_ROI_Boundary_image_112_0.png
_images/Crop_ROI_Boundary_image_112_1.png
[77]:
# bin_spots = (1-adata_impt_sc.uns["local_z_p"])[adata_impt_sc.uns['local_stat']['n_spots']>2]
bin_spots = adata_impt_sc.uns['selected_spots'].astype(int)[adata_impt_sc.uns['local_stat']['n_spots']>2]
print(bin_spots.shape[0], " pairs used for spatial clustering")
bin_spots=bin_spots.fillna(0)
bin_spots
677  pairs used for spatial clustering
[77]:
8041 8049 8097 8207 8282 8354 8374 8419 8434 8469 ... 24138 24144 24148 24191 24267 24271 24329 24366 24389 24548
EFNB1_EPHA4 0 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0
EFNB1_EPHB2 0 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0
EFNB1_EPHB4 0 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0
EFNB2_EPHB1 0 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0
EFNB2_EPHB2 0 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
CCL20_CCR6 0 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0
CCL5_CCR5 0 0 0 0 0 0 0 0 1 1 ... 0 0 0 0 0 0 0 0 0 0
CCL4_CCR5 0 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0
CXCL1_ACKR1 1 1 1 1 1 1 1 1 1 1 ... 0 0 0 0 0 0 0 0 0 0
CORT_SSTR2 0 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0

677 rows × 2672 columns

[78]:
from threadpoolctl import threadpool_limits
import NaiveDE
import SpatialDE

with threadpool_limits(limits=1, user_api='blas'):
    start = time.time()
    results = SpatialDE.run(adata_impt_sc.obsm['spatial'], bin_spots.transpose())
    print("--- %.3f seconds ---" %(time.time()-start))

    start = time.time()
    histology_results, patterns = SpatialDE.aeh.spatial_patterns(
        adata_impt_sc.obsm['spatial'], bin_spots.transpose(), results, C=3, l=3, verbosity=1)
    print("--- %.3f seconds ---" %(time.time()-start))
INFO:root:Performing DE test
INFO:root:Pre-calculating USU^T = K's ...
INFO:root:Done: 3.2e+01s
INFO:root:Fitting gene models
Models:   0%|                                                                                                                 | 0/10 [00:00<?, ?it/s]
  0%|                                                                                                                        | 0/677 [00:00<?, ?it/s]
  3%|███▋                                                                                                          | 23/677 [00:00<00:02, 224.08it/s]
  7%|███████▋                                                                                                      | 47/677 [00:00<00:02, 229.60it/s]
 10%|███████████▌                                                                                                  | 71/677 [00:00<00:02, 230.49it/s]
 14%|███████████████▍                                                                                              | 95/677 [00:00<00:02, 230.21it/s]
 18%|███████████████████▏                                                                                         | 119/677 [00:00<00:02, 230.01it/s]
 21%|███████████████████████                                                                                      | 143/677 [00:00<00:02, 232.45it/s]
 25%|██████████████████████████▉                                                                                  | 167/677 [00:00<00:02, 232.29it/s]
 28%|██████████████████████████████▊                                                                              | 191/677 [00:00<00:02, 229.64it/s]
 32%|██████████████████████████████████▍                                                                          | 214/677 [00:00<00:02, 229.68it/s]
 35%|██████████████████████████████████████▏                                                                      | 237/677 [00:01<00:01, 228.33it/s]
 38%|█████████████████████████████████████████▊                                                                   | 260/677 [00:01<00:01, 228.70it/s]
 42%|█████████████████████████████████████████████▌                                                               | 283/677 [00:01<00:01, 228.83it/s]
 45%|█████████████████████████████████████████████████▎                                                           | 306/677 [00:01<00:01, 228.17it/s]
 49%|████████████████████████████████████████████████████▉                                                        | 329/677 [00:01<00:01, 227.16it/s]
 52%|████████████████████████████████████████████████████████▉                                                    | 354/677 [00:01<00:01, 232.41it/s]
 56%|█████████████████████████████████████████████████████████████                                                | 379/677 [00:01<00:01, 235.17it/s]
 60%|█████████████████████████████████████████████████████████████████▏                                           | 405/677 [00:01<00:01, 239.97it/s]
 63%|█████████████████████████████████████████████████████████████████████                                        | 429/677 [00:01<00:01, 237.98it/s]
 67%|█████████████████████████████████████████████████████████████████████████                                    | 454/677 [00:01<00:00, 240.47it/s]
 71%|█████████████████████████████████████████████████████████████████████████████                                | 479/677 [00:02<00:00, 237.55it/s]
 74%|████████████████████████████████████████████████████████████████████████████████▉                            | 503/677 [00:02<00:00, 235.37it/s]
 78%|████████████████████████████████████████████████████████████████████████████████████▊                        | 527/677 [00:02<00:00, 236.04it/s]
 81%|████████████████████████████████████████████████████████████████████████████████████████▋                    | 551/677 [00:02<00:00, 236.87it/s]
 85%|████████████████████████████████████████████████████████████████████████████████████████████▌                | 575/677 [00:02<00:00, 236.15it/s]
 88%|████████████████████████████████████████████████████████████████████████████████████████████████▍            | 599/677 [00:02<00:00, 235.47it/s]
 92%|████████████████████████████████████████████████████████████████████████████████████████████████████▎        | 623/677 [00:02<00:00, 233.76it/s]
 96%|████████████████████████████████████████████████████████████████████████████████████████████████████████▏    | 647/677 [00:02<00:00, 232.02it/s]
 99%|████████████████████████████████████████████████████████████████████████████████████████████████████████████▏| 672/677 [00:02<00:00, 234.93it/s]
Models:  10%|██████████▌                                                                                              | 1/10 [00:02<00:26,  2.91s/it]
  0%|                                                                                                                        | 0/677 [00:00<?, ?it/s]
  3%|███▌                                                                                                          | 22/677 [00:00<00:03, 215.94it/s]
  6%|███████▏                                                                                                      | 44/677 [00:00<00:02, 213.37it/s]
 10%|██████████▋                                                                                                   | 66/677 [00:00<00:02, 213.13it/s]
 13%|██████████████▎                                                                                               | 88/677 [00:00<00:02, 212.29it/s]
 16%|█████████████████▋                                                                                           | 110/677 [00:00<00:02, 214.62it/s]
 19%|█████████████████████▎                                                                                       | 132/677 [00:00<00:02, 213.91it/s]
 23%|████████████████████████▉                                                                                    | 155/677 [00:00<00:02, 217.38it/s]
 26%|████████████████████████████▍                                                                                | 177/677 [00:00<00:02, 214.65it/s]
 29%|████████████████████████████████                                                                             | 199/677 [00:00<00:02, 213.89it/s]
 33%|███████████████████████████████████▌                                                                         | 221/677 [00:01<00:02, 213.31it/s]
 36%|███████████████████████████████████████                                                                      | 243/677 [00:01<00:02, 213.19it/s]
 39%|██████████████████████████████████████████▋                                                                  | 265/677 [00:01<00:01, 213.06it/s]
 42%|██████████████████████████████████████████████▏                                                              | 287/677 [00:01<00:01, 211.35it/s]
 46%|█████████████████████████████████████████████████▊                                                           | 309/677 [00:01<00:01, 212.64it/s]
 49%|█████████████████████████████████████████████████████▎                                                       | 331/677 [00:01<00:01, 212.70it/s]
 52%|████████████████████████████████████████████████████████▊                                                    | 353/677 [00:01<00:01, 213.95it/s]
 55%|████████████████████████████████████████████████████████████▍                                                | 375/677 [00:01<00:01, 213.80it/s]
 59%|███████████████████████████████████████████████████████████████▉                                             | 397/677 [00:01<00:01, 213.02it/s]
 62%|███████████████████████████████████████████████████████████████████▍                                         | 419/677 [00:01<00:01, 211.93it/s]
 65%|███████████████████████████████████████████████████████████████████████                                      | 441/677 [00:02<00:01, 212.30it/s]
 68%|██████████████████████████████████████████████████████████████████████████▌                                  | 463/677 [00:02<00:01, 213.22it/s]
 72%|██████████████████████████████████████████████████████████████████████████████                               | 485/677 [00:02<00:00, 211.63it/s]
 75%|█████████████████████████████████████████████████████████████████████████████████▋                           | 507/677 [00:02<00:00, 211.72it/s]
 78%|█████████████████████████████████████████████████████████████████████████████████████▏                       | 529/677 [00:02<00:00, 211.97it/s]
 81%|████████████████████████████████████████████████████████████████████████████████████████▋                    | 551/677 [00:02<00:00, 211.59it/s]
 85%|████████████████████████████████████████████████████████████████████████████████████████████▎                | 573/677 [00:02<00:00, 210.92it/s]
 88%|███████████████████████████████████████████████████████████████████████████████████████████████▊             | 595/677 [00:02<00:00, 210.67it/s]
 91%|███████████████████████████████████████████████████████████████████████████████████████████████████▎         | 617/677 [00:02<00:00, 211.24it/s]
 94%|██████████████████████████████████████████████████████████████████████████████████████████████████████▉      | 639/677 [00:03<00:00, 211.66it/s]
 98%|██████████████████████████████████████████████████████████████████████████████████████████████████████████▍  | 661/677 [00:03<00:00, 212.63it/s]
Models:  20%|█████████████████████                                                                                    | 2/10 [00:06<00:24,  3.07s/it]
  0%|                                                                                                                        | 0/677 [00:00<?, ?it/s]
  4%|████▍                                                                                                         | 27/677 [00:00<00:02, 261.41it/s]
  8%|████████▊                                                                                                     | 54/677 [00:00<00:02, 259.48it/s]
 12%|████████████▉                                                                                                 | 80/677 [00:00<00:02, 257.62it/s]
 16%|█████████████████                                                                                            | 106/677 [00:00<00:02, 258.05it/s]
 19%|█████████████████████▎                                                                                       | 132/677 [00:00<00:02, 256.90it/s]
 23%|█████████████████████████▍                                                                                   | 158/677 [00:00<00:02, 254.52it/s]
 27%|█████████████████████████████▌                                                                               | 184/677 [00:00<00:01, 253.28it/s]
 31%|█████████████████████████████████▊                                                                           | 210/677 [00:00<00:01, 254.65it/s]
 35%|█████████████████████████████████████▉                                                                       | 236/677 [00:00<00:01, 255.90it/s]
 39%|██████████████████████████████████████████▏                                                                  | 262/677 [00:01<00:01, 255.99it/s]
 43%|██████████████████████████████████████████████▎                                                              | 288/677 [00:01<00:01, 256.69it/s]
 46%|██████████████████████████████████████████████████▌                                                          | 314/677 [00:01<00:01, 255.65it/s]
 50%|██████████████████████████████████████████████████████▋                                                      | 340/677 [00:01<00:01, 256.83it/s]
 54%|██████████████████████████████████████████████████████████▉                                                  | 366/677 [00:01<00:01, 256.00it/s]
 58%|███████████████████████████████████████████████████████████████                                              | 392/677 [00:01<00:01, 254.77it/s]
 62%|███████████████████████████████████████████████████████████████████▎                                         | 418/677 [00:01<00:01, 254.49it/s]
 66%|███████████████████████████████████████████████████████████████████████▍                                     | 444/677 [00:01<00:00, 254.50it/s]
 69%|███████████████████████████████████████████████████████████████████████████▋                                 | 470/677 [00:01<00:00, 255.10it/s]
 73%|███████████████████████████████████████████████████████████████████████████████▊                             | 496/677 [00:01<00:00, 254.62it/s]
 77%|████████████████████████████████████████████████████████████████████████████████████                         | 522/677 [00:02<00:00, 253.31it/s]
 81%|████████████████████████████████████████████████████████████████████████████████████████▏                    | 548/677 [00:02<00:00, 250.21it/s]
 85%|████████████████████████████████████████████████████████████████████████████████████████████▍                | 574/677 [00:02<00:00, 250.41it/s]
 89%|████████████████████████████████████████████████████████████████████████████████████████████████▌            | 600/677 [00:02<00:00, 251.59it/s]
 92%|████████████████████████████████████████████████████████████████████████████████████████████████████▊        | 626/677 [00:02<00:00, 248.69it/s]
 96%|████████████████████████████████████████████████████████████████████████████████████████████████████████▉    | 652/677 [00:02<00:00, 251.61it/s]
Models:  30%|███████████████████████████████▌                                                                         | 3/10 [00:08<00:20,  2.89s/it]
  0%|                                                                                                                        | 0/677 [00:00<?, ?it/s]
  4%|████                                                                                                          | 25/677 [00:00<00:02, 245.62it/s]
  8%|████████▎                                                                                                     | 51/677 [00:00<00:02, 249.46it/s]
 11%|████████████▎                                                                                                 | 76/677 [00:00<00:02, 237.18it/s]
 15%|████████████████▎                                                                                            | 101/677 [00:00<00:02, 241.88it/s]
 19%|████████████████████▎                                                                                        | 126/677 [00:00<00:02, 239.54it/s]
 22%|████████████████████████▎                                                                                    | 151/677 [00:00<00:02, 240.23it/s]
 26%|████████████████████████████▎                                                                                | 176/677 [00:00<00:02, 240.37it/s]
 30%|████████████████████████████████▎                                                                            | 201/677 [00:00<00:01, 241.28it/s]
 33%|████████████████████████████████████▍                                                                        | 226/677 [00:00<00:01, 242.46it/s]
 37%|████████████████████████████████████████▍                                                                    | 251/677 [00:01<00:01, 239.30it/s]
 41%|████████████████████████████████████████████▍                                                                | 276/677 [00:01<00:01, 240.93it/s]
 44%|████████████████████████████████████████████████▍                                                            | 301/677 [00:01<00:01, 240.48it/s]
 48%|████████████████████████████████████████████████████▍                                                        | 326/677 [00:01<00:01, 236.32it/s]
 52%|████████████████████████████████████████████████████████▌                                                    | 351/677 [00:01<00:01, 236.54it/s]
 55%|████████████████████████████████████████████████████████████▍                                                | 375/677 [00:01<00:01, 235.75it/s]
 59%|████████████████████████████████████████████████████████████████▏                                            | 399/677 [00:01<00:01, 230.79it/s]
 63%|████████████████████████████████████████████████████████████████████▎                                        | 424/677 [00:01<00:01, 233.83it/s]
 66%|████████████████████████████████████████████████████████████████████████▏                                    | 448/677 [00:01<00:01, 227.51it/s]
 70%|████████████████████████████████████████████████████████████████████████████▏                                | 473/677 [00:01<00:00, 232.44it/s]
 73%|████████████████████████████████████████████████████████████████████████████████                             | 497/677 [00:02<00:00, 230.33it/s]
 77%|███████████████████████████████████████████████████████████████████████████████████▉                         | 521/677 [00:02<00:00, 228.92it/s]
 80%|███████████████████████████████████████████████████████████████████████████████████████▌                     | 544/677 [00:02<00:00, 228.84it/s]
 84%|███████████████████████████████████████████████████████████████████████████████████████████▎                 | 567/677 [00:02<00:00, 228.76it/s]
 87%|██████████████████████████████████████████████████████████████████████████████████████████████▉              | 590/677 [00:02<00:00, 219.14it/s]
 91%|██████████████████████████████████████████████████████████████████████████████████████████████████▊          | 614/677 [00:02<00:00, 224.62it/s]
 94%|██████████████████████████████████████████████████████████████████████████████████████████████████████▌      | 637/677 [00:02<00:00, 225.53it/s]
 98%|██████████████████████████████████████████████████████████████████████████████████████████████████████████▍  | 661/677 [00:02<00:00, 229.25it/s]
Models:  40%|██████████████████████████████████████████                                                               | 4/10 [00:11<00:17,  2.89s/it]
  0%|                                                                                                                        | 0/677 [00:00<?, ?it/s]
  3%|███▌                                                                                                          | 22/677 [00:00<00:03, 217.70it/s]
  6%|███████▏                                                                                                      | 44/677 [00:00<00:02, 218.23it/s]
 10%|██████████▋                                                                                                   | 66/677 [00:00<00:02, 215.91it/s]
 13%|██████████████▎                                                                                               | 88/677 [00:00<00:02, 214.64it/s]
 16%|█████████████████▋                                                                                           | 110/677 [00:00<00:02, 215.48it/s]
 19%|█████████████████████▎                                                                                       | 132/677 [00:00<00:02, 215.86it/s]
 23%|████████████████████████▊                                                                                    | 154/677 [00:00<00:02, 215.82it/s]
 26%|████████████████████████████▎                                                                                | 176/677 [00:00<00:02, 216.34it/s]
 29%|███████████████████████████████▉                                                                             | 198/677 [00:00<00:02, 213.62it/s]
 32%|███████████████████████████████████▍                                                                         | 220/677 [00:01<00:02, 213.15it/s]
 36%|██████████████████████████████████████▉                                                                      | 242/677 [00:01<00:02, 211.70it/s]
 39%|██████████████████████████████████████████▌                                                                  | 264/677 [00:01<00:01, 213.42it/s]
 42%|██████████████████████████████████████████████                                                               | 286/677 [00:01<00:01, 210.74it/s]
 45%|█████████████████████████████████████████████████▌                                                           | 308/677 [00:01<00:01, 210.50it/s]
 49%|█████████████████████████████████████████████████████▏                                                       | 330/677 [00:01<00:01, 211.29it/s]
 52%|████████████████████████████████████████████████████████▋                                                    | 352/677 [00:01<00:01, 211.93it/s]
 55%|████████████████████████████████████████████████████████████▏                                                | 374/677 [00:01<00:01, 212.09it/s]
 58%|███████████████████████████████████████████████████████████████▊                                             | 396/677 [00:01<00:01, 213.17it/s]
 62%|███████████████████████████████████████████████████████████████████▎                                         | 418/677 [00:01<00:01, 212.77it/s]
 65%|██████████████████████████████████████████████████████████████████████▊                                      | 440/677 [00:02<00:01, 213.55it/s]
 68%|██████████████████████████████████████████████████████████████████████████▌                                  | 463/677 [00:02<00:00, 215.66it/s]
 72%|██████████████████████████████████████████████████████████████████████████████▏                              | 486/677 [00:02<00:00, 217.02it/s]
 75%|█████████████████████████████████████████████████████████████████████████████████▉                           | 509/677 [00:02<00:00, 216.81it/s]
 78%|█████████████████████████████████████████████████████████████████████████████████████▍                       | 531/677 [00:02<00:00, 217.40it/s]
 82%|█████████████████████████████████████████████████████████████████████████████████████████                    | 553/677 [00:02<00:00, 217.97it/s]
 85%|████████████████████████████████████████████████████████████████████████████████████████████▌                | 575/677 [00:02<00:00, 216.08it/s]
 88%|████████████████████████████████████████████████████████████████████████████████████████████████             | 597/677 [00:02<00:00, 215.72it/s]
 91%|███████████████████████████████████████████████████████████████████████████████████████████████████▋         | 619/677 [00:02<00:00, 215.46it/s]
 95%|███████████████████████████████████████████████████████████████████████████████████████████████████████▏     | 641/677 [00:02<00:00, 216.42it/s]
 98%|██████████████████████████████████████████████████████████████████████████████████████████████████████████▋  | 663/677 [00:03<00:00, 215.36it/s]
Models:  50%|████████████████████████████████████████████████████▌                                                    | 5/10 [00:14<00:14,  2.99s/it]
  0%|                                                                                                                        | 0/677 [00:00<?, ?it/s]
  3%|███▍                                                                                                          | 21/677 [00:00<00:03, 205.78it/s]
  6%|██████▊                                                                                                       | 42/677 [00:00<00:03, 204.80it/s]
  9%|██████████▏                                                                                                   | 63/677 [00:00<00:02, 204.85it/s]
 12%|█████████████▋                                                                                                | 84/677 [00:00<00:02, 206.42it/s]
 16%|████████████████▉                                                                                            | 105/677 [00:00<00:02, 206.20it/s]
 19%|████████████████████▎                                                                                        | 126/677 [00:00<00:02, 206.33it/s]
 22%|███████████████████████▋                                                                                     | 147/677 [00:00<00:02, 206.41it/s]
 25%|███████████████████████████                                                                                  | 168/677 [00:00<00:02, 207.12it/s]
 28%|██████████████████████████████▍                                                                              | 189/677 [00:00<00:02, 207.67it/s]
 31%|█████████████████████████████████▊                                                                           | 210/677 [00:01<00:02, 207.78it/s]
 34%|█████████████████████████████████████▏                                                                       | 231/677 [00:01<00:02, 207.97it/s]
 37%|████████████████████████████████████████▌                                                                    | 252/677 [00:01<00:02, 208.19it/s]
 40%|███████████████████████████████████████████▉                                                                 | 273/677 [00:01<00:01, 207.70it/s]
 43%|███████████████████████████████████████████████▎                                                             | 294/677 [00:01<00:01, 207.60it/s]
 47%|██████████████████████████████████████████████████▋                                                          | 315/677 [00:01<00:01, 207.21it/s]
 50%|██████████████████████████████████████████████████████                                                       | 336/677 [00:01<00:01, 206.35it/s]
 53%|█████████████████████████████████████████████████████████▍                                                   | 357/677 [00:01<00:01, 206.02it/s]
 56%|████████████████████████████████████████████████████████████▊                                                | 378/677 [00:01<00:01, 205.65it/s]
 59%|████████████████████████████████████████████████████████████████▏                                            | 399/677 [00:01<00:01, 204.48it/s]
 62%|███████████████████████████████████████████████████████████████████▌                                         | 420/677 [00:02<00:01, 204.20it/s]
 65%|███████████████████████████████████████████████████████████████████████                                      | 441/677 [00:02<00:01, 204.31it/s]
 68%|██████████████████████████████████████████████████████████████████████████▍                                  | 462/677 [00:02<00:01, 204.23it/s]
 71%|█████████████████████████████████████████████████████████████████████████████▊                               | 483/677 [00:02<00:00, 204.82it/s]
 74%|█████████████████████████████████████████████████████████████████████████████████▏                           | 504/677 [00:02<00:00, 205.97it/s]
 78%|████████████████████████████████████████████████████████████████████████████████████▌                        | 525/677 [00:02<00:00, 206.63it/s]
 81%|███████████████████████████████████████████████████████████████████████████████████████▉                     | 546/677 [00:02<00:00, 206.30it/s]
 84%|███████████████████████████████████████████████████████████████████████████████████████████▎                 | 567/677 [00:02<00:00, 205.60it/s]
 87%|██████████████████████████████████████████████████████████████████████████████████████████████▋              | 588/677 [00:02<00:00, 205.59it/s]
 90%|██████████████████████████████████████████████████████████████████████████████████████████████████           | 609/677 [00:02<00:00, 205.25it/s]
 93%|█████████████████████████████████████████████████████████████████████████████████████████████████████▍       | 630/677 [00:03<00:00, 206.05it/s]
 96%|████████████████████████████████████████████████████████████████████████████████████████████████████████▉    | 652/677 [00:03<00:00, 207.23it/s]
 99%|████████████████████████████████████████████████████████████████████████████████████████████████████████████▎| 673/677 [00:03<00:00, 207.28it/s]
Models:  60%|███████████████████████████████████████████████████████████████                                          | 6/10 [00:18<00:12,  3.09s/it]
  0%|                                                                                                                        | 0/677 [00:00<?, ?it/s]
  3%|███▌                                                                                                          | 22/677 [00:00<00:03, 217.98it/s]
  7%|███████▎                                                                                                      | 45/677 [00:00<00:02, 222.10it/s]
 10%|███████████                                                                                                   | 68/677 [00:00<00:02, 221.42it/s]
 13%|██████████████▊                                                                                               | 91/677 [00:00<00:02, 222.67it/s]
 17%|██████████████████▎                                                                                          | 114/677 [00:00<00:02, 221.75it/s]
 20%|██████████████████████                                                                                       | 137/677 [00:00<00:02, 219.52it/s]
 23%|█████████████████████████▌                                                                                   | 159/677 [00:00<00:02, 218.10it/s]
 27%|█████████████████████████████▎                                                                               | 182/677 [00:00<00:02, 218.76it/s]
 30%|█████████████████████████████████                                                                            | 205/677 [00:00<00:02, 220.42it/s]
 34%|████████████████████████████████████▊                                                                        | 229/677 [00:01<00:02, 223.45it/s]
 37%|████████████████████████████████████████▌                                                                    | 252/677 [00:01<00:01, 219.89it/s]
 41%|████████████████████████████████████████████▎                                                                | 275/677 [00:01<00:01, 220.93it/s]
 44%|███████████████████████████████████████████████▉                                                             | 298/677 [00:01<00:01, 217.55it/s]
 47%|███████████████████████████████████████████████████▋                                                         | 321/677 [00:01<00:01, 218.89it/s]
 51%|███████████████████████████████████████████████████████▏                                                     | 343/677 [00:01<00:01, 218.40it/s]
 54%|██████████████████████████████████████████████████████████▊                                                  | 365/677 [00:01<00:01, 214.70it/s]
 57%|██████████████████████████████████████████████████████████████▎                                              | 387/677 [00:01<00:01, 212.62it/s]
 60%|█████████████████████████████████████████████████████████████████▊                                           | 409/677 [00:01<00:01, 211.22it/s]
 64%|█████████████████████████████████████████████████████████████████████▍                                       | 431/677 [00:01<00:01, 210.46it/s]
 67%|████████████████████████████████████████████████████████████████████████▉                                    | 453/677 [00:02<00:01, 208.71it/s]
 70%|████████████████████████████████████████████████████████████████████████████▎                                | 474/677 [00:02<00:00, 207.81it/s]
 73%|███████████████████████████████████████████████████████████████████████████████▋                             | 495/677 [00:02<00:00, 207.11it/s]
 76%|███████████████████████████████████████████████████████████████████████████████████                          | 516/677 [00:02<00:00, 207.00it/s]
 79%|██████████████████████████████████████████████████████████████████████████████████████▍                      | 537/677 [00:02<00:00, 205.80it/s]
 83%|██████████████████████████████████████████████████████████████████████████████████████████                   | 559/677 [00:02<00:00, 208.74it/s]
 86%|█████████████████████████████████████████████████████████████████████████████████████████████▌               | 581/677 [00:02<00:00, 210.19it/s]
 89%|█████████████████████████████████████████████████████████████████████████████████████████████████            | 603/677 [00:02<00:00, 209.95it/s]
 92%|████████████████████████████████████████████████████████████████████████████████████████████████████▋        | 625/677 [00:02<00:00, 209.91it/s]
 95%|████████████████████████████████████████████████████████████████████████████████████████████████████████     | 646/677 [00:03<00:00, 209.49it/s]
 99%|███████████████████████████████████████████████████████████████████████████████████████████████████████████▌ | 668/677 [00:03<00:00, 209.27it/s]
Models:  70%|█████████████████████████████████████████████████████████████████████████▌                               | 7/10 [00:21<00:09,  3.12s/it]
  0%|                                                                                                                        | 0/677 [00:00<?, ?it/s]
  4%|████▏                                                                                                         | 26/677 [00:00<00:02, 256.96it/s]
  8%|████████▍                                                                                                     | 52/677 [00:00<00:02, 257.60it/s]
 12%|████████████▋                                                                                                 | 78/677 [00:00<00:02, 254.42it/s]
 15%|████████████████▋                                                                                            | 104/677 [00:00<00:02, 255.91it/s]
 19%|████████████████████▉                                                                                        | 130/677 [00:00<00:02, 256.55it/s]
 23%|█████████████████████████                                                                                    | 156/677 [00:00<00:02, 255.81it/s]
 27%|█████████████████████████████▎                                                                               | 182/677 [00:00<00:01, 251.76it/s]
 31%|█████████████████████████████████▍                                                                           | 208/677 [00:00<00:01, 250.69it/s]
 35%|█████████████████████████████████████▋                                                                       | 234/677 [00:00<00:01, 247.31it/s]
 38%|█████████████████████████████████████████▊                                                                   | 260/677 [00:01<00:01, 249.86it/s]
 42%|██████████████████████████████████████████████                                                               | 286/677 [00:01<00:01, 249.00it/s]
 46%|██████████████████████████████████████████████████▏                                                          | 312/677 [00:01<00:01, 247.36it/s]
 50%|██████████████████████████████████████████████████████▍                                                      | 338/677 [00:01<00:01, 250.33it/s]
 54%|██████████████████████████████████████████████████████████▌                                                  | 364/677 [00:01<00:01, 248.01it/s]
 57%|██████████████████████████████████████████████████████████████▋                                              | 389/677 [00:01<00:01, 248.10it/s]
 61%|██████████████████████████████████████████████████████████████████▊                                          | 415/677 [00:01<00:01, 249.21it/s]
 65%|███████████████████████████████████████████████████████████████████████                                      | 441/677 [00:01<00:00, 251.00it/s]
 69%|███████████████████████████████████████████████████████████████████████████▏                                 | 467/677 [00:01<00:00, 253.52it/s]
 73%|███████████████████████████████████████████████████████████████████████████████▍                             | 493/677 [00:01<00:00, 254.11it/s]
 77%|███████████████████████████████████████████████████████████████████████████████████▌                         | 519/677 [00:02<00:00, 252.83it/s]
 81%|███████████████████████████████████████████████████████████████████████████████████████▋                     | 545/677 [00:02<00:00, 252.13it/s]
 84%|███████████████████████████████████████████████████████████████████████████████████████████▉                 | 571/677 [00:02<00:00, 253.73it/s]
 88%|████████████████████████████████████████████████████████████████████████████████████████████████             | 597/677 [00:02<00:00, 254.66it/s]
 92%|████████████████████████████████████████████████████████████████████████████████████████████████████▎        | 623/677 [00:02<00:00, 255.07it/s]
 96%|████████████████████████████████████████████████████████████████████████████████████████████████████████▍    | 649/677 [00:02<00:00, 256.38it/s]
100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████▋| 675/677 [00:02<00:00, 255.27it/s]
Models:  80%|████████████████████████████████████████████████████████████████████████████████████                     | 8/10 [00:23<00:05,  2.98s/it]
  0%|                                                                                                                        | 0/677 [00:00<?, ?it/s]
  4%|███▉                                                                                                          | 24/677 [00:00<00:02, 235.25it/s]
  7%|████████                                                                                                      | 50/677 [00:00<00:02, 244.71it/s]
 11%|████████████▏                                                                                                 | 75/677 [00:00<00:02, 244.59it/s]
 15%|████████████████                                                                                             | 100/677 [00:00<00:02, 245.08it/s]
 18%|████████████████████▏                                                                                        | 125/677 [00:00<00:02, 243.42it/s]
 22%|████████████████████████▎                                                                                    | 151/677 [00:00<00:02, 245.22it/s]
 26%|████████████████████████████▎                                                                                | 176/677 [00:00<00:02, 242.45it/s]
 30%|████████████████████████████████▎                                                                            | 201/677 [00:00<00:01, 241.87it/s]
 33%|████████████████████████████████████▍                                                                        | 226/677 [00:00<00:01, 241.87it/s]
 37%|████████████████████████████████████████▍                                                                    | 251/677 [00:01<00:01, 242.00it/s]
 41%|████████████████████████████████████████████▍                                                                | 276/677 [00:01<00:01, 239.64it/s]
 44%|████████████████████████████████████████████████▍                                                            | 301/677 [00:01<00:01, 241.19it/s]
 48%|████████████████████████████████████████████████████▍                                                        | 326/677 [00:01<00:01, 242.39it/s]
 52%|████████████████████████████████████████████████████████▋                                                    | 352/677 [00:01<00:01, 245.38it/s]
 56%|████████████████████████████████████████████████████████████▋                                                | 377/677 [00:01<00:01, 245.09it/s]
 60%|████████████████████████████████████████████████████████████████▉                                            | 403/677 [00:01<00:01, 246.60it/s]
 63%|████████████████████████████████████████████████████████████████████▉                                        | 428/677 [00:01<00:01, 244.80it/s]
 67%|████████████████████████████████████████████████████████████████████████▉                                    | 453/677 [00:01<00:00, 244.47it/s]
 71%|█████████████████████████████████████████████████████████████████████████████                                | 479/677 [00:01<00:00, 246.37it/s]
 74%|█████████████████████████████████████████████████████████████████████████████████▏                           | 504/677 [00:02<00:00, 242.83it/s]
 78%|█████████████████████████████████████████████████████████████████████████████████████▎                       | 530/677 [00:02<00:00, 245.33it/s]
 82%|█████████████████████████████████████████████████████████████████████████████████████████▎                   | 555/677 [00:02<00:00, 245.47it/s]
 86%|█████████████████████████████████████████████████████████████████████████████████████████████▍               | 580/677 [00:02<00:00, 246.58it/s]
 90%|█████████████████████████████████████████████████████████████████████████████████████████████████▌           | 606/677 [00:02<00:00, 249.74it/s]
 93%|█████████████████████████████████████████████████████████████████████████████████████████████████████▊       | 632/677 [00:02<00:00, 250.79it/s]
 97%|█████████████████████████████████████████████████████████████████████████████████████████████████████████▉   | 658/677 [00:02<00:00, 253.45it/s]
Models:  90%|██████████████████████████████████████████████████████████████████████████████████████████████▌          | 9/10 [00:26<00:02,  2.91s/it]
  0%|                                                                                                                        | 0/677 [00:00<?, ?it/s]
  4%|███▉                                                                                                          | 24/677 [00:00<00:02, 231.47it/s]
  7%|████████                                                                                                      | 50/677 [00:00<00:02, 243.91it/s]
 11%|████████████▏                                                                                                 | 75/677 [00:00<00:02, 243.81it/s]
 15%|████████████████                                                                                             | 100/677 [00:00<00:02, 244.40it/s]
 18%|████████████████████▏                                                                                        | 125/677 [00:00<00:02, 244.81it/s]
 22%|████████████████████████▎                                                                                    | 151/677 [00:00<00:02, 247.11it/s]
 26%|████████████████████████████▎                                                                                | 176/677 [00:00<00:02, 239.71it/s]
 30%|████████████████████████████████▎                                                                            | 201/677 [00:00<00:02, 236.43it/s]
 33%|████████████████████████████████████▏                                                                        | 225/677 [00:00<00:01, 235.78it/s]
 37%|████████████████████████████████████████                                                                     | 249/677 [00:01<00:01, 233.39it/s]
 40%|███████████████████████████████████████████▉                                                                 | 273/677 [00:01<00:01, 229.07it/s]
 44%|███████████████████████████████████████████████▊                                                             | 297/677 [00:01<00:01, 231.95it/s]
 47%|███████████████████████████████████████████████████▋                                                         | 321/677 [00:01<00:01, 233.43it/s]
 51%|███████████████████████████████████████████████████████▌                                                     | 345/677 [00:01<00:01, 234.59it/s]
 55%|███████████████████████████████████████████████████████████▌                                                 | 370/677 [00:01<00:01, 238.42it/s]
 58%|███████████████████████████████████████████████████████████████▍                                             | 394/677 [00:01<00:01, 238.51it/s]
 62%|███████████████████████████████████████████████████████████████████▎                                         | 418/677 [00:01<00:01, 238.26it/s]
 65%|███████████████████████████████████████████████████████████████████████▏                                     | 442/677 [00:01<00:00, 238.03it/s]
 69%|███████████████████████████████████████████████████████████████████████████▏                                 | 467/677 [00:01<00:00, 238.72it/s]
 73%|███████████████████████████████████████████████████████████████████████████████                              | 491/677 [00:02<00:00, 234.55it/s]
 76%|███████████████████████████████████████████████████████████████████████████████████                          | 516/677 [00:02<00:00, 238.22it/s]
 80%|███████████████████████████████████████████████████████████████████████████████████████                      | 541/677 [00:02<00:00, 239.09it/s]
 84%|███████████████████████████████████████████████████████████████████████████████████████████▎                 | 567/677 [00:02<00:00, 242.90it/s]
 88%|███████████████████████████████████████████████████████████████████████████████████████████████▍             | 593/677 [00:02<00:00, 245.57it/s]
 91%|███████████████████████████████████████████████████████████████████████████████████████████████████▌         | 618/677 [00:02<00:00, 245.55it/s]
 95%|███████████████████████████████████████████████████████████████████████████████████████████████████████▌     | 643/677 [00:02<00:00, 243.48it/s]
 99%|███████████████████████████████████████████████████████████████████████████████████████████████████████████▌ | 668/677 [00:02<00:00, 245.32it/s]
Models: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████| 10/10 [00:29<00:00,  2.96s/it]
INFO:root:Finished fitting 10 models to 677 genes
--- 62.522 seconds ---
iter 0, ELBO: -3.67e+08
iter 1, ELBO: -1.53e+08, delta_ELBO: 2.14e+08
iter 2, ELBO: -1.53e+08, delta_ELBO: 2.68e+05
iter 3, ELBO: -1.53e+08, delta_ELBO: 6.05e+03
iter 4, ELBO: -1.53e+08, delta_ELBO: 4.78e+04
iter 5, ELBO: -1.53e+08, delta_ELBO: 2.73e+03
iter 6, ELBO: -1.53e+08, delta_ELBO: 4.59e+02
iter 7, ELBO: -1.53e+08, delta_ELBO: 1.56e+02
iter 8, ELBO: -1.53e+08, delta_ELBO: 5.98e+01
iter 9, ELBO: -1.53e+08, delta_ELBO: 1.35e+01
iter 10, ELBO: -1.53e+08, delta_ELBO: 7.42e+00
iter 11, ELBO: -1.53e+08, delta_ELBO: 5.93e+00
iter 12, ELBO: -1.53e+08, delta_ELBO: 1.28e+01
iter 13, ELBO: -1.53e+08, delta_ELBO: 2.24e+00
iter 14, ELBO: -1.53e+08, delta_ELBO: 5.41e-02
iter 15, ELBO: -1.53e+08, delta_ELBO: 1.79e-01
iter 16, ELBO: -1.53e+08, delta_ELBO: 4.42e+00
iter 17, ELBO: -1.53e+08, delta_ELBO: 5.51e-01
iter 18, ELBO: -1.53e+08, delta_ELBO: 1.45e+00
iter 19, ELBO: -1.53e+08, delta_ELBO: 5.29e+00
iter 20, ELBO: -1.53e+08, delta_ELBO: 4.71e+00
iter 21, ELBO: -1.53e+08, delta_ELBO: 9.95e-04
Converged on iter 21
--- 1741.350 seconds ---
[87]:
fst.spatialDE_clusters(histology_results, patterns, adata_impt_sc.obsm['spatial'], w=3, s=1,
                       figsize=(16, 5), trans=True, format='svg', marker='o',
                       save_path='NPC1_mel_DE_clusters_singleT_boun1.svg')
_images/Crop_ROI_Boundary_image_115_0.png
[143]:
!pwd
/mnt/lingyu/nfs_share2/Python/FineST/FineST_local/Dataset/NPC/CropRec
[144]:
patientxy = 'patient1'
adata_save = fst.clean_save_adata(adata_impt_sc, str(patientxy)+'_adata_pattern_boundary.h5ad')
[83]:
histology_results.to_csv('NPC1_histology_results_sc_Bound1.csv', index=False)
patterns.to_csv('NPC1_patterns_sc_Bound1.csv', index=False)

2.3 L-R-TF-TG

[88]:
import os
import pandas as pd
os.chdir(str(path)+'FineST/FineST_local/Dataset/NPC/CropRec/')
histology_results = pd.read_csv('NPC1_histology_results_sc_Bound1.csv')
histology_results
[88]:
g pattern membership
0 EFNB1_EPHA4 0 1.0
1 EFNB1_EPHB2 0 1.0
2 EFNB1_EPHB4 0 1.0
3 EFNB2_EPHB1 0 1.0
4 EFNB2_EPHB2 0 1.0
... ... ... ...
672 CCL4_CCR5 2 1.0
673 CXCL1_ACKR1 2 1.0
674 CORT_SSTR2 1 1.0
675 HLA-G_CD8A 2 1.0
676 IL7_IL7R_IL2RG 2 1.0

677 rows × 3 columns

[89]:
path = '/mnt/lingyu/nfs_share2/Python/'
os.chdir(str(path) + 'FineST/FineST/')

Receptor2TF = fst.extract_tf(species='human')
Receptor2TF
[89]:
receptor pathway tf tf_PPR category
0 PKM Glycolysis / Gluconeogenesis ENO1 0.042934 Metabolism
1 ALDOA Glycolysis / Gluconeogenesis ENO1 0.009696 Metabolism
2 GPI Glycolysis / Gluconeogenesis ENO1 0.000961 Metabolism
3 MINPP1 Glycolysis / Gluconeogenesis ENO1 0.066090 Metabolism
4 NPR2 Purine metabolism NME2 0.022436 Metabolism
... ... ... ... ... ...
93601 PLXNA1,TREM2,TYROBP Nervous system development PSMD9 0.000153 Developmental Biology
93602 PLXNA1,TREM2,TYROBP Nervous system development SOS1 0.002213 Developmental Biology
93603 PLXNA1,TREM2,TYROBP Nervous system development SOS2 0.000206 Developmental Biology
93604 PLXNA1,TREM2,TYROBP Nervous system development SRC 0.009406 Developmental Biology
93605 PLXNA1,TREM2,TYROBP Nervous system development UPF2 0.000044 Developmental Biology

93606 rows × 5 columns

[90]:
RegNetwork = pd.read_csv(str(path)+'FineST/FineST/FineST/datasets/RegNetwork/Regnetwork_hum.csv')
RegNetwork.columns = ['tf', 'target']
RegNetwork
[90]:
tf target
0 ZBTB33 WASH8P
1 ZBTB33 CHAF1A
2 ZBTB33 MTRNR2L1
3 ZBTB33 MTRNR2L2
4 ZBTB33 MTRNR2L8
... ... ...
192067 ZNF76 CDKN1A
192068 ZNF76 PCYT1A
192069 ZNF76 TALDO1
192070 ZNRD1 ABCB1
192071 ZNRD1 BCL2

192072 rows × 2 columns

[91]:
os.chdir(str(path)+'FineST/FineST_local/Dataset/NPC/CropRec/')
!pwd
/mnt/lingyu/nfs_share2/Python/FineST/FineST_local/Dataset/NPC/CropRec
[92]:
tmp = fst.pattern_LR2TF2TG(histology_results, pattern_num=0, R_TFdatabase=Receptor2TF, TF_TGdatabase=RegNetwork)
tmp
This pattern contain %s unique ligand 126
This pattern contain %s unique receptor 141
This pattern contain %s unique tf 325
[92]:
Ligand Receptor tf Target value
0 ICAM5 CD209 CEBPB LOC100270746 0.009272
1 ICAM5 CD209 CEBPB MIR3187 0.009272
2 ICAM5 CD209 CEBPB MTRNR2L4 0.009272
3 ICAM5 CD209 CEBPB ABCF2 0.009272
4 ICAM5 CD209 CEBPB HIPK3 0.009272
... ... ... ... ... ...
20728743 CSF3 IFNGR2 ZNF382 STAT3 0.026704
20728744 TNFSF11 IFNGR1 ZNF382 STAT5B 0.026704
20728745 CSF3 IFNGR2 ZNF382 STAT5B 0.026704
20728746 TNFSF11 IFNGR1 ZNF383 JUN 0.026704
20728747 CSF3 IFNGR2 ZNF383 JUN 0.026704

12625083 rows × 5 columns

[93]:
## order according to 'value'
tmp = tmp.sort_values(by="value", ascending=False)
## statistic value classes
num_classes = tmp['value'].nunique()
print('Number of unique classes: ', num_classes)
Number of unique classes:  1933
[94]:
selected_rows = []
num_sele = 1
for value, group in tmp.groupby('value'):
    selected_rows.append(group.head(num_sele))

tmp_sele = pd.concat(selected_rows)
tmp_sele = tmp_sele.sort_values(by="value", ascending=False)
print('Length of tmp_sele:\n', len(tmp_sele))
print(tmp_sele)
Length of tmp_sele:
 1933
          Ligand Receptor      tf  Target     value
18319836    WNT6   TGFBR2   SMAD2  SMURF2  0.459459
20630485    BMP6   TGFBR1   SMAD3    GLI2  0.459459
16418870    BMP6   TGFBR1   SMAD4  HMG20A  0.390541
16444316    WNT6   TGFBR2   SMAD4  SPTBN1  0.390541
20243197  LRRC4B    PTPRF  CTNNB1     MYC  0.285677
...          ...      ...     ...     ...       ...
20726959   L1CAM    L1CAM    UPF2    UPF1  0.000001
20727000  COL9A2    NRCAM    UPF2    UPF1  0.000001
19865569  COL9A2    NRCAM    ABL1    CD55  0.000001
20726974  COL4A2    ITGA9    UPF2    UPF1  0.000001
20726973  COL9A3    ITGB1    UPF2    UPF1  0.000001

[1933 rows x 5 columns]
[95]:
num_top = 20
tmp_sele_top = tmp_sele[:num_top]
tmp_sele_top
[95]:
Ligand Receptor tf Target value
18319836 WNT6 TGFBR2 SMAD2 SMURF2 0.459459
20630485 BMP6 TGFBR1 SMAD3 GLI2 0.459459
16418870 BMP6 TGFBR1 SMAD4 HMG20A 0.390541
16444316 WNT6 TGFBR2 SMAD4 SPTBN1 0.390541
20243197 LRRC4B PTPRF CTNNB1 MYC 0.285677
18338258 JAG2 NOTCH3 RBPJ CTBP2 0.283606
20521804 PTN IL1R1 NFKB1 PTGER4 0.247764
19640905 CDH1 CDH1 CTNNB1 MAGI2 0.230145
15040153 IL1A IL6ST STAT3 ISPD-AS1 0.228232
16540412 BMP6 ACVR2B SMAD3 STUB1 0.221048
16422254 WNT10A ACVR1B SMAD4 MAPK3 0.214991
20725485 PTN IL1R1 TRAF6 CD40 0.186953
18330395 WNT6 TGFBR2 SMAD2 TOB1 0.183767
20725757 PTN IL1R1 TRAF6 VEGFA 0.183608
20725615 CTF1 TNFRSF11A TRAF6 TICAM1 0.182458
19770060 PTPRM PTPRM CTNNB1 TGFBR2 0.180933
20124859 XCL1 EGFR RB1 SKP2 0.166107
14845662 CSF3 IFNGR2 STAT1 LMO1 0.163834
16443688 BMP8A BMPR1A SMAD4 SNRNP70 0.162637
20371441 TNFSF11 IFNGR1 IRF9 IL27 0.160319
[105]:
ligand_list = set(tmp_sele_top['Ligand'])
receptor_list = set(tmp_sele_top['Receptor'])
[131]:
## select some inmortant ligands and receptors
# ligand_list = {'MIF', 'CXCL16', 'PVR',  'CD70', 'EFNA5', 'ICAM2', 'EFNA5', 'LCK', 'GP1BA'}   # 'L1CAM', 'JAM3', 'JAM2',
# receptor_list = {'ACKR3', 'CXCR6', 'TIGIT', 'CD27', 'CD8B1', 'EPHA3', 'ITGAL', 'ITGB2', 'JAM2', 'EPHA3', 'ITGA4', 'CD8A', 'ITGAM'}
subdf = fst.top_pattern_LR2TF(tmp_sele_top, ligand_list, receptor_list, top_num=15)
subdf
Ligand and Receptor in R2TFdatabase: 20
[131]:
Ligand_symbol Receptor_symbol TF Target value
18319836 WNT6 TGFBR2 SMAD2 SMURF2 0.459459
20630485 BMP6 TGFBR1 SMAD3 GLI2 0.459459
16418870 BMP6 TGFBR1 SMAD4 HMG20A 0.390541
16444316 WNT6 TGFBR2 SMAD4 SPTBN1 0.390541
20243197 LRRC4B PTPRF CTNNB1 MYC 0.285677
18338258 JAG2 NOTCH3 RBPJ CTBP2 0.283606
20521804 PTN IL1R1 NFKB1 PTGER4 0.247764
19640905 CDH1 CDH1 CTNNB1 MAGI2 0.230145
15040153 IL1A IL6ST STAT3 ISPD-AS1 0.228232
16540412 BMP6 ACVR2B SMAD3 STUB1 0.221048
16422254 WNT10A ACVR1B SMAD4 MAPK3 0.214991
20725485 PTN IL1R1 TRAF6 CD40 0.186953
18330395 WNT6 TGFBR2 SMAD2 TOB1 0.183767
20725757 PTN IL1R1 TRAF6 VEGFA 0.183608
20725615 CTF1 TNFRSF11A TRAF6 TICAM1 0.182458
[139]:
fstplt.sankey_LR2TF2TG(subdf, width=600, height=600, title='Pattern 0',  alpha_color=0.6)
[140]:
fstplt.sankey_LR2TF2TG(subdf, width=600, height=600, title='Pattern 0',  alpha_color=0.6,
                    save_path='LRTR_Bond1_pattern0.svg', fig_format='svg')

2.4 Pathway enrichment

[141]:
dic=dict()
for i in histology_results.sort_values('pattern').pattern.unique():
    dic['Pattern_{}'.format(i)]=histology_results.query('pattern == @i').sort_values('membership')['g'].values
[142]:
## run code
(result,
 pathway_res,
 result_select,
 result_pattern_all) = fst.pathway_analysis(sample=adata_impt_sc,
                                            all_interactions=None,
                                            interaction_ls=None,
                                            name=None,
                                            groups=["Pattern_0"],
                                            cut_off=1,
                                            dic=dic)
[145]:
print(result.shape)
(438, 5)
[146]:
print(result_select.shape)
result_select.head()
(65, 5)
[146]:
fisher_p pathway_size selected selected_inters name
1
WNT 5.722954e-10 160 80 {WNT4_FZD1_LRP5, WNT6_FZD1_LRP6, WNT7B_FZD9_LR... Pattern_0
COLLAGEN 8.989853e-01 120 29 {COL4A2_ITGA11_ITGB1, COL9A3_ITGA9_ITGB1, COL9... Pattern_0
BMP 1.507264e-03 54 26 {BMP4_BMPR1B_BMPR2, BMP8A_ACVR1_ACVR2B, BMP7_B... Pattern_0
SEMA3 6.907397e-04 39 21 {SEMA3D_NRP1_PLXNA2, SEMA3B_NRP1_PLXNA2, SEMA3... Pattern_0
LAMININ 9.999999e-01 143 17 {LAMB3_DAG1, LAMB3_ITGA3_ITGB1, LAMA5_SV2C, LA... Pattern_0
[158]:
# figsize=(7,20) for num_cutoff=1, p_cutoff=None,
Pattern = 'Pattern_0'
fstplt.dot_path(adata_impt_sc, dic=dic, num_cutoff=2, p_cutoff=0.5, groups=[str(Pattern)], step=8, figsize=(3,9))
_images/Crop_ROI_Boundary_image_137_0.png
[159]:
# fstplt.dot_path(adata_impt_sc, dic=dic, num_cutoff=2, p_cutoff=0.5, groups=[str(Pattern)], step=8, figsize=(3,9),
#              pdf=f'mel_DE_enrichment_{Pattern}_bound1')
_images/Crop_ROI_Boundary_image_138_0.png
[160]:
Pattern = 'Pattern_1'
fstplt.dot_path(adata_impt_sc, dic=dic, num_cutoff=2, p_cutoff=0.5, groups=[str(Pattern)], step=6, figsize=(3,5))
_images/Crop_ROI_Boundary_image_139_0.png
[163]:
# fstplt.dot_path(adata_impt_sc, dic=dic, num_cutoff=2, p_cutoff=0.5, groups=[str(Pattern)], step=6, figsize=(3,5),
#                pdf=f"mel_DE_enrichment_{Pattern}_bound1")
[167]:
Pattern = 'Pattern_2'
fstplt.dot_path(adata_impt_sc, dic=dic, num_cutoff=2, p_cutoff=0.5, groups=[str(Pattern)], step=2, figsize=(3,9))
_images/Crop_ROI_Boundary_image_141_0.png
[169]:
# fstplt.dot_path(adata_impt_sc, dic=dic, num_cutoff=2, p_cutoff=0.5, groups=[str(Pattern)], step=2, figsize=(3,9),
#                 pdf=f"mel_DE_enrichment_{Pattern}_bound1")
[170]:
## chord diagrams to visualize the aggregated cell types (or interactions)
adata_impt_sc.obsm['celltypes'] = adata_impt_sc.obsm['TransImp_ct_pred']
pl.chord_celltype(adata_impt_sc, pairs=['CXCL16_CXCR6'], ncol=1, min_quantile=0.01)
[170]:
GridPlot(
id = 'p1165', …)
[172]:
## chord diagrams to visualize the aggregated cell types (or interactions)
adata_impt_sc.obsm['celltypes'] = adata_impt_sc.obsm['TransImp_ct_pred']
pl.chord_celltype(adata_impt_sc, pairs=['CD70_CD27'], ncol=1, min_quantile=0.01)
[172]:
GridPlot(
id = 'p2285', …)

3. Corp ROI or WSI region from CRC Visium HD dataset

3.1 Crop the whole slide imaging (WSI) with crosspoding adata

The 10x Visium HD dataset (CRC 16um-binned) from Oliveira, et al can be downloaded from CRC16um in Goole Drive.
- where ROI4.csv and SelectedShapes.csv are two coordinate files used in this notebook.
- ROI1.csv, ROI2.csv and ROI3.csv are other three ROIs in paper,
- Rec1.csv, Rec2.csv and Rec3.csv are rectangular regions inpaper.
- Colon_Cancer_square_016um.h5ad can be found at figshare.
[21]:
os.chdir(str(path))
roi_path = './VisiumHD/Dataset/Colon_Cancer/ResultsROIs/SelectedShapes.csv'
img_path = './VisiumHD/Dataset/Colon_Cancer/Visium_HD_Human_Colon_Cancer_tissue_image.btf'
adata_path = './VisiumHD/Dataset/Colon_Cancer_square_016um.h5ad'
crop_img_path = './FineST/FineST_local/Dataset/CRC16um/StarDist/DataOutput/cropped_ROI_image.tif'
crop_adata_path = './FineST/FineST_local/Dataset/CRC16um/StarDist/DataOutput/CRC_square_016um_ROI.h5ad'
[22]:
cropped_img, adata_roi = fst.crop_img_adata(roi_path,
                                            img_path, adata_path,
                                            crop_img_path, crop_adata_path, save=True)
ROI coordinates from napari package:
    index shape-type  vertex-index        axis-0        axis-1
0      0    polygon             0  22446.377777  40506.552504
1      0    polygon             1  22733.185336  65136.151680
2      0    polygon             2   -749.183601  65207.853570
3      0    polygon             3   -785.034546  40900.912899
img shape:
 (48740, 75250, 3)
polygon:
 [[22446.37777685 40506.55250441]
 [22733.18533639 65136.15168022]
 [ -749.18360124 65207.8535701 ]
 [ -785.03454618 40900.91289879]]
polygon adjusted:
 [[22446.37777685 40506.55250441]
 [22733.18533639 65136.15168022]
 [    0.         65207.8535701 ]
 [    0.         40900.91289879]]
cropped_img shape:
 (22734, 24701, 3)
The adata:
 AnnData object with n_obs × n_vars = 137051 × 18085
    obs: 'in_tissue', 'array_row', 'array_col'
    var: 'gene_ids', 'feature_types', 'genome'
    uns: 'spatial'
    obsm: 'spatial'
The range of original adata:
 [[40624.27653892974, 65266.45052751989], [-1887.0913465497406, 22680.620734693424]]
[23]:
print(cropped_img.shape)
print(adata_roi)
(22734, 24701, 3)
AnnData object with n_obs × n_vars = 136922 × 18085
    obs: 'in_tissue', 'array_row', 'array_col'
    var: 'gene_ids', 'feature_types', 'genome'
    uns: 'spatial'
    obsm: 'spatial'
[24]:
print(adata_roi.obsm["spatial"][:,0].min(), adata_roi.obsm["spatial"][:,0].max())
print(adata_roi.obsm["spatial"][:,1].min(), adata_roi.obsm["spatial"][:,1].max())
40624.27653892974 65167.50360834386
0.4282210569424407 22680.620734693424
[25]:
plt.imshow(cropped_img)
plt.show()
_images/Crop_ROI_Boundary_image_152_0.png
[26]:
adata_roi.to_df()
[26]:
SAMD11 NOC2L KLHL17 PLEKHN1 PERM1 HES4 ISG15 AGRN RNF223 C1orf159 ... MT-ND2 MT-CO2 MT-ATP6 MT-CO3 MT-ND3 MT-ND4L MT-ND4 MT-ND5 MT-ND6 MT-CYB
s_016um_00052_00082-1 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
s_016um_00010_00367-1 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 ... 10.0 31.0 53.0 36.0 15.0 17.0 54.0 3.0 3.0 10.0
s_016um_00163_00399-1 0.0 0.0 0.0 0.0 0.0 1.0 8.0 2.0 0.0 0.0 ... 0.0 1.0 2.0 1.0 2.0 1.0 5.0 0.0 1.0 1.0
s_016um_00238_00388-1 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ... 1.0 2.0 6.0 4.0 1.0 2.0 12.0 2.0 0.0 0.0
s_016um_00144_00175-1 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ... 0.0 1.0 0.0 2.0 1.0 1.0 2.0 0.0 0.0 0.0
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
s_016um_00375_00231-1 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ... 3.0 7.0 14.0 15.0 3.0 4.0 14.0 5.0 1.0 4.0
s_016um_00109_00223-1 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 ... 0.0 2.0 5.0 2.0 1.0 0.0 1.0 0.0 0.0 3.0
s_016um_00039_00175-1 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ... 0.0 1.0 0.0 0.0 0.0 0.0 2.0 0.0 0.0 1.0
s_016um_00037_00193-1 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 1.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0
s_016um_00144_00329-1 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ... 0.0 3.0 4.0 2.0 4.0 0.0 4.0 0.0 3.0 3.0

136922 rows × 18085 columns

[27]:
fstplt.gene_expr(adata_roi, adata_roi.to_df(), gene_selet='SPP1', marker='s', s=0.5, save_path=None)
_images/Crop_ROI_Boundary_image_154_0.png