R:連結KEGG資料庫獲取更多描述資訊

王哲MGG_AI發表於2024-07-06
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")

相關文章