This is a test version of Biostars. For the public version, visit https://www.biostars.org.
convert h5ad to rds

Does anyone knows how to convert an h5ad file into an .rds file using a python script?

single-cell transcriptome

1 answer

In python (Jupyter lab), do the following

import numpy as np
import pandas as pd
import scanpy as sc
import scipy
import os
import scipy.io as io

!mkdir matrix_files

adata = sc.read_h5ad('YourData.h5ad')
adata = adata.raw.to_adata()

with open('matrix_files/barcodes.tsv', 'w') as f:
    for item in adata.obs_names:
        f.write(item + '\n')

with open('matrix_files/features.tsv', 'w') as f:
    for item in ['\t'.join([x,x,'Gene Expression']) for x in adata.var_names]:
        f.write(item + '\n')

io.mmwrite('matrix_files/matrix', adata.X.T)
!ls matrix_files
!gzip matrix_files/*
!ls matrix_files
adata.obs.to_csv('metadata.csv')

And then in R do the following:

library(Seurat)

raw_data=Read10X("matrix_files")
metadata=read.csv("metadata.csv")
gc()
data_so=CreateSeuratObject(counts = raw_data, meta.data = metadata)

Log in to answer this question.