Analyze a dataset in memory#

Here, we’ll analyze the growing dataset by loading it into memory.

This is only possible if it’s not too large. If you deal with particularly large data, please read the guide on iterating over datta batches (to come).

import lamindb as ln
import lnschema_bionty as lb
import anndata as ad
💡 loaded instance: testuser1/test-scrna (lamindb 0.54.4)
ln.track()
💡 notebook imports: anndata==0.9.2 lamindb==0.54.4 lnschema_bionty==0.31.2 scanpy==1.9.5
💡 Transform(id='mfWKm8OtAzp8z8', name='Analyze a dataset in memory', short_name='scrna3', version='0', type=notebook, updated_at=2023-10-02 10:19:57, created_by_id='DzTjkKse')
💡 Run(id='dIXkxmrzM6g8V5QxuDtq', run_at=2023-10-02 10:19:57, transform_id='mfWKm8OtAzp8z8', created_by_id='DzTjkKse')
ln.Dataset.filter().df()
name description version hash reference reference_type transform_id run_id file_id initial_version_id updated_at created_by_id
id
p563WE4VtMIf6FQfXJmN My versioned scRNA-seq dataset None 1 WEFcMZxJNmMiUOFrcSTaig None None Nv48yAceNSh8z8 zfuVcVfil2w5LqYdHn2K p563WE4VtMIf6FQfXJmN None 2023-10-02 10:18:54 DzTjkKse
p563WE4VtMIf6FQfXJE4 My versioned scRNA-seq dataset None 2 0Uq1qU7xX7R6pyWN3oOT None None ManDYgmftZ8Cz8 BJevtAUNru8jYr9cO093 None p563WE4VtMIf6FQfXJmN 2023-10-02 10:19:42 DzTjkKse
dataset = ln.Dataset.filter(name="My versioned scRNA-seq dataset", version="2").one()
dataset.files.df()
storage_id key suffix accessor description version size hash hash_type transform_id run_id initial_version_id updated_at created_by_id
id
be3FZ3wa9dpwAGDafjLI 7ZVN6khD None .h5ad AnnData 10x reference adata None 660792 a2V0IgOjMRHsCeZH169UOQ md5 ManDYgmftZ8Cz8 BJevtAUNru8jYr9cO093 None 2023-10-02 10:19:35 DzTjkKse
p563WE4VtMIf6FQfXJmN 7ZVN6khD None .h5ad AnnData Conde22 None 28049505 WEFcMZxJNmMiUOFrcSTaig md5 Nv48yAceNSh8z8 zfuVcVfil2w5LqYdHn2K None 2023-10-02 10:18:54 DzTjkKse

If the dataset doesn’t consist of too many files, we can now load it into memory.

Under-the-hood, the AnnData objects are concatenated during loading.

The amount of time this takes depends on a variety of factors.

If it occurs often, one might consider storing a concatenated version of the dataset, rather than the individual pieces.

adata = dataset.load()

The default is an outer join during concatenation as in pandas:

adata
AnnData object with n_obs × n_vars = 1718 × 36508
    obs: 'cell_type', 'n_genes', 'percent_mito', 'louvain', 'donor', 'tissue', 'assay', 'file_id'
    obsm: 'X_pca', 'X_umap'

The AnnData has the reference to the individual files in the .obs annotations:

adata.obs.file_id.cat.categories
Index(['be3FZ3wa9dpwAGDafjLI', 'p563WE4VtMIf6FQfXJmN'], dtype='object')

We can easily obtain ensemble IDs for gene symbols using the look up object:

genes = lb.Gene.lookup(field="symbol")
genes.itm2a.ensembl_gene_id
'ENSG00000078596'

Let us create a plot:

import scanpy as sc

sc.pp.pca(adata, n_comps=2)
2023-10-02 10:20:01,869:INFO - Failed to extract font properties from /usr/share/fonts/truetype/noto/NotoColorEmoji.ttf: In FT2Font: Can not load face (unknown file format; error code 0x2)
2023-10-02 10:20:02,008:INFO - generated new fontManager
sc.pl.pca(
    adata,
    color=genes.itm2a.ensembl_gene_id,
    title=(
        f"{genes.itm2a.symbol} / {genes.itm2a.ensembl_gene_id} /"
        f" {genes.itm2a.description}"
    ),
    save="_itm2a",
)
WARNING: saving figure to file figures/pca_itm2a.pdf
https://d33wubrfki0l68.cloudfront.net/a16b94d52965bac89711015a9f479cef8f8ce793/6e514/_images/ce5748da790bc9e7ae953d5579b9db31f24f18e7140bef36910844e66a896392.png
file = ln.File("./figures/pca_itm2a.pdf", description="My result on ITM2A")
file.save()
file.view_flow()
https://d33wubrfki0l68.cloudfront.net/7239249ae5127b8e3d33dab2257bd291ee5b86f7/e3931/_images/c890f769c632cfdd7bfb0675e22b375daf376a0a92a3e82dec870a5d1205fac0.svg

Given the image is part of the notebook, there isn’t an actual need to save it and you can also rely on the report that you’ll create when saving the notebook via the command line via:

lamin save <notebook_path>