STOmics / Stereopy

A toolkit of spatial transcriptomic analysis.
MIT License
179 stars 59 forks source link

Different cells attributes between `write_h5ad` and `read_h5ad` #282

Closed Zjianglin closed 1 month ago

Zjianglin commented 1 month ago

Hi developers,

I tried to export the integrated StereoExpData object to local file using 'write_h5ad'. However, when I load the exported object using 'read_h5ad' again, it lost some cells attributes such as 'dnbCount', 'area', 'sample'.

Here is my codes and output:

print(data)
outh5ad = os.path.join(processed_data_odir, '{}_001_integrated.h5ad'.format(projectx))
st.io.write_h5ad(data, use_raw=True, use_result=True, output=outh5ad, split_batches=False)
print('Export integrated 7 samples to {}'.format(outh5ad))

#  ==> 
StereoExpData object with n_cells X n_genes = 677302 X 18003
bin_type: cell_bins
offset_x = None
offset_y = None
cells: ['cell_name', 'dnbCount', 'area', 'sample', 'total_counts', 'n_genes_by_counts', 'pct_counts_mt', 'batch', 'leiden']
genes: ['gene_name']
cells_matrix = ['pca', 'pca_integrated', 'umap_integrated']
cells_pairwise = ['neighbors_integrated']
key_record: {'pca': ['pca', 'pca_integrated'], 'neighbors': ['neighbors_integrated'], 'umap': ['umap_integrated'], 'cluster': ['leiden'], 'gene_exp_cluster': ['gene_exp_leiden']}
result: ['pca', 'pca_integrated', 'neighbors_integrated', 'umap_integrated', 'leiden', 'gene_exp_leiden', 'pca_variance_ratio']
Export integrated 7 samples to /path/to/PJX_001_integrated.h5ad

adata2 = st.io.read_h5ad(file_path=outh5ad, flavor='stereopy')
print('Read {} from {}'.format(adata2, outh5ad))
print('outh5ad == previous data: {}'.format(adata2 == data))
# ==>
Read StereoExpData object with n_cells X n_genes = 677302 X 18003
bin_type: cell_bins
offset_x = None
offset_y = None
cells: ['cell_name', 'total_counts', 'pct_counts_mt', 'n_genes_by_counts', 'leiden']
genes: ['gene_name']
cells_matrix = ['pca', 'pca_integrated', 'umap_integrated']
cells_pairwise = ['neighbors_integrated']
key_record: {'pca': ['pca', 'pca_integrated'], 'neighbors': ['neighbors_integrated'], 'umap': ['umap_integrated'], 'cluster': ['leiden'], 'gene_exp_cluster': ['gene_exp_leiden']}
result: ['pca', 'pca_integrated', 'neighbors_integrated', 'umap_integrated', 'leiden', 'gene_exp_leiden'] from /path/to/PJX_001_integrated.h5ad
outh5ad == previous data: False

Here is my session infos:

holoviews           1.17.1
natsort             7.1.1
numpy               1.23.5
pandas              1.5.3
panel               0.14.4
seaborn             0.12.2
session_info        1.0.0
stereo              1.2.0
torch               1.10.0+cu102

How Could I keep same cells as well as genes infos during export and load? Thanks.

tanliwei-genomics-cn commented 1 month ago

Thanks for your feedback, this problem will be solved in next version.