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




   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")]
 
           