Skip to content

Instantly share code, notes, and snippets.

@dosorio
Created February 20, 2019 21:27
Show Gist options
  • Save dosorio/5850bad8c502e56d7ec8843e5c3aed98 to your computer and use it in GitHub Desktop.
Save dosorio/5850bad8c502e56d7ec8843e5c3aed98 to your computer and use it in GitHub Desktop.
Function to read the binary output of salmon alevin in R (single-cell RNA-seq)
readSalmonAlevin <- function(alevinFolder){
matrixFile <- paste0(alevinFolder, "/quants_mat.gz")
matrixFile <- gzfile(description = matrixFile, open = 'rb')
genes <- readLines(paste0(alevinFolder,"/quants_mat_cols.txt"))
cells <- readLines(paste0(alevinFolder,"/quants_mat_rows.txt"))
n <- length(genes)*length(cells)
counts <- readBin(matrixFile,what = 'numeric', n = n)
close.connection(matrixFile)
countMatrix <- matrix(data = counts,
nrow = length(cells),
ncol = length(genes),
byrow = TRUE)
colnames(countMatrix) <- genes
rownames(countMatrix) <- cells
require(Matrix)
countMatrix <- as(countMatrix, "sparseMatrix")
countMatrix <- t(countMatrix)
return(countMatrix)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment