Storing a gene expression matrix in a matrix.mtx
2
1
Entering edit mode
6.0 years ago
Za ▴ 140

Hi,

I have a read counts file from single cell seq from RSEM, I want to store that in a matrix.mtx format required for Seurat pipeline, how can I do that?

> head(all.counts)
              HSC1_1 HSC1_2 HSC1_3 HSC1_4 HSC1_5 HSC1_6 HSC1_7
0610005C13Rik      0      0      0      0      0      0      0
0610007P14Rik     25      0    304      0    154    550    117
0610008F07Rik      0      0      0      0      0      0      0
0610009B14Rik      0      0      0      0      0      0      0
0610009B22Rik      0      0      0      8     83    521    236
0610009D07Rik    241      0     66     60    356    173    385

to

%%MatrixMarket matrix coordinate real general
%
32738 2700 2286884
32709 1 4
32707 1 1
32706 1 10
32704 1 1
32703 1 5
32702 1 6
32700 1 10
32699 1 25
32698 1 3
32697 1 8
32527 1 1
Single cell software error • 18k views
ADD COMMENT
5
Entering edit mode
6.0 years ago
jbalberge ▴ 170

Hi! Use the Matrix library to create and write a sparse matrix:

library(Matrix)

# generate single-cell RNA seq data
gbm <- replicate(10, rpois(10, 1))
rownames(gbm) <- paste0("gene_", 1:nrow(gbm))
colnames(gbm) <- paste0("cell_",1:ncol(gbm))

# save sparse matrix
sparse.gbm <- Matrix(gbm , sparse = T )
head(sparse.gbm)
writeMM(obj = sparse.gbm, file="matrix.mtx")

# save genes and cells names
write(x = rownames(gbm), file = "genes.tsv")
write(x = colnames(gbm), file = "barcodes.tsv")

Result:

$ head matrix.mtx 
%%MatrixMarket matrix coordinate integer general
10 10 65
1 1 3
2 1 1
3 1 3
6 1 1
7 1 1
8 1 1
10 1 3
1 2 2

Jean-Baptiste

ADD COMMENT
0
Entering edit mode

Sorry, where you have used my all.counts file to produce matrix.mtx ????? In your your code there is not my matrix.mtx file

Sorry, I mean I have already a barcode and annotation file and read counts data

ADD REPLY
0
Entering edit mode

Replace gbm (=gene-barcode matrix) by all.counts :

sparse.all.counts <- Matrix(all.counts , sparse = T ) # save all.counts as a sparse matrix
head(sparse.all.counts)
writeMM(obj = sparse.all.counts, file="matrix.mtx") # write the sparse matrix in the matrix.mtx file
ADD REPLY
1
Entering edit mode

Thanks a lot

You even can't imagine that how much you have been helpful in this post and saved me from a harsh permanent error

ADD REPLY
0
Entering edit mode

Sorry,

This is head of my matrix.mtx

%%MatrixMarket matrix coordinate ***integer*** general
13406 1562 6512772
6 1 1
40 1 1

In one tutorial, the head of sparse matrix is so

%%MatrixMarket matrix coordinate ***real*** general
%
32738 2700 2286884
32709 1 4
32707 1 1
32706 1 10

If you kindly consider, mine is integer and is not real, another point mine starts with 13406 and does not go like the tutorial descending, may you please help me in figuring out how to make my spars matrix correct?

ADD REPLY
1
Entering edit mode

What do you mean correct? Isn't it correct though? Should you have any non-integer value in your matrix, it would be stored differently (real, complex) by the R library. Could be that the matrix writer from your tutorial is not the same. This line 13406 1562 6512772 tells the matrix reader that your matrix has 13406 rows, 1562 columns and 6512772 non-zeros values. The rows below are triplets of the row coordinate, column coordinate, and value.

ADD REPLY
0
Entering edit mode

Thanks a lot

As tutorial and my matrix both come from read counts of single cell seq data, my first column of my matrix is 13406 then 6, 40 and ascending but in tutorial is 32738, 32709, 32707,,,, and descending. How I can make my matrix descending?

This is the tail of tutorial matrix

122 2700 1
121 2700 1
82 2700 1

and this is the tail of my matrix

13398 1562 9
13402 1562 10
13405 1562 1
ADD REPLY
0
Entering edit mode
4.1 years ago

Another answer:

You can use write10xCounts() from DropletUtils.

Kevin

ADD COMMENT

Login before adding your answer.

Traffic: 2926 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