Question: Storing a gene expression matrix in a matrix.mtx
0
Entering edit mode

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
ADD COMMENTlink 21 months ago Za • 120 • updated 21 months ago jbalberge • 80
2
Entering edit mode

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 COMMENTlink 21 months ago jbalberge • 80
Entering edit mode
0

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 REPLYlink 21 months ago
Za
• 120
Entering edit mode
0

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 REPLYlink 21 months ago
jbalberge
• 80
Entering edit mode
0

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 REPLYlink 21 months ago
Za
• 120
Entering edit mode
0

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 REPLYlink 21 months ago
Za
• 120
Entering edit mode
1

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 REPLYlink 20 months ago
jbalberge
• 80
Entering edit mode
0

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 REPLYlink 20 months ago
Za
• 120

Login before adding your answer.

Powered by the version 1.8