How to convert dataframe properly to anndata?
Entering edit mode
8 months ago
bioinfo ▴ 150

I have created a dataframe like this:

# tf-bc matrix

matrix_dir = "filtered_tf_bc_matrix"
mat =, "matrix.mtx.gz"))

motifs_path = os.path.join(matrix_dir, "motifs.tsv")
motif_ids = [row[0] for row in csv.reader(open(motifs_path), delimiter="\t")]
motif_names = [row[1] for row in csv.reader(open(motifs_path), delimiter="\t")]
barcodes_path = os.path.join(matrix_dir, "barcodes.tsv.gz")
barcodes = [row[0] for row in csv.reader(, mode="rt"), delimiter="\t")]
matrix = pd.DataFrame.sparse.from_spmatrix(mat)
matrix.columns = barcodes
matrix.insert(loc=0, column="motif_names", value=motif_names)
#matrix.insert(loc=0, column="motif_ids", value=motif_ids)
# display matrix
# save the table as a CSV (note the CSV will be a very large file)
matrix.to_csv("mex_matrix.csv", index=False)

Then I read it as anndata like this: adata3 = ad.AnnData(matrix.T)

I have 2 issues with the anndata object. The first one is that there are no counts on the object. The second one is that when I do I see the barcodes but above it says motif names.

How can I import my file properly?

Thank you

enter image description here

single-cell python anndata • 394 views

Login before adding your answer.

Traffic: 1806 users visited in the last hour
Help About
Access RSS

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

Powered by the version 2.3.6