cellannotation / cap-anndata

BSD 3-Clause "New" or "Revised" License
4 stars 0 forks source link

Support for layers? #17

Open mtvector opened 2 months ago

mtvector commented 2 months ago

Hi there, Great work on this package!! I'm wondering if I'm missing something but I can't find any function for loading layers from an h5ad? If I'm not mistaken, it would be great to have support for layers as well as obsp and varp, which would complete your support for the anndata h5ad format. Thanks! MT

rm1113 commented 2 months ago

Hi MT,

Thanks for your interest!

You’re right, we haven't yet provided support for layers and the obsp/varp sections. We plan to add this functionality in the near future.

In the meantime, here’s a workaround example for achieving layers with CapAnnData in backed mode (though it's a bit ugly for now):

import anndata as ad
import numpy as np
from scipy.sparse import csr_matrix

# Create AnnData object with random data
adata = ad.AnnData(X=np.random.random((10, 10)))
adata.layers["dense_layer"] = np.random.random((10,10))
adata.layers["sparse_layer"] = csr_matrix(np.random.random((10,10)))
adata.write_h5ad("test.h5ad")

# Use CapAnnData to read the h5ad file
from cap_anndata import read_h5ad
from anndata.experimental import sparse_dataset

with read_h5ad("test.h5ad") as cap_adata:
    print(cap_adata.X)  # <HDF5 dataset "X": shape (10, 10), type "<f8">
    print(cap_adata._file["layers"]["dense_layer"])  # <HDF5 dataset "dense_layer": shape (10, 10), type "<f8">
    print(cap_adata._file["layers"]["sparse_layer"])  # <HDF5 group "/layers/sparse_layer" (3 members)>
    print(sparse_dataset(cap_adata._file["layers"]["sparse_layer"]))  # CSRDataset: backend hdf5, shape (10, 10), data_dtype float64
mtvector commented 2 months ago

Ok I think that makes sense, so ._file is like indexing into the h5 normally?

rm1113 commented 1 month ago

so ._file is like indexing into the h5 normally?

Yeah, exactly