如何将包含 S4 对象的大型列表编写为 CSV 文件?

LN3*_*LN3 2 r bioinformatics r-s4

我有运行并输出一个大列表的代码。我一直坚持将输出写入文件,因为我不断收到不同的错误,因此我无法以通常用于数据帧的任何方式写入文件。

\n

我使用的代码和数据是这样的:

\n
library(GeneOverlap)\nlibrary(dplyr)\nlibrary(stringr)\n\ndataset1 <- structure(list(Gene = c("Gene1", "Gene1", "Gene2", "Gene3", "Gene3.", \n"Gene3"), Gene_count = c(5L, 5L, 3L, 16L, 16L, 16L), Phenotype = c("Phenotype1", \n"Phenotype2", "Phenotype1", "Phenotype6", "Phenotype2", "Phenotype1"\n)), row.names = c(NA, -6L), class = c("data.table", "data.frame"\n))\n\n\ndataset2 <- structure(list(Gene = c("Gene1", "Gene1", "Gene4", "Gene2", "Gene6", \n"Gene7"), Gene_count = c(10L, 10L, 4L, 17L, 3L, 2L), Phenotype = c("Phenotype1", \n"Phenotype2", "Phenotype1", "Phenotype6", "Phenotype2", "Phenotype1"\n)), row.names = c(NA, -6L), class = c("data.table", "data.frame"\n))\n\nd1_split <- split(dataset1, dataset1$Phenotype)\nd2_split <- split(dataset2, dataset2$Phenotype)\n\n# this should be TRUE in order for Map to work correctly\nall(names(d1_split) == names(d2_split))\n\ntests <- Map(function(d1, d2) {\n  go.obj <- newGeneOverlap(d1$Gene, d2$Gene, genome.size = 1871)\n  return(testGeneOverlap(go.obj))\n}, d1_split, d2_split)\n
Run Code Online (Sandbox Code Playgroud)\n

然后,我想将大型列表对象写到一个文件中 - 理想情况下将上面代码中tests每个对象的 p 值作为一列获取。Phenotype但我不断收到与以下任意一件事相关的各种错误:

\n
library(Matrix)\nlibrary(data.table)\nlstData <- Map(as.data.frame, tests)\nError in as.data.frame.default(dots[[1L]][[1L]]) : \n  cannot coerce class \xe2\x80\x98structure("GeneOverlap", package = "GeneOverlap")\xe2\x80\x99 to a data.frame\ndfrData <- rbindlist(lstData)\nError in rbindlist(lstData) : object 'lstData' not found\n
Run Code Online (Sandbox Code Playgroud)\n
Error in fwrite(tests, "list.csv") : \n  Column 1's type is 'S4' - not yet implemented in fwrite.\n
Run Code Online (Sandbox Code Playgroud)\n
library(data.table)\noutputfile <- "test.csv" #output file name\nsep <- "," #define the separator (related to format of the output file)\nfor(nam in names(tests)){\n  fwrite(list(nam), file=outputfile, sep=sep, append=T) #write names of the list elements\n  ele <- tests[[nam]]\n  if(is.list(ele)) fwrite(ele, file=outputfile, sep=sep, append=T, col.names=T) else fwrite(data.frame(matrix(ele, nrow=1)), file=outputfile, append=T) #write elements of the list\n  fwrite(list(NA), file=outputfile, append=T) #add an empty row to separate elements\n}\n\nError in as.vector(data) : \n  no method for coercing this S4 class to a vector\n
Run Code Online (Sandbox Code Playgroud)\n

我一直在尝试理解 S4 对象,但我是 R 初学者 - 我可以使用哪些函数或包来写出我的tests对象?上面包含示例数据来运行所有代码。

\n

jdo*_*res 5

GeneOverlap包具有多个用于访问测试结果统计数据的函数get*您可以将其与tidyverse结合起来创建一个整洁的结果表:

results <- tibble(pheno = names(tests), tests = tests) %>% 
  rowwise() %>% 
  mutate(
    across(tests, 
           .fns = list(tested = getTested, pval = getPval, OR = getOddsRatio, jaccard = getJaccard), 
           .names = '{.fn}')
  ) %>% 
  select(-tests) # drop test object column

  pheno      tested    pval    OR jaccard
  <chr>      <lgl>    <dbl> <dbl>   <dbl>
1 Phenotype1 TRUE   0.00481  410.   0.2  
2 Phenotype2 TRUE   0.00214 1302.   0.333
3 Phenotype6 TRUE   1          0    0    
Run Code Online (Sandbox Code Playgroud)

然后,您可以使用write_csv或类似的方法保存此数据框。