





#! usr/bin/python
####zhaoyunfei
####20250411
import scanpy as sc
import pandas as pd
import CellNeighborEX
import os
from cellphonedb.src.core.methods import cpdb_statistical_analysis_method
adata = sc.read("scimap.xenium.h5ad")
df = CellNeighborEX.neighbors.create_dataframe(adata, coord_key='spatial', celltype_key='subtype')
closest_distances = CellNeighborEX.neighbors.calculate_closest_distance(df, save=True)
# Find immediate neighbors using Delaunay triangulation and retrieve the spatial connectivity matrix.
matrix = CellNeighborEX.neighbors.detect_neighbors(adata, coord_key='spatial', type='generic', knn=None, radius_value=None, delaunay=True)
# Calculate the number of neighbors for each cell.
neiNum = CellNeighborEX.neighbors.get_neighbors(matrix)
df_processed = CellNeighborEX.neighbors.process_dataframe(df, matrix, neiNum, save=True)
# All categorzied files (index_, matchComb_, neiCombUnique_, prop_ .csv) are saved in the "categorized_data folder" in the root directory.
CellNeighborEX.categorization.generate_input_files(data_type = "Image", df = df_processed, sample_size=30, min_sample_size=1)
# Normalize gene expression data.
barcodes = df_processed['barcode'].tolist()
adata = adata[barcodes, :]
sc.pp.normalize_total(adata, target_sum=1e4) # normlization
sc.pp.log1p(adata)
# Save the data into dataframes.
index_name = adata.obs.index.name
df_cell_id = pd.DataFrame(adata.obs.index)
df_cell_id = df_cell_id.rename(columns={index_name: 0})
var_name = adata.var.index.name
df_gene_name = pd.DataFrame(adata.var.index)
df_gene_name = df_gene_name.rename(columns={var_name: 0})
df_log_data = adata.T.to_df()
###Perform neighbor-dependent gene expression analysis
data_type = "Image" # Image: image-based ST data, NGS: NGS-based ST data
lrCutoff = 0.4
pCutoff = 0.01
pCutoff2 = 0.05
direction = 'up'
normality_test = False
top_genes = 30
path_categorization = './categorized_data/'
DEG_list = CellNeighborEX.DEanalysis.analyze_data(df_cell_id, df_gene_name, df_log_data, path_categorization, data_type, lrCutoff, pCutoff, pCutoff2, direction, normality_test, top_genes, save=True)原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。