如何使用sqlite数据库填充bigstatsr::FBM以供以后使用?

如何使用sqlite数据库填充bigstatsr::FBM以供以后使用?,r,r-bigmemory,bigstatsr,R,R Bigmemory,Bigstatsr,我是bigstatsr软件包的新手。我有一个sqlite数据库,我想将其转换为一个包含40k行(基因)60K列(样本)的FBM矩阵,供以后使用。我找到了如何用随机值填充矩阵的示例,但我不确定用sqlite数据库中的值填充矩阵的最佳方法是什么 目前我是按顺序进行的,下面是一些模拟代码: library(bigstatsr) library(RSQLite) library(dplyr) number_genes <- 50e3 number_samples <- 70e3 larg

我是bigstatsr软件包的新手。我有一个sqlite数据库,我想将其转换为一个包含40k行(基因)60K列(样本)的FBM矩阵,供以后使用。我找到了如何用随机值填充矩阵的示例,但我不确定用sqlite数据库中的值填充矩阵的最佳方法是什么

目前我是按顺序进行的,下面是一些模拟代码:

library(bigstatsr)
library(RSQLite)
library(dplyr)

number_genes <- 50e3
number_samples <- 70e3

large_genomic_matrix <- bigstatsr::FBM(nrow = number_genes, 
                                       ncol = number_samples, 
                                       type = "double", 
                                       backingfile = "fbm_large_genomic_matrix")

# Code to get a single df at the time
database_connection <- dbConnect(RSQLite::SQLite(), "database.sqlite")


sample_index_counter <- 1

for(current_sample in vector_with_sample_names){
  
  sqlite_df <- DBI::dbListTables(conn = database_connection) %>%
    dplyr::tbl("genomic_data") %>%
    dplyr::filter(sample == current_sample) %>% 
    dplyr::collect()
  
  large_genomic_matrix[, sample_index_counter] <- sqlite_df$value
  sample_index_counter <- sample_index_counter + 1
  
}

big_write(large_genomic_matrix, "large_genomic_matrix.out", every_nrow = 1000, progress = interactive())
库(bigstatsr)
图书馆(RSQLite)
图书馆(dplyr)

number_genes这是你自己做的一次非常好的首次尝试

  • 这里效率低下的是为每个样本测试
    dplyr::filter(sample==current\u sample)
    。我会先尝试使用
    match()
    来获取索引。然后,单独填充每一列会有点效率低下。正如您所说,您可以使用
    big\u apply()
    按块执行此操作

  • big_write()
    用于将FBM写入某些文本文件(例如csv)。这里需要使用的是
    FBM()$save()
    (自述文件中示例的第二行),然后在.rds文件(自述文件的下一行)上使用
    big_attach()


  • 谢谢,我将尝试这些解决方案!