This is a test version of Biostars. For the public version, visit https://www.biostars.org.
create a gene count matrix csv file from seurat object

Hello,

I would like to know how to create a csv file from a raw gene count sparse matrix from a seurat object.

library(Seurat)
library(tidyverse)
library(Matrix)


abc <- Read10X_h5(filename = "../input/abc_raw_feature_bc_matrix.h5")
se_obj <- CreateSeuratObject(abc, project="ABC")
count_matx <- se_obj@assays$RNA@counts
writeMM(count_matx, "../output/abc.csv")

The abc.csv file doesn't look what I wanted. I'd like to have a matrix that each row represents gene_id and each column represents cell_barcode. I'm just not sure how to create such a matrix csv file.

head abc.csv

%%MatrixMarket matrix coordinate integer general
36601 1830874 41217502
7589 1 1
36562 2 1
8286 3 1
17209 4 1
18177 4 1
count_matx |> head ()
6 x 1830874 sparse Matrix of class "dgCMatrix"
  [[ suppressing 70 column names ‘AAACCCAAGAAACCCA-1’, ‘AAACCCAAGAAACCCG-1’, ‘AAACCCAAGAAACTCA-1’ ... ]]

MIR1302-2HG . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
FAM138A     . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
OR4F5       . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
AL627309.1  . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
AL627309.3  . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
AL627309.2  . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

MIR1302-2HG ......
FAM138A     ......
OR4F5       ......
AL627309.1  ......
AL627309.3  ......
AL627309.2  ......
seurat sparsematrix

Have you tried a simple write.table with the count_matx object?

Thanks for the input. I did. The process froze in the middle of generating a csv.file.

write.csv(count_matx, "../output/abc.csv")
Warning: sparse->dense coercion: allocating vector of size 499.3 GiB

That's a sign that you should not be writing a CSV file of this magnitude. Why are you trying to get a CSV anyway?

I understand that, but unfortunately that's what I am asked to do.

Take this information to the person that asked you to do this and if they insist, figure out how to lease a high RAM machine (HPC/Cloud) and use data.table::fwrite.

In fact, you could try fwrite on your current machine instead of write.csv() and see if that works.

0 answers

No answers yet.

Log in to answer this question.