create a gene count matrix csv file from seurat object
0
0
Entering edit mode
7 months ago
jkim ▴ 190

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 • 780 views
ADD COMMENT
0
Entering edit mode

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

ADD REPLY
0
Entering edit mode

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
ADD REPLY
1
Entering edit mode

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?

ADD REPLY
0
Entering edit mode

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

ADD REPLY
1
Entering edit mode

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.

ADD REPLY
0
Entering edit mode

Thanks! In fact, I found a function works for me. It is just unfortunate that the researcher doesn't seem to understand the dimension of the matrix...

https://rdrr.io/github/AllenInstitute/scrattch.io/man/write_dgCMatrix_csv.html

ADD REPLY

Login before adding your answer.

Traffic: 1893 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6