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

I have created a dataframe like this:

# tf-bc matrix

matrix_dir = "filtered_tf_bc_matrix"
mat = scipy.io.mmread(os.path.join(matrix_dir, "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(gzip.open(barcodes_path, 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
print(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 • 334 views
ADD COMMENT

Login before adding your answer.

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