1换薄、cellphoneBD Rstudio 前期準(zhǔn)備工作
建立輸入文件
setwd("D:/D/metropolypus/FastMNN/cellphoneDB")
###根據(jù)colname名字區(qū)分BD單細(xì)胞和10X單細(xì)胞測序询张,提取10X單細(xì)胞測序數(shù)據(jù)
colnames(sce)
sce$tech <- sapply(colnames(x = sce), function(x) unlist(strsplit(x, "_"))[1])
sce$IF=sapply(sce$tech, function(x) unlist(nchar(x)>10))
table(sce$IF)
Idents(sce)<-sce$IF
ten=subset(sce,idents = c("TRUE"))
table(ten$sample)
subcell <- sample(colnames(ten),10000)
sce <- ten[,subcell]
library(SeuratDisk)
# 將Seurat對(duì)象保存為h5Seurat文件
SaveH5Seurat(sce, filename = "seurat_obj.h5Seurat")
# 轉(zhuǎn)換為h5ad格式
Convert("seurat_obj.h5Seurat", dest = "h5ad")
# 安裝pandas和anndata
# pip install pandas anndata
import pandas as pd
import anndata
# 讀取CSV文件
expression_matrix = pd.read_csv("/mnt/d/D/temp/cellphoneDB/expression_matrix.csv", index_col=0)
# 創(chuàng)建AnnData對(duì)象
adata = anndata.AnnData(X=expression_matrix)
# 保存為.h5ad文件
adata.write("/mnt/d/D/temp/cellphoneDB/sce.h5ad")
print(count_cells[:10])
sce$Cell = rownames(sce@meta.data)
df = sce@meta.data[,c("Cell","celltype")]
colnames(df) = c("Cell","cell_type")
df$Cell <- gsub("-", ".", df$Cell)
write.table(df, file = "meta1.txt",sep = "\t",quote = F,row.names = F)
sce$Cell = rownames(sce@meta.data)
df = sce@meta.data[,c("celltype","group")]
colnames(df) = c("cell_type","microenvironment")
write.table(df, file = "microenvironment.txt",sep = "\t",quote = F,row.names = F)
DEG.Au <-dplyr::filter(DEG.A,DEG.A$avg_log2F >0.25)
write.table(DEG.Au, file = "degs_file.txt",sep = "\t",quote = F,row.names = F)
2镊叁、CellPhoneDB python中運(yùn)行
#https://github.com/ventolab/CellphoneDB
#https://github.com/ventolab/CellphoneDB/blob/master/NatureProtocols2024_case_studies/CaseExample1_differentiation/analysis_method3_CellSign_microenvironments.ipynb
conda create -n cpdb python=3.8
source activate
conda activate cpdb
source activate cpdb
cd /mnt/d/D/temp/cellphoneDB
import pandas as pd
# 加載 meta 文件
meta_df = pd.read_csv("/mnt/d/D/temp/cellphoneDB/meta1.txt", sep="\t")
meta_clusters = set(meta_df['cell_type']) # 使用meta文件中實(shí)際使用的列名
# 加載 DEGs 文件(需要早excel表格中手動(dòng)調(diào)節(jié)DEGs每列的順序需要和example_data中DEGs的順序保持一致)
degs_df = pd.read_csv("/mnt/d/D/temp/cellphoneDB/degs_file.txt", sep="\t")
degs_clusters = set(degs_df.iloc[:, 5]) # 取DEGs文件的第一列
# 檢查 DEGs 中不存在于 meta 中的集群/細(xì)胞類型
missing_clusters = degs_clusters - meta_clusters
if missing_clusters:
print("Missing clusters in meta:", missing_clusters)
else:
print("All clusters match.")
from cellphonedb.src.core.methods import cpdb_degs_analysis_method
cpdb_results = cpdb_degs_analysis_method.call(
cpdb_file_path = "/mnt/d/D/temp/cellphoneDB/cellphonedb.zip",
meta_file_path = "/mnt/d/D/temp/cellphoneDB/meta1.txt",
counts_file_path = "/mnt/d/D/temp/cellphoneDB/sce.h5ad",
degs_file_path = "/mnt/d/D/temp/cellphoneDB/degs_file.txt",
counts_data = 'hgnc_symbol',
threshold = 0.1,
output_path = "/mnt/d/D/temp/cellphoneDB/out/")
##代碼運(yùn)行時(shí)缩宜,所有表格列名的位置都要和D:\D\temp\cellphonedb\CellphoneDB-master\CellphoneDB-master\example_data中順序一樣
import anndata
adata = anndata.read_h5ad("/mnt/d/D/temp/cellphoneDB/seurat_obj.h5ad")
import matplotlib.pyplot as plt
# 調(diào)用繪制熱圖的函數(shù)
kpy.plot_cpdb_heatmap(
pvals=cpdb_results['relevant_interactions'],
degs_analysis=True,
figsize=(5, 5),
title="Sum of significant interactions"
)
# 保存圖像到指定路徑
output_path = "/mnt/d/D/temp/cellphoneDB/heatmap.png"
plt.savefig(output_path, format='png', dpi=300, bbox_inches='tight')
# 關(guān)閉當(dāng)前圖以釋放內(nèi)存
plt.close()
from plotnine import facet_wrap
# 創(chuàng)建圖形對(duì)象
p = kpy.plot_cpdb(
adata=adata,
cell_type1=".",
cell_type2="Mast_cells",
means=cpdb_results['means'],
pvals=cpdb_results['relevant_interactions'],
celltype_key="celltype",
figsize=(12, 8),
title="Interactions with Mast cells",
max_size=6,
highlight_size=0.75,
degs_analysis=True,
standard_scale=False
)
# 添加facet_wrap
final_plot = p + facet_wrap("~ classification", ncol=2)
# 使用 plotnine 的 draw 方法的猛,然后調(diào)用保存
output_path = "/mnt/d/D/temp/cellphoneDB/interaction_plot.mastcells.pdf"
from plotnine import ggplot
final_plot.save(filename=output_path, format='pdf', dpi=600, height=20, width=12, units='in')
p=kpy.plot_cpdb(
adata = adata,
cell_type1 = "Mast_cells",
cell_type2 = ".",
means = cpdb_results['means'],
pvals = cpdb_results['relevant_interactions'],
celltype_key = "celltype",
# genes = ["JAG1"],
figsize = (10,15),
title = "Interactions between oocytes and granulosa in the follicles",
max_size = 5,
highlight_size = 0.75,
degs_analysis = True,
standard_scale = False,
# interaction_scores = cpdb_results['interaction_scores'],
scale_alpha_by_interaction_scores=True,
)
output_path = "/mnt/d/D/temp/cellphoneDB/interaction_mastcell.allcells1.pdf"
from plotnine import ggplot
p.save(filename=output_path, format='pdf', dpi=600, height=8, width=8, units='in')
p.save(filename=output_path, format='pdf', dpi=300)
# 關(guān)閉當(dāng)前圖以釋放內(nèi)存
plt.close()
p = kpy.plot_cpdb(
adata = adata,
cell_type1 = ".",
cell_type2 = "Mast_cells",
means = cpdb_results['means'],
pvals = cpdb_results['relevant_interactions'],
celltype_key = "celltype",
genes = ["DHRS9"],
figsize = (10,5),
title = "Interactions involving DHRS9",
max_size = 5,
highlight_size = 0.75,
degs_analysis = True,
standard_scale = False
)
# 使用 plotnine 的 draw 方法安皱,然后調(diào)用保存
output_path = "/mnt/d/D/temp/cellphoneDB/interaction_plotDHRS9-PGR.pdf"
final_plot.save(filename=output_path, format='pdf', dpi=300)
# 關(guān)閉當(dāng)前圖以釋放內(nèi)存
plt.close()
p = kpy.plot_cpdb(
adata = adata,
cell_type1 = "unciliated_EpC|ciliated_EpC",
cell_type2 = "Mast_cells",
means = cpdb_results['means'],
pvals = cpdb_results['relevant_interactions'],
celltype_key = "celltype",
figsize = (8, 5),
title = "Interactions between oocytes and granulosa in the follicles \n supported by downstream TF activity",
max_size = 6,
highlight_size = 0.75,
degs_analysis = True,
standard_scale = False,
cellsign = cpdb_results['CellSign_active_interactions'],
filter_by_cellsign = True,
scale_alpha_by_cellsign = False
)