Tutorial 1: dlPFCs

Import Modules

[1]:
import scanpy as sc
import squidpy as sq
import sys
sys.path.append(r"/home/yqzhou/JUPYTER/MyPackage/Pianno")
import pianno as po
import json
from os.path import join
import matplotlib.pyplot as plt
import matplotlib as mpl

Data input

[2]:
# Setting the data and configuration file storage path.
sample_name = "GT151673"
config_path = "/home/yqzhou/JUPYTER/Revision/Tutorials/" + sample_name
adata = sc.read(join(config_path, "pianno_adata.h5ad"))

(Optional) Step1:Automatic hyper-parameter selection

[3]:
# Specify a known marker gene for each pattern
Patterndict = dict(L1 = ['CXCL14'],
                   L2 = ['HPCAL1'],
                   L3 = ['FREM3'],
                   L4 = ['VAMP1'],
                   L5 = ['PCP4'],
                   L6 = ['KRT17'],
                   WM = ['MOBP'])
[4]:
# If the connection fails, try a few more times.
# Open the Web UI URLs to visualize the hyperparameter tuning process.
# The default experiment lasts for a maximum of 10 minutes,
# which can be modified according to the actual situation.
adata = po.AutoPatternRecognition(adata,
                                  Patterndict=Patterndict,
                                  config_path=config_path,
                                  param_tuning=True,
                                  max_experiment_duration='10m')
Configuration path of Pianno: /home/yqzhou/JUPYTER/Revision/Tutorials/GT151673
[2024-01-16 22:41:35] Creating experiment, Experiment ID: u6xaf7js
[2024-01-16 22:41:35] Starting web server...
[2024-01-16 22:41:36] Timeout, retry...
[2024-01-16 22:41:37] Setting up...
[2024-01-16 22:41:37] Web UI URLs: http://127.0.0.1:8080 http://10.10.10.6:8080 http://11.11.11.6:8080
[2024-01-16 22:51:59] Stopping experiment, please wait...
[2024-01-16 22:52:02] Experiment stopped
WARNING: Please specify a valid `library_id` or set it permanently in `adata.uns['spatial']`
_images/GT151673_7_1.png
[5]:
# Print the optimal parameters saved in the previous step.
with open(join(config_path, "best_params.json"),'r') as f:
    best_params_dict = json.load(f)
for key in best_params_dict:
    best_params = best_params_dict[key]
best_params
[5]:
{'n_class': 2,
 'dilation_radius': 3.0,
 'denoise_weight': 0.1,
 'unsharp_radius': 3.0,
 'unsharp_amount': 4.0,
 'gaussian_blur': 2.0}

(Optional) Step2:Marker Selection

[6]:
# Take the top 10 DEGs as candidate marker genes to make a proposed patterndict
Patterndict = po.ProposedPatterndict(adata, top_n=10)
[7]:
# Visualization of candidate marker genes
for k, v in Patterndict.items():
    print(k)
    print(v)
    with mpl.rc_context({'axes.facecolor':  'black',
                         'figure.figsize': [4.5, 5]}):
        sc.pl.spatial(adata, #cmap='magma',
                      layer='DenoisedX',
                      color=v,
                      ncols=5, size=5,
                      spot_size=25,
                      vmin=0, vmax='p99'
                     )
L1
['VIM', 'MYL9', 'FADS2', 'ELN', 'WFS1', 'IGFBP7', 'RAMP1', 'FABP7', 'FXYD1', 'MT1G']
_images/GT151673_11_1.png
L2
['RASGRF2', 'SERPINE2', 'HPCAL1', 'C1QL2', 'LAMP5', 'GNAL', 'CAMK2N1', 'SOWAHA', 'CALB2', 'PENK']
_images/GT151673_11_3.png
L3
['CUX2', 'LINC00507', 'PCDH8', 'ASS1', 'HS6ST3', 'FREM3', 'HOPX', 'LINC01007', 'VSTM2A', 'CALB1']
_images/GT151673_11_5.png
L4
['NEFM', 'NEFL', 'PLCH1', 'SYT2', 'NEFH', 'VAMP1']
_images/GT151673_11_7.png
L5
['FRMPD2', 'PKD2L1', 'RHO', 'HS3ST2', 'PCP4L1', 'SMYD2', 'TRABD2A', 'CAMK2D', 'PCP4', 'TMSB10']
_images/GT151673_11_9.png
L6
['SMIM32', 'OLFML2B', 'SEMA3E', 'CCL19', 'KRT17', 'IGHG1', 'IGHG4', 'NR4A2', 'IGLC2', 'CPB1']
_images/GT151673_11_11.png
WM
['CERS2', 'SEPT4', 'MBP', 'AATK', 'PIP4K2A', 'NPC1', 'ELOVL1', 'NDRG1', 'PAQR6', 'MOBP']
_images/GT151673_11_13.png

Step3:Initial Pattern Recognition

[8]:
# Construct the marker list by selecting 1-3 genes for each pattern
# from the above candidate marker genes.
Patterndict = dict(L1 = ["CXCL14",'MT1G','RELN'],
                   L2 = ['HPCAL1', "C1QL2"],
                   L3 = ['FREM3', 'HOPX', 'CALB1'],
                   L4 = ['VAMP1','NEFH','SYT2'],
                   L5 = ['PCP4','TRABD2A'],
                   L6 = ['KRT17','NR4A2'],
                   WM = ['MOBP','MBP','PAQR6'])
[9]:
adata = po.AutoPatternRecognition(adata,
                                  Patterndict=Patterndict,
                                  config_path=config_path,
                                  param_tuning=False)
WARNING: Please specify a valid `library_id` or set it permanently in `adata.uns['spatial']`
_images/GT151673_14_1.png

Step4:Annotation Improvement

[10]:
adata = po.AnnotationImprovement(adata)
---Create Spatial Graph: Done!
---Compute Spatial Energy: Done!
---Find K-Nearest Neighbor in UMAP: Done!
---Compute KNN Energy: Done!
---Compute Global Energy: Done!
WARNING: Please specify a valid `library_id` or set it permanently in `adata.uns['spatial']`
WARNING: Please specify a valid `library_id` or set it permanently in `adata.uns['spatial']`
WARNING: Please specify a valid `library_id` or set it permanently in `adata.uns['spatial']`
_images/GT151673_16_1.png
_images/GT151673_16_2.png
_images/GT151673_16_3.png
[ ]: