Question: mat2csv matrix to csv
1
gravatar for yueli7
4 months ago by
yueli7110
China
yueli7110 wrote:

Hello, Everyone,

The raw data is coming from https://satijalab.org/seurat/v3.1/pbmc3k_tutorial.html.

pbmc3k_filtered_gene_bc_matrices.tar.gz.

It is the single-cell file: barcodes.tsv, genes.tsv, matrix.mtx.

I wanted to transfer matrix.mtx file to csv file. I tried many python mat2csv, none of them worked.

Thanks in advance for any help!

Best,

Yue

rna-seq • 620 views
ADD COMMENTlink modified 4 months ago • written 4 months ago by yueli7110
1

Please improve your post by adding more details and context. These how-to on how to ask good questions may help you:

Shorter version:

[ Please read before posting a question ] -- How To Ask A Good Question

Longer version:

How To Ask Good Questions On Technical And Scientific Forums

ADD REPLYlink written 4 months ago by h.mon29k
1

Several applications can output files with the mtx extension. Where is your file coming from?
For the Matrix Market and Harwell-Boeing formats, you have the R functions readMM() and readHB() in the Matrix package. The Matrix Market format is used for the matrix.mtx files from 10X Genomics.

ADD REPLYlink written 4 months ago by Jean-Karim Heriche22k
1

Did you try Cellranger's mat2csv?

ADD REPLYlink written 4 months ago by swbarnes27.5k

Hello, swbarnes2,

Thank you so much for your kindly suggestion!

I followed

https://support.10xgenomics.com/single-cell-gene-expression/software/pipelines/latest/output/matrices

matrix_dir = "/opt/filtered_feature_bc_matrix/"
barcode.path <- paste0(matrix_dir, "barcodes.tsv")
features.path <- paste0(matrix_dir, "features.tsv")
matrix.path <- paste0(matrix_dir, "matrix.mtx")
mat <- readMM(file = matrix.path)
Error in open.connection(file) : cannot open the connection
In addition: Warning message:
In open.connection(file) :
  cannot open file '/opt/filtered_feature_bc_matrix/matrix.mtx': No such file or directory
  
ADD REPLYlink modified 4 months ago by h.mon29k • written 4 months ago by yueli7110

Okay, but is that the right path for you?

ADD REPLYlink written 4 months ago by swbarnes27.5k

Hello, swbarnes2,

Thank you so much for your help!

ADD REPLYlink modified 4 months ago • written 4 months ago by yueli7110

Hello, sebarnes2,

I also have a question.

Thank you in advance for your great help!

Best,

Yue

$ cd /opt
$ export PATH=/opt/cellranger-3.1.0:$PATH
$ cellranger sitecheck > sitecheck.txt
bash: sitecheck.txt: Permission denied

I also tried;

$ export PATH=/opt/cellranger-3.1.0:$PATH
$ cellranger testrun --id=tiny
cellranger: command not found
ADD REPLYlink written 4 months ago by yueli7110

It works.

export PATH=/opt/cellranger-3.1.0:$PATH
vim ~/.bashrc
export PATH="~/opt/cellranger-3.1.0:$PATH"

 cellranger

/home/li/opt/cellranger-3.1.0/cellranger-cs/3.1.0/bin cellranger (3.1.0) Copyright (c) 2019 10x Genomics, Inc. All rights reserved.

ADD REPLYlink modified 4 months ago • written 4 months ago by yueli7110
2
gravatar for yueli7
4 months ago by
yueli7110
China
yueli7110 wrote:

R

library(Matrix)
matrix_dir = "/home/li/"
barcode.path <- paste0(matrix_dir, "barcodes.tsv")
features.path <- paste0(matrix_dir, "features.tsv")
matrix.path <- paste0(matrix_dir, "matrix.mtx")
mat <- readMM(file = matrix.path)
feature.names = read.delim(features.path, 
                       header = FALSE,
                       stringsAsFactors = FALSE)
barcode.names = read.delim(barcode.path, 
                       header = FALSE,
                       stringsAsFactors = FALSE)
colnames(mat) = barcode.names$V1
rownames(mat) = feature.names$V1

Python

import csv
import gzip
import os
import scipy.io
matrix_dir = "/home/li/"
mat = scipy.io.mmread(os.path.join(matrix_dir, "matrix.mtx"))
features_path = os.path.join(matrix_dir, "features.tsv")
feature_ids = [row[0] for row in csv.reader(open(features_path), delimiter="\t")]
gene_names = [row[1] for row in csv.reader(open(features_path), delimiter="\t")]
feature_types = [row[2] for row in csv.reader(open(features_path), delimiter="\t")]
barcodes_path = os.path.join(matrix_dir, "barcodes.tsv")
barcodes = [row[0] for row in csv.reader(open(barcodes_path), delimiter="\t")]

 

ADD COMMENTlink written 4 months ago by yueli7110
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1763 users visited in the last hour