❝1、《KS科研分享与服务》公众号有QQ交流群,进入门槛是20元(完全是为了防止白嫖党,请理解),请考虑清楚。群里有免费推文的注释代码和示例数据(终身拥有),没有付费内容,群成员福利是购买单个付费内容半价!
2、《KS科研分享与服务》微信VIP群只针对购买打包代码的小伙伴(公众号所有付费内容合集)!微信群不是单独的,是对于打包的人答疑解惑和交流的平台、群成员专享视频教程,帖子提前发布,以及其他更多福利!
点击:→ 加入微信vip群:2024-2025《KS科研分享与服务》付费内容打包集合
3、需进QQ群或者打包代码入微信VIP的小伙伴请添加作者微信了解,请备注目的,除此之外请勿添加,谢谢!
详情请联系作者:
❞
devtools::install_github("jinworks/CellChat")
library(CellChat)
#version 2.1.2
setwd("D:\\KS项目\\公众号文章\\cellchat受配体数据库更新及自定义")
#Step 1: 加载数据库文件
interaction_input <- read.csv(file = './cpdb数据库文件/interaction_input.csv')
complex_input <- read.csv(file = './cpdb数据库文件/complex_input.csv', row.names = 1)
geneInfo <- read.csv(file = './cpdb数据库文件/gene_input.csv')
#Step 2: 修改文件,以期让其适用于cellchat
#gene information---需要有一列列名是“Symbol”
geneInfo$Symbol <- geneInfo$hgnc_symbol#将基因名那一列命名为Symbol
geneInfo <- select(geneInfo, -c("ensembl"))
geneInfo <- unique(geneInfo)#去除重复行
#interaction_input---获取受配体信息(选择有用的,去除冗余的信息)
# get the ligand information---添加一列ligand,并将其转化为symbol(大家熟悉的形式,cpdb这里写的是protein uniprot)
idx_partnerA <- match(interaction_input$partner_a, geneInfo$uniprot)
idx.use <- !is.na(idx_partnerA)
interaction_input$ligand <- interaction_input$partner_a
interaction_input$ligand[idx.use] <- geneInfo$hgnc_symbol[idx_partnerA[idx.use]]
# get the receptor information---添加一列receptor
idx_partnerB <- match(interaction_input$partner_b, geneInfo$uniprot)
idx.use <- !is.na(idx_partnerB)
interaction_input$receptor <- interaction_input$partner_b
interaction_input$receptor[idx.use] <- geneInfo$hgnc_symbol[idx_partnerB[idx.use]]
# get other information
interaction_input$interaction_name <- interaction_input$interactors
interaction_input$interaction_name_2 <- interaction_input$interaction_name
interaction_input$pathway_name <- interaction_input$classification
interaction_input$pathway_name <- gsub(".*by ", "", interaction_input$pathway_name)
interaction_input$annotation <- interaction_input$directionality
interaction_input <- select(interaction_input, -c("partner_a","partner_b","protein_name_a","protein_name_b","interactors","classification","directionality"))
##other_info---prepare complex_input file
complexsubunits <- dplyr::select(complex_input, starts_with("uniprot"))
for (i in 1:ncol(complexsubunits)) {
idx_complex <- match(complex_input[,paste0("uniprot_",i)], geneInfo$uniprot)
idx.use <- !is.na(idx_complex)
complex_input[idx.use,paste0("uniprot_",i)] <- geneInfo$hgnc_symbol[idx_complex[idx.use]]
}
colnames(complex_input)[1:ncol(complexsubunits)] <- paste0("subunit_",seq_len(ncol(complexsubunits)))
complex_input <- dplyr::select(complex_input, starts_with("subunit"))
## prepare other information
other_info <- list(complex = complex_input)
#Step 3: 更新数据库
#这里为什么trim.pathway选择T,一定要让interactions有pathways注释,这是因为:
#Warning: If no pathway information of each L-R pair is provided, all pathway-level analysis from CellChat cannot be used, such as computeCommunProb!
#仅仅只有cpdb数据库
db.new.cpdb <- updateCellChatDB(db = interaction_input, gene_info = geneInfo, other_info = other_info, trim.pathway = T,species_target = "human",merged = F)
#将cpdb数据库和cellchat合并
db.new.cpdb_ccdb <- updateCellChatDB(db = interaction_input, gene_info = geneInfo, other_info = other_info, trim.pathway = T,species_target = "human",merged = T)
#保存文件,后续分析直接使用
save(db.new.cpdb, file = 'db_new_cpdb.RData')
save(db.new.cpdb_ccdb, file = 'db.new.cpdb_ccdb.RData')
CellChatDB <- CellChatDB.mouse
class(CellChatDB)#它是一个list,里面包含4个文件,即4个dataframe
# [1] "list"
interaction_input <- CellChatDB$interaction
complex_input <- CellChatDB$complex
cofactor_input <- CellChatDB$cofactor
geneInfo <- CellChatDB$geneInfo
write.csv(interaction_input, file = "interaction_input_CellChatDB.csv")
write.csv(complex_input, file = "complex_input_CellChatDB.csv")
write.csv(cofactor_input, file = "cofactor_input_CellChatDB.csv")
write.csv(geneInfo, file = "geneInfo_input_CellChatDB.csv")
#Excel中打开,就能具体查看到底有什么信息
#然后直接修改即可
#文件按照要求修改之后,读入对应的文件
interaction_input <- read.csv(file = 'interaction_input_CellChatDB.csv', row.names = 1)
complex_input <- read.csv(file = 'complex_input_CellChatDB.csv', row.names = 1)
cofactor_input <- read.csv(file = 'cofactor_input_CellChatDB.csv', row.names = 1)
geneInfo <- read.csv(file = ' geneInfo_input_CellChatDB.csv', row.names = 1)
#cellchat数据库是list形式,所以将修改好的文件(4个dataframe)以list形式储存即可
CellChatDB <- list()
CellChatDB$interaction <- interaction_input
CellChatDB$complex <- complex_input
CellChatDB$cofactor <- cofactor_input
CellChatDB$geneInfo <- geneInfo