以 function() 及 lapply 處理多個列表

26 views
Skip to first unread message

Iris.c

unread,
Aug 22, 2023, 4:11:35 AM8/22/23
to R軟體使用者論壇
Hi,

附圖是我的資料型態。
我想用 function() 及 lapply 針對多個基因列表進行功能性分析及畫圖,下面是我的 code。

----------------------------------------------------------------------------------------------
# 匯入檔案
data_dir <- '/Users/irischang/Downloads/acidic_FPH'
SampleList <- list("C1_up_1", "C1_up_2", "C1_up_3", "C2_up_1", "C2_up_2", "C2_up_3")

for (i in SampleList){
  a <- paste0(i, ".csv")
  P.i <- file.path(data_dir, a)
  nam <- paste0(i)
  assign(nam, read.csv(P.i, header = TRUE))
}

# 建立功能性分析 function
doGO <- function(x){
  x <- data.frame(x)
  r <- regexpr("AT[0-9]+G[0-9]+", x$AGI) # 把 AGI 小數點後編號移除
  x$AGI[which(r != -1)] <- regmatches(x$AGI, r) 
 
  x.df <- bitr(x$AGI, fromType = "TAIR", # 把 AGI 編號轉換為 ENTREZID
               toType = "ENTREZID",
               OrgDb = org.At.tair.db)
  xgo <- enrichGO(gene          = x.df$ENTREZID, # 進行功能性分析
                  OrgDb         = org.At.tair.db,
                  ont           = "BP",
                  pAdjustMethod = "BH",
                  pvalueCutoff  = 1.0,
                  readable      = FALSE)
  if (length(xgo) == 1) { # 如果無顯著結果則回傳 NULL
    return(NULL)
  }else{
    d <- godata('org.At.tair.db', ont="BP") # 針對結果畫圖
    xgo2 <- pairwise_termsim(xgo, method = "Wang",semData = d)
    x_plot2 <- emapplot(xgo2, showCategory = 20)
    return(x_plot2)
  }
}

# 針對 SampleList 裡的基因列表執行功能
lapply(SampleList, doGO)
----------------------------------------------------------------------------------------------

獲得下面的結果
----------------------------------------------------------------------------------------------
Error in (function (classes, fdef, mtable) :
unable to find an inherited method for function ‘pairwise_termsim’ for signature ‘"NULL"’
----------------------------------------------------------------------------------------------

感覺應該是在把 AGI 小數點後編號移除的步驟執行有問題,因為如果不把功能性分析流程寫成 function,直接用同樣的流程分析單一基因列表則可以正常獲得結果圖。

已經嘗試過把 function 裡指定 column 的符號 $ 改成 [[ ]],但仍然無法順利執行。
有人知道該如何修改嗎,感覺可能是 function() 裡指定 column 的部分沒寫好,但不確定是哪裡出錯了。
截圖 2023-08-22 下午3.43.22.png

WEPA ^_^

unread,
Aug 31, 2023, 1:11:54 AM8/31/23
to R軟體使用者論壇
Hi friend,

應該是 xgo <- enrichGO( ) 程式碼執行結果有問題, 因此在  pairwise_termsim() 函數執行時會有錯誤.

參考以下文章再修正, 或是提供可測試執行用(部分)資料集.

# end
Iris.c 在 2023年8月22日 星期二下午4:11:35 [UTC+8] 的信中寫道:
Reply all
Reply to author
Forward
0 new messages