Tutorials_ST_HE
[1]:
import scanpy as sc
import torch
import os
import pandas as pd
import sys
[2]:
import matplotlib.pyplot as plt
import community as louvain
import AgaeSMO as AgaeSMO_v1
AgaeSMO_v1.fix_seed(2022)
[3]:
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
[4]:
os.environ['R_HOME'] = '/public/home/off_liukunpeng/software/anaconda3/envs/pyg1/lib/R'
data of demo deposited in https://drive.google.com/drive/folders/1eK5zSKmSV2eaQ9jhIEcKPXwuiWaGfldH?usp=sharing
[5]:
adata_omics1 = sc.read_visium(path='data/orchid/Slide_1',
count_file="filtered_feature_bc_matrix.h5",
source_image_path="spatial")
adata_omics1.var_names_make_unique()
[6]:
adata_omics1.var["mt"] = adata_omics1.var_names.str.startswith("MT-")
adata_omics1.var["ercc"] = adata_omics1.var_names.str.startswith("ERCC-")
sc.pp.calculate_qc_metrics(adata_omics1, qc_vars=["mt","ercc"], inplace=True)
sc.pp.filter_genes(adata_omics1, min_cells=3)
sc.pp.highly_variable_genes(adata_omics1, flavor="seurat_v3", n_top_genes=3000)
sc.pp.normalize_total(adata_omics1, target_sum=1e4)
sc.pp.log1p(adata_omics1)
sc.pp.scale(adata_omics1)
[7]:
adata=adata_omics1.copy()
his_key=list(adata.uns["spatial"].keys())[0]
#int spot xy in his
adata.obsm["spatial_px"]=adata.obsm["spatial"]*adata.uns["spatial"][his_key]["scalefactors"]["tissue_hires_scalef"]
adata.obsm["spatial_px"]=adata.obsm["spatial_px"].astype(int)
# plot_spot_his(adata.uns["spatial"][his_key]["images"]["hires"],adata.obsm["spatial_px"])
#calculate box xy
r=8
adata=AgaeSMO_v1.calculate_box(adata,adata.obsm["spatial_px"],r)
adata.obsm['patch']=AgaeSMO_v1.his_path(adata,adata.uns["spatial"][his_key]["images"]["hires"])
adata.obsm["patch_flattern"]=adata.obsm["patch"].reshape(adata.obsm["patch"].shape[0],-1)
AgaeSMO_v1.check_patch_his(adata,his_key,r,save_path=None)
#counstruct his adata
adata_omics2=sc.AnnData(adata.obsm["patch_flattern"], dtype="float64")
adata_omics2.obs=adata.obs.copy()
adata_omics2.obsm=adata.obsm.copy()
adata_omics2.obsm['feat'] = AgaeSMO_v1.pca(adata_omics2, n_comps=50)
adata_omics2.obsm['tensor']=adata_omics2.X
/public/home/off_liukunpeng/software/anaconda3/envs/pyg1/lib/python3.9/site-packages/anndata/_core/anndata.py:430: FutureWarning: The dtype argument is deprecated and will be removed in late 2024.
warnings.warn(
[8]:
adata_omics1.obsm['feat'] = AgaeSMO_v1.pca(adata_omics1[:, adata_omics1.var['highly_variable']], n_comps=50)
adata_omics1.obsm['tensor']=adata_omics1[:, adata_omics1.var['highly_variable']].X.toarray()
[9]:
data = AgaeSMO_v1.construct_neighbor_graph(adata_omics1, adata_omics2,6,6,k=1)
# In[20]:
model = AgaeSMO_v1.Train_AgaeSMO(data,
device=device,
learning_rate=0.001,
weight_decay=0,
epochs=1000,
loss_weight=[1,1,1,1],
loss_fun="sce")
------Calculating spatial graph...
The graph contains 13592 edges, 2379 cells.
5.7133 neighbors per cell on average.
------Calculating spatial graph...
/public/home/off_liukunpeng/project/11_cluster_problem/AgaeSMO/AgaeSMO/utils.py:529: FutureWarning: pandas.value_counts is deprecated and will be removed in a future version. Use pd.Series(obj).value_counts() instead.
plot_df = pd.value_counts(pd.value_counts(adata.uns['adj_spatial_']['Cell1']))
/public/home/off_liukunpeng/project/11_cluster_problem/AgaeSMO/AgaeSMO/utils.py:529: FutureWarning: pandas.value_counts is deprecated and will be removed in a future version. Use pd.Series(obj).value_counts() instead.
plot_df = pd.value_counts(pd.value_counts(adata.uns['adj_spatial_']['Cell1']))
The graph contains 13592 edges, 2379 cells.
5.7133 neighbors per cell on average.
/public/home/off_liukunpeng/project/11_cluster_problem/AgaeSMO/AgaeSMO/utils.py:529: FutureWarning: pandas.value_counts is deprecated and will be removed in a future version. Use pd.Series(obj).value_counts() instead.
plot_df = pd.value_counts(pd.value_counts(adata.uns['adj_spatial_']['Cell1']))
/public/home/off_liukunpeng/project/11_cluster_problem/AgaeSMO/AgaeSMO/utils.py:529: FutureWarning: pandas.value_counts is deprecated and will be removed in a future version. Use pd.Series(obj).value_counts() instead.
plot_df = pd.value_counts(pd.value_counts(adata.uns['adj_spatial_']['Cell1']))
torch.Size([2379, 3000]) torch.Size([2379, 768])
[10]:
output = model.train()
dim_input1 3000 dim_input2 768
dim_output1 30 dim_output2 30
0%| | 0/1000 [00:00<?, ?it/s]/public/home/off_liukunpeng/software/anaconda3/envs/pyg1/lib/python3.9/site-packages/torch/nn/functional.py:1956: UserWarning: nn.functional.tanh is deprecated. Use torch.tanh instead.
warnings.warn("nn.functional.tanh is deprecated. Use torch.tanh instead.")
/public/home/off_liukunpeng/project/11_cluster_problem/AgaeSMO/AgaeSMO/layer.py:250: UserWarning: Implicit dimension choice for softmax has been deprecated. Change the call to include dim=X as an argument.
self.alpha = F.softmax(torch.squeeze(self.vu) + 1e-6)
epoch:0 2.231630802154541
epoch:10 0.8900532722473145
epoch:20 0.6542530059814453
epoch:30 0.5672029852867126
epoch:40 0.5292019248008728
epoch:50 0.5034826397895813
epoch:60 0.48116204142570496
7%|▋ | 70/1000 [00:00<00:01, 481.31it/s]
epoch:70 0.4674921929836273
epoch:80 0.4541186988353729
epoch:90 0.4475562572479248
epoch:100 0.43704646825790405
epoch:110 0.4305034875869751
12%|█▏ | 119/1000 [00:03<00:27, 32.38it/s]
epoch:120 0.42299532890319824
epoch:130 0.417154461145401
14%|█▍ | 140/1000 [00:04<00:32, 26.57it/s]
epoch:140 0.4108169376850128
15%|█▌ | 153/1000 [00:05<00:35, 24.12it/s]
epoch:150 0.4030659794807434
16%|█▌ | 161/1000 [00:05<00:37, 22.61it/s]
epoch:160 0.3970189690589905
17%|█▋ | 172/1000 [00:06<00:39, 21.05it/s]
epoch:170 0.3902672529220581
18%|█▊ | 182/1000 [00:06<00:42, 19.31it/s]
epoch:180 0.38470810651779175
19%|█▉ | 194/1000 [00:07<00:45, 17.56it/s]
epoch:190 0.3795607388019562
20%|██ | 204/1000 [00:08<00:47, 16.89it/s]
epoch:200 0.37233680486679077
21%|██▏ | 214/1000 [00:08<00:46, 16.74it/s]
epoch:210 0.36789950728416443
22%|██▏ | 224/1000 [00:09<00:46, 16.71it/s]
epoch:220 0.36337822675704956
23%|██▎ | 234/1000 [00:09<00:45, 16.70it/s]
epoch:230 0.3586377799510956
24%|██▍ | 244/1000 [00:10<00:45, 16.70it/s]
epoch:240 0.355252742767334
25%|██▌ | 254/1000 [00:11<00:44, 16.70it/s]
epoch:250 0.3510761260986328
26%|██▋ | 264/1000 [00:11<00:44, 16.70it/s]
epoch:260 0.34652724862098694
27%|██▋ | 274/1000 [00:12<00:43, 16.70it/s]
epoch:270 0.3427547514438629
28%|██▊ | 284/1000 [00:12<00:42, 16.69it/s]
epoch:280 0.33850228786468506
29%|██▉ | 294/1000 [00:13<00:42, 16.69it/s]
epoch:290 0.3352644145488739
30%|███ | 304/1000 [00:14<00:41, 16.70it/s]
epoch:300 0.3311406075954437
31%|███▏ | 314/1000 [00:14<00:41, 16.69it/s]
epoch:310 0.3267447352409363
32%|███▏ | 324/1000 [00:15<00:40, 16.70it/s]
epoch:320 0.3227553367614746
33%|███▎ | 334/1000 [00:15<00:39, 16.69it/s]
epoch:330 0.31969043612480164
34%|███▍ | 344/1000 [00:16<00:39, 16.69it/s]
epoch:340 0.31361764669418335
35%|███▌ | 354/1000 [00:17<00:38, 16.69it/s]
epoch:350 0.31316035985946655
36%|███▋ | 364/1000 [00:17<00:38, 16.70it/s]
epoch:360 0.30924397706985474
37%|███▋ | 374/1000 [00:18<00:37, 16.73it/s]
epoch:370 0.304782509803772
38%|███▊ | 384/1000 [00:18<00:36, 16.74it/s]
epoch:380 0.30291858315467834
39%|███▉ | 394/1000 [00:19<00:36, 16.75it/s]
epoch:390 0.29845166206359863
40%|████ | 404/1000 [00:20<00:35, 16.73it/s]
epoch:400 0.2951827943325043
41%|████▏ | 414/1000 [00:20<00:34, 16.74it/s]
epoch:410 0.29188522696495056
42%|████▏ | 424/1000 [00:21<00:34, 16.75it/s]
epoch:420 0.28955599665641785
43%|████▎ | 434/1000 [00:21<00:33, 16.73it/s]
epoch:430 0.2863369286060333
44%|████▍ | 444/1000 [00:22<00:33, 16.74it/s]
epoch:440 0.28172916173934937
45%|████▌ | 454/1000 [00:23<00:32, 16.73it/s]
epoch:450 0.28001856803894043
46%|████▋ | 464/1000 [00:23<00:32, 16.73it/s]
epoch:460 0.27795863151550293
47%|████▋ | 474/1000 [00:24<00:31, 16.74it/s]
epoch:470 0.2725886404514313
48%|████▊ | 484/1000 [00:24<00:30, 16.74it/s]
epoch:480 0.27131912112236023
49%|████▉ | 494/1000 [00:25<00:30, 16.74it/s]
epoch:490 0.26871800422668457
50%|█████ | 504/1000 [00:26<00:29, 16.72it/s]
epoch:500 0.26638469099998474
51%|█████▏ | 514/1000 [00:26<00:29, 16.73it/s]
epoch:510 0.2649954557418823
52%|█████▏ | 524/1000 [00:27<00:28, 16.74it/s]
epoch:520 0.26098519563674927
53%|█████▎ | 534/1000 [00:27<00:27, 16.73it/s]
epoch:530 0.25807690620422363
54%|█████▍ | 544/1000 [00:28<00:27, 16.74it/s]
epoch:540 0.2557789981365204
55%|█████▌ | 554/1000 [00:29<00:26, 16.73it/s]
epoch:550 0.2540942430496216
56%|█████▋ | 564/1000 [00:29<00:26, 16.73it/s]
epoch:560 0.25121673941612244
57%|█████▋ | 574/1000 [00:30<00:25, 16.71it/s]
epoch:570 0.24824164807796478
58%|█████▊ | 584/1000 [00:30<00:24, 16.70it/s]
epoch:580 0.24619083106517792
59%|█████▉ | 594/1000 [00:31<00:24, 16.73it/s]
epoch:590 0.24625560641288757
60%|██████ | 604/1000 [00:32<00:23, 16.73it/s]
epoch:600 0.24326445162296295
61%|██████▏ | 614/1000 [00:32<00:23, 16.72it/s]
epoch:610 0.23931223154067993
62%|██████▏ | 624/1000 [00:33<00:22, 16.71it/s]
epoch:620 0.23616795241832733
63%|██████▎ | 634/1000 [00:33<00:21, 16.72it/s]
epoch:630 0.41039198637008667
64%|██████▍ | 644/1000 [00:34<00:21, 16.72it/s]
epoch:640 0.5807430148124695
65%|██████▌ | 654/1000 [00:35<00:20, 16.73it/s]
epoch:650 0.6242638230323792
66%|██████▋ | 664/1000 [00:35<00:20, 16.74it/s]
epoch:660 0.7607449293136597
67%|██████▋ | 674/1000 [00:36<00:19, 16.75it/s]
epoch:670 0.741120457649231
68%|██████▊ | 684/1000 [00:36<00:18, 16.75it/s]
epoch:680 0.706127941608429
69%|██████▉ | 694/1000 [00:37<00:18, 16.73it/s]
epoch:690 0.6896498799324036
70%|███████ | 704/1000 [00:38<00:17, 16.74it/s]
epoch:700 0.7320688962936401
71%|███████▏ | 714/1000 [00:38<00:17, 16.73it/s]
epoch:710 0.6772519946098328
72%|███████▏ | 724/1000 [00:39<00:16, 16.74it/s]
epoch:720 0.661413311958313
73%|███████▎ | 734/1000 [00:39<00:15, 16.72it/s]
epoch:730 0.622728168964386
74%|███████▍ | 744/1000 [00:40<00:15, 16.74it/s]
epoch:740 0.5310701131820679
75%|███████▌ | 754/1000 [00:41<00:14, 16.74it/s]
epoch:750 0.5279706716537476
76%|███████▋ | 764/1000 [00:41<00:14, 16.71it/s]
epoch:760 0.590103030204773
77%|███████▋ | 774/1000 [00:42<00:13, 16.70it/s]
epoch:770 0.5526713132858276
78%|███████▊ | 784/1000 [00:42<00:12, 16.70it/s]
epoch:780 0.5141893625259399
79%|███████▉ | 794/1000 [00:43<00:12, 16.70it/s]
epoch:790 0.4853868782520294
80%|████████ | 803/1000 [00:43<00:11, 16.93it/s]
epoch:800 0.49681827425956726
81%|████████▏ | 814/1000 [00:44<00:10, 16.98it/s]
epoch:810 0.5180875062942505
82%|████████▏ | 824/1000 [00:45<00:10, 16.75it/s]
epoch:820 0.4734618067741394
83%|████████▎ | 834/1000 [00:45<00:09, 16.71it/s]
epoch:830 0.4800970256328583
84%|████████▍ | 844/1000 [00:46<00:09, 16.69it/s]
epoch:840 0.4869852662086487
85%|████████▌ | 854/1000 [00:47<00:08, 16.69it/s]
epoch:850 0.4612230062484741
86%|████████▋ | 864/1000 [00:47<00:08, 16.69it/s]
epoch:860 0.9159031510353088
87%|████████▋ | 874/1000 [00:48<00:07, 16.70it/s]
epoch:870 0.47257116436958313
88%|████████▊ | 884/1000 [00:48<00:06, 16.69it/s]
epoch:880 0.4744756817817688
89%|████████▉ | 894/1000 [00:49<00:06, 16.69it/s]
epoch:890 0.46232593059539795
90%|█████████ | 904/1000 [00:49<00:05, 16.69it/s]
epoch:900 0.45996689796447754
91%|█████████▏| 914/1000 [00:50<00:05, 16.69it/s]
epoch:910 0.4460028111934662
92%|█████████▏| 924/1000 [00:51<00:04, 16.69it/s]
epoch:920 0.4333406984806061
93%|█████████▎| 934/1000 [00:51<00:03, 16.69it/s]
epoch:930 0.4422244429588318
94%|█████████▍| 944/1000 [00:52<00:03, 16.69it/s]
epoch:940 0.46161577105522156
95%|█████████▌| 954/1000 [00:52<00:02, 16.69it/s]
epoch:950 0.49867767095565796
96%|█████████▋| 964/1000 [00:53<00:02, 16.70it/s]
epoch:960 0.5258305668830872
97%|█████████▋| 974/1000 [00:54<00:01, 16.69it/s]
epoch:970 0.5297571420669556
98%|█████████▊| 984/1000 [00:54<00:00, 16.69it/s]
epoch:980 0.5262701511383057
99%|█████████▉| 994/1000 [00:55<00:00, 16.69it/s]
epoch:990 0.5300549864768982
100%|██████████| 1000/1000 [00:55<00:00, 17.94it/s]
Model training finished!
[11]:
adata = adata_omics1.copy()
adata.obsm['emb_latent_omics1'] = output['emb_latent_omics1'].copy()
adata.obsm['emb_latent_omics2'] = output['emb_latent_omics2'].copy()
adata.obsm['AgaeSMO'] = output['AgaeSMO'].copy()
adata.obsm['alpha'] = output['alpha']
[12]:
n_cluster=16
tool = 'mclust' # mclust, leiden, and louvain
AgaeSMO_v1.clustering(adata,refine_=True, key='AgaeSMO', add_key='AgaeSMO', n_clusters=n_cluster, method="mclust", use_pca=True)# visualization
annotation=pd.read_csv("/public/home/off_liukunpeng/project/7_public_data/Data/osfstorage-archive/slide1_annotation.csv")
adata.obs["GrounTruth"]=list(annotation["annotation"])
indexs=AgaeSMO_v1.supervise_index(adata,"AgaeSMO",'GrounTruth')
ARI=indexs["ARI"]
fig, ax = plt.subplots()
sc.pl.spatial(adata, basis='spatial', color='AgaeSMO',ax=ax ,title='AgaeSMO', s=10, show=False)
R[write to console]: __ __
____ ___ _____/ /_ _______/ /_
/ __ `__ \/ ___/ / / / / ___/ __/
/ / / / / / /__/ / /_/ (__ ) /_
/_/ /_/ /_/\___/_/\__,_/____/\__/ version 6.1.1
Type 'citation("mclust")' for citing this R package in publications.
fitting ...
|======================================================================| 100%
/public/home/off_liukunpeng/software/anaconda3/envs/pyg1/lib/python3.9/site-packages/scanpy/plotting/_tools/scatterplots.py:1234: FutureWarning: The default value of 'ignore' for the `na_action` parameter in pandas.Categorical.map is deprecated and will be changed to 'None' in a future version. Please set na_action to the desired value to avoid seeing this warning
color_vector = pd.Categorical(values.map(color_map))
[12]:
[<Axes: title={'center': 'AgaeSMO'}, xlabel='spatial1', ylabel='spatial2'>]
[ ]: