mafeiyang / ACTINN

GNU General Public License v3.0
35 stars 21 forks source link

Loom file support? #4

Open ekernf01 opened 4 years ago

ekernf01 commented 4 years ago

It would be much easier for data analysts to access this tool if it would accept file formats readily exported from Seurat, Scanpy, or similar tools. Is there any plan to support h5ad or loom files?

For the Seurat 3 users out there, here is a snippet of R code to export a Seurat v3 object to 10X triplet format, which is a good workaround for now. This uses pipes from magrittr, objects from Seurat >=3, and sparse matrices from Matrix, but I think it has no other dependencies.

Write10X = function (object, data.dir, metadata.include = colnames(object@meta.data), 
                     slot = "counts", include_all = FALSE, ...) 
{
  dir.create(data.dir, recursive = T, showWarnings = F)
  write.table(FetchData(object, metadata.include), file.path(data.dir, 
                                                             paste0("metadata.tsv")), row.names = T, col.names = T, 
              quote = F, sep = "\t")
  raw_dge = GetAssayData(object, slot = slot, ...)
  if (!include_all) {
    raw_dge = raw_dge[, Cells(object)]
  }
  rm(object)
  gc()
  if ("Matrix" != attr(class(raw_dge), "package")) {
    warning("\nAttempting to coerce data to sparse matrix from Matrix::Matrix before export. Please check your results.\n")
    raw_dge %<>% (Matrix::Matrix(sparse = T))
  }
  add_fake_ensg = function(x) data.frame("PLACEHOLDER_FOR_ENSG" %>% 
                                           paste0(seq_along(x)), x)
  wtsv = function(X, file) write.table(X, file, sep = "\t", 
                                       quote = F, row.names = F, col.names = F)
  Matrix::writeMM(raw_dge, file = file.path(data.dir, "matrix.mtx"))
  raw_dge %>% colnames %>% wtsv(file.path(data.dir, "barcodes.tsv"))
  raw_dge %>% rownames %>% add_fake_ensg %>% wtsv(file.path(data.dir, 
                                                            "genes.tsv"))
  return()
}