【问题标题】:Loop saving output to matrix循环保存输出到矩阵
【发布时间】:2017-03-24 06:28:55
【问题描述】:

我正在尝试访问 NCBI SRA 数据库,在其中查询 ID 列表并将输出保存到矩阵中。

我正在使用 Bioconductor 的 sradb 包来执行此操作,现在我可以访问和查询数据库,但它真的很慢,我无法弄清楚如何保存循环输出。

文件 GPL11154_GSMs.txt 包含我感兴趣的 ID。它看起来像这样:

GSM616127
GSM616128
GSM616129
GSM663427
GSM665037

我现在拥有的内容会在每次迭代时更新结果。

#source("https://bioconductor.org/biocLite.R")
#biocLite("SRAdb")
library(SRAdb)

#connect to databasse
sqlfile <- getSRAdbFile()
sra_con <- dbConnect(SQLite(),sqlfile)


## lists all the tables in the SQLite database
sra_tables <- dbListTables(sra_con)
sra_tables


dbGetQuery(sra_con,'PRAGMA TABLE_INFO(study)')

## checking the structure of the tables
#dbListFields(sra_con,"experiment")
#dbListFields(sra_con,"run")



#read in file with sample IDs per platform
x <- scan("GPL11154_GSMs.txt", what="", sep="\n")
gsm_list <- strsplit(x, "[[:space:]]+")  # Separate elements by one or more whitepace
for (gsm in gsm_list){
  gsm_to_srr <- getSRA(search_terms = gsm, out_types = c("submission", "study", "sample","experiment", "run"), sra_con)
  print(gsm_to_srr)
  }

【问题讨论】:

    标签: r bioinformatics bioconductor ncbi


    【解决方案1】:

    lapply代替forloop,试试:

    res <- lapply(gsm_list, function(gsm){
      getSRA(search_terms = gsm,
             out_types = c("submission", "study",
                           "sample","experiment", "run"),
             sra_con) })
    

    从手册中,getSRA 应该返回一个 data.frame,所以res 对象将具有 data.frames 列表。如果我们需要将 data.frames 列表转换为一个 data.frame,请this post 了解如何操作。

    【讨论】:

    • 是的,我转换了 res.df = as.data.frame(do.call(rbind, res)) 然后保存了。它工作得很好。谢谢
    猜你喜欢
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-30
    • 1970-01-01
    相关资源
    最近更新 更多