[英]How to populate bigstatsr::FBM with sqlite database for later consumption?
我是 bigstatsr package 的新手。 我有一個 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())
我有兩個問題:
提前致謝
這是您自己進行的非常好的第一次嘗試。
這里低效的是為每個樣本測試dplyr::filter(sample == current_sample)
。 我會嘗試首先使用match()
來獲取索引。 然后,有點低效的是單獨填充每一列。 正如您所說,您可以使用big_apply()
逐塊執行此操作。
big_write()
用於將 FBM 寫入某個文本文件(例如 csv)。 您在這里想要的是使用FBM()$save()
(自述文件中示例的第二行),然后在 .rds 文件(自述文件的下一行)上使用big_attach()
)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.