Pandas dataframe to H5ad file
2
0
Entering edit mode
2.7 years ago
roy.granit ▴ 880

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 • 4.9k views
ADD COMMENT
5
Entering edit mode
ADD COMMENT
0
Entering edit mode

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

ADD REPLY
1
Entering edit mode
2.7 years ago

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.

ADD COMMENT
0
Entering edit mode

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

ADD REPLY

Login before adding your answer.

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