Ken Furudate
import scanpy as sc
import anndata as ad
import squidpy as sq
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
import matplotlib as mpl
%matplotlib inline
import matplotlib.font_manager
plt.rcParams['font.sans-serif'] = ['Arial'] + plt.rcParams['font.sans-serif']
plt.rcParams["font.size"] = 20
import os
import seaborn as sns
from pathlib import Path
import pickle
def pickle_load(path):
with open(path, mode='rb') as f:
data = pickle.load(f)
return data
from IPython.core.display import display, HTML
display(HTML("<style>.container { width:80% !important; }</style>"))
import warnings
warnings.filterwarnings('ignore')
sc.logging.print_header()
print(f"squidpy=={sq.__version__}")
datadir = "/data/spatial/"
in_f = "integrated_data.h5ad"
data = sc.read_h5ad(datadir + in_f)
set(data.obs['sample'])
data.obs
adata = data[data.obs['sample']=="A"]
bdata = data[data.obs['sample']=="B"]
cdata = data[data.obs['sample']=="C"]
ddata = data[data.obs['sample']=="D"]
adata = adata[adata.obs['category']=="tumor"]
bdata = bdata[bdata.obs['category']=="tumor"]
cdata = cdata[cdata.obs['category']=="tumor"]
ddata = ddata[ddata.obs['category']=="tumor"]
adata.uns["spatial"] = pickle_load(datadir + 'adata_uns_spatial.pickle')
bdata.uns["spatial"] = pickle_load(datadir + 'bdata_uns_spatial.pickle')
cdata.uns["spatial"] = pickle_load(datadir + 'cdata_uns_spatial.pickle')
ddata.uns["spatial"] = pickle_load(datadir + 'ddata_uns_spatial.pickle')
sample_lst = ["A", "B", "C", "D"]
def set_param(input_data, sample):
scale = input_data.uns['spatial'][f"{sample}"]['scalefactors']['tissue_hires_scalef']
img = sq.im.ImageContainer(input_data.uns['spatial'][f"{sample}"]['images']['hires'],
scale=scale,
library_id=f"{sample}")
img.show()
return scale, img
scale_a, img_a = set_param(adata, "A2")
scale_b, img_b = set_param(bdata, "B2")
scale_c, img_c = set_param(cdata, "C2")
scale_d, img_d = set_param(ddata, "D2")
select_col="category"
select_g = sorted(list(set(adata.obs[select_col])))
select_g
res = sq.gr.ligrec(
adata,
n_perms=1000,
threshold=0.01,
cluster_key=select_col,
copy=True,
use_raw=False,
transmitter_params={"categories": "ligand"},
receiver_params={"categories": "receptor"},
interactions_params={'resources': 'CellPhoneDB'},
corr_method="fdr_bh",
corr_axis="clusters",
alpha=0.05,
seed=42,
n_jobs=4
)
res["means"]
res_means_T = res["means"].T
order_ = ["tumor", "peritumor", "non_tumor"]
new_idx = pd.MultiIndex.from_product([order_ ,
order_ ],
names=res_means_T.index.names
)
res_df = pd.concat([res_means_T.reindex(new_idx).T,
res["pvalues"].T.reindex(new_idx).T.add_suffix("_pval")],
axis=1)
res_df
res["means"] = res_means_T.reindex(new_idx).T
res["means"]
res["pvalues"] = res["pvalues"].T.reindex(new_idx).T
res["pvalues"]
res["metadata"]
sq.pl.ligrec(res,
source_groups='tumor',
means_range=(0.95, np.inf),
pvalue_threshold=0.05,
alpha=1e-4,
show_colorbar=True,
remove_nonsig_interactions=True,
)
res = sq.gr.ligrec(
bdata,
n_perms=1000,
threshold=0.01,
cluster_key=select_col,
copy=True,
use_raw=False,
transmitter_params={"categories": "ligand"},
receiver_params={"categories": "receptor"},
interactions_params={'resources': 'CellPhoneDB'},
corr_method="fdr_bh",
corr_axis="clusters",
alpha=0.05,
seed=42,
n_jobs=4
)
res_means_T = res["means"].T
order_ = ["tumor", "peritumor", "non_tumor"]
new_idx = pd.MultiIndex.from_product([order_ ,order_ ],
names=res_means_T.index.names
)
res_df = pd.concat([res_means_T.reindex(new_idx).T,
res["pvalues"].T.reindex(new_idx).T.add_suffix("_pval")],
axis=1)
res["means"] = res_means_T.reindex(new_idx).T
res["pvalues"] = res["pvalues"].T.reindex(new_idx).T
sq.pl.ligrec(res,
source_groups='tumor',
means_range=(0.95, np.inf),
pvalue_threshold=0.05,
alpha=1e-4,
show_colorbar=True,
remove_nonsig_interactions=True,
)
res = sq.gr.ligrec(
ddata,
n_perms=1000,
threshold=0.01,
cluster_key=select_col,
copy=True,
use_raw=False,
transmitter_params={"categories": "ligand"},
receiver_params={"categories": "receptor"},
interactions_params={'resources': 'CellPhoneDB'},
corr_method="fdr_bh",
corr_axis="clusters",
alpha=0.05,
seed=42,
n_jobs=4
)
res_means_T = res["means"].T
order_ = ["tumor", "peritumor", "non_tumor"]
new_idx = pd.MultiIndex.from_product([order_ ,order_ ],
names=res_means_T.index.names
)
res_df = pd.concat([res_means_T.reindex(new_idx).T,
res["pvalues"].T.reindex(new_idx).T.add_suffix("_pval")],
axis=1)
res["means"] = res_means_T.reindex(new_idx).T
res["pvalues"] = res["pvalues"].T.reindex(new_idx).T
sq.pl.ligrec(res,
source_groups='tumor',
means_range=(0.95, np.inf),
pvalue_threshold=0.05,
alpha=1e-4,
show_colorbar=True,
remove_nonsig_interactions=True,
)