rm (list = ls ()) #清除所有变量
library(KEGGREST)
# 设置工作目录并读取数据
setwd("C:\\Users\\Administrator\\Desktop\\New_microtable\\HUMAnN") # 根据实际路径修改
pathways_df <- read.table("kegg_abundance.txt", header = TRUE, stringsAsFactors = FALSE)
# 初始化结果数据框
pathways_df$pathway_name <- NA
pathways_df$pathway_description <- NA
pathways_df$pathway_class <- NA
pathways_df$pathway_map <- NA
#获取每个Pathway的详细信息
for (i in 1:nrow(pathways_df)) {
pathway_id <- pathways_df$Pathway[i]
# 获取Pathway信息
pathway_info <- tryCatch(keggGet(pathway_id)[[1]], error = function(e) NULL)
# 如果获取到信息,则提取并添加到结果数据框中
if (!is.null(pathway_info)) {
pathways_df$pathway_name[i] <- ifelse(!is.null(pathway_info$NAME), pathway_info$NAME, NA)
pathways_df$pathway_description[i] <- ifelse(!is.null(pathway_info$DESCRIPTION), pathway_info$DESCRIPTION, NA)
pathways_df$pathway_class[i] <- ifelse(!is.null(pathway_info$CLASS), pathway_info$CLASS, NA)
pathways_df$pathway_map[i] <- paste0("http://www.kegg.jp/kegg-bin/show_pathway?", pathway_id)
}
}
# 保存结果到新的文本文件
write.table(pathways_df, file = "pathways_with_details.txt", quote = FALSE, row.names = FALSE, sep = "\t")
标签:KEGG,读取数据,数据库,获取,链接,描述 From: https://www.cnblogs.com/wzbzk/p/18287324