【视频教程】Cellphonedb v5更新:python版Scanpy单细胞分析后续之cpdb细胞通讯

学术   2024-11-05 16:00   重庆  

偷偷问一下,关注了吗

内容获取


1、购买打包合集(《KS科研分享与服务》付费内容打包集合),价格感人,可以加入微信VIP群(答疑交流群,甚至有小伙伴觉得群比代码更好),可以获取建号以来所有内容,群成员专享视频教程,提前更新,其他更多福利!


2、《KS科研分享与服务》公众号有QQ群,进入门槛是20元(完全是为了防止白嫖党,请理解),请考虑清楚。群里有免费推文的注释代码和示例数据(终身拥有),没有付费内容,群成员福利是购买单个付费内容半价!


需要者详情请联系作者(非需要者勿扰,处理太费时间):


这个问题来源于群里小伙伴的咨询。之前我们演示cpdb分析的时候侧重的是R分析seurat对象准备input data,小伙伴提到“我单细胞一开始就是用scanpy分析的,怎么进行CPDB分析呢?”,其实之前的cellphonedb V5教程里面我们已经提到了(单细胞通讯分析之Cellphonedbv5完整内容(视频教程))。然而我们前面的帖子也分享了h5ad转seurat的方式(玩转单细胞(16):Scanpy单细胞h5ad数据转化为Seurat对象),以及seurat转h5ad的多个方式(Seurat来源单细胞PAGA分析(seurat转h5ad)),所以他们之间不存在隔阂了,搞不定互相转化就好了。

虽然我们scanpy的教程还没有做好,但是,我想还是可以先进行一下scanpy后续之cpdb分析。我们提供了多种方式准备input data for cpdb,同时也算是对cpdb教程的更新,提供了cpdb结果数据处理整合的简单函数(自写),也做了一些可视化,全部提供视频解说,所以,之前购买过单细胞通讯分析之Cellphonedbv5完整内容(视频教程))的小伙伴,联系作者免费获取本教程。本内容已发布微信VIP群,请自行下载!

要提取h5ad数据,需要了解anndata结构:


cpdb分析counts_file_path参数可以是表达矩阵,也可以是单细胞分析结果h5ad对象。但是矩阵是normalize后的,我们演示使用的数据还是scanpy官网教程中PBMC的数据,数据没有任何生物学意义,仅仅作为cpdb流程演示。
import pandas as pdimport scanpy as scimport numpy as np

#load dataadata = sc.read_h5ad('./pbmc3k.h5ad')adata = adata.raw.to_adata()adata
adata_cpdb = adata[~adata.obs['leiden'].isin(['Megakaryocytes'])]
#normalizecounts = adata_cpdb.X.toarray()adata_cpdb.X = countssc.pp.normalize_per_cell(adata_cpdb, counts_per_cell_after=1e4)adata_cpdb.layers["norm"] = adata_cpdb.X
counts = pd.DataFrame(data=adata_cpdb.layers["norm"], index=adata_cpdb.obs.index.tolist(), columns=adata_cpdb.var.index.tolist()).Tcounts.index.name = "Gene"
#save metameta = pd.DataFrame(data=adata_cpdb.obs['leiden'].tolist(), index=adata_cpdb.obs.index.tolist(), columns=["Cell type"])meta.index.name = "Cell"meta
CPDB分析:input count matrix & meta!
import numpy as npimport pandas as pdimport scanpy as scimport anndataimport osimport sysfrom scipy import sparse
#Download database from sourcefrom IPython.display import HTML, displayfrom cellphonedb.utils import db_releases_utilsdisplay(HTML(db_releases_utils.get_remote_database_versions_html()['db_releases_html_table']))
cpdb_version = 'v5.0.0'cpdb_target_dir = os.path.join('./', cpdb_version)
from cellphonedb.utils import db_utilsdb_utils.download_database(cpdb_target_dir, cpdb_version)
sc.settings.verbosity = 3  # verbosity: errors (0), warnings (1), info (2), hints (3)sc.logging.print_versions()sc.settings.set_figure_params(dpi=100)  # low dpi (dots per inch) yields small inline figures
cpdb_file_path = './v5.0.0/cellphonedb.zip'meta_file_path = './meta_cpdb.txt'counts_file_path = './count_cpdb.txt'out_path = './test1'
from cellphonedb.src.core.methods import cpdb_statistical_analysis_method
cpdb_results1 = cpdb_statistical_analysis_method.call( cpdb_file_path = cpdb_file_path, meta_file_path = meta_file_path, counts_file_path = counts_file_path, counts_data = 'hgnc_symbol', #active_tfs_file_path = active_tf_path, #microenvs_file_path = microenvs_file_path, score_interactions = True, iterations = 1000, threshold = 0.1, threads = 5, debug_seed = 42, result_precision = 3, pvalue = 0.05, separator = '|', debug = False, output_path = out_path )
CPDB分析:input h5ad & meta!
cpdb_file_path = './v5.0.0/cellphonedb.zip'meta_file_path = './meta_cpdb.txt'counts_file_path = './adata_cpdb.h5ad'out_path = './test2'
from cellphonedb.src.core.methods import cpdb_statistical_analysis_method
cpdb_results2 = cpdb_statistical_analysis_method.call( cpdb_file_path = cpdb_file_path, meta_file_path = meta_file_path, counts_file_path = counts_file_path, counts_data = 'hgnc_symbol', #active_tfs_file_path = active_tf_path, #microenvs_file_path = microenvs_file_path, score_interactions = True, iterations = 1000, threshold = 0.1, threads = 5, debug_seed = 42, result_precision = 3, pvalue = 0.05, separator = '|', debug = False, output_path = out_path )
整合结果:两种方式一样的结果!这种结果可以导出在R或者py中进行各种可视化!
cpdb_pbmc = summary_cpdb_results(cpdb_results1)cpdb_pbmc.head()

利用cpdb里面的函数,提取结果进行一些可视化!
from cellphonedb.utils import search_utils
search_results = search_utils.search_analysis_results( query_cell_types_1 = ['CD4 T'], # List of cells 1, will be paired to cells 2 (list or 'All'). query_cell_types_2 = ['CD14 Monocytes', 'FCGR3A Monocytes'], # List of cells 2, will be paired to cells 1 (list or 'All'). # query_genes = ['TGFBR1'], # filter interactions based on the genes participating (list). #query_interactions = ['CSF1_CSF1R'], # filter intereactions based on their name (list). significant_means = cpdb_results1['significant_means'], # significant_means file generated by CellphoneDB. deconvoluted = cpdb_results1['deconvoluted'], # devonvoluted file generated by CellphoneDB. interaction_scores = cpdb_results1['interaction_scores'], # interaction score generated by CellphoneDB. query_minimum_score = 50, # minimum score that an interaction must have to be filtered. separator = '|', # separator (default: |) employed to split cells (cellA|cellB). long_format = True # converts the output into a wide table, removing non-significant interactions)
search_results.head()

觉得我们分享有些用的,点个赞再走呗!

关注我们获取精彩内容:


关注不迷路:扫描下面二维码关注公众号!
B站视频号链接https://space.bilibili.com/471040659?spm_id_from=333.1007.0.0




关注 KS科研分享与服务,

认清正版优质内容和服务!

优质内容持续输出,物超所值!

合作联系:ks_account@163.com

新的板块-重要通知-双向选择

KS科研分享与服务
科研学习交流于分享,生信学习笔记,科研经历和生活!
 最新文章