Cell type deconvoluation of Visium HD
Translate cell types from single cell reference to ST data (full-rank cluster mode)
In addition to shared genes,
TransDeconvrequires single cell type lables and per-cell annotations as input;After fitting the translation function,
TransDeconvwill output the predictions as well as the weights for each cell type
[ ]:
# import FineST as fst
# from FineST.datasets import dataset
# import FineST.plottings as fstplt
# print("FineST version: %s" %fst.__version__)
## From GPU2
import os
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
[5]:
# import scvelo as scv
import scanpy as sc
import numpy as np
import pandas as pd
from transpa.util import expVeloImp, leiden_cluster, expDeconv
import torch
import warnings
warnings.filterwarnings('ignore')
seed = 10
device = torch.device("cuda:0") if torch.cuda.is_available() else torch.device("cpu")
[ ]:
import os
path = '/mnt/lingyu/nfs_share2/Python/'
# path = '/home/lingyu/ssd/Python/'
1. Add metadata and log normalized (Run once time)
WARNING: It seems you use rank_genes_groups on the raw count data. Please logarithmize your data before calling rank_genes_groups.
Add meta data - 2024.9.23
[3]:
## Load Reference h5 Data
FlexOutPath = str(path)+"VisiumHD/DatasetPaper/Chrom_scFlex_aggre/AggrOutput/outs/" # Path to cellranger aggr output folder
FlexRef = sc.read_10x_h5(str(FlexOutPath)+'count/filtered_feature_bc_matrix.h5')
print(FlexRef)
## cell type annotation
MetaData = pd.read_csv(str(path)+'VisiumHD/Code/HumanColonCancer_VisiumHD-main/MetaData/SingleCell_MetaData.csv')
MetaData
AnnData object with n_obs × n_vars = 279691 × 18167
var: 'gene_ids', 'feature_types', 'genome'
[3]:
| Barcode | Patient | BC | QCFilter | Level1 | Level2 | |
|---|---|---|---|---|---|---|
| 0 | AAACAAGCAACAGCACACTTTAGG-1 | P2CRC | BC1 | Remove | QC_Filtered | QC_Filtered |
| 1 | AAACAAGCAACAGCTAACTTTAGG-1 | P2CRC | BC1 | Keep | B cells | Plasma |
| 2 | AAACAAGCAACTGTTCACTTTAGG-1 | P2CRC | BC1 | Keep | T cells | CD4 T cell |
| 3 | AAACAAGCAAGGCCTGACTTTAGG-1 | P2CRC | BC1 | Keep | Tumor | Tumor III |
| 4 | AAACAAGCACATAGTGACTTTAGG-1 | P2CRC | BC1 | Keep | Tumor | Tumor III |
| ... | ... | ... | ... | ... | ... | ... |
| 279686 | TTTGTGAGTACCGCACATGTTGAC-32 | P3NAT | BC4 | Keep | Smooth Muscle | Smooth Muscle |
| 279687 | TTTGTGAGTATTGTCAATGTTGAC-32 | P3NAT | BC4 | Keep | Smooth Muscle | SM Stress Response |
| 279688 | TTTGTGAGTGACCTATATGTTGAC-32 | P3NAT | BC4 | Keep | Intestinal Epithelial | Enterocyte |
| 279689 | TTTGTGAGTGACCTTGATGTTGAC-32 | P3NAT | BC4 | Keep | Myeloid | Macrophage |
| 279690 | TTTGTGAGTTGCACGCATGTTGAC-32 | P3NAT | BC4 | Keep | B cells | Plasma |
279691 rows × 6 columns
[14]:
## ensure the barcode order is same
FlexRef.obs_names
[14]:
Index(['AAACAAGCAACAGCACACTTTAGG-1', 'AAACAAGCAACAGCTAACTTTAGG-1',
'AAACAAGCAACTGTTCACTTTAGG-1', 'AAACAAGCAAGGCCTGACTTTAGG-1',
'AAACAAGCACATAGTGACTTTAGG-1', 'AAACAAGCAGCATTTCACTTTAGG-1',
'AAACAAGCAGGGCTATACTTTAGG-1', 'AAACAAGCATCAGGTAACTTTAGG-1',
'AAACAAGCATTGTGAGACTTTAGG-1', 'AAACCAATCAACTGTTACTTTAGG-1',
...
'TTTGGCGGTCGTTGCAATGTTGAC-32', 'TTTGGCGGTGAGTAATATGTTGAC-32',
'TTTGGCGGTTCAAGGAATGTTGAC-32', 'TTTGGCGGTTGGCGAGATGTTGAC-32',
'TTTGTGAGTAAAGCGCATGTTGAC-32', 'TTTGTGAGTACCGCACATGTTGAC-32',
'TTTGTGAGTATTGTCAATGTTGAC-32', 'TTTGTGAGTGACCTATATGTTGAC-32',
'TTTGTGAGTGACCTTGATGTTGAC-32', 'TTTGTGAGTTGCACGCATGTTGAC-32'],
dtype='object', length=279691)
[4]:
## learn from
# Clusters with > 25 cells
KpIdents = MetaData['Level2'].value_counts()
KpIdents = KpIdents[KpIdents > 25].index.tolist()
print(KpIdents)
# Filter MetaData and FlexRef
MetaData = MetaData[MetaData['Level2'].isin(KpIdents)]
print(MetaData.shape)
FlexRef = FlexRef[MetaData['Barcode'],:]
print(FlexRef)
['Tumor I', 'Plasma', 'QC_Filtered', 'Macrophage', 'CD4 T cell', 'Goblet', 'SM Stress Response', 'Smooth Muscle', 'Tumor III', 'CAF', 'Fibroblast', 'CD8 Cytotoxic T cell', 'Mature B', 'Tumor V', 'Tumor II', 'Endothelial', 'Epithelial', 'vSM', 'Enterocyte', 'Enteric Glial', 'Pericytes', 'Neutrophil', 'Proliferating Immune II', 'Mast', 'Myofibroblast', 'Unknown III (SM)', 'Lymphatic Endothelial', 'Neuroendocrine', 'mRegDC', 'Tuft', 'pDC', 'Adipocyte']
(279691, 6)
View of AnnData object with n_obs × n_vars = 279691 × 18167
var: 'gene_ids', 'feature_types', 'genome'
[28]:
## Fix cell type labels as spacexr doesn't allow special characters (i.e. spaces)
CTRef = MetaData['Level2'].replace("/", "_", regex=True)
print(CTRef)
## need Categorical for obs['Cluster']
CTRef = pd.Categorical(CTRef)
print(CTRef)
## add cluster to adata
FlexRef.obs['Cluster'] = CTRef
print(FlexRef)
0 QC_Filtered
1 Plasma
2 CD4 T cell
3 Tumor III
4 Tumor III
...
279686 Smooth Muscle
279687 SM Stress Response
279688 Enterocyte
279689 Macrophage
279690 Plasma
Name: Level2, Length: 279691, dtype: object
[28]:
['QC_Filtered', 'Plasma', 'CD4 T cell', 'Tumor III', 'Tumor III', ..., 'Smooth Muscle', 'SM Stress Response', 'Enterocyte', 'Macrophage', 'Plasma']
Length: 279691
Categories (32, object): ['Adipocyte', 'CAF', 'CD4 T cell', 'CD8 Cytotoxic T cell', ..., 'Unknown III (SM)', 'mRegDC', 'pDC', 'vSM']
[36]:
## save for cell type annotation
# FlexRef.write_h5ad(str(path)+'VisiumHD/DatasetPaper/Chrom_scFlex_aggre/Outputs/TransImp/filtered_feature_bc_matrix_CRC_reference.h5ad')
Load data – 2024.9.23
[3]:
## load preprocessed scRNA-seq and spatial datasets
RNA_PATH = str(path)+'VisiumHD/DatasetPaper/Chrom_scFlex_aggre/Outputs/TransImp/filtered_feature_bc_matrix_CRC_reference.h5ad'
RNA = scv.read(RNA_PATH)
print(RNA)
AnnData object with n_obs × n_vars = 279691 × 18167
obs: 'Cluster'
var: 'gene_ids', 'feature_types', 'genome'
[4]:
print(RNA.X.todense())
print(RNA.X.min(), RNA.X.max())
[[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 55237.0
[5]:
sc.pp.normalize_total(RNA, target_sum=2000)
sc.pp.log1p(RNA)
print(RNA)
print(RNA.X.min(), RNA.X.max())
AnnData object with n_obs × n_vars = 279691 × 18167
obs: 'Cluster'
var: 'gene_ids', 'feature_types', 'genome'
uns: 'log1p'
0.0 7.524665
[6]:
RNA.var_names
[6]:
Index(['SAMD11', 'NOC2L', 'KLHL17', 'PLEKHN1', 'PERM1', 'HES4', 'ISG15',
'AGRN', 'RNF223', 'C1orf159',
...
'ENSG00000275993', 'ENSG00000276289', 'ENSG00000278674',
'ENSG00000283597', 'ENSG00000284824', 'ENSG00000284906',
'ENSG00000285162', 'ENSG00000285269', 'ENSG00000285329',
'ENSG00000286070'],
dtype='object', length=18167)
[7]:
## save for cell type annotation
# RNA.write_h5ad(str(path)+'VisiumHD/DatasetPaper/Chrom_scFlex_aggre/Outputs/TransImp/filtered_feature_bc_matrix_CRC_reference_4SpatialScope.h5ad')
2. Begin cell type deconvoluation
[19]:
## load preprocessed scRNA-seq and spatial datasets
RNA_PATH = str(path)+'FineST/FineST_local/Dataset/CRC16um/TransImp/filtered_feature_bc_matrix_CRC_reference_4SpatialScope.h5ad'
patientxy = 'CRC16um'
ST_PATH = str(path)+'FineST/FineST_local/Dataset/ImputData/'+str(patientxy)+'/'+str(patientxy)+'_adata_impt_16um.h5ad'
# str(path)+'NPC/Data/stdata/ImputData/'+str(patientxy)+'/'+str(patientxy)+'_adata_pre16um_newdist.h5ad'
[20]:
RNA = sc.read(RNA_PATH)
HybISS = sc.read(ST_PATH)
RNA, HybISS
[20]:
(AnnData object with n_obs × n_vars = 279691 × 18167
obs: 'Cluster'
var: 'gene_ids', 'feature_types', 'genome'
uns: 'log1p',
AnnData object with n_obs × n_vars = 136954 × 862
obs: 'array_row', 'array_col'
var: 'gene_ids', 'feature_types', 'genome', 'mt', 'n_cells'
uns: 'spatial'
obsm: 'spatial')
[9]:
print(RNA.obs['Cluster'])
print(set(RNA.obs['Cluster']))
AAACAAGCAACAGCACACTTTAGG-1 QC_Filtered
AAACAAGCAACAGCTAACTTTAGG-1 Plasma
AAACAAGCAACTGTTCACTTTAGG-1 CD4 T cell
AAACAAGCAAGGCCTGACTTTAGG-1 Tumor III
AAACAAGCACATAGTGACTTTAGG-1 Tumor III
...
TTTGTGAGTACCGCACATGTTGAC-32 Smooth Muscle
TTTGTGAGTATTGTCAATGTTGAC-32 SM Stress Response
TTTGTGAGTGACCTATATGTTGAC-32 Enterocyte
TTTGTGAGTGACCTTGATGTTGAC-32 Macrophage
TTTGTGAGTTGCACGCATGTTGAC-32 Plasma
Name: Cluster, Length: 279691, dtype: category
Categories (32, object): ['Adipocyte', 'CAF', 'CD4 T cell', 'CD8 Cytotoxic T cell', ..., 'Unknown III (SM)', 'mRegDC', 'pDC', 'vSM']
{'Mature B', 'Tumor III', 'Tumor II', 'Plasma', 'Unknown III (SM)', 'Neuroendocrine', 'vSM', 'Proliferating Immune II', 'Myofibroblast', 'QC_Filtered', 'Mast', 'Smooth Muscle', 'Macrophage', 'SM Stress Response', 'CAF', 'Pericytes', 'Fibroblast', 'Enteric Glial', 'mRegDC', 'Enterocyte', 'CD4 T cell', 'Tumor I', 'CD8 Cytotoxic T cell', 'Tumor V', 'pDC', 'Epithelial', 'Goblet', 'Tuft', 'Neutrophil', 'Adipocyte', 'Lymphatic Endothelial', 'Endothelial'}
The following code prepares input for expDeconv, it
filters SC types with less than 2 samples,
select top cell-type marker genes as training candidate,
align training & testing genes
prepare cell cluster labels and annotations
[10]:
sc.pp.pca(HybISS, n_comps=30)
celltype_counts = RNA.obs['Cluster'].value_counts()
celltype_drop = celltype_counts.index[celltype_counts < 2]
rna_adata = RNA.copy()
if len(celltype_drop):
print(f'Drop celltype {list(celltype_drop)} (contain less 2 samples)')
rna_adata = RNA[~RNA.obs['Cluster'].isin(celltype_drop),].copy()
sc.tl.rank_genes_groups(rna_adata, groupby="Cluster", use_raw=False, method="wilcoxon")
markers_df = pd.DataFrame(rna_adata.uns["rank_genes_groups"]["names"]).iloc[0:100, :]
genes_sc = np.unique(markers_df.melt().value.values)
gene = np.intersect1d(genes_sc, HybISS.var_names)
print(gene.shape)
rna_adata = rna_adata[:, gene].copy()
spa_adata = HybISS[:, gene].copy()
print(rna_adata)
print(spa_adata)
(213,)
AnnData object with n_obs × n_vars = 279691 × 213
obs: 'Cluster'
var: 'gene_ids', 'feature_types', 'genome'
uns: 'log1p', 'rank_genes_groups'
AnnData object with n_obs × n_vars = 136954 × 213
obs: 'array_row', 'array_col'
var: 'gene_ids', 'feature_types', 'genome', 'mt', 'n_cells'
uns: 'spatial', 'pca'
obsm: 'spatial', 'X_pca'
varm: 'PCs'
[11]:
df_ref = pd.DataFrame(rna_adata.X.A, columns=rna_adata.var_names)
df_tgt = pd.DataFrame(spa_adata.X, columns=spa_adata.var_names)
print(df_ref.shape, df_tgt.shape)
(279691, 213) (136954, 213)
[12]:
shared_gene = np.intersect1d(df_ref.columns, df_tgt.columns)
df_ref = df_ref[shared_gene]
df_tgt = df_tgt[shared_gene]
classes = rna_adata.obs.Cluster.values
ct_list = np.unique(classes)
Run the main cell type deconvoluation method
Normalize result and select the top1 cell type
[13]:
preds, weight = expDeconv(
df_ref=df_ref,
df_tgt=df_tgt,
classes=classes,
ct_list=ct_list,
n_epochs=8000,
lr=1e-2,
seed=seed,
device=device
)
df_results = pd.DataFrame(weight, columns=ct_list)
df_results = (df_results.T/df_results.sum(axis=1)).T
# df_results
##########################################################
# Different: for Add cell type proportion and save adata
##########################################################
df_results.index = HybISS.obs_names
df_results
[LinTrans] Epoch: 8000/8000, loss: 0.974411: 100%|███████████████████████████████████████████████████████████████| 8000/8000 [03:09<00:00, 42.29it/s]
[13]:
| Adipocyte | CAF | CD4 T cell | CD8 Cytotoxic T cell | Endothelial | Enteric Glial | Enterocyte | Epithelial | Fibroblast | Goblet | ... | Smooth Muscle | Tuft | Tumor I | Tumor II | Tumor III | Tumor V | Unknown III (SM) | mRegDC | pDC | vSM | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| s_016um_00000_00000-1 | 1.081231e-07 | 2.543530e-08 | 0.000000e+00 | 0.041273 | 3.746202e-01 | 5.883067e-03 | 6.089363e-35 | 0.000000e+00 | 8.706025e-11 | 0.000000e+00 | ... | 7.109824e-39 | 5.483281e-42 | 5.220449e-24 | 0.000000e+00 | 0.000000e+00 | 0.000000e+00 | 0.000000e+00 | 0.000000e+00 | 0.0 | 1.283190e-06 |
| s_016um_00001_00000-1 | 2.071451e-03 | 0.000000e+00 | 0.000000e+00 | 0.017977 | 5.758587e-01 | 0.000000e+00 | 0.000000e+00 | 0.000000e+00 | 0.000000e+00 | 1.996402e-36 | ... | 3.592395e-02 | 0.000000e+00 | 0.000000e+00 | 0.000000e+00 | 0.000000e+00 | 0.000000e+00 | 0.000000e+00 | 0.000000e+00 | 0.0 | 0.000000e+00 |
| s_016um_00002_00000-1 | 7.869539e-03 | 0.000000e+00 | 3.523927e-03 | 0.000000 | 3.319341e-02 | 1.070087e-01 | 2.150448e-01 | 1.378650e-02 | 6.866121e-09 | 9.605341e-07 | ... | 1.289546e-01 | 0.000000e+00 | 2.697052e-05 | 1.299842e-18 | 1.018576e-09 | 1.428597e-01 | 0.000000e+00 | 0.000000e+00 | 0.0 | 0.000000e+00 |
| s_016um_00003_00000-1 | 9.980773e-04 | 0.000000e+00 | 0.000000e+00 | 0.050979 | 0.000000e+00 | 3.141153e-03 | 2.532001e-09 | 3.326362e-01 | 1.159341e-11 | 5.477972e-02 | ... | 6.447878e-04 | 7.000887e-42 | 1.158974e-21 | 3.267436e-26 | 6.146237e-04 | 1.985230e-01 | 0.000000e+00 | 0.000000e+00 | 0.0 | 0.000000e+00 |
| s_016um_00004_00000-1 | 2.186809e-07 | 6.847235e-12 | 2.567169e-03 | 0.410314 | 2.217555e-41 | 0.000000e+00 | 1.048519e-02 | 0.000000e+00 | 1.731594e-01 | 0.000000e+00 | ... | 1.499253e-19 | 0.000000e+00 | 0.000000e+00 | 3.867526e-21 | 0.000000e+00 | 0.000000e+00 | 2.499927e-01 | 0.000000e+00 | 0.0 | 6.139263e-30 |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| s_016um_00298_00418-1 | 1.392086e-05 | 4.367921e-05 | 1.251749e-01 | 0.014302 | 0.000000e+00 | 0.000000e+00 | 3.493753e-07 | 5.803113e-17 | 1.402505e-01 | 6.489931e-02 | ... | 2.875939e-04 | 0.000000e+00 | 8.342499e-05 | 5.820036e-06 | 7.954293e-10 | 1.307442e-01 | 0.000000e+00 | 0.000000e+00 | 0.0 | 1.714308e-05 |
| s_016um_00299_00418-1 | 1.865773e-03 | 3.968654e-05 | 5.709611e-02 | 0.015811 | 0.000000e+00 | 7.326737e-20 | 5.428285e-06 | 1.214634e-03 | 1.390242e-01 | 7.597196e-02 | ... | 5.049859e-03 | 0.000000e+00 | 5.393454e-11 | 2.763067e-11 | 1.570379e-01 | 2.337270e-06 | 3.627582e-25 | 0.000000e+00 | 0.0 | 9.377921e-05 |
| s_016um_00300_00418-1 | 1.178704e-05 | 1.480940e-04 | 6.004398e-15 | 0.000000 | 5.020083e-08 | 1.328463e-34 | 1.307984e-07 | 3.114768e-03 | 1.367531e-02 | 4.737025e-03 | ... | 6.009623e-04 | 5.022578e-02 | 6.254902e-11 | 2.096479e-06 | 1.062680e-01 | 1.833141e-03 | 7.272886e-07 | 0.000000e+00 | 0.0 | 1.022970e-06 |
| s_016um_00301_00418-1 | 1.267766e-05 | 2.611451e-04 | 5.029442e-05 | 0.027817 | 6.192860e-07 | 2.173593e-03 | 4.615775e-02 | 2.452103e-04 | 8.882487e-02 | 1.174856e-02 | ... | 4.012435e-02 | 0.000000e+00 | 6.833607e-05 | 1.714957e-07 | 2.886459e-01 | 1.210625e-08 | 6.035668e-05 | 2.568720e-41 | 0.0 | 1.859294e-18 |
| s_016um_00302_00418-1 | 3.616732e-26 | 0.000000e+00 | 2.669729e-02 | 0.035266 | 0.000000e+00 | 0.000000e+00 | 2.208075e-01 | 2.734127e-02 | 0.000000e+00 | 7.729110e-15 | ... | 2.687337e-24 | 0.000000e+00 | 1.340937e-01 | 0.000000e+00 | 2.210239e-01 | 8.313494e-02 | 0.000000e+00 | 7.186540e-31 | 0.0 | 0.000000e+00 |
136954 rows × 32 columns
[14]:
print(df_results[df_results.columns].idxmax(axis=1))
s_016um_00000_00000-1 Endothelial
s_016um_00001_00000-1 Endothelial
s_016um_00002_00000-1 Macrophage
s_016um_00003_00000-1 Epithelial
s_016um_00004_00000-1 CD8 Cytotoxic T cell
...
s_016um_00298_00418-1 Mast
s_016um_00299_00418-1 Mast
s_016um_00300_00418-1 Myofibroblast
s_016um_00301_00418-1 Tumor III
s_016um_00302_00418-1 Tumor III
Length: 136954, dtype: object
[15]:
print(df_results.columns[np.argmax(df_results.values, axis=1)])
Index(['Endothelial', 'Endothelial', 'Macrophage', 'Epithelial',
'CD8 Cytotoxic T cell', 'Smooth Muscle', 'Enteric Glial',
'Smooth Muscle', 'vSM', 'vSM',
...
'Neutrophil', 'Epithelial', 'Epithelial', 'Enteric Glial', 'Neutrophil',
'Mast', 'Mast', 'Myofibroblast', 'Tumor III', 'Tumor III'],
dtype='object', length=136954)
[16]:
HybISS.obs['cell_type'] = df_results.columns[np.argmax(df_results.values, axis=1)]
HybISS
[16]:
AnnData object with n_obs × n_vars = 136954 × 862
obs: 'array_row', 'array_col', 'cell_type'
var: 'gene_ids', 'feature_types', 'genome', 'mt', 'n_cells'
uns: 'spatial', 'pca'
obsm: 'spatial', 'X_pca'
varm: 'PCs'
Add cell type propotion and save adata
[20]:
df_results.index = df_results.index.astype(str)
HybISS.obsm['TransImp_ct_pred'] = df_results
HybISS
[20]:
AnnData object with n_obs × n_vars = 136954 × 862
obs: 'array_row', 'array_col', 'cell_type'
var: 'gene_ids', 'feature_types', 'genome', 'mt', 'n_cells'
uns: 'spatial', 'pca'
obsm: 'spatial', 'X_pca', 'TransImp_ct_pred'
varm: 'PCs'
[21]:
## save the results
os.chdir(str(path)+'FineST/FineST_local/Dataset/CRC16um/TransImp/')
HybISS.write(str(patientxy)+"nuclei_anno_TransImp_CRC16um.h5ad")
[22]:
!pwd
/mnt/lingyu/nfs_share2/Python/FineST/FineST_local/Dataset/CRC16um/TransImp
3. Deconvolution result visualization
[7]:
patientxy = 'CRC16um'
os.chdir(str(path)+'FineST/FineST_local/Dataset/CRC16um/TransImp/')
HybISS = sc.read_h5ad(str(patientxy)+"nuclei_anno_TransImp_CRC16um.h5ad")
print(HybISS)
AnnData object with n_obs × n_vars = 136954 × 862
obs: 'array_row', 'array_col', 'cell_type'
var: 'gene_ids', 'feature_types', 'genome', 'mt', 'n_cells'
uns: 'pca', 'spatial'
obsm: 'TransImp_ct_pred', 'X_pca', 'spatial'
varm: 'PCs'
[8]:
HybISS.obs['cell_type']
[8]:
s_016um_00000_00000-1 Endothelial
s_016um_00001_00000-1 Endothelial
s_016um_00002_00000-1 Macrophage
s_016um_00003_00000-1 Epithelial
s_016um_00004_00000-1 CD8 Cytotoxic T cell
...
s_016um_00298_00418-1 Mast
s_016um_00299_00418-1 Mast
s_016um_00300_00418-1 Myofibroblast
s_016um_00301_00418-1 Tumor III
s_016um_00302_00418-1 Tumor III
Name: cell_type, Length: 136954, dtype: category
Categories (32, object): ['Adipocyte', 'CAF', 'CD4 T cell', 'CD8 Cytotoxic T cell', ..., 'Unknown III (SM)', 'mRegDC', 'pDC', 'vSM']
[ ]:
import matplotlib.pyplot as plt
##################################
# CRC paper
##################################
def ColorsClusters():
x = ["128 128 128",
"89 106 55",
"150 86 53",
"116 20 12",
"42 98 24",
"128 128 38",
"69 61 134",
"96 177 119",
"55 126 127",
"85 128 176",
"4 0 123",
"165 204 79",
"210 167 65",
"127 23 134",
"234 51 35",
"93 203 207",
"240 146 53",
"254 255 84",
"183 45 130",
"12 0 197",
"117 251 76",
"117 251 141",
"202 49 66",
"86 188 249",
"232 167 108",
"147 44 231",
"190 253 91",
"234 51 247",
"69 142 247",
"205 118 147",
"238 230 151",
"234 134 119",
"212 162 217",
"182 215 228",
"120 105 230",
"224 135 232",
"175 249 162",
"160 251 214",
"250 228 200"]
# Convert to RGB and then to hexadecimal
Cols = ['#'+''.join([f'{int(j):02x}' for j in i.split()]) for i in x]
return Cols
# Assuming you have a list of categories
categories = ["Adipocyte", "CAF", "CD4 T cell", "CD8 Cytotoxic T cell", "Endothelial",
"Enteric Glial", "Enterocyte", "Epithelial", "Fibroblast", "Goblet",
"Lymphatic Endothelial", "Macrophage", "Mast", "Mature B", "mRegDC",
"Myofibroblast", "Neuroendocrine", "Neutrophil", "pDC", "Pericytes",
"Plasma", "Proliferating Immune II", "QC_Filtered", "SM Stress Response",
"Smooth Muscle", "Tuft", "Tumor I", "Tumor II", "Tumor III", "Tumor V",
"Unknown III (SM)", "vSM"]
## like R scale_color_manual(values=ColorsClusters()[7:38])
color_codes = ColorsClusters()[6:38]
# Create color mapping dictionary
ctype_hex_map = dict(zip(categories, color_codes))
[10]:
ctype_hex_map
[10]:
{'Adipocyte': '#453d86',
'CAF': '#60b177',
'CD4 T cell': '#377e7f',
'CD8 Cytotoxic T cell': '#5580b0',
'Endothelial': '#04007b',
'Enteric Glial': '#a5cc4f',
'Enterocyte': '#d2a741',
'Epithelial': '#7f1786',
'Fibroblast': '#ea3323',
'Goblet': '#5dcbcf',
'Lymphatic Endothelial': '#f09235',
'Macrophage': '#feff54',
'Mast': '#b72d82',
'Mature B': '#0c00c5',
'mRegDC': '#75fb4c',
'Myofibroblast': '#75fb8d',
'Neuroendocrine': '#ca3142',
'Neutrophil': '#56bcf9',
'pDC': '#e8a76c',
'Pericytes': '#932ce7',
'Plasma': '#befd5b',
'Proliferating Immune II': '#ea33f7',
'QC_Filtered': '#458ef7',
'SM Stress Response': '#cd7693',
'Smooth Muscle': '#eee697',
'Tuft': '#ea8677',
'Tumor I': '#d4a2d9',
'Tumor II': '#b6d7e4',
'Tumor III': '#7869e6',
'Tumor V': '#e087e8',
'Unknown III (SM)': '#aff9a2',
'vSM': '#a0fbd6'}
[10]:
fig, ax = plt.subplots(1, 1, figsize=(12, 8), dpi=100)
sc.pl.spatial(HybISS, img_key=None, color="cell_type", size=1.2, alpha_img=0.9, palette=ctype_hex_map, title="TransImp: FIneST", ax=ax)
[ ]:
clust_labels = categories
HybISS.obs[clust_labels] = HybISS.obsm['TransImp_ct_pred']
HybISS
AnnData object with n_obs × n_vars = 136954 × 862
obs: 'array_row', 'array_col', 'cell_type', 'Adipocyte', 'CAF', 'CD4 T cell', 'CD8 Cytotoxic T cell', 'Endothelial', 'Enteric Glial', 'Enterocyte', 'Epithelial', 'Fibroblast', 'Goblet', 'Lymphatic Endothelial', 'Macrophage', 'Mast', 'Mature B', 'mRegDC', 'Myofibroblast', 'Neuroendocrine', 'Neutrophil', 'pDC', 'Pericytes', 'Plasma', 'Proliferating Immune II', 'QC_Filtered', 'SM Stress Response', 'Smooth Muscle', 'Tuft', 'Tumor I', 'Tumor II', 'Tumor III', 'Tumor V', 'Unknown III (SM)', 'vSM'
var: 'gene_ids', 'feature_types', 'genome', 'mt', 'n_cells'
uns: 'pca', 'spatial'
obsm: 'TransImp_ct_pred', 'X_pca', 'spatial'
varm: 'PCs'
[15]:
import matplotlib as mpl
with mpl.rc_context({'axes.facecolor': 'white', 'figure.figsize': [4.0, 3.8]}):
sc.pl.spatial(HybISS, cmap='Greys', # binary # 'vidiris' 'magma'
color=['Goblet', 'Enterocyte', 'Fibroblast', 'Tumor III', 'Macrophage', 'CAF', 'CD4 T cell', 'CD8 Cytotoxic T cell'],
# color=['Goblet', 'Enterocyte', 'Fibroblast', 'Tumor III', 'Macrophage', 'CAF', 'CD4 T cell', 'CD8 Cytotoxic T cell'],
ncols=4, size=1.2,
# img_key='hires',
img_key=None,
vmin=0, vmax='p99.2', # limit color scale at 99.2% quantile of cell abundance
save='Cell_type_8_FineST_white.pdf' # add this line to save the figure as a PDF file
)
WARNING: saving figure to file figures/showCell_type_8_FineST_white.pdf
[17]:
# os.chdir(str(path)+'SpatialData/Data/resultsROIs_CRC16/')
os.chdir(str(path)+'FineST/FineST_local/Dataset/CRC16um/TransImp/')
import matplotlib as mpl
HybISS_copy = HybISS.copy()
HybISS_copy.obs["Fibroblast"] = 0
HybISS_copy.obs.loc[HybISS_copy.obs["cell_type"] == "Fibroblast", "Fibroblast"] = 1
with mpl.rc_context({'axes.facecolor': 'white', 'figure.figsize': [4.5, 3.8]}):
sc.pl.spatial(HybISS_copy, cmap='Greys',
color="Fibroblast",
ncols=1, size=1.2,
img_key=None,
vmin=0, vmax='p99.2',
save='Fibroblast_FineST.pdf'
)
WARNING: saving figure to file figures/showFibroblast_FineST.pdf
[18]:
HybISS_copy = HybISS.copy()
HybISS_copy.obs["vSM"] = 0
HybISS_copy.obs.loc[HybISS_copy.obs["cell_type"] == "vSM", "vSM"] = 1
import matplotlib as mpl
with mpl.rc_context({'axes.facecolor': 'white', 'figure.figsize': [4.5, 3.8]}):
sc.pl.spatial(HybISS_copy, cmap='Greys',
color="vSM",
ncols=1, size=1.2,
img_key=None,
vmin=0, vmax='p99.2',
save='vSM_FineST.pdf'
)
WARNING: saving figure to file figures/showvSM_FineST.pdf
[19]:
HybISS_copy = HybISS.copy()
HybISS_copy.obs["Pericytes"] = 0
HybISS_copy.obs.loc[HybISS_copy.obs["cell_type"] == "Pericytes", "Pericytes"] = 1
import matplotlib as mpl
with mpl.rc_context({'axes.facecolor': 'white', 'figure.figsize': [4.5, 3.8]}):
sc.pl.spatial(HybISS_copy, cmap='Greys',
color="Pericytes",
ncols=1, size=1.2,
img_key=None,
vmin=0, vmax='p99.2',
save='Pericytes_FineST.pdf'
)
WARNING: saving figure to file figures/showPericytes_FineST.pdf
[20]:
HybISS_copy = HybISS.copy()
HybISS_copy.obs["Goblet"] = 0
HybISS_copy.obs.loc[HybISS_copy.obs["cell_type"] == "Goblet", "Goblet"] = 1
import matplotlib as mpl
with mpl.rc_context({'axes.facecolor': 'white', 'figure.figsize': [4.5, 3.8]}):
sc.pl.spatial(HybISS_copy, cmap='Greys',
color="Goblet",
ncols=1, size=1.2,
img_key=None,
vmin=0, vmax='p99.2',
save='Goblet_FineST.pdf'
)
WARNING: saving figure to file figures/showGoblet_FineST.pdf
[21]:
HybISS_copy = HybISS.copy()
HybISS_copy.obs["Enterocyte"] = 0
HybISS_copy.obs.loc[HybISS_copy.obs["cell_type"] == "Enterocyte", "Enterocyte"] = 1
import matplotlib as mpl
with mpl.rc_context({'axes.facecolor': 'white', 'figure.figsize': [4.5, 3.8]}):
sc.pl.spatial(HybISS_copy, cmap='Greys',
color="Enterocyte",
ncols=1, size=1.2,
img_key=None,
vmin=0, vmax='p99.2',
save='Enterocyte_FineST.pdf'
)
WARNING: saving figure to file figures/showEnterocyte_FineST.pdf
[22]:
HybISS_copy = HybISS.copy()
HybISS_copy.obs["Macrophage"] = 0
HybISS_copy.obs.loc[HybISS_copy.obs["cell_type"] == "Macrophage", "Macrophage"] = 1
import matplotlib as mpl
with mpl.rc_context({'axes.facecolor': 'white', 'figure.figsize': [4.5, 3.8]}):
sc.pl.spatial(HybISS_copy, cmap='Greys',
color="Macrophage",
ncols=1, size=1.2,
img_key=None,
vmin=0, vmax='p99.2',
save='Fibroblast_cells.pdf'
)
WARNING: saving figure to file figures/showFibroblast_cells.pdf
[ ]:
## Set values below 0.1 to 0 to reduce grayscale noise
import matplotlib as mpl
import scanpy as sc
HybISS_copy = HybISS.copy()
color=['Goblet', 'Enterocyte', 'Fibroblast', 'Tumor III', 'Macrophage', 'CAF', 'CD4 T cell', 'CD8 Cytotoxic T cell']
HybISS_copy.obs.loc[:, color] = np.where(HybISS_copy.obs.loc[:, color] < 0.1, 0, HybISS_copy.obs.loc[:, color])
with mpl.rc_context({'axes.facecolor': 'white', 'figure.figsize': [4.0, 3.8]}):
sc.pl.spatial(HybISS_copy, cmap='Greys',
color=color,
ncols=4, size=1.2,
img_key=None,
vmin=0, vmax='p99.2',
save='Cell_type_8_FineST_white_01.pdf'
)
WARNING: saving figure to file figures/showCell_type_8_FineST_white_01.pdf
[27]:
import matplotlib as mpl
HybISS_copy = HybISS.copy()
color=['Plasma', 'Proliferating Immune II', 'Tumor I', 'Tumor II', 'Tumor III', 'Tumor V', 'Unknown III (SM)', 'vSM']
HybISS_copy.obs.loc[:, color] = np.where(HybISS_copy.obs.loc[:, color] < 0.1, 0, HybISS_copy.obs.loc[:, color])
with mpl.rc_context({'axes.facecolor': 'white', 'figure.figsize': [4.0, 3.8]}):
sc.pl.spatial(HybISS_copy, cmap='Greys',
color=['Plasma', 'Proliferating Immune II', 'Tumor I', 'Tumor II', 'Tumor III', 'Tumor V', 'Unknown III (SM)', 'vSM'],
ncols=4, size=1.2,
# img_key='hires',
img_key=None,
vmin=0, vmax='p99.2', # limit color scale at 99.2% quantile of cell abundance
save='Cell_type_8_FineST_white_01_continu.pdf' # add this line to save the figure as a PDF file
)
WARNING: saving figure to file figures/showCell_type_8_FineST_white_01_continu.pdf
[ ]:
# os.chdir(str(path)+'SpatialData/Data/resultsROIs_CRC16/')
os.chdir(str(path)+'FineST/FineST_local/Dataset/CRC16um/TransImp/')
fig, ax = plt.subplots(1, 1, dpi=100)
sc.pl.spatial(HybISS, img_key=None, color="cell_type", size=1.2, alpha_img=0.9, palette=ctype_hex_map, title="TransImp: FineST", ax=ax)
fig.savefig('CRC6_anno_nuclei_TransImp_FineST100dpi.pdf', format='pdf')
[12]:
p2_res = HybISS.obs['cell_type']
ss = pd.DataFrame(p2_res.value_counts()/p2_res.shape[0])
ss.columns = ['CRC16_TransImp_FineST']
df_res = ss.T
df_res
[12]:
| cell_type | Tumor III | CAF | Goblet | Tumor V | QC_Filtered | vSM | Tumor I | Endothelial | Neutrophil | Enterocyte | ... | Mast | Macrophage | CD4 T cell | Smooth Muscle | Unknown III (SM) | mRegDC | Adipocyte | Neuroendocrine | Tuft | pDC |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| CRC16_TransImp_FineST | 0.138623 | 0.110161 | 0.075244 | 0.062912 | 0.054734 | 0.052718 | 0.049119 | 0.039736 | 0.036246 | 0.034296 | ... | 0.014136 | 0.013669 | 0.010807 | 0.008755 | 0.007375 | 0.005929 | 0.005024 | 0.004761 | 0.002724 | 0.00111 |
1 rows × 32 columns
[13]:
df_res.columns
# !pwd
df_res.to_csv('celltype_prop_CRC16_FineST_ROIall.csv')
color_dict = ctype_hex_map
plot_columns = np.array(df_res.columns)
plot_colors = [color_dict[_] for _ in plot_columns]
labels = plot_columns
colors = plot_colors
labels = plot_columns#df_res.columns
colors = plot_colors#['#1D2F6F', '#8390FA', '#6EAF46', '#FAC748']
title = 'Video Game Sales By Platform and Region\n'
subtitle = 'Proportion of Games Sold by Region'
[17]:
fstplt.plot_stackedbar_p(df_res, labels, colors, title, subtitle, fig_size=(6, 1.5), save_path='TranImp_nuclei_CRC16_FineST.pdf')
[21]:
####################
# Reference data
####################
## load preprocessed scRNA-seq
RNA_PATH = str(path)+'FineST/FineST_local/Dataset/CRC16um/TransImp/filtered_feature_bc_matrix_CRC_reference_4SpatialScope.h5ad'
RNA = sc.read(RNA_PATH)
p2_res_RNA = RNA.obs['Cluster']
ss_RNA = pd.DataFrame(p2_res_RNA.value_counts()/p2_res_RNA.shape[0])
ss_RNA.columns = ['CRC16_Reference']
df_res_RNA = ss_RNA.T
df_res_RNA
[21]:
| Cluster | Tumor I | Plasma | QC_Filtered | Macrophage | CD4 T cell | Goblet | SM Stress Response | Smooth Muscle | Tumor III | CAF | ... | Proliferating Immune II | Mast | Myofibroblast | Unknown III (SM) | Lymphatic Endothelial | Neuroendocrine | mRegDC | Tuft | pDC | Adipocyte |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| CRC16_Reference | 0.124148 | 0.086742 | 0.070678 | 0.06884 | 0.062226 | 0.060964 | 0.050338 | 0.048879 | 0.047274 | 0.046895 | ... | 0.008888 | 0.006922 | 0.005949 | 0.003936 | 0.003847 | 0.001688 | 0.00143 | 0.000415 | 0.000408 | 0.000404 |
1 rows × 32 columns
[22]:
df_res_RNA.columns
# !pwd
df_res.to_csv('celltype_prop_CRC16um_scRNAseq.csv')
color_dict = ctype_hex_map
plot_columns = np.array(df_res_RNA.columns)
plot_colors = [color_dict[_] for _ in plot_columns]
labels = plot_columns
colors = plot_colors
title = 'Video Game Sales By Platform and Region\n'
subtitle = 'Proportion of Games Sold by Region'
[24]:
fstplt.plot_stackedbar_p(df_res_RNA, labels, colors, title, subtitle, fig_size=(6, 1.5), save_path='TranImp_nuclei_CRC16_Reference.pdf')
4. ROI region analysis
/mnt/lingyu/nfs_share2/Python/TESLA/TESLA/tutorial/tutorial_lly_CRC_human.ipynb
ROI1
[29]:
patientxy = 'CRC16um'
os.chdir(str(path)+'FineST/FineST_local/Dataset/CRC16um/TransImp/')
HybISS = sc.read_h5ad(str(patientxy)+"nuclei_anno_TransImp_CRC16um.h5ad")
print(HybISS)
AnnData object with n_obs × n_vars = 136954 × 862
obs: 'array_row', 'array_col', 'cell_type'
var: 'gene_ids', 'feature_types', 'genome', 'mt', 'n_cells'
uns: 'pca', 'spatial'
obsm: 'TransImp_ct_pred', 'X_pca', 'spatial'
varm: 'PCs'
[30]:
import squidpy as sq
import matplotlib.pyplot as plt
from PIL import Image
Image.MAX_IMAGE_PIXELS = None
# os.chdir(str(path)+'VisiumHD/Dataset/Colon_Cancer/ResultsROIs')
os.chdir(str(path)+'FineST/FineST_local/Dataset/CRC16um/TransImp/ResultsROIs')
[31]:
shape1 = np.array([[45947.6607929431, 48915.9884076833, 48915.9884076833, 45947.6607929431],
[10611.3113159769, 10611.3113159769, 13579.6389307166, 13579.6389307166]])
polygon1 = shape1.T
polygon1
###############################################################
# ROI:DCIS 1 (Begin at left corner)
###############################################################
from matplotlib.path import Path
polygon = Path(polygon1)
st_crop_fineST_DCIS1 = HybISS[polygon.contains_points(HybISS.obsm["spatial"]), :].copy()
st_crop_fineST_DCIS1
[31]:
AnnData object with n_obs × n_vars = 2587 × 862
obs: 'array_row', 'array_col', 'cell_type'
var: 'gene_ids', 'feature_types', 'genome', 'mt', 'n_cells'
uns: 'pca', 'spatial'
obsm: 'TransImp_ct_pred', 'X_pca', 'spatial'
varm: 'PCs'
[32]:
## ['B', 'Myeloid', 'T', 'Treg', 'fibroblast', 'tumor']].idxmax(axis=1)
st_crop_fineST_DCIS1.obs["ROI1"]=pd.DataFrame(st_crop_fineST_DCIS1.obsm['TransImp_ct_pred'][["Adipocyte", "CAF", "CD4 T cell", "CD8 Cytotoxic T cell", "Endothelial",
"Enteric Glial", "Enterocyte", "Epithelial", "Fibroblast", "Goblet",
"Lymphatic Endothelial", "Macrophage", "Mast", "Mature B", "mRegDC",
"Myofibroblast", "Neuroendocrine", "Neutrophil", "pDC", "Pericytes",
"Plasma", "Proliferating Immune II", "QC_Filtered", "SM Stress Response",
"Smooth Muscle", "Tuft", "Tumor I", "Tumor II", "Tumor III", "Tumor V",
"Unknown III (SM)", "vSM"]].idxmax(axis=1))
st_crop_fineST_DCIS1
[32]:
AnnData object with n_obs × n_vars = 2587 × 862
obs: 'array_row', 'array_col', 'cell_type', 'ROI1'
var: 'gene_ids', 'feature_types', 'genome', 'mt', 'n_cells'
uns: 'pca', 'spatial'
obsm: 'TransImp_ct_pred', 'X_pca', 'spatial'
varm: 'PCs'
[33]:
## save ROI 1 cell type
st_crop_fineST_DCIS1.write('CRC_square_016um_clty_ROI1.h5ad')
... storing 'ROI1' as categorical
[34]:
st_crop_fineST_DCIS1.obsm['spatial']
[34]:
array([[45948.334453, 12900.262854],
[45949.114808, 12841.8595 ],
[45949.89516 , 12783.456335],
...,
[48896.254796, 10778.071978],
[48897.030413, 10719.67253 ],
[48897.806028, 10661.27327 ]])
[50]:
fig, ax = plt.subplots(1, 1, dpi=100)
sc.pl.spatial(st_crop_fineST_DCIS1, img_key=None, color="ROI1", palette=ctype_hex_map, size=1.3, ax=ax)
fig.savefig('CRC16_TransImp_celty_ROI1.pdf', format='pdf')
[39]:
p2_res = st_crop_fineST_DCIS1.obs['cell_type']
ss = pd.DataFrame(p2_res.value_counts()/p2_res.shape[0])
ss.columns = ['CRC16_TransImp_ROI1:FineST']
df_res = ss.T
df_res
[39]:
| cell_type | CAF | Tumor III | Goblet | Endothelial | Tumor I | Epithelial | QC_Filtered | Macrophage | vSM | Tumor V | ... | Mature B | Fibroblast | Plasma | CD4 T cell | Enterocyte | SM Stress Response | Unknown III (SM) | Neutrophil | Smooth Muscle | mRegDC |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| CRC16_TransImp_ROI1:FineST | 0.622729 | 0.082721 | 0.042134 | 0.027831 | 0.024739 | 0.023966 | 0.019714 | 0.018941 | 0.017395 | 0.015075 | ... | 0.005025 | 0.005025 | 0.002319 | 0.001933 | 0.001546 | 0.00116 | 0.00116 | 0.000773 | 0.000387 | 0.000387 |
1 rows × 30 columns
[40]:
df_res.columns
# !pwd
df_res.to_csv('celltype_prop_CRC16_FineST_ROI1.csv')
color_dict = ctype_hex_map
plot_columns = np.array(df_res.columns)
plot_colors = [color_dict[_] for _ in plot_columns]
labels = plot_columns
colors = plot_colors
labels = plot_columns#df_res.columns
colors = plot_colors#['#1D2F6F', '#8390FA', '#6EAF46', '#FAC748']
title = 'Video Game Sales By Platform and Region\n'
subtitle = 'Proportion of Games Sold by Region'
[41]:
fstplt.plot_stackedbar_p(df_res, labels, colors, title, subtitle, fig_size=(6, 1.5))
[43]:
# fstplt.plot_stackedbar_p(df_res, labels, colors, title, subtitle, fig_size=(6, 1.5), save_path= 'TranImp_nuclei_CRC16_ROI1.pdf')
ROI2
[44]:
shape2 = np.array([[52077.8888860289, 55058.8127505544, 55058.8127505544, 52077.8888860289],
[4882.07020205375, 4882.07020205375, 7862.99406657947, 7862.99406657947]])
polygon2 = shape2.T
polygon2
###############################################################
# ROI:DCIS 1 (Begin at left corner)
###############################################################
from matplotlib.path import Path
polygon = Path(polygon2)
st_crop_fineST_DCIS2 = HybISS[polygon.contains_points(HybISS.obsm["spatial"]), :].copy()
st_crop_fineST_DCIS2
[44]:
AnnData object with n_obs × n_vars = 2599 × 862
obs: 'array_row', 'array_col', 'cell_type'
var: 'gene_ids', 'feature_types', 'genome', 'mt', 'n_cells'
uns: 'pca', 'spatial'
obsm: 'TransImp_ct_pred', 'X_pca', 'spatial'
varm: 'PCs'
[45]:
## ['B', 'Myeloid', 'T', 'Treg', 'fibroblast', 'tumor']].idxmax(axis=1)
st_crop_fineST_DCIS2.obs["ROI2"]=pd.DataFrame(st_crop_fineST_DCIS2.obsm['TransImp_ct_pred'][["Adipocyte", "CAF", "CD4 T cell", "CD8 Cytotoxic T cell", "Endothelial",
"Enteric Glial", "Enterocyte", "Epithelial", "Fibroblast", "Goblet",
"Lymphatic Endothelial", "Macrophage", "Mast", "Mature B", "mRegDC",
"Myofibroblast", "Neuroendocrine", "Neutrophil", "pDC", "Pericytes",
"Plasma", "Proliferating Immune II", "QC_Filtered", "SM Stress Response",
"Smooth Muscle", "Tuft", "Tumor I", "Tumor II", "Tumor III", "Tumor V",
"Unknown III (SM)", "vSM"]].idxmax(axis=1))
st_crop_fineST_DCIS2
[45]:
AnnData object with n_obs × n_vars = 2599 × 862
obs: 'array_row', 'array_col', 'cell_type', 'ROI2'
var: 'gene_ids', 'feature_types', 'genome', 'mt', 'n_cells'
uns: 'pca', 'spatial'
obsm: 'TransImp_ct_pred', 'X_pca', 'spatial'
varm: 'PCs'
[46]:
## save ROI 2 cell type
st_crop_fineST_DCIS2.write('CRC_square_016um_clty_ROI2.h5ad')
... storing 'ROI2' as categorical
[47]:
st_crop_fineST_DCIS2.obsm['spatial']
[47]:
array([[52088.458275, 7841.473687],
[52089.228748, 7783.080526],
[52089.99922 , 7724.687552],
...,
[55045.410032, 5018.742789],
[55046.17574 , 4960.355781],
[55046.941445, 4901.968961]])
[49]:
fig, ax = plt.subplots(1, 1, dpi=100)
sc.pl.spatial(st_crop_fineST_DCIS2, img_key=None, color="ROI2", palette=ctype_hex_map, size=1.3, ax=ax)
fig.savefig('CRC16_TransImp_celty_ROI2.pdf', format='pdf')
[52]:
p2_res = st_crop_fineST_DCIS2.obs['cell_type']
ss = pd.DataFrame(p2_res.value_counts()/p2_res.shape[0])
ss.columns = ['CRC16_TransImp_ROI2:FineST']
df_res = ss.T
df_res
[52]:
| cell_type | Tumor III | Tumor V | QC_Filtered | Tumor I | Goblet | Tumor II | Epithelial | Pericytes | Proliferating Immune II | Neutrophil | ... | SM Stress Response | Plasma | mRegDC | Tuft | Smooth Muscle | Macrophage | Fibroblast | pDC | CAF | CD8 Cytotoxic T cell |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| CRC16_TransImp_ROI2:FineST | 0.344748 | 0.136976 | 0.086957 | 0.085802 | 0.070412 | 0.050789 | 0.030012 | 0.021547 | 0.018469 | 0.017314 | ... | 0.003848 | 0.003848 | 0.003078 | 0.002693 | 0.002309 | 0.001924 | 0.001924 | 0.001924 | 0.001154 | 0.00077 |
1 rows × 32 columns
[54]:
df_res.columns
# !pwd
df_res.to_csv('celltype_prop_CRC16_FineST_ROI2.csv')
color_dict = ctype_hex_map
plot_columns = np.array(df_res.columns)
plot_colors = [color_dict[_] for _ in plot_columns]
labels = plot_columns
colors = plot_colors
labels = plot_columns#df_res.columns
colors = plot_colors#['#1D2F6F', '#8390FA', '#6EAF46', '#FAC748']
title = 'Video Game Sales By Platform and Region\n'
subtitle = 'Proportion of Games Sold by Region'
[55]:
fstplt.plot_stackedbar_p(df_res, labels, colors, title, subtitle, fig_size=(6, 1.5))
[57]:
# fstplt.plot_stackedbar_p(df_res, labels, colors, title, subtitle, fig_size=(6, 1.5),save_path='TranImp_nuclei_CRC16_ROI2.pdf')
ROI3
[58]:
shape3 = np.array([[52613.6180305706, 55608.9653402287, 55608.9653402287, 52613.6180305706],
[11143.3282285769, 11143.3282285769, 14138.6755382349, 14138.6755382349]])
polygon3 = shape3.T
polygon3
###############################################################
# ROI:DCIS 1 (Begin at left corner)
###############################################################
from matplotlib.path import Path
polygon = Path(polygon3)
st_crop_fineST_DCIS3 = HybISS[polygon.contains_points(HybISS.obsm["spatial"]), :].copy()
st_crop_fineST_DCIS3
[58]:
AnnData object with n_obs × n_vars = 2642 × 862
obs: 'array_row', 'array_col', 'cell_type'
var: 'gene_ids', 'feature_types', 'genome', 'mt', 'n_cells'
uns: 'pca', 'spatial'
obsm: 'TransImp_ct_pred', 'X_pca', 'spatial'
varm: 'PCs'
[59]:
## ['B', 'Myeloid', 'T', 'Treg', 'fibroblast', 'tumor']].idxmax(axis=1)
st_crop_fineST_DCIS3.obs["ROI3"]=pd.DataFrame(st_crop_fineST_DCIS3.obsm['TransImp_ct_pred'][["Adipocyte", "CAF", "CD4 T cell", "CD8 Cytotoxic T cell", "Endothelial",
"Enteric Glial", "Enterocyte", "Epithelial", "Fibroblast", "Goblet",
"Lymphatic Endothelial", "Macrophage", "Mast", "Mature B", "mRegDC",
"Myofibroblast", "Neuroendocrine", "Neutrophil", "pDC", "Pericytes",
"Plasma", "Proliferating Immune II", "QC_Filtered", "SM Stress Response",
"Smooth Muscle", "Tuft", "Tumor I", "Tumor II", "Tumor III", "Tumor V",
"Unknown III (SM)", "vSM"]].idxmax(axis=1))
st_crop_fineST_DCIS3
[59]:
AnnData object with n_obs × n_vars = 2642 × 862
obs: 'array_row', 'array_col', 'cell_type', 'ROI3'
var: 'gene_ids', 'feature_types', 'genome', 'mt', 'n_cells'
uns: 'pca', 'spatial'
obsm: 'TransImp_ct_pred', 'X_pca', 'spatial'
varm: 'PCs'
[61]:
## save ROI 3 cell type
st_crop_fineST_DCIS3.write('CRC_square_016um_clty_ROI3.h5ad')
... storing 'ROI3' as categorical
[62]:
st_crop_fineST_DCIS3.obsm['spatial']
[62]:
array([[52613.827722, 12287.640481],
[52614.597454, 12229.232468],
[52615.367184, 12170.824644],
...,
[55605.838612, 11275.736277],
[55606.60356 , 11217.328548],
[55607.368506, 11158.921007]])
[64]:
fig, ax = plt.subplots(1, 1, dpi=100)
sc.pl.spatial(st_crop_fineST_DCIS3, img_key=None, color="ROI3", palette=ctype_hex_map, size=1.3, ax=ax)
fig.savefig('CRC16_TransImp_celty_ROI3.pdf', format='pdf')
[65]:
p2_res = st_crop_fineST_DCIS3.obs['cell_type']
ss = pd.DataFrame(p2_res.value_counts()/p2_res.shape[0])
ss.columns = ['CRC16_TransImp_ROI3:FineST']
df_res = ss.T
df_res
[65]:
| cell_type | CAF | Goblet | Endothelial | Lymphatic Endothelial | SM Stress Response | Epithelial | Mature B | CD8 Cytotoxic T cell | Plasma | vSM | ... | Neuroendocrine | Smooth Muscle | mRegDC | Neutrophil | Tuft | Adipocyte | pDC | Tumor II | Tumor I | QC_Filtered |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| CRC16_TransImp_ROI3:FineST | 0.195307 | 0.184709 | 0.107873 | 0.077593 | 0.053747 | 0.040878 | 0.040121 | 0.039364 | 0.039364 | 0.030659 | ... | 0.006813 | 0.005678 | 0.004164 | 0.00265 | 0.00265 | 0.001893 | 0.001514 | 0.000757 | 0.000379 | 0.000379 |
1 rows × 30 columns
[66]:
df_res.columns
# !pwd
df_res.to_csv('celltype_prop_CRC16_FineST_ROI3.csv')
color_dict = ctype_hex_map
plot_columns = np.array(df_res.columns)
plot_colors = [color_dict[_] for _ in plot_columns]
labels = plot_columns
colors = plot_colors
labels = plot_columns#df_res.columns
colors = plot_colors#['#1D2F6F', '#8390FA', '#6EAF46', '#FAC748']
title = 'Video Game Sales By Platform and Region\n'
subtitle = 'Proportion of Games Sold by Region'
[67]:
fstplt.plot_stackedbar_p(df_res, labels, colors, title, subtitle, fig_size=(6, 1.5))
[69]:
# fstplt.plot_stackedbar_p(df_res, labels, colors, title, subtitle, fig_size=(6, 1.5),save_path='TranImp_nuclei_CRC16_ROI3.pdf')
ROI 4
[70]:
shape4 = np.array([[48070.1338209981, 51062.2984069588, 51062.2984069588, 48070.1338209981],
[15391.3012700471, 15391.3012700471, 18383.4658560077, 18383.4658560077]])
polygon4 = shape4.T
polygon4
###############################################################
# ROI:DCIS 1 (Begin at left corner)
###############################################################
from matplotlib.path import Path
polygon = Path(polygon4)
st_crop_fineST_DCIS4 = HybISS[polygon.contains_points(HybISS.obsm["spatial"]), :].copy()
st_crop_fineST_DCIS4
[70]:
AnnData object with n_obs × n_vars = 2625 × 862
obs: 'array_row', 'array_col', 'cell_type'
var: 'gene_ids', 'feature_types', 'genome', 'mt', 'n_cells'
uns: 'pca', 'spatial'
obsm: 'TransImp_ct_pred', 'X_pca', 'spatial'
varm: 'PCs'
[71]:
## ['B', 'Myeloid', 'T', 'Treg', 'fibroblast', 'tumor']].idxmax(axis=1)
st_crop_fineST_DCIS4.obs["ROI4"]=pd.DataFrame(st_crop_fineST_DCIS4.obsm['TransImp_ct_pred'][["Adipocyte", "CAF", "CD4 T cell", "CD8 Cytotoxic T cell", "Endothelial",
"Enteric Glial", "Enterocyte", "Epithelial", "Fibroblast", "Goblet",
"Lymphatic Endothelial", "Macrophage", "Mast", "Mature B", "mRegDC",
"Myofibroblast", "Neuroendocrine", "Neutrophil", "pDC", "Pericytes",
"Plasma", "Proliferating Immune II", "QC_Filtered", "SM Stress Response",
"Smooth Muscle", "Tuft", "Tumor I", "Tumor II", "Tumor III", "Tumor V",
"Unknown III (SM)", "vSM"]].idxmax(axis=1))
st_crop_fineST_DCIS4
[71]:
AnnData object with n_obs × n_vars = 2625 × 862
obs: 'array_row', 'array_col', 'cell_type', 'ROI4'
var: 'gene_ids', 'feature_types', 'genome', 'mt', 'n_cells'
uns: 'pca', 'spatial'
obsm: 'TransImp_ct_pred', 'X_pca', 'spatial'
varm: 'PCs'
[72]:
## save ROI 4 cell type
st_crop_fineST_DCIS4.write('CRC_square_016um_clty_ROI4.h5ad')
... storing 'ROI4' as categorical
[73]:
st_crop_fineST_DCIS4.obsm['spatial']
[73]:
array([[48070.457283, 15791.019442],
[48071.234322, 15732.60466 ],
[48072.011359, 15674.190066],
...,
[51052.38312 , 15538.581306],
[51053.155407, 15480.16437 ],
[51053.927692, 15421.747622]])
[74]:
fig, ax = plt.subplots(1, 1, dpi=100)
sc.pl.spatial(st_crop_fineST_DCIS4, img_key=None, color="ROI4", palette=ctype_hex_map, size=1.3, ax=ax)
fig.savefig('CRC16_TransImp_celty_ROI4.pdf', format='pdf')
[76]:
p2_res = st_crop_fineST_DCIS4.obs['cell_type']
ss = pd.DataFrame(p2_res.value_counts()/p2_res.shape[0])
ss.columns = ['CRC16_TransImp_ROI4:FineST']
df_res = ss.T
df_res
[76]:
| cell_type | CAF | Endothelial | Fibroblast | Lymphatic Endothelial | Mast | Tumor III | Tumor V | Pericytes | CD8 Cytotoxic T cell | Enteric Glial | ... | SM Stress Response | mRegDC | CD4 T cell | Neuroendocrine | Myofibroblast | Unknown III (SM) | Neutrophil | Tuft | pDC | Enterocyte |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| CRC16_TransImp_ROI4:FineST | 0.389333 | 0.134095 | 0.098286 | 0.049143 | 0.039238 | 0.037714 | 0.026667 | 0.026286 | 0.022476 | 0.022476 | ... | 0.004952 | 0.004571 | 0.00419 | 0.002667 | 0.002667 | 0.002286 | 0.002286 | 0.001905 | 0.001905 | 0.000381 |
1 rows × 32 columns
[77]:
df_res.columns
# !pwd
df_res.to_csv('celltype_prop_CRC16_FineST_ROI4.csv')
color_dict = ctype_hex_map
plot_columns = np.array(df_res.columns)
plot_colors = [color_dict[_] for _ in plot_columns]
labels = plot_columns
colors = plot_colors
labels = plot_columns#df_res.columns
colors = plot_colors#['#1D2F6F', '#8390FA', '#6EAF46', '#FAC748']
title = 'Video Game Sales By Platform and Region\n'
subtitle = 'Proportion of Games Sold by Region'
[78]:
fstplt.plot_stackedbar_p(df_res, labels, colors, title, subtitle, fig_size=(6, 1.5))
[80]:
# fstplt.plot_stackedbar_p(df_res, labels, colors, title, subtitle, fig_size=(6, 1.5), save_path='TranImp_nuclei_CRC16_ROI4.pdf')
5. ROIs mark
[ ]:
import os
path = '/ssd/users/lingyu/Python/'
# path = '/mnt/lingyu/nfs_share2/Python/'
[99]:
from pathlib import Path
path_to_visium_bundle = Path(str(path)+"VisiumHD/Dataset/Colon_Cancer/square_016um/").expanduser()
from skimage.io import imread as skimread
visium_high_res_image = skimread(
path_to_visium_bundle / "spatial" / "tissue_hires_image.png"
)
import json
with open(path_to_visium_bundle / "spatial" / "scalefactors_json.json") as file:
visium_scale_factors = json.load(file)
[102]:
from matplotlib import pyplot as plt
from matplotlib.patches import Polygon
fig, ax = plt.subplots(dpi=100)
ax.imshow(visium_high_res_image)
polygon = Polygon(polygon1 * visium_scale_factors["tissue_hires_scalef"], fill=False, edgecolor='gray')
ax.add_patch(polygon)
# polygon = Polygon(polygon2 * visium_scale_factors["tissue_hires_scalef"], fill=False, edgecolor='gray')
# ax.add_patch(polygon)
polygon = Polygon(polygon3 * visium_scale_factors["tissue_hires_scalef"], fill=False, edgecolor='gray')
ax.add_patch(polygon)
polygon = Polygon(polygon4 * visium_scale_factors["tissue_hires_scalef"], fill=False, edgecolor='gray')
ax.add_patch(polygon)
plt.show()
## save
# os.chdir(str(path)+'VisiumHD/Dataset/Colon_Cancer/ResultsROIs')
# fig.savefig('Visium_ROIs_CRC16_mark.pdf', format='pdf')
6. Rectangle (Res.)
/mnt/lingyu/nfs_share2/Python/TESLA/TESLA/tutorial/tutorial_lly_CRC_human.ipynb
[81]:
patientxy = 'CRC16um'
os.chdir(str(path)+'FineST/FineST_local/Dataset/CRC16um/TransImp/')
HybISS = sc.read_h5ad(str(patientxy)+"nuclei_anno_TransImp_CRC16um.h5ad")
print(HybISS)
AnnData object with n_obs × n_vars = 136954 × 862
obs: 'array_row', 'array_col', 'cell_type'
var: 'gene_ids', 'feature_types', 'genome', 'mt', 'n_cells'
uns: 'pca', 'spatial'
obsm: 'TransImp_ct_pred', 'X_pca', 'spatial'
varm: 'PCs'
Rec1
[82]:
!pwd
/mnt/lingyu/nfs_share2/Python/FineST/FineST_local/Dataset/CRC16um/TransImp
[115]:
import squidpy as sq
import matplotlib.pyplot as plt
from PIL import Image
Image.MAX_IMAGE_PIXELS = None
[116]:
os.chdir(str(path)+'FineST/FineST_local/Dataset/CRC16um/TransImp/ResultsRecs')
coord1 = pd.read_csv('Rec1.csv')
polygon1 = np.stack([coord1['axis-1'], coord1['axis-0']]).T
polygon1
[116]:
array([[40618.9924722 , 22433.34678589],
[47773.37478782, 22433.34678589],
[47773.37478782, 18270.06216155],
[40618.9924722 , 18270.06216155]])
[117]:
###############################################################
# ROI:DCIS 1 (Begin at left corner)
###############################################################
from matplotlib.path import Path
polygon = Path(polygon1)
st_crop_fineST_DCIS1 = HybISS[polygon.contains_points(HybISS.obsm["spatial"]), :].copy()
print(st_crop_fineST_DCIS1)
## ['B', 'Myeloid', 'T', 'Treg', 'fibroblast', 'tumor']].idxmax(axis=1)
st_crop_fineST_DCIS1.obs["Rec1"]=pd.DataFrame(st_crop_fineST_DCIS1.obsm['TransImp_ct_pred'][["Adipocyte", "CAF", "CD4 T cell", "CD8 Cytotoxic T cell", "Endothelial",
"Enteric Glial", "Enterocyte", "Epithelial", "Fibroblast", "Goblet",
"Lymphatic Endothelial", "Macrophage", "Mast", "Mature B", "mRegDC",
"Myofibroblast", "Neuroendocrine", "Neutrophil", "pDC", "Pericytes",
"Plasma", "Proliferating Immune II", "QC_Filtered", "SM Stress Response",
"Smooth Muscle", "Tuft", "Tumor I", "Tumor II", "Tumor III", "Tumor V",
"Unknown III (SM)", "vSM"]].idxmax(axis=1))
print(st_crop_fineST_DCIS1)
AnnData object with n_obs × n_vars = 8085 × 862
obs: 'array_row', 'array_col', 'cell_type'
var: 'gene_ids', 'feature_types', 'genome', 'mt', 'n_cells'
uns: 'pca', 'spatial'
obsm: 'TransImp_ct_pred', 'X_pca', 'spatial'
varm: 'PCs'
AnnData object with n_obs × n_vars = 8085 × 862
obs: 'array_row', 'array_col', 'cell_type', 'Rec1'
var: 'gene_ids', 'feature_types', 'genome', 'mt', 'n_cells'
uns: 'pca', 'spatial'
obsm: 'TransImp_ct_pred', 'X_pca', 'spatial'
varm: 'PCs'
[118]:
## save Rec 1 cell type
st_crop_fineST_DCIS1.write('CRC_square_016um_clty_Rec1.h5ad')
... storing 'Rec1' as categorical
[119]:
st_crop_fineST_DCIS1.obsm['spatial']
[119]:
array([[40624.276539, 22352.723279],
[40625.065572, 22294.294759],
[40625.854602, 22235.866427],
...,
[47771.585237, 20695.400617],
[47772.362858, 20636.970325],
[47773.140476, 20578.54022 ]])
[120]:
fig, ax = plt.subplots(1, 1, dpi=100)
sc.pl.spatial(st_crop_fineST_DCIS1, img_key=None, color="Rec1", palette=ctype_hex_map, size=1.2, ax=ax)
fig.savefig('CRC16_TransImp_celty_Rec1.pdf', format='pdf')
[121]:
p2_res = st_crop_fineST_DCIS1.obs['cell_type']
ss = pd.DataFrame(p2_res.value_counts()/p2_res.shape[0])
ss.columns = ['CRC16_TransImp_Res1:FineST']
df_res = ss.T
df_res
[121]:
| cell_type | vSM | Smooth Muscle | Fibroblast | Mature B | SM Stress Response | Lymphatic Endothelial | Endothelial | Enteric Glial | CD8 Cytotoxic T cell | Macrophage | ... | Tumor III | Adipocyte | Unknown III (SM) | Myofibroblast | Tumor I | Goblet | Tuft | Neuroendocrine | Enterocyte | pDC |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| CRC16_TransImp_Res1:FineST | 0.477798 | 0.071119 | 0.066419 | 0.062832 | 0.061101 | 0.049474 | 0.041311 | 0.034756 | 0.033271 | 0.015708 | ... | 0.001855 | 0.001732 | 0.001361 | 0.000742 | 0.000742 | 0.000618 | 0.000495 | 0.000247 | 0.000247 | 0.000247 |
1 rows × 32 columns
[122]:
df_res.columns
!pwd
df_res.to_csv('celltype_prop_CRC16_FineST_Res1.csv')
color_dict = ctype_hex_map
plot_columns = np.array(df_res.columns)
plot_colors = [color_dict[_] for _ in plot_columns]
labels = plot_columns
colors = plot_colors
labels = plot_columns#df_res.columns
colors = plot_colors#['#1D2F6F', '#8390FA', '#6EAF46', '#FAC748']
title = 'Video Game Sales By Platform and Region\n'
subtitle = 'Proportion of Games Sold by Region'
/mnt/lingyu/nfs_share2/Python/FineST/FineST_local/Dataset/CRC16um/TransImp/ResultsRecs
[124]:
fstplt.plot_stackedbar_p(df_res, labels, colors, title, subtitle, fig_size=(6, 1.5), save_path='TranImp_nuclei_CRC16_Res1.pdf')
Res2
[125]:
coord2 = pd.read_csv('Rec2.csv')
polygon2 = np.stack([coord2['axis-1'], coord2['axis-0']]).T
print(polygon2)
[[47766.86075085 10179.74439218]
[49313.4996414 10179.74439218]
[49313.4996414 12206.90966136]
[47766.86075085 12206.90966136]]
[126]:
###############################################################
# ROI:Res 1 (Begin at left corner)
###############################################################
from matplotlib.path import Path
polygon = Path(polygon2)
st_crop_fineST_DCIS2 = HybISS[polygon.contains_points(HybISS.obsm["spatial"]), :].copy()
print(st_crop_fineST_DCIS2)
## ['B', 'Myeloid', 'T', 'Treg', 'fibroblast', 'tumor']].idxmax(axis=1)
st_crop_fineST_DCIS2.obs["Res2"]=pd.DataFrame(st_crop_fineST_DCIS2.obsm['TransImp_ct_pred'][["Adipocyte", "CAF", "CD4 T cell", "CD8 Cytotoxic T cell", "Endothelial",
"Enteric Glial", "Enterocyte", "Epithelial", "Fibroblast", "Goblet",
"Lymphatic Endothelial", "Macrophage", "Mast", "Mature B", "mRegDC",
"Myofibroblast", "Neuroendocrine", "Neutrophil", "pDC", "Pericytes",
"Plasma", "Proliferating Immune II", "QC_Filtered", "SM Stress Response",
"Smooth Muscle", "Tuft", "Tumor I", "Tumor II", "Tumor III", "Tumor V",
"Unknown III (SM)", "vSM"]].idxmax(axis=1))
print(st_crop_fineST_DCIS2)
AnnData object with n_obs × n_vars = 944 × 862
obs: 'array_row', 'array_col', 'cell_type'
var: 'gene_ids', 'feature_types', 'genome', 'mt', 'n_cells'
uns: 'pca', 'spatial'
obsm: 'TransImp_ct_pred', 'X_pca', 'spatial'
varm: 'PCs'
AnnData object with n_obs × n_vars = 944 × 862
obs: 'array_row', 'array_col', 'cell_type', 'Res2'
var: 'gene_ids', 'feature_types', 'genome', 'mt', 'n_cells'
uns: 'pca', 'spatial'
obsm: 'TransImp_ct_pred', 'X_pca', 'spatial'
varm: 'PCs'
[127]:
## save ROI 2 cell type
st_crop_fineST_DCIS2.write('CRC_square_016um_clty_Res2.h5ad')
... storing 'Res2' as categorical
[128]:
fig, ax = plt.subplots(1, 1, dpi=100)
sc.pl.spatial(st_crop_fineST_DCIS2, img_key=None, color="Res2", palette=ctype_hex_map, size=1.3, ax=ax)
fig.savefig('CRC16_TransImp_celty_Res2.pdf', format='pdf')
[129]:
p2_res = st_crop_fineST_DCIS2.obs['cell_type']
ss = pd.DataFrame(p2_res.value_counts()/p2_res.shape[0])
ss.columns = ['CRC16_TransImp_Res2:FineST']
df_res = ss.T
df_res
[129]:
| cell_type | CAF | Tumor III | CD8 Cytotoxic T cell | Endothelial | Lymphatic Endothelial | Pericytes | vSM | Epithelial | Tumor I | Mast | ... | CD4 T cell | Smooth Muscle | Tumor II | Goblet | Plasma | SM Stress Response | mRegDC | Neutrophil | pDC | Neuroendocrine |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| CRC16_TransImp_Res2:FineST | 0.729873 | 0.032839 | 0.028602 | 0.027542 | 0.022246 | 0.019068 | 0.012712 | 0.012712 | 0.011653 | 0.010593 | ... | 0.006356 | 0.004237 | 0.004237 | 0.003178 | 0.002119 | 0.002119 | 0.002119 | 0.001059 | 0.001059 | 0.001059 |
1 rows × 27 columns
[130]:
print(df_res.columns)
# !pwd
df_res.to_csv('celltype_prop_CRC16_FineST_Res2.csv')
color_dict = ctype_hex_map
plot_columns = np.array(df_res.columns)
plot_colors = [color_dict[_] for _ in plot_columns]
labels = plot_columns
colors = plot_colors
labels = plot_columns#df_res.columns
colors = plot_colors#['#1D2F6F', '#8390FA', '#6EAF46', '#FAC748']
title = 'Video Game Sales By Platform and Region\n'
subtitle = 'Proportion of Games Sold by Region'
CategoricalIndex(['CAF', 'Tumor III', 'CD8 Cytotoxic T cell', 'Endothelial',
'Lymphatic Endothelial', 'Pericytes', 'vSM', 'Epithelial',
'Tumor I', 'Mast', 'Mature B', 'Proliferating Immune II',
'Fibroblast', 'QC_Filtered', 'Macrophage', 'Adipocyte',
'Enteric Glial', 'CD4 T cell', 'Smooth Muscle', 'Tumor II',
'Goblet', 'Plasma', 'SM Stress Response', 'mRegDC',
'Neutrophil', 'pDC', 'Neuroendocrine'],
categories=['Adipocyte', 'CAF', 'CD4 T cell', 'CD8 Cytotoxic T cell', ..., 'Tumor III', 'mRegDC', 'pDC', 'vSM'], ordered=False, dtype='category', name='cell_type')
[132]:
fstplt.plot_stackedbar_p(df_res, labels, colors, title, subtitle, fig_size=(6, 1.5), save_path='TranImp_nuclei_CRC16_Res2.pdf')
Res3
[133]:
coord3 = pd.read_csv('Rec3.csv')
polygon3 = np.stack([coord3['axis-1'], coord3['axis-0']]).T
print(polygon3)
[[51527.1453418 13318.83358355]
[52402.34626925 13318.83358355]
[52402.34626925 15006.67596 ]
[51527.1453418 15006.67596 ]]
[134]:
###############################################################
# ROI:DCIS 1 (Begin at left corner)
###############################################################
from matplotlib.path import Path
polygon = Path(polygon3)
st_crop_fineST_DCIS3 = HybISS[polygon.contains_points(HybISS.obsm["spatial"]), :].copy()
print(st_crop_fineST_DCIS3)
## ['B', 'Myeloid', 'T', 'Treg', 'fibroblast', 'tumor']].idxmax(axis=1)
st_crop_fineST_DCIS3.obs["Res3"]=pd.DataFrame(st_crop_fineST_DCIS3.obsm['TransImp_ct_pred'][["Adipocyte", "CAF", "CD4 T cell", "CD8 Cytotoxic T cell", "Endothelial",
"Enteric Glial", "Enterocyte", "Epithelial", "Fibroblast", "Goblet",
"Lymphatic Endothelial", "Macrophage", "Mast", "Mature B", "mRegDC",
"Myofibroblast", "Neuroendocrine", "Neutrophil", "pDC", "Pericytes",
"Plasma", "Proliferating Immune II", "QC_Filtered", "SM Stress Response",
"Smooth Muscle", "Tuft", "Tumor I", "Tumor II", "Tumor III", "Tumor V",
"Unknown III (SM)", "vSM"]].idxmax(axis=1))
print(st_crop_fineST_DCIS3)
AnnData object with n_obs × n_vars = 435 × 862
obs: 'array_row', 'array_col', 'cell_type'
var: 'gene_ids', 'feature_types', 'genome', 'mt', 'n_cells'
uns: 'pca', 'spatial'
obsm: 'TransImp_ct_pred', 'X_pca', 'spatial'
varm: 'PCs'
AnnData object with n_obs × n_vars = 435 × 862
obs: 'array_row', 'array_col', 'cell_type', 'Res3'
var: 'gene_ids', 'feature_types', 'genome', 'mt', 'n_cells'
uns: 'pca', 'spatial'
obsm: 'TransImp_ct_pred', 'X_pca', 'spatial'
varm: 'PCs'
[136]:
## save ROI 3 cell type
st_crop_fineST_DCIS3.write('CRC_square_016um_clty_Res3.h5ad')
[137]:
fig, ax = plt.subplots(1, 1, dpi=100)
sc.pl.spatial(st_crop_fineST_DCIS3, img_key=None, color="Res3", palette=ctype_hex_map, size=1.3, ax=ax)
fig.savefig('CRC16_TransImp_celty_Res3.pdf', format='pdf')
[138]:
p2_res = st_crop_fineST_DCIS3.obs['cell_type']
ss = pd.DataFrame(p2_res.value_counts()/p2_res.shape[0])
ss.columns = ['CRC16_TransImp_Res3:FineST']
df_res = ss.T
df_res
[138]:
| cell_type | CAF | Pericytes | Lymphatic Endothelial | Endothelial | SM Stress Response | Enteric Glial | vSM | Fibroblast | CD8 Cytotoxic T cell | Smooth Muscle | Unknown III (SM) | Macrophage | Mast | Adipocyte | Epithelial |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| CRC16_TransImp_Res3:FineST | 0.590805 | 0.144828 | 0.089655 | 0.050575 | 0.043678 | 0.016092 | 0.016092 | 0.011494 | 0.009195 | 0.006897 | 0.006897 | 0.004598 | 0.004598 | 0.002299 | 0.002299 |
[139]:
print(df_res.columns)
!pwd
df_res.to_csv('celltype_prop_CRC16_FineST_Res3.csv')
color_dict = ctype_hex_map
plot_columns = np.array(df_res.columns)
plot_colors = [color_dict[_] for _ in plot_columns]
labels = plot_columns
colors = plot_colors
labels = plot_columns#df_res.columns
colors = plot_colors#['#1D2F6F', '#8390FA', '#6EAF46', '#FAC748']
title = 'Video Game Sales By Platform and Region\n'
subtitle = 'Proportion of Games Sold by Region'
CategoricalIndex(['CAF', 'Pericytes', 'Lymphatic Endothelial', 'Endothelial',
'SM Stress Response', 'Enteric Glial', 'vSM', 'Fibroblast',
'CD8 Cytotoxic T cell', 'Smooth Muscle', 'Unknown III (SM)',
'Macrophage', 'Mast', 'Adipocyte', 'Epithelial'],
categories=['Adipocyte', 'CAF', 'CD8 Cytotoxic T cell', 'Endothelial', ..., 'SM Stress Response', 'Smooth Muscle', 'Unknown III (SM)', 'vSM'], ordered=False, dtype='category', name='cell_type')
/mnt/lingyu/nfs_share2/Python/FineST/FineST_local/Dataset/CRC16um/TransImp/ResultsRecs
[141]:
fstplt.plot_stackedbar_p(df_res, labels, colors, title, subtitle, fig_size=(6, 1.5), save_path='TranImp_nuclei_CRC16_Res3.pdf')
Res4
[142]:
coord4 = pd.read_csv('Rec4.csv')
polygon4 = np.stack([coord4['axis-1'], coord4['axis-0']]).T
print(polygon4)
[[56922.85899494 22298.90749711]
[59458.08797607 22298.90749711]
[59458.08797607 13602.39927774]
[56922.85899494 13602.39927774]]
[143]:
###############################################################
# ROI:DCIS 4 (Begin at left corner)
###############################################################
from matplotlib.path import Path
polygon = Path(polygon4)
st_crop_fineST_DCIS4 = HybISS[polygon.contains_points(HybISS.obsm["spatial"]), :].copy()
print(st_crop_fineST_DCIS4)
## ['B', 'Myeloid', 'T', 'Treg', 'fibroblast', 'tumor']].idxmax(axis=1)
st_crop_fineST_DCIS4.obs["Res4"]=pd.DataFrame(st_crop_fineST_DCIS4.obsm['TransImp_ct_pred'][["Adipocyte", "CAF", "CD4 T cell", "CD8 Cytotoxic T cell", "Endothelial",
"Enteric Glial", "Enterocyte", "Epithelial", "Fibroblast", "Goblet",
"Lymphatic Endothelial", "Macrophage", "Mast", "Mature B", "mRegDC",
"Myofibroblast", "Neuroendocrine", "Neutrophil", "pDC", "Pericytes",
"Plasma", "Proliferating Immune II", "QC_Filtered", "SM Stress Response",
"Smooth Muscle", "Tuft", "Tumor I", "Tumor II", "Tumor III", "Tumor V",
"Unknown III (SM)", "vSM"]].idxmax(axis=1))
print(st_crop_fineST_DCIS4)
AnnData object with n_obs × n_vars = 6353 × 862
obs: 'array_row', 'array_col', 'cell_type'
var: 'gene_ids', 'feature_types', 'genome', 'mt', 'n_cells'
uns: 'pca', 'spatial'
obsm: 'TransImp_ct_pred', 'X_pca', 'spatial'
varm: 'PCs'
AnnData object with n_obs × n_vars = 6353 × 862
obs: 'array_row', 'array_col', 'cell_type', 'Res4'
var: 'gene_ids', 'feature_types', 'genome', 'mt', 'n_cells'
uns: 'pca', 'spatial'
obsm: 'TransImp_ct_pred', 'X_pca', 'spatial'
varm: 'PCs'
[144]:
## save Res4 cell type
st_crop_fineST_DCIS4.write('CRC_square_016um_clty_Res4.h5ad')
... storing 'Res4' as categorical
[145]:
fig, ax = plt.subplots(1, 1, dpi=100)
sc.pl.spatial(st_crop_fineST_DCIS4, img_key=None, color="Res4", palette=ctype_hex_map, size=1.3, ax=ax)
fig.savefig('CRC16_TransImp_celty_Res4.pdf', format='pdf')
[146]:
p2_res = st_crop_fineST_DCIS4.obs['cell_type']
ss = pd.DataFrame(p2_res.value_counts()/p2_res.shape[0])
ss.columns = ['CRC16_TransImp_Res4:FineST']
df_res = ss.T
df_res
[146]:
| cell_type | Enterocyte | Goblet | Neutrophil | Plasma | Epithelial | Myofibroblast | Mature B | Tumor II | Enteric Glial | Endothelial | ... | vSM | Unknown III (SM) | Fibroblast | CAF | Adipocyte | Pericytes | Tuft | Smooth Muscle | SM Stress Response | pDC |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| CRC16_TransImp_Res4:FineST | 0.471746 | 0.106564 | 0.069101 | 0.067212 | 0.046907 | 0.041398 | 0.030537 | 0.015898 | 0.015426 | 0.012907 | ... | 0.004093 | 0.003778 | 0.00362 | 0.003463 | 0.002204 | 0.001731 | 0.001574 | 0.001259 | 0.000944 | 0.000472 |
1 rows × 32 columns
[147]:
print(df_res.columns)
# !pwd
df_res.to_csv('celltype_prop_CRC16_FineST_Res4.csv')
color_dict = ctype_hex_map
plot_columns = np.array(df_res.columns)
plot_colors = [color_dict[_] for _ in plot_columns]
labels = plot_columns
colors = plot_colors
labels = plot_columns#df_res.columns
colors = plot_colors#['#1D2F6F', '#8390FA', '#6EAF46', '#FAC748']
title = 'Video Game Sales By Platform and Region\n'
subtitle = 'Proportion of Games Sold by Region'
CategoricalIndex(['Enterocyte', 'Goblet', 'Neutrophil', 'Plasma', 'Epithelial',
'Myofibroblast', 'Mature B', 'Tumor II', 'Enteric Glial',
'Endothelial', 'Tumor V', 'Macrophage', 'Mast',
'Proliferating Immune II', 'Lymphatic Endothelial',
'Tumor III', 'CD4 T cell', 'QC_Filtered', 'mRegDC',
'CD8 Cytotoxic T cell', 'Tumor I', 'Neuroendocrine', 'vSM',
'Unknown III (SM)', 'Fibroblast', 'CAF', 'Adipocyte',
'Pericytes', 'Tuft', 'Smooth Muscle', 'SM Stress Response',
'pDC'],
categories=['Adipocyte', 'CAF', 'CD4 T cell', 'CD8 Cytotoxic T cell', ..., 'Unknown III (SM)', 'mRegDC', 'pDC', 'vSM'], ordered=False, dtype='category', name='cell_type')
[149]:
fstplt.plot_stackedbar_p(df_res, labels, colors, title, subtitle, fig_size=(6, 1.5), save_path='TranImp_nuclei_CRC16_Res4.pdf')
7. Res.s mark
[ ]:
import os
path = '/ssd/users/lingyu/Python/'
# path = '/mnt/lingyu/nfs_share2/Python/'
[123]:
from pathlib import Path
path_to_visium_bundle = Path(str(path)+"VisiumHD/Dataset/Colon_Cancer/square_016um/").expanduser()
from skimage.io import imread as skimread
visium_high_res_image = skimread(
path_to_visium_bundle / "spatial" / "tissue_hires_image.png"
)
[124]:
import json
with open(path_to_visium_bundle / "spatial" / "scalefactors_json.json") as file:
visium_scale_factors = json.load(file)
[126]:
from matplotlib import pyplot as plt
from matplotlib.patches import Polygon
fig, ax = plt.subplots(dpi=100)
ax.imshow(visium_high_res_image)
polygon = Polygon(polygon1 * visium_scale_factors["tissue_hires_scalef"], fill=False, edgecolor='white')
ax.add_patch(polygon)
# polygon = Polygon(polygon2 * visium_scale_factors["tissue_hires_scalef"], fill=False, edgecolor='white')
ax.add_patch(polygon)
polygon = Polygon(polygon3 * visium_scale_factors["tissue_hires_scalef"], fill=False, edgecolor='white')
ax.add_patch(polygon)
polygon = Polygon(polygon4 * visium_scale_factors["tissue_hires_scalef"], fill=False, edgecolor='white')
ax.add_patch(polygon)
plt.show()
[127]:
## save
# os.chdir(str(path)+'VisiumHD/Dataset/Colon_Cancer/ResultsRecs')
# fig.savefig('Visium_Recs_CRC16_mark.pdf', format='pdf')