升级R v4.0.0与topGO v2.40.0后GO富集代码报错求助
解决topGO v2.40.0/R v4.0.0中构建GOdata时的"argument is of length zero"报错
这个报错通常是因为新版本topGO在处理GO注释映射时,对基因ID类型的要求更严格,或是与最新版注释包(如org.Hs.eg.db)的交互逻辑发生了变化。结合你的代码,我整理了几个可行的解决方案:
解决方案1:明确指定注释函数的ID类型
在调用annFUN.org时,显式声明基因ID的类型(你的reference使用的是org.Hs.eg.db的默认keys,也就是ENTREZID)。修改后的代码如下:
require(topGO) require(org.Hs.eg.db) reference = setNames(rep(0, 1000), keys(org.Hs.eg.db)[1:1000]) reference[1:50] = 1 GOdata <- new( "topGOdata", description = "Simple session", ontology = "BP", allGenes = reference, geneSel = function(set){return(set==1)}, annot = annFUN.org, mapping = "org.Hs.eg.db", ID = "ENTREZID" ) # 新增该行,明确ID类型
新版本的annFUN.org可能不再默认识别ID类型,显式指定后可避免根节点判断时出现空值。
解决方案2:更新/重新安装相关包
确保你的org.Hs.eg.db和topGO都是对应Bioconductor版本的最新版(R4.0对应Bioconductor 3.11,topGO v2.40.0属于这个版本)。可以用以下命令强制重装:
if (!requireNamespace("BiocManager", quietly = TRUE)) install.packages("BiocManager") BiocManager::install(c("topGO", "org.Hs.eg.db"), force = TRUE)
版本升级后包的依赖关系可能出现异常,强制重装通常能修复这类问题。
解决方案3:手动构建基因-GO映射列表
如果前两种方法无效,可以绕过annFUN.org,自行构建基因到GO术语的映射后再传递给topGOdata:
# 构建ENTREZID到GO BP的映射 go_map <- mapIds(org.Hs.eg.db, keys = names(reference), keytype = "ENTREZID", column = "GOALL") # 过滤掉无GO注释的基因 go_map <- go_map[!is.na(go_map)] # 转换为topGO需要的列表格式 gene2GO <- split(names(go_map), go_map) # 使用自定义映射构建GOdata GOdata <- new( "topGOdata", description = "Simple session", ontology = "BP", allGenes = reference, geneSel = function(set){return(set==1)}, annot = annFUN.gene2GO, gene2GO = gene2GO )
这种方法更灵活,直接控制注释映射过程,能规避annFUN.org在新版本中的兼容性问题。
内容的提问来源于stack exchange,提问作者randr
相关产品推荐
相关产品推荐

