This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Pandas dataframe to H5ad file

Hi All,

I have single cell mRNA expression matrix in a CSV file, how can I save it as AnnData (H5ad) format? I can export to the h5 format using pandas, yet it's not optimized for scRNA and support is lacking in many scRNA tools

Thanks, Roy

h5 python pandas

2 answers

See if this helps in that case: https://chanzuckerberg.github.io/scRNA-python-workshop/preprocessing/00-tabula-muris.html

Thanks, looks promising!

Meanwhile, I also came up with this:

# dataframe for annotating the observations
obs = pd.DataFrame()
obs['sample'] = df.columns
var_names = df.index
# dataframe for annotating the variables
var = pd.DataFrame(index=var_names)
# the data matrix 
X = df.T.values
adata = anndata.AnnData(X, obs=obs, var=var, dtype='int32')

output:

AnnData object with n_obs × n_vars = 10 × 24514 obs: 'sample'

Hope it's also OK

I converted the count matrix to seurat object (using seurat package) and then to h5ad. Library used is sceasy. However, it requires reticulate package. Configuration is little bit tricky. Once configured, code is straight forward convertFormat(smdf2, from="seurat", to="anndata",outFile="test.h5ad"). If you are using Matrix package, load reticulate package just before conversion.

Thanks, I actually use sceasy for similar tasks, but was hoping to do this using python

Log in to answer this question.